xref: /btstack/test/crypto/aes_cmac_test.c (revision 0561b2d8d5dba972c7daa57d5e677f7a1327edfd)
1 
2 #include <stdio.h>
3 #include <string.h>
4 #include "aes_cmac.h"
5 
6 // #include "btstack_util.h"
7 
8 typedef uint8_t sm_key24_t[3];
9 typedef uint8_t sm_key56_t[7];
10 typedef uint8_t sm_key256_t[32];
11 
12 static const char * key_string      = "2b7e1516 28aed2a6 abf71588 09cf4f3c";
13 static const char * k0_string       = "7df76b0c 1ab899b3 3e42f047 b91b546f";
14 static const char * k1_string       = "fbeed618 35713366 7c85e08f 7236a8de";
15 static const char * k2_string       = "f7ddac30 6ae266cc f90bc11e e46d513b";
16 
17 static const char * m0_string       = "";
18 static const char * cmac_m0_string  = "bb1d6929 e9593728 7fa37d12 9b756746";
19 static const char * m16_string       = "6bc1bee2 2e409f96 e93d7e11 7393172a";
20 static const char * cmac_m16_string  = "070a16b4 6b4d4144 f79bdd9d d04a287c";
21 static const char * m40_string       = "6bc1bee2 2e409f96 e93d7e11 7393172a ae2d8a57 1e03ac9c 9eb76fac 45af8e51 30c81c46 a35ce411";
22 static const char * cmac_m40_string  = "dfa66747 de9ae630 30ca3261 1497c827";
23 static const char * m64_string       = "6bc1bee2 2e409f96 e93d7e11 7393172a ae2d8a57 1e03ac9c 9eb76fac 45af8e51 30c81c46 a35ce411 e5fbc119 1a0a52ef f69f2445 df4f9b17 ad2b417b e66c3710";
24 static const char * cmac_m64_string  = "51f0bebf 7e3b9d92 fc497417 79363cfe";
25 
26 // f4
27 static const char * f4_u_string 	= "20b003d2 f297be2c 5e2c83a7 e9f9a5b9 eff49111 acf4fddb cc030148 0e359de6";
28 static const char * f4_v_string 	= "55188b3d 32f6bb9a 900afcfb eed4e72a 59cb9ac2 f19d7cfb 6b4fdd49 f47fc5fd";
29 static const char * f4_x_string 	= "d5cb8454 d177733e ffffb2ec 712baeab";
30 static const char * f4_z_string 	= "00";
31 static const char * f4_cmac_string 	= "f2c916f1 07a9bd1c f1eda1be a974872d";
32 
33 // f5
34 const char * f5_w_string       = "ec0234a3 57c8ad05 341010a6 0a397d9b 99796b13 b4f866f1 868d34f3 73bfa698";
35 const char * f5_t_string       = "3c128f20 de883288 97624bdb 8dac6989";
36 const char * f5_n1_string      = "d5cb8454 d177733e ffffb2ec 712baeab";
37 const char * f5_n2_string      = "a6e8e7cc 25a75f6e 216583f7 ff3dc4cf";
38 const char * f5_a1_string      = "00561237 37bfce";
39 const char * f5_a2_string      = "00a71370 2dcfc1";
40 const char * f5_cmac_string    = "2965f176 a1084a02 fd3f6a20 ce636e20 69867911 69d7cd23 980522b5 94750a38";
41 
42 // f6
43 const char * f6_n1_string 	 	= "d5cb8454 d177733e ffffb2ec 712baeab";
44 const char * f6_n2_string 	 	= "a6e8e7cc 25a75f6e 216583f7 ff3dc4cf";
45 const char * f6_mac_key_string 	= "2965f176 a1084a02 fd3f6a20 ce636e20";
46 const char * f6_r_string 	 	= "12a3343b b453bb54 08da42d2 0c2d0fc8";
47 const char * f6_io_cap_string 	= "010102";
48 const char * f6_a1_string 	 	= "00561237 37bfce";
49 const char * f6_a2_string 	 	= "00a71370 2dcfc1";
50 const char * f6_cmac_string 	= "e3c47398 9cd0e8c5 d26c0b09 da958f61";
51 
52 // g2
53 const char * g2_u_string 	 	= "20b003d2 f297be2c 5e2c83a7 e9f9a5b9 eff49111 acf4fddb cc030148 0e359de6";
54 const char * g2_v_string 	 	= "55188b3d 32f6bb9a 900afcfb eed4e72a 59cb9ac2 f19d7cfb 6b4fdd49 f47fc5fd";
55 const char * g2_x_string 	 	= "d5cb8454 d177733e ffffb2ec 712baeab";
56 const char * g2_y_string 	 	= "a6e8e7cc 25a75f6e 216583f7 ff3dc4cf";
57 // const char * g2_cmac_string 	= "1536d18d e3d20df9 9b7044c1 2f9ed5ba";
58 const char * g2_res_string 	 	= "2f9ed5ba";
59 
60 // h6
61 const char * h6_key_string 	    = "ec0234a3 57c8ad05 341010a6 0a397d9b";
62 const char * h6_key_id_string 	= "6c656272";
63 const char * h6_cmac_string     = "2d9ae102 e76dc91c e8d3a9e2 80b16399";
64 
65 static uint32_t big_endian_read_32( const uint8_t * buffer, int pos) {
66     return ((uint32_t) buffer[(pos)+3]) | (((uint32_t)buffer[(pos)+2]) << 8) | (((uint32_t)buffer[(pos)+1]) << 16) | (((uint32_t) buffer[pos]) << 24);
67 }
68 
69 static void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
70     buffer[pos++] = value >> 24;
71     buffer[pos++] = value >> 16;
72     buffer[pos++] = value >> 8;
73     buffer[pos++] = value;
74 }
75 
76 static void hexdump2(void *data, int size){
77     if (size <= 0) return;
78     int i;
79     for (i=0; i<size;i++){
80         printf("%02X ", ((uint8_t *)data)[i]);
81     }
82     printf("\n");
83 }
84 
85 static int nibble_for_char(char c){
86     if (c >= '0' && c <= '9') return c - '0';
87     if (c >= 'a' && c <= 'f') return c - 'a' + 10;
88     if (c >= 'A' && c <= 'F') return c - 'A' + 10;
89     return -1;
90 }
91 
92 static int parse_hex(uint8_t * buffer, const char * hex_string){
93 	int len = 0;
94 	while (*hex_string){
95 		if (*hex_string == ' '){
96 			hex_string++;
97 			continue;
98 		}
99 		int high_nibble = nibble_for_char(*hex_string++);
100 		int low_nibble = nibble_for_char(*hex_string++);
101 		*buffer++ = (high_nibble << 4) | low_nibble;
102 		len++;
103 	}
104 	return len;
105 }
106 
107 #define LOG_KEY(NAME) { printf("%16s: ", #NAME); hexdump2(NAME, 16); }
108 #define PARSE_KEY(NAME) { parse_hex(NAME, NAME##_string); LOG_KEY(NAME); }
109 #define VALIDATE_KEY(NAME) { LOG_KEY(NAME); sm_key_t test; parse_hex(test, NAME##_string); if (memcmp(NAME, test, 16)){ printf("Error calculating key\n"); } }
110 #define VALIDATE_MESSAGE(NAME) validate_message(#NAME, NAME##_string, cmac_##NAME##_string)
111 
112 
113 static void validate_message(const char * name, const char * message_string, const char * cmac_string){
114 
115 	uint8_t m[128];
116 	int len = parse_hex(m, message_string);
117 
118 	sm_key_t cmac;
119 	parse_hex(cmac, cmac_string);
120 
121 	printf("-- verify message %s, len %u:\nm:    %s\ncmac: %s\n", name, len, message_string, cmac_string);
122 
123 	sm_key_t key;
124 	parse_hex(key, key_string);
125 
126 	sm_key_t cmac_test;
127 	aes_cmac(cmac_test, key, m, len);
128 
129 	LOG_KEY(cmac_test);
130 
131 	if (memcmp(cmac_test, cmac, 16)){
132 		printf("CMAC incorrect!\n");
133 	} else {
134 		printf("CMAC correct!\n");
135 	}
136 }
137 
138 static void f4(sm_key_t res, const sm_key256_t u, const sm_key256_t v, const sm_key_t x, uint8_t z){
139 	uint8_t buffer[65];
140 	memcpy(buffer, u, 32);
141 	memcpy(buffer+32, v, 32);
142 	buffer[64] = z;
143 	// hexdump2(buffer, sizeof(buffer));
144 	aes_cmac(res, x, buffer, sizeof(buffer));
145 }
146 
147 const sm_key_t f5_salt = { 0x6C ,0x88, 0x83, 0x91, 0xAA, 0xF5, 0xA5, 0x38, 0x60, 0x37, 0x0B, 0xDB, 0x5A, 0x60, 0x83, 0xBE};
148 const uint8_t f5_key_id[] = { 0x62, 0x74, 0x6c, 0x65 };
149 const uint8_t f5_length[] = { 0x01, 0x00};
150 static void f5(sm_key256_t res, const sm_key256_t w, const sm_key_t n1, const sm_key_t n2, const sm_key56_t a1, const sm_key56_t a2){
151 	// T = AES-CMACSAL_T(W)
152 	sm_key_t t;
153 	aes_cmac(t, f5_salt, w, 32);
154 	// f5(W, N1, N2, A1, A2) = AES-CMACT (Counter = 0 || keyID || N1 || N2|| A1|| A2 || Length = 256) -- this is the MacKey
155 	uint8_t buffer[53];
156 	buffer[0] = 0;
157 	memcpy(buffer+01, f5_key_id, 4);
158 	memcpy(buffer+05, n1, 16);
159 	memcpy(buffer+21, n2, 16);
160 	memcpy(buffer+37, a1, 7);
161 	memcpy(buffer+44, a2, 7);
162 	memcpy(buffer+51, f5_length, 2);
163 	// hexdump2(buffer, sizeof(buffer));
164 	aes_cmac(res, t, buffer, sizeof(buffer));
165 	// hexdump2(res, 16);
166 	//           			|| AES-CMACT (Counter = 1 || keyID || N1 || N2|| A1|| A2 || Length = 256) -- this is the LTK
167 	buffer[0] = 1;
168 	// hexdump2(buffer, sizeof(buffer));
169 	aes_cmac(res+16, t, buffer, sizeof(buffer));
170 	// hexdump2(res+16, 16);
171 }
172 
173 // f6(W, N1, N2, R, IOcap, A1, A2) = AES-CMACW (N1 || N2 || R || IOcap || A1 || A2
174 // - W is 128 bits
175 // - N1 is 128 bits
176 // - N2 is 128 bits
177 // - R is 128 bits
178 // - IOcap is 24 bits
179 // - A1 is 56 bits
180 // - A2 is 56 bits
181 static void f6(sm_key_t res, const sm_key_t w, const sm_key_t n1, const sm_key_t n2, const sm_key_t r, const sm_key24_t io_cap, const sm_key56_t a1, const sm_key56_t a2){
182 	uint8_t buffer[65];
183 	memcpy(buffer, n1, 16);
184 	memcpy(buffer+16, n2, 16);
185 	memcpy(buffer+32, r, 16);
186 	memcpy(buffer+48, io_cap, 3);
187 	memcpy(buffer+51, a1, 7);
188 	memcpy(buffer+58, a2, 7);
189 	aes_cmac(res, w, buffer,sizeof(buffer));
190 }
191 
192 // g2(U, V, X, Y) = AES-CMACX(U || V || Y) mod 2^32
193 // - U is 256 bits
194 // - V is 256 bits
195 // - X is 128 bits
196 // - Y is 128 bits
197 static uint32_t g2(const sm_key256_t u, const sm_key256_t v, const sm_key_t x, const sm_key_t y){
198 	uint8_t buffer[80];
199 	memcpy(buffer, u, 32);
200 	memcpy(buffer+32, v, 32);
201 	memcpy(buffer+64, y, 16);
202 	sm_key_t cmac;
203 	aes_cmac(cmac, x, buffer, sizeof(buffer));
204 	return big_endian_read_32(cmac, 12);
205 }
206 
207 // h6(W, keyID) = AES-CMACW(keyID)
208 // - W is 128 bits
209 // - keyID is 32 bits
210 static void h6(sm_key_t res, const sm_key_t w, const uint32_t key_id){
211 	uint8_t key_id_buffer[4];
212 	big_endian_store_32(key_id_buffer, 0, key_id);
213 	aes_cmac(res, w, key_id_buffer, 4);
214 }
215 
216 int main(void){
217 	sm_key_t key, k0, k1, k2, zero;
218 	memset(zero, 0, 16);
219 	PARSE_KEY(key);
220 
221 	// validate subkey k0,k1,k2 generation
222 	aes128_calc_cyphertext(key, zero, k0);
223 	VALIDATE_KEY(k0);
224 	aes_cmac_calc_subkeys(k0, k1, k2);
225 	VALIDATE_KEY(k1);
226 	VALIDATE_KEY(k2);
227 
228 	// validate AES_CMAC for some messages
229 	VALIDATE_MESSAGE(m0);
230 	VALIDATE_MESSAGE(m16);
231 	VALIDATE_MESSAGE(m40);
232 	VALIDATE_MESSAGE(m64);
233 
234 	// validate f4
235 	printf("-- verify f4\n");
236 	sm_key_t f4_x, f4_cmac, f4_cmac_test;
237 	sm_key256_t f4_u, f4_v;
238 	uint8_t f4_z;
239 	parse_hex(f4_cmac, f4_cmac_string);
240 	parse_hex(f4_u, f4_u_string);
241 	parse_hex(f4_v, f4_v_string);
242 	parse_hex(f4_x, f4_x_string);
243 	parse_hex(&f4_z, f4_z_string);
244 	f4(f4_cmac_test, f4_u, f4_v, f4_x, f4_z);
245 	if (memcmp(f4_cmac_test, f4_cmac, 16)){
246 		printf("CMAC incorrect!\n");
247 	} else {
248 		printf("CMAC correct!\n");
249 	}
250 
251 	// valdiate f5
252 	printf("-- verify f5\n");
253 	sm_key_t f5_cmac, f5_mackey, f5_n1, f5_n2;
254 	sm_key56_t f5_a1, f5_a2;
255 	sm_key256_t f5_w, f5_res;
256 	uint8_t f5_z;
257 	parse_hex(f5_w, f5_w_string);
258 	parse_hex(f5_n1, f5_n1_string);
259 	parse_hex(f5_n2, f5_n2_string);
260 	parse_hex(f5_a1, f5_a1_string);
261 	parse_hex(f5_a2, f5_a2_string);
262 	f5(f5_res, f5_w, f5_n1, f5_n2, f5_a1, f5_a2);
263 	printf("MacKey:");
264 	hexdump2(f5_res, 16);
265 	printf("LTK:   ");
266 	hexdump2(f5_res+16, 16);
267 	parse_hex(f5_cmac, f5_cmac_string);
268 	if (memcmp(f5_res, f5_cmac, 16)){
269 		printf("CMAC incorrect!\n");
270 	} else {
271 		printf("CMAC correct!\n");
272 	}
273 
274 	// validate f6
275 	printf("-- verify f6\n");
276 	sm_key_t f6_cmac, f6_mac_key, f6_n1, f6_n2, f6_r, f6_res;
277 	sm_key24_t f6_io_cap;
278 	sm_key56_t f6_a1, f6_a2;
279 	uint8_t f6_z;
280 	parse_hex(f6_n1, f6_n1_string);
281 	parse_hex(f6_n2, f6_n2_string);
282 	parse_hex(f6_a1, f6_a1_string);
283 	parse_hex(f6_a2, f6_a2_string);
284 	parse_hex(f6_mac_key, f6_mac_key_string);
285 	parse_hex(f6_r, f6_r_string);
286 	parse_hex(f6_io_cap, f6_io_cap_string);
287 	f6(f6_res, f6_mac_key, f6_n1, f6_n2, f6_r, f6_io_cap, f6_a1, f6_a2);
288 	hexdump2(f6_res, 16);
289 	parse_hex(f6_cmac, f6_cmac_string);
290 	if (memcmp(f6_res, f6_cmac, 16)){
291 		printf("CMAC incorrect!\n");
292 	} else {
293 		printf("CMAC correct!\n");
294 	}
295 
296 	// validate g2
297 	printf("-- verify g2\n");
298 	sm_key_t g2_cmac, g2_x, g2_y;
299 	sm_key256_t g2_u, g2_v;
300 	parse_hex(g2_x, g2_x_string);
301 	parse_hex(g2_y, g2_y_string);
302 	parse_hex(g2_u, g2_u_string);
303 	parse_hex(g2_v, g2_v_string);
304 	uint32_t g2_test = g2(g2_u, g2_v, g2_x, g2_y);
305 	printf("%08x\n", g2_test);
306 	uint8_t g2_res_buffer[4];
307 	parse_hex(g2_res_buffer, g2_res_string);
308 	uint32_t g2_res = big_endian_read_32(g2_res_buffer, 0);
309 	if (g2_test != g2_res){
310 		printf("G2 incorrect!\n");
311 	} else {
312 		printf("G2 correct!\n");
313 	}
314 
315 	// validate h6
316 	printf("-- verify h6\n");
317 	sm_key_t h6_key, h6_res, h6_cmac;
318 	uint8_t h6_key_id_buffer[4];
319 	parse_hex(h6_key, h6_key_string);
320 	parse_hex(h6_key_id_buffer, h6_key_id_string);
321 	parse_hex(h6_cmac, h6_cmac_string);
322 	uint32_t h6_key_id = big_endian_read_32(h6_key_id_buffer, 0);
323 	h6(h6_res, h6_key, h6_key_id);
324 	hexdump2(h6_res, 16);
325 	if (memcmp(h6_res, h6_cmac, 16)){
326 		printf("CMAC incorrect!\n");
327 	} else {
328 		printf("CMAC correct!\n");
329 	}
330 }
331