1 /*
2 * Counter with CBC-MAC (CCM) with AES
3 *
4 * Copyright (c) 2010-2012, Jouni Malinen <j@w1.fi>
5 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
10 #ifdef CONFIG_IEEE80211W
11 #include "includes.h"
12
13 #include "common.h"
14 #include "aes.h"
15 #include "aes_wrap.h"
16
17
xor_aes_block(u8 * dst,const u8 * src)18 static void xor_aes_block(u8 *dst, const u8 *src)
19 {
20 u32 *d = (u32 *) dst;
21 u32 *s = (u32 *) src;
22 *d++ ^= *s++;
23 *d++ ^= *s++;
24 *d++ ^= *s++;
25 *d++ ^= *s++;
26 }
27
28
aes_ccm_auth_start(void * aes,size_t M,size_t L,const u8 * nonce,const u8 * aad,size_t aad_len,size_t plain_len,u8 * x)29 static void aes_ccm_auth_start(void *aes, size_t M, size_t L, const u8 *nonce,
30 const u8 *aad, size_t aad_len, size_t plain_len,
31 u8 *x)
32 {
33 u8 aad_buf[2 * AES_BLOCK_SIZE];
34 u8 b[AES_BLOCK_SIZE];
35
36 /* Authentication */
37 /* B_0: Flags | Nonce N | l(m) */
38 b[0] = aad_len ? 0x40 : 0 /* Adata */;
39 b[0] |= (((M - 2) / 2) /* M' */ << 3);
40 b[0] |= (L - 1) /* L' */;
41 os_memcpy(&b[1], nonce, 15 - L);
42 WPA_PUT_BE16(&b[AES_BLOCK_SIZE - L], plain_len);
43
44 wpa_hexdump_key(MSG_DEBUG, "CCM B_0", b, AES_BLOCK_SIZE);
45 aes_encrypt(aes, b, x); /* X_1 = E(K, B_0) */
46
47 if (!aad_len)
48 return;
49
50 WPA_PUT_BE16(aad_buf, aad_len);
51 os_memcpy(aad_buf + 2, aad, aad_len);
52 os_memset(aad_buf + 2 + aad_len, 0, sizeof(aad_buf) - 2 - aad_len);
53
54 xor_aes_block(aad_buf, x);
55 aes_encrypt(aes, aad_buf, x); /* X_2 = E(K, X_1 XOR B_1) */
56
57 if (aad_len > AES_BLOCK_SIZE - 2) {
58 xor_aes_block(&aad_buf[AES_BLOCK_SIZE], x);
59 /* X_3 = E(K, X_2 XOR B_2) */
60 aes_encrypt(aes, &aad_buf[AES_BLOCK_SIZE], x);
61 }
62 }
63
64
aes_ccm_auth(void * aes,const u8 * data,size_t len,u8 * x)65 static void aes_ccm_auth(void *aes, const u8 *data, size_t len, u8 *x)
66 {
67 size_t last = len % AES_BLOCK_SIZE;
68 size_t i;
69
70 for (i = 0; i < len / AES_BLOCK_SIZE; i++) {
71 /* X_i+1 = E(K, X_i XOR B_i) */
72 xor_aes_block(x, data);
73 data += AES_BLOCK_SIZE;
74 aes_encrypt(aes, x, x);
75 }
76 if (last) {
77 /* XOR zero-padded last block */
78 for (i = 0; i < last; i++)
79 x[i] ^= *data++;
80 aes_encrypt(aes, x, x);
81 }
82 }
83
84
aes_ccm_encr_start(size_t L,const u8 * nonce,u8 * a)85 static void aes_ccm_encr_start(size_t L, const u8 *nonce, u8 *a)
86 {
87 /* A_i = Flags | Nonce N | Counter i */
88 a[0] = L - 1; /* Flags = L' */
89 os_memcpy(&a[1], nonce, 15 - L);
90 }
91
92
aes_ccm_encr(void * aes,size_t L,const u8 * in,size_t len,u8 * out,u8 * a)93 static void aes_ccm_encr(void *aes, size_t L, const u8 *in, size_t len, u8 *out,
94 u8 *a)
95 {
96 size_t last = len % AES_BLOCK_SIZE;
97 size_t i;
98
99 /* crypt = msg XOR (S_1 | S_2 | ... | S_n) */
100 for (i = 1; i <= len / AES_BLOCK_SIZE; i++) {
101 WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], i);
102 /* S_i = E(K, A_i) */
103 aes_encrypt(aes, a, out);
104 xor_aes_block(out, in);
105 out += AES_BLOCK_SIZE;
106 in += AES_BLOCK_SIZE;
107 }
108 if (last) {
109 WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], i);
110 aes_encrypt(aes, a, out);
111 /* XOR zero-padded last block */
112 for (i = 0; i < last; i++)
113 *out++ ^= *in++;
114 }
115 }
116
117
aes_ccm_encr_auth(void * aes,size_t M,u8 * x,u8 * a,u8 * auth)118 static void aes_ccm_encr_auth(void *aes, size_t M, u8 *x, u8 *a, u8 *auth)
119 {
120 size_t i;
121 u8 tmp[AES_BLOCK_SIZE];
122
123 wpa_hexdump_key(MSG_DEBUG, "CCM T", x, M);
124 /* U = T XOR S_0; S_0 = E(K, A_0) */
125 WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], 0);
126 aes_encrypt(aes, a, tmp);
127 for (i = 0; i < M; i++)
128 auth[i] = x[i] ^ tmp[i];
129 wpa_hexdump_key(MSG_DEBUG, "CCM U", auth, M);
130 }
131
132
aes_ccm_decr_auth(void * aes,size_t M,u8 * a,const u8 * auth,u8 * t)133 static void aes_ccm_decr_auth(void *aes, size_t M, u8 *a, const u8 *auth, u8 *t)
134 {
135 size_t i;
136 u8 tmp[AES_BLOCK_SIZE];
137
138 wpa_hexdump_key(MSG_DEBUG, "CCM U", auth, M);
139 /* U = T XOR S_0; S_0 = E(K, A_0) */
140 WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], 0);
141 aes_encrypt(aes, a, tmp);
142 for (i = 0; i < M; i++)
143 t[i] = auth[i] ^ tmp[i];
144 wpa_hexdump_key(MSG_DEBUG, "CCM T", t, M);
145 }
146
147
148 /* AES-CCM with fixed L=2 and aad_len <= 30 assumption */
aes_ccm_ae(const u8 * key,size_t key_len,const u8 * nonce,size_t M,const u8 * plain,size_t plain_len,const u8 * aad,size_t aad_len,u8 * crypt,u8 * auth)149 int aes_ccm_ae(const u8 *key, size_t key_len, const u8 *nonce,
150 size_t M, const u8 *plain, size_t plain_len,
151 const u8 *aad, size_t aad_len, u8 *crypt, u8 *auth)
152 {
153 const size_t L = 2;
154 void *aes;
155 u8 x[AES_BLOCK_SIZE], a[AES_BLOCK_SIZE];
156
157 if (aad_len > 30 || M > AES_BLOCK_SIZE)
158 return -1;
159
160 aes = aes_encrypt_init(key, key_len);
161 if (aes == NULL)
162 return -1;
163
164 aes_ccm_auth_start(aes, M, L, nonce, aad, aad_len, plain_len, x);
165 aes_ccm_auth(aes, plain, plain_len, x);
166
167 /* Encryption */
168 aes_ccm_encr_start(L, nonce, a);
169 aes_ccm_encr(aes, L, plain, plain_len, crypt, a);
170 aes_ccm_encr_auth(aes, M, x, a, auth);
171
172 aes_encrypt_deinit(aes);
173
174 return 0;
175 }
176
177
178 /* AES-CCM with fixed L=2 and aad_len <= 30 assumption */
aes_ccm_ad(const u8 * key,size_t key_len,const u8 * nonce,size_t M,const u8 * crypt,size_t crypt_len,const u8 * aad,size_t aad_len,const u8 * auth,u8 * plain)179 int aes_ccm_ad(const u8 *key, size_t key_len, const u8 *nonce,
180 size_t M, const u8 *crypt, size_t crypt_len,
181 const u8 *aad, size_t aad_len, const u8 *auth, u8 *plain)
182 {
183 const size_t L = 2;
184 void *aes;
185 u8 x[AES_BLOCK_SIZE], a[AES_BLOCK_SIZE];
186 u8 t[AES_BLOCK_SIZE];
187
188 if (aad_len > 30 || M > AES_BLOCK_SIZE)
189 return -1;
190
191 aes = aes_encrypt_init(key, key_len);
192 if (aes == NULL)
193 return -1;
194
195 /* Decryption */
196 aes_ccm_encr_start(L, nonce, a);
197 aes_ccm_decr_auth(aes, M, a, auth, t);
198
199 /* plaintext = msg XOR (S_1 | S_2 | ... | S_n) */
200 aes_ccm_encr(aes, L, crypt, crypt_len, plain, a);
201
202 aes_ccm_auth_start(aes, M, L, nonce, aad, aad_len, crypt_len, x);
203 aes_ccm_auth(aes, plain, crypt_len, x);
204
205 aes_encrypt_deinit(aes);
206
207 if (os_memcmp_const(x, t, M) != 0) {
208 wpa_printf(MSG_DEBUG, "CCM: Auth mismatch");
209 return -1;
210 }
211
212 return 0;
213 }
214 #endif /* CONFIG_IEEE80211W */
215