Bluetooth: Tests: Encrypted Advertising Data test

Add a new BabbleSim test that use the sample data from the Supplement to
the Bluetooth Core Specification to validate the Encrypted Advertising
Data feature implementation.

Signed-off-by: Théo Battrel <theo.battrel@nordicsemi.no>
This commit is contained in:
Théo Battrel 2023-02-27 14:41:22 +01:00 committed by Johan Hedberg
commit acfe688c4e
11 changed files with 600 additions and 0 deletions

View file

@ -0,0 +1,25 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
if(NOT DEFINED ENV{BSIM_COMPONENTS_PATH})
message(FATAL_ERROR "This test requires the BabbleSim simulator. Please set \
the environment variable BSIM_COMPONENTS_PATH to point to its \
components folder. More information can be found in \
https://babblesim.github.io/folder_structure_and_env.html")
endif()
find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
project(bsim_test_ead_css_sample_data)
target_sources(app PRIVATE
src/main.c
src/common.c
src/central.c
src/peripheral.c
)
zephyr_include_directories(
$ENV{BSIM_COMPONENTS_PATH}/libUtilv1/src/
$ENV{BSIM_COMPONENTS_PATH}/libPhyComv1/src/
)

View file

@ -0,0 +1,17 @@
CONFIG_BT=y
CONFIG_BT_CENTRAL=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_DEVICE_NAME="EAD CSS Sample Data"
CONFIG_BT_SMP=y
CONFIG_BT_EXT_ADV=y
CONFIG_BT_EAD=y
CONFIG_LOG=y
CONFIG_BT_EAD_LOG_LEVEL_DBG=y
CONFIG_BT_CTLR_ADV_DATA_LEN_MAX=191
CONFIG_BT_CTLR_SCAN_DATA_LEN_MAX=191
CONFIG_BT_TESTING=y

View file

@ -0,0 +1,109 @@
/* Copyright (c) 2023 Nordic Semiconductor ASA
* SPDX-License-Identifier: Apache-2.0
*/
#include "common.h"
extern const struct test_sample_data *sample_data;
extern int data_set;
static bool data_parse_cb(struct bt_data *data, void *user_data)
{
if (data->type == BT_DATA_ENCRYPTED_AD_DATA) {
int err;
uint8_t decrypted_payload[sample_data->size_ad_data];
struct net_buf_simple decrypted_buf;
size_t decrypted_data_size = BT_EAD_DECRYPTED_PAYLOAD_SIZE(data->data_len);
if (decrypted_data_size != sample_data->size_ad_data) {
LOG_ERR("Size of decrypted data: %d", decrypted_data_size);
LOG_ERR("Size of sample data: %d", sample_data->size_ad_data);
FAIL("Computed size of data does not match the size of the data from the "
"sample. (data set %d)\n",
data_set);
}
if (memcmp(sample_data->randomizer_little_endian, data->data,
BT_EAD_RANDOMIZER_SIZE) != 0) {
LOG_ERR("Received Randomizer: %s",
bt_hex(data->data, BT_EAD_RANDOMIZER_SIZE));
LOG_ERR("Expected Randomizer from sample: %s",
bt_hex(sample_data->randomizer_little_endian,
BT_EAD_RANDOMIZER_SIZE));
FAIL("Received Randomizer does not match the expected one.\n");
}
net_buf_simple_init_with_data(&decrypted_buf, decrypted_payload,
decrypted_data_size);
err = bt_ead_decrypt(sample_data->session_key, sample_data->iv, data->data,
data->data_len, decrypted_buf.data);
if (err != 0) {
FAIL("Error during decryption.\n");
} else if (memcmp(decrypted_buf.data, sample_data->ad_data, decrypted_data_size)) {
LOG_HEXDUMP_ERR(decrypted_buf.data, decrypted_data_size,
"Decrypted data from bt_ead_decrypt:");
LOG_HEXDUMP_ERR(sample_data->ad_data, sample_data->size_ad_data,
"Expected data from sample:");
FAIL("Decrypted AD data does not match expected sample data. (data set "
"%d)\n",
data_set);
}
LOG_HEXDUMP_DBG(decrypted_buf.data, decrypted_data_size, "Raw decrypted data: ");
bt_data_parse(&decrypted_buf, &data_parse_cb, NULL);
PASS("Central test passed. (data set %d)\n", data_set);
return false;
}
LOG_DBG("Parsed data:");
LOG_DBG("len : %d", data->data_len);
LOG_DBG("type: 0x%02x", data->type);
LOG_HEXDUMP_DBG(data->data, data->data_len, "data:");
return true;
}
static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
struct net_buf_simple *ad)
{
char addr_str[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
LOG_DBG("Device found: %s (RSSI %d)", addr_str, rssi);
bt_data_parse(ad, &data_parse_cb, NULL);
}
static void start_scan(void)
{
int err;
err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found);
if (err) {
FAIL("Scanning failed to start (err %d)\n", err);
}
LOG_DBG("Scanning successfully started");
}
void test_central(void)
{
int err;
LOG_DBG("Central device. (data set %d)", data_set);
err = bt_enable(NULL);
if (err) {
FAIL("Bluetooth init failed (err %d)\n", err);
}
LOG_DBG("Bluetooth initialized");
start_scan();
}

