1 #include <stdio.h> 2 #include <stdint.h> 3 #include "btstack_util.h" 4 #include "aes_cmac.h" 5 #include <errno.h> 6 #include "aes_ccm.h" 7 8 typedef uint8_t key_t[16]; 9 10 #define LOG_KEY(NAME) { printf("%16s: ", #NAME); printf_hexdump(NAME, 16); } 11 #define PARSE_KEY(NAME) { parse_hex(NAME, NAME##_string); LOG_KEY(NAME); } 12 #define DEFINE_KEY(NAME, VALUE) key_t NAME; parse_hex(NAME, VALUE); LOG_KEY(NAME); 13 14 static int parse_hex(uint8_t * buffer, const char * hex_string){ 15 int len = 0; 16 while (*hex_string){ 17 if (*hex_string == ' '){ 18 hex_string++; 19 continue; 20 } 21 int high_nibble = nibble_for_char(*hex_string++); 22 int low_nibble = nibble_for_char(*hex_string++); 23 *buffer++ = (high_nibble << 4) | low_nibble; 24 len++; 25 } 26 return len; 27 } 28 29 static void message_24(void){ 30 DEFINE_KEY(encryption_key, "0953fa93e7caac9638f58820220a398e"); 31 32 uint8_t network_nonce[13]; 33 parse_hex(network_nonce, "000307080d1234000012345677"); 34 printf("%16s: ", "network_nonce"); printf_hexdump(network_nonce, 13); 35 36 uint8_t plaintext[18]; 37 parse_hex(plaintext, "9736e6a03401de1547118463123e5f6a17b9"); 38 printf("%16s: ", "plaintext"); printf_hexdump(plaintext, sizeof(plaintext)); 39 40 uint8_t ciphertext[18+4]; 41 bt_mesh_ccm_encrypt(encryption_key, network_nonce, plaintext, sizeof(plaintext), NULL, 0, ciphertext, 4); 42 printf("%16s: ", "ciphertext"); printf_hexdump(ciphertext, 18); 43 printf("%16s: ", "NetMIC"); printf_hexdump(&ciphertext[18], 4); 44 } 45 46 int main(void){ 47 message_24(); 48 return 0; 49 } 50