1 /*
2 * Copyright (c) 2017 Nordic Semiconductor ASA
3 * Copyright (c) 2015-2016 Intel Corporation
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <string.h>
9 #include <errno.h>
10
11 #include <zephyr/kernel.h>
12 #include <zephyr/sys/byteorder.h>
13 #include <zephyr/sys/check.h>
14
15 #include <zephyr/bluetooth/bluetooth.h>
16 #include <zephyr/bluetooth/hci.h>
17 #include <zephyr/bluetooth/conn.h>
18 #include <zephyr/bluetooth/crypto.h>
19
20 #include <tinycrypt/constants.h>
21 #include <tinycrypt/hmac_prng.h>
22 #include <tinycrypt/aes.h>
23 #include <tinycrypt/utils.h>
24
25 #include "common/bt_str.h"
26
27 #include "hci_core.h"
28
29 #define LOG_LEVEL CONFIG_BT_HCI_CORE_LOG_LEVEL
30 #include <zephyr/logging/log.h>
31 LOG_MODULE_REGISTER(bt_host_crypto);
32
33 static struct tc_hmac_prng_struct prng;
34
prng_reseed(struct tc_hmac_prng_struct * h)35 static int prng_reseed(struct tc_hmac_prng_struct *h)
36 {
37 uint8_t seed[32];
38 int64_t extra;
39 int ret;
40
41 ret = bt_hci_le_rand(seed, sizeof(seed));
42 if (ret) {
43 return ret;
44 }
45
46 extra = k_uptime_get();
47
48 ret = tc_hmac_prng_reseed(h, seed, sizeof(seed), (uint8_t *)&extra,
49 sizeof(extra));
50 if (ret == TC_CRYPTO_FAIL) {
51 LOG_ERR("Failed to re-seed PRNG");
52 return -EIO;
53 }
54
55 return 0;
56 }
57
prng_init(void)58 int prng_init(void)
59 {
60 uint8_t perso[8];
61 int ret;
62
63 ret = bt_hci_le_rand(perso, sizeof(perso));
64 if (ret) {
65 return ret;
66 }
67
68 ret = tc_hmac_prng_init(&prng, perso, sizeof(perso));
69 if (ret == TC_CRYPTO_FAIL) {
70 LOG_ERR("Failed to initialize PRNG");
71 return -EIO;
72 }
73
74 /* re-seed is needed after init */
75 return prng_reseed(&prng);
76 }
77
78 #if defined(CONFIG_BT_HOST_CRYPTO_PRNG)
bt_rand(void * buf,size_t len)79 int bt_rand(void *buf, size_t len)
80 {
81 int ret;
82
83 CHECKIF(buf == NULL || len == 0) {
84 return -EINVAL;
85 }
86
87 ret = tc_hmac_prng_generate(buf, len, &prng);
88 if (ret == TC_HMAC_PRNG_RESEED_REQ) {
89 ret = prng_reseed(&prng);
90 if (ret) {
91 return ret;
92 }
93
94 ret = tc_hmac_prng_generate(buf, len, &prng);
95 }
96
97 if (ret == TC_CRYPTO_SUCCESS) {
98 return 0;
99 }
100
101 return -EIO;
102 }
103 #else /* !CONFIG_BT_HOST_CRYPTO_PRNG */
bt_rand(void * buf,size_t len)104 int bt_rand(void *buf, size_t len)
105 {
106 CHECKIF(buf == NULL || len == 0) {
107 return -EINVAL;
108 }
109
110 return bt_hci_le_rand(buf, len);
111 }
112 #endif /* CONFIG_BT_HOST_CRYPTO_PRNG */
113
bt_encrypt_le(const uint8_t key[16],const uint8_t plaintext[16],uint8_t enc_data[16])114 int bt_encrypt_le(const uint8_t key[16], const uint8_t plaintext[16],
115 uint8_t enc_data[16])
116 {
117 struct tc_aes_key_sched_struct s;
118 uint8_t tmp[16];
119
120 CHECKIF(key == NULL || plaintext == NULL || enc_data == NULL) {
121 return -EINVAL;
122 }
123
124 LOG_DBG("key %s", bt_hex(key, 16));
125 LOG_DBG("plaintext %s", bt_hex(plaintext, 16));
126
127 sys_memcpy_swap(tmp, key, 16);
128
129 if (tc_aes128_set_encrypt_key(&s, tmp) == TC_CRYPTO_FAIL) {
130 return -EINVAL;
131 }
132
133 sys_memcpy_swap(tmp, plaintext, 16);
134
135 if (tc_aes_encrypt(enc_data, tmp, &s) == TC_CRYPTO_FAIL) {
136 return -EINVAL;
137 }
138
139 sys_mem_swap(enc_data, 16);
140
141 LOG_DBG("enc_data %s", bt_hex(enc_data, 16));
142
143 return 0;
144 }
145
bt_encrypt_be(const uint8_t key[16],const uint8_t plaintext[16],uint8_t enc_data[16])146 int bt_encrypt_be(const uint8_t key[16], const uint8_t plaintext[16],
147 uint8_t enc_data[16])
148 {
149 struct tc_aes_key_sched_struct s;
150
151 CHECKIF(key == NULL || plaintext == NULL || enc_data == NULL) {
152 return -EINVAL;
153 }
154
155 LOG_DBG("key %s", bt_hex(key, 16));
156 LOG_DBG("plaintext %s", bt_hex(plaintext, 16));
157
158 if (tc_aes128_set_encrypt_key(&s, key) == TC_CRYPTO_FAIL) {
159 return -EINVAL;
160 }
161
162 if (tc_aes_encrypt(enc_data, plaintext, &s) == TC_CRYPTO_FAIL) {
163 return -EINVAL;
164 }
165
166 LOG_DBG("enc_data %s", bt_hex(enc_data, 16));
167
168 return 0;
169 }
170
171 #ifdef ZTEST_UNITTEST
bt_crypto_get_hmac_prng_instance(void)172 struct tc_hmac_prng_struct *bt_crypto_get_hmac_prng_instance(void)
173 {
174 return &prng;
175 }
176 #endif /* ZTEST_UNITTEST */
177