xref: /btstack/test/crypto/aes_cmac_test2.cpp (revision 77fc16ac1cd75d5c4040068fe473d4ee8878d298)
1e0ff5d41SMatthias Ringwald /*
2e0ff5d41SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3e0ff5d41SMatthias Ringwald  *
4e0ff5d41SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5e0ff5d41SMatthias Ringwald  * modification, are permitted provided that the following conditions
6e0ff5d41SMatthias Ringwald  * are met:
7e0ff5d41SMatthias Ringwald  *
8e0ff5d41SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9e0ff5d41SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10e0ff5d41SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11e0ff5d41SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12e0ff5d41SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13e0ff5d41SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14e0ff5d41SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15e0ff5d41SMatthias Ringwald  *    from this software without specific prior written permission.
16e0ff5d41SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17e0ff5d41SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18e0ff5d41SMatthias Ringwald  *    monetary gain.
19e0ff5d41SMatthias Ringwald  *
20e0ff5d41SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21e0ff5d41SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22e0ff5d41SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23e0ff5d41SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24e0ff5d41SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25e0ff5d41SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26e0ff5d41SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27e0ff5d41SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28e0ff5d41SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29e0ff5d41SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30e0ff5d41SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31e0ff5d41SMatthias Ringwald  * SUCH DAMAGE.
32e0ff5d41SMatthias Ringwald  *
33e0ff5d41SMatthias Ringwald  * Please inquire about commercial licensing options at
34e0ff5d41SMatthias Ringwald  * [email protected]
35e0ff5d41SMatthias Ringwald  *
36e0ff5d41SMatthias Ringwald  */
37e0ff5d41SMatthias Ringwald 
38e0ff5d41SMatthias Ringwald 
39e0ff5d41SMatthias Ringwald #include <stdint.h>
40e0ff5d41SMatthias Ringwald #include <stdio.h>
41e0ff5d41SMatthias Ringwald #include <stdlib.h>
42e0ff5d41SMatthias Ringwald #include <string.h>
43e0ff5d41SMatthias Ringwald 
44e0ff5d41SMatthias Ringwald #include "CppUTest/TestHarness.h"
45e0ff5d41SMatthias Ringwald #include "CppUTest/CommandLineTestRunner.h"
46e0ff5d41SMatthias Ringwald 
47e0ff5d41SMatthias Ringwald #include "hci.h"
48e0ff5d41SMatthias Ringwald #include "btstack_util.h"
49e0ff5d41SMatthias Ringwald #include "bluetooth.h"
50e0ff5d41SMatthias Ringwald #include "btstack_crypto.h"
51e0ff5d41SMatthias Ringwald 
52e0ff5d41SMatthias Ringwald static btstack_crypto_aes128_cmac_t cmac_context;
53e0ff5d41SMatthias Ringwald static uint8_t cmac_calculated[16];
54e0ff5d41SMatthias Ringwald 
55e0ff5d41SMatthias Ringwald static const char key_string[]        = "2b7e1516 28aed2a6 abf71588 09cf4f3c";
56e0ff5d41SMatthias Ringwald static const char example_16_string[] = "6bc1bee2 2e409f96 e93d7e11 7393172a";
57e0ff5d41SMatthias Ringwald static const char example_40_string[] = "6bc1bee2 2e409f96 e93d7e11 7393172a ae2d8a57 1e03ac9c 9eb76fac 45af8e51 30c81c46 a35ce411";
58e0ff5d41SMatthias Ringwald static const char cmac_0_string[]     = "bb1d6929 e9593728 7fa37d12 9b756746";
59e0ff5d41SMatthias Ringwald static const char cmac_16_string[]    = "070a16b4 6b4d4144 f79bdd9d d04a287c";
60e0ff5d41SMatthias Ringwald static const char cmac_40_string[]    = "dfa66747 de9ae630 30ca3261 1497c827";
61e0ff5d41SMatthias Ringwald 
parse_hex(uint8_t * buffer,const char * hex_string)62e0ff5d41SMatthias Ringwald static int parse_hex(uint8_t * buffer, const char * hex_string){
63e0ff5d41SMatthias Ringwald     int len = 0;
64e0ff5d41SMatthias Ringwald     while (*hex_string){
65e0ff5d41SMatthias Ringwald         if (*hex_string == ' '){
66e0ff5d41SMatthias Ringwald             hex_string++;
67e0ff5d41SMatthias Ringwald             continue;
68e0ff5d41SMatthias Ringwald         }
69e0ff5d41SMatthias Ringwald         int high_nibble = nibble_for_char(*hex_string++);
70e0ff5d41SMatthias Ringwald         int low_nibble = nibble_for_char(*hex_string++);
71e0ff5d41SMatthias Ringwald         *buffer++ = (high_nibble << 4) | low_nibble;
72e0ff5d41SMatthias Ringwald         len++;
73e0ff5d41SMatthias Ringwald     }
74e0ff5d41SMatthias Ringwald     return len;
75e0ff5d41SMatthias Ringwald }
76e0ff5d41SMatthias Ringwald 
gatt_hash_calculated(void * arg)77e0ff5d41SMatthias Ringwald static void gatt_hash_calculated(void * arg){
78e0ff5d41SMatthias Ringwald     UNUSED(arg);
79e0ff5d41SMatthias Ringwald }
80e0ff5d41SMatthias Ringwald 
CHECK_EQUAL_ARRAY(const uint8_t * expected,uint8_t * actual,int size)81e0ff5d41SMatthias Ringwald void CHECK_EQUAL_ARRAY(const uint8_t * expected, uint8_t * actual, int size){
82e0ff5d41SMatthias Ringwald     for (int i=0; i<size; i++){
83e0ff5d41SMatthias Ringwald         // printf("%03u: %02x - %02x\n", i, expected[i], actual[i]);
84e0ff5d41SMatthias Ringwald         BYTES_EQUAL(expected[i], actual[i]);
85e0ff5d41SMatthias Ringwald     }
86e0ff5d41SMatthias Ringwald }
87e0ff5d41SMatthias Ringwald 
88e0ff5d41SMatthias Ringwald // mock
89e0ff5d41SMatthias Ringwald extern "C" {
hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler)90e0ff5d41SMatthias Ringwald     void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
91e0ff5d41SMatthias Ringwald     }
hci_can_send_command_packet_now(void)92*77fc16acSMatthias Ringwald     bool hci_can_send_command_packet_now(void){
93*77fc16acSMatthias Ringwald         return true;
94e0ff5d41SMatthias Ringwald     }
hci_get_state(void)95e0ff5d41SMatthias Ringwald     HCI_STATE hci_get_state(void){
96e0ff5d41SMatthias Ringwald         return HCI_STATE_WORKING;
97e0ff5d41SMatthias Ringwald     }
hci_halting_defer(void)98e0ff5d41SMatthias Ringwald     void hci_halting_defer(void){
99e0ff5d41SMatthias Ringwald     }
hci_send_cmd(const hci_cmd_t * cmd,...)100*77fc16acSMatthias Ringwald     uint8_t hci_send_cmd(const hci_cmd_t *cmd, ...){
101e0ff5d41SMatthias Ringwald         printf("hci_send_cmd opcode %04x\n", cmd->opcode);
102*77fc16acSMatthias Ringwald         return ERROR_CODE_SUCCESS;
103e0ff5d41SMatthias Ringwald     }
104e0ff5d41SMatthias Ringwald }
105e0ff5d41SMatthias Ringwald 
TEST_GROUP(AES_CMAC)106e0ff5d41SMatthias Ringwald TEST_GROUP(AES_CMAC){
107e0ff5d41SMatthias Ringwald };
108e0ff5d41SMatthias Ringwald 
TEST(AES_CMAC,CMAC_0)109e0ff5d41SMatthias Ringwald TEST(AES_CMAC,CMAC_0){
110e0ff5d41SMatthias Ringwald     uint8_t k[16];
111e0ff5d41SMatthias Ringwald     uint8_t cmac[16];
112e0ff5d41SMatthias Ringwald     parse_hex(k, key_string);
113e0ff5d41SMatthias Ringwald     parse_hex(cmac, cmac_0_string);
114e0ff5d41SMatthias Ringwald     btstack_crypto_aes128_cmac_message(&cmac_context, k, 0, NULL, cmac_calculated, gatt_hash_calculated, NULL);
115e0ff5d41SMatthias Ringwald     CHECK_EQUAL_ARRAY(cmac, cmac_calculated, 16);
116e0ff5d41SMatthias Ringwald }
117e0ff5d41SMatthias Ringwald 
TEST(AES_CMAC,CMAC_16)118e0ff5d41SMatthias Ringwald TEST(AES_CMAC,CMAC_16){
119e0ff5d41SMatthias Ringwald     uint8_t k[16];
120e0ff5d41SMatthias Ringwald     uint8_t cmac[16];
121e0ff5d41SMatthias Ringwald     uint8_t m[16];
122e0ff5d41SMatthias Ringwald     parse_hex(k, key_string);
123e0ff5d41SMatthias Ringwald     parse_hex(m, example_16_string);
124e0ff5d41SMatthias Ringwald     parse_hex(cmac, cmac_16_string);
125e0ff5d41SMatthias Ringwald     btstack_crypto_aes128_cmac_message(&cmac_context, k, 16, m, cmac_calculated, gatt_hash_calculated, NULL);
126e0ff5d41SMatthias Ringwald     CHECK_EQUAL_ARRAY(cmac, cmac_calculated, 16);
127e0ff5d41SMatthias Ringwald }
128e0ff5d41SMatthias Ringwald 
TEST(AES_CMAC,CMAC_40)129e0ff5d41SMatthias Ringwald TEST(AES_CMAC,CMAC_40){
130e0ff5d41SMatthias Ringwald     uint8_t k[16];
131e0ff5d41SMatthias Ringwald     uint8_t cmac[16];
132e0ff5d41SMatthias Ringwald     uint8_t m[40];
133e0ff5d41SMatthias Ringwald     parse_hex(k, key_string);
134e0ff5d41SMatthias Ringwald     parse_hex(m, example_40_string);
135e0ff5d41SMatthias Ringwald     parse_hex(cmac, cmac_40_string);
136e0ff5d41SMatthias Ringwald     btstack_crypto_aes128_cmac_message(&cmac_context, k, 40, m, cmac_calculated, gatt_hash_calculated, NULL);
137e0ff5d41SMatthias Ringwald     printf_hexdump(cmac_calculated, 16);
138e0ff5d41SMatthias Ringwald     CHECK_EQUAL_ARRAY(cmac, cmac_calculated, 16);
139e0ff5d41SMatthias Ringwald }
140e0ff5d41SMatthias Ringwald 
main(int argc,const char * argv[])141e0ff5d41SMatthias Ringwald int main (int argc, const char * argv[]){
142e0ff5d41SMatthias Ringwald     return CommandLineTestRunner::RunAllTests(argc, argv);
143e0ff5d41SMatthias Ringwald }
144