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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | /* Copyright (c) 2023 Nordic Semiconductor ASA
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/bluetooth/ead.h>
#include <zephyr/bluetooth/crypto.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <zephyr/sys/check.h>
#include <zephyr/logging/log.h>
/** nonce size in bytes */
#define BT_EAD_NONCE_SIZE 13
/* This value is used to set the directionBit of the CCM nonce to the MSB of the Randomizer field
* (see Supplement to the Bluetooth Core Specification v11, Part A 1.23.3)
*/
#define BT_EAD_RANDOMIZER_DIRECTION_BIT 7
/** Additional Authenticated Data size in bytes */
#define BT_EAD_AAD_SIZE 1
/* Fixed value used for the Additional Authenticated Data (see Supplement to the Bluetooth Core
* Specification v11, Part A 1.23.3)
*/
static uint8_t bt_ead_aad[] = {0xEA};
BUILD_ASSERT(sizeof(bt_ead_aad) == BT_EAD_AAD_SIZE);
LOG_MODULE_REGISTER(bt_encrypted_ad_data, CONFIG_BT_EAD_LOG_LEVEL);
static int bt_ead_generate_randomizer(uint8_t randomizer[BT_EAD_RANDOMIZER_SIZE])
{
int err;
err = bt_rand(randomizer, BT_EAD_RANDOMIZER_SIZE);
if (err != 0) {
return -ECANCELED;
}
/* From Supplement to the Bluetooth Core Specification v11, Part A 1.23.3: The directionBit
* of the CCM nonce shall be set to the most significant bit of the Randomizer field.
*/
randomizer[4] |= 1 << BT_EAD_RANDOMIZER_DIRECTION_BIT;
return 0;
}
static int bt_ead_generate_nonce(const uint8_t iv[BT_EAD_IV_SIZE],
const uint8_t randomizer[BT_EAD_RANDOMIZER_SIZE], uint8_t *nonce)
{
uint8_t new_randomizer[BT_EAD_RANDOMIZER_SIZE];
if (randomizer == NULL) {
int err;
err = bt_ead_generate_randomizer(new_randomizer);
if (err != 0) {
LOG_DBG("Failed to generate Randomizer");
return -ECANCELED;
}
randomizer = new_randomizer;
}
memcpy(&nonce[0], randomizer, BT_EAD_RANDOMIZER_SIZE);
memcpy(&nonce[BT_EAD_RANDOMIZER_SIZE], iv, BT_EAD_IV_SIZE);
return 0;
}
static int 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)
{
int err;
uint8_t nonce[BT_EAD_NONCE_SIZE];
size_t ead_size = BT_EAD_RANDOMIZER_SIZE + payload_size + BT_EAD_MIC_SIZE;
err = bt_ead_generate_nonce(iv, randomizer, nonce);
if (err != 0) {
return -ECANCELED;
}
memcpy(encrypted_payload, nonce, BT_EAD_RANDOMIZER_SIZE);
err = bt_ccm_encrypt(session_key, nonce, payload, payload_size, bt_ead_aad, BT_EAD_AAD_SIZE,
&encrypted_payload[BT_EAD_RANDOMIZER_SIZE], BT_EAD_MIC_SIZE);
if (err != 0) {
LOG_DBG("Failed to encrypt the payload (bt_ccm_encrypt err %d)", err);
return -EIO;
}
LOG_HEXDUMP_DBG(encrypted_payload, ead_size, "Encrypted Data: ");
return 0;
}
int bt_ead_encrypt(const uint8_t session_key[BT_EAD_KEY_SIZE], const uint8_t iv[BT_EAD_IV_SIZE],
const uint8_t *payload, size_t payload_size, uint8_t *encrypted_payload)
{
CHECKIF(session_key == NULL) {
LOG_DBG("session_key is NULL");
return -EINVAL;
}
CHECKIF(iv == NULL) {
LOG_DBG("iv is NULL");
return -EINVAL;
}
CHECKIF(payload == NULL) {
LOG_DBG("payload is NULL");
return -EINVAL;
}
CHECKIF(encrypted_payload == NULL) {
LOG_DBG("encrypted_payload is NULL");
return -EINVAL;
}
if (payload_size == 0) {
LOG_WRN("payload_size is set to 0. The encrypted result will only contain the "
"Randomizer and the MIC.");
}
return ead_encrypt(session_key, iv, NULL, payload, payload_size, encrypted_payload);
}
#if defined(CONFIG_BT_TESTING)
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)
{
CHECKIF(session_key == NULL) {
LOG_DBG("session_key is NULL");
return -EINVAL;
}
CHECKIF(iv == NULL) {
LOG_DBG("iv is NULL");
return -EINVAL;
}
CHECKIF(randomizer == NULL) {
LOG_DBG("randomizer is NULL");
return -EINVAL;
}
CHECKIF(payload == NULL) {
LOG_DBG("payload is NULL");
return -EINVAL;
}
CHECKIF(encrypted_payload == NULL) {
LOG_DBG("encrypted_payload is NULL");
return -EINVAL;
}
if (payload_size == 0) {
LOG_WRN("payload_size is set to 0. The encrypted result will be filled with only "
"the Randomizer and the MIC.");
}
return ead_encrypt(session_key, iv, randomizer, payload, payload_size, encrypted_payload);
}
#endif /* CONFIG_BT_TESTING */
static int ead_decrypt(const uint8_t session_key[BT_EAD_KEY_SIZE], const uint8_t iv[BT_EAD_IV_SIZE],
const uint8_t *encrypted_payload, size_t encrypted_payload_size,
uint8_t *payload)
{
int err;
uint8_t nonce[BT_EAD_NONCE_SIZE];
const uint8_t *encrypted_ad_data = &encrypted_payload[BT_EAD_RANDOMIZER_SIZE];
size_t encrypted_ad_data_size = encrypted_payload_size - BT_EAD_RANDOMIZER_SIZE;
size_t payload_size = encrypted_ad_data_size - BT_EAD_MIC_SIZE;
const uint8_t *randomizer = encrypted_payload;
err = bt_ead_generate_nonce(iv, randomizer, nonce);
if (err != 0) {
return -EIO;
}
LOG_HEXDUMP_DBG(encrypted_ad_data, encrypted_ad_data_size, "Encrypted Data: ");
err = bt_ccm_decrypt(session_key, nonce, encrypted_ad_data, payload_size, bt_ead_aad,
BT_EAD_AAD_SIZE, payload, BT_EAD_MIC_SIZE);
LOG_HEXDUMP_DBG(payload, payload_size, "Decrypted Data: ");
if (err != 0) {
LOG_DBG("Failed to decrypt the data (bt_ccm_decrypt err %d)", err);
return -EIO;
}
return 0;
}
int bt_ead_decrypt(const uint8_t session_key[BT_EAD_KEY_SIZE], const uint8_t iv[BT_EAD_IV_SIZE],
const uint8_t *encrypted_payload, size_t encrypted_payload_size,
uint8_t *payload)
{
CHECKIF(session_key == NULL) {
LOG_DBG("session_key is NULL");
return -EINVAL;
}
CHECKIF(iv == NULL) {
LOG_DBG("iv is NULL");
return -EINVAL;
}
CHECKIF(encrypted_payload == NULL) {
LOG_DBG("encrypted_payload is NULL");
return -EINVAL;
}
CHECKIF(payload == NULL) {
LOG_DBG("payload is NULL");
return -EINVAL;
}
if (encrypted_payload_size < BT_EAD_RANDOMIZER_SIZE + BT_EAD_MIC_SIZE) {
LOG_DBG("encrypted_payload_size is not large enough.");
return -EINVAL;
} else if (encrypted_payload_size == BT_EAD_RANDOMIZER_SIZE + BT_EAD_MIC_SIZE) {
LOG_WRN("encrypted_payload_size not large enough to contain encrypted data.");
}
return ead_decrypt(session_key, iv, encrypted_payload, encrypted_payload_size, payload);
}
|