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