blob: f3ea31a79d0cdd146d34eefe724fb5955d10459d [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_API_H__
9#define __TFM_CRYPTO_API_H__
10
11#ifdef __cplusplus
12extern "C" {
13#endif
14
15#include <stdint.h>
16#include "tfm_crypto_defs.h"
17#include "psa_crypto.h"
18
19/**
20 * \brief List of possible operation types supported by the TFM based
21 * implementation. This type is needed by the operation allocation,
22 * lookup and release functions.
23 *
24 */
25enum tfm_crypto_operation_type {
26 TFM_CRYPTO_OPERATION_NONE = 0,
27 TFM_CRYPTO_CIPHER_OPERATION = 1,
28 TFM_CRYPTO_MAC_OPERATION = 2,
29 TFM_CRYPTO_HASH_OPERATION = 3,
Antonio de Angelis8908f472018-08-31 15:44:25 +010030
31 /* Used to force the enum size */
32 TFM_CRYPTO_OPERATION_TYPE_MAX = INT_MAX
33};
34
35/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010036 * \brief Initialise the service
Antonio de Angelis8908f472018-08-31 15:44:25 +010037 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010038 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +010039 */
40enum tfm_crypto_err_t tfm_crypto_init(void);
41
42/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010043 * \brief Initialise the Key module
44 *
45 * \return Return values as described in \ref tfm_crypto_err_t
46 */
47enum tfm_crypto_err_t tfm_crypto_init_key(void);
48
49/**
50 * \brief Initialise the Alloc module
51 *
52 * \return Return values as described in \ref tfm_crypto_err_t
53 */
54enum tfm_crypto_err_t tfm_crypto_init_alloc(void);
55
56/**
Antonio de Angelis819c2f32019-02-06 14:32:02 +000057 * \brief Allocate an operation context in the backend
Antonio de Angelis8908f472018-08-31 15:44:25 +010058 *
Antonio de Angelis819c2f32019-02-06 14:32:02 +000059 * \param[in] type Type of the operation context to allocate
60 * \param[out] oper Pointer to the frontend operation
61 * \param[out ctx Double pointer to the corresponding context
Antonio de Angelis8908f472018-08-31 15:44:25 +010062 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010063 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +010064 */
65enum tfm_crypto_err_t tfm_crypto_operation_alloc(
66 enum tfm_crypto_operation_type type,
Antonio de Angelis819c2f32019-02-06 14:32:02 +000067 void *oper,
68 void **ctx);
Antonio de Angelis8908f472018-08-31 15:44:25 +010069/**
Antonio de Angelis819c2f32019-02-06 14:32:02 +000070 * \brief Release an operation context in the backend
Antonio de Angelis8908f472018-08-31 15:44:25 +010071 *
Antonio de Angelis819c2f32019-02-06 14:32:02 +000072 * \param[in] type Type of the operation context to release
73 * \param[in/out] oper Pointer to the frontend operation for the release
74 * of the corresponding backend context
Antonio de Angelis8908f472018-08-31 15:44:25 +010075 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010076 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +010077 */
Antonio de Angelis819c2f32019-02-06 14:32:02 +000078enum tfm_crypto_err_t tfm_crypto_operation_release(
79 enum tfm_crypto_operation_type type,
80 void *oper);
Antonio de Angelis8908f472018-08-31 15:44:25 +010081/**
Antonio de Angelis819c2f32019-02-06 14:32:02 +000082 * \brief Look up an operation context in the backend for the corresponding
83 * frontend operation
Antonio de Angelis8908f472018-08-31 15:44:25 +010084 *
Antonio de Angelis819c2f32019-02-06 14:32:02 +000085 * \param[in] type Type of the operation context to look up
86 * \param[in] oper Pointer to the frontend operation
87 * \param[out] ctx Double pointer to the corresponding context
Antonio de Angelis8908f472018-08-31 15:44:25 +010088 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010089 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +010090 */
91enum tfm_crypto_err_t tfm_crypto_operation_lookup(
92 enum tfm_crypto_operation_type type,
Antonio de Angelis819c2f32019-02-06 14:32:02 +000093 void *oper,
94 void **ctx);
Antonio de Angelis8908f472018-08-31 15:44:25 +010095/**
Jamie Foxefd82732018-11-26 10:34:32 +000096 * \brief Retrieve a key from the provided key slot according to the key
97 * policy and algorithm provided. This function is expected to be
98 * called intra-service
99 *
100 * \param[in] key Key slot
101 * \param[in] usage Usage policy to be used on the retrieved key
102 * \param[in] alg Algorithm to be used for the retrieved key
103 * \param[out] data Buffer to hold the exported key
104 * \param[in] data_size Length of the buffer pointed to by data
105 * \param[out] data_length Length of the exported key
106 *
107 * \return Return values as described in \ref tfm_crypto_err_t
108 */
109enum tfm_crypto_err_t tfm_crypto_get_key(psa_key_slot_t key,
110 psa_key_usage_t usage,
111 psa_algorithm_t alg,
112 uint8_t *data,
113 size_t data_size,
114 size_t *data_length);
115/**
Antonio de Angelis8908f472018-08-31 15:44:25 +0100116 * \brief Import the key data in the provided key slot
117 *
118 * \param[in] key Key slot
119 * \param[in] type Key type
120 * \param[in] data Key data to import
121 * \param[in] data_length Length in bytes of the data field
122 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100123 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100124 */
125enum tfm_crypto_err_t tfm_crypto_import_key(psa_key_slot_t key,
126 psa_key_type_t type,
127 const uint8_t *data,
128 size_t data_length);
129/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100130 * \brief Destroy the key in the provided key slot
Antonio de Angelis8908f472018-08-31 15:44:25 +0100131 *
132 * \param[in] key Key slot
133 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100134 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100135 */
136enum tfm_crypto_err_t tfm_crypto_destroy_key(psa_key_slot_t key);
137
138/**
139 * \brief Retrieve key information for the provided key slot
140 *
141 * \param[in] key Key slot
142 * \param[out] type Key type associated to the key slot requested
143 * \param[out] bits Length in bits of the key in the requested slot
144 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100145 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100146 */
147enum tfm_crypto_err_t tfm_crypto_get_key_information(psa_key_slot_t key,
148 psa_key_type_t *type,
149 size_t *bits);
150/**
151 * \brief Export the key contained in the provided key slot
152 *
153 * \param[in] key Key slot
154 * \param[out] data Buffer to hold the exported key
155 * \param[in] data_size Length of the buffer pointed to by data
156 * \param[out] data_length Length of the exported key
157 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100158 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100159 */
160enum tfm_crypto_err_t tfm_crypto_export_key(psa_key_slot_t key,
161 uint8_t *data,
162 size_t data_size,
163 size_t *data_length);
Jamie Foxefd82732018-11-26 10:34:32 +0000164
165/**
166 * \brief Initialise the key policy to a default that forbids any use of the
167 * key
168 *
169 * \param[out] policy Key policy to initialise
170 *
171 * \return Return values as described in \ref tfm_crypto_err_t
172 */
173enum tfm_crypto_err_t tfm_crypto_key_policy_init(psa_key_policy_t *policy);
174
175/**
176 * \brief Set the permitted usage and algorithm for the provided key policy
177 *
178 * \param[out] policy Key policy to modify
179 * \param[in] usage Permitted usage
180 * \param[in] alg Permitted algorithm
181 *
182 * \return Return values as described in \ref tfm_crypto_err_t
183 */
184enum tfm_crypto_err_t tfm_crypto_key_policy_set_usage(psa_key_policy_t *policy,
185 psa_key_usage_t usage,
186 psa_algorithm_t alg);
187
188/**
189 * \brief Get the permitted usage for the provided key policy
190 *
191 * \param[in] policy Key policy
192 * \param[out] usage Permitted usage for this key policy
193 *
194 * \return Return values as described in \ref tfm_crypto_err_t
195 */
196enum tfm_crypto_err_t tfm_crypto_key_policy_get_usage(
197 const psa_key_policy_t *policy,
198 psa_key_usage_t *usage);
199
200/**
201 * \brief Get the permitted algorithm for the provided key policy
202 *
203 * \param[in] policy Key policy
204 * \param[out] alg Permitted algorithm for this key policy
205 *
206 * \return Return values as described in \ref tfm_crypto_err_t
207 */
208enum tfm_crypto_err_t tfm_crypto_key_policy_get_algorithm(
209 const psa_key_policy_t *policy,
210 psa_algorithm_t *alg);
211
212/**
213 * \brief Set the key policy for the provided key slot
214 *
215 * \param[in] key Key slot
216 * \param[in] policy Key policy
217 *
218 * \return Return values as described in \ref tfm_crypto_err_t
219 */
220enum tfm_crypto_err_t tfm_crypto_set_key_policy(psa_key_slot_t key,
221 const psa_key_policy_t *policy);
222
223/**
224 * \brief Get the key policy for the provided key slot
225 *
226 * \param[in] key Key slot
227 * \param[out] policy Key policy
228 *
229 * \return Return values as described in \ref tfm_crypto_err_t
230 */
231enum tfm_crypto_err_t tfm_crypto_get_key_policy(psa_key_slot_t key,
232 psa_key_policy_t *policy);
233
234/**
235 * \brief Set the lifetime for the provided key slot
236 *
237 * \param[in] key Key slot
238 * \param[in] lifetime Lifetime value
239 *
240 * \return Return values as described in \ref tfm_crypto_err_t
241 */
242enum tfm_crypto_err_t tfm_crypto_set_key_lifetime(psa_key_slot_t key,
243 psa_key_lifetime_t lifetime);
244
245/**
246 * \brief Get the lifetime for the provided key slot
247 *
248 * \param[in] key Key slot
249 * \param[out] lifetime Lifetime value
250 *
251 * \return Return values as described in \ref tfm_crypto_err_t
252 */
253enum tfm_crypto_err_t tfm_crypto_get_key_lifetime(psa_key_slot_t key,
254 psa_key_lifetime_t *lifetime);
255
Antonio de Angelis8908f472018-08-31 15:44:25 +0100256/**
257 * \brief Export the public key contained in the provided key slot
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100258 * for an asymmetric key pair
Antonio de Angelis8908f472018-08-31 15:44:25 +0100259 *
260 * \param[in] key Key slot
261 * \param[out] data Buffer to hold the exported key
262 * \param[in] data_size Length of the buffer pointed to by data
263 * \param[out] data_length Length of the exported key
264 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100265 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100266 */
267enum tfm_crypto_err_t tfm_crypto_export_public_key(psa_key_slot_t key,
268 uint8_t *data,
269 size_t data_size,
270 size_t *data_length);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100271/**
272 * \brief Set the initialisation vector on the provided cipher operation
273 *
274 * \param[in] operation Cipher operation context
275 * \param[in] iv Buffer that contains the IV
276 * \param[in] iv_length Length of the provided IV
277 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100278 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100279 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000280enum tfm_crypto_err_t tfm_crypto_cipher_set_iv(
Antonio de Angelis8908f472018-08-31 15:44:25 +0100281 psa_cipher_operation_t *operation,
282 const unsigned char *iv,
283 size_t iv_length);
284/**
285 * \brief Set the cipher operation using the provided algorithm and key slot,
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100286 * for encryption context
Antonio de Angelis8908f472018-08-31 15:44:25 +0100287 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100288 * \note A successful call to this function initialises a cipher operation
289 * context which will be referred using the operation parameter
Antonio de Angelis8908f472018-08-31 15:44:25 +0100290 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100291 * \param[out] operation Cipher operation context
292 * \param[in] key Key slot to bind to the cipher context
293 * \param[in] alg Algorithm to use for the cipher operation
294 *
295 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100296 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000297enum tfm_crypto_err_t tfm_crypto_cipher_encrypt_setup(
Antonio de Angelis8908f472018-08-31 15:44:25 +0100298 psa_cipher_operation_t *operation,
299 psa_key_slot_t key,
300 psa_algorithm_t alg);
301/**
302 * \brief Set the cipher operation using the provided algorithm and key slot,
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100303 * for decryption context
Antonio de Angelis8908f472018-08-31 15:44:25 +0100304 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100305 * \note A successful call to this function initialises a cipher operation
306 * context which will be referred using the operation parameter
Antonio de Angelis8908f472018-08-31 15:44:25 +0100307 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100308 * \param[out] operation Cipher operation context
309 * \param[in] key Key slot to bind to the cipher context
310 * \param[in] alg Algorithm to use for the cipher operation
311 *
312 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100313 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000314enum tfm_crypto_err_t tfm_crypto_cipher_decrypt_setup(
Antonio de Angelis8908f472018-08-31 15:44:25 +0100315 psa_cipher_operation_t *operation,
316 psa_key_slot_t key,
317 psa_algorithm_t alg);
318/**
319 * \brief Update the cipher context with a chunk of input data to create a
320 * chunk of encrypted output data (for encryption contexts), or to
321 * decrypt a chunk of encrypted input data to obtain decrypted data
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100322 * (for decryption contexts)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100323 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100324 * \param[in/out] operation Cipher operation context
325 * \param[in] input Buffer containing input data
326 * \param[in] input_length Input length
327 * \param[out] output Buffer containing output data
328 * \param[in] output_size Size of the output buffer
329 * \param[out] output_length Size of the produced output
Antonio de Angelis8908f472018-08-31 15:44:25 +0100330 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100331 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100332 */
333enum tfm_crypto_err_t tfm_crypto_cipher_update(
334 psa_cipher_operation_t *operation,
335 const uint8_t *input,
336 size_t input_length,
337 unsigned char *output,
338 size_t output_size,
339 size_t *output_length);
340/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100341 * \brief Finalise a cipher context flushing out any remaining block of
Antonio de Angelis8908f472018-08-31 15:44:25 +0100342 * output data
343 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100344 * \note A successful call to this function de-initialises the cipher operation
345 * context provided as parameter
Antonio de Angelis8908f472018-08-31 15:44:25 +0100346 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100347 * \param[in/out] operation Cipher operation context
348 * \param[out] output Buffer containing output data
349 * \param[in] output_size Size of the output buffer
350 * \param[out] output_length Size of the produced output
351 *
352 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100353 */
354enum tfm_crypto_err_t tfm_crypto_cipher_finish(
355 psa_cipher_operation_t *operation,
356 uint8_t *output,
357 size_t output_size,
358 size_t *output_length);
359/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100360 * \brief Abort a cipher operation, clears the operation context provided
Antonio de Angelis8908f472018-08-31 15:44:25 +0100361 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100362 * \note A successful call to this function de-initialises the cipher operation
363 * context provided as parameter
Antonio de Angelis8908f472018-08-31 15:44:25 +0100364 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100365 * \param[in/out] operation Cipher operation context
366 *
367 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100368 */
369enum tfm_crypto_err_t tfm_crypto_cipher_abort(
370 psa_cipher_operation_t *operation);
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100371/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100372 * \brief Start a hash operation with the provided algorithm
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100373 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100374 * \note A successful call to this function initialises a hash operation
375 * context which will be referred using the operation parameter
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100376 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100377 * \param[out] operation Hash operation context
378 * \param[in] alg Algorithm chosen as hash
379 *
380 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100381 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000382enum tfm_crypto_err_t tfm_crypto_hash_setup(psa_hash_operation_t *operation,
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100383 psa_algorithm_t alg);
384/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100385 * \brief Add a new input chunk to the data for which the final hash value
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100386 * will be computed
387 *
388 * \param[in] operation Hash operation context
389 * \param[in] input Buffer containing the input data
390 * \param[in] input_length Size of the provided input data
391 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100392 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100393 */
394enum tfm_crypto_err_t tfm_crypto_hash_update(psa_hash_operation_t *operation,
395 const uint8_t *input,
396 size_t input_length);
397/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100398 * \brief Finalise a hash context operation producing the final hash value
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100399 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100400 * \note A successful call to this function de-initialises the hash operation
401 * context provided as parameter
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100402 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100403 * \param[in/out] operation Hash operation context
404 * \param[out] hash Buffer containing hash data
405 * \param[in] hash_size Size of the hash buffer
406 * \param[out] hash_length Size of the produced hash
407 *
408 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100409 */
410enum tfm_crypto_err_t tfm_crypto_hash_finish(psa_hash_operation_t *operation,
411 uint8_t *hash,
412 size_t hash_size,
413 size_t *hash_length);
414/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100415 * \brief Finalise a hash context operation, verifying that the final hash
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100416 * value matches the one provided as input
417 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100418 * \note A successful call to this function de-initialises the hash operation
419 * context provided as parameter. The hash operation is de-initialised
420 * also in case TFM_CRYPTO_ERR_PSA_ERROR_INVALID_SIGNATURE is returned
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100421 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100422 * \param[in/out] operation Hash operation context
423 * \param[in] hash Buffer containing the provided hash value
424 * \param[in] hash_length Size of the provided hash value
425 *
426 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100427 */
428enum tfm_crypto_err_t tfm_crypto_hash_verify(psa_hash_operation_t *operation,
429 const uint8_t *hash,
430 size_t hash_length);
431/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100432 * \brief Abort a hash operation, clears the operation context provided
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100433 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100434 * \note A successful call to this function de-initialises the hash operation
435 * context provided as parameter
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100436 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100437 * \param[in/out] operation Hash operation context
438 *
439 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100440 */
441enum tfm_crypto_err_t tfm_crypto_hash_abort(psa_hash_operation_t *operation);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100442
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100443/**
444 * \brief Start a MAC operation with the provided algorithm (for signing)
445 *
446 * \note A successful call to this function initialises a MAC operation
447 * context which will be referred using the operation parameter
448 *
449 * \param[out] operation MAC operation context
450 * \param[in] key Key slot to bind to the MAC context
451 * \param[in] alg Algorithm chosen as MAC
452 *
453 * \return Return values as described in \ref tfm_crypto_err_t
454 */
455enum tfm_crypto_err_t tfm_crypto_mac_sign_setup(psa_mac_operation_t *operation,
456 psa_key_slot_t key,
457 psa_algorithm_t alg);
458/**
459 * \brief Start a MAC operation with the provided algorithm (for verifying)
460 *
461 * \note A successful call to this function initialises a MAC operation
462 * context which will be referred using the operation parameter
463 *
464 * \param[out] operation MAC operation context
465 * \param[in] key Key slot to bind to the MAC context
466 * \param[in] alg Algorithm chosen as MAC
467 *
468 * \return Return values as described in \ref tfm_crypto_err_t
469 */
470enum tfm_crypto_err_t tfm_crypto_mac_verify_setup(
471 psa_mac_operation_t *operation,
472 psa_key_slot_t key,
473 psa_algorithm_t alg);
474/**
475 * \brief Adds a new input chunk to the data for which the final MAC value
476 * will be computed
477 *
478 * \param[in] operation MAC operation context
479 * \param[in] input Buffer containing the input data
480 * \param[in] input_length Size of the provided input data
481 *
482 * \return Return values as described in \ref tfm_crypto_err_t
483 */
484enum tfm_crypto_err_t tfm_crypto_mac_update(psa_mac_operation_t *operation,
485 const uint8_t *input,
486 size_t input_length);
487/**
488 * \brief Finalise a MAC context operation producing the final MAC value
489 *
490 * \param[in/out] operation Mac operation context
491 * \param[out] mac Buffer containing MAC data
492 * \param[in] mac_size Size of the mac buffer
493 * \param[out] mac_length Size of the produced mac
494 *
495 * \return Return values as described in \ref tfm_crypto_err_t
496 */
497enum tfm_crypto_err_t tfm_crypto_mac_sign_finish(psa_mac_operation_t *operation,
498 uint8_t *mac,
499 size_t mac_size,
500 size_t *mac_length);
501/**
502 * \brief Finalise a MAC context operation, verifying that the final MAC value
503 * matches the one provided as input
504 *
505 * \param[in/out] operation MAC operation context
506 * \param[in] mac Buffer containing the provided MAC value
507 * \param[in] mac_length Size of the provided MAC value
508 *
509 * \return Return values as described in \ref tfm_crypto_err_t
510 */
511enum tfm_crypto_err_t tfm_crypto_mac_verify_finish(
512 psa_mac_operation_t *operation,
513 const uint8_t *mac,
514 size_t mac_length);
515/**
516 * \brief Abort a MAC operation, clear the operation context provided
517 *
518 * \param[in/out] operation MAC operation context
519 *
520 * \return Return values as described in \ref tfm_crypto_err_t
521 */
522enum tfm_crypto_err_t tfm_crypto_mac_abort(psa_mac_operation_t *operation);
523
Antonio de Angelis3a480992018-11-07 11:53:28 +0000524/**
525 * \brief Perform an AEAD encryption operation on input data with additional
526 * data to be authenticated, producing ciphertext in output with an
527 * appended authentication tag
528 *
529 * \param[in] key Key slot for the key
530 * \param[in] alg Algorithm to be used
531 * \param[in] nonce Pointer to a buffer holding a nonce or IV
532 * to use
533 * \param[in] nonce_length Size in bytes of the nonce or IV data
534 * \param[in] additional_data Additional information to be authenticated
535 * \param[in] additional_data_length Size in bytes of the additional data
536 * \param[in] plaintext Buffer pointing to data to be encrypted
537 * \param[in] plaintext_length Size in bytes of the plain text buffer
538 * \param[out] ciphertext Output encrypted data, with the
539 * authentication tag appended
540 * \param[in] ciphertext_size Size in bytes of the buffer to hold the
541 * cipher text plus authentication tag
542 * \param[out] ciphertext_length Size of the ciphertext plus tag produced
543 * as output
544 *
545 * \return Return values as described in \ref tfm_crypto_err_t
546 */
547enum tfm_crypto_err_t tfm_crypto_aead_encrypt(psa_key_slot_t key,
548 psa_algorithm_t alg,
549 const uint8_t *nonce,
550 size_t nonce_length,
551 const uint8_t *additional_data,
552 size_t additional_data_length,
553 const uint8_t *plaintext,
554 size_t plaintext_length,
555 uint8_t *ciphertext,
556 size_t ciphertext_size,
557 size_t *ciphertext_length);
558/**
559 * \brief Perform an AEAD decryption operation on input data with additional
560 * data to be verified, producing back the original plain text in case
561 * the verification of the authentication tag is successful
562 *
563 * \param[in] key Key slot for the key
564 * \param[in] alg Algorithm to be used
565 * \param[in] nonce Pointer to a buffer holding a nonce or IV
566 * to use
567 * \param[in] nonce_length Size in bytes of the nonce or IV data
568 * \param[in] additional_data Additional information which was
569 * authenticated but not encrypted
570 * \param[in] additional_data_length Size in bytes of the additional data
571 * \param[in] ciphertext Buffer pointing to data be decrypted
572 * \param[in] ciphertext_length Size in bytes of the cipher text buffer
573 * \param[out] plaintext Buffer for decrypted output data
574 * \param[in] plaintext_size Size in bytes of the buffer to hold the
575 * plain text
576 * \param[out] plaintext_length Size of the plain text actually produced
577 *
578 * \return Return values as described in \ref tfm_crypto_err_t
579 */
580enum tfm_crypto_err_t tfm_crypto_aead_decrypt(psa_key_slot_t key,
581 psa_algorithm_t alg,
582 const uint8_t *nonce,
583 size_t nonce_length,
584 const uint8_t *additional_data,
585 size_t additional_data_length,
586 const uint8_t *ciphertext,
587 size_t ciphertext_length,
588 uint8_t *plaintext,
589 size_t plaintext_size,
590 size_t *plaintext_length);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100591#ifdef __cplusplus
592}
593#endif
594
595#endif /* __TFM_CRYPTO_API_H__ */