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 LOG_MODULE_DECLARE(bt_bsim_ead_sample_data, CONFIG_BT_EAD_LOG_LEVEL);
50 
51 struct test_sample_data {
52 	const uint8_t session_key[BT_EAD_KEY_SIZE];
53 	const uint8_t iv[BT_EAD_IV_SIZE];
54 	const uint8_t randomizer_little_endian[BT_EAD_RANDOMIZER_SIZE];
55 	const uint8_t *ad_data;
56 	const size_t size_ad_data;
57 	const uint8_t *ead;
58 	const size_t size_ead;
59 };
60 
61 /* Encrypted Advertising Data Set 1 (ref: Supplement to the Bluetooth Core
62  * Specification v11, Part A, 2.3.1)
63  */
64 
65 #define SIZE_SAMPLE_AD_DATA_1 20
66 static const uint8_t sample_ad_data_1[] = {0x0F, 0x09, 0x53, 0x68, 0x6F, 0x72, 0x74,
67 					   0x20, 0x4D, 0x69, 0x6E, 0x69, 0x2D, 0x42,
68 					   0x75, 0x73, 0x03, 0x19, 0x0A, 0x8C};
69 BUILD_ASSERT(sizeof(sample_ad_data_1) == SIZE_SAMPLE_AD_DATA_1);
70 
71 #define SIZE_SAMPLE_EAD_1 29
72 static const uint8_t sample_ead_1[] = {
73 	0x18, 0xE1, 0x57, 0xCA, 0xDE, 0x74, 0xE4, 0xDC, 0xAF, 0xDC, 0x51, 0xC7, 0x28, 0x28, 0x10,
74 	0xC2, 0x21, 0x7F, 0x0E, 0x4C, 0xEF, 0x43, 0x43, 0x18, 0x1F, 0xBA, 0x00, 0x69, 0xCC,
75 };
76 BUILD_ASSERT(sizeof(sample_ead_1) == SIZE_SAMPLE_EAD_1);
77 
78 static const struct test_sample_data sample_data_1 = {
79 	.session_key = {0x57, 0xA9, 0xDA, 0x12, 0xD1, 0x2E, 0x6E, 0x13, 0x1E, 0x20, 0x61, 0x2A,
80 			0xD1, 0x0A, 0x6A, 0x19},
81 	.iv = {0x9E, 0x7A, 0x00, 0xEF, 0xB1, 0x7A, 0xE7, 0x46},
82 	.randomizer_little_endian = {0x18, 0xE1, 0x57, 0xCA, 0xDE},
83 	.ad_data = sample_ad_data_1,
84 	.size_ad_data = SIZE_SAMPLE_AD_DATA_1,
85 	.ead = sample_ead_1,
86 	.size_ead = SIZE_SAMPLE_EAD_1,
87 };
88 
89 /* Encrypted Advertising Data Set 2 (ref: Supplement to the Bluetooth Core
90  * Specification v11, Part A, 2.3.2)
91  */
92 
93 #define SIZE_SAMPLE_AD_DATA_2 20
94 static const uint8_t sample_ad_data_2[] = {0x0F, 0x09, 0x53, 0x68, 0x6F, 0x72, 0x74,
95 					   0x20, 0x4D, 0x69, 0x6E, 0x69, 0x2D, 0x42,
96 					   0x75, 0x73, 0x03, 0x19, 0x0A, 0x8C};
97 BUILD_ASSERT(sizeof(sample_ad_data_2) == SIZE_SAMPLE_AD_DATA_2);
98 
99 #define SIZE_SAMPLE_EAD_2 29
100 static const uint8_t sample_ead_2[] = {0x8d, 0x1c, 0x97, 0x6e, 0x7a, 0x35, 0x44, 0x40, 0x76, 0x12,
101 				       0x57, 0x88, 0xc2, 0x38, 0xa5, 0x8e, 0x8b, 0xd9, 0xcf, 0xf0,
102 				       0xde, 0xfe, 0x25, 0x1a, 0x8e, 0x72, 0x75, 0x45, 0x4c};
103 BUILD_ASSERT(sizeof(sample_ead_2) == SIZE_SAMPLE_EAD_2);
104 
105 static const struct test_sample_data sample_data_2 = {
106 	.session_key = {0x57, 0xA9, 0xDA, 0x12, 0xD1, 0x2E, 0x6E, 0x13, 0x1E, 0x20, 0x61, 0x2A,
107 			0xD1, 0x0A, 0x6A, 0x19},
108 	.iv = {0x9E, 0x7A, 0x00, 0xEF, 0xB1, 0x7A, 0xE7, 0x46},
109 	.randomizer_little_endian = {0x8D, 0x1C, 0x97, 0x6E, 0x7A},
110 	.ad_data = sample_ad_data_2,
111 	.size_ad_data = SIZE_SAMPLE_AD_DATA_2,
112 	.ead = sample_ead_2,
113 	.size_ead = SIZE_SAMPLE_EAD_2,
114 };
115