-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathcracen_psa.h
418 lines (306 loc) · 18.5 KB
/
cracen_psa.h
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
241
242
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
* Copyright (c) 2023-2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#ifndef CRACEN_PSA_H
#define CRACEN_PSA_H
#include <psa/crypto.h>
#include <stddef.h>
#include <string.h>
#include <zephyr/sys/__assert.h>
#include <cracen/mem_helpers.h>
#include "cracen_psa_primitives.h"
#include "cracen_psa_kmu.h"
#include "cracen_psa_key_ids.h"
#include "sxsymcrypt/keyref.h"
#ifdef __NRF_TFM__
#include <tfm_builtin_key_loader.h>
#endif
/**
* See "PSA Cryptography API" for documentation.
*/
psa_status_t cracen_sign_message(const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *input,
size_t input_length, uint8_t *signature, size_t signature_size,
size_t *signature_length);
psa_status_t cracen_verify_message(const psa_key_attributes_t *attributes,
const uint8_t *key_buffer, size_t key_buffer_size,
psa_algorithm_t alg, const uint8_t *input, size_t input_length,
const uint8_t *signature, size_t signature_length);
psa_status_t cracen_sign_hash(const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *hash,
size_t hash_length, uint8_t *signature, size_t signature_size,
size_t *signature_length);
psa_status_t cracen_verify_hash(const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *hash,
size_t hash_length, const uint8_t *signature,
size_t signature_length);
psa_status_t cracen_hash_compute(psa_algorithm_t alg, const uint8_t *input, size_t input_length,
uint8_t *hash, size_t hash_size, size_t *hash_length);
psa_status_t cracen_hash_setup(cracen_hash_operation_t *operation, psa_algorithm_t alg);
static inline psa_status_t cracen_hash_clone(const cracen_hash_operation_t *source_operation,
cracen_hash_operation_t *target_operation)
{
__ASSERT_NO_MSG(source_operation != NULL);
__ASSERT_NO_MSG(target_operation != NULL);
memcpy(target_operation, source_operation, sizeof(cracen_hash_operation_t));
return PSA_SUCCESS;
}
psa_status_t cracen_hash_update(cracen_hash_operation_t *operation, const uint8_t *input,
const size_t input_length);
psa_status_t cracen_hash_finish(cracen_hash_operation_t *operation, uint8_t *hash, size_t hash_size,
size_t *hash_length);
static inline psa_status_t cracen_hash_abort(cracen_hash_operation_t *operation)
{
__ASSERT_NO_MSG(operation != NULL);
safe_memzero(operation, sizeof(cracen_hash_operation_t));
return PSA_SUCCESS;
}
psa_status_t cracen_aead_encrypt(const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *nonce,
size_t nonce_length, const uint8_t *additional_data,
size_t additional_data_length, const uint8_t *plaintext,
size_t plaintext_length, uint8_t *ciphertext,
size_t ciphertext_size, size_t *ciphertext_length);
psa_status_t cracen_aead_decrypt(const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *nonce,
size_t nonce_length, const uint8_t *additional_data,
size_t additional_data_length, const uint8_t *ciphertext,
size_t ciphertext_length, uint8_t *plaintext,
size_t plaintext_size, size_t *plaintext_length);
psa_status_t cracen_cipher_encrypt(const psa_key_attributes_t *attributes,
const uint8_t *key_buffer, size_t key_buffer_size,
psa_algorithm_t alg, const uint8_t *iv, size_t iv_length,
const uint8_t *input, size_t input_length, uint8_t *output,
size_t output_size, size_t *output_length);
psa_status_t cracen_cipher_decrypt(const psa_key_attributes_t *attributes,
const uint8_t *key_buffer, size_t key_buffer_size,
psa_algorithm_t alg, const uint8_t *input, size_t input_length,
uint8_t *output, size_t output_size, size_t *output_length);
psa_status_t cracen_cipher_encrypt_setup(cracen_cipher_operation_t *operation,
const psa_key_attributes_t *attributes,
const uint8_t *key_buffer, size_t key_buffer_size,
psa_algorithm_t alg);
psa_status_t cracen_cipher_decrypt_setup(cracen_cipher_operation_t *operation,
const psa_key_attributes_t *attributes,
const uint8_t *key_buffer, size_t key_buffer_size,
psa_algorithm_t alg);
psa_status_t cracen_cipher_set_iv(cracen_cipher_operation_t *operation, const uint8_t *iv,
size_t iv_length);
psa_status_t cracen_cipher_update(cracen_cipher_operation_t *operation, const uint8_t *input,
size_t input_length, uint8_t *output, size_t output_size,
size_t *output_length);
psa_status_t cracen_cipher_finish(cracen_cipher_operation_t *operation, uint8_t *output,
size_t output_size, size_t *output_length);
psa_status_t cracen_cipher_abort(cracen_cipher_operation_t *operation);
psa_status_t cracen_aead_encrypt_setup(cracen_aead_operation_t *operation,
const psa_key_attributes_t *attributes,
const uint8_t *key_buffer, size_t key_buffer_size,
psa_algorithm_t alg);
psa_status_t cracen_aead_decrypt_setup(cracen_aead_operation_t *operation,
const psa_key_attributes_t *attributes,
const uint8_t *key_buffer, size_t key_buffer_size,
psa_algorithm_t alg);
psa_status_t cracen_aead_set_nonce(cracen_aead_operation_t *operation, const uint8_t *nonce,
size_t nonce_length);
psa_status_t cracen_aead_set_lengths(cracen_aead_operation_t *operation, size_t ad_length,
size_t plaintext_length);
psa_status_t cracen_aead_update_ad(cracen_aead_operation_t *operation, const uint8_t *input,
size_t input_length);
psa_status_t cracen_aead_update(cracen_aead_operation_t *operation, const uint8_t *input,
size_t input_length, uint8_t *output, size_t output_size,
size_t *output_length);
psa_status_t cracen_aead_finish(cracen_aead_operation_t *operation, uint8_t *ciphertext,
size_t ciphertext_size, size_t *ciphertext_length, uint8_t *tag,
size_t tag_size, size_t *tag_length);
psa_status_t cracen_aead_verify(cracen_aead_operation_t *operation, uint8_t *plaintext,
size_t plaintext_size, size_t *plaintext_length, const uint8_t *tag,
size_t tag_length);
psa_status_t cracen_aead_abort(cracen_aead_operation_t *operation);
psa_status_t cracen_mac_compute(const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *input,
size_t input_length, uint8_t *mac, size_t mac_size,
size_t *mac_length);
psa_status_t cracen_mac_sign_setup(cracen_mac_operation_t *operation,
const psa_key_attributes_t *attributes,
const uint8_t *key_buffer, size_t key_buffer_size,
psa_algorithm_t alg);
psa_status_t cracen_mac_verify_setup(cracen_mac_operation_t *operation,
const psa_key_attributes_t *attributes,
const uint8_t *key_buffer, size_t key_buffer_size,
psa_algorithm_t alg);
psa_status_t cracen_mac_update(cracen_mac_operation_t *operation, const uint8_t *input,
size_t input_length);
psa_status_t cracen_mac_sign_finish(cracen_mac_operation_t *operation, uint8_t *mac,
size_t mac_size, size_t *mac_length);
psa_status_t cracen_mac_verify_finish(cracen_mac_operation_t *operation, const uint8_t *mac,
size_t mac_length);
psa_status_t cracen_mac_abort(cracen_mac_operation_t *operation);
psa_status_t cracen_key_agreement(const psa_key_attributes_t *attributes, const uint8_t *priv_key,
size_t priv_key_size, const uint8_t *publ_key,
size_t publ_key_size, uint8_t *output, size_t output_size,
size_t *output_length, psa_algorithm_t alg);
psa_status_t cracen_key_derivation_setup(cracen_key_derivation_operation_t *operation,
psa_algorithm_t alg);
psa_status_t cracen_key_derivation_set_capacity(cracen_key_derivation_operation_t *operation,
size_t capacity);
psa_status_t cracen_key_derivation_input_key(cracen_key_derivation_operation_t *operation,
psa_key_derivation_step_t step,
const psa_key_attributes_t *attributes,
const uint8_t *key_buffer, size_t key_buffer_size);
psa_status_t cracen_key_derivation_input_bytes(cracen_key_derivation_operation_t *operation,
psa_key_derivation_step_t step, const uint8_t *data,
size_t data_length);
psa_status_t cracen_key_derivation_input_integer(cracen_key_derivation_operation_t *operation,
psa_key_derivation_step_t step, uint64_t value);
psa_status_t cracen_key_derivation_output_bytes(cracen_key_derivation_operation_t *operation,
uint8_t *output, size_t output_length);
psa_status_t cracen_key_derivation_abort(cracen_key_derivation_operation_t *operation);
psa_status_t cracen_export_public_key(const psa_key_attributes_t *attributes,
const uint8_t *key_buffer, size_t key_buffer_size,
uint8_t *data, size_t data_size, size_t *data_length);
psa_status_t cracen_import_key(const psa_key_attributes_t *attributes, const uint8_t *data,
size_t data_length, uint8_t *key_buffer, size_t key_buffer_size,
size_t *key_buffer_length, size_t *key_bits);
psa_status_t cracen_generate_key(const psa_key_attributes_t *attributes, uint8_t *key_buffer,
size_t key_buffer_size, size_t *key_buffer_length);
psa_status_t cracen_asymmetric_encrypt(const psa_key_attributes_t *attributes,
const uint8_t *key_buffer, size_t key_buffer_size,
psa_algorithm_t alg, const uint8_t *input,
size_t input_length, const uint8_t *salt, size_t salt_length,
uint8_t *output, size_t output_size, size_t *output_length);
psa_status_t cracen_asymmetric_decrypt(const psa_key_attributes_t *attributes,
const uint8_t *key_buffer, size_t key_buffer_size,
psa_algorithm_t alg, const uint8_t *input,
size_t input_length, const uint8_t *salt, size_t salt_length,
uint8_t *output, size_t output_size, size_t *output_length);
psa_status_t cracen_get_builtin_key(psa_drv_slot_number_t slot_number,
psa_key_attributes_t *attributes, uint8_t *key_buffer,
size_t key_buffer_size, size_t *key_buffer_length);
psa_status_t cracen_export_key(const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
size_t key_buffer_size, uint8_t *data, size_t data_size,
size_t *data_length);
psa_status_t cracen_copy_key(psa_key_attributes_t *attributes, const uint8_t *source_key,
size_t source_key_length, uint8_t *target_key_buffer,
size_t target_key_buffer_size, size_t *target_key_buffer_length);
psa_status_t cracen_destroy_key(const psa_key_attributes_t *attributes);
psa_status_t cracen_get_opaque_size(const psa_key_attributes_t *attributes, size_t *key_size);
psa_status_t cracen_jpake_setup(cracen_jpake_operation_t *operation,
const psa_key_attributes_t *attributes, const uint8_t *password,
size_t password_length,
const psa_pake_cipher_suite_t *cipher_suite);
psa_status_t cracen_jpake_set_password_key(cracen_jpake_operation_t *operation,
const psa_key_attributes_t *attributes,
const uint8_t *password, size_t password_length);
psa_status_t cracen_jpake_set_user(cracen_jpake_operation_t *operation, const uint8_t *user_id,
size_t user_id_len);
psa_status_t cracen_jpake_set_peer(cracen_jpake_operation_t *operation, const uint8_t *peer_id,
size_t peer_id_len);
psa_status_t cracen_jpake_set_role(cracen_jpake_operation_t *operation, psa_pake_role_t role);
psa_status_t cracen_jpake_output(cracen_jpake_operation_t *operation, psa_pake_step_t step,
uint8_t *output, size_t output_size, size_t *output_length);
psa_status_t cracen_jpake_input(cracen_jpake_operation_t *operation, psa_pake_step_t step,
const uint8_t *input, size_t input_length);
psa_status_t cracen_jpake_get_shared_key(cracen_jpake_operation_t *operation,
const psa_key_attributes_t *attributes, uint8_t *output,
size_t output_size, size_t *output_length);
psa_status_t cracen_jpake_abort(cracen_jpake_operation_t *operation);
psa_status_t cracen_init_random(cracen_prng_context_t *context);
psa_status_t cracen_get_random(cracen_prng_context_t *context, uint8_t *output, size_t output_size);
psa_status_t cracen_free_random(cracen_prng_context_t *context);
psa_status_t cracen_srp_setup(cracen_srp_operation_t *operation,
const psa_key_attributes_t *attributes, const uint8_t *password,
size_t password_length, const psa_pake_cipher_suite_t *cipher_suite);
psa_status_t cracen_srp_set_user(cracen_srp_operation_t *operation, const uint8_t *user_id,
size_t user_id_len);
psa_status_t cracen_srp_set_role(cracen_srp_operation_t *operation, psa_pake_role_t role);
psa_status_t cracen_srp_set_password_key(cracen_srp_operation_t *operation,
const psa_key_attributes_t *attributes,
const uint8_t *password, size_t password_length);
psa_status_t cracen_srp_output(cracen_srp_operation_t *operation, psa_pake_step_t step,
uint8_t *output, size_t output_size, size_t *output_length);
psa_status_t cracen_srp_input(cracen_srp_operation_t *operation, psa_pake_step_t step,
const uint8_t *input, size_t input_length);
psa_status_t cracen_srp_get_shared_key(cracen_srp_operation_t *operation,
const psa_key_attributes_t *attributes, uint8_t *output,
size_t output_size, size_t *output_length);
psa_status_t cracen_srp_abort(cracen_srp_operation_t *operation);
psa_status_t cracen_pake_setup(cracen_pake_operation_t *operation,
const psa_key_attributes_t *attributes, const uint8_t *password,
size_t password_length, const psa_pake_cipher_suite_t *cipher_suite);
psa_status_t cracen_pake_set_user(cracen_pake_operation_t *operation, const uint8_t *user_id,
size_t user_id_len);
psa_status_t cracen_pake_set_peer(cracen_pake_operation_t *operation, const uint8_t *peer_id,
size_t peer_id_len);
psa_status_t cracen_pake_set_context(cracen_pake_operation_t *operation, const uint8_t *context,
size_t context_length);
psa_status_t cracen_pake_set_role(cracen_pake_operation_t *operation, psa_pake_role_t role);
psa_status_t cracen_pake_output(cracen_pake_operation_t *operation, psa_pake_step_t step,
uint8_t *output, size_t output_size, size_t *output_length);
psa_status_t cracen_pake_input(cracen_pake_operation_t *operation, psa_pake_step_t step,
const uint8_t *input, size_t input_length);
psa_status_t cracen_pake_get_shared_key(cracen_pake_operation_t *operation,
const psa_key_attributes_t *attributes, uint8_t *key_buffer,
size_t key_buffer_size, size_t *key_buffer_length);
psa_status_t cracen_pake_abort(cracen_pake_operation_t *operation);
psa_status_t cracen_spake2p_setup(cracen_spake2p_operation_t *operation,
const psa_key_attributes_t *attributes, const uint8_t *password,
size_t password_length,
const psa_pake_cipher_suite_t *cipher_suite);
psa_status_t cracen_spake2p_set_password_key(cracen_spake2p_operation_t *operation,
const psa_key_attributes_t *attributes,
const uint8_t *password, size_t password_length);
psa_status_t cracen_spake2p_set_user(cracen_spake2p_operation_t *operation, const uint8_t *user_id,
size_t user_id_len);
psa_status_t cracen_spake2p_set_peer(cracen_spake2p_operation_t *operation, const uint8_t *peer_id,
size_t peer_id_len);
psa_status_t cracen_spake2p_set_context(cracen_spake2p_operation_t *operation,
const uint8_t *context, size_t context_length);
psa_status_t cracen_spake2p_set_role(cracen_spake2p_operation_t *operation, psa_pake_role_t role);
psa_status_t cracen_spake2p_output(cracen_spake2p_operation_t *operation, psa_pake_step_t step,
uint8_t *output, size_t output_size, size_t *output_length);
psa_status_t cracen_spake2p_input(cracen_spake2p_operation_t *operation, psa_pake_step_t step,
const uint8_t *input, size_t input_length);
psa_status_t cracen_spake2p_get_shared_key(cracen_spake2p_operation_t *operation,
const psa_key_attributes_t *attributes, uint8_t *output,
size_t output_size, size_t *output_length);
psa_status_t cracen_spake2p_abort(cracen_spake2p_operation_t *operation);
int cracen_ed25519_sign(const uint8_t *priv_key, char *signature, const uint8_t *message,
size_t message_length);
int cracen_ed25519_verify(const uint8_t *pub_key, const char *message, size_t message_length,
const char *signature);
int cracen_ed25519ph_sign(const uint8_t *priv_key, char *signature, const uint8_t *message,
size_t message_length, bool is_message);
int cracen_ed25519ph_verify(const uint8_t *pub_key, const char *message, size_t message_length,
const char *signature, bool is_message);
int cracen_ed25519_create_pubkey(const uint8_t *priv_key, uint8_t *pub_key);
int be_cmp(const unsigned char *a, const unsigned char *b, size_t sz, int carry);
int ecc_create_genpubkey(const char *priv_key, char *pub_key, const struct sx_pk_ecurve *curve);
int ecc_create_genprivkey(const struct sx_pk_ecurve *curve, char *priv_key, size_t priv_key_size);
int cracen_ecdsa_verify_message(const char *pubkey, const struct sxhashalg *hashalg,
const uint8_t *message, size_t message_length,
const struct sx_pk_ecurve *curve, const uint8_t *signature);
int cracen_ecdsa_verify_digest(const char *pubkey, const uint8_t *digest, size_t digestsz,
const struct sx_pk_ecurve *curve, const uint8_t *signature);
int cracen_ecdsa_sign_message(const struct ecc_priv_key *privkey, const struct sxhashalg *hashalg,
const struct sx_pk_ecurve *curve, const uint8_t *message,
size_t message_length, const uint8_t *signature);
int cracen_ecdsa_sign_digest(const struct ecc_priv_key *privkey, const struct sxhashalg *hashalg,
const struct sx_pk_ecurve *curve, const uint8_t *digest,
size_t digest_length, const uint8_t *signature);
int cracen_ecdsa_sign_message_deterministic(const struct ecc_priv_key *privkey,
const struct sxhashalg *hashalg,
const struct sx_pk_ecurve *curve,
const uint8_t *message, size_t message_length,
const uint8_t *signature);
int cracen_ecdsa_sign_digest_deterministic(const struct ecc_priv_key *privkey,
const struct sxhashalg *hashalg,
const struct sx_pk_ecurve *curve, const uint8_t *digest,
size_t digestsz, const uint8_t *signature);
int rndinrange_create(const unsigned char *n, size_t nsz, unsigned char *out);
int mac_create_hmac(const struct sxhashalg *hashalg, struct sxhash *hashopctx, const char *key,
size_t keysz, char *workmem, size_t workmemsz);
int hmac_produce(struct sxhash *hashctx, const struct sxhashalg *hashalg, char *out, size_t sz,
char *workmem);
#endif /* CRACEN_PSA_H */