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
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
73
74
79
80
86
87
97
98
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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* ... */
/* ... */
#ifndef BTSTACK_CTRYPTO_H
#define BTSTACK_CTRYPTO_H
#include "btstack_defines.h"
#include "btstack_config.h"
#if defined __cplusplus
extern "C" {
#endif
#define CMAC_TEMP_API
typedef enum {
BTSTACK_CRYPTO_RANDOM,
BTSTACK_CRYPTO_AES128,
BTSTACK_CRYPTO_CMAC_GENERATOR,
BTSTACK_CRYPTO_CMAC_MESSAGE,
BTSTACK_CRYPTO_ECC_P256_GENERATE_KEY,
BTSTACK_CRYPTO_ECC_P256_CALCULATE_DHKEY,
BTSTACK_CRYPTO_CCM_DIGEST_BLOCK,
BTSTACK_CRYPTO_CCM_ENCRYPT_BLOCK,
BTSTACK_CRYPTO_CCM_DECRYPT_BLOCK,
...} btstack_crypto_operation_t;
typedef struct {
btstack_context_callback_registration_t context_callback;
btstack_crypto_operation_t operation;
...} btstack_crypto_t;
typedef struct {
btstack_crypto_t btstack_crypto;
uint8_t * buffer;
uint16_t size;
...} btstack_crypto_random_t;
typedef struct {
btstack_crypto_t btstack_crypto;
const uint8_t * key;
const uint8_t * plaintext;
uint8_t * ciphertext;
...} btstack_crypto_aes128_t;
typedef struct {
btstack_crypto_t btstack_crypto;
const uint8_t * key;
uint16_t size;
union {
uint8_t (*get_byte_callback)(uint16_t pos);
const uint8_t * message;
...} data;
uint8_t * hash;
...} btstack_crypto_aes128_cmac_t;
typedef struct {
btstack_crypto_t btstack_crypto;
uint8_t * public_key;
uint8_t * dhkey;
...} btstack_crypto_ecc_p256_t;
typedef enum {
CCM_CALCULATE_X1,
CCM_W4_X1,
CCM_CALCULATE_AAD_XN,
CCM_W4_AAD_XN,
CCM_CALCULATE_XN,
CCM_W4_XN,
CCM_CALCULATE_S0,
CCM_W4_S0,
CCM_CALCULATE_SN,
CCM_W4_SN,
...} btstack_crypto_ccm_state_t;
typedef struct {
btstack_crypto_t btstack_crypto;
btstack_crypto_ccm_state_t state;
const uint8_t * key;
const uint8_t * nonce;
const uint8_t * input;
uint8_t * output;
uint8_t x_i[16];
uint16_t aad_offset;
uint16_t aad_len;
uint16_t message_len;
uint16_t counter;
uint16_t block_len;
uint8_t auth_len;
uint8_t aad_remainder_len;
...} btstack_crypto_ccm_t;
/* ... */
void btstack_crypto_init(void);
/* ... */
void btstack_crypto_random_generate(btstack_crypto_random_t * request, uint8_t * buffer, uint16_t size, void (* callback)(void * arg), void * callback_arg);
/* ... */
void btstack_crypto_aes128_encrypt(btstack_crypto_aes128_t * request, const uint8_t * key, const uint8_t * plaintext, uint8_t * ciphertext, void (* callback)(void * arg), void * callback_arg);
/* ... */
void btstack_crypto_aes128_cmac_generator(btstack_crypto_aes128_cmac_t * request, const uint8_t * key, uint16_t size, uint8_t (*get_byte_callback)(uint16_t pos), uint8_t * hash, void (* callback)(void * arg), void * callback_arg);
/* ... */
void btstack_crypto_aes128_cmac_message(btstack_crypto_aes128_cmac_t * request, const uint8_t * key, uint16_t size, const uint8_t * message, uint8_t * hash, void (* callback)(void * arg), void * callback_arg);
/* ... */
void btstack_crypto_aes128_cmac_zero(btstack_crypto_aes128_cmac_t * request, uint16_t size, const uint8_t * message, uint8_t * hash, void (* callback)(void * arg), void * callback_arg);
/* ... */
void btstack_crypto_ecc_p256_generate_key(btstack_crypto_ecc_p256_t * request, uint8_t * public_key, void (* callback)(void * arg), void * callback_arg);
/* ... */
void btstack_crypto_ecc_p256_calculate_dhkey(btstack_crypto_ecc_p256_t * request, const uint8_t * public_key, uint8_t * dhkey, void (* callback)(void * arg), void * callback_arg);
/* ... */
int btstack_crypto_ecc_p256_validate_public_key(const uint8_t * public_key);
/* ... */
void btstack_crypto_ccm_init(btstack_crypto_ccm_t * request, const uint8_t * key, const uint8_t * nonce, uint16_t message_len, uint16_t additional_authenticated_data_len, uint8_t auth_len);
/* ... */
void btstack_crypto_ccm_get_authentication_value(btstack_crypto_ccm_t * request, uint8_t * authentication_value);
/* ... */
void btstack_crypto_ccm_digest(btstack_crypto_ccm_t * request, uint8_t * additional_authenticated_data, uint16_t additional_authenticated_data_len, void (* callback)(void * arg), void * callback_arg);
/* ... */
void btstack_crypto_ccm_encrypt_block(btstack_crypto_ccm_t * request, uint16_t len, const uint8_t * plaintext, uint8_t * ciphertext, void (* callback)(void * arg), void * callback_arg);
/* ... */
void btstack_crypto_ccm_decrypt_block(btstack_crypto_ccm_t * request, uint16_t len, const uint8_t * ciphertext, uint8_t * plaintext, void (* callback)(void * arg), void * callback_arg);
#if defined(ENABLE_SOFTWARE_AES128) || defined (HAVE_AES128)
/* ... */
void btstack_aes128_calc(const uint8_t * key, const uint8_t * plaintext, uint8_t * ciphertext);/* ... */
#endif
/* ... */
void btstack_crypto_deinit(void);
void btstack_crypto_ecc_p256_set_key(const uint8_t * public_key, const uint8_t * private_key);
int btstack_crypto_idle(void);
void btstack_crypto_reset(void);
#if defined __cplusplus
}extern "C" { ... }
#endif
/* ... */
#endif