Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 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,
};
|