View file

@ -0,0 +1,39 @@
/* Copyright (c) 2023 Nordic Semiconductor ASA
* SPDX-License-Identifier: Apache-2.0
*/
#include "common.h"
#include "bs_cmd_line.h"
#define SAMPLE_DATA_SET_SIZE 2
static const struct test_sample_data *sample_data_set[] = {
&sample_data_1,
&sample_data_2,
};
BUILD_ASSERT(ARRAY_SIZE(sample_data_set) == SAMPLE_DATA_SET_SIZE);
const struct test_sample_data *sample_data;
int data_set;
void test_args_parse(int argc, char *argv[])
{
bs_args_struct_t args_struct[] = {
{
.dest = &data_set,
.type = 'i',
.name = "{1, 2}",
.option = "data-set",
.descript = "Sample data set ID",
},
};
bs_args_parse_all_cmd_line(argc, argv, args_struct);
if (data_set < 1 || data_set > SAMPLE_DATA_SET_SIZE) {
data_set = 1;
}
sample_data = sample_data_set[data_set - 1];
}

View file

@ -0,0 +1,137 @@
/* Copyright (c) 2023 Nordic Semiconductor ASA
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/bluetooth/ead.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/logging/log.h>
#include "common/bt_str.h"
#include "bs_tracing.h"
#include "bstests.h"
/**
* @brief Encrypt and authenticate the given advertising data.
*
* This is the same function as @ref bt_ead_encrypt except this one adds the @p
* randomizer parameter to let the user set the randomizer value.
*
* @note This function should only be used for testing purposes, it is only
* available when @kconfig{CONFIG_BT_TESTING} is enabled.
*
* @param[in] session_key Key of @ref BT_EAD_KEY_SIZE bytes used for the
* encryption.
* @param[in] iv Initialisation Vector used to generate the nonce. It must be
* changed each time the Session Key changes.
* @param[in] randomizer Randomizer value used to generate the nonce. The value
* is also placed in front of the encrypted advertising data.
* @param[in] payload Advertising Data to encrypt. Can be multiple advertising
* structures that are concatenated.
* @param[in] payload_size Size of the Advertising Data to encrypt.
* @param[out] encrypted_payload Encrypted Ad Data including the Randomizer and
* the MIC. Size must be at least @ref BT_EAD_RANDOMIZER_SIZE + @p
* payload_size + @ref BT_EAD_MIC_SIZE. Use @ref
* BT_EAD_ENCRYPTED_PAYLOAD_SIZE to get the right size.
*
* @retval 0 Data have been correctly encrypted and authenticated.
* @retval -EIO Error occurred during the encryption or the authentication.
* @retval -EINVAL One of the argument is a NULL pointer.
*/
int bt_test_ead_encrypt(const uint8_t session_key[BT_EAD_KEY_SIZE],
const uint8_t iv[BT_EAD_IV_SIZE],
const uint8_t randomizer[BT_EAD_RANDOMIZER_SIZE], const uint8_t *payload,
size_t payload_size, uint8_t *encrypted_payload);
#define FAIL(...) \
do { \
bst_result = Failed; \
bs_trace_error_time_line(__VA_ARGS__); \
} while (0)
#define PASS(...) \
do { \
bst_result = Passed; \
bs_trace_info_time(1, __VA_ARGS__); \
} while (0)
extern enum bst_result_t bst_result;
#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t) false
#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true)
#define GET_FLAG(flag) (bool)atomic_get(&flag)
#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false)
#define WAIT_FOR_FLAG(flag) \
while (!(bool)atomic_get(&flag)) { \
(void)k_sleep(K_MSEC(1)); \
}
LOG_MODULE_DECLARE(bt_bsim_ead_sample_data, CONFIG_BT_EAD_LOG_LEVEL);
struct test_sample_data {
const uint8_t session_key[BT_EAD_KEY_SIZE];
const uint8_t iv[BT_EAD_IV_SIZE];
const uint8_t randomizer_little_endian[BT_EAD_RANDOMIZER_SIZE];
const uint8_t *ad_data;
const size_t size_ad_data;
const uint8_t *ead;
const size_t size_ead;
};
/* Encrypted Advertising Data Set 1 (ref: Supplement to the Bluetooth Core
* Specification v11, Part A, 2.3.1)
*/
#define SIZE_SAMPLE_AD_DATA_1 20
static const uint8_t sample_ad_data_1[] = {0x0F, 0x09, 0x53, 0x68, 0x6F, 0x72, 0x74,
0x20, 0x4D, 0x69, 0x6E, 0x69, 0x2D, 0x42,
0x75, 0x73, 0x03, 0x19, 0x0A, 0x8C};
BUILD_ASSERT(sizeof(sample_ad_data_1) == SIZE_SAMPLE_AD_DATA_1);
#define SIZE_SAMPLE_EAD_1 29
static const uint8_t sample_ead_1[] = {
0x18, 0xE1, 0x57, 0xCA, 0xDE, 0x74, 0xE4, 0xDC, 0xAF, 0xDC, 0x51, 0xC7, 0x28, 0x28, 0x10,
0xC2, 0x21, 0x7F, 0x0E, 0x4C, 0xEF, 0x43, 0x43, 0x18, 0x1F, 0xBA, 0x00, 0x69, 0xCC,
};
BUILD_ASSERT(sizeof(sample_ead_1) == SIZE_SAMPLE_EAD_1);
static const struct test_sample_data sample_data_1 = {
.session_key = {0x57, 0xA9, 0xDA, 0x12, 0xD1, 0x2E, 0x6E, 0x13, 0x1E, 0x20, 0x61, 0x2A,
0xD1, 0x0A, 0x6A, 0x19},
.iv = {0x9E, 0x7A, 0x00, 0xEF, 0xB1, 0x7A, 0xE7, 0x46},
.randomizer_little_endian = {0x18, 0xE1, 0x57, 0xCA, 0xDE},
.ad_data = sample_ad_data_1,
.size_ad_data = SIZE_SAMPLE_AD_DATA_1,
.ead = sample_ead_1,
.size_ead = SIZE_SAMPLE_EAD_1,
};
/* Encrypted Advertising Data Set 2 (ref: Supplement to the Bluetooth Core
* Specification v11, Part A, 2.3.2)
*/
#define SIZE_SAMPLE_AD_DATA_2 20
static const uint8_t sample_ad_data_2[] = {0x0F, 0x09, 0x53, 0x68, 0x6F, 0x72, 0x74,
0x20, 0x4D, 0x69, 0x6E, 0x69, 0x2D, 0x42,
0x75, 0x73, 0x03, 0x19, 0x0A, 0x8C};
BUILD_ASSERT(sizeof(sample_ad_data_2) == SIZE_SAMPLE_AD_DATA_2);
#define SIZE_SAMPLE_EAD_2 29
static const uint8_t sample_ead_2[] = {0x8d, 0x1c, 0x97, 0x6e, 0x7a, 0x35, 0x44, 0x40, 0x76, 0x12,
0x57, 0x88, 0xc2, 0x38, 0xa5, 0x8e, 0x8b, 0xd9, 0xcf, 0xf0,
0xde, 0xfe, 0x25, 0x1a, 0x8e, 0x72, 0x75, 0x45, 0x4c};
BUILD_ASSERT(sizeof(sample_ead_2) == SIZE_SAMPLE_EAD_2);
static const struct test_sample_data sample_data_2 = {
.session_key = {0x57, 0xA9, 0xDA, 0x12, 0xD1, 0x2E, 0x6E, 0x13, 0x1E, 0x20, 0x61, 0x2A,
0xD1, 0x0A, 0x6A, 0x19},
.iv = {0x9E, 0x7A, 0x00, 0xEF, 0xB1, 0x7A, 0xE7, 0x46},
.randomizer_little_endian = {0x8D, 0x1C, 0x97, 0x6E, 0x7A},
.ad_data = sample_ad_data_2,
.size_ad_data = SIZE_SAMPLE_AD_DATA_2,
.ead = sample_ead_2,
.size_ead = SIZE_SAMPLE_EAD_2,
};

