Contiki-NG
ccm-star.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, Hasso-Plattner-Institut.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * AES_128-based CCM* implementation.
36  * \author
37  * Original: Konrad Krentz <konrad.krentz@gmail.com>
38  * Generified version: Justin King-Lacroix <justin.kinglacroix@gmail.com>
39  */
40 
41 #include "ccm-star.h"
42 #include "lib/aes-128.h"
43 #include <string.h>
44 
45 /* see RFC 3610 */
46 #define CCM_STAR_AUTH_FLAGS(Adata, M) ((Adata ? (1u << 6) : 0) | (((M - 2u) >> 1) << 3) | 1u)
47 #define CCM_STAR_ENCRYPTION_FLAGS 1
48 
49 /*---------------------------------------------------------------------------*/
50 static void
51 set_iv(uint8_t *iv,
52  uint8_t flags,
53  const uint8_t *nonce,
54  uint8_t counter)
55 {
56  iv[0] = flags;
57  memcpy(iv + 1, nonce, CCM_STAR_NONCE_LENGTH);
58  iv[14] = 0;
59  iv[15] = counter;
60 }
61 /*---------------------------------------------------------------------------*/
62 /* XORs the block m[pos] ... m[pos + 15] with K_{counter} */
63 static void
64 ctr_step(const uint8_t *nonce,
65  uint8_t pos,
66  uint8_t *m_and_result,
67  uint8_t m_len,
68  uint8_t counter)
69 {
70  uint8_t a[AES_128_BLOCK_SIZE];
71  uint8_t i;
72 
73  set_iv(a, CCM_STAR_ENCRYPTION_FLAGS, nonce, counter);
74  AES_128.encrypt(a);
75 
76  for(i = 0; (pos + i < m_len) && (i < AES_128_BLOCK_SIZE); i++) {
77  m_and_result[pos + i] ^= a[i];
78  }
79 }
80 /*---------------------------------------------------------------------------*/
81 static void
82 mic(const uint8_t *nonce,
83  const uint8_t *m, uint8_t m_len,
84  const uint8_t *a, uint8_t a_len,
85  uint8_t *result,
86  uint8_t mic_len)
87 {
88  uint8_t x[AES_128_BLOCK_SIZE];
89  uint8_t pos;
90  uint8_t i;
91 
92  set_iv(x, CCM_STAR_AUTH_FLAGS(a_len, mic_len), nonce, m_len);
93  AES_128.encrypt(x);
94 
95  if(a_len) {
96  x[1] = x[1] ^ a_len;
97  for(i = 2; (i - 2 < a_len) && (i < AES_128_BLOCK_SIZE); i++) {
98  x[i] ^= a[i - 2];
99  }
100 
101  AES_128.encrypt(x);
102 
103  pos = 14;
104  while(pos < a_len) {
105  for(i = 0; (pos + i < a_len) && (i < AES_128_BLOCK_SIZE); i++) {
106  x[i] ^= a[pos + i];
107  }
108  pos += AES_128_BLOCK_SIZE;
109  AES_128.encrypt(x);
110  }
111  }
112 
113  if(m_len) {
114  pos = 0;
115  while(pos < m_len) {
116  for(i = 0; (pos + i < m_len) && (i < AES_128_BLOCK_SIZE); i++) {
117  x[i] ^= m[pos + i];
118  }
119  pos += AES_128_BLOCK_SIZE;
120  AES_128.encrypt(x);
121  }
122  }
123 
124  ctr_step(nonce, 0, x, AES_128_BLOCK_SIZE, 0);
125 
126  memcpy(result, x, mic_len);
127 }
128 /*---------------------------------------------------------------------------*/
129 static void
130 ctr(const uint8_t *nonce, uint8_t *m, uint8_t m_len)
131 {
132  uint8_t pos;
133  uint8_t counter;
134 
135  pos = 0;
136  counter = 1;
137  while(pos < m_len) {
138  ctr_step(nonce, pos, m, m_len, counter++);
139  pos += AES_128_BLOCK_SIZE;
140  }
141 }
142 /*---------------------------------------------------------------------------*/
143 static void
144 set_key(const uint8_t *key)
145 {
146  AES_128.set_key(key);
147 }
148 /*---------------------------------------------------------------------------*/
149 static void
150 aead(const uint8_t* nonce,
151  uint8_t* m, uint8_t m_len,
152  const uint8_t* a, uint8_t a_len,
153  uint8_t *result, uint8_t mic_len,
154  int forward)
155 {
156  if(!forward) {
157  /* decrypt */
158  ctr(nonce, m, m_len);
159  }
160 
161  mic(nonce,
162  m, m_len,
163  a, a_len,
164  result,
165  mic_len);
166 
167  if(forward) {
168  /* encrypt */
169  ctr(nonce, m, m_len);
170  }
171 }
172 /*---------------------------------------------------------------------------*/
173 const struct ccm_star_driver ccm_star_driver = {
174  set_key,
175  aead
176 };
177 /*---------------------------------------------------------------------------*/
Structure of CCM* drivers.
Definition: ccm-star.h:56
CCM* header file.
void(* aead)(const uint8_t *nonce, uint8_t *m, uint8_t m_len, const uint8_t *a, uint8_t a_len, uint8_t *result, uint8_t mic_len, int forward)
Combines authentication and encryption.
Definition: ccm-star.h:73
AES-128.
void(* set_key)(const uint8_t *key)
Sets the key in use.
Definition: ccm-star.h:62
void(* set_key)(const uint8_t *key)
Sets the current key.
Definition: aes-128.h:62