diff --git a/src/ble/mesh/mesh_crypto.c b/src/ble/mesh/mesh_crypto.c new file mode 100644 index 000000000..5d53e143c --- /dev/null +++ b/src/ble/mesh/mesh_crypto.c @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2017 BlueKitchen GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holders nor the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * 4. Any redistribution, use, or modification is done solely for + * personal benefit and not for any commercial purpose or for + * monetary gain. + * + * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS + * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * Please inquire about commercial licensing options at + * contact@bluekitchen-gmbh.com + * + */ + +#define __BTSTACK_FILE__ "mesh_crypto.c" + +#include +#include +#include "mesh_crypto.h" + +// mesh k1 - might get moved to btstack_crypto and all vars go into btstack_crypto_mesh_k1_t struct +static uint8_t mesh_k1_temp[16]; +static void (* mesh_k1_callback)(void * arg); +static void * mesh_k1_arg; +static const uint8_t * mesh_k1_p; +static uint16_t mesh_k1_p_len; +static uint8_t * mesh_k1_result; + +static void mesh_k1_temp_calculated(void * arg){ + btstack_crypto_aes128_cmac_t * request = (btstack_crypto_aes128_cmac_t*) arg; + btstack_crypto_aes128_cmac_message(request, mesh_k1_temp, mesh_k1_p_len, mesh_k1_p, mesh_k1_result, mesh_k1_callback, mesh_k1_arg); +} + +void mesh_k1(btstack_crypto_aes128_cmac_t * request, const uint8_t * n, uint16_t n_len, const uint8_t * salt, + const uint8_t * p, const uint16_t p_len, uint8_t * result, void (* callback)(void * arg), void * callback_arg){ + mesh_k1_callback = callback; + mesh_k1_arg = callback_arg; + mesh_k1_p = p; + mesh_k1_p_len = p_len; + mesh_k1_result = result; + btstack_crypto_aes128_cmac_message(request, salt, n_len, n, mesh_k1_temp, mesh_k1_temp_calculated, request); +} + +// mesh k3 - might get moved to btstack_crypto and all vars go into btstack_crypto_mesh_k3_t struct +static const uint8_t mesh_k3_tag[5] = { 'i', 'd', '6', '4', 0x01}; +static uint8_t mesh_k3_temp[16]; +static uint8_t mesh_k3_result128[16]; +static void (* mesh_k3_callback)(void * arg); +static void * mesh_k3_arg; +static const uint8_t * mesh_k3_n; +static uint8_t * mesh_k3_result; + +// AES-CMAC_ZERO('smk3') +static const uint8_t mesh_salt_smk3[] = { 0x00, 0x36, 0x44, 0x35, 0x03, 0xf1, 0x95, 0xcc, 0x8a, 0x71, 0x6e, 0x13, 0x62, 0x91, 0xc3, 0x02, }; + +static void mesh_k3_result128_calculated(void * arg){ + UNUSED(arg); + memcpy(mesh_k3_result, &mesh_k3_result128[8], 8); + (*mesh_k3_callback)(mesh_k3_arg); +} +static void mesh_k3_temp_callback(void * arg){ + btstack_crypto_aes128_cmac_t * request = (btstack_crypto_aes128_cmac_t*) arg; + btstack_crypto_aes128_cmac_message(request, mesh_k3_temp, sizeof(mesh_k3_tag), mesh_k3_tag, mesh_k3_result128, mesh_k3_result128_calculated, request); +} +void mesh_k3(btstack_crypto_aes128_cmac_t * request, const uint8_t * n, uint8_t * result, void (* callback)(void * arg), void * callback_arg){ + mesh_k3_callback = callback; + mesh_k3_arg = callback_arg; + mesh_k3_n = n; + mesh_k3_result = result; + btstack_crypto_aes128_cmac_message(request, mesh_salt_smk3, 16, mesh_k3_n, mesh_k3_temp, mesh_k3_temp_callback, request); +} diff --git a/src/ble/mesh/mesh_crypto.h b/src/ble/mesh/mesh_crypto.h new file mode 100644 index 000000000..4a8fa0873 --- /dev/null +++ b/src/ble/mesh/mesh_crypto.h @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2017 BlueKitchen GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holders nor the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * 4. Any redistribution, use, or modification is done solely for + * personal benefit and not for any commercial purpose or for + * monetary gain. + * + * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS + * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * Please inquire about commercial licensing options at + * contact@bluekitchen-gmbh.com + * + */ + +/* + * provisioning.h + */ + +#ifndef __MESH_CRYPTO_H +#define __MESH_CRYPTO_H + +#include +#include "btstack_defines.h" +#include "btstack_crypto.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +/** + * Calculate mesh k1 function + */ +void mesh_k1(btstack_crypto_aes128_cmac_t * request, const uint8_t * n, uint16_t n_len, const uint8_t * salt, + const uint8_t * p, const uint16_t p_len, uint8_t * result, void (* callback)(void * arg), void * callback_arg); + +/** + * Calculate mesh k3 function + */ +void mesh_k3(btstack_crypto_aes128_cmac_t * request, const uint8_t * n, uint8_t * result, void (* callback)(void * arg), void * callback_arg); + +#ifdef __cplusplus +} /* end of extern "C" */ +#endif + +#endif diff --git a/test/mesh/Makefile b/test/mesh/Makefile index dae29eba8..5d5a26fd9 100644 --- a/test/mesh/Makefile +++ b/test/mesh/Makefile @@ -41,19 +41,19 @@ CC_UNIT = g++ CFLAGS += $(shell pkg-config libusb-1.0 --cflags) LDFLAGS += $(shell pkg-config libusb-1.0 --libs) -mesh: ${CORE_OBJ} ${COMMON_OBJ} ${SM_OBJ} provisioning.o provisioning_device.o mesh.o +mesh: ${CORE_OBJ} ${COMMON_OBJ} ${SM_OBJ} mesh_crypto.o provisioning_device.o mesh.o ${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@ -provisioner: ${CORE_OBJ} ${COMMON_OBJ} ${SM_OBJ} provisioning.o provisioning_provisioner.o provisioner.o +provisioner: ${CORE_OBJ} ${COMMON_OBJ} ${SM_OBJ} mesh_crypto.o provisioning_provisioner.o provisioner.o ${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@ sniffer: ${CORE_OBJ} ${COMMON_OBJ} ${SM_OBJ} sniffer.c ${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@ -provisioning_device_test: provisioning_device_test.cpp uECC.o provisioning.o provisioning_device.o btstack_crypto.o btstack_util.o btstack_linked_list.o mock.o rijndael.o hci_cmd.o hci_dump.o +provisioning_device_test: provisioning_device_test.cpp uECC.o mesh_crypto.o provisioning_device.o btstack_crypto.o btstack_util.o btstack_linked_list.o mock.o rijndael.o hci_cmd.o hci_dump.o ${CC_UNIT} ${CFLAGS} ${LDFLAGS} $^ -lCppUTest -lCppUTestExt -o $@ -provisioning_provisioner_test: provisioning_provisioner_test.cpp uECC.o provisioning.o provisioning_provisioner.o btstack_crypto.o btstack_util.o btstack_linked_list.o mock.o rijndael.o hci_cmd.o hci_dump.o +provisioning_provisioner_test: provisioning_provisioner_test.cpp uECC.o mesh_crypto.o provisioning_provisioner.o btstack_crypto.o btstack_util.o btstack_linked_list.o mock.o rijndael.o hci_cmd.o hci_dump.o ${CC_UNIT} ${CFLAGS} ${LDFLAGS} $^ -lCppUTest -lCppUTestExt -o $@ EXAMPLES = mesh provisioner sniffer provisioning_device_test provisioning_provisioner_test diff --git a/test/mesh/provisioning.c b/test/mesh/provisioning.c index 32a88d3e7..944833340 100644 --- a/test/mesh/provisioning.c +++ b/test/mesh/provisioning.c @@ -40,55 +40,3 @@ #include #include #include "provisioning.h" - -// mesh k1 - might get moved to btstack_crypto and all vars go into btstack_crypto_mesh_k1_t struct -static uint8_t mesh_k1_temp[16]; -static void (* mesh_k1_callback)(void * arg); -static void * mesh_k1_arg; -static const uint8_t * mesh_k1_p; -static uint16_t mesh_k1_p_len; -static uint8_t * mesh_k1_result; - -static void mesh_k1_temp_calculated(void * arg){ - btstack_crypto_aes128_cmac_t * request = (btstack_crypto_aes128_cmac_t*) arg; - btstack_crypto_aes128_cmac_message(request, mesh_k1_temp, mesh_k1_p_len, mesh_k1_p, mesh_k1_result, mesh_k1_callback, mesh_k1_arg); -} - -void mesh_k1(btstack_crypto_aes128_cmac_t * request, const uint8_t * n, uint16_t n_len, const uint8_t * salt, - const uint8_t * p, const uint16_t p_len, uint8_t * result, void (* callback)(void * arg), void * callback_arg){ - mesh_k1_callback = callback; - mesh_k1_arg = callback_arg; - mesh_k1_p = p; - mesh_k1_p_len = p_len; - mesh_k1_result = result; - btstack_crypto_aes128_cmac_message(request, salt, n_len, n, mesh_k1_temp, mesh_k1_temp_calculated, request); -} - -// mesh k3 - might get moved to btstack_crypto and all vars go into btstack_crypto_mesh_k3_t struct -static const uint8_t mesh_k3_tag[5] = { 'i', 'd', '6', '4', 0x01}; -static uint8_t mesh_k3_temp[16]; -static uint8_t mesh_k3_result128[16]; -static void (* mesh_k3_callback)(void * arg); -static void * mesh_k3_arg; -static const uint8_t * mesh_k3_n; -static uint8_t * mesh_k3_result; - -// AES-CMAC_ZERO('smk3') -static const uint8_t mesh_salt_smk3[] = { 0x00, 0x36, 0x44, 0x35, 0x03, 0xf1, 0x95, 0xcc, 0x8a, 0x71, 0x6e, 0x13, 0x62, 0x91, 0xc3, 0x02, }; - -static void mesh_k3_result128_calculated(void * arg){ - UNUSED(arg); - memcpy(mesh_k3_result, &mesh_k3_result128[8], 8); - (*mesh_k3_callback)(mesh_k3_arg); -} -static void mesh_k3_temp_callback(void * arg){ - btstack_crypto_aes128_cmac_t * request = (btstack_crypto_aes128_cmac_t*) arg; - btstack_crypto_aes128_cmac_message(request, mesh_k3_temp, sizeof(mesh_k3_tag), mesh_k3_tag, mesh_k3_result128, mesh_k3_result128_calculated, request); -} -void mesh_k3(btstack_crypto_aes128_cmac_t * request, const uint8_t * n, uint8_t * result, void (* callback)(void * arg), void * callback_arg){ - mesh_k3_callback = callback; - mesh_k3_arg = callback_arg; - mesh_k3_n = n; - mesh_k3_result = result; - btstack_crypto_aes128_cmac_message(request, mesh_salt_smk3, 16, mesh_k3_n, mesh_k3_temp, mesh_k3_temp_callback, request); -} diff --git a/test/mesh/provisioning.h b/test/mesh/provisioning.h index 0ebeaf9e4..6127c984f 100644 --- a/test/mesh/provisioning.h +++ b/test/mesh/provisioning.h @@ -79,22 +79,13 @@ extern "C" #define MESH_INPUT_OOB_NUMBER 0x04 #define MESH_INPUT_OOB_STRING 0x08 + typedef struct { uint8_t network_id[8]; uint8_t beacon_key[16]; uint32_t iv_index; } mesh_provisioning_data; -/** - * Calculate mesh k1 function - */ -void mesh_k1(btstack_crypto_aes128_cmac_t * request, const uint8_t * n, uint16_t n_len, const uint8_t * salt, - const uint8_t * p, const uint16_t p_len, uint8_t * result, void (* callback)(void * arg), void * callback_arg); - -/** - * Calculate mesh k3 function - */ -void mesh_k3(btstack_crypto_aes128_cmac_t * request, const uint8_t * n, uint8_t * result, void (* callback)(void * arg), void * callback_arg); #ifdef __cplusplus } /* end of extern "C" */ diff --git a/test/mesh/provisioning_device.c b/test/mesh/provisioning_device.c index 874818ab4..70e4cd7a1 100644 --- a/test/mesh/provisioning_device.c +++ b/test/mesh/provisioning_device.c @@ -42,6 +42,7 @@ #include #include #include "ble/mesh/pb_adv.h" +#include "ble/mesh/mesh_crypto.h" #include "classic/rfcomm.h" // for crc8 #include "btstack.h" #include "provisioning.h" diff --git a/test/mesh/provisioning_provisioner.c b/test/mesh/provisioning_provisioner.c index c9d8012ab..8d6c1c165 100644 --- a/test/mesh/provisioning_provisioner.c +++ b/test/mesh/provisioning_provisioner.c @@ -42,6 +42,7 @@ #include #include #include "ble/mesh/pb_adv.h" +#include "ble/mesh/mesh_crypto.h" #include "classic/rfcomm.h" // for crc8 #include "btstack.h" #include "provisioning.h"