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