blob: 36388858322e88d766f691f6b79d6bc9118a85f4 [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,
30 TFM_CRYPTO_KEY_POLICY = 4,
31
32 /* Used to force the enum size */
33 TFM_CRYPTO_OPERATION_TYPE_MAX = INT_MAX
34};
35
36/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010037 * \brief Initialise the service
Antonio de Angelis8908f472018-08-31 15:44:25 +010038 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010039 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +010040 */
41enum tfm_crypto_err_t tfm_crypto_init(void);
42
43/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010044 * \brief Initialise the Key module
45 *
46 * \return Return values as described in \ref tfm_crypto_err_t
47 */
48enum tfm_crypto_err_t tfm_crypto_init_key(void);
49
50/**
51 * \brief Initialise the Alloc module
52 *
53 * \return Return values as described in \ref tfm_crypto_err_t
54 */
55enum tfm_crypto_err_t tfm_crypto_init_alloc(void);
56
57/**
58 * \brief Allocate an operation object
Antonio de Angelis8908f472018-08-31 15:44:25 +010059 *
60 * \param[in] type Type of the operation object to allocate
61 * \param[out] handle Pointer to the corresponding handle assigned
62 *
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,
67 uint32_t *handle);
68/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010069 * \brief Release an operation object
Antonio de Angelis8908f472018-08-31 15:44:25 +010070 *
71 * \param[in] handle Pointer to the handle for the release of the
72 * corresponding object
73 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010074 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +010075 */
76enum tfm_crypto_err_t tfm_crypto_operation_release(uint32_t *handle);
77
78/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010079 * \brief Look up an operation object pointer from the corresponding handle
Antonio de Angelis8908f472018-08-31 15:44:25 +010080 *
81 * \param[in] type Type of the operation object to look up
82 * \param[in] handle Handle to the operation object to look up
83 * \param[out] oper Double pointer to the corresponding object
84 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010085 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +010086 */
87enum tfm_crypto_err_t tfm_crypto_operation_lookup(
88 enum tfm_crypto_operation_type type,
Antonio de Angelis377a1552018-11-22 17:02:40 +000089 uint32_t handle,
Antonio de Angelis8908f472018-08-31 15:44:25 +010090 void **oper);
91/**
92 * \brief Import the key data in the provided key slot
93 *
94 * \param[in] key Key slot
95 * \param[in] type Key type
96 * \param[in] data Key data to import
97 * \param[in] data_length Length in bytes of the data field
98 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010099 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100100 */
101enum tfm_crypto_err_t tfm_crypto_import_key(psa_key_slot_t key,
102 psa_key_type_t type,
103 const uint8_t *data,
104 size_t data_length);
105/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100106 * \brief Destroy the key in the provided key slot
Antonio de Angelis8908f472018-08-31 15:44:25 +0100107 *
108 * \param[in] key Key slot
109 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100110 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100111 */
112enum tfm_crypto_err_t tfm_crypto_destroy_key(psa_key_slot_t key);
113
114/**
115 * \brief Retrieve key information for the provided key slot
116 *
117 * \param[in] key Key slot
118 * \param[out] type Key type associated to the key slot requested
119 * \param[out] bits Length in bits of the key in the requested slot
120 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100121 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100122 */
123enum tfm_crypto_err_t tfm_crypto_get_key_information(psa_key_slot_t key,
124 psa_key_type_t *type,
125 size_t *bits);
126/**
127 * \brief Export the key contained in the provided key slot
128 *
129 * \param[in] key Key slot
130 * \param[out] data Buffer to hold the exported key
131 * \param[in] data_size Length of the buffer pointed to by data
132 * \param[out] data_length Length of the exported key
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_export_key(psa_key_slot_t key,
137 uint8_t *data,
138 size_t data_size,
139 size_t *data_length);
140/**
141 * \brief Export the public key contained in the provided key slot
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100142 * for an asymmetric key pair
Antonio de Angelis8908f472018-08-31 15:44:25 +0100143 *
144 * \param[in] key Key slot
145 * \param[out] data Buffer to hold the exported key
146 * \param[in] data_size Length of the buffer pointed to by data
147 * \param[out] data_length Length of the exported key
148 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100149 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100150 */
151enum tfm_crypto_err_t tfm_crypto_export_public_key(psa_key_slot_t key,
152 uint8_t *data,
153 size_t data_size,
154 size_t *data_length);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100155/**
156 * \brief Set the initialisation vector on the provided cipher operation
157 *
158 * \param[in] operation Cipher operation context
159 * \param[in] iv Buffer that contains the IV
160 * \param[in] iv_length Length of the provided IV
161 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100162 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100163 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000164enum tfm_crypto_err_t tfm_crypto_cipher_set_iv(
Antonio de Angelis8908f472018-08-31 15:44:25 +0100165 psa_cipher_operation_t *operation,
166 const unsigned char *iv,
167 size_t iv_length);
168/**
169 * \brief Set the cipher operation using the provided algorithm and key slot,
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100170 * for encryption context
Antonio de Angelis8908f472018-08-31 15:44:25 +0100171 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100172 * \note A successful call to this function initialises a cipher operation
173 * context which will be referred using the operation parameter
Antonio de Angelis8908f472018-08-31 15:44:25 +0100174 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100175 * \param[out] operation Cipher operation context
176 * \param[in] key Key slot to bind to the cipher context
177 * \param[in] alg Algorithm to use for the cipher operation
178 *
179 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100180 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000181enum tfm_crypto_err_t tfm_crypto_cipher_encrypt_setup(
Antonio de Angelis8908f472018-08-31 15:44:25 +0100182 psa_cipher_operation_t *operation,
183 psa_key_slot_t key,
184 psa_algorithm_t alg);
185/**
186 * \brief Set the cipher operation using the provided algorithm and key slot,
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100187 * for decryption context
Antonio de Angelis8908f472018-08-31 15:44:25 +0100188 *
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
Antonio de Angelis8908f472018-08-31 15:44:25 +0100191 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100192 * \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
195 *
196 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100197 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000198enum tfm_crypto_err_t tfm_crypto_cipher_decrypt_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 Update the cipher context with a chunk of input data to create a
204 * chunk of encrypted output data (for encryption contexts), or to
205 * decrypt a chunk of encrypted input data to obtain decrypted data
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100206 * (for decryption contexts)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100207 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100208 * \param[in/out] operation Cipher operation context
209 * \param[in] input Buffer containing input data
210 * \param[in] input_length Input length
211 * \param[out] output Buffer containing output data
212 * \param[in] output_size Size of the output buffer
213 * \param[out] output_length Size of the produced output
Antonio de Angelis8908f472018-08-31 15:44:25 +0100214 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100215 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100216 */
217enum tfm_crypto_err_t tfm_crypto_cipher_update(
218 psa_cipher_operation_t *operation,
219 const uint8_t *input,
220 size_t input_length,
221 unsigned char *output,
222 size_t output_size,
223 size_t *output_length);
224/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100225 * \brief Finalise a cipher context flushing out any remaining block of
Antonio de Angelis8908f472018-08-31 15:44:25 +0100226 * output data
227 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100228 * \note A successful call to this function de-initialises the cipher operation
229 * context provided as parameter
Antonio de Angelis8908f472018-08-31 15:44:25 +0100230 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100231 * \param[in/out] operation Cipher operation context
232 * \param[out] output Buffer containing output data
233 * \param[in] output_size Size of the output buffer
234 * \param[out] output_length Size of the produced output
235 *
236 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100237 */
238enum tfm_crypto_err_t tfm_crypto_cipher_finish(
239 psa_cipher_operation_t *operation,
240 uint8_t *output,
241 size_t output_size,
242 size_t *output_length);
243/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100244 * \brief Abort a cipher operation, clears the operation context provided
Antonio de Angelis8908f472018-08-31 15:44:25 +0100245 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100246 * \note A successful call to this function de-initialises the cipher operation
247 * context provided as parameter
Antonio de Angelis8908f472018-08-31 15:44:25 +0100248 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100249 * \param[in/out] operation Cipher operation context
250 *
251 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100252 */
253enum tfm_crypto_err_t tfm_crypto_cipher_abort(
254 psa_cipher_operation_t *operation);
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100255/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100256 * \brief Start a hash operation with the provided algorithm
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100257 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100258 * \note A successful call to this function initialises a hash operation
259 * context which will be referred using the operation parameter
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100260 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100261 * \param[out] operation Hash operation context
262 * \param[in] alg Algorithm chosen as hash
263 *
264 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100265 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000266enum tfm_crypto_err_t tfm_crypto_hash_setup(psa_hash_operation_t *operation,
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100267 psa_algorithm_t alg);
268/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100269 * \brief Add a new input chunk to the data for which the final hash value
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100270 * will be computed
271 *
272 * \param[in] operation Hash operation context
273 * \param[in] input Buffer containing the input data
274 * \param[in] input_length Size of the provided input data
275 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100276 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100277 */
278enum tfm_crypto_err_t tfm_crypto_hash_update(psa_hash_operation_t *operation,
279 const uint8_t *input,
280 size_t input_length);
281/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100282 * \brief Finalise a hash context operation producing the final hash value
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100283 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100284 * \note A successful call to this function de-initialises the hash operation
285 * context provided as parameter
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100286 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100287 * \param[in/out] operation Hash operation context
288 * \param[out] hash Buffer containing hash data
289 * \param[in] hash_size Size of the hash buffer
290 * \param[out] hash_length Size of the produced hash
291 *
292 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100293 */
294enum tfm_crypto_err_t tfm_crypto_hash_finish(psa_hash_operation_t *operation,
295 uint8_t *hash,
296 size_t hash_size,
297 size_t *hash_length);
298/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100299 * \brief Finalise a hash context operation, verifying that the final hash
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100300 * value matches the one provided as input
301 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100302 * \note A successful call to this function de-initialises the hash operation
303 * context provided as parameter. The hash operation is de-initialised
304 * also in case TFM_CRYPTO_ERR_PSA_ERROR_INVALID_SIGNATURE is returned
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100305 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100306 * \param[in/out] operation Hash operation context
307 * \param[in] hash Buffer containing the provided hash value
308 * \param[in] hash_length Size of the provided hash value
309 *
310 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100311 */
312enum tfm_crypto_err_t tfm_crypto_hash_verify(psa_hash_operation_t *operation,
313 const uint8_t *hash,
314 size_t hash_length);
315/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100316 * \brief Abort a hash operation, clears the operation context provided
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100317 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100318 * \note A successful call to this function de-initialises the hash operation
319 * context provided as parameter
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100320 *
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100321 * \param[in/out] operation Hash operation context
322 *
323 * \return Return values as described in \ref tfm_crypto_err_t
Antonio de Angelisa6f72162018-09-05 11:00:37 +0100324 */
325enum tfm_crypto_err_t tfm_crypto_hash_abort(psa_hash_operation_t *operation);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100326
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100327/**
328 * \brief Start a MAC operation with the provided algorithm (for signing)
329 *
330 * \note A successful call to this function initialises a MAC operation
331 * context which will be referred using the operation parameter
332 *
333 * \param[out] operation MAC operation context
334 * \param[in] key Key slot to bind to the MAC context
335 * \param[in] alg Algorithm chosen as MAC
336 *
337 * \return Return values as described in \ref tfm_crypto_err_t
338 */
339enum tfm_crypto_err_t tfm_crypto_mac_sign_setup(psa_mac_operation_t *operation,
340 psa_key_slot_t key,
341 psa_algorithm_t alg);
342/**
343 * \brief Start a MAC operation with the provided algorithm (for verifying)
344 *
345 * \note A successful call to this function initialises a MAC operation
346 * context which will be referred using the operation parameter
347 *
348 * \param[out] operation MAC operation context
349 * \param[in] key Key slot to bind to the MAC context
350 * \param[in] alg Algorithm chosen as MAC
351 *
352 * \return Return values as described in \ref tfm_crypto_err_t
353 */
354enum tfm_crypto_err_t tfm_crypto_mac_verify_setup(
355 psa_mac_operation_t *operation,
356 psa_key_slot_t key,
357 psa_algorithm_t alg);
358/**
359 * \brief Adds a new input chunk to the data for which the final MAC value
360 * will be computed
361 *
362 * \param[in] operation MAC operation context
363 * \param[in] input Buffer containing the input data
364 * \param[in] input_length Size of the provided input data
365 *
366 * \return Return values as described in \ref tfm_crypto_err_t
367 */
368enum tfm_crypto_err_t tfm_crypto_mac_update(psa_mac_operation_t *operation,
369 const uint8_t *input,
370 size_t input_length);
371/**
372 * \brief Finalise a MAC context operation producing the final MAC value
373 *
374 * \param[in/out] operation Mac operation context
375 * \param[out] mac Buffer containing MAC data
376 * \param[in] mac_size Size of the mac buffer
377 * \param[out] mac_length Size of the produced mac
378 *
379 * \return Return values as described in \ref tfm_crypto_err_t
380 */
381enum tfm_crypto_err_t tfm_crypto_mac_sign_finish(psa_mac_operation_t *operation,
382 uint8_t *mac,
383 size_t mac_size,
384 size_t *mac_length);
385/**
386 * \brief Finalise a MAC context operation, verifying that the final MAC value
387 * matches the one provided as input
388 *
389 * \param[in/out] operation MAC operation context
390 * \param[in] mac Buffer containing the provided MAC value
391 * \param[in] mac_length Size of the provided MAC value
392 *
393 * \return Return values as described in \ref tfm_crypto_err_t
394 */
395enum tfm_crypto_err_t tfm_crypto_mac_verify_finish(
396 psa_mac_operation_t *operation,
397 const uint8_t *mac,
398 size_t mac_length);
399/**
400 * \brief Abort a MAC operation, clear the operation context provided
401 *
402 * \param[in/out] operation MAC operation context
403 *
404 * \return Return values as described in \ref tfm_crypto_err_t
405 */
406enum tfm_crypto_err_t tfm_crypto_mac_abort(psa_mac_operation_t *operation);
407
Antonio de Angelis8908f472018-08-31 15:44:25 +0100408#ifdef __cplusplus
409}
410#endif
411
412#endif /* __TFM_CRYPTO_API_H__ */