From a4419cc8cf9456e33cdb8cf7405e185a4850a286 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Thu, 9 Jan 2020 20:02:07 +0100 Subject: [PATCH] att_db_util: implement att_db_util_hash_calc test/att_db_util: add test for calc gatt database hash --- src/ble/att_db_util.c | 11 ++++++ src/ble/att_db_util.h | 14 +++++-- test/att_db/Makefile | 10 ++++- test/att_db/att_db_util_test.c | 67 ++++++++++++++++++++++------------ test/att_db/btstack_config.h | 37 +++++++++++++++++++ 5 files changed, 110 insertions(+), 29 deletions(-) create mode 100644 test/att_db/btstack_config.h diff --git a/src/ble/att_db_util.c b/src/ble/att_db_util.c index 0a681cf94..d7eec6095 100644 --- a/src/ble/att_db_util.c +++ b/src/ble/att_db_util.c @@ -369,3 +369,14 @@ uint8_t att_db_util_hash_get_next(void){ } return next; } + +static uint8_t att_db_util_hash_get(uint16_t offset){ + UNUSED(offset); + return att_db_util_hash_get_next(); +} + +void att_db_util_hash_calc(btstack_crypto_aes128_cmac_t * request, uint8_t * db_hash, void (* callback)(void * arg), void * callback_arg){ + static const uint8_t zero_key[16] = { 0 }; + att_db_util_hash_init(); + btstack_crypto_aes128_cmac_generator(request, zero_key, att_db_hash_len, &att_db_util_hash_get, db_hash, callback, callback_arg); +} diff --git a/src/ble/att_db_util.h b/src/ble/att_db_util.h index 7b600bfe5..60955b740 100644 --- a/src/ble/att_db_util.h +++ b/src/ble/att_db_util.h @@ -44,10 +44,7 @@ #define __ATT_DB_UTIL #include "btstack_config.h" -# -#ifdef HAVE_MALLOC -#else -#endif +#include "btstack_crypto.h" #include @@ -176,6 +173,15 @@ void att_db_util_hash_init(void); */ uint8_t att_db_util_hash_get_next(void); +/** + * @brief Calculate GATT Database Hash using crypto engine + * @param request + * @param db_hash + * @param callback + * @param callback_arg + */ +void att_db_util_hash_calc(btstack_crypto_aes128_cmac_t * request, uint8_t * db_hash, void (* callback)(void * arg), void * callback_arg); + /* API_END */ #if defined __cplusplus diff --git a/test/att_db/Makefile b/test/att_db/Makefile index f6ac0f820..1c9c20152 100644 --- a/test/att_db/Makefile +++ b/test/att_db/Makefile @@ -5,13 +5,15 @@ CC = g++ BTSTACK_ROOT = ../.. CFLAGS = -g -Wall \ - -I.. \ + -I. \ -I${BTSTACK_ROOT}/src - +CFLAGS += -I${BTSTACK_ROOT}/3rd-party/rijndael + CFLAGS += -fprofile-arcs -ftest-coverage -fsanitize=address LDFLAGS += -lCppUTest -lCppUTestExt +VPATH += ${BTSTACK_ROOT}/3rd-party/rijndael VPATH += ${BTSTACK_ROOT}/src VPATH += ${BTSTACK_ROOT}/src/ble VPATH += ${BTSTACK_ROOT}/platform/posix @@ -20,6 +22,10 @@ COMMON = \ btstack_util.c \ hci_dump.c \ att_db_util.c \ + btstack_crypto.c \ + btstack_linked_list.c \ + hci_cmd.c \ + rijndael.c COMMON_OBJ = $(COMMON:.c=.o) diff --git a/test/att_db/att_db_util_test.c b/test/att_db/att_db_util_test.c index 1b8ebffcc..4d49870c1 100644 --- a/test/att_db/att_db_util_test.c +++ b/test/att_db/att_db_util_test.c @@ -34,12 +34,6 @@ * contact@bluekitchen-gmbh.com * */ - -// ***************************************************************************** -// -// test rfcomm query tests -// -// ***************************************************************************** #include @@ -50,13 +44,19 @@ #include "CppUTest/TestHarness.h" #include "CppUTest/CommandLineTestRunner.h" +#include "hci.h" #include "ble/att_db.h" #include "ble/att_db_util.h" #include "btstack_util.h" #include "bluetooth.h" - + +#include "btstack_crypto.h" + #include "att_db_util_test.h" +static btstack_crypto_aes128_cmac_t cmac_context; +static uint8_t cmac_calculated[16]; + // 0000FF10-0000-1000-8000-00805F9B34FB uint8_t counter_service_uuid[] = { 0x00, 0x00, 0xFF, 0x10, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; // 0000FF11-0000-1000-8000-00805F9B34FB @@ -64,15 +64,23 @@ uint8_t counter_characteristic_uuid[] = { 0x00, 0x00, 0xFF, 0x11, 0x00, 0x00, 0x // gatt database hash test message static const uint8_t gatt_database_hash_test_message[] = { - 0x01, 0x00, 0x00, 0x28, 0x00, 0x18, 0x02, 0x00, 0x03, 0x28, 0x0A, 0x03, 0x00, 0x00, 0x2A, 0x04, - 0x00, 0x03, 0x28, 0x02, 0x05, 0x00, 0x01, 0x2A, 0x06, 0x00, 0x00, 0x28, 0x01, 0x18, 0x07, 0x00, - 0x03, 0x28, 0x20, 0x08, 0x00, 0x05, 0x2A, 0x09, 0x00, 0x02, 0x29, 0x0A, 0x00, 0x03, 0x28, 0x0A, - 0x0B, 0x00, 0x29, 0x2B, 0x0C, 0x00, 0x03, 0x28, 0x02, 0x0D, 0x00, 0x2A, 0x2B, 0x0E, 0x00, 0x00, - 0x28, 0x08, 0x18, 0x0F, 0x00, 0x02, 0x28, 0x14, 0x00, 0x16, 0x00, 0x0F, 0x18, 0x10, 0x00, 0x03, - 0x28, 0xA2, 0x11, 0x00, 0x18, 0x2A, 0x12, 0x00, 0x02, 0x29, 0x13, 0x00, 0x00, 0x29, 0x00, 0x00, - 0x14, 0x00, 0x01, 0x28, 0x0F, 0x18, 0x15, 0x00, 0x03, 0x28, 0x02, 0x16, 0x00, 0x19, 0x2A + 0x01, 0x00, 0x00, 0x28, 0x00, 0x18, 0x02, 0x00, 0x03, 0x28, 0x0A, 0x03, 0x00, 0x00, 0x2A, 0x04, + 0x00, 0x03, 0x28, 0x02, 0x05, 0x00, 0x01, 0x2A, 0x06, 0x00, 0x00, 0x28, 0x01, 0x18, 0x07, 0x00, + 0x03, 0x28, 0x20, 0x08, 0x00, 0x05, 0x2A, 0x09, 0x00, 0x02, 0x29, 0x0A, 0x00, 0x03, 0x28, 0x0A, + 0x0B, 0x00, 0x29, 0x2B, 0x0C, 0x00, 0x03, 0x28, 0x02, 0x0D, 0x00, 0x2A, 0x2B, 0x0E, 0x00, 0x00, + 0x28, 0x08, 0x18, 0x0F, 0x00, 0x02, 0x28, 0x14, 0x00, 0x16, 0x00, 0x0F, 0x18, 0x10, 0x00, 0x03, + 0x28, 0xA2, 0x11, 0x00, 0x18, 0x2A, 0x12, 0x00, 0x02, 0x29, 0x13, 0x00, 0x00, 0x29, 0x00, 0x00, + 0x14, 0x00, 0x01, 0x28, 0x0F, 0x18, 0x15, 0x00, 0x03, 0x28, 0x02, 0x16, 0x00, 0x19, 0x2A }; +static const uint8_t gatt_database_hash_expected[] = { + 0xF1, 0xCA, 0x2D, 0x48, 0xEC, 0xF5, 0x8B, 0xAC, 0x8A, 0x88, 0x30, 0xBB, 0xB9, 0xFB, 0xA9, 0x90 +}; + +static void gatt_hash_calculated(void * arg){ + UNUSED(arg); +} + void CHECK_EQUAL_ARRAY(const uint8_t * expected, uint8_t * actual, int size){ for (int i=0; iopcode); + return 0; + } } TEST_GROUP(AttDbUtil){ @@ -91,7 +115,6 @@ TEST_GROUP(AttDbUtil){ } }; - TEST(AttDbUtil, LeCounterDb){ att_db_util_add_service_uuid16(GAP_SERVICE_UUID); att_db_util_add_characteristic_uuid16(GAP_DEVICE_NAME_UUID, ATT_PROPERTY_READ, ATT_SECURITY_NONE, ATT_SECURITY_NONE, (uint8_t*)"SPP+LE Counter", 14); @@ -132,11 +155,6 @@ TEST(AttDbUtil, GattHash){ att_db_util_add_secondary_service_uuid16(0x180f); att_db_util_add_characteristic_uuid16(0x2a19, ATT_PROPERTY_READ, ATT_SECURITY_NONE, ATT_SECURITY_NONE, (uint8_t*)battery_level, sizeof(battery_level)); - // uint8_t * addr = att_db_util_get_address(); - // uint16_t size = att_db_util_get_size(); - // printf("HASH DB %p, len %u\n", addr, size); - // printf_hexdump(addr, size); - uint16_t hash_len = att_db_util_hash_len(); CHECK_EQUAL(sizeof(gatt_database_hash_test_message), hash_len); @@ -145,9 +163,12 @@ TEST(AttDbUtil, GattHash){ for (i=0;i