xref: /btstack/test/crypto/aes_ccm_test.c (revision cb93c223bc3c718fbf6e414578af3369cbf3345f)
1 #include <stdio.h>
2 #include <stdint.h>
3 #include "btstack_util.h"
4 #include "aes_ccm.h"
5 #include "btstack_crypto.h"
6 
7 typedef uint8_t key_t[16];
8 
9 #define LOG_KEY(NAME) { printf("%16s: ", #NAME); printf_hexdump(NAME, 16); }
10 #define PARSE_KEY(NAME) { parse_hex(NAME, NAME##_string); LOG_KEY(NAME); }
11 #define DEFINE_KEY(NAME, VALUE) key_t NAME; parse_hex(NAME, VALUE); LOG_KEY(NAME);
12 
13 static int parse_hex(uint8_t * buffer, const char * hex_string){
14 	int len = 0;
15 	while (*hex_string){
16 		if (*hex_string == ' '){
17 			hex_string++;
18 			continue;
19 		}
20 		int high_nibble = nibble_for_char(*hex_string++);
21 		int low_nibble = nibble_for_char(*hex_string++);
22 		*buffer++ = (high_nibble << 4) | low_nibble;
23 		len++;
24 	}
25 	return len;
26 }
27 
28 static void ccm_done(void * arg){
29 
30 }
31 
32 static void message_24(void){
33 	DEFINE_KEY(encryption_key, "0953fa93e7caac9638f58820220a398e");
34 
35 	uint8_t network_nonce[13];
36 	parse_hex(network_nonce, "000307080d1234000012345677");
37 	printf("%16s: ", "network_nonce"); printf_hexdump(network_nonce, 13);
38 
39 	uint8_t plaintext[18];
40 	parse_hex(plaintext, "9736e6a03401de1547118463123e5f6a17b9");
41 	printf("%16s: ", "plaintext"); printf_hexdump(plaintext, sizeof(plaintext));
42 
43 	printf("Reference:\n");
44 	uint8_t ciphertext[18+4];
45 	bt_mesh_ccm_encrypt(encryption_key, network_nonce, plaintext, sizeof(plaintext), NULL, 0, ciphertext, 4);
46 	printf("%16s: ", "ciphertext"); printf_hexdump(ciphertext, 18);
47 	printf("%16s: ", "NetMIC");     printf_hexdump(&ciphertext[18], 4);
48 
49 	// btstack_crypto
50 	printf("btstack_crypto:\n");
51 	uint8_t net_mic[4];
52 	btstack_crypto_init();
53 	btstack_crypto_ccm_t btstack_crypto_ccm;
54 	btstack_crypo_ccm_init(&btstack_crypto_ccm, encryption_key, network_nonce, sizeof(plaintext), 4);
55 	btstack_crypto_ccm_encrypt_block(&btstack_crypto_ccm, sizeof(plaintext), plaintext, ciphertext, &ccm_done, NULL);
56 	btstack_crypo_ccm_get_authentication_value(&btstack_crypto_ccm, net_mic);
57 	printf("%16s: ", "ciphertext"); printf_hexdump(ciphertext, 18);
58 	printf("%16s: ", "NetMIC");     printf_hexdump(net_mic, 4);
59 }
60 
61 int main(void){
62 	message_24();
63 	return 0;
64 }
65