View file

@ -0,0 +1,65 @@
/* Copyright (c) 2023 Nordic Semiconductor ASA
* SPDX-License-Identifier: Apache-2.0
*/
#include "bs_types.h"
#include "bs_tracing.h"
#include "bstests.h"
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(bt_bsim_ead_sample_data, CONFIG_BT_EAD_LOG_LEVEL);
extern enum bst_result_t bst_result;
#define WAIT_TIME_S 20
#define WAIT_TIME (WAIT_TIME_S * 1e6)
extern void test_central(void);
extern void test_peripheral(void);
extern void test_args_parse(int argc, char *argv[]);
void test_tick(bs_time_t HW_device_time)
{
if (bst_result != Passed) {
bst_result = Failed;
bs_trace_error_time_line("Test failed (not passed after %d seconds)\n",
WAIT_TIME_S);
}
}
static void test_ead_sample_data_init(void)
{
bst_ticker_set_next_tick_absolute(WAIT_TIME);
bst_result = In_progress;
}
static const struct bst_test_instance test_def[] = {
{
.test_id = "central",
.test_descr = "Central device",
.test_post_init_f = test_ead_sample_data_init,
.test_tick_f = test_tick,
.test_main_f = test_central,
.test_args_f = test_args_parse,
},
{
.test_id = "peripheral",
.test_descr = "Peripheral device",
.test_post_init_f = test_ead_sample_data_init,
.test_tick_f = test_tick,
.test_main_f = test_peripheral,
.test_args_f = test_args_parse,
},
BSTEST_END_MARKER};
struct bst_test_list *test_encrypted_ad_data_install(struct bst_test_list *tests)
{
return bst_add_tests(tests, test_def);
}
bst_test_install_t test_installers[] = {test_encrypted_ad_data_install, NULL};
void main(void)
{
bst_main();
}

