att_db_util: implement att_db_util_hash_calc

test/att_db_util: add test for calc gatt database hash
This commit is contained in:
Matthias Ringwald 2020-01-09 20:02:07 +01:00
parent 73843592ce
commit a4419cc8cf
5 changed files with 110 additions and 29 deletions

View File

@ -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);
}

View File

@ -44,10 +44,7 @@
#define __ATT_DB_UTIL
#include "btstack_config.h"
#
#ifdef HAVE_MALLOC
#else
#endif
#include "btstack_crypto.h"
#include <stdint.h>
@ -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

View File

@ -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)

View File

@ -34,12 +34,6 @@
* contact@bluekitchen-gmbh.com
*
*/
// *****************************************************************************
//
// test rfcomm query tests
//
// *****************************************************************************
#include <stdint.h>
@ -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; i<size; i++){
// printf("%03u: %02x - %02x\n", i, expected[i], actual[i]);
@ -81,8 +89,24 @@ void CHECK_EQUAL_ARRAY(const uint8_t * expected, uint8_t * actual, int size){
}
// mock
extern "C"
void att_set_db(uint8_t const * db){
extern "C" {
void att_set_db(uint8_t const * db){
}
void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
}
int hci_can_send_command_packet_now(void){
return 1;
}
HCI_STATE hci_get_state(void){
return HCI_STATE_WORKING;
}
void hci_halting_defer(void){
}
int hci_send_cmd(const hci_cmd_t *cmd, ...){
printf("hci_send_cmd opcode %04x\n", cmd->opcode);
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<hash_len;i++){
uint8_t actual_byte = att_db_util_hash_get_next();
uint8_t expected_byte = gatt_database_hash_test_message[i];
// printf("%03u: %02x - %02x\n", i, expected_byte, actual_byte);
CHECK_EQUAL(expected_byte, actual_byte);
}
// calc cmac
att_db_util_hash_calc(&cmac_context, cmac_calculated, &gatt_hash_calculated, NULL);
CHECK_EQUAL_ARRAY(gatt_database_hash_expected, cmac_calculated, 16);
}
int main (int argc, const char * argv[]){

View File

@ -0,0 +1,37 @@
//
// btstack_config.h for most tests
//
#ifndef __BTSTACK_CONFIG
#define __BTSTACK_CONFIG
// Port related features
#define HAVE_MALLOC
#define HAVE_ASSERT
#define HAVE_POSIX_TIME
#define HAVE_POSIX_FILE_IO
#define HAVE_BTSTACK_STDIN
// BTstack features that can be enabled
#define ENABLE_BLE
#define ENABLE_CLASSIC
// #define ENABLE_LOG_DEBUG
#define ENABLE_LOG_ERROR
#define ENABLE_LOG_INFO
#define ENABLE_SDP_DES_DUMP
#define ENABLE_SDP_EXTRA_QUERIES
// #define ENABLE_LE_SECURE_CONNECTIONS
#define ENABLE_LE_SIGNED_WRITE
#define ENABLE_LE_PERIPHERAL
#define ENABLE_LE_CENTRAL
#define ENABLE_SDP_EXTRA_QUERIES
#define ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
#define ENABLE_SOFTWARE_AES128
// BTstack configuration. buffers, sizes, ...
#define HCI_ACL_PAYLOAD_SIZE 1024
#define HCI_INCOMING_PRE_BUFFER_SIZE 6
#define NVM_NUM_LINK_KEYS 2
#define NVM_NUM_DEVICE_DB_ENTRIES 4
#endif