test/crypto: test btstack_crypto ccm

This commit is contained in:
Matthias Ringwald 2018-10-29 22:33:01 +01:00
parent ba1e2dc4c9
commit 995faa292d
3 changed files with 81 additions and 20 deletions

View File

@ -18,23 +18,6 @@ VPATH += ${BTSTACK_ROOT}/platform/posix
VPATH += ${BTSTACK_ROOT}/3rd-party/micro-ecc
VPATH += ${BTSTACK_ROOT}/test/rijndael
COMMON = \
btstack_crypto.c \
btstack_linked_list.c \
btstack_memory.c \
btstack_memory_pool.c \
btstack_run_loop.c \
btstack_run_loop_posix.c \
hci_cmd.c \
hci_dump.c \
le_device_db_memory.c \
mock.c \
rijndael.c \
sm.c \
btstack_util.c \
COMMON_OBJ = $(COMMON:.c=.o)
MBEDTLS = \
ecp.c \
ecp_curves.c \
@ -45,7 +28,7 @@ MICROECC = \
all: aes_ccm_test aestest ecc_micro_ecc aes_cmac_test
aes_ccm_test: aes_ccm.o aes_ccm_test.o btstack_util.o hci_dump.o aes_cmac.o rijndael.o
aes_ccm_test: aes_ccm.o aes_ccm_test.o btstack_crypto.o btstack_linked_list.o hci_cmd.o btstack_util.o hci_dump.o aes_cmac.o rijndael.o mock.o
aestest: aestest.o rijndael.o
${CC} ${CFLAGS} $^ -o $@

View File

@ -1,9 +1,8 @@
#include <stdio.h>
#include <stdint.h>
#include "btstack_util.h"
#include "aes_cmac.h"
#include <errno.h>
#include "aes_ccm.h"
#include "btstack_crypto.h"
typedef uint8_t key_t[16];
@ -26,6 +25,10 @@ static int parse_hex(uint8_t * buffer, const char * hex_string){
return len;
}
static void ccm_done(void * arg){
}
static void message_24(void){
DEFINE_KEY(encryption_key, "0953fa93e7caac9638f58820220a398e");
@ -37,10 +40,22 @@ static void message_24(void){
parse_hex(plaintext, "9736e6a03401de1547118463123e5f6a17b9");
printf("%16s: ", "plaintext"); printf_hexdump(plaintext, sizeof(plaintext));
printf("Reference:\n");
uint8_t ciphertext[18+4];
bt_mesh_ccm_encrypt(encryption_key, network_nonce, plaintext, sizeof(plaintext), NULL, 0, ciphertext, 4);
printf("%16s: ", "ciphertext"); printf_hexdump(ciphertext, 18);
printf("%16s: ", "NetMIC"); printf_hexdump(&ciphertext[18], 4);
// btstack_crypto
printf("btstack_crypto:\n");
uint8_t net_mic[4];
btstack_crypto_init();
btstack_crypto_ccm_t btstack_crypto_ccm;
btstack_crypo_ccm_init(&btstack_crypto_ccm, encryption_key, network_nonce, sizeof(plaintext), 4);
btstack_crypto_ccm_encrypt_block(&btstack_crypto_ccm, sizeof(plaintext), plaintext, ciphertext, &ccm_done, NULL);
btstack_crypo_ccm_get_authentication_value(&btstack_crypto_ccm, net_mic);
printf("%16s: ", "ciphertext"); printf_hexdump(ciphertext, 18);
printf("%16s: ", "NetMIC"); printf_hexdump(net_mic, 4);
}
int main(void){

63
test/crypto/mock.c Normal file
View File

@ -0,0 +1,63 @@
#include "btstack_util.h"
#include "hci.h"
#include "aes_cmac.h"
#include "hci_dump.h"
#include <stdio.h>
static btstack_linked_list_t event_packet_handlers;
static uint8_t packet_buffer[256];
static uint16_t packet_buffer_len;
static uint8_t aes128_cyphertext[16];
void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
btstack_linked_list_add(&event_packet_handlers, (btstack_linked_item_t *) callback_handler);
}
int hci_can_send_command_packet_now(void){
return 1;
}
HCI_STATE hci_get_state(void){
return HCI_STATE_WORKING;
}
static void mock_simulate_hci_event(uint8_t * packet, uint16_t size){
static int level = 0;
// hci_dump_packet(HCI_EVENT_PACKET, 1, packet, size);
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it ,&event_packet_handlers);
while (btstack_linked_list_iterator_has_next(&it)){
btstack_packet_callback_registration_t * item = (btstack_packet_callback_registration_t *) btstack_linked_list_iterator_next(&it);
item->callback(HCI_EVENT_PACKET, 0, packet, size);
}
}
static void aes128_report_result(void){
uint8_t le_enc_result[22];
uint8_t enc1_data[] = { 0x0e, 0x14, 0x01, 0x17, 0x20, 0x00 };
memcpy (le_enc_result, enc1_data, 6);
reverse_128(aes128_cyphertext, &le_enc_result[6]);
mock_simulate_hci_event(&le_enc_result[0], sizeof(le_enc_result));
}
int hci_send_cmd(const hci_cmd_t *cmd, ...){
va_list argptr;
va_start(argptr, cmd);
uint16_t len = hci_cmd_create_from_template(packet_buffer, cmd, argptr);
va_end(argptr);
hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet_buffer, len);
// dump_packet(HCI_COMMAND_DATA_PACKET, packet_buffer, len);
packet_buffer_len = len;
if (cmd->opcode == hci_le_encrypt.opcode){
uint8_t * key_flipped = &packet_buffer[3];
uint8_t key[16];
reverse_128(key_flipped, key);
uint8_t * plaintext_flipped = &packet_buffer[19];
uint8_t plaintext[16];
reverse_128(plaintext_flipped, plaintext);
aes128_calc_cyphertext(key, plaintext, aes128_cyphertext);
aes128_report_result();
}
return 0;
}