blob: 1e0c9d09f0b81ec418ad0f37898a3fce9e8b6565 [file] [log] [blame]
Antonio de Angelis14276e92018-07-10 14:35:43 +01001/*
Mate Toth-Pal2a6f8c22018-12-13 16:37:17 +01002 * Copyright (c) 2018-2019, Arm Limited. All rights reserved.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01007/**
Jamie Foxcc31d402019-01-28 17:13:52 +00008 * \file psa/crypto.h
Antonio de Angelis14276e92018-07-10 14:35:43 +01009 * \brief Platform Security Architecture cryptography module
10 */
11
12#ifndef PSA_CRYPTO_H
13#define PSA_CRYPTO_H
14
Jamie Foxcc31d402019-01-28 17:13:52 +000015#include "psa/crypto_platform.h"
Antonio de Angelis8908f472018-08-31 15:44:25 +010016
Antonio de Angelis14276e92018-07-10 14:35:43 +010017#include <stddef.h>
18
19#ifdef __DOXYGEN_ONLY__
20/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
Jamie Fox0e54ebc2019-04-09 14:21:04 +010021 * must be defined in the crypto_platform.h header. These mock definitions
Antonio de Angelis14276e92018-07-10 14:35:43 +010022 * are present in this file as a convenience to generate pretty-printed
Antonio de Angelis377a1552018-11-22 17:02:40 +000023 * documentation that includes those definitions. */
Antonio de Angelis14276e92018-07-10 14:35:43 +010024
25/** \defgroup platform Implementation-specific definitions
26 * @{
27 */
28
Jamie Fox0e54ebc2019-04-09 14:21:04 +010029/** \brief Key handle.
Antonio de Angelis14276e92018-07-10 14:35:43 +010030 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +010031 * This type represents open handles to keys. It must be an unsigned integral
Antonio de Angelis14276e92018-07-10 14:35:43 +010032 * type. The choice of type is implementation-dependent.
Antonio de Angelis14276e92018-07-10 14:35:43 +010033 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +010034 * 0 is not a valid key handle. How other handle values are assigned is
35 * implementation-dependent.
Antonio de Angelis14276e92018-07-10 14:35:43 +010036 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010037typedef _unsigned_integral_type_ psa_key_handle_t;
Antonio de Angelis14276e92018-07-10 14:35:43 +010038
39/**@}*/
40#endif /* __DOXYGEN_ONLY__ */
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
Jamie Fox0e54ebc2019-04-09 14:21:04 +010046/* The file "crypto_types.h" declares types that encode errors,
47 * algorithms, key types, policies, etc. */
Jamie Foxcc31d402019-01-28 17:13:52 +000048#include "psa/crypto_types.h"
Jamie Fox0e54ebc2019-04-09 14:21:04 +010049
50/* The file "crypto_values.h" declares macros to build and analyze values
51 * of integral types defined in "crypto_types.h". */
Jamie Foxcc31d402019-01-28 17:13:52 +000052#include "psa/crypto_values.h"
Jamie Fox0e54ebc2019-04-09 14:21:04 +010053
54/** \defgroup initialization Library initialization
Antonio de Angelis14276e92018-07-10 14:35:43 +010055 * @{
56 */
57
Antonio de Angelis14276e92018-07-10 14:35:43 +010058/**
59 * \brief Library initialization.
60 *
61 * Applications must call this function before calling any other
62 * function in this module.
63 *
64 * Applications may call this function more than once. Once a call
65 * succeeds, subsequent calls are guaranteed to succeed.
66 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +010067 * If the application calls other functions before calling psa_crypto_init(),
68 * the behavior is undefined. Implementations are encouraged to either perform
69 * the operation as if the library had been initialized or to return
70 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
71 * implementations should not return a success status if the lack of
72 * initialization may have security implications, for example due to improper
73 * seeding of the random number generator.
74 *
Antonio de Angelis377a1552018-11-22 17:02:40 +000075 * \retval #PSA_SUCCESS
76 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
77 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
78 * \retval #PSA_ERROR_HARDWARE_FAILURE
79 * \retval #PSA_ERROR_TAMPERING_DETECTED
80 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Antonio de Angelis14276e92018-07-10 14:35:43 +010081 */
82psa_status_t psa_crypto_init(void);
83
Antonio de Angelis14276e92018-07-10 14:35:43 +010084/**@}*/
85
86/** \defgroup policy Key policies
87 * @{
88 */
89
Antonio de Angelis377a1552018-11-22 17:02:40 +000090/** The type of the key policy data structure.
91 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +010092 * Before calling any function on a key policy, the application must initialize
93 * it by any of the following means:
94 * - Set the structure to all-bits-zero, for example:
95 * \code
96 * psa_key_policy_t policy;
97 * memset(&policy, 0, sizeof(policy));
98 * \endcode
99 * - Initialize the structure to logical zero values, for example:
100 * \code
101 * psa_key_policy_t policy = {0};
102 * \endcode
103 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
104 * for example:
105 * \code
106 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
107 * \endcode
108 * - Assign the result of the function psa_key_policy_init()
109 * to the structure, for example:
110 * \code
111 * psa_key_policy_t policy;
112 * policy = psa_key_policy_init();
113 * \endcode
114 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000115 * This is an implementation-defined \c struct. Applications should not
116 * make any assumptions about the content of this structure except
117 * as directed by the documentation of a specific implementation. */
118typedef struct psa_key_policy_s psa_key_policy_t;
Antonio de Angelis14276e92018-07-10 14:35:43 +0100119
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100120/** \def PSA_KEY_POLICY_INIT
Antonio de Angelis377a1552018-11-22 17:02:40 +0000121 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100122 * This macro returns a suitable initializer for a key policy object of type
123 * #psa_key_policy_t.
Antonio de Angelis8908f472018-08-31 15:44:25 +0100124 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100125#ifdef __DOXYGEN_ONLY__
126/* This is an example definition for documentation purposes.
127 * Implementations should define a suitable value in `crypto_struct.h`.
128 */
129#define PSA_KEY_POLICY_INIT {0}
130#endif
131
132/** Return an initial value for a key policy that forbids all usage of the key.
133 */
134static psa_key_policy_t psa_key_policy_init(void);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100135
136/** \brief Set the standard fields of a policy structure.
137 *
138 * Note that this function does not make any consistency check of the
139 * parameters. The values are only checked when applying the policy to
140 * a key slot with psa_set_key_policy().
Antonio de Angelis377a1552018-11-22 17:02:40 +0000141 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100142 * \param[in,out] policy The key policy to modify. It must have been
143 * initialized as per the documentation for
144 * #psa_key_policy_t.
145 * \param usage The permitted uses for the key.
146 * \param alg The algorithm that the key may be used for.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100147 */
148void psa_key_policy_set_usage(psa_key_policy_t *policy,
149 psa_key_usage_t usage,
150 psa_algorithm_t alg);
151
Antonio de Angelis377a1552018-11-22 17:02:40 +0000152/** \brief Retrieve the usage field of a policy structure.
153 *
154 * \param[in] policy The policy object to query.
155 *
156 * \return The permitted uses for a key with this policy.
157 */
158psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100159
Antonio de Angelis377a1552018-11-22 17:02:40 +0000160/** \brief Retrieve the algorithm field of a policy structure.
161 *
162 * \param[in] policy The policy object to query.
163 *
164 * \return The permitted algorithm for a key with this policy.
165 */
166psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100167
168/** \brief Set the usage policy on a key slot.
169 *
170 * This function must be called on an empty key slot, before importing,
171 * generating or creating a key in the slot. Changing the policy of an
172 * existing key is not permitted.
173 *
174 * Implementations may set restrictions on supported key policies
175 * depending on the key type and the key slot.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000176 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100177 * \param handle Handle to the key whose policy is to be changed.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000178 * \param[in] policy The policy object to query.
179 *
180 * \retval #PSA_SUCCESS
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100181 * Success.
182 * If the key is persistent, it is implementation-defined whether
183 * the policy has been saved to persistent storage. Implementations
184 * may defer saving the policy until the key material is created.
185 * \retval #PSA_ERROR_INVALID_HANDLE
186 * \retval #PSA_ERROR_ALREADY_EXISTS
Antonio de Angelis377a1552018-11-22 17:02:40 +0000187 * \retval #PSA_ERROR_NOT_SUPPORTED
188 * \retval #PSA_ERROR_INVALID_ARGUMENT
189 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
190 * \retval #PSA_ERROR_HARDWARE_FAILURE
191 * \retval #PSA_ERROR_TAMPERING_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100192 * \retval #PSA_ERROR_BAD_STATE
193 * The library has not been previously initialized by psa_crypto_init().
194 * It is implementation-dependent whether a failure to initialize
195 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100196 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100197psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Antonio de Angelis14276e92018-07-10 14:35:43 +0100198 const psa_key_policy_t *policy);
199
Antonio de Angelis377a1552018-11-22 17:02:40 +0000200/** \brief Get the usage policy for a key slot.
201 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100202 * \param handle Handle to the key slot whose policy is being queried.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000203 * \param[out] policy On success, the key's policy.
204 *
205 * \retval #PSA_SUCCESS
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100206 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +0000207 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
208 * \retval #PSA_ERROR_HARDWARE_FAILURE
209 * \retval #PSA_ERROR_TAMPERING_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100210 * \retval #PSA_ERROR_BAD_STATE
211 * The library has not been previously initialized by psa_crypto_init().
212 * It is implementation-dependent whether a failure to initialize
213 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000214 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100215psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Antonio de Angelis14276e92018-07-10 14:35:43 +0100216 psa_key_policy_t *policy);
217
218/**@}*/
219
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100220/** \defgroup key_management Key management
Antonio de Angelis14276e92018-07-10 14:35:43 +0100221 * @{
222 */
223
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100224/** \brief Retrieve the lifetime of an open key.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100225 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100226 * \param handle Handle to query.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000227 * \param[out] lifetime On success, the lifetime value.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100228 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000229 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +0100230 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100231 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +0000232 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
233 * \retval #PSA_ERROR_HARDWARE_FAILURE
234 * \retval #PSA_ERROR_TAMPERING_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100235 * \retval #PSA_ERROR_BAD_STATE
236 * The library has not been previously initialized by psa_crypto_init().
237 * It is implementation-dependent whether a failure to initialize
238 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100239 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100240psa_status_t psa_get_key_lifetime(psa_key_handle_t handle,
Antonio de Angelis14276e92018-07-10 14:35:43 +0100241 psa_key_lifetime_t *lifetime);
242
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100243
244/** Allocate a key slot for a transient key, i.e. a key which is only stored
245 * in volatile memory.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100246 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100247 * The allocated key slot and its handle remain valid until the
248 * application calls psa_close_key() or psa_destroy_key() or until the
249 * application terminates.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100250 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100251 * \param[out] handle On success, a handle to a volatile key slot.
252 *
253 * \retval #PSA_SUCCESS
254 * Success. The application can now use the value of `*handle`
255 * to access the newly allocated key slot.
256 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
257 * There was not enough memory, or the maximum number of key slots
258 * has been reached.
259 */
260psa_status_t psa_allocate_key(psa_key_handle_t *handle);
261
262/** Open a handle to an existing persistent key.
263 *
264 * Open a handle to a key which was previously created with psa_create_key().
265 *
266 * \param lifetime The lifetime of the key. This designates a storage
267 * area where the key material is stored. This must not
268 * be #PSA_KEY_LIFETIME_VOLATILE.
269 * \param id The persistent identifier of the key.
270 * \param[out] handle On success, a handle to a key slot which contains
271 * the data and metadata loaded from the specified
272 * persistent location.
273 *
274 * \retval #PSA_SUCCESS
275 * Success. The application can now use the value of `*handle`
276 * to access the newly allocated key slot.
277 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
278 * \retval #PSA_ERROR_DOES_NOT_EXIST
279 * \retval #PSA_ERROR_INVALID_ARGUMENT
280 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
281 * \retval #PSA_ERROR_INVALID_ARGUMENT
282 * \p id is invalid for the specified lifetime.
283 * \retval #PSA_ERROR_NOT_SUPPORTED
284 * \p lifetime is not supported.
285 * \retval #PSA_ERROR_NOT_PERMITTED
286 * The specified key exists, but the application does not have the
287 * permission to access it. Note that this specification does not
288 * define any way to create such a key, but it may be possible
289 * through implementation-specific means.
290 */
291psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
292 psa_key_id_t id,
293 psa_key_handle_t *handle);
294
295/** Create a new persistent key slot.
296 *
297 * Create a new persistent key slot and return a handle to it. The handle
298 * remains valid until the application calls psa_close_key() or terminates.
299 * The application can open the key again with psa_open_key() until it
300 * removes the key by calling psa_destroy_key().
301 *
302 * \param lifetime The lifetime of the key. This designates a storage
303 * area where the key material is stored. This must not
304 * be #PSA_KEY_LIFETIME_VOLATILE.
305 * \param id The persistent identifier of the key.
306 * \param[out] handle On success, a handle to the newly created key slot.
307 * When key material is later created in this key slot,
308 * it will be saved to the specified persistent location.
309 *
310 * \retval #PSA_SUCCESS
311 * Success. The application can now use the value of `*handle`
312 * to access the newly allocated key slot.
313 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
314 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
315 * \retval #PSA_ERROR_ALREADY_EXISTS
316 * There is already a key with the identifier \p id in the storage
317 * area designated by \p lifetime.
318 * \retval #PSA_ERROR_INVALID_ARGUMENT
319 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
320 * \retval #PSA_ERROR_INVALID_ARGUMENT
321 * \p id is invalid for the specified lifetime.
322 * \retval #PSA_ERROR_NOT_SUPPORTED
323 * \p lifetime is not supported.
324 * \retval #PSA_ERROR_NOT_PERMITTED
325 * \p lifetime is valid, but the application does not have the
326 * permission to create a key there.
327 */
328psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
329 psa_key_id_t id,
330 psa_key_handle_t *handle);
331
332/** Close a key handle.
333 *
334 * If the handle designates a volatile key, destroy the key material and
335 * free all associated resources, just like psa_destroy_key().
336 *
337 * If the handle designates a persistent key, free all resources associated
338 * with the key in volatile memory. The key slot in persistent storage is
339 * not affected and can be opened again later with psa_open_key().
340 *
341 * \param handle The key handle to close.
342 *
343 * \retval #PSA_SUCCESS
344 * \retval #PSA_ERROR_INVALID_HANDLE
345 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
346 */
347psa_status_t psa_close_key(psa_key_handle_t handle);
348
349/**@}*/
350
351/** \defgroup import_export Key import and export
352 * @{
353 */
354
355/**
356 * \brief Import a key in binary format.
357 *
358 * This function supports any output from psa_export_key(). Refer to the
359 * documentation of psa_export_public_key() for the format of public keys
360 * and to the documentation of psa_export_key() for the format for
361 * other key types.
362 *
363 * This specification supports a single format for each key type.
364 * Implementations may support other formats as long as the standard
365 * format is supported. Implementations that support other formats
366 * should ensure that the formats are clearly unambiguous so as to
367 * minimize the risk that an invalid input is accidentally interpreted
368 * according to a different format.
369 *
370 * \param handle Handle to the slot where the key will be stored.
371 * It must have been obtained by calling
372 * psa_allocate_key() or psa_create_key() and must
373 * not contain key material yet.
374 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
375 * import, the key slot will contain a key of this type.
376 * \param[in] data Buffer containing the key data. The content of this
377 * buffer is interpreted according to \p type. It must
378 * contain the format described in the documentation
379 * of psa_export_key() or psa_export_public_key() for
380 * the chosen type.
381 * \param data_length Size of the \p data buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100382 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000383 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +0100384 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100385 * If the key is persistent, the key material and the key's metadata
386 * have been saved to persistent storage.
387 * \retval #PSA_ERROR_INVALID_HANDLE
388 * \retval #PSA_ERROR_NOT_SUPPORTED
389 * The key type or key size is not supported, either by the
390 * implementation in general or in this particular slot.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000391 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis14276e92018-07-10 14:35:43 +0100392 * The key slot is invalid,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100393 * or the key data is not correctly formatted.
394 * \retval #PSA_ERROR_ALREADY_EXISTS
395 * There is already a key in the specified slot.
396 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
397 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
398 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
399 * \retval #PSA_ERROR_STORAGE_FAILURE
400 * \retval #PSA_ERROR_HARDWARE_FAILURE
401 * \retval #PSA_ERROR_TAMPERING_DETECTED
402 * \retval #PSA_ERROR_BAD_STATE
403 * The library has not been previously initialized by psa_crypto_init().
404 * It is implementation-dependent whether a failure to initialize
405 * results in this error code.
406 */
407psa_status_t psa_import_key(psa_key_handle_t handle,
408 psa_key_type_t type,
409 const uint8_t *data,
410 size_t data_length);
411
412/**
413 * \brief Destroy a key.
414 *
415 * This function destroys the content of the key slot from both volatile
416 * memory and, if applicable, non-volatile storage. Implementations shall
417 * make a best effort to ensure that any previous content of the slot is
418 * unrecoverable.
419 *
420 * This function also erases any metadata such as policies and frees all
421 * resources associated with the key.
422 *
423 * \param handle Handle to the key slot to erase.
424 *
425 * \retval #PSA_SUCCESS
426 * The slot's content, if any, has been erased.
427 * \retval #PSA_ERROR_NOT_PERMITTED
428 * The slot holds content and cannot be erased because it is
429 * read-only, either due to a policy or due to physical restrictions.
430 * \retval #PSA_ERROR_INVALID_HANDLE
431 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
432 * There was an failure in communication with the cryptoprocessor.
433 * The key material may still be present in the cryptoprocessor.
434 * \retval #PSA_ERROR_STORAGE_FAILURE
435 * The storage is corrupted. Implementations shall make a best effort
436 * to erase key material even in this stage, however applications
437 * should be aware that it may be impossible to guarantee that the
438 * key material is not recoverable in such cases.
439 * \retval #PSA_ERROR_TAMPERING_DETECTED
440 * An unexpected condition which is not a storage corruption or
441 * a communication failure occurred. The cryptoprocessor may have
442 * been compromised.
443 * \retval #PSA_ERROR_BAD_STATE
444 * The library has not been previously initialized by psa_crypto_init().
445 * It is implementation-dependent whether a failure to initialize
446 * results in this error code.
447 */
448psa_status_t psa_destroy_key(psa_key_handle_t handle);
449
450/**
451 * \brief Get basic metadata about a key.
452 *
453 * \param handle Handle to the key slot to query.
454 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
455 * This may be a null pointer, in which case the key type
456 * is not written.
457 * \param[out] bits On success, the key size in bits.
458 * This may be a null pointer, in which case the key size
459 * is not written.
460 *
461 * \retval #PSA_SUCCESS
462 * \retval #PSA_ERROR_INVALID_HANDLE
463 * \retval #PSA_ERROR_DOES_NOT_EXIST
464 * The handle is to a key slot which does not contain key material yet.
465 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
466 * \retval #PSA_ERROR_HARDWARE_FAILURE
467 * \retval #PSA_ERROR_TAMPERING_DETECTED
468 * \retval #PSA_ERROR_BAD_STATE
469 * The library has not been previously initialized by psa_crypto_init().
470 * It is implementation-dependent whether a failure to initialize
471 * results in this error code.
472 */
473psa_status_t psa_get_key_information(psa_key_handle_t handle,
474 psa_key_type_t *type,
475 size_t *bits);
476
477/**
478 * \brief Export a key in binary format.
479 *
480 * The output of this function can be passed to psa_import_key() to
481 * create an equivalent object.
482 *
483 * If the implementation of psa_import_key() supports other formats
484 * beyond the format specified here, the output from psa_export_key()
485 * must use the representation specified here, not the original
486 * representation.
487 *
488 * For standard key types, the output format is as follows:
489 *
490 * - For symmetric keys (including MAC keys), the format is the
491 * raw bytes of the key.
492 * - For DES, the key data consists of 8 bytes. The parity bits must be
493 * correct.
494 * - For Triple-DES, the format is the concatenation of the
495 * two or three DES keys.
496 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
497 * is the non-encrypted DER encoding of the representation defined by
498 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
499 * ```
500 * RSAPrivateKey ::= SEQUENCE {
501 * version INTEGER, -- must be 0
502 * modulus INTEGER, -- n
503 * publicExponent INTEGER, -- e
504 * privateExponent INTEGER, -- d
505 * prime1 INTEGER, -- p
506 * prime2 INTEGER, -- q
507 * exponent1 INTEGER, -- d mod (p-1)
508 * exponent2 INTEGER, -- d mod (q-1)
509 * coefficient INTEGER, -- (inverse of q) mod p
510 * }
511 * ```
512 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format
513 * is the non-encrypted DER encoding of the representation used by
514 * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows:
515 * ```
516 * DSAPrivateKey ::= SEQUENCE {
517 * version INTEGER, -- must be 0
518 * prime INTEGER, -- p
519 * subprime INTEGER, -- q
520 * generator INTEGER, -- g
521 * public INTEGER, -- y
522 * private INTEGER, -- x
523 * }
524 * ```
525 * - For elliptic curve key pairs (key types for which
526 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
527 * a representation of the private value as a `ceiling(m/8)`-byte string
528 * where `m` is the bit size associated with the curve, i.e. the bit size
529 * of the order of the curve's coordinate field. This byte string is
530 * in little-endian order for Montgomery curves (curve types
531 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
532 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
533 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
534 * This is the content of the `privateKey` field of the `ECPrivateKey`
535 * format defined by RFC 5915.
536 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
537 * true), the format is the same as for psa_export_public_key().
538 *
539 * \param handle Handle to the key to export.
540 * \param[out] data Buffer where the key data is to be written.
541 * \param data_size Size of the \p data buffer in bytes.
542 * \param[out] data_length On success, the number of bytes
543 * that make up the key data.
544 *
545 * \retval #PSA_SUCCESS
546 * \retval #PSA_ERROR_INVALID_HANDLE
547 * \retval #PSA_ERROR_DOES_NOT_EXIST
548 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis377a1552018-11-22 17:02:40 +0000549 * \retval #PSA_ERROR_NOT_SUPPORTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100550 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
551 * The size of the \p data buffer is too small. You can determine a
552 * sufficient buffer size by calling
553 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
554 * where \c type is the key type
555 * and \c bits is the key size in bits.
556 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
557 * \retval #PSA_ERROR_HARDWARE_FAILURE
558 * \retval #PSA_ERROR_TAMPERING_DETECTED
559 * \retval #PSA_ERROR_BAD_STATE
560 * The library has not been previously initialized by psa_crypto_init().
561 * It is implementation-dependent whether a failure to initialize
562 * results in this error code.
563 */
564psa_status_t psa_export_key(psa_key_handle_t handle,
565 uint8_t *data,
566 size_t data_size,
567 size_t *data_length);
568
569/**
570 * \brief Export a public key or the public part of a key pair in binary format.
571 *
572 * The output of this function can be passed to psa_import_key() to
573 * create an object that is equivalent to the public key.
574 *
575 * This specification supports a single format for each key type.
576 * Implementations may support other formats as long as the standard
577 * format is supported. Implementations that support other formats
578 * should ensure that the formats are clearly unambiguous so as to
579 * minimize the risk that an invalid input is accidentally interpreted
580 * according to a different format.
581 *
582 * For standard key types, the output format is as follows:
583 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
584 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
585 * ```
586 * RSAPublicKey ::= SEQUENCE {
587 * modulus INTEGER, -- n
588 * publicExponent INTEGER } -- e
589 * ```
590 * - For elliptic curve public keys (key types for which
591 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
592 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint:
593 * Let `m` be the bit size associated with the curve, i.e. the bit size of
594 * `q` for a curve over `F_q`. The representation consists of:
595 * - The byte 0x04;
596 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
597 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
598 *
599 * For other public key types, the format is the DER representation defined by
600 * RFC 5280 as `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
601 * specified below.
602 * ```
603 * SubjectPublicKeyInfo ::= SEQUENCE {
604 * algorithm AlgorithmIdentifier,
605 * subjectPublicKey BIT STRING }
606 * AlgorithmIdentifier ::= SEQUENCE {
607 * algorithm OBJECT IDENTIFIER,
608 * parameters ANY DEFINED BY algorithm OPTIONAL }
609 * ```
610 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
611 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
612 * `DSAPublicKey`,
613 * with the OID `id-dsa`,
614 * and with the parameters `DSS-Parms`.
615 * ```
616 * id-dsa OBJECT IDENTIFIER ::= {
617 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
618 *
619 * Dss-Parms ::= SEQUENCE {
620 * p INTEGER,
621 * q INTEGER,
622 * g INTEGER }
623 * DSAPublicKey ::= INTEGER -- public key, Y
624 * ```
625 *
626 * \param handle Handle to the key to export.
627 * \param[out] data Buffer where the key data is to be written.
628 * \param data_size Size of the \p data buffer in bytes.
629 * \param[out] data_length On success, the number of bytes
630 * that make up the key data.
631 *
632 * \retval #PSA_SUCCESS
633 * \retval #PSA_ERROR_INVALID_HANDLE
634 * \retval #PSA_ERROR_DOES_NOT_EXIST
635 * \retval #PSA_ERROR_INVALID_ARGUMENT
636 * The key is neither a public key nor a key pair.
637 * \retval #PSA_ERROR_NOT_SUPPORTED
638 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
639 * The size of the \p data buffer is too small. You can determine a
640 * sufficient buffer size by calling
641 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
642 * where \c type is the key type
643 * and \c bits is the key size in bits.
644 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
645 * \retval #PSA_ERROR_HARDWARE_FAILURE
646 * \retval #PSA_ERROR_TAMPERING_DETECTED
647 * \retval #PSA_ERROR_BAD_STATE
648 * The library has not been previously initialized by psa_crypto_init().
649 * It is implementation-dependent whether a failure to initialize
650 * results in this error code.
651 */
652psa_status_t psa_export_public_key(psa_key_handle_t handle,
653 uint8_t *data,
654 size_t data_size,
655 size_t *data_length);
656
657/** Make a copy of a key.
658 *
659 * Copy key material from one location to another.
660 *
661 * This function is primarily useful to copy a key from one location
662 * to another, since it populates a key using the material from
663 * another key which may have a different lifetime.
664 *
665 * In an implementation where slots have different ownerships,
666 * this function may be used to share a key with a different party,
667 * subject to implementation-defined restrictions on key sharing.
668 * In this case \p constraint would typically prevent the recipient
669 * from exporting the key.
670 *
671 * The resulting key may only be used in a way that conforms to all
672 * three of: the policy of the source key, the policy previously set
673 * on the target, and the \p constraint parameter passed when calling
674 * this function.
675 * - The usage flags on the resulting key are the bitwise-and of the
676 * usage flags on the source policy, the previously-set target policy
677 * and the policy constraint.
678 * - If all three policies allow the same algorithm or wildcard-based
679 * algorithm policy, the resulting key has the same algorithm policy.
680 * - If one of the policies allows an algorithm and all the other policies
681 * either allow the same algorithm or a wildcard-based algorithm policy
682 * that includes this algorithm, the resulting key allows the same
683 * algorithm.
684 *
685 * The effect of this function on implementation-defined metadata is
686 * implementation-defined.
687 *
688 * \param source_handle The key to copy. It must be a handle to an
689 * occupied slot.
690 * \param target_handle A handle to the target slot. It must not contain
691 * key material yet.
692 * \param[in] constraint An optional policy constraint. If this parameter
693 * is non-null then the resulting key will conform
694 * to this policy in addition to the source policy
695 * and the policy already present on the target
696 * slot. If this parameter is null then the
697 * function behaves in the same way as if it was
698 * the target policy, i.e. only the source and
699 * target policies apply.
700 *
701 * \retval #PSA_SUCCESS
702 * \retval #PSA_ERROR_INVALID_HANDLE
703 * \retval #PSA_ERROR_ALREADY_EXISTS
704 * \p target already contains key material.
705 * \retval #PSA_ERROR_DOES_NOT_EXIST
706 * \p source does not contain key material.
707 * \retval #PSA_ERROR_INVALID_ARGUMENT
708 * The policy constraints on the source, on the target and
709 * \p constraints are incompatible.
710 * \retval #PSA_ERROR_NOT_PERMITTED
711 * The source key is not exportable and its lifetime does not
712 * allow copying it to the target's lifetime.
713 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
714 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Antonio de Angelis377a1552018-11-22 17:02:40 +0000715 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
716 * \retval #PSA_ERROR_HARDWARE_FAILURE
717 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +0100718 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100719psa_status_t psa_copy_key(psa_key_handle_t source_handle,
720 psa_key_handle_t target_handle,
721 const psa_key_policy_t *constraint);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100722
723/**@}*/
724
725/** \defgroup hash Message digests
726 * @{
727 */
728
Antonio de Angelis377a1552018-11-22 17:02:40 +0000729/** The type of the state data structure for multipart hash operations.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100730 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100731 * Before calling any function on a hash operation object, the application must
732 * initialize it by any of the following means:
733 * - Set the structure to all-bits-zero, for example:
734 * \code
735 * psa_hash_operation_t operation;
736 * memset(&operation, 0, sizeof(operation));
737 * \endcode
738 * - Initialize the structure to logical zero values, for example:
739 * \code
740 * psa_hash_operation_t operation = {0};
741 * \endcode
742 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
743 * for example:
744 * \code
745 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
746 * \endcode
747 * - Assign the result of the function psa_hash_operation_init()
748 * to the structure, for example:
749 * \code
750 * psa_hash_operation_t operation;
751 * operation = psa_hash_operation_init();
752 * \endcode
753 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000754 * This is an implementation-defined \c struct. Applications should not
755 * make any assumptions about the content of this structure except
756 * as directed by the documentation of a specific implementation. */
757typedef struct psa_hash_operation_s psa_hash_operation_t;
Antonio de Angelis14276e92018-07-10 14:35:43 +0100758
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100759/** \def PSA_HASH_OPERATION_INIT
Antonio de Angelis14276e92018-07-10 14:35:43 +0100760 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100761 * This macro returns a suitable initializer for a hash operation object
762 * of type #psa_hash_operation_t.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100763 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100764#ifdef __DOXYGEN_ONLY__
765/* This is an example definition for documentation purposes.
766 * Implementations should define a suitable value in `crypto_struct.h`.
767 */
768#define PSA_HASH_OPERATION_INIT {0}
769#endif
Antonio de Angelis14276e92018-07-10 14:35:43 +0100770
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100771/** Return an initial value for a hash operation object.
772 */
773static psa_hash_operation_t psa_hash_operation_init(void);
774
775/** Set up a multipart hash operation.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100776 *
777 * The sequence of operations to calculate a hash (message digest)
778 * is as follows:
779 * -# Allocate an operation object which will be passed to all the functions
780 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100781 * -# Initialize the operation object with one of the methods described in the
782 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000783 * -# Call psa_hash_setup() to specify the algorithm.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100784 * -# Call psa_hash_update() zero, one or more times, passing a fragment
785 * of the message each time. The hash that is calculated is the hash
786 * of the concatenation of these messages in order.
787 * -# To calculate the hash, call psa_hash_finish().
788 * To compare the hash with an expected value, call psa_hash_verify().
789 *
790 * The application may call psa_hash_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100791 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100792 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000793 * After a successful call to psa_hash_setup(), the application must
Antonio de Angelis14276e92018-07-10 14:35:43 +0100794 * eventually terminate the operation. The following events terminate an
795 * operation:
796 * - A failed call to psa_hash_update().
797 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
798 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100799 * \param[in,out] operation The operation object to set up. It must have
800 * been initialized as per the documentation for
801 * #psa_hash_operation_t and not yet in use.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000802 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
803 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +0100804 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000805 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +0100806 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000807 * \retval #PSA_ERROR_NOT_SUPPORTED
808 * \p alg is not supported or is not a hash algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100809 * \retval #PSA_ERROR_BAD_STATE
810 * The operation state is not valid (already set up and not
811 * subsequently completed).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000812 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
813 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
814 * \retval #PSA_ERROR_HARDWARE_FAILURE
815 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +0100816 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000817psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Antonio de Angelis14276e92018-07-10 14:35:43 +0100818 psa_algorithm_t alg);
819
820/** Add a message fragment to a multipart hash operation.
821 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000822 * The application must call psa_hash_setup() before calling this function.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100823 *
824 * If this function returns an error status, the operation becomes inactive.
825 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000826 * \param[in,out] operation Active hash operation.
827 * \param[in] input Buffer containing the message fragment to hash.
828 * \param input_length Size of the \p input buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100829 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000830 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +0100831 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000832 * \retval #PSA_ERROR_BAD_STATE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100833 * The operation state is not valid (not set up, or already completed).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000834 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
835 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
836 * \retval #PSA_ERROR_HARDWARE_FAILURE
837 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +0100838 */
839psa_status_t psa_hash_update(psa_hash_operation_t *operation,
840 const uint8_t *input,
841 size_t input_length);
842
843/** Finish the calculation of the hash of a message.
844 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000845 * The application must call psa_hash_setup() before calling this function.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100846 * This function calculates the hash of the message formed by concatenating
847 * the inputs passed to preceding calls to psa_hash_update().
848 *
849 * When this function returns, the operation becomes inactive.
850 *
851 * \warning Applications should not call this function if they expect
852 * a specific value for the hash. Call psa_hash_verify() instead.
853 * Beware that comparing integrity or authenticity data such as
854 * hash values with a function such as \c memcmp is risky
855 * because the time taken by the comparison may leak information
856 * about the hashed data which could allow an attacker to guess
857 * a valid hash and thereby bypass security controls.
858 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000859 * \param[in,out] operation Active hash operation.
860 * \param[out] hash Buffer where the hash is to be written.
861 * \param hash_size Size of the \p hash buffer in bytes.
862 * \param[out] hash_length On success, the number of bytes
863 * that make up the hash value. This is always
864 * #PSA_HASH_SIZE(\c alg) where \c alg is the
865 * hash algorithm that is calculated.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100866 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000867 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +0100868 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000869 * \retval #PSA_ERROR_BAD_STATE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100870 * The operation state is not valid (not set up, or already completed).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000871 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
872 * The size of the \p hash buffer is too small. You can determine a
873 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +0100874 * where \c alg is the hash algorithm that is calculated.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000875 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
876 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
877 * \retval #PSA_ERROR_HARDWARE_FAILURE
878 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +0100879 */
880psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
881 uint8_t *hash,
882 size_t hash_size,
883 size_t *hash_length);
884
885/** Finish the calculation of the hash of a message and compare it with
886 * an expected value.
887 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000888 * The application must call psa_hash_setup() before calling this function.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100889 * This function calculates the hash of the message formed by concatenating
890 * the inputs passed to preceding calls to psa_hash_update(). It then
891 * compares the calculated hash with the expected hash passed as a
892 * parameter to this function.
893 *
894 * When this function returns, the operation becomes inactive.
895 *
896 * \note Implementations shall make the best effort to ensure that the
897 * comparison between the actual hash and the expected hash is performed
898 * in constant time.
899 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000900 * \param[in,out] operation Active hash operation.
901 * \param[in] hash Buffer containing the expected hash value.
902 * \param hash_length Size of the \p hash buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100903 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000904 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +0100905 * The expected hash is identical to the actual hash of the message.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000906 * \retval #PSA_ERROR_INVALID_SIGNATURE
Antonio de Angelis14276e92018-07-10 14:35:43 +0100907 * The hash of the message was calculated successfully, but it
908 * differs from the expected hash.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000909 * \retval #PSA_ERROR_BAD_STATE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100910 * The operation state is not valid (not set up, or already completed).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000911 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
912 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
913 * \retval #PSA_ERROR_HARDWARE_FAILURE
914 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +0100915 */
916psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
917 const uint8_t *hash,
918 size_t hash_length);
919
920/** Abort a hash operation.
921 *
Antonio de Angelis14276e92018-07-10 14:35:43 +0100922 * Aborting an operation frees all associated resources except for the
Antonio de Angelis377a1552018-11-22 17:02:40 +0000923 * \p operation structure itself. Once aborted, the operation object
924 * can be reused for another operation by calling
925 * psa_hash_setup() again.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100926 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000927 * You may call this function any time after the operation object has
928 * been initialized by any of the following methods:
929 * - A call to psa_hash_setup(), whether it succeeds or not.
930 * - Initializing the \c struct to all-bits-zero.
931 * - Initializing the \c struct to logical zeros, e.g.
932 * `psa_hash_operation_t operation = {0}`.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100933 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000934 * In particular, calling psa_hash_abort() after the operation has been
935 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
936 * psa_hash_verify() is safe and has no effect.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100937 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000938 * \param[in,out] operation Initialized hash operation.
939 *
940 * \retval #PSA_SUCCESS
941 * \retval #PSA_ERROR_BAD_STATE
942 * \p operation is not an active hash operation.
943 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
944 * \retval #PSA_ERROR_HARDWARE_FAILURE
945 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +0100946 */
947psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
948
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100949/** Clone a hash operation.
950 *
951 * This function copies the state of an ongoing hash operation to
952 * a new operation object. In other words, this function is equivalent
953 * to calling psa_hash_setup() on \p target_operation with the same
954 * algorithm that \p source_operation was set up for, then
955 * psa_hash_update() on \p target_operation with the same input that
956 * that was passed to \p source_operation. After this function returns, the
957 * two objects are independent, i.e. subsequent calls involving one of
958 * the objects do not affect the other object.
959 *
960 * \param[in] source_operation The active hash operation to clone.
961 * \param[in,out] target_operation The operation object to set up.
962 * It must be initialized but not active.
963 *
964 * \retval #PSA_SUCCESS
965 * \retval #PSA_ERROR_BAD_STATE
966 * \p source_operation is not an active hash operation.
967 * \retval #PSA_ERROR_BAD_STATE
968 * \p target_operation is active.
969 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
970 * \retval #PSA_ERROR_HARDWARE_FAILURE
971 * \retval #PSA_ERROR_TAMPERING_DETECTED
972 */
973psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
974 psa_hash_operation_t *target_operation);
975
Antonio de Angelis14276e92018-07-10 14:35:43 +0100976/**@}*/
977
978/** \defgroup MAC Message authentication codes
979 * @{
980 */
981
Antonio de Angelis377a1552018-11-22 17:02:40 +0000982/** The type of the state data structure for multipart MAC operations.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100983 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100984 * Before calling any function on a MAC operation object, the application must
985 * initialize it by any of the following means:
986 * - Set the structure to all-bits-zero, for example:
987 * \code
988 * psa_mac_operation_t operation;
989 * memset(&operation, 0, sizeof(operation));
990 * \endcode
991 * - Initialize the structure to logical zero values, for example:
992 * \code
993 * psa_mac_operation_t operation = {0};
994 * \endcode
995 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
996 * for example:
997 * \code
998 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
999 * \endcode
1000 * - Assign the result of the function psa_mac_operation_init()
1001 * to the structure, for example:
1002 * \code
1003 * psa_mac_operation_t operation;
1004 * operation = psa_mac_operation_init();
1005 * \endcode
1006 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001007 * This is an implementation-defined \c struct. Applications should not
1008 * make any assumptions about the content of this structure except
1009 * as directed by the documentation of a specific implementation. */
1010typedef struct psa_mac_operation_s psa_mac_operation_t;
Antonio de Angelis14276e92018-07-10 14:35:43 +01001011
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001012/** \def PSA_MAC_OPERATION_INIT
1013 *
1014 * This macro returns a suitable initializer for a MAC operation object of type
1015 * #psa_mac_operation_t.
1016 */
1017#ifdef __DOXYGEN_ONLY__
1018/* This is an example definition for documentation purposes.
1019 * Implementations should define a suitable value in `crypto_struct.h`.
1020 */
1021#define PSA_MAC_OPERATION_INIT {0}
1022#endif
1023
1024/** Return an initial value for a MAC operation object.
1025 */
1026static psa_mac_operation_t psa_mac_operation_init(void);
1027
1028/** Set up a multipart MAC calculation operation.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001029 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001030 * This function sets up the calculation of the MAC
1031 * (message authentication code) of a byte string.
1032 * To verify the MAC of a message against an
1033 * expected value, use psa_mac_verify_setup() instead.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001034 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001035 * The sequence of operations to calculate a MAC is as follows:
Antonio de Angelis14276e92018-07-10 14:35:43 +01001036 * -# Allocate an operation object which will be passed to all the functions
1037 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001038 * -# Initialize the operation object with one of the methods described in the
1039 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001040 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001041 * The key remains associated with the operation even if the content
1042 * of the key slot changes.
1043 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1044 * of the message each time. The MAC that is calculated is the MAC
1045 * of the concatenation of these messages in order.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001046 * -# At the end of the message, call psa_mac_sign_finish() to finish
1047 * calculating the MAC value and retrieve it.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001048 *
1049 * The application may call psa_mac_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001050 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001051 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001052 * After a successful call to psa_mac_sign_setup(), the application must
1053 * eventually terminate the operation through one of the following methods:
Antonio de Angelis14276e92018-07-10 14:35:43 +01001054 * - A failed call to psa_mac_update().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001055 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001056 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001057 * \param[in,out] operation The operation object to set up. It must have
1058 * been initialized as per the documentation for
1059 * #psa_mac_operation_t and not yet in use.
1060 * \param handle Handle to the key to use for the operation.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001061 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1062 * such that #PSA_ALG_IS_MAC(alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001063 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001064 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001065 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001066 * \retval #PSA_ERROR_INVALID_HANDLE
1067 * \retval #PSA_ERROR_DOES_NOT_EXIST
Antonio de Angelis377a1552018-11-22 17:02:40 +00001068 * \retval #PSA_ERROR_NOT_PERMITTED
1069 * \retval #PSA_ERROR_INVALID_ARGUMENT
1070 * \p key is not compatible with \p alg.
1071 * \retval #PSA_ERROR_NOT_SUPPORTED
1072 * \p alg is not supported or is not a MAC algorithm.
1073 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1074 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1075 * \retval #PSA_ERROR_HARDWARE_FAILURE
1076 * \retval #PSA_ERROR_TAMPERING_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001077 * \retval #PSA_ERROR_BAD_STATE
1078 * The operation state is not valid (already set up and not
1079 * subsequently completed).
1080 * \retval #PSA_ERROR_BAD_STATE
1081 * The library has not been previously initialized by psa_crypto_init().
1082 * It is implementation-dependent whether a failure to initialize
1083 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001084 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001085psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001086 psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001087 psa_algorithm_t alg);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001088
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001089/** Set up a multipart MAC verification operation.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001090 *
1091 * This function sets up the verification of the MAC
1092 * (message authentication code) of a byte string against an expected value.
1093 *
1094 * The sequence of operations to verify a MAC is as follows:
1095 * -# Allocate an operation object which will be passed to all the functions
1096 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001097 * -# Initialize the operation object with one of the methods described in the
1098 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001099 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1100 * The key remains associated with the operation even if the content
1101 * of the key slot changes.
1102 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1103 * of the message each time. The MAC that is calculated is the MAC
1104 * of the concatenation of these messages in order.
1105 * -# At the end of the message, call psa_mac_verify_finish() to finish
1106 * calculating the actual MAC of the message and verify it against
1107 * the expected value.
1108 *
1109 * The application may call psa_mac_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001110 * has been initialized.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001111 *
1112 * After a successful call to psa_mac_verify_setup(), the application must
1113 * eventually terminate the operation through one of the following methods:
1114 * - A failed call to psa_mac_update().
1115 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1116 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001117 * \param[in,out] operation The operation object to set up. It must have
1118 * been initialized as per the documentation for
1119 * #psa_mac_operation_t and not yet in use.
1120 * \param handle Handle to the key to use for the operation.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001121 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1122 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1123 *
1124 * \retval #PSA_SUCCESS
1125 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001126 * \retval #PSA_ERROR_INVALID_HANDLE
1127 * \retval #PSA_ERROR_DOES_NOT_EXIST
Antonio de Angelis377a1552018-11-22 17:02:40 +00001128 * \retval #PSA_ERROR_NOT_PERMITTED
1129 * \retval #PSA_ERROR_INVALID_ARGUMENT
1130 * \c key is not compatible with \c alg.
1131 * \retval #PSA_ERROR_NOT_SUPPORTED
1132 * \c alg is not supported or is not a MAC algorithm.
1133 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1134 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1135 * \retval #PSA_ERROR_HARDWARE_FAILURE
1136 * \retval #PSA_ERROR_TAMPERING_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001137 * \retval #PSA_ERROR_BAD_STATE
1138 * The operation state is not valid (already set up and not
1139 * subsequently completed).
1140 * \retval #PSA_ERROR_BAD_STATE
1141 * The library has not been previously initialized by psa_crypto_init().
1142 * It is implementation-dependent whether a failure to initialize
1143 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001144 */
1145psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001146 psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001147 psa_algorithm_t alg);
1148
1149/** Add a message fragment to a multipart MAC operation.
1150 *
1151 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1152 * before calling this function.
1153 *
1154 * If this function returns an error status, the operation becomes inactive.
1155 *
1156 * \param[in,out] operation Active MAC operation.
1157 * \param[in] input Buffer containing the message fragment to add to
1158 * the MAC calculation.
1159 * \param input_length Size of the \p input buffer in bytes.
1160 *
1161 * \retval #PSA_SUCCESS
1162 * Success.
1163 * \retval #PSA_ERROR_BAD_STATE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001164 * The operation state is not valid (not set up, or already completed).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001165 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1166 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1167 * \retval #PSA_ERROR_HARDWARE_FAILURE
1168 * \retval #PSA_ERROR_TAMPERING_DETECTED
1169 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001170psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1171 const uint8_t *input,
1172 size_t input_length);
1173
Antonio de Angelis377a1552018-11-22 17:02:40 +00001174/** Finish the calculation of the MAC of a message.
1175 *
1176 * The application must call psa_mac_sign_setup() before calling this function.
1177 * This function calculates the MAC of the message formed by concatenating
1178 * the inputs passed to preceding calls to psa_mac_update().
1179 *
1180 * When this function returns, the operation becomes inactive.
1181 *
1182 * \warning Applications should not call this function if they expect
1183 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1184 * Beware that comparing integrity or authenticity data such as
1185 * MAC values with a function such as \c memcmp is risky
1186 * because the time taken by the comparison may leak information
1187 * about the MAC value which could allow an attacker to guess
1188 * a valid MAC and thereby bypass security controls.
1189 *
1190 * \param[in,out] operation Active MAC operation.
1191 * \param[out] mac Buffer where the MAC value is to be written.
1192 * \param mac_size Size of the \p mac buffer in bytes.
1193 * \param[out] mac_length On success, the number of bytes
1194 * that make up the MAC value. This is always
1195 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
1196 * where \c key_type and \c key_bits are the type and
1197 * bit-size respectively of the key and \c alg is the
1198 * MAC algorithm that is calculated.
1199 *
1200 * \retval #PSA_SUCCESS
1201 * Success.
1202 * \retval #PSA_ERROR_BAD_STATE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001203 * The operation state is not valid (not set up, or already completed).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001204 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1205 * The size of the \p mac buffer is too small. You can determine a
1206 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1207 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1208 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1209 * \retval #PSA_ERROR_HARDWARE_FAILURE
1210 * \retval #PSA_ERROR_TAMPERING_DETECTED
1211 */
1212psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1213 uint8_t *mac,
1214 size_t mac_size,
1215 size_t *mac_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001216
Antonio de Angelis377a1552018-11-22 17:02:40 +00001217/** Finish the calculation of the MAC of a message and compare it with
1218 * an expected value.
1219 *
1220 * The application must call psa_mac_verify_setup() before calling this function.
1221 * This function calculates the MAC of the message formed by concatenating
1222 * the inputs passed to preceding calls to psa_mac_update(). It then
1223 * compares the calculated MAC with the expected MAC passed as a
1224 * parameter to this function.
1225 *
1226 * When this function returns, the operation becomes inactive.
1227 *
1228 * \note Implementations shall make the best effort to ensure that the
1229 * comparison between the actual MAC and the expected MAC is performed
1230 * in constant time.
1231 *
1232 * \param[in,out] operation Active MAC operation.
1233 * \param[in] mac Buffer containing the expected MAC value.
1234 * \param mac_length Size of the \p mac buffer in bytes.
1235 *
1236 * \retval #PSA_SUCCESS
1237 * The expected MAC is identical to the actual MAC of the message.
1238 * \retval #PSA_ERROR_INVALID_SIGNATURE
1239 * The MAC of the message was calculated successfully, but it
1240 * differs from the expected MAC.
1241 * \retval #PSA_ERROR_BAD_STATE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001242 * The operation state is not valid (not set up, or already completed).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001243 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1244 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1245 * \retval #PSA_ERROR_HARDWARE_FAILURE
1246 * \retval #PSA_ERROR_TAMPERING_DETECTED
1247 */
1248psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1249 const uint8_t *mac,
1250 size_t mac_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001251
Antonio de Angelis377a1552018-11-22 17:02:40 +00001252/** Abort a MAC operation.
1253 *
1254 * Aborting an operation frees all associated resources except for the
1255 * \p operation structure itself. Once aborted, the operation object
1256 * can be reused for another operation by calling
1257 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
1258 *
1259 * You may call this function any time after the operation object has
1260 * been initialized by any of the following methods:
1261 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1262 * it succeeds or not.
1263 * - Initializing the \c struct to all-bits-zero.
1264 * - Initializing the \c struct to logical zeros, e.g.
1265 * `psa_mac_operation_t operation = {0}`.
1266 *
1267 * In particular, calling psa_mac_abort() after the operation has been
1268 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1269 * psa_mac_verify_finish() is safe and has no effect.
1270 *
1271 * \param[in,out] operation Initialized MAC operation.
1272 *
1273 * \retval #PSA_SUCCESS
1274 * \retval #PSA_ERROR_BAD_STATE
1275 * \p operation is not an active MAC operation.
1276 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1277 * \retval #PSA_ERROR_HARDWARE_FAILURE
1278 * \retval #PSA_ERROR_TAMPERING_DETECTED
1279 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001280psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1281
1282/**@}*/
1283
1284/** \defgroup cipher Symmetric ciphers
1285 * @{
1286 */
1287
Antonio de Angelis377a1552018-11-22 17:02:40 +00001288/** The type of the state data structure for multipart cipher operations.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001289 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001290 * Before calling any function on a cipher operation object, the application
1291 * must initialize it by any of the following means:
1292 * - Set the structure to all-bits-zero, for example:
1293 * \code
1294 * psa_cipher_operation_t operation;
1295 * memset(&operation, 0, sizeof(operation));
1296 * \endcode
1297 * - Initialize the structure to logical zero values, for example:
1298 * \code
1299 * psa_cipher_operation_t operation = {0};
1300 * \endcode
1301 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1302 * for example:
1303 * \code
1304 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1305 * \endcode
1306 * - Assign the result of the function psa_cipher_operation_init()
1307 * to the structure, for example:
1308 * \code
1309 * psa_cipher_operation_t operation;
1310 * operation = psa_cipher_operation_init();
1311 * \endcode
1312 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001313 * This is an implementation-defined \c struct. Applications should not
1314 * make any assumptions about the content of this structure except
1315 * as directed by the documentation of a specific implementation. */
1316typedef struct psa_cipher_operation_s psa_cipher_operation_t;
Antonio de Angelis14276e92018-07-10 14:35:43 +01001317
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001318/** \def PSA_CIPHER_OPERATION_INIT
1319 *
1320 * This macro returns a suitable initializer for a cipher operation object of
1321 * type #psa_cipher_operation_t.
1322 */
1323#ifdef __DOXYGEN_ONLY__
1324/* This is an example definition for documentation purposes.
1325 * Implementations should define a suitable value in `crypto_struct.h`.
1326 */
1327#define PSA_CIPHER_OPERATION_INIT {0}
1328#endif
1329
1330/** Return an initial value for a cipher operation object.
1331 */
1332static psa_cipher_operation_t psa_cipher_operation_init(void);
1333
Antonio de Angelis14276e92018-07-10 14:35:43 +01001334/** Set the key for a multipart symmetric encryption operation.
1335 *
1336 * The sequence of operations to encrypt a message with a symmetric cipher
1337 * is as follows:
1338 * -# Allocate an operation object which will be passed to all the functions
1339 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001340 * -# Initialize the operation object with one of the methods described in the
1341 * documentation for #psa_cipher_operation_t, e.g.
1342 * PSA_CIPHER_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001343 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001344 * The key remains associated with the operation even if the content
1345 * of the key slot changes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001346 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Antonio de Angelis14276e92018-07-10 14:35:43 +01001347 * generate or set the IV (initialization vector). You should use
Antonio de Angelis377a1552018-11-22 17:02:40 +00001348 * psa_cipher_generate_iv() unless the protocol you are implementing
Antonio de Angelis14276e92018-07-10 14:35:43 +01001349 * requires a specific IV value.
1350 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1351 * of the message each time.
1352 * -# Call psa_cipher_finish().
1353 *
1354 * The application may call psa_cipher_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001355 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001356 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001357 * After a successful call to psa_cipher_encrypt_setup(), the application must
Antonio de Angelis14276e92018-07-10 14:35:43 +01001358 * eventually terminate the operation. The following events terminate an
1359 * operation:
Antonio de Angelis377a1552018-11-22 17:02:40 +00001360 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Antonio de Angelis14276e92018-07-10 14:35:43 +01001361 * or psa_cipher_update().
1362 * - A call to psa_cipher_finish() or psa_cipher_abort().
1363 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001364 * \param[in,out] operation The operation object to set up. It must have
1365 * been initialized as per the documentation for
1366 * #psa_cipher_operation_t and not yet in use.
1367 * \param handle Handle to the key to use for the operation.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001368 * \param alg The cipher algorithm to compute
1369 * (\c PSA_ALG_XXX value such that
1370 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001371 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001372 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001373 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001374 * \retval #PSA_ERROR_INVALID_HANDLE
1375 * \retval #PSA_ERROR_DOES_NOT_EXIST
Antonio de Angelis377a1552018-11-22 17:02:40 +00001376 * \retval #PSA_ERROR_NOT_PERMITTED
1377 * \retval #PSA_ERROR_INVALID_ARGUMENT
1378 * \p key is not compatible with \p alg.
1379 * \retval #PSA_ERROR_NOT_SUPPORTED
1380 * \p alg is not supported or is not a cipher algorithm.
1381 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1382 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1383 * \retval #PSA_ERROR_HARDWARE_FAILURE
1384 * \retval #PSA_ERROR_TAMPERING_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001385 * \retval #PSA_ERROR_BAD_STATE
1386 * The operation state is not valid (already set up and not
1387 * subsequently completed).
1388 * \retval #PSA_ERROR_BAD_STATE
1389 * The library has not been previously initialized by psa_crypto_init().
1390 * It is implementation-dependent whether a failure to initialize
1391 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001392 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001393psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001394 psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001395 psa_algorithm_t alg);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001396
1397/** Set the key for a multipart symmetric decryption operation.
1398 *
1399 * The sequence of operations to decrypt a message with a symmetric cipher
1400 * is as follows:
1401 * -# Allocate an operation object which will be passed to all the functions
1402 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001403 * -# Initialize the operation object with one of the methods described in the
1404 * documentation for #psa_cipher_operation_t, e.g.
1405 * PSA_CIPHER_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001406 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001407 * The key remains associated with the operation even if the content
1408 * of the key slot changes.
1409 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1410 * decryption. If the IV is prepended to the ciphertext, you can call
1411 * psa_cipher_update() on a buffer containing the IV followed by the
1412 * beginning of the message.
1413 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1414 * of the message each time.
1415 * -# Call psa_cipher_finish().
1416 *
1417 * The application may call psa_cipher_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001418 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001419 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001420 * After a successful call to psa_cipher_decrypt_setup(), the application must
Antonio de Angelis14276e92018-07-10 14:35:43 +01001421 * eventually terminate the operation. The following events terminate an
1422 * operation:
1423 * - A failed call to psa_cipher_update().
1424 * - A call to psa_cipher_finish() or psa_cipher_abort().
1425 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001426 * \param[in,out] operation The operation object to set up. It must have
1427 * been initialized as per the documentation for
1428 * #psa_cipher_operation_t and not yet in use.
1429 * \param handle Handle to the key to use for the operation.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001430 * \param alg The cipher algorithm to compute
1431 * (\c PSA_ALG_XXX value such that
1432 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001433 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001434 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001435 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001436 * \retval #PSA_ERROR_INVALID_HANDLE
1437 * \retval #PSA_ERROR_DOES_NOT_EXIST
Antonio de Angelis377a1552018-11-22 17:02:40 +00001438 * \retval #PSA_ERROR_NOT_PERMITTED
1439 * \retval #PSA_ERROR_INVALID_ARGUMENT
1440 * \p key is not compatible with \p alg.
1441 * \retval #PSA_ERROR_NOT_SUPPORTED
1442 * \p alg is not supported or is not a cipher algorithm.
1443 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1444 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1445 * \retval #PSA_ERROR_HARDWARE_FAILURE
1446 * \retval #PSA_ERROR_TAMPERING_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001447 * \retval #PSA_ERROR_BAD_STATE
1448 * The operation state is not valid (already set up and not
1449 * subsequently completed).
1450 * \retval #PSA_ERROR_BAD_STATE
1451 * The library has not been previously initialized by psa_crypto_init().
1452 * It is implementation-dependent whether a failure to initialize
1453 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001454 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001455psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001456 psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001457 psa_algorithm_t alg);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001458
Antonio de Angelis377a1552018-11-22 17:02:40 +00001459/** Generate an IV for a symmetric encryption operation.
1460 *
1461 * This function generates a random IV (initialization vector), nonce
1462 * or initial counter value for the encryption operation as appropriate
1463 * for the chosen algorithm, key type and key size.
1464 *
1465 * The application must call psa_cipher_encrypt_setup() before
1466 * calling this function.
1467 *
1468 * If this function returns an error status, the operation becomes inactive.
1469 *
1470 * \param[in,out] operation Active cipher operation.
1471 * \param[out] iv Buffer where the generated IV is to be written.
1472 * \param iv_size Size of the \p iv buffer in bytes.
1473 * \param[out] iv_length On success, the number of bytes of the
1474 * generated IV.
1475 *
1476 * \retval #PSA_SUCCESS
1477 * Success.
1478 * \retval #PSA_ERROR_BAD_STATE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001479 * The operation state is not valid (not set up, or IV already set).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001480 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1481 * The size of the \p iv buffer is too small.
1482 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1483 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1484 * \retval #PSA_ERROR_HARDWARE_FAILURE
1485 * \retval #PSA_ERROR_TAMPERING_DETECTED
1486 */
1487psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1488 unsigned char *iv,
1489 size_t iv_size,
1490 size_t *iv_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001491
Antonio de Angelis377a1552018-11-22 17:02:40 +00001492/** Set the IV for a symmetric encryption or decryption operation.
1493 *
1494 * This function sets the random IV (initialization vector), nonce
1495 * or initial counter value for the encryption or decryption operation.
1496 *
1497 * The application must call psa_cipher_encrypt_setup() before
1498 * calling this function.
1499 *
1500 * If this function returns an error status, the operation becomes inactive.
1501 *
1502 * \note When encrypting, applications should use psa_cipher_generate_iv()
1503 * instead of this function, unless implementing a protocol that requires
1504 * a non-random IV.
1505 *
1506 * \param[in,out] operation Active cipher operation.
1507 * \param[in] iv Buffer containing the IV to use.
1508 * \param iv_length Size of the IV in bytes.
1509 *
1510 * \retval #PSA_SUCCESS
1511 * Success.
1512 * \retval #PSA_ERROR_BAD_STATE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001513 * The operation state is not valid (not set up, or IV already set).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001514 * \retval #PSA_ERROR_INVALID_ARGUMENT
1515 * The size of \p iv is not acceptable for the chosen algorithm,
1516 * or the chosen algorithm does not use an IV.
1517 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1518 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1519 * \retval #PSA_ERROR_HARDWARE_FAILURE
1520 * \retval #PSA_ERROR_TAMPERING_DETECTED
1521 */
1522psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1523 const unsigned char *iv,
1524 size_t iv_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001525
Antonio de Angelis377a1552018-11-22 17:02:40 +00001526/** Encrypt or decrypt a message fragment in an active cipher operation.
1527 *
1528 * Before calling this function, you must:
1529 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1530 * The choice of setup function determines whether this function
1531 * encrypts or decrypts its input.
1532 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1533 * (recommended when encrypting) or psa_cipher_set_iv().
1534 *
1535 * If this function returns an error status, the operation becomes inactive.
1536 *
1537 * \param[in,out] operation Active cipher operation.
1538 * \param[in] input Buffer containing the message fragment to
1539 * encrypt or decrypt.
1540 * \param input_length Size of the \p input buffer in bytes.
1541 * \param[out] output Buffer where the output is to be written.
1542 * \param output_size Size of the \p output buffer in bytes.
1543 * \param[out] output_length On success, the number of bytes
1544 * that make up the returned output.
1545 *
1546 * \retval #PSA_SUCCESS
1547 * Success.
1548 * \retval #PSA_ERROR_BAD_STATE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001549 * The operation state is not valid (not set up, IV required but
Antonio de Angelis377a1552018-11-22 17:02:40 +00001550 * not set, or already completed).
1551 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1552 * The size of the \p output buffer is too small.
1553 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1554 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1555 * \retval #PSA_ERROR_HARDWARE_FAILURE
1556 * \retval #PSA_ERROR_TAMPERING_DETECTED
1557 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001558psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1559 const uint8_t *input,
1560 size_t input_length,
1561 unsigned char *output,
1562 size_t output_size,
1563 size_t *output_length);
1564
Antonio de Angelis377a1552018-11-22 17:02:40 +00001565/** Finish encrypting or decrypting a message in a cipher operation.
1566 *
1567 * The application must call psa_cipher_encrypt_setup() or
1568 * psa_cipher_decrypt_setup() before calling this function. The choice
1569 * of setup function determines whether this function encrypts or
1570 * decrypts its input.
1571 *
1572 * This function finishes the encryption or decryption of the message
1573 * formed by concatenating the inputs passed to preceding calls to
1574 * psa_cipher_update().
1575 *
1576 * When this function returns, the operation becomes inactive.
1577 *
1578 * \param[in,out] operation Active cipher operation.
1579 * \param[out] output Buffer where the output is to be written.
1580 * \param output_size Size of the \p output buffer in bytes.
1581 * \param[out] output_length On success, the number of bytes
1582 * that make up the returned output.
1583 *
1584 * \retval #PSA_SUCCESS
1585 * Success.
1586 * \retval #PSA_ERROR_BAD_STATE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001587 * The operation state is not valid (not set up, IV required but
Antonio de Angelis377a1552018-11-22 17:02:40 +00001588 * not set, or already completed).
1589 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1590 * The size of the \p output buffer is too small.
1591 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1592 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1593 * \retval #PSA_ERROR_HARDWARE_FAILURE
1594 * \retval #PSA_ERROR_TAMPERING_DETECTED
1595 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001596psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
1597 uint8_t *output,
1598 size_t output_size,
1599 size_t *output_length);
1600
Antonio de Angelis377a1552018-11-22 17:02:40 +00001601/** Abort a cipher operation.
1602 *
1603 * Aborting an operation frees all associated resources except for the
1604 * \p operation structure itself. Once aborted, the operation object
1605 * can be reused for another operation by calling
1606 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
1607 *
1608 * You may call this function any time after the operation object has
1609 * been initialized by any of the following methods:
1610 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1611 * whether it succeeds or not.
1612 * - Initializing the \c struct to all-bits-zero.
1613 * - Initializing the \c struct to logical zeros, e.g.
1614 * `psa_cipher_operation_t operation = {0}`.
1615 *
1616 * In particular, calling psa_cipher_abort() after the operation has been
1617 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1618 * is safe and has no effect.
1619 *
1620 * \param[in,out] operation Initialized cipher operation.
1621 *
1622 * \retval #PSA_SUCCESS
1623 * \retval #PSA_ERROR_BAD_STATE
1624 * \p operation is not an active cipher operation.
1625 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1626 * \retval #PSA_ERROR_HARDWARE_FAILURE
1627 * \retval #PSA_ERROR_TAMPERING_DETECTED
1628 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001629psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1630
1631/**@}*/
1632
1633/** \defgroup aead Authenticated encryption with associated data (AEAD)
1634 * @{
1635 */
1636
Antonio de Angelis14276e92018-07-10 14:35:43 +01001637/** Process an authenticated encryption operation.
1638 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001639 * \param handle Handle to the key to use for the operation.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001640 * \param alg The AEAD algorithm to compute
1641 * (\c PSA_ALG_XXX value such that
Antonio de Angelis377a1552018-11-22 17:02:40 +00001642 * #PSA_ALG_IS_AEAD(\p alg) is true).
1643 * \param[in] nonce Nonce or IV to use.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001644 * \param nonce_length Size of the \p nonce buffer in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001645 * \param[in] additional_data Additional data that will be authenticated
Antonio de Angelis14276e92018-07-10 14:35:43 +01001646 * but not encrypted.
1647 * \param additional_data_length Size of \p additional_data in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001648 * \param[in] plaintext Data that will be authenticated and
Antonio de Angelis14276e92018-07-10 14:35:43 +01001649 * encrypted.
1650 * \param plaintext_length Size of \p plaintext in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001651 * \param[out] ciphertext Output buffer for the authenticated and
Antonio de Angelis14276e92018-07-10 14:35:43 +01001652 * encrypted data. The additional data is not
1653 * part of this output. For algorithms where the
1654 * encrypted data and the authentication tag
1655 * are defined as separate outputs, the
1656 * authentication tag is appended to the
1657 * encrypted data.
1658 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1659 * This must be at least
1660 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1661 * \p plaintext_length).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001662 * \param[out] ciphertext_length On success, the size of the output
Antonio de Angelis14276e92018-07-10 14:35:43 +01001663 * in the \b ciphertext buffer.
1664 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001665 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001666 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001667 * \retval #PSA_ERROR_INVALID_HANDLE
1668 * \retval #PSA_ERROR_DOES_NOT_EXIST
Antonio de Angelis377a1552018-11-22 17:02:40 +00001669 * \retval #PSA_ERROR_NOT_PERMITTED
1670 * \retval #PSA_ERROR_INVALID_ARGUMENT
1671 * \p key is not compatible with \p alg.
1672 * \retval #PSA_ERROR_NOT_SUPPORTED
1673 * \p alg is not supported or is not an AEAD algorithm.
1674 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1675 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1676 * \retval #PSA_ERROR_HARDWARE_FAILURE
1677 * \retval #PSA_ERROR_TAMPERING_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001678 * \retval #PSA_ERROR_BAD_STATE
1679 * The library has not been previously initialized by psa_crypto_init().
1680 * It is implementation-dependent whether a failure to initialize
1681 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001682 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001683psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001684 psa_algorithm_t alg,
1685 const uint8_t *nonce,
1686 size_t nonce_length,
1687 const uint8_t *additional_data,
1688 size_t additional_data_length,
1689 const uint8_t *plaintext,
1690 size_t plaintext_length,
1691 uint8_t *ciphertext,
1692 size_t ciphertext_size,
1693 size_t *ciphertext_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001694
1695/** Process an authenticated decryption operation.
1696 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001697 * \param handle Handle to the key to use for the operation.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001698 * \param alg The AEAD algorithm to compute
1699 * (\c PSA_ALG_XXX value such that
Antonio de Angelis377a1552018-11-22 17:02:40 +00001700 * #PSA_ALG_IS_AEAD(\p alg) is true).
1701 * \param[in] nonce Nonce or IV to use.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001702 * \param nonce_length Size of the \p nonce buffer in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001703 * \param[in] additional_data Additional data that has been authenticated
Antonio de Angelis14276e92018-07-10 14:35:43 +01001704 * but not encrypted.
1705 * \param additional_data_length Size of \p additional_data in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001706 * \param[in] ciphertext Data that has been authenticated and
Antonio de Angelis14276e92018-07-10 14:35:43 +01001707 * encrypted. For algorithms where the
1708 * encrypted data and the authentication tag
1709 * are defined as separate inputs, the buffer
1710 * must contain the encrypted data followed
1711 * by the authentication tag.
1712 * \param ciphertext_length Size of \p ciphertext in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001713 * \param[out] plaintext Output buffer for the decrypted data.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001714 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1715 * This must be at least
1716 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1717 * \p ciphertext_length).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001718 * \param[out] plaintext_length On success, the size of the output
Antonio de Angelis14276e92018-07-10 14:35:43 +01001719 * in the \b plaintext buffer.
1720 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001721 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001722 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001723 * \retval #PSA_ERROR_INVALID_HANDLE
1724 * \retval #PSA_ERROR_DOES_NOT_EXIST
Antonio de Angelis377a1552018-11-22 17:02:40 +00001725 * \retval #PSA_ERROR_INVALID_SIGNATURE
Antonio de Angelis14276e92018-07-10 14:35:43 +01001726 * The ciphertext is not authentic.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001727 * \retval #PSA_ERROR_NOT_PERMITTED
1728 * \retval #PSA_ERROR_INVALID_ARGUMENT
1729 * \p key is not compatible with \p alg.
1730 * \retval #PSA_ERROR_NOT_SUPPORTED
1731 * \p alg is not supported or is not an AEAD algorithm.
1732 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1733 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1734 * \retval #PSA_ERROR_HARDWARE_FAILURE
1735 * \retval #PSA_ERROR_TAMPERING_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001736 * \retval #PSA_ERROR_BAD_STATE
1737 * The library has not been previously initialized by psa_crypto_init().
1738 * It is implementation-dependent whether a failure to initialize
1739 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001740 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001741psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001742 psa_algorithm_t alg,
1743 const uint8_t *nonce,
1744 size_t nonce_length,
1745 const uint8_t *additional_data,
1746 size_t additional_data_length,
1747 const uint8_t *ciphertext,
1748 size_t ciphertext_length,
1749 uint8_t *plaintext,
1750 size_t plaintext_size,
1751 size_t *plaintext_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001752
1753/**@}*/
1754
1755/** \defgroup asymmetric Asymmetric cryptography
1756 * @{
1757 */
1758
1759/**
Antonio de Angelis14276e92018-07-10 14:35:43 +01001760 * \brief Sign a hash or short message with a private key.
1761 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001762 * Note that to perform a hash-and-sign signature algorithm, you must
1763 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
1764 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1765 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1766 * to determine the hash algorithm to use.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001767 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001768 * \param handle Handle to the key to use for the operation.
1769 * It must be an asymmetric key pair.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001770 * \param alg A signature algorithm that is compatible with
1771 * the type of \p key.
1772 * \param[in] hash The hash or message to sign.
1773 * \param hash_length Size of the \p hash buffer in bytes.
1774 * \param[out] signature Buffer where the signature is to be written.
1775 * \param signature_size Size of the \p signature buffer in bytes.
1776 * \param[out] signature_length On success, the number of bytes
1777 * that make up the returned signature value.
1778 *
1779 * \retval #PSA_SUCCESS
1780 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1781 * The size of the \p signature buffer is too small. You can
Antonio de Angelis14276e92018-07-10 14:35:43 +01001782 * determine a sufficient buffer size by calling
Antonio de Angelis377a1552018-11-22 17:02:40 +00001783 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01001784 * where \c key_type and \c key_bits are the type and bit-size
Antonio de Angelis377a1552018-11-22 17:02:40 +00001785 * respectively of \p key.
1786 * \retval #PSA_ERROR_NOT_SUPPORTED
1787 * \retval #PSA_ERROR_INVALID_ARGUMENT
1788 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1789 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1790 * \retval #PSA_ERROR_HARDWARE_FAILURE
1791 * \retval #PSA_ERROR_TAMPERING_DETECTED
1792 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001793 * \retval #PSA_ERROR_BAD_STATE
1794 * The library has not been previously initialized by psa_crypto_init().
1795 * It is implementation-dependent whether a failure to initialize
1796 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001797 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001798psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Antonio de Angelis14276e92018-07-10 14:35:43 +01001799 psa_algorithm_t alg,
1800 const uint8_t *hash,
1801 size_t hash_length,
Antonio de Angelis14276e92018-07-10 14:35:43 +01001802 uint8_t *signature,
1803 size_t signature_size,
1804 size_t *signature_length);
1805
1806/**
1807 * \brief Verify the signature a hash or short message using a public key.
1808 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001809 * Note that to perform a hash-and-sign signature algorithm, you must
1810 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
1811 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1812 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1813 * to determine the hash algorithm to use.
1814 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001815 * \param handle Handle to the key to use for the operation.
1816 * It must be a public key or an asymmetric key pair.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001817 * \param alg A signature algorithm that is compatible with
Antonio de Angelis377a1552018-11-22 17:02:40 +00001818 * the type of \p key.
1819 * \param[in] hash The hash or message whose signature is to be
1820 * verified.
1821 * \param hash_length Size of the \p hash buffer in bytes.
1822 * \param[in] signature Buffer containing the signature to verify.
1823 * \param signature_length Size of the \p signature buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001824 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001825 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001826 * The signature is valid.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001827 * \retval #PSA_ERROR_INVALID_SIGNATURE
Antonio de Angelis14276e92018-07-10 14:35:43 +01001828 * The calculation was perfomed successfully, but the passed
1829 * signature is not a valid signature.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001830 * \retval #PSA_ERROR_NOT_SUPPORTED
1831 * \retval #PSA_ERROR_INVALID_ARGUMENT
1832 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1833 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1834 * \retval #PSA_ERROR_HARDWARE_FAILURE
1835 * \retval #PSA_ERROR_TAMPERING_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001836 * \retval #PSA_ERROR_BAD_STATE
1837 * The library has not been previously initialized by psa_crypto_init().
1838 * It is implementation-dependent whether a failure to initialize
1839 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001840 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001841psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Antonio de Angelis14276e92018-07-10 14:35:43 +01001842 psa_algorithm_t alg,
1843 const uint8_t *hash,
1844 size_t hash_length,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001845 const uint8_t *signature,
1846 size_t signature_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001847
Antonio de Angelis14276e92018-07-10 14:35:43 +01001848/**
1849 * \brief Encrypt a short message with a public key.
1850 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001851 * \param handle Handle to the key to use for the operation.
1852 * It must be a public key or an asymmetric
1853 * key pair.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001854 * \param alg An asymmetric encryption algorithm that is
1855 * compatible with the type of \p key.
1856 * \param[in] input The message to encrypt.
1857 * \param input_length Size of the \p input buffer in bytes.
1858 * \param[in] salt A salt or label, if supported by the
1859 * encryption algorithm.
1860 * If the algorithm does not support a
1861 * salt, pass \c NULL.
1862 * If the algorithm supports an optional
1863 * salt and you do not want to pass a salt,
1864 * pass \c NULL.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001865 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001866 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1867 * supported.
1868 * \param salt_length Size of the \p salt buffer in bytes.
1869 * If \p salt is \c NULL, pass 0.
1870 * \param[out] output Buffer where the encrypted message is to
1871 * be written.
1872 * \param output_size Size of the \p output buffer in bytes.
1873 * \param[out] output_length On success, the number of bytes
1874 * that make up the returned output.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001875 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001876 * \retval #PSA_SUCCESS
1877 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1878 * The size of the \p output buffer is too small. You can
Antonio de Angelis14276e92018-07-10 14:35:43 +01001879 * determine a sufficient buffer size by calling
Antonio de Angelis377a1552018-11-22 17:02:40 +00001880 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01001881 * where \c key_type and \c key_bits are the type and bit-size
Antonio de Angelis377a1552018-11-22 17:02:40 +00001882 * respectively of \p key.
1883 * \retval #PSA_ERROR_NOT_SUPPORTED
1884 * \retval #PSA_ERROR_INVALID_ARGUMENT
1885 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1886 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1887 * \retval #PSA_ERROR_HARDWARE_FAILURE
1888 * \retval #PSA_ERROR_TAMPERING_DETECTED
1889 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001890 * \retval #PSA_ERROR_BAD_STATE
1891 * The library has not been previously initialized by psa_crypto_init().
1892 * It is implementation-dependent whether a failure to initialize
1893 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001894 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001895psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Antonio de Angelis14276e92018-07-10 14:35:43 +01001896 psa_algorithm_t alg,
1897 const uint8_t *input,
1898 size_t input_length,
1899 const uint8_t *salt,
1900 size_t salt_length,
1901 uint8_t *output,
1902 size_t output_size,
1903 size_t *output_length);
1904
1905/**
1906 * \brief Decrypt a short message with a private key.
1907 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001908 * \param handle Handle to the key to use for the operation.
1909 * It must be an asymmetric key pair.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001910 * \param alg An asymmetric encryption algorithm that is
1911 * compatible with the type of \p key.
1912 * \param[in] input The message to decrypt.
1913 * \param input_length Size of the \p input buffer in bytes.
1914 * \param[in] salt A salt or label, if supported by the
1915 * encryption algorithm.
1916 * If the algorithm does not support a
1917 * salt, pass \c NULL.
1918 * If the algorithm supports an optional
1919 * salt and you do not want to pass a salt,
1920 * pass \c NULL.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001921 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001922 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1923 * supported.
1924 * \param salt_length Size of the \p salt buffer in bytes.
1925 * If \p salt is \c NULL, pass 0.
1926 * \param[out] output Buffer where the decrypted message is to
1927 * be written.
1928 * \param output_size Size of the \c output buffer in bytes.
1929 * \param[out] output_length On success, the number of bytes
1930 * that make up the returned output.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001931 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001932 * \retval #PSA_SUCCESS
1933 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1934 * The size of the \p output buffer is too small. You can
Antonio de Angelis14276e92018-07-10 14:35:43 +01001935 * determine a sufficient buffer size by calling
Antonio de Angelis377a1552018-11-22 17:02:40 +00001936 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01001937 * where \c key_type and \c key_bits are the type and bit-size
Antonio de Angelis377a1552018-11-22 17:02:40 +00001938 * respectively of \p key.
1939 * \retval #PSA_ERROR_NOT_SUPPORTED
1940 * \retval #PSA_ERROR_INVALID_ARGUMENT
1941 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1942 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1943 * \retval #PSA_ERROR_HARDWARE_FAILURE
1944 * \retval #PSA_ERROR_TAMPERING_DETECTED
1945 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
1946 * \retval #PSA_ERROR_INVALID_PADDING
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001947 * \retval #PSA_ERROR_BAD_STATE
1948 * The library has not been previously initialized by psa_crypto_init().
1949 * It is implementation-dependent whether a failure to initialize
1950 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001951 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001952psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Antonio de Angelis14276e92018-07-10 14:35:43 +01001953 psa_algorithm_t alg,
1954 const uint8_t *input,
1955 size_t input_length,
1956 const uint8_t *salt,
1957 size_t salt_length,
1958 uint8_t *output,
1959 size_t output_size,
1960 size_t *output_length);
1961
1962/**@}*/
1963
Antonio de Angelis377a1552018-11-22 17:02:40 +00001964/** \defgroup generators Generators
1965 * @{
1966 */
1967
1968/** The type of the state data structure for generators.
1969 *
1970 * Before calling any function on a generator, the application must
1971 * initialize it by any of the following means:
1972 * - Set the structure to all-bits-zero, for example:
1973 * \code
1974 * psa_crypto_generator_t generator;
1975 * memset(&generator, 0, sizeof(generator));
1976 * \endcode
1977 * - Initialize the structure to logical zero values, for example:
1978 * \code
1979 * psa_crypto_generator_t generator = {0};
1980 * \endcode
1981 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
1982 * for example:
1983 * \code
1984 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1985 * \endcode
1986 * - Assign the result of the function psa_crypto_generator_init()
1987 * to the structure, for example:
1988 * \code
1989 * psa_crypto_generator_t generator;
1990 * generator = psa_crypto_generator_init();
1991 * \endcode
1992 *
1993 * This is an implementation-defined \c struct. Applications should not
1994 * make any assumptions about the content of this structure except
1995 * as directed by the documentation of a specific implementation.
1996 */
1997typedef struct psa_crypto_generator_s psa_crypto_generator_t;
1998
1999/** \def PSA_CRYPTO_GENERATOR_INIT
2000 *
2001 * This macro returns a suitable initializer for a generator object
2002 * of type #psa_crypto_generator_t.
2003 */
2004#ifdef __DOXYGEN_ONLY__
2005/* This is an example definition for documentation purposes.
2006 * Implementations should define a suitable value in `crypto_struct.h`.
2007 */
2008#define PSA_CRYPTO_GENERATOR_INIT {0}
2009#endif
2010
2011/** Return an initial value for a generator object.
2012 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002013static psa_crypto_generator_t psa_crypto_generator_init(void);
Antonio de Angelis377a1552018-11-22 17:02:40 +00002014
2015/** Retrieve the current capacity of a generator.
2016 *
2017 * The capacity of a generator is the maximum number of bytes that it can
2018 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2019 *
2020 * \param[in] generator The generator to query.
2021 * \param[out] capacity On success, the capacity of the generator.
2022 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002023 * \retval #PSA_SUCCESS
2024 * \retval #PSA_ERROR_BAD_STATE
2025 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Antonio de Angelis377a1552018-11-22 17:02:40 +00002026 */
2027psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2028 size_t *capacity);
2029
2030/** Read some data from a generator.
2031 *
2032 * This function reads and returns a sequence of bytes from a generator.
2033 * The data that is read is discarded from the generator. The generator's
2034 * capacity is decreased by the number of bytes read.
2035 *
2036 * \param[in,out] generator The generator object to read from.
2037 * \param[out] output Buffer where the generator output will be
2038 * written.
2039 * \param output_length Number of bytes to output.
2040 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002041 * \retval #PSA_SUCCESS
2042 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Antonio de Angelis377a1552018-11-22 17:02:40 +00002043 * There were fewer than \p output_length bytes
2044 * in the generator. Note that in this case, no
2045 * output is written to the output buffer.
2046 * The generator's capacity is set to 0, thus
2047 * subsequent calls to this function will not
2048 * succeed, even with a smaller output buffer.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002049 * \retval #PSA_ERROR_BAD_STATE
2050 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2051 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2052 * \retval #PSA_ERROR_HARDWARE_FAILURE
2053 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis377a1552018-11-22 17:02:40 +00002054 */
2055psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2056 uint8_t *output,
2057 size_t output_length);
2058
2059/** Create a symmetric key from data read from a generator.
2060 *
2061 * This function reads a sequence of bytes from a generator and imports
2062 * these bytes as a key.
2063 * The data that is read is discarded from the generator. The generator's
2064 * capacity is decreased by the number of bytes read.
2065 *
2066 * This function is equivalent to calling #psa_generator_read and
2067 * passing the resulting output to #psa_import_key, but
2068 * if the implementation provides an isolation boundary then
2069 * the key material is not exposed outside the isolation boundary.
2070 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002071 * \param handle Handle to the slot where the key will be stored.
2072 * It must have been obtained by calling
2073 * psa_allocate_key() or psa_create_key() and must
2074 * not contain key material yet.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002075 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2076 * This must be a symmetric key type.
2077 * \param bits Key size in bits.
2078 * \param[in,out] generator The generator object to read from.
2079 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002080 * \retval #PSA_SUCCESS
Antonio de Angelis377a1552018-11-22 17:02:40 +00002081 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002082 * If the key is persistent, the key material and the key's metadata
2083 * have been saved to persistent storage.
2084 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Antonio de Angelis377a1552018-11-22 17:02:40 +00002085 * There were fewer than \p output_length bytes
2086 * in the generator. Note that in this case, no
2087 * output is written to the output buffer.
2088 * The generator's capacity is set to 0, thus
2089 * subsequent calls to this function will not
2090 * succeed, even with a smaller output buffer.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002091 * \retval #PSA_ERROR_NOT_SUPPORTED
Antonio de Angelis377a1552018-11-22 17:02:40 +00002092 * The key type or key size is not supported, either by the
2093 * implementation in general or in this particular slot.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002094 * \retval #PSA_ERROR_BAD_STATE
2095 * \retval #PSA_ERROR_INVALID_HANDLE
2096 * \retval #PSA_ERROR_ALREADY_EXISTS
Antonio de Angelis377a1552018-11-22 17:02:40 +00002097 * There is already a key in the specified slot.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002098 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2099 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2100 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2101 * \retval #PSA_ERROR_HARDWARE_FAILURE
2102 * \retval #PSA_ERROR_TAMPERING_DETECTED
2103 * \retval #PSA_ERROR_BAD_STATE
2104 * The library has not been previously initialized by psa_crypto_init().
2105 * It is implementation-dependent whether a failure to initialize
2106 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002107 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002108psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +00002109 psa_key_type_t type,
2110 size_t bits,
2111 psa_crypto_generator_t *generator);
2112
2113/** Abort a generator.
2114 *
2115 * Once a generator has been aborted, its capacity is zero.
2116 * Aborting a generator frees all associated resources except for the
2117 * \c generator structure itself.
2118 *
2119 * This function may be called at any time as long as the generator
2120 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2121 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2122 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2123 * on a generator that has not been set up.
2124 *
2125 * Once aborted, the generator object may be called.
2126 *
2127 * \param[in,out] generator The generator to abort.
2128 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002129 * \retval #PSA_SUCCESS
2130 * \retval #PSA_ERROR_BAD_STATE
2131 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2132 * \retval #PSA_ERROR_HARDWARE_FAILURE
2133 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis377a1552018-11-22 17:02:40 +00002134 */
2135psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2136
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002137/** Use the maximum possible capacity for a generator.
2138 *
2139 * Use this value as the capacity argument when setting up a generator
2140 * to indicate that the generator should have the maximum possible capacity.
2141 * The value of the maximum possible capacity depends on the generator
2142 * algorithm.
2143 */
2144#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2145
Antonio de Angelis377a1552018-11-22 17:02:40 +00002146/**@}*/
2147
2148/** \defgroup derivation Key derivation
2149 * @{
2150 */
2151
2152/** Set up a key derivation operation.
2153 *
2154 * A key derivation algorithm takes three inputs: a secret input \p key and
2155 * two non-secret inputs \p label and p salt.
2156 * The result of this function is a byte generator which can
2157 * be used to produce keys and other cryptographic material.
2158 *
2159 * The role of \p label and \p salt is as follows:
2160 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2161 * and \p label is the info string used in the "expand" step.
2162 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002163 * \param[in,out] generator The generator object to set up. It must have
2164 * been initialized as per the documentation for
2165 * #psa_crypto_generator_t and not yet in use.
2166 * \param handle Handle to the secret key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002167 * \param alg The key derivation algorithm to compute
2168 * (\c PSA_ALG_XXX value such that
2169 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2170 * \param[in] salt Salt to use.
2171 * \param salt_length Size of the \p salt buffer in bytes.
2172 * \param[in] label Label to use.
2173 * \param label_length Size of the \p label buffer in bytes.
2174 * \param capacity The maximum number of bytes that the
2175 * generator will be able to provide.
2176 *
2177 * \retval #PSA_SUCCESS
2178 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002179 * \retval #PSA_ERROR_INVALID_HANDLE
2180 * \retval #PSA_ERROR_DOES_NOT_EXIST
Antonio de Angelis377a1552018-11-22 17:02:40 +00002181 * \retval #PSA_ERROR_NOT_PERMITTED
2182 * \retval #PSA_ERROR_INVALID_ARGUMENT
2183 * \c key is not compatible with \c alg,
2184 * or \p capacity is too large for the specified algorithm and key.
2185 * \retval #PSA_ERROR_NOT_SUPPORTED
2186 * \c alg is not supported or is not a key derivation algorithm.
2187 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2188 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2189 * \retval #PSA_ERROR_HARDWARE_FAILURE
2190 * \retval #PSA_ERROR_TAMPERING_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002191 * \retval #PSA_ERROR_BAD_STATE
2192 * The library has not been previously initialized by psa_crypto_init().
2193 * It is implementation-dependent whether a failure to initialize
2194 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002195 */
2196psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002197 psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +00002198 psa_algorithm_t alg,
2199 const uint8_t *salt,
2200 size_t salt_length,
2201 const uint8_t *label,
2202 size_t label_length,
2203 size_t capacity);
2204
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002205/** Set up a key agreement operation.
2206 *
2207 * A key agreement algorithm takes two inputs: a private key \p private_key
2208 * a public key \p peer_key.
2209 * The result of this function is a byte generator which can
2210 * be used to produce keys and other cryptographic material.
2211 *
2212 * The resulting generator always has the maximum capacity permitted by
2213 * the algorithm.
2214 *
2215 * \param[in,out] generator The generator object to set up. It must have been
2216 * initialized as per the documentation for
2217 * #psa_crypto_generator_t and not yet in use.
2218 * \param private_key Handle to the private key to use.
2219 * \param[in] peer_key Public key of the peer. The peer key must be in the
2220 * same format that psa_import_key() accepts for the
2221 * public key type corresponding to the type of
2222 * \p private_key. That is, this function performs the
2223 * equivalent of
2224 * `psa_import_key(internal_public_key_handle,
2225 * PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(private_key_type),
2226 * peer_key, peer_key_length)` where
2227 * `private_key_type` is the type of \p private_key.
2228 * For example, for EC keys, this means that \p
2229 * peer_key is interpreted as a point on the curve
2230 * that the private key is associated with. The
2231 * standard formats for public keys are documented in
2232 * the documentation of psa_export_public_key().
2233 * \param peer_key_length Size of \p peer_key in bytes.
2234 * \param alg The key agreement algorithm to compute
2235 * (\c PSA_ALG_XXX value such that
2236 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
2237 *
2238 * \retval #PSA_SUCCESS
2239 * Success.
2240 * \retval #PSA_ERROR_INVALID_HANDLE
2241 * \retval #PSA_ERROR_DOES_NOT_EXIST
2242 * \retval #PSA_ERROR_NOT_PERMITTED
2243 * \retval #PSA_ERROR_INVALID_ARGUMENT
2244 * \c private_key is not compatible with \c alg,
2245 * or \p peer_key is not valid for \c alg or not compatible with
2246 * \c private_key.
2247 * \retval #PSA_ERROR_NOT_SUPPORTED
2248 * \c alg is not supported or is not a key derivation algorithm.
2249 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2250 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2251 * \retval #PSA_ERROR_HARDWARE_FAILURE
2252 * \retval #PSA_ERROR_TAMPERING_DETECTED
2253 */
2254psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
2255 psa_key_handle_t private_key,
2256 const uint8_t *peer_key,
2257 size_t peer_key_length,
2258 psa_algorithm_t alg);
2259
Antonio de Angelis377a1552018-11-22 17:02:40 +00002260/**@}*/
2261
2262/** \defgroup random Random generation
Antonio de Angelis14276e92018-07-10 14:35:43 +01002263 * @{
2264 */
2265
2266/**
2267 * \brief Generate random bytes.
2268 *
2269 * \warning This function **can** fail! Callers MUST check the return status
2270 * and MUST NOT use the content of the output buffer if the return
2271 * status is not #PSA_SUCCESS.
2272 *
2273 * \note To generate a key, use psa_generate_key() instead.
2274 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002275 * \param[out] output Output buffer for the generated data.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002276 * \param output_size Number of bytes to generate and output.
2277 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002278 * \retval #PSA_SUCCESS
2279 * \retval #PSA_ERROR_NOT_SUPPORTED
2280 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2281 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2282 * \retval #PSA_ERROR_HARDWARE_FAILURE
2283 * \retval #PSA_ERROR_TAMPERING_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002284 * \retval #PSA_ERROR_BAD_STATE
2285 * The library has not been previously initialized by psa_crypto_init().
2286 * It is implementation-dependent whether a failure to initialize
2287 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002288 */
2289psa_status_t psa_generate_random(uint8_t *output,
2290 size_t output_size);
2291
Antonio de Angelis377a1552018-11-22 17:02:40 +00002292/** Extra parameters for RSA key generation.
2293 *
2294 * You may pass a pointer to a structure of this type as the \c extra
2295 * parameter to psa_generate_key().
2296 */
2297typedef struct {
2298 uint32_t e; /**< Public exponent value. Default: 65537. */
2299} psa_generate_key_extra_rsa;
2300
Antonio de Angelis14276e92018-07-10 14:35:43 +01002301/**
2302 * \brief Generate a key or key pair.
2303 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002304 * \param handle Handle to the slot where the key will be stored.
2305 * It must have been obtained by calling
2306 * psa_allocate_key() or psa_create_key() and must
2307 * not contain key material yet.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002308 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2309 * \param bits Key size in bits.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002310 * \param[in] extra Extra parameters for key generation. The
Antonio de Angelis14276e92018-07-10 14:35:43 +01002311 * interpretation of this parameter depends on
Antonio de Angelis377a1552018-11-22 17:02:40 +00002312 * \p type. All types support \c NULL to use
2313 * default parameters. Implementation that support
2314 * the generation of vendor-specific key types
2315 * that allow extra parameters shall document
2316 * the format of these extra parameters and
2317 * the default values. For standard parameters,
2318 * the meaning of \p extra is as follows:
2319 * - For a symmetric key type (a type such
2320 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2321 * false), \p extra must be \c NULL.
2322 * - For an elliptic curve key type (a type
2323 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2324 * false), \p extra must be \c NULL.
2325 * - For an RSA key (\p type is
2326 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2327 * optional #psa_generate_key_extra_rsa structure
2328 * specifying the public exponent. The
2329 * default public exponent used when \p extra
2330 * is \c NULL is 65537.
2331 * \param extra_size Size of the buffer that \p extra
2332 * points to, in bytes. Note that if \p extra is
2333 * \c NULL then \p extra_size must be zero.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002334 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002335 * \retval #PSA_SUCCESS
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002336 * Success.
2337 * If the key is persistent, the key material and the key's metadata
2338 * have been saved to persistent storage.
2339 * \retval #PSA_ERROR_INVALID_HANDLE
2340 * \retval #PSA_ERROR_ALREADY_EXISTS
2341 * There is already a key in the specified slot.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002342 * \retval #PSA_ERROR_NOT_SUPPORTED
2343 * \retval #PSA_ERROR_INVALID_ARGUMENT
2344 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2345 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2346 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2347 * \retval #PSA_ERROR_HARDWARE_FAILURE
2348 * \retval #PSA_ERROR_TAMPERING_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002349 * \retval #PSA_ERROR_BAD_STATE
2350 * The library has not been previously initialized by psa_crypto_init().
2351 * It is implementation-dependent whether a failure to initialize
2352 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002353 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002354psa_status_t psa_generate_key(psa_key_handle_t handle,
Antonio de Angelis14276e92018-07-10 14:35:43 +01002355 psa_key_type_t type,
2356 size_t bits,
Antonio de Angelis377a1552018-11-22 17:02:40 +00002357 const void *extra,
2358 size_t extra_size);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002359
2360/**@}*/
2361
2362#ifdef __cplusplus
2363}
2364#endif
2365
Antonio de Angelis377a1552018-11-22 17:02:40 +00002366/* The file "crypto_sizes.h" contains definitions for size calculation
2367 * macros whose definitions are implementation-specific. */
Jamie Foxcc31d402019-01-28 17:13:52 +00002368#include "psa/crypto_sizes.h"
Antonio de Angelis377a1552018-11-22 17:02:40 +00002369
2370/* The file "crypto_struct.h" contains definitions for
2371 * implementation-specific structs that are declared above. */
Jamie Foxcc31d402019-01-28 17:13:52 +00002372#include "psa/crypto_struct.h"
Antonio de Angelis377a1552018-11-22 17:02:40 +00002373
2374/* The file "crypto_extra.h" contains vendor-specific definitions. This
2375 * can include vendor-defined algorithms, extra functions, etc. */
Jamie Foxcc31d402019-01-28 17:13:52 +00002376#include "psa/crypto_extra.h"
Antonio de Angelis8908f472018-08-31 15:44:25 +01002377
Antonio de Angelis14276e92018-07-10 14:35:43 +01002378#endif /* PSA_CRYPTO_H */