blob: 82654eff8c008723803531d9ad2449a7d1fdf988 [file] [log] [blame]
Antonio de Angelis8908f472018-08-31 15:44:25 +01001/*
Antonio de Angelis377a1552018-11-22 17:02:40 +00002 * Copyright (c) 2018-2019, Arm Limited. All rights reserved.
Antonio de Angelis8908f472018-08-31 15:44:25 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#ifndef __TFM_CRYPTO_VENEERS_H__
9#define __TFM_CRYPTO_VENEERS_H__
10
11#ifdef __cplusplus
12extern "C" {
13#endif
14
15#include "tfm_crypto_defs.h"
16
17#include "psa_crypto.h"
18
19#include "crypto_psa_wrappers.h"
20
21/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010022 * \brief Import the key data in the provided key slot (veneer function)
Antonio de Angelis8908f472018-08-31 15:44:25 +010023 *
24 * \param[in] key Key slot
25 * \param[in] type Key type
26 * \param[in] data Key data to import
27 * \param[in] data_length Length in bytes of the data field
28 *
29 * \return Return values as described in \ref tfm_crypto_err_t
30 */
31enum tfm_crypto_err_t tfm_crypto_veneer_import_key(psa_key_slot_t key,
32 psa_key_type_t type,
33 const uint8_t *data,
34 size_t data_length);
35/**
36 * \brief Destroy the key in the provided key slot (veneer function)
37 *
38 * \param[in] key Key slot
39 *
40 * \return Return values as described in \ref tfm_crypto_err_t
41 */
42enum tfm_crypto_err_t tfm_crypto_veneer_destroy_key(psa_key_slot_t key);
43
44/**
45 * \brief Retrieve key information for the provided key slot (veneer function)
46 *
47 * \param[in] key Key slot
48 * \param[out] type Key type associated to the key slot requested
49 * \param[out] bits Length in bits of the key in the requested slot
50 *
51 * \return Return values as described in \ref tfm_crypto_err_t
52 */
53enum tfm_crypto_err_t tfm_crypto_veneer_get_key_information(
54 psa_key_slot_t key,
55 psa_key_type_t *type,
56 size_t *bits);
57/**
58 * \brief Export the key contained in the provided key slot (veneer function)
59 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010060 * \param[in] key Key slot
Antonio de Angelis8908f472018-08-31 15:44:25 +010061 * \param[out] data Buffer to hold the exported key
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010062 * \param[in] data_size Length of the buffer pointed to by data
Antonio de Angelis8908f472018-08-31 15:44:25 +010063 * \param[out] data_length Length of the exported key
64 *
65 * \return Return values as described in \ref tfm_crypto_err_t
66 */
67enum tfm_crypto_err_t tfm_crypto_veneer_export_key(psa_key_slot_t key,
68 uint8_t *data,
69 size_t data_size,
70 size_t *data_length);
Jamie Foxefd82732018-11-26 10:34:32 +000071
72/**
73 * \brief Initialise the key policy to a default that forbids any use of the
74 * key (veneer function)
75 *
76 * \param[out] policy Key policy to initialise
77 *
78 * \return Return values as described in \ref tfm_crypto_err_t
79 */
80enum tfm_crypto_err_t tfm_crypto_veneer_key_policy_init(
81 psa_key_policy_t *policy);
82
83/**
84 * \brief Set the permitted usage and algorithm for the provided key policy
85 * (veneer function)
86 *
87 * \param[out] policy Key policy to modify
88 * \param[in] usage Permitted usage
89 * \param[in] alg Permitted algorithm
90 *
91 * \return Return values as described in \ref tfm_crypto_err_t
92 */
93enum tfm_crypto_err_t tfm_crypto_veneer_key_policy_set_usage(
94 psa_key_policy_t *policy,
95 psa_key_usage_t usage,
96 psa_algorithm_t alg);
97
98/**
99 * \brief Get the permitted usage for the provided key policy (veneer function)
100 *
101 * \param[in] policy Key policy
102 * \param[out] usage Permitted usage for this key policy
103 *
104 * \return Return values as described in \ref tfm_crypto_err_t
105 */
106enum tfm_crypto_err_t tfm_crypto_veneer_key_policy_get_usage(
107 const psa_key_policy_t *policy,
108 psa_key_usage_t *usage);
109
110/**
111 * \brief Get the permitted algorithm for the provided key policy
112 * (veneer function)
113 *
114 * \param[in] policy Key policy
115 * \param[out] alg Permitted algorithm for this key policy
116 *
117 * \return Return values as described in \ref tfm_crypto_err_t
118 */
119enum tfm_crypto_err_t tfm_crypto_veneer_key_policy_get_algorithm(
120 const psa_key_policy_t *policy,
121 psa_algorithm_t *alg);
122
123/**
124 * \brief Set the key policy for the provided key slot (veneer function)
125 *
126 * \param[in] key Key slot
127 * \param[in] policy Key policy
128 *
129 * \return Return values as described in \ref tfm_crypto_err_t
130 */
131enum tfm_crypto_err_t tfm_crypto_veneer_set_key_policy(
132 psa_key_slot_t key,
133 const psa_key_policy_t *policy);
134
135/**
136 * \brief Get the key policy for the provided key slot (veneer function)
137 *
138 * \param[in] key Key slot
139 * \param[out] policy Key policy
140 *
141 * \return Return values as described in \ref tfm_crypto_err_t
142 */
143enum tfm_crypto_err_t tfm_crypto_veneer_get_key_policy(
144 psa_key_slot_t key,
145 psa_key_policy_t *policy);
146
147/**
148 * \brief Set the lifetime for the provided key slot (veneer function)
149 *
150 * \param[in] key Key slot
151 * \param[in] lifetime Lifetime value
152 *
153 * \return Return values as described in \ref tfm_crypto_err_t
154 */
155enum tfm_crypto_err_t tfm_crypto_veneer_set_key_lifetime(
156 psa_key_slot_t key,
157 psa_key_lifetime_t lifetime);
158
159/**
160 * \brief Get the lifetime for the provided key slot (veneer function)
161 *
162 * \param[in] key Key slot
163 * \param[out] lifetime Lifetime value
164 *
165 * \return Return values as described in \ref tfm_crypto_err_t
166 */
167enum tfm_crypto_err_t tfm_crypto_veneer_get_key_lifetime(
168 psa_key_slot_t key,
169 psa_key_lifetime_t *lifetime);
170
Antonio de Angelis8908f472018-08-31 15:44:25 +0100171/**
172 * \brief Set the initialisation vector on the provided cipher operation (veneer
173 * function)
174 *
175 * \param[in] operation Cipher operation context
176 * \param[in] iv Buffer that contains the IV
177 * \param[in] iv_length Length of the provided IV
178 *
179 * \return Return values as described in \ref tfm_crypto_err_t
180 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000181enum tfm_crypto_err_t tfm_crypto_veneer_cipher_set_iv(
Antonio de Angelis8908f472018-08-31 15:44:25 +0100182 psa_cipher_operation_t *operation,
183 const unsigned char *iv,
184 size_t iv_length);
185/**
186 * \brief Set the cipher operation using the provided algorithm and key slot,
187 * for encryption context (veneer function)
188 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100189 * \note A successful call to this function initialises a cipher operation
190 * context which will be referred using the operation parameter
191 *
192 * \param[out] operation Cipher operation context
193 * \param[in] key Key slot to bind to the cipher context
194 * \param[in] alg Algorithm to use for the cipher operation
Antonio de Angelis8908f472018-08-31 15:44:25 +0100195 *
196 * \return Return values as described in \ref tfm_crypto_err_t
197 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000198enum tfm_crypto_err_t tfm_crypto_veneer_cipher_encrypt_setup(
Antonio de Angelis8908f472018-08-31 15:44:25 +0100199 psa_cipher_operation_t *operation,
200 psa_key_slot_t key,
201 psa_algorithm_t alg);
202/**
203 * \brief Set the cipher operation using the provided algorithm and key slot,
204 * for decryption context (veneer function)
205 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100206 * \note A successful call to this function initialises a cipher operation
207 * context which will be referred using the operation parameter
208 *
209 * \param[out] operation Cipher operation context
210 * \param[in] key Key slot to bind to the cipher context
211 * \param[in] alg Algorithm to use for the cipher operation
Antonio de Angelis8908f472018-08-31 15:44:25 +0100212 *
213 * \return Return values as described in \ref tfm_crypto_err_t
214 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000215enum tfm_crypto_err_t tfm_crypto_veneer_cipher_decrypt_setup(
Antonio de Angelis8908f472018-08-31 15:44:25 +0100216 psa_cipher_operation_t *operation,
217 psa_key_slot_t key,
218 psa_algorithm_t alg);
219/**
220 * \brief Update the cipher context with a chunk of input data to create a
221 * chunk of encrypted output data (for encryption contexts), or to
222 * decrypt a chunk of encrypted input data to obtain decrypted data
223 * (for decryption contexts) (veneer function)
224 *
225 * \param[in] operation Cipher operation context
226 * \param[in] input_s Pointer to the struct containing input parameters
227 * \param[out] output_s Pointer to the struct containing output parameters
228 *
229 * \return Return values as described in \ref tfm_crypto_err_t
230 */
231enum tfm_crypto_err_t tfm_crypto_veneer_cipher_update(
232 psa_cipher_operation_t *operation,
233 struct psa_cipher_update_input *input_s,
234 struct psa_cipher_update_output *output_s);
235/**
Antonio de Angelis8908f472018-08-31 15:44:25 +0100236 * \brief Finalise a cipher context flushing out any remaining block of
237 * output data (veneer function)
238 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100239 * \note A successful call to this function releases the cipher operation
240 * context provided as parameter
241 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100242 * \param[in,out] operation Cipher operation context
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100243 * \param[out] output Buffer containing output data
244 * \param[in] output_size Size of the output buffer
245 * \param[out] output_length Size of the produced output
Antonio de Angelis8908f472018-08-31 15:44:25 +0100246 *
247 * \return Return values as described in \ref tfm_crypto_err_t
248 */
249enum tfm_crypto_err_t tfm_crypto_veneer_cipher_finish(
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100250 psa_cipher_operation_t *operation,
251 uint8_t *output,
252 size_t output_size,
253 size_t *output_length);
254/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100255 * \brief Abort a cipher operation, clear the operation context provided
256 * (veneer function)
257 *
258 * \note A successful call to this function releases the cipher operation
259 * context provided as parameter
260 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100261 * \param[in,out] operation Cipher operation context
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100262 *
263 * \return Return values as described in \ref tfm_crypto_err_t
264 */
265enum tfm_crypto_err_t tfm_crypto_veneer_cipher_abort(
266 psa_cipher_operation_t *operation);
267/**
Antonio de Angelis377a1552018-11-22 17:02:40 +0000268 * \brief Setup a hash operation with the provided algorithm (veneer function)
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100269 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100270 * \note A successful call to this function initialises a hash operation
271 * context which will be referred using the operation parameter
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100272 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100273 * \param[out] operation Hash operation context
274 * \param[in] alg Algorithm chosen as hash
275 *
276 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100277 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000278enum tfm_crypto_err_t tfm_crypto_veneer_hash_setup(
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100279 psa_hash_operation_t *operation,
280 psa_algorithm_t alg);
281/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100282 * \brief Add a new input chunk to the data for which the final hash value
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100283 * will be computed (veneer function)
284 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100285 * \param[in,out] operation Hash operation context
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100286 * \param[in] input Buffer containing the input data
287 * \param[in] input_length Size of the provided input data
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100288 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100289 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100290 */
291enum tfm_crypto_err_t tfm_crypto_veneer_hash_update(
292 psa_hash_operation_t *operation,
293 const uint8_t *input,
294 size_t input_length);
295/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100296 * \brief Finalise a hash context operation producing the final hash value
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100297 * (veneer function)
298 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100299 * \note A successful call to this function releases the hash operation
300 * context provided as parameter
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100301 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100302 * \param[in,out] operation Hash operation context
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100303 * \param[out] hash Buffer containing hash data
304 * \param[in] hash_size Size of the hash buffer
305 * \param[out] hash_length Size of the produced hash
306 *
307 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100308 */
309enum tfm_crypto_err_t tfm_crypto_veneer_hash_finish(
310 psa_hash_operation_t *operation,
311 uint8_t *hash,
312 size_t hash_size,
313 size_t *hash_length);
314/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100315 * \brief Finalise a hash context operation, verifying that the final hash
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100316 * value matches the one provided as input (veneer function)
317 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100318 * \note A successful call to this function releases the hash operation
319 * context provided as parameter. The hash operation is released
320 * also in case TFM_CRYPTO_ERR_PSA_ERROR_INVALID_SIGNATURE is returned
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100321 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100322 * \param[in,out] operation Hash operation context
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100323 * \param[in] hash Buffer containing the provided hash value
324 * \param[in] hash_length Size of the provided hash value
325 *
326 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100327 */
328enum tfm_crypto_err_t tfm_crypto_veneer_hash_verify(
329 psa_hash_operation_t *operation,
330 const uint8_t *hash,
331 size_t hash_length);
332/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100333 * \brief Abort a hash operation, clears the operation context provided
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100334 * (veneer function)
335 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100336 * \note A successful call to this function releases the hash operation
337 * context provided as parameter
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100338 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100339 * \param[in,out] operation Hash operation context
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100340 *
341 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100342 */
343enum tfm_crypto_err_t tfm_crypto_veneer_hash_abort(
344 psa_hash_operation_t *operation);
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100345/**
346 * \brief Start a MAC operation with the provided algorithm (for signing)
347 * (veneer function)
348 *
349 * \note A successful call to this function initialises a MAC operation
350 * context which will be referred using the operation parameter
351 *
352 * \param[out] operation MAC operation context
353 * \param[in] key Key slot to bind to the MAC context
354 * \param[in] alg Algorithm chosen as MAC
355 *
356 * \return Return values as described in \ref tfm_crypto_err_t
357 */
358enum tfm_crypto_err_t tfm_crypto_veneer_mac_sign_setup(
359 psa_mac_operation_t *operation,
360 psa_key_slot_t key,
361 psa_algorithm_t alg);
362/**
363 * \brief Start a MAC operation with the provided algorithm (for verifying)
364 * (veneer function)
365 *
366 * \note A successful call to this function initialises a MAC operation
367 * context which will be referred using the operation parameter
368 *
369 * \param[out] operation MAC operation context
370 * \param[in] key Key slot to bind to the MAC context
371 * \param[in] alg Algorithm chosen as MAC
372 *
373 * \return Return values as described in \ref tfm_crypto_err_t
374 */
375enum tfm_crypto_err_t tfm_crypto_veneer_mac_verify_setup(
376 psa_mac_operation_t *operation,
377 psa_key_slot_t key,
378 psa_algorithm_t alg);
379/**
380 * \brief Adds a new input chunk to the data for which the final MAC value
381 * will be computed (veneer function)
382 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100383 * \param[in,out] operation MAC operation context
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100384 * \param[in] input Buffer containing the input data
385 * \param[in] input_length Size of the provided input data
386 *
387 * \return Return values as described in \ref tfm_crypto_err_t
388 */
389enum tfm_crypto_err_t tfm_crypto_veneer_mac_update(
390 psa_mac_operation_t *operation,
391 const uint8_t *input,
392 size_t input_length);
393
394/**
395 * \brief Finalises a MAC context operation producing the final MAC value
396 * (veneer function)
397 *
398 * \note A successful call to this function releases the MAC operation
399 * context provided as parameter
400 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100401 * \param[in,out] operation MAC operation context
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100402 * \param[out] mac Buffer containing MAC data
403 * \param[in] mac_size Size of the MAC buffer
404 * \param[out] mac_length Size of the produced MAC
405 *
406 * \return Return values as described in \ref tfm_crypto_err_t
407 */
408enum tfm_crypto_err_t tfm_crypto_veneer_mac_sign_finish(
409 psa_mac_operation_t *operation,
410 uint8_t *mac,
411 size_t mac_size,
412 size_t *mac_length);
413/**
414 * \brief Finalise a MAC context operation, verifying that the final MAC value
415 * matches the one provided as input (veneer function)
416 *
417 * \note A successful call to this function releases the MAC operation
418 * context provided as parameter. The MAC operation is released
419 * also in case TFM_CRYPTO_ERR_PSA_ERROR_INVALID_SIGNATURE is returned
420 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100421 * \param[in,out] operation MAC operation context
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100422 * \param[in] mac Buffer containing the provided MAC value
423 * \param[in] mac_length Size of the provided MAC value
424 *
425 * \return Return values as described in \ref tfm_crypto_err_t
426 */
427enum tfm_crypto_err_t tfm_crypto_veneer_mac_verify_finish(
428 psa_mac_operation_t *operation,
429 const uint8_t *mac,
430 size_t mac_length);
431/**
432 * \brief Abort a MAC operation, clear the operation context provided
433 * (veneer function)
434 *
435 * \note A successful call to this function releases the MAC operation
436 * context provided as parameter
437 *
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100438 * \param[in,out] operation MAC operation context
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100439 *
440 * \return Return values as described in \ref tfm_crypto_err_t
441 */
442enum tfm_crypto_err_t tfm_crypto_veneer_mac_abort(
443 psa_mac_operation_t *operation);
Antonio de Angelis3a480992018-11-07 11:53:28 +0000444/**
445 * \brief Perform an AEAD encryption operation on input data with additional
446 * data to be authenticated, producing ciphertext in output with an
447 * appended authentication tag (veneer function)
448 *
449 * \param[in] input_s Pointer to the struct containing input parameters
450 * \param[out] output_s Pointer to the struct containing output parameters
451 *
452 * \return Return values as described in \ref tfm_crypto_err_t
453 */
454enum tfm_crypto_err_t tfm_crypto_veneer_aead_encrypt(
455 struct psa_aead_encrypt_input *input_s,
456 struct psa_aead_encrypt_output *output_s);
457/**
458 * \brief Perform an AEAD decryption operation on input data with additional
459 * data to be verified, producing back the original plain text in case
460 * the verification of the authentication tag is successful (veneer
461 * function)
462 *
463 * \param[in] input_s Pointer to the struct containing input parameters
464 * \param[out] output_s Pointer to the struct containing output parameters
465 *
466 * \return Return values as described in \ref tfm_crypto_err_t
467 */
468enum tfm_crypto_err_t tfm_crypto_veneer_aead_decrypt(
469 struct psa_aead_decrypt_input *input_s,
470 struct psa_aead_decrypt_output *output_s);
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100471
Antonio de Angelis8908f472018-08-31 15:44:25 +0100472#ifdef __cplusplus
473}
474#endif
475
476#endif /* __TFM_CRYPTO_VENEERS_H__ */