View file

@ -0,0 +1,114 @@
/* Copyright (c) 2023 Nordic Semiconductor ASA
* SPDX-License-Identifier: Apache-2.0
*/
#include "common.h"
extern const struct test_sample_data *sample_data;
extern int data_set;
static void create_adv(struct bt_le_ext_adv **adv)
{
int err;
struct bt_le_adv_param params;
memset(&params, 0, sizeof(struct bt_le_adv_param));
params.options |= BT_LE_ADV_OPT_CONNECTABLE;
params.options |= BT_LE_ADV_OPT_EXT_ADV;
params.id = BT_ID_DEFAULT;
params.sid = 0;
params.interval_min = BT_GAP_ADV_SLOW_INT_MIN;
params.interval_max = BT_GAP_ADV_SLOW_INT_MAX;
err = bt_le_ext_adv_create(&params, NULL, adv);
if (err) {
FAIL("Failed to create advertiser (%d)\n", err);
}
}
static void start_adv(struct bt_le_ext_adv *adv)
{
int err;
int32_t timeout = 0;
uint8_t num_events = 0;
struct bt_le_ext_adv_start_param start_params;
start_params.timeout = timeout;
start_params.num_events = num_events;
err = bt_le_ext_adv_start(adv, &start_params);
if (err) {
FAIL("Failed to start advertiser (%d)\n", err);
}
LOG_DBG("Advertiser started");
}
static void set_ad_data(struct bt_le_ext_adv *adv)
{
int err;
uint8_t ead[sample_data->size_ead];
struct bt_data ead_struct;
size_t size_ad_data = sample_data->size_ad_data;
size_t size_ead = BT_EAD_ENCRYPTED_PAYLOAD_SIZE(size_ad_data);
if (size_ead != sample_data->size_ead) {
LOG_ERR("Size of ead: %zu\n", size_ead);
LOG_ERR("Size of sample_ead: %zu", sample_data->size_ead);
FAIL("Computed size of encrypted data does not match the size of the encrypted "
"data from the sample. (data set %d)\n",
data_set);
}
err = bt_test_ead_encrypt(sample_data->session_key, sample_data->iv,
sample_data->randomizer_little_endian, sample_data->ad_data,
size_ad_data, ead);
if (err != 0) {
FAIL("Error during encryption.\n");
} else if (memcmp(ead, sample_data->ead, sample_data->size_ead) != 0) {
LOG_HEXDUMP_ERR(ead, size_ead, "Encrypted data from bt_ead_encrypt:");
LOG_HEXDUMP_ERR(sample_data->ead, sample_data->size_ead,
"Encrypted data from sample:");
FAIL("Encrypted AD data does not match the ones provided in the sample. (data set "
"%d)\n",
data_set);
}
LOG_HEXDUMP_DBG(ead, size_ead, "Encrypted data:");
ead_struct.data_len = size_ead;
ead_struct.type = BT_DATA_ENCRYPTED_AD_DATA;
ead_struct.data = ead;
err = bt_le_ext_adv_set_data(adv, &ead_struct, 1, NULL, 0);
if (err) {
FAIL("Failed to set advertising data (%d)\n", err);
}
PASS("Peripheral test passed. (data set %d)\n", data_set);
}
void test_peripheral(void)
{
int err;
struct bt_le_ext_adv *adv = NULL;
LOG_DBG("Peripheral device. (data set %d)", data_set);
err = bt_enable(NULL);
if (err) {
FAIL("Bluetooth init failed (err %d)\n", err);
}
LOG_DBG("Bluetooth initialized");
create_adv(&adv);
start_adv(adv);
set_ad_data(adv);
}

