1 /* Copyright (c) 2023 Nordic Semiconductor ASA 2 * SPDX-License-Identifier: Apache-2.0 3 */ 4 5 #include <zephyr/kernel.h> 6 7 #include <zephyr/bluetooth/ead.h> 8 #include <zephyr/bluetooth/bluetooth.h> 9 10 #include <zephyr/logging/log.h> 11 12 #include "common/bt_str.h" 13 14 #include "bs_tracing.h" 15 #include "bstests.h" 16 17 /** 18 * @brief Encrypt and authenticate the given advertising data. 19 * 20 * This is the same function as @ref bt_ead_encrypt except this one adds the @p 21 * randomizer parameter to let the user set the randomizer value. 22 * 23 * @note This function should only be used for testing purposes, it is only 24 * available when @kconfig{CONFIG_BT_TESTING} is enabled. 25 * 26 * @param[in] session_key Key of @ref BT_EAD_KEY_SIZE bytes used for the 27 * encryption. 28 * @param[in] iv Initialisation Vector used to generate the nonce. It must be 29 * changed each time the Session Key changes. 30 * @param[in] randomizer Randomizer value used to generate the nonce. The value 31 * is also placed in front of the encrypted advertising data. 32 * @param[in] payload Advertising Data to encrypt. Can be multiple advertising 33 * structures that are concatenated. 34 * @param[in] payload_size Size of the Advertising Data to encrypt. 35 * @param[out] encrypted_payload Encrypted Ad Data including the Randomizer and 36 * the MIC. Size must be at least @ref BT_EAD_RANDOMIZER_SIZE + @p 37 * payload_size + @ref BT_EAD_MIC_SIZE. Use @ref 38 * BT_EAD_ENCRYPTED_PAYLOAD_SIZE to get the right size. 39 * 40 * @retval 0 Data have been correctly encrypted and authenticated. 41 * @retval -EIO Error occurred during the encryption or the authentication. 42 * @retval -EINVAL One of the argument is a NULL pointer. 43 */ 44 int bt_test_ead_encrypt(const uint8_t session_key[BT_EAD_KEY_SIZE], 45 const uint8_t iv[BT_EAD_IV_SIZE], 46 const uint8_t randomizer[BT_EAD_RANDOMIZER_SIZE], const uint8_t *payload, 47 size_t payload_size, uint8_t *encrypted_payload); 48 49 #define FAIL(...) \ 50 do { \ 51 bst_result = Failed; \ 52 bs_trace_error_time_line(__VA_ARGS__); \ 53 } while (0) 54 55 #define PASS(...) \ 56 do { \ 57 bst_result = Passed; \ 58 bs_trace_info_time(1, __VA_ARGS__); \ 59 } while (0) 60 61 extern enum bst_result_t bst_result; 62 63 #define CREATE_FLAG(flag) static atomic_t flag = (atomic_t) false 64 #define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) 65 #define GET_FLAG(flag) (bool)atomic_get(&flag) 66 #define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) 67 #define WAIT_FOR_FLAG(flag) \ 68 while (!(bool)atomic_get(&flag)) { \ 69 (void)k_sleep(K_MSEC(1)); \ 70 } 71 72 LOG_MODULE_DECLARE(bt_bsim_ead_sample_data, CONFIG_BT_EAD_LOG_LEVEL); 73 74 struct test_sample_data { 75 const uint8_t session_key[BT_EAD_KEY_SIZE]; 76 const uint8_t iv[BT_EAD_IV_SIZE]; 77 const uint8_t randomizer_little_endian[BT_EAD_RANDOMIZER_SIZE]; 78 const uint8_t *ad_data; 79 const size_t size_ad_data; 80 const uint8_t *ead; 81 const size_t size_ead; 82 }; 83 84 /* Encrypted Advertising Data Set 1 (ref: Supplement to the Bluetooth Core 85 * Specification v11, Part A, 2.3.1) 86 */ 87 88 #define SIZE_SAMPLE_AD_DATA_1 20 89 static const uint8_t sample_ad_data_1[] = {0x0F, 0x09, 0x53, 0x68, 0x6F, 0x72, 0x74, 90 0x20, 0x4D, 0x69, 0x6E, 0x69, 0x2D, 0x42, 91 0x75, 0x73, 0x03, 0x19, 0x0A, 0x8C}; 92 BUILD_ASSERT(sizeof(sample_ad_data_1) == SIZE_SAMPLE_AD_DATA_1); 93 94 #define SIZE_SAMPLE_EAD_1 29 95 static const uint8_t sample_ead_1[] = { 96 0x18, 0xE1, 0x57, 0xCA, 0xDE, 0x74, 0xE4, 0xDC, 0xAF, 0xDC, 0x51, 0xC7, 0x28, 0x28, 0x10, 97 0xC2, 0x21, 0x7F, 0x0E, 0x4C, 0xEF, 0x43, 0x43, 0x18, 0x1F, 0xBA, 0x00, 0x69, 0xCC, 98 }; 99 BUILD_ASSERT(sizeof(sample_ead_1) == SIZE_SAMPLE_EAD_1); 100 101 static const struct test_sample_data sample_data_1 = { 102 .session_key = {0x57, 0xA9, 0xDA, 0x12, 0xD1, 0x2E, 0x6E, 0x13, 0x1E, 0x20, 0x61, 0x2A, 103 0xD1, 0x0A, 0x6A, 0x19}, 104 .iv = {0x9E, 0x7A, 0x00, 0xEF, 0xB1, 0x7A, 0xE7, 0x46}, 105 .randomizer_little_endian = {0x18, 0xE1, 0x57, 0xCA, 0xDE}, 106 .ad_data = sample_ad_data_1, 107 .size_ad_data = SIZE_SAMPLE_AD_DATA_1, 108 .ead = sample_ead_1, 109 .size_ead = SIZE_SAMPLE_EAD_1, 110 }; 111 112 /* Encrypted Advertising Data Set 2 (ref: Supplement to the Bluetooth Core 113 * Specification v11, Part A, 2.3.2) 114 */ 115 116 #define SIZE_SAMPLE_AD_DATA_2 20 117 static const uint8_t sample_ad_data_2[] = {0x0F, 0x09, 0x53, 0x68, 0x6F, 0x72, 0x74, 118 0x20, 0x4D, 0x69, 0x6E, 0x69, 0x2D, 0x42, 119 0x75, 0x73, 0x03, 0x19, 0x0A, 0x8C}; 120 BUILD_ASSERT(sizeof(sample_ad_data_2) == SIZE_SAMPLE_AD_DATA_2); 121 122 #define SIZE_SAMPLE_EAD_2 29 123 static const uint8_t sample_ead_2[] = {0x8d, 0x1c, 0x97, 0x6e, 0x7a, 0x35, 0x44, 0x40, 0x76, 0x12, 124 0x57, 0x88, 0xc2, 0x38, 0xa5, 0x8e, 0x8b, 0xd9, 0xcf, 0xf0, 125 0xde, 0xfe, 0x25, 0x1a, 0x8e, 0x72, 0x75, 0x45, 0x4c}; 126 BUILD_ASSERT(sizeof(sample_ead_2) == SIZE_SAMPLE_EAD_2); 127 128 static const struct test_sample_data sample_data_2 = { 129 .session_key = {0x57, 0xA9, 0xDA, 0x12, 0xD1, 0x2E, 0x6E, 0x13, 0x1E, 0x20, 0x61, 0x2A, 130 0xD1, 0x0A, 0x6A, 0x19}, 131 .iv = {0x9E, 0x7A, 0x00, 0xEF, 0xB1, 0x7A, 0xE7, 0x46}, 132 .randomizer_little_endian = {0x8D, 0x1C, 0x97, 0x6E, 0x7A}, 133 .ad_data = sample_ad_data_2, 134 .size_ad_data = SIZE_SAMPLE_AD_DATA_2, 135 .ead = sample_ead_2, 136 .size_ead = SIZE_SAMPLE_EAD_2, 137 }; 138