View file

@ -0,0 +1,11 @@
#!/bin/env bash
# Copyright 2023 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
set -eu
bash_source_dir="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
source "${bash_source_dir}/_env.sh"
west build -b nrf52_bsim && \
cp -v build/zephyr/zephyr.exe "${test_exe}"

View file

@ -0,0 +1,15 @@
#!/bin/env bash
# Copyright 2023 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
set -eu
bash_source_dir="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
: "${BSIM_OUT_PATH:?BSIM_OUT_PATH must be defined}"
test_name="$(basename "$(realpath "$bash_source_dir/..")")"
bsim_bin="${BSIM_OUT_PATH}/bin"
verbosity_level=2
BOARD="${BOARD:-nrf52_bsim}"
simulation_id="$test_name"
test_exe="${bsim_bin}/bs_${BOARD}_tests_bsim_bluetooth_host_adv_encrypted_${test_name}_prj_conf"

View file

@ -0,0 +1,67 @@
#!/bin/env bash
# Copyright 2023 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
set -eu
bash_source_dir="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
# Read variable definitions output by _env.sh
source "${bash_source_dir}/_env.sh"
process_ids=""
exit_code=0
function Execute() {
if [ ! -f $1 ]; then
echo -e " \e[91m$(pwd)/$(basename $1) cannot be found (did you forget to\
compile it?)\e[39m"
exit 1
fi
timeout 30 $@ &
process_ids="$process_ids $!"
}
: "${BSIM_OUT_PATH:?BSIM_OUT_PATH must be defined}"
cd ${BSIM_OUT_PATH}/bin
data_set=1
Execute "$test_exe" \
-v=${verbosity_level} -s="${simulation_id}_${data_set}" -d=0 -testid=central \
-RealEncryption=1 -argstest data-set="${data_set}"
Execute "$test_exe" \
-v=${verbosity_level} -s="${simulation_id}_${data_set}" -d=1 -testid=peripheral \
-RealEncryption=1 -argstest data-set="${data_set}"
Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s="${simulation_id}_${data_set}" \
-D=2 -sim_length=60e6 $@
for process_id in $process_ids; do
wait $process_id || let "exit_code=$?"
done
process_ids=''
data_set=2
Execute "$test_exe" \
-v=${verbosity_level} -s="${simulation_id}_${data_set}" -d=0 -testid=central \
-RealEncryption=1 -argstest data-set="${data_set}"
Execute "$test_exe" \
-v=${verbosity_level} -s="${simulation_id}_${data_set}" -d=1 -testid=peripheral \
-RealEncryption=1 -argstest data-set="${data_set}"
Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s="${simulation_id}_${data_set}" \
-D=2 -sim_length=60e6 $@
for process_id in $process_ids; do
wait $process_id || let "exit_code=$?"
done
if [ ${exit_code} -ne 0 ]; then
exit_code=1
fi
exit $exit_code # the last exit code != 0

View file

@ -24,6 +24,7 @@ app=tests/bsim/bluetooth/host/adv/resume conf_file=prj_2.conf compile
app=tests/bsim/bluetooth/host/adv/chain compile
app=tests/bsim/bluetooth/host/adv/periodic compile
app=tests/bsim/bluetooth/host/adv/periodic conf_file=prj_long_data.conf compile
app=tests/bsim/bluetooth/host/adv/encrypted/css_sample_data compile
app=tests/bsim/bluetooth/host/att/eatt conf_file=prj_collision.conf compile
app=tests/bsim/bluetooth/host/att/eatt conf_file=prj_multiple_conn.conf compile