blob: 0099baab7d950fb1fb66d0e285af0219322ffa74 [file] [log] [blame]
Antonio de Angelis14276e92018-07-10 14:35:43 +01001/*
Maulik Patel28659c42021-01-06 14:09:22 +00002 * Copyright (c) 2018-2021, 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
15#include <stddef.h>
16
17#ifdef __DOXYGEN_ONLY__
18/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
Jamie Fox0e54ebc2019-04-09 14:21:04 +010019 * must be defined in the crypto_platform.h header. These mock definitions
Antonio de Angelis14276e92018-07-10 14:35:43 +010020 * are present in this file as a convenience to generate pretty-printed
Antonio de Angelis377a1552018-11-22 17:02:40 +000021 * documentation that includes those definitions. */
Antonio de Angelis14276e92018-07-10 14:35:43 +010022
23/** \defgroup platform Implementation-specific definitions
24 * @{
25 */
26
Antonio de Angelis14276e92018-07-10 14:35:43 +010027/**@}*/
28#endif /* __DOXYGEN_ONLY__ */
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
Jamie Fox0e54ebc2019-04-09 14:21:04 +010034/* The file "crypto_types.h" declares types that encode errors,
35 * algorithms, key types, policies, etc. */
Jamie Foxcc31d402019-01-28 17:13:52 +000036#include "psa/crypto_types.h"
Jamie Fox0e54ebc2019-04-09 14:21:04 +010037
Antonio de Angelis04debbd2019-10-14 12:12:52 +010038/** \defgroup version API version
39 * @{
40 */
41
42/**
43 * The major version of this implementation of the PSA Crypto API
44 */
45#define PSA_CRYPTO_API_VERSION_MAJOR 1
46
47/**
48 * The minor version of this implementation of the PSA Crypto API
49 */
50#define PSA_CRYPTO_API_VERSION_MINOR 0
51
52/**@}*/
53
Jamie Fox0e54ebc2019-04-09 14:21:04 +010054/* The file "crypto_values.h" declares macros to build and analyze values
55 * of integral types defined in "crypto_types.h". */
Jamie Foxcc31d402019-01-28 17:13:52 +000056#include "psa/crypto_values.h"
Jamie Fox0e54ebc2019-04-09 14:21:04 +010057
58/** \defgroup initialization Library initialization
Antonio de Angelis14276e92018-07-10 14:35:43 +010059 * @{
60 */
61
Antonio de Angelis14276e92018-07-10 14:35:43 +010062/**
63 * \brief Library initialization.
64 *
65 * Applications must call this function before calling any other
66 * function in this module.
67 *
68 * Applications may call this function more than once. Once a call
69 * succeeds, subsequent calls are guaranteed to succeed.
70 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +010071 * If the application calls other functions before calling psa_crypto_init(),
72 * the behavior is undefined. Implementations are encouraged to either perform
73 * the operation as if the library had been initialized or to return
74 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
75 * implementations should not return a success status if the lack of
76 * initialization may have security implications, for example due to improper
77 * seeding of the random number generator.
78 *
Antonio de Angelis377a1552018-11-22 17:02:40 +000079 * \retval #PSA_SUCCESS
80 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Maulik Patel13b27cf2021-05-14 11:44:53 +010081 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Antonio de Angelis377a1552018-11-22 17:02:40 +000082 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
83 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +010084 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Antonio de Angelis377a1552018-11-22 17:02:40 +000085 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Maulik Patel13b27cf2021-05-14 11:44:53 +010086 * \retval #PSA_ERROR_STORAGE_FAILURE
87 * \retval #PSA_ERROR_DATA_INVALID
88 * \retval #PSA_ERROR_DATA_CORRUPT
Antonio de Angelis14276e92018-07-10 14:35:43 +010089 */
90psa_status_t psa_crypto_init(void);
91
Antonio de Angelis14276e92018-07-10 14:35:43 +010092/**@}*/
93
Antonio de Angelis04debbd2019-10-14 12:12:52 +010094/** \addtogroup attributes
Antonio de Angelis14276e92018-07-10 14:35:43 +010095 * @{
96 */
97
Antonio de Angelis04debbd2019-10-14 12:12:52 +010098/** \def PSA_KEY_ATTRIBUTES_INIT
Antonio de Angelis377a1552018-11-22 17:02:40 +000099 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100100 * This macro returns a suitable initializer for a key attribute structure
101 * of type #psa_key_attributes_t.
Antonio de Angelis8908f472018-08-31 15:44:25 +0100102 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100103#ifdef __DOXYGEN_ONLY__
104/* This is an example definition for documentation purposes.
105 * Implementations should define a suitable value in `crypto_struct.h`.
106 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100107#define PSA_KEY_ATTRIBUTES_INIT {0}
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100108#endif
109
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100110/** Return an initial value for a key attributes structure.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100111 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100112static psa_key_attributes_t psa_key_attributes_init(void);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100113
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100114/** Declare a key as persistent and set its key identifier.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100115 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100116 * If the attribute structure currently declares the key as volatile (which
117 * is the default content of an attribute structure), this function sets
118 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000119 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100120 * This function does not access storage, it merely stores the given
121 * value in the structure.
122 * The persistent key will be written to storage when the attribute
123 * structure is passed to a key creation function such as
124 * psa_import_key(), psa_generate_key(),
125 * psa_key_derivation_output_key() or psa_copy_key().
126 *
127 * This function may be declared as `static` (i.e. without external
128 * linkage). This function may be provided as a function-like macro,
129 * but in this case it must evaluate each of its arguments exactly once.
130 *
Maulik Patel28659c42021-01-06 14:09:22 +0000131 * \param[out] attributes The attribute structure to write to.
132 * \param key The persistent identifier for the key.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100133 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100134static void psa_set_key_id(psa_key_attributes_t *attributes,
Maulik Patel28659c42021-01-06 14:09:22 +0000135 psa_key_id_t key);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100136
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100137/** Set the location of a persistent key.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000138 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100139 * To make a key persistent, you must give it a persistent key identifier
140 * with psa_set_key_id(). By default, a key that has a persistent identifier
141 * is stored in the default storage area identifier by
142 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
143 * area, or to explicitly declare the key as volatile.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000144 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100145 * This function does not access storage, it merely stores the given
146 * value in the structure.
147 * The persistent key will be written to storage when the attribute
148 * structure is passed to a key creation function such as
149 * psa_import_key(), psa_generate_key(),
150 * psa_key_derivation_output_key() or psa_copy_key().
151 *
152 * This function may be declared as `static` (i.e. without external
153 * linkage). This function may be provided as a function-like macro,
154 * but in this case it must evaluate each of its arguments exactly once.
155 *
156 * \param[out] attributes The attribute structure to write to.
157 * \param lifetime The lifetime for the key.
158 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
159 * key will be volatile, and the key identifier
160 * attribute is reset to 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000161 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100162static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
163 psa_key_lifetime_t lifetime);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100164
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100165/** Retrieve the key identifier from key attributes.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000166 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100167 * This function may be declared as `static` (i.e. without external
168 * linkage). This function may be provided as a function-like macro,
169 * but in this case it must evaluate its argument exactly once.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000170 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100171 * \param[in] attributes The key attribute structure to query.
172 *
173 * \return The persistent identifier stored in the attribute structure.
174 * This value is unspecified if the attribute structure declares
175 * the key as volatile.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000176 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100177static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100178
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100179/** Retrieve the lifetime from key attributes.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100180 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100181 * This function may be declared as `static` (i.e. without external
182 * linkage). This function may be provided as a function-like macro,
183 * but in this case it must evaluate its argument exactly once.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100184 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100185 * \param[in] attributes The key attribute structure to query.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000186 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100187 * \return The lifetime value stored in the attribute structure.
188 */
189static psa_key_lifetime_t psa_get_key_lifetime(
190 const psa_key_attributes_t *attributes);
191
192/** Declare usage flags for a key.
193 *
194 * Usage flags are part of a key's usage policy. They encode what
195 * kind of operations are permitted on the key. For more details,
196 * refer to the documentation of the type #psa_key_usage_t.
197 *
198 * This function overwrites any usage flags
199 * previously set in \p attributes.
200 *
201 * This function may be declared as `static` (i.e. without external
202 * linkage). This function may be provided as a function-like macro,
203 * but in this case it must evaluate each of its arguments exactly once.
204 *
205 * \param[out] attributes The attribute structure to write to.
206 * \param usage_flags The usage flags to write.
207 */
208static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
209 psa_key_usage_t usage_flags);
210
211/** Retrieve the usage flags from key attributes.
212 *
213 * This function may be declared as `static` (i.e. without external
214 * linkage). This function may be provided as a function-like macro,
215 * but in this case it must evaluate its argument exactly once.
216 *
217 * \param[in] attributes The key attribute structure to query.
218 *
219 * \return The usage flags stored in the attribute structure.
220 */
221static psa_key_usage_t psa_get_key_usage_flags(
222 const psa_key_attributes_t *attributes);
223
224/** Declare the permitted algorithm policy for a key.
225 *
226 * The permitted algorithm policy of a key encodes which algorithm or
227 * algorithms are permitted to be used with this key. The following
228 * algorithm policies are supported:
229 * - 0 does not allow any cryptographic operation with the key. The key
230 * may be used for non-cryptographic actions such as exporting (if
231 * permitted by the usage flags).
232 * - An algorithm value permits this particular algorithm.
233 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
234 * signature scheme with any hash algorithm.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100235 * - An algorithm built from #PSA_ALG_AT_LEAST_THIS_LENGTH_MAC allows
236 * any MAC algorithm from the same base class (e.g. CMAC) which
237 * generates/verifies a MAC length greater than or equal to the length
238 * encoded in the wildcard algorithm.
239 * - An algorithm built from #PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG
240 * allows any AEAD algorithm from the same base class (e.g. CCM) which
241 * generates/verifies a tag length greater than or equal to the length
242 * encoded in the wildcard algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100243 *
244 * This function overwrites any algorithm policy
245 * previously set in \p attributes.
246 *
247 * This function may be declared as `static` (i.e. without external
248 * linkage). This function may be provided as a function-like macro,
249 * but in this case it must evaluate each of its arguments exactly once.
250 *
251 * \param[out] attributes The attribute structure to write to.
252 * \param alg The permitted algorithm policy to write.
253 */
254static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
255 psa_algorithm_t alg);
256
257
258/** Retrieve the algorithm policy from key attributes.
259 *
260 * This function may be declared as `static` (i.e. without external
261 * linkage). This function may be provided as a function-like macro,
262 * but in this case it must evaluate its argument exactly once.
263 *
264 * \param[in] attributes The key attribute structure to query.
265 *
266 * \return The algorithm stored in the attribute structure.
267 */
268static psa_algorithm_t psa_get_key_algorithm(
269 const psa_key_attributes_t *attributes);
270
271/** Declare the type of a key.
272 *
273 * This function overwrites any key type
274 * previously set in \p attributes.
275 *
276 * This function may be declared as `static` (i.e. without external
277 * linkage). This function may be provided as a function-like macro,
278 * but in this case it must evaluate each of its arguments exactly once.
279 *
280 * \param[out] attributes The attribute structure to write to.
281 * \param type The key type to write.
282 * If this is 0, the key type in \p attributes
283 * becomes unspecified.
284 */
285static void psa_set_key_type(psa_key_attributes_t *attributes,
286 psa_key_type_t type);
287
288
289/** Declare the size of a key.
290 *
291 * This function overwrites any key size previously set in \p attributes.
292 *
293 * This function may be declared as `static` (i.e. without external
294 * linkage). This function may be provided as a function-like macro,
295 * but in this case it must evaluate each of its arguments exactly once.
296 *
297 * \param[out] attributes The attribute structure to write to.
298 * \param bits The key size in bits.
299 * If this is 0, the key size in \p attributes
300 * becomes unspecified. Keys of size 0 are
301 * not supported.
302 */
303static void psa_set_key_bits(psa_key_attributes_t *attributes,
304 size_t bits);
305
306/** Retrieve the key type from key attributes.
307 *
308 * This function may be declared as `static` (i.e. without external
309 * linkage). This function may be provided as a function-like macro,
310 * but in this case it must evaluate its argument exactly once.
311 *
312 * \param[in] attributes The key attribute structure to query.
313 *
314 * \return The key type stored in the attribute structure.
315 */
316static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
317
318/** Retrieve the key size from key attributes.
319 *
320 * This function may be declared as `static` (i.e. without external
321 * linkage). This function may be provided as a function-like macro,
322 * but in this case it must evaluate its argument exactly once.
323 *
324 * \param[in] attributes The key attribute structure to query.
325 *
326 * \return The key size stored in the attribute structure, in bits.
327 */
328static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
329
330/** Retrieve the attributes of a key.
331 *
332 * This function first resets the attribute structure as with
333 * psa_reset_key_attributes(). It then copies the attributes of
334 * the given key into the given attribute structure.
335 *
336 * \note This function may allocate memory or other resources.
337 * Once you have called this function on an attribute structure,
338 * you must call psa_reset_key_attributes() to free these resources.
339 *
Maulik Patel28659c42021-01-06 14:09:22 +0000340 * \param[in] key Identifier of the key to query.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100341 * \param[in,out] attributes On success, the attributes of the key.
342 * On failure, equivalent to a
343 * freshly-initialized structure.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000344 *
345 * \retval #PSA_SUCCESS
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100346 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100347 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Antonio de Angelis377a1552018-11-22 17:02:40 +0000348 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100349 * \retval #PSA_ERROR_CORRUPTION_DETECTED
350 * \retval #PSA_ERROR_STORAGE_FAILURE
Maulik Patel13b27cf2021-05-14 11:44:53 +0100351 * \retval #PSA_ERROR_DATA_CORRUPT
352 * \retval #PSA_ERROR_DATA_INVALID
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100353 * \retval #PSA_ERROR_BAD_STATE
354 * The library has not been previously initialized by psa_crypto_init().
355 * It is implementation-dependent whether a failure to initialize
356 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100357 */
Maulik Patel28659c42021-01-06 14:09:22 +0000358psa_status_t psa_get_key_attributes(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100359 psa_key_attributes_t *attributes);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100360
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100361/** Reset a key attribute structure to a freshly initialized state.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000362 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100363 * You must initialize the attribute structure as described in the
364 * documentation of the type #psa_key_attributes_t before calling this
365 * function. Once the structure has been initialized, you may call this
366 * function at any time.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000367 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100368 * This function frees any auxiliary resources that the structure
369 * may contain.
370 *
371 * \param[in,out] attributes The attribute structure to reset.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000372 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100373void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100374
375/**@}*/
376
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100377/** \defgroup key_management Key management
Antonio de Angelis14276e92018-07-10 14:35:43 +0100378 * @{
379 */
380
Maulik Patel28659c42021-01-06 14:09:22 +0000381/** Remove non-essential copies of key material from memory.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100382 *
Maulik Patel28659c42021-01-06 14:09:22 +0000383 * If the key identifier designates a volatile key, this functions does not do
384 * anything and returns successfully.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100385 *
Maulik Patel28659c42021-01-06 14:09:22 +0000386 * If the key identifier designates a persistent key, then this function will
387 * free all resources associated with the key in volatile memory. The key
388 * data in persistent storage is not affected and the key can still be used.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100389 *
Maulik Patel28659c42021-01-06 14:09:22 +0000390 * \param key Identifier of the key to purge.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100391 *
392 * \retval #PSA_SUCCESS
Maulik Patel28659c42021-01-06 14:09:22 +0000393 * The key material will have been removed from memory if it is not
394 * currently required.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100395 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +0000396 * \p key is not a valid key identifier.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100397 * \retval #PSA_ERROR_BAD_STATE
398 * The library has not been previously initialized by psa_crypto_init().
399 * It is implementation-dependent whether a failure to initialize
400 * results in this error code.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100401 */
Maulik Patel28659c42021-01-06 14:09:22 +0000402psa_status_t psa_purge_key(psa_key_id_t key);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100403
404/** Make a copy of a key.
405 *
406 * Copy key material from one location to another.
407 *
408 * This function is primarily useful to copy a key from one location
409 * to another, since it populates a key using the material from
410 * another key which may have a different lifetime.
411 *
412 * This function may be used to share a key with a different party,
413 * subject to implementation-defined restrictions on key sharing.
414 *
415 * The policy on the source key must have the usage flag
416 * #PSA_KEY_USAGE_COPY set.
417 * This flag is sufficient to permit the copy if the key has the lifetime
418 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
419 * Some secure elements do not provide a way to copy a key without
420 * making it extractable from the secure element. If a key is located
421 * in such a secure element, then the key must have both usage flags
422 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
423 * a copy of the key outside the secure element.
424 *
425 * The resulting key may only be used in a way that conforms to
426 * both the policy of the original key and the policy specified in
427 * the \p attributes parameter:
428 * - The usage flags on the resulting key are the bitwise-and of the
429 * usage flags on the source policy and the usage flags in \p attributes.
430 * - If both allow the same algorithm or wildcard-based
431 * algorithm policy, the resulting key has the same algorithm policy.
432 * - If either of the policies allows an algorithm and the other policy
433 * allows a wildcard-based algorithm policy that includes this algorithm,
434 * the resulting key allows the same algorithm.
435 * - If the policies do not allow any algorithm in common, this function
436 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
437 *
438 * The effect of this function on implementation-defined attributes is
439 * implementation-defined.
440 *
Maulik Patel28659c42021-01-06 14:09:22 +0000441 * \param source_key The key to copy. It must allow the usage
442 * #PSA_KEY_USAGE_COPY. If a private or secret key is
443 * being copied outside of a secure element it must
444 * also allow #PSA_KEY_USAGE_EXPORT.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100445 * \param[in] attributes The attributes for the new key.
446 * They are used as follows:
447 * - The key type and size may be 0. If either is
448 * nonzero, it must match the corresponding
449 * attribute of the source key.
450 * - The key location (the lifetime and, for
451 * persistent keys, the key identifier) is
452 * used directly.
453 * - The policy constraints (usage flags and
454 * algorithm policy) are combined from
455 * the source key and \p attributes so that
456 * both sets of restrictions apply, as
457 * described in the documentation of this function.
Maulik Patel28659c42021-01-06 14:09:22 +0000458 * \param[out] target_key On success, an identifier for the newly created
459 * key. For persistent keys, this is the key
460 * identifier defined in \p attributes.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100461 * \c 0 on failure.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100462 *
463 * \retval #PSA_SUCCESS
464 * \retval #PSA_ERROR_INVALID_HANDLE
Maulik Patel28659c42021-01-06 14:09:22 +0000465 * \p source_key is invalid.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100466 * \retval #PSA_ERROR_ALREADY_EXISTS
467 * This is an attempt to create a persistent key, and there is
468 * already a persistent key with the given identifier.
469 * \retval #PSA_ERROR_INVALID_ARGUMENT
470 * The lifetime or identifier in \p attributes are invalid.
471 * \retval #PSA_ERROR_INVALID_ARGUMENT
472 * The policy constraints on the source and specified in
473 * \p attributes are incompatible.
474 * \retval #PSA_ERROR_INVALID_ARGUMENT
475 * \p attributes specifies a key type or key size
476 * which does not match the attributes of the source key.
477 * \retval #PSA_ERROR_NOT_PERMITTED
478 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
479 * \retval #PSA_ERROR_NOT_PERMITTED
480 * The source key is not exportable and its lifetime does not
481 * allow copying it to the target's lifetime.
482 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
483 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100484 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100485 * \retval #PSA_ERROR_HARDWARE_FAILURE
Maulik Patel13b27cf2021-05-14 11:44:53 +0100486 * \retval #PSA_ERROR_DATA_INVALID
487 * \retval #PSA_ERROR_DATA_CORRUPT
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100488 * \retval #PSA_ERROR_STORAGE_FAILURE
489 * \retval #PSA_ERROR_CORRUPTION_DETECTED
490 * \retval #PSA_ERROR_BAD_STATE
491 * The library has not been previously initialized by psa_crypto_init().
492 * It is implementation-dependent whether a failure to initialize
493 * results in this error code.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100494 */
Maulik Patel28659c42021-01-06 14:09:22 +0000495psa_status_t psa_copy_key(psa_key_id_t source_key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100496 const psa_key_attributes_t *attributes,
Maulik Patel28659c42021-01-06 14:09:22 +0000497 psa_key_id_t *target_key);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100498
499
500/**
501 * \brief Destroy a key.
502 *
503 * This function destroys a key from both volatile
504 * memory and, if applicable, non-volatile storage. Implementations shall
505 * make a best effort to ensure that that the key material cannot be recovered.
506 *
507 * This function also erases any metadata such as policies and frees
Maulik Patel28659c42021-01-06 14:09:22 +0000508 * resources associated with the key.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100509 *
510 * If a key is currently in use in a multipart operation, then destroying the
511 * key will cause the multipart operation to fail.
512 *
Maulik Patel28659c42021-01-06 14:09:22 +0000513 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
514 * return #PSA_SUCCESS.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100515 *
516 * \retval #PSA_SUCCESS
Maulik Patel28659c42021-01-06 14:09:22 +0000517 * \p key was a valid identifier and the key material that it
518 * referred to has been erased. Alternatively, \p key is \c 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100519 * \retval #PSA_ERROR_NOT_PERMITTED
520 * The key cannot be erased because it is
521 * read-only, either due to a policy or due to physical restrictions.
522 * \retval #PSA_ERROR_INVALID_HANDLE
Maulik Patel28659c42021-01-06 14:09:22 +0000523 * \p key is not a valid identifier nor \c 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100524 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
525 * There was an failure in communication with the cryptoprocessor.
526 * The key material may still be present in the cryptoprocessor.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100527 * \retval #PSA_ERROR_DATA_INVALID
528 * This error is typically a result of either storage corruption on a
529 * cleartext storage backend, or an attempt to read data that was
530 * written by an incompatible version of the library.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100531 * \retval #PSA_ERROR_STORAGE_FAILURE
532 * The storage is corrupted. Implementations shall make a best effort
533 * to erase key material even in this stage, however applications
534 * should be aware that it may be impossible to guarantee that the
535 * key material is not recoverable in such cases.
536 * \retval #PSA_ERROR_CORRUPTION_DETECTED
537 * An unexpected condition which is not a storage corruption or
538 * a communication failure occurred. The cryptoprocessor may have
539 * been compromised.
540 * \retval #PSA_ERROR_BAD_STATE
541 * The library has not been previously initialized by psa_crypto_init().
542 * It is implementation-dependent whether a failure to initialize
543 * results in this error code.
544 */
Maulik Patel28659c42021-01-06 14:09:22 +0000545psa_status_t psa_destroy_key(psa_key_id_t key);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100546
547/**@}*/
548
549/** \defgroup import_export Key import and export
550 * @{
551 */
552
553/**
554 * \brief Import a key in binary format.
555 *
556 * This function supports any output from psa_export_key(). Refer to the
557 * documentation of psa_export_public_key() for the format of public keys
558 * and to the documentation of psa_export_key() for the format for
559 * other key types.
560 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100561 * The key data determines the key size. The attributes may optionally
562 * specify a key size; in this case it must match the size determined
563 * from the key data. A key size of 0 in \p attributes indicates that
564 * the key size is solely determined by the key data.
565 *
566 * Implementations must reject an attempt to import a key of size 0.
567 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100568 * This specification supports a single format for each key type.
569 * Implementations may support other formats as long as the standard
570 * format is supported. Implementations that support other formats
571 * should ensure that the formats are clearly unambiguous so as to
572 * minimize the risk that an invalid input is accidentally interpreted
573 * according to a different format.
574 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100575 * \param[in] attributes The attributes for the new key.
576 * The key size is always determined from the
577 * \p data buffer.
578 * If the key size in \p attributes is nonzero,
579 * it must be equal to the size from \p data.
Maulik Patel28659c42021-01-06 14:09:22 +0000580 * \param[out] key On success, an identifier to the newly created key.
581 * For persistent keys, this is the key identifier
582 * defined in \p attributes.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100583 * \c 0 on failure.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100584 * \param[in] data Buffer containing the key data. The content of this
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100585 * buffer is interpreted according to the type declared
586 * in \p attributes.
587 * All implementations must support at least the format
588 * described in the documentation
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100589 * of psa_export_key() or psa_export_public_key() for
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100590 * the chosen type. Implementations may allow other
591 * formats, but should be conservative: implementations
592 * should err on the side of rejecting content if it
593 * may be erroneous (e.g. wrong type or truncated data).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100594 * \param data_length Size of the \p data buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100595 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000596 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +0100597 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100598 * If the key is persistent, the key material and the key's metadata
599 * have been saved to persistent storage.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100600 * \retval #PSA_ERROR_ALREADY_EXISTS
601 * This is an attempt to create a persistent key, and there is
602 * already a persistent key with the given identifier.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100603 * \retval #PSA_ERROR_NOT_SUPPORTED
604 * The key type or key size is not supported, either by the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100605 * implementation in general or in this particular persistent location.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000606 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100607 * The key attributes, as a whole, are invalid.
608 * \retval #PSA_ERROR_INVALID_ARGUMENT
609 * The key data is not correctly formatted.
610 * \retval #PSA_ERROR_INVALID_ARGUMENT
611 * The size in \p attributes is nonzero and does not match the size
612 * of the key data.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100613 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
614 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
615 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Maulik Patel13b27cf2021-05-14 11:44:53 +0100616 * \retval #PSA_ERROR_DATA_CORRUPT
617 * \retval #PSA_ERROR_DATA_INVALID
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100618 * \retval #PSA_ERROR_STORAGE_FAILURE
619 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100620 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100621 * \retval #PSA_ERROR_BAD_STATE
622 * The library has not been previously initialized by psa_crypto_init().
623 * It is implementation-dependent whether a failure to initialize
624 * results in this error code.
625 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100626psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100627 const uint8_t *data,
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100628 size_t data_length,
Maulik Patel28659c42021-01-06 14:09:22 +0000629 psa_key_id_t *key);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100630
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100631
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100632
633/**
634 * \brief Export a key in binary format.
635 *
636 * The output of this function can be passed to psa_import_key() to
637 * create an equivalent object.
638 *
639 * If the implementation of psa_import_key() supports other formats
640 * beyond the format specified here, the output from psa_export_key()
641 * must use the representation specified here, not the original
642 * representation.
643 *
644 * For standard key types, the output format is as follows:
645 *
646 * - For symmetric keys (including MAC keys), the format is the
647 * raw bytes of the key.
648 * - For DES, the key data consists of 8 bytes. The parity bits must be
649 * correct.
650 * - For Triple-DES, the format is the concatenation of the
651 * two or three DES keys.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100652 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100653 * is the non-encrypted DER encoding of the representation defined by
654 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
655 * ```
656 * RSAPrivateKey ::= SEQUENCE {
657 * version INTEGER, -- must be 0
658 * modulus INTEGER, -- n
659 * publicExponent INTEGER, -- e
660 * privateExponent INTEGER, -- d
661 * prime1 INTEGER, -- p
662 * prime2 INTEGER, -- q
663 * exponent1 INTEGER, -- d mod (p-1)
664 * exponent2 INTEGER, -- d mod (q-1)
665 * coefficient INTEGER, -- (inverse of q) mod p
666 * }
667 * ```
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100668 * - For elliptic curve key pairs (key types for which
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100669 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100670 * a representation of the private value as a `ceiling(m/8)`-byte string
671 * where `m` is the bit size associated with the curve, i.e. the bit size
672 * of the order of the curve's coordinate field. This byte string is
673 * in little-endian order for Montgomery curves (curve types
Summer Qin0e5b2e02020-10-22 11:23:39 +0800674 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
675 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
676 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
677 * For Weierstrass curves, this is the content of the `privateKey` field of
678 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
679 * the format is defined by RFC 7748, and output is masked according to §5.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100680 * - For Diffie-Hellman key exchange key pairs (key types for which
681 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
682 * format is the representation of the private key `x` as a big-endian byte
683 * string. The length of the byte string is the private key size in bytes
684 * (leading zeroes are not stripped).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100685 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
686 * true), the format is the same as for psa_export_public_key().
687 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100688 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
689 *
Maulik Patel28659c42021-01-06 14:09:22 +0000690 * \param key Identifier of the key to export. It must allow the
691 * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
692 * key.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100693 * \param[out] data Buffer where the key data is to be written.
694 * \param data_size Size of the \p data buffer in bytes.
695 * \param[out] data_length On success, the number of bytes
696 * that make up the key data.
697 *
698 * \retval #PSA_SUCCESS
699 * \retval #PSA_ERROR_INVALID_HANDLE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100700 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100701 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000702 * \retval #PSA_ERROR_NOT_SUPPORTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100703 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
704 * The size of the \p data buffer is too small. You can determine a
705 * sufficient buffer size by calling
Maulik Patel13b27cf2021-05-14 11:44:53 +0100706 * #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100707 * where \c type is the key type
708 * and \c bits is the key size in bits.
709 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
710 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100711 * \retval #PSA_ERROR_CORRUPTION_DETECTED
712 * \retval #PSA_ERROR_STORAGE_FAILURE
713 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100714 * \retval #PSA_ERROR_BAD_STATE
715 * The library has not been previously initialized by psa_crypto_init().
716 * It is implementation-dependent whether a failure to initialize
717 * results in this error code.
718 */
Maulik Patel28659c42021-01-06 14:09:22 +0000719psa_status_t psa_export_key(psa_key_id_t key,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100720 uint8_t *data,
721 size_t data_size,
722 size_t *data_length);
723
724/**
725 * \brief Export a public key or the public part of a key pair in binary format.
726 *
727 * The output of this function can be passed to psa_import_key() to
728 * create an object that is equivalent to the public key.
729 *
730 * This specification supports a single format for each key type.
731 * Implementations may support other formats as long as the standard
732 * format is supported. Implementations that support other formats
733 * should ensure that the formats are clearly unambiguous so as to
734 * minimize the risk that an invalid input is accidentally interpreted
735 * according to a different format.
736 *
737 * For standard key types, the output format is as follows:
738 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
739 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
740 * ```
741 * RSAPublicKey ::= SEQUENCE {
742 * modulus INTEGER, -- n
743 * publicExponent INTEGER } -- e
744 * ```
745 * - For elliptic curve public keys (key types for which
746 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100747 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100748 * Let `m` be the bit size associated with the curve, i.e. the bit size of
749 * `q` for a curve over `F_q`. The representation consists of:
750 * - The byte 0x04;
751 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
752 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100753 * - For Diffie-Hellman key exchange public keys (key types for which
754 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
755 * the format is the representation of the public key `y = g^x mod p` as a
756 * big-endian byte string. The length of the byte string is the length of the
757 * base prime `p` in bytes.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100758 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100759 * Exporting a public key object or the public part of a key pair is
760 * always permitted, regardless of the key's usage flags.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100761 *
Maulik Patel28659c42021-01-06 14:09:22 +0000762 * \param key Identifier of the key to export.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100763 * \param[out] data Buffer where the key data is to be written.
764 * \param data_size Size of the \p data buffer in bytes.
765 * \param[out] data_length On success, the number of bytes
766 * that make up the key data.
767 *
768 * \retval #PSA_SUCCESS
769 * \retval #PSA_ERROR_INVALID_HANDLE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100770 * \retval #PSA_ERROR_INVALID_ARGUMENT
771 * The key is neither a public key nor a key pair.
772 * \retval #PSA_ERROR_NOT_SUPPORTED
773 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
774 * The size of the \p data buffer is too small. You can determine a
775 * sufficient buffer size by calling
Maulik Patel13b27cf2021-05-14 11:44:53 +0100776 * #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100777 * where \c type is the key type
778 * and \c bits is the key size in bits.
779 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
780 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100781 * \retval #PSA_ERROR_CORRUPTION_DETECTED
782 * \retval #PSA_ERROR_STORAGE_FAILURE
783 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100784 * \retval #PSA_ERROR_BAD_STATE
785 * The library has not been previously initialized by psa_crypto_init().
786 * It is implementation-dependent whether a failure to initialize
787 * results in this error code.
788 */
Maulik Patel28659c42021-01-06 14:09:22 +0000789psa_status_t psa_export_public_key(psa_key_id_t key,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100790 uint8_t *data,
791 size_t data_size,
792 size_t *data_length);
793
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100794
Antonio de Angelis14276e92018-07-10 14:35:43 +0100795
796/**@}*/
797
798/** \defgroup hash Message digests
799 * @{
800 */
801
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100802/** Calculate the hash (digest) of a message.
803 *
804 * \note To verify the hash of a message against an
805 * expected value, use psa_hash_compare() instead.
806 *
807 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
808 * such that #PSA_ALG_IS_HASH(\p alg) is true).
809 * \param[in] input Buffer containing the message to hash.
810 * \param input_length Size of the \p input buffer in bytes.
811 * \param[out] hash Buffer where the hash is to be written.
812 * \param hash_size Size of the \p hash buffer in bytes.
813 * \param[out] hash_length On success, the number of bytes
814 * that make up the hash value. This is always
Maulik Patel13b27cf2021-05-14 11:44:53 +0100815 * #PSA_HASH_LENGTH(\p alg).
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100816 *
817 * \retval #PSA_SUCCESS
818 * Success.
819 * \retval #PSA_ERROR_NOT_SUPPORTED
820 * \p alg is not supported or is not a hash algorithm.
821 * \retval #PSA_ERROR_INVALID_ARGUMENT
822 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
823 * \p hash_size is too small
824 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
825 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
826 * \retval #PSA_ERROR_HARDWARE_FAILURE
827 * \retval #PSA_ERROR_CORRUPTION_DETECTED
828 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
829 * \retval #PSA_ERROR_BAD_STATE
830 * The library has not been previously initialized by psa_crypto_init().
831 * It is implementation-dependent whether a failure to initialize
832 * results in this error code.
833 */
834psa_status_t psa_hash_compute(psa_algorithm_t alg,
835 const uint8_t *input,
836 size_t input_length,
837 uint8_t *hash,
838 size_t hash_size,
839 size_t *hash_length);
840
841/** Calculate the hash (digest) of a message and compare it with a
842 * reference value.
843 *
844 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
845 * such that #PSA_ALG_IS_HASH(\p alg) is true).
846 * \param[in] input Buffer containing the message to hash.
847 * \param input_length Size of the \p input buffer in bytes.
848 * \param[out] hash Buffer containing the expected hash value.
849 * \param hash_length Size of the \p hash buffer in bytes.
850 *
851 * \retval #PSA_SUCCESS
852 * The expected hash is identical to the actual hash of the input.
853 * \retval #PSA_ERROR_INVALID_SIGNATURE
854 * The hash of the message was calculated successfully, but it
855 * differs from the expected hash.
856 * \retval #PSA_ERROR_NOT_SUPPORTED
857 * \p alg is not supported or is not a hash algorithm.
858 * \retval #PSA_ERROR_INVALID_ARGUMENT
859 * \p input_length or \p hash_length do not match the hash size for \p alg
860 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
861 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
862 * \retval #PSA_ERROR_HARDWARE_FAILURE
863 * \retval #PSA_ERROR_CORRUPTION_DETECTED
864 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
865 * \retval #PSA_ERROR_BAD_STATE
866 * The library has not been previously initialized by psa_crypto_init().
867 * It is implementation-dependent whether a failure to initialize
868 * results in this error code.
869 */
870psa_status_t psa_hash_compare(psa_algorithm_t alg,
871 const uint8_t *input,
872 size_t input_length,
873 const uint8_t *hash,
Soby Mathew07ef6e42020-07-20 21:09:23 +0100874 size_t hash_length);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100875
Antonio de Angelis377a1552018-11-22 17:02:40 +0000876/** The type of the state data structure for multipart hash operations.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100877 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100878 * Before calling any function on a hash operation object, the application must
879 * initialize it by any of the following means:
880 * - Set the structure to all-bits-zero, for example:
881 * \code
882 * psa_hash_operation_t operation;
883 * memset(&operation, 0, sizeof(operation));
884 * \endcode
885 * - Initialize the structure to logical zero values, for example:
886 * \code
887 * psa_hash_operation_t operation = {0};
888 * \endcode
889 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
890 * for example:
891 * \code
892 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
893 * \endcode
894 * - Assign the result of the function psa_hash_operation_init()
895 * to the structure, for example:
896 * \code
897 * psa_hash_operation_t operation;
898 * operation = psa_hash_operation_init();
899 * \endcode
900 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000901 * This is an implementation-defined \c struct. Applications should not
902 * make any assumptions about the content of this structure except
903 * as directed by the documentation of a specific implementation. */
904typedef struct psa_hash_operation_s psa_hash_operation_t;
Antonio de Angelis14276e92018-07-10 14:35:43 +0100905
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100906/** \def PSA_HASH_OPERATION_INIT
Antonio de Angelis14276e92018-07-10 14:35:43 +0100907 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100908 * This macro returns a suitable initializer for a hash operation object
909 * of type #psa_hash_operation_t.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100910 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100911#ifdef __DOXYGEN_ONLY__
912/* This is an example definition for documentation purposes.
913 * Implementations should define a suitable value in `crypto_struct.h`.
914 */
915#define PSA_HASH_OPERATION_INIT {0}
916#endif
Antonio de Angelis14276e92018-07-10 14:35:43 +0100917
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100918/** Return an initial value for a hash operation object.
919 */
920static psa_hash_operation_t psa_hash_operation_init(void);
921
922/** Set up a multipart hash operation.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100923 *
924 * The sequence of operations to calculate a hash (message digest)
925 * is as follows:
926 * -# Allocate an operation object which will be passed to all the functions
927 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100928 * -# Initialize the operation object with one of the methods described in the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100929 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000930 * -# Call psa_hash_setup() to specify the algorithm.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100931 * -# Call psa_hash_update() zero, one or more times, passing a fragment
932 * of the message each time. The hash that is calculated is the hash
933 * of the concatenation of these messages in order.
934 * -# To calculate the hash, call psa_hash_finish().
935 * To compare the hash with an expected value, call psa_hash_verify().
936 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100937 * If an error occurs at any step after a call to psa_hash_setup(), the
938 * operation will need to be reset by a call to psa_hash_abort(). The
939 * application may call psa_hash_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100940 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100941 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000942 * After a successful call to psa_hash_setup(), the application must
Antonio de Angelis14276e92018-07-10 14:35:43 +0100943 * eventually terminate the operation. The following events terminate an
944 * operation:
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100945 * - A successful call to psa_hash_finish() or psa_hash_verify().
946 * - A call to psa_hash_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +0100947 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100948 * \param[in,out] operation The operation object to set up. It must have
949 * been initialized as per the documentation for
950 * #psa_hash_operation_t and not yet in use.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000951 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
952 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +0100953 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000954 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +0100955 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000956 * \retval #PSA_ERROR_NOT_SUPPORTED
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100957 * \p alg is not a supported hash algorithm.
958 * \retval #PSA_ERROR_INVALID_ARGUMENT
959 * \p alg is not a hash algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100960 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100961 * The operation state is not valid (it must be inactive).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000962 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
963 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
964 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100965 * \retval #PSA_ERROR_CORRUPTION_DETECTED
966 * \retval #PSA_ERROR_BAD_STATE
967 * The library has not been previously initialized by psa_crypto_init().
968 * It is implementation-dependent whether a failure to initialize
969 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100970 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000971psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Antonio de Angelis14276e92018-07-10 14:35:43 +0100972 psa_algorithm_t alg);
973
974/** Add a message fragment to a multipart hash operation.
975 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000976 * The application must call psa_hash_setup() before calling this function.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100977 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100978 * If this function returns an error status, the operation enters an error
979 * state and must be aborted by calling psa_hash_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +0100980 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000981 * \param[in,out] operation Active hash operation.
982 * \param[in] input Buffer containing the message fragment to hash.
983 * \param input_length Size of the \p input buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100984 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000985 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +0100986 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000987 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100988 * The operation state is not valid (it muct be active).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000989 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
990 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
991 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100992 * \retval #PSA_ERROR_CORRUPTION_DETECTED
993 * \retval #PSA_ERROR_BAD_STATE
994 * The library has not been previously initialized by psa_crypto_init().
995 * It is implementation-dependent whether a failure to initialize
996 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100997 */
998psa_status_t psa_hash_update(psa_hash_operation_t *operation,
999 const uint8_t *input,
1000 size_t input_length);
1001
1002/** Finish the calculation of the hash of a message.
1003 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001004 * The application must call psa_hash_setup() before calling this function.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001005 * This function calculates the hash of the message formed by concatenating
1006 * the inputs passed to preceding calls to psa_hash_update().
1007 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001008 * When this function returns successfuly, the operation becomes inactive.
1009 * If this function returns an error status, the operation enters an error
1010 * state and must be aborted by calling psa_hash_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001011 *
1012 * \warning Applications should not call this function if they expect
1013 * a specific value for the hash. Call psa_hash_verify() instead.
1014 * Beware that comparing integrity or authenticity data such as
1015 * hash values with a function such as \c memcmp is risky
1016 * because the time taken by the comparison may leak information
1017 * about the hashed data which could allow an attacker to guess
1018 * a valid hash and thereby bypass security controls.
1019 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001020 * \param[in,out] operation Active hash operation.
1021 * \param[out] hash Buffer where the hash is to be written.
1022 * \param hash_size Size of the \p hash buffer in bytes.
1023 * \param[out] hash_length On success, the number of bytes
1024 * that make up the hash value. This is always
Maulik Patel13b27cf2021-05-14 11:44:53 +01001025 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
Antonio de Angelis377a1552018-11-22 17:02:40 +00001026 * hash algorithm that is calculated.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001027 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001028 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001029 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001030 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001031 * The operation state is not valid (it must be active).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001032 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1033 * The size of the \p hash buffer is too small. You can determine a
Maulik Patel13b27cf2021-05-14 11:44:53 +01001034 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01001035 * where \c alg is the hash algorithm that is calculated.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001036 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1037 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1038 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001039 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1040 * \retval #PSA_ERROR_BAD_STATE
1041 * The library has not been previously initialized by psa_crypto_init().
1042 * It is implementation-dependent whether a failure to initialize
1043 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001044 */
1045psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1046 uint8_t *hash,
1047 size_t hash_size,
1048 size_t *hash_length);
1049
1050/** Finish the calculation of the hash of a message and compare it with
1051 * an expected value.
1052 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001053 * The application must call psa_hash_setup() before calling this function.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001054 * This function calculates the hash of the message formed by concatenating
1055 * the inputs passed to preceding calls to psa_hash_update(). It then
1056 * compares the calculated hash with the expected hash passed as a
1057 * parameter to this function.
1058 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001059 * When this function returns successfuly, the operation becomes inactive.
1060 * If this function returns an error status, the operation enters an error
1061 * state and must be aborted by calling psa_hash_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001062 *
1063 * \note Implementations shall make the best effort to ensure that the
1064 * comparison between the actual hash and the expected hash is performed
1065 * in constant time.
1066 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001067 * \param[in,out] operation Active hash operation.
1068 * \param[in] hash Buffer containing the expected hash value.
1069 * \param hash_length Size of the \p hash buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001070 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001071 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001072 * The expected hash is identical to the actual hash of the message.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001073 * \retval #PSA_ERROR_INVALID_SIGNATURE
Antonio de Angelis14276e92018-07-10 14:35:43 +01001074 * The hash of the message was calculated successfully, but it
1075 * differs from the expected hash.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001076 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001077 * The operation state is not valid (it must be active).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001078 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1079 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1080 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001081 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1082 * \retval #PSA_ERROR_BAD_STATE
1083 * The library has not been previously initialized by psa_crypto_init().
1084 * It is implementation-dependent whether a failure to initialize
1085 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001086 */
1087psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1088 const uint8_t *hash,
1089 size_t hash_length);
1090
1091/** Abort a hash operation.
1092 *
Antonio de Angelis14276e92018-07-10 14:35:43 +01001093 * Aborting an operation frees all associated resources except for the
Antonio de Angelis377a1552018-11-22 17:02:40 +00001094 * \p operation structure itself. Once aborted, the operation object
1095 * can be reused for another operation by calling
1096 * psa_hash_setup() again.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001097 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001098 * You may call this function any time after the operation object has
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001099 * been initialized by one of the methods described in #psa_hash_operation_t.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001100 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001101 * In particular, calling psa_hash_abort() after the operation has been
1102 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1103 * psa_hash_verify() is safe and has no effect.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001104 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001105 * \param[in,out] operation Initialized hash operation.
1106 *
1107 * \retval #PSA_SUCCESS
Antonio de Angelis377a1552018-11-22 17:02:40 +00001108 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1109 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001110 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1111 * \retval #PSA_ERROR_BAD_STATE
1112 * The library has not been previously initialized by psa_crypto_init().
1113 * It is implementation-dependent whether a failure to initialize
1114 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001115 */
1116psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
1117
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001118/** Clone a hash operation.
1119 *
1120 * This function copies the state of an ongoing hash operation to
1121 * a new operation object. In other words, this function is equivalent
1122 * to calling psa_hash_setup() on \p target_operation with the same
1123 * algorithm that \p source_operation was set up for, then
1124 * psa_hash_update() on \p target_operation with the same input that
1125 * that was passed to \p source_operation. After this function returns, the
1126 * two objects are independent, i.e. subsequent calls involving one of
1127 * the objects do not affect the other object.
1128 *
1129 * \param[in] source_operation The active hash operation to clone.
1130 * \param[in,out] target_operation The operation object to set up.
1131 * It must be initialized but not active.
1132 *
1133 * \retval #PSA_SUCCESS
1134 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001135 * The \p source_operation state is not valid (it must be active).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001136 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001137 * The \p target_operation state is not valid (it must be inactive).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001138 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1139 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001140 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1141 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1142 * \retval #PSA_ERROR_BAD_STATE
1143 * The library has not been previously initialized by psa_crypto_init().
1144 * It is implementation-dependent whether a failure to initialize
1145 * results in this error code.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001146 */
1147psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1148 psa_hash_operation_t *target_operation);
1149
Antonio de Angelis14276e92018-07-10 14:35:43 +01001150/**@}*/
1151
1152/** \defgroup MAC Message authentication codes
1153 * @{
1154 */
1155
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001156/** Calculate the MAC (message authentication code) of a message.
1157 *
1158 * \note To verify the MAC of a message against an
1159 * expected value, use psa_mac_verify() instead.
1160 * Beware that comparing integrity or authenticity data such as
1161 * MAC values with a function such as \c memcmp is risky
1162 * because the time taken by the comparison may leak information
1163 * about the MAC value which could allow an attacker to guess
1164 * a valid MAC and thereby bypass security controls.
1165 *
Maulik Patel28659c42021-01-06 14:09:22 +00001166 * \param key Identifier of the key to use for the operation. It
1167 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001168 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1169 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1170 * \param[in] input Buffer containing the input message.
1171 * \param input_length Size of the \p input buffer in bytes.
1172 * \param[out] mac Buffer where the MAC value is to be written.
1173 * \param mac_size Size of the \p mac buffer in bytes.
1174 * \param[out] mac_length On success, the number of bytes
1175 * that make up the MAC value.
1176 *
1177 * \retval #PSA_SUCCESS
1178 * Success.
1179 * \retval #PSA_ERROR_INVALID_HANDLE
1180 * \retval #PSA_ERROR_NOT_PERMITTED
1181 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00001182 * \p key is not compatible with \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001183 * \retval #PSA_ERROR_NOT_SUPPORTED
1184 * \p alg is not supported or is not a MAC algorithm.
1185 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1186 * \p mac_size is too small
1187 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1188 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1189 * \retval #PSA_ERROR_HARDWARE_FAILURE
1190 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1191 * \retval #PSA_ERROR_STORAGE_FAILURE
1192 * The key could not be retrieved from storage.
1193 * \retval #PSA_ERROR_BAD_STATE
1194 * The library has not been previously initialized by psa_crypto_init().
1195 * It is implementation-dependent whether a failure to initialize
1196 * results in this error code.
1197 */
Maulik Patel28659c42021-01-06 14:09:22 +00001198psa_status_t psa_mac_compute(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001199 psa_algorithm_t alg,
1200 const uint8_t *input,
1201 size_t input_length,
1202 uint8_t *mac,
1203 size_t mac_size,
1204 size_t *mac_length);
1205
1206/** Calculate the MAC of a message and compare it with a reference value.
1207 *
Maulik Patel28659c42021-01-06 14:09:22 +00001208 * \param key Identifier of the key to use for the operation. It
1209 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001210 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1211 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1212 * \param[in] input Buffer containing the input message.
1213 * \param input_length Size of the \p input buffer in bytes.
1214 * \param[out] mac Buffer containing the expected MAC value.
1215 * \param mac_length Size of the \p mac buffer in bytes.
1216 *
1217 * \retval #PSA_SUCCESS
1218 * The expected MAC is identical to the actual MAC of the input.
1219 * \retval #PSA_ERROR_INVALID_SIGNATURE
1220 * The MAC of the message was calculated successfully, but it
1221 * differs from the expected value.
1222 * \retval #PSA_ERROR_INVALID_HANDLE
1223 * \retval #PSA_ERROR_NOT_PERMITTED
1224 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00001225 * \p key is not compatible with \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001226 * \retval #PSA_ERROR_NOT_SUPPORTED
1227 * \p alg is not supported or is not a MAC algorithm.
1228 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1229 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1230 * \retval #PSA_ERROR_HARDWARE_FAILURE
1231 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1232 * \retval #PSA_ERROR_STORAGE_FAILURE
1233 * The key could not be retrieved from storage.
1234 * \retval #PSA_ERROR_BAD_STATE
1235 * The library has not been previously initialized by psa_crypto_init().
1236 * It is implementation-dependent whether a failure to initialize
1237 * results in this error code.
1238 */
Maulik Patel28659c42021-01-06 14:09:22 +00001239psa_status_t psa_mac_verify(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001240 psa_algorithm_t alg,
1241 const uint8_t *input,
1242 size_t input_length,
1243 const uint8_t *mac,
Soby Mathew07ef6e42020-07-20 21:09:23 +01001244 size_t mac_length);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001245
Antonio de Angelis377a1552018-11-22 17:02:40 +00001246/** The type of the state data structure for multipart MAC operations.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001247 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001248 * Before calling any function on a MAC operation object, the application must
1249 * initialize it by any of the following means:
1250 * - Set the structure to all-bits-zero, for example:
1251 * \code
1252 * psa_mac_operation_t operation;
1253 * memset(&operation, 0, sizeof(operation));
1254 * \endcode
1255 * - Initialize the structure to logical zero values, for example:
1256 * \code
1257 * psa_mac_operation_t operation = {0};
1258 * \endcode
1259 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1260 * for example:
1261 * \code
1262 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1263 * \endcode
1264 * - Assign the result of the function psa_mac_operation_init()
1265 * to the structure, for example:
1266 * \code
1267 * psa_mac_operation_t operation;
1268 * operation = psa_mac_operation_init();
1269 * \endcode
1270 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001271 * This is an implementation-defined \c struct. Applications should not
1272 * make any assumptions about the content of this structure except
1273 * as directed by the documentation of a specific implementation. */
1274typedef struct psa_mac_operation_s psa_mac_operation_t;
Antonio de Angelis14276e92018-07-10 14:35:43 +01001275
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001276/** \def PSA_MAC_OPERATION_INIT
1277 *
1278 * This macro returns a suitable initializer for a MAC operation object of type
1279 * #psa_mac_operation_t.
1280 */
1281#ifdef __DOXYGEN_ONLY__
1282/* This is an example definition for documentation purposes.
1283 * Implementations should define a suitable value in `crypto_struct.h`.
1284 */
1285#define PSA_MAC_OPERATION_INIT {0}
1286#endif
1287
1288/** Return an initial value for a MAC operation object.
1289 */
1290static psa_mac_operation_t psa_mac_operation_init(void);
1291
1292/** Set up a multipart MAC calculation operation.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001293 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001294 * This function sets up the calculation of the MAC
1295 * (message authentication code) of a byte string.
1296 * To verify the MAC of a message against an
1297 * expected value, use psa_mac_verify_setup() instead.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001298 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001299 * The sequence of operations to calculate a MAC is as follows:
Antonio de Angelis14276e92018-07-10 14:35:43 +01001300 * -# Allocate an operation object which will be passed to all the functions
1301 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001302 * -# Initialize the operation object with one of the methods described in the
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001303 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001304 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001305 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1306 * of the message each time. The MAC that is calculated is the MAC
1307 * of the concatenation of these messages in order.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001308 * -# At the end of the message, call psa_mac_sign_finish() to finish
1309 * calculating the MAC value and retrieve it.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001310 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001311 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1312 * operation will need to be reset by a call to psa_mac_abort(). The
1313 * application may call psa_mac_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001314 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001315 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001316 * After a successful call to psa_mac_sign_setup(), the application must
1317 * eventually terminate the operation through one of the following methods:
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001318 * - A successful call to psa_mac_sign_finish().
1319 * - A call to psa_mac_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001320 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001321 * \param[in,out] operation The operation object to set up. It must have
1322 * been initialized as per the documentation for
1323 * #psa_mac_operation_t and not yet in use.
Maulik Patel28659c42021-01-06 14:09:22 +00001324 * \param key Identifier of the key to use for the operation. It
1325 * must remain valid until the operation terminates.
1326 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001327 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001328 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001329 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001330 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001331 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001332 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00001333 * \retval #PSA_ERROR_NOT_PERMITTED
1334 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00001335 * \p key is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001336 * \retval #PSA_ERROR_NOT_SUPPORTED
1337 * \p alg is not supported or is not a MAC algorithm.
1338 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1339 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1340 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001341 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1342 * \retval #PSA_ERROR_STORAGE_FAILURE
1343 * The key could not be retrieved from storage.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001344 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001345 * The operation state is not valid (it must be inactive).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001346 * \retval #PSA_ERROR_BAD_STATE
1347 * The library has not been previously initialized by psa_crypto_init().
1348 * It is implementation-dependent whether a failure to initialize
1349 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001350 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001351psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00001352 psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001353 psa_algorithm_t alg);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001354
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001355/** Set up a multipart MAC verification operation.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001356 *
1357 * This function sets up the verification of the MAC
1358 * (message authentication code) of a byte string against an expected value.
1359 *
1360 * The sequence of operations to verify a MAC is as follows:
1361 * -# Allocate an operation object which will be passed to all the functions
1362 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001363 * -# Initialize the operation object with one of the methods described in the
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001364 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001365 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001366 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1367 * of the message each time. The MAC that is calculated is the MAC
1368 * of the concatenation of these messages in order.
1369 * -# At the end of the message, call psa_mac_verify_finish() to finish
1370 * calculating the actual MAC of the message and verify it against
1371 * the expected value.
1372 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001373 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1374 * operation will need to be reset by a call to psa_mac_abort(). The
1375 * application may call psa_mac_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001376 * has been initialized.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001377 *
1378 * After a successful call to psa_mac_verify_setup(), the application must
1379 * eventually terminate the operation through one of the following methods:
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001380 * - A successful call to psa_mac_verify_finish().
1381 * - A call to psa_mac_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001382 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001383 * \param[in,out] operation The operation object to set up. It must have
1384 * been initialized as per the documentation for
1385 * #psa_mac_operation_t and not yet in use.
Maulik Patel28659c42021-01-06 14:09:22 +00001386 * \param key Identifier of the key to use for the operation. It
1387 * must remain valid until the operation terminates.
1388 * It must allow the usage
1389 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001390 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1391 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1392 *
1393 * \retval #PSA_SUCCESS
1394 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001395 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00001396 * \retval #PSA_ERROR_NOT_PERMITTED
1397 * \retval #PSA_ERROR_INVALID_ARGUMENT
1398 * \c key is not compatible with \c alg.
1399 * \retval #PSA_ERROR_NOT_SUPPORTED
1400 * \c alg is not supported or is not a MAC algorithm.
1401 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1402 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1403 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001404 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1405 * \retval #PSA_ERROR_STORAGE_FAILURE
1406 * The key could not be retrieved from storage
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001407 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001408 * The operation state is not valid (it must be inactive).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001409 * \retval #PSA_ERROR_BAD_STATE
1410 * The library has not been previously initialized by psa_crypto_init().
1411 * It is implementation-dependent whether a failure to initialize
1412 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001413 */
1414psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00001415 psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001416 psa_algorithm_t alg);
1417
1418/** Add a message fragment to a multipart MAC operation.
1419 *
1420 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1421 * before calling this function.
1422 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001423 * If this function returns an error status, the operation enters an error
1424 * state and must be aborted by calling psa_mac_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001425 *
1426 * \param[in,out] operation Active MAC operation.
1427 * \param[in] input Buffer containing the message fragment to add to
1428 * the MAC calculation.
1429 * \param input_length Size of the \p input buffer in bytes.
1430 *
1431 * \retval #PSA_SUCCESS
1432 * Success.
1433 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001434 * The operation state is not valid (it must be active).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001435 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1436 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1437 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001438 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1439 * \retval #PSA_ERROR_STORAGE_FAILURE
1440 * \retval #PSA_ERROR_BAD_STATE
1441 * The library has not been previously initialized by psa_crypto_init().
1442 * It is implementation-dependent whether a failure to initialize
1443 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001444 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001445psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1446 const uint8_t *input,
1447 size_t input_length);
1448
Antonio de Angelis377a1552018-11-22 17:02:40 +00001449/** Finish the calculation of the MAC of a message.
1450 *
1451 * The application must call psa_mac_sign_setup() before calling this function.
1452 * This function calculates the MAC of the message formed by concatenating
1453 * the inputs passed to preceding calls to psa_mac_update().
1454 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001455 * When this function returns successfuly, the operation becomes inactive.
1456 * If this function returns an error status, the operation enters an error
1457 * state and must be aborted by calling psa_mac_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001458 *
1459 * \warning Applications should not call this function if they expect
1460 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1461 * Beware that comparing integrity or authenticity data such as
1462 * MAC values with a function such as \c memcmp is risky
1463 * because the time taken by the comparison may leak information
1464 * about the MAC value which could allow an attacker to guess
1465 * a valid MAC and thereby bypass security controls.
1466 *
1467 * \param[in,out] operation Active MAC operation.
1468 * \param[out] mac Buffer where the MAC value is to be written.
1469 * \param mac_size Size of the \p mac buffer in bytes.
1470 * \param[out] mac_length On success, the number of bytes
1471 * that make up the MAC value. This is always
Maulik Patel13b27cf2021-05-14 11:44:53 +01001472 * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
Antonio de Angelis377a1552018-11-22 17:02:40 +00001473 * where \c key_type and \c key_bits are the type and
1474 * bit-size respectively of the key and \c alg is the
1475 * MAC algorithm that is calculated.
1476 *
1477 * \retval #PSA_SUCCESS
1478 * Success.
1479 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001480 * The operation state is not valid (it must be an active mac sign
1481 * operation).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001482 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1483 * The size of the \p mac buffer is too small. You can determine a
Maulik Patel13b27cf2021-05-14 11:44:53 +01001484 * sufficient buffer size by calling PSA_MAC_LENGTH().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001485 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1486 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1487 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001488 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1489 * \retval #PSA_ERROR_STORAGE_FAILURE
1490 * \retval #PSA_ERROR_BAD_STATE
1491 * The library has not been previously initialized by psa_crypto_init().
1492 * It is implementation-dependent whether a failure to initialize
1493 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001494 */
1495psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1496 uint8_t *mac,
1497 size_t mac_size,
1498 size_t *mac_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001499
Antonio de Angelis377a1552018-11-22 17:02:40 +00001500/** Finish the calculation of the MAC of a message and compare it with
1501 * an expected value.
1502 *
1503 * The application must call psa_mac_verify_setup() before calling this function.
1504 * This function calculates the MAC of the message formed by concatenating
1505 * the inputs passed to preceding calls to psa_mac_update(). It then
1506 * compares the calculated MAC with the expected MAC passed as a
1507 * parameter to this function.
1508 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001509 * When this function returns successfuly, the operation becomes inactive.
1510 * If this function returns an error status, the operation enters an error
1511 * state and must be aborted by calling psa_mac_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001512 *
1513 * \note Implementations shall make the best effort to ensure that the
1514 * comparison between the actual MAC and the expected MAC is performed
1515 * in constant time.
1516 *
1517 * \param[in,out] operation Active MAC operation.
1518 * \param[in] mac Buffer containing the expected MAC value.
1519 * \param mac_length Size of the \p mac buffer in bytes.
1520 *
1521 * \retval #PSA_SUCCESS
1522 * The expected MAC is identical to the actual MAC of the message.
1523 * \retval #PSA_ERROR_INVALID_SIGNATURE
1524 * The MAC of the message was calculated successfully, but it
1525 * differs from the expected MAC.
1526 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001527 * The operation state is not valid (it must be an active mac verify
1528 * operation).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001529 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1530 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1531 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001532 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1533 * \retval #PSA_ERROR_STORAGE_FAILURE
1534 * \retval #PSA_ERROR_BAD_STATE
1535 * The library has not been previously initialized by psa_crypto_init().
1536 * It is implementation-dependent whether a failure to initialize
1537 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001538 */
1539psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1540 const uint8_t *mac,
1541 size_t mac_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001542
Antonio de Angelis377a1552018-11-22 17:02:40 +00001543/** Abort a MAC operation.
1544 *
1545 * Aborting an operation frees all associated resources except for the
1546 * \p operation structure itself. Once aborted, the operation object
1547 * can be reused for another operation by calling
1548 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
1549 *
1550 * You may call this function any time after the operation object has
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001551 * been initialized by one of the methods described in #psa_mac_operation_t.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001552 *
1553 * In particular, calling psa_mac_abort() after the operation has been
1554 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1555 * psa_mac_verify_finish() is safe and has no effect.
1556 *
1557 * \param[in,out] operation Initialized MAC operation.
1558 *
1559 * \retval #PSA_SUCCESS
Antonio de Angelis377a1552018-11-22 17:02:40 +00001560 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1561 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001562 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1563 * \retval #PSA_ERROR_BAD_STATE
1564 * The library has not been previously initialized by psa_crypto_init().
1565 * It is implementation-dependent whether a failure to initialize
1566 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001567 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001568psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1569
1570/**@}*/
1571
1572/** \defgroup cipher Symmetric ciphers
1573 * @{
1574 */
1575
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001576/** Encrypt a message using a symmetric cipher.
1577 *
1578 * This function encrypts a message with a random IV (initialization
1579 * vector). Use the multipart operation interface with a
1580 * #psa_cipher_operation_t object to provide other forms of IV.
1581 *
Maulik Patel28659c42021-01-06 14:09:22 +00001582 * \param key Identifier of the key to use for the operation.
1583 * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001584 * \param alg The cipher algorithm to compute
1585 * (\c PSA_ALG_XXX value such that
1586 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1587 * \param[in] input Buffer containing the message to encrypt.
1588 * \param input_length Size of the \p input buffer in bytes.
1589 * \param[out] output Buffer where the output is to be written.
1590 * The output contains the IV followed by
1591 * the ciphertext proper.
1592 * \param output_size Size of the \p output buffer in bytes.
1593 * \param[out] output_length On success, the number of bytes
1594 * that make up the output.
1595 *
1596 * \retval #PSA_SUCCESS
1597 * Success.
1598 * \retval #PSA_ERROR_INVALID_HANDLE
1599 * \retval #PSA_ERROR_NOT_PERMITTED
1600 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00001601 * \p key is not compatible with \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001602 * \retval #PSA_ERROR_NOT_SUPPORTED
1603 * \p alg is not supported or is not a cipher algorithm.
1604 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1605 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1606 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1607 * \retval #PSA_ERROR_HARDWARE_FAILURE
1608 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1609 * \retval #PSA_ERROR_STORAGE_FAILURE
1610 * \retval #PSA_ERROR_BAD_STATE
1611 * The library has not been previously initialized by psa_crypto_init().
1612 * It is implementation-dependent whether a failure to initialize
1613 * results in this error code.
1614 */
Maulik Patel28659c42021-01-06 14:09:22 +00001615psa_status_t psa_cipher_encrypt(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001616 psa_algorithm_t alg,
1617 const uint8_t *input,
1618 size_t input_length,
1619 uint8_t *output,
1620 size_t output_size,
1621 size_t *output_length);
1622
1623/** Decrypt a message using a symmetric cipher.
1624 *
1625 * This function decrypts a message encrypted with a symmetric cipher.
1626 *
Maulik Patel28659c42021-01-06 14:09:22 +00001627 * \param key Identifier of the key to use for the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001628 * It must remain valid until the operation
Maulik Patel28659c42021-01-06 14:09:22 +00001629 * terminates. It must allow the usage
1630 * #PSA_KEY_USAGE_DECRYPT.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001631 * \param alg The cipher algorithm to compute
1632 * (\c PSA_ALG_XXX value such that
1633 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1634 * \param[in] input Buffer containing the message to decrypt.
1635 * This consists of the IV followed by the
1636 * ciphertext proper.
1637 * \param input_length Size of the \p input buffer in bytes.
1638 * \param[out] output Buffer where the plaintext is to be written.
1639 * \param output_size Size of the \p output buffer in bytes.
1640 * \param[out] output_length On success, the number of bytes
1641 * that make up the output.
1642 *
1643 * \retval #PSA_SUCCESS
1644 * Success.
1645 * \retval #PSA_ERROR_INVALID_HANDLE
1646 * \retval #PSA_ERROR_NOT_PERMITTED
1647 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00001648 * \p key is not compatible with \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001649 * \retval #PSA_ERROR_NOT_SUPPORTED
1650 * \p alg is not supported or is not a cipher algorithm.
1651 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1652 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1653 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1654 * \retval #PSA_ERROR_HARDWARE_FAILURE
1655 * \retval #PSA_ERROR_STORAGE_FAILURE
1656 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1657 * \retval #PSA_ERROR_BAD_STATE
1658 * The library has not been previously initialized by psa_crypto_init().
1659 * It is implementation-dependent whether a failure to initialize
1660 * results in this error code.
1661 */
Maulik Patel28659c42021-01-06 14:09:22 +00001662psa_status_t psa_cipher_decrypt(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001663 psa_algorithm_t alg,
1664 const uint8_t *input,
1665 size_t input_length,
1666 uint8_t *output,
1667 size_t output_size,
1668 size_t *output_length);
1669
Antonio de Angelis377a1552018-11-22 17:02:40 +00001670/** The type of the state data structure for multipart cipher operations.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001671 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001672 * Before calling any function on a cipher operation object, the application
1673 * must initialize it by any of the following means:
1674 * - Set the structure to all-bits-zero, for example:
1675 * \code
1676 * psa_cipher_operation_t operation;
1677 * memset(&operation, 0, sizeof(operation));
1678 * \endcode
1679 * - Initialize the structure to logical zero values, for example:
1680 * \code
1681 * psa_cipher_operation_t operation = {0};
1682 * \endcode
1683 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1684 * for example:
1685 * \code
1686 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1687 * \endcode
1688 * - Assign the result of the function psa_cipher_operation_init()
1689 * to the structure, for example:
1690 * \code
1691 * psa_cipher_operation_t operation;
1692 * operation = psa_cipher_operation_init();
1693 * \endcode
1694 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001695 * This is an implementation-defined \c struct. Applications should not
1696 * make any assumptions about the content of this structure except
1697 * as directed by the documentation of a specific implementation. */
1698typedef struct psa_cipher_operation_s psa_cipher_operation_t;
Antonio de Angelis14276e92018-07-10 14:35:43 +01001699
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001700/** \def PSA_CIPHER_OPERATION_INIT
1701 *
1702 * This macro returns a suitable initializer for a cipher operation object of
1703 * type #psa_cipher_operation_t.
1704 */
1705#ifdef __DOXYGEN_ONLY__
1706/* This is an example definition for documentation purposes.
1707 * Implementations should define a suitable value in `crypto_struct.h`.
1708 */
1709#define PSA_CIPHER_OPERATION_INIT {0}
1710#endif
1711
1712/** Return an initial value for a cipher operation object.
1713 */
1714static psa_cipher_operation_t psa_cipher_operation_init(void);
1715
Antonio de Angelis14276e92018-07-10 14:35:43 +01001716/** Set the key for a multipart symmetric encryption operation.
1717 *
1718 * The sequence of operations to encrypt a message with a symmetric cipher
1719 * is as follows:
1720 * -# Allocate an operation object which will be passed to all the functions
1721 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001722 * -# Initialize the operation object with one of the methods described in the
1723 * documentation for #psa_cipher_operation_t, e.g.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001724 * #PSA_CIPHER_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001725 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001726 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Antonio de Angelis14276e92018-07-10 14:35:43 +01001727 * generate or set the IV (initialization vector). You should use
Antonio de Angelis377a1552018-11-22 17:02:40 +00001728 * psa_cipher_generate_iv() unless the protocol you are implementing
Antonio de Angelis14276e92018-07-10 14:35:43 +01001729 * requires a specific IV value.
1730 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1731 * of the message each time.
1732 * -# Call psa_cipher_finish().
1733 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001734 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1735 * the operation will need to be reset by a call to psa_cipher_abort(). The
1736 * application may call psa_cipher_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001737 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001738 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001739 * After a successful call to psa_cipher_encrypt_setup(), the application must
Antonio de Angelis14276e92018-07-10 14:35:43 +01001740 * eventually terminate the operation. The following events terminate an
1741 * operation:
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001742 * - A successful call to psa_cipher_finish().
1743 * - A call to psa_cipher_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001744 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001745 * \param[in,out] operation The operation object to set up. It must have
1746 * been initialized as per the documentation for
1747 * #psa_cipher_operation_t and not yet in use.
Maulik Patel28659c42021-01-06 14:09:22 +00001748 * \param key Identifier of the key to use for the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001749 * It must remain valid until the operation
Maulik Patel28659c42021-01-06 14:09:22 +00001750 * terminates. It must allow the usage
1751 * #PSA_KEY_USAGE_ENCRYPT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001752 * \param alg The cipher algorithm to compute
1753 * (\c PSA_ALG_XXX value such that
1754 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001755 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001756 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001757 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001758 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00001759 * \retval #PSA_ERROR_NOT_PERMITTED
1760 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00001761 * \p key is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001762 * \retval #PSA_ERROR_NOT_SUPPORTED
1763 * \p alg is not supported or is not a cipher algorithm.
1764 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1765 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1766 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001767 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1768 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001769 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001770 * The operation state is not valid (it must be inactive).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001771 * \retval #PSA_ERROR_BAD_STATE
1772 * The library has not been previously initialized by psa_crypto_init().
1773 * It is implementation-dependent whether a failure to initialize
1774 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001775 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001776psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00001777 psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001778 psa_algorithm_t alg);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001779
1780/** Set the key for a multipart symmetric decryption operation.
1781 *
1782 * The sequence of operations to decrypt a message with a symmetric cipher
1783 * is as follows:
1784 * -# Allocate an operation object which will be passed to all the functions
1785 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001786 * -# Initialize the operation object with one of the methods described in the
1787 * documentation for #psa_cipher_operation_t, e.g.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001788 * #PSA_CIPHER_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001789 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001790 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Antonio de Angelis14276e92018-07-10 14:35:43 +01001791 * decryption. If the IV is prepended to the ciphertext, you can call
1792 * psa_cipher_update() on a buffer containing the IV followed by the
1793 * beginning of the message.
1794 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1795 * of the message each time.
1796 * -# Call psa_cipher_finish().
1797 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001798 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1799 * the operation will need to be reset by a call to psa_cipher_abort(). The
1800 * application may call psa_cipher_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001801 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001802 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001803 * After a successful call to psa_cipher_decrypt_setup(), the application must
Antonio de Angelis14276e92018-07-10 14:35:43 +01001804 * eventually terminate the operation. The following events terminate an
1805 * operation:
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001806 * - A successful call to psa_cipher_finish().
1807 * - A call to psa_cipher_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001808 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001809 * \param[in,out] operation The operation object to set up. It must have
1810 * been initialized as per the documentation for
1811 * #psa_cipher_operation_t and not yet in use.
Maulik Patel28659c42021-01-06 14:09:22 +00001812 * \param key Identifier of the key to use for the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001813 * It must remain valid until the operation
Maulik Patel28659c42021-01-06 14:09:22 +00001814 * terminates. It must allow the usage
1815 * #PSA_KEY_USAGE_DECRYPT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001816 * \param alg The cipher algorithm to compute
1817 * (\c PSA_ALG_XXX value such that
1818 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001819 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001820 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001821 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001822 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00001823 * \retval #PSA_ERROR_NOT_PERMITTED
1824 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00001825 * \p key is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001826 * \retval #PSA_ERROR_NOT_SUPPORTED
1827 * \p alg is not supported or is not a cipher algorithm.
1828 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1829 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1830 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001831 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1832 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001833 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001834 * The operation state is not valid (it must be inactive).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001835 * \retval #PSA_ERROR_BAD_STATE
1836 * The library has not been previously initialized by psa_crypto_init().
1837 * It is implementation-dependent whether a failure to initialize
1838 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001839 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001840psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00001841 psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001842 psa_algorithm_t alg);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001843
Antonio de Angelis377a1552018-11-22 17:02:40 +00001844/** Generate an IV for a symmetric encryption operation.
1845 *
1846 * This function generates a random IV (initialization vector), nonce
1847 * or initial counter value for the encryption operation as appropriate
1848 * for the chosen algorithm, key type and key size.
1849 *
1850 * The application must call psa_cipher_encrypt_setup() before
1851 * calling this function.
1852 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001853 * If this function returns an error status, the operation enters an error
1854 * state and must be aborted by calling psa_cipher_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001855 *
1856 * \param[in,out] operation Active cipher operation.
1857 * \param[out] iv Buffer where the generated IV is to be written.
1858 * \param iv_size Size of the \p iv buffer in bytes.
1859 * \param[out] iv_length On success, the number of bytes of the
1860 * generated IV.
1861 *
1862 * \retval #PSA_SUCCESS
1863 * Success.
1864 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001865 * The operation state is not valid (it must be active, with no IV set).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001866 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1867 * The size of the \p iv buffer is too small.
1868 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1869 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1870 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001871 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1872 * \retval #PSA_ERROR_STORAGE_FAILURE
1873 * \retval #PSA_ERROR_BAD_STATE
1874 * The library has not been previously initialized by psa_crypto_init().
1875 * It is implementation-dependent whether a failure to initialize
1876 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001877 */
1878psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001879 uint8_t *iv,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001880 size_t iv_size,
1881 size_t *iv_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001882
Antonio de Angelis377a1552018-11-22 17:02:40 +00001883/** Set the IV for a symmetric encryption or decryption operation.
1884 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001885 * This function sets the IV (initialization vector), nonce
Antonio de Angelis377a1552018-11-22 17:02:40 +00001886 * or initial counter value for the encryption or decryption operation.
1887 *
1888 * The application must call psa_cipher_encrypt_setup() before
1889 * calling this function.
1890 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001891 * If this function returns an error status, the operation enters an error
1892 * state and must be aborted by calling psa_cipher_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001893 *
1894 * \note When encrypting, applications should use psa_cipher_generate_iv()
1895 * instead of this function, unless implementing a protocol that requires
1896 * a non-random IV.
1897 *
1898 * \param[in,out] operation Active cipher operation.
1899 * \param[in] iv Buffer containing the IV to use.
1900 * \param iv_length Size of the IV in bytes.
1901 *
1902 * \retval #PSA_SUCCESS
1903 * Success.
1904 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001905 * The operation state is not valid (it must be an active cipher
1906 * encrypt operation, with no IV set).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001907 * \retval #PSA_ERROR_INVALID_ARGUMENT
1908 * The size of \p iv is not acceptable for the chosen algorithm,
1909 * or the chosen algorithm does not use an IV.
1910 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1911 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1912 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001913 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1914 * \retval #PSA_ERROR_STORAGE_FAILURE
1915 * \retval #PSA_ERROR_BAD_STATE
1916 * The library has not been previously initialized by psa_crypto_init().
1917 * It is implementation-dependent whether a failure to initialize
1918 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001919 */
1920psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001921 const uint8_t *iv,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001922 size_t iv_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001923
Antonio de Angelis377a1552018-11-22 17:02:40 +00001924/** Encrypt or decrypt a message fragment in an active cipher operation.
1925 *
1926 * Before calling this function, you must:
1927 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1928 * The choice of setup function determines whether this function
1929 * encrypts or decrypts its input.
1930 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1931 * (recommended when encrypting) or psa_cipher_set_iv().
1932 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001933 * If this function returns an error status, the operation enters an error
1934 * state and must be aborted by calling psa_cipher_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001935 *
1936 * \param[in,out] operation Active cipher operation.
1937 * \param[in] input Buffer containing the message fragment to
1938 * encrypt or decrypt.
1939 * \param input_length Size of the \p input buffer in bytes.
1940 * \param[out] output Buffer where the output is to be written.
1941 * \param output_size Size of the \p output buffer in bytes.
1942 * \param[out] output_length On success, the number of bytes
1943 * that make up the returned output.
1944 *
1945 * \retval #PSA_SUCCESS
1946 * Success.
1947 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001948 * The operation state is not valid (it must be active, with an IV set
1949 * if required for the algorithm).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001950 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1951 * The size of the \p output buffer is too small.
1952 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1953 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1954 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001955 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1956 * \retval #PSA_ERROR_STORAGE_FAILURE
1957 * \retval #PSA_ERROR_BAD_STATE
1958 * The library has not been previously initialized by psa_crypto_init().
1959 * It is implementation-dependent whether a failure to initialize
1960 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001961 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001962psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1963 const uint8_t *input,
1964 size_t input_length,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001965 uint8_t *output,
Antonio de Angelis14276e92018-07-10 14:35:43 +01001966 size_t output_size,
1967 size_t *output_length);
1968
Antonio de Angelis377a1552018-11-22 17:02:40 +00001969/** Finish encrypting or decrypting a message in a cipher operation.
1970 *
1971 * The application must call psa_cipher_encrypt_setup() or
1972 * psa_cipher_decrypt_setup() before calling this function. The choice
1973 * of setup function determines whether this function encrypts or
1974 * decrypts its input.
1975 *
1976 * This function finishes the encryption or decryption of the message
1977 * formed by concatenating the inputs passed to preceding calls to
1978 * psa_cipher_update().
1979 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001980 * When this function returns successfuly, the operation becomes inactive.
1981 * If this function returns an error status, the operation enters an error
1982 * state and must be aborted by calling psa_cipher_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001983 *
1984 * \param[in,out] operation Active cipher operation.
1985 * \param[out] output Buffer where the output is to be written.
1986 * \param output_size Size of the \p output buffer in bytes.
1987 * \param[out] output_length On success, the number of bytes
1988 * that make up the returned output.
1989 *
1990 * \retval #PSA_SUCCESS
1991 * Success.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001992 * \retval #PSA_ERROR_INVALID_ARGUMENT
1993 * The total input size passed to this operation is not valid for
1994 * this particular algorithm. For example, the algorithm is a based
1995 * on block cipher and requires a whole number of blocks, but the
1996 * total input size is not a multiple of the block size.
1997 * \retval #PSA_ERROR_INVALID_PADDING
1998 * This is a decryption operation for an algorithm that includes
1999 * padding, and the ciphertext does not contain valid padding.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002000 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002001 * The operation state is not valid (it must be active, with an IV set
2002 * if required for the algorithm).
Antonio de Angelis377a1552018-11-22 17:02:40 +00002003 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2004 * The size of the \p output buffer is too small.
2005 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2006 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2007 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002008 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2009 * \retval #PSA_ERROR_STORAGE_FAILURE
2010 * \retval #PSA_ERROR_BAD_STATE
2011 * The library has not been previously initialized by psa_crypto_init().
2012 * It is implementation-dependent whether a failure to initialize
2013 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002014 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01002015psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
2016 uint8_t *output,
2017 size_t output_size,
2018 size_t *output_length);
2019
Antonio de Angelis377a1552018-11-22 17:02:40 +00002020/** Abort a cipher operation.
2021 *
2022 * Aborting an operation frees all associated resources except for the
2023 * \p operation structure itself. Once aborted, the operation object
2024 * can be reused for another operation by calling
2025 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
2026 *
2027 * You may call this function any time after the operation object has
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002028 * been initialized as described in #psa_cipher_operation_t.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002029 *
2030 * In particular, calling psa_cipher_abort() after the operation has been
2031 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2032 * is safe and has no effect.
2033 *
2034 * \param[in,out] operation Initialized cipher operation.
2035 *
2036 * \retval #PSA_SUCCESS
Antonio de Angelis377a1552018-11-22 17:02:40 +00002037 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2038 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002039 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2040 * \retval #PSA_ERROR_BAD_STATE
2041 * The library has not been previously initialized by psa_crypto_init().
2042 * It is implementation-dependent whether a failure to initialize
2043 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002044 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01002045psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2046
2047/**@}*/
2048
2049/** \defgroup aead Authenticated encryption with associated data (AEAD)
2050 * @{
2051 */
2052
Antonio de Angelis14276e92018-07-10 14:35:43 +01002053/** Process an authenticated encryption operation.
2054 *
Maulik Patel28659c42021-01-06 14:09:22 +00002055 * \param key Identifier of the key to use for the
2056 * operation. It must allow the usage
2057 * #PSA_KEY_USAGE_ENCRYPT.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002058 * \param alg The AEAD algorithm to compute
2059 * (\c PSA_ALG_XXX value such that
Antonio de Angelis377a1552018-11-22 17:02:40 +00002060 * #PSA_ALG_IS_AEAD(\p alg) is true).
2061 * \param[in] nonce Nonce or IV to use.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002062 * \param nonce_length Size of the \p nonce buffer in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002063 * \param[in] additional_data Additional data that will be authenticated
Antonio de Angelis14276e92018-07-10 14:35:43 +01002064 * but not encrypted.
2065 * \param additional_data_length Size of \p additional_data in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002066 * \param[in] plaintext Data that will be authenticated and
Antonio de Angelis14276e92018-07-10 14:35:43 +01002067 * encrypted.
2068 * \param plaintext_length Size of \p plaintext in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002069 * \param[out] ciphertext Output buffer for the authenticated and
Antonio de Angelis14276e92018-07-10 14:35:43 +01002070 * encrypted data. The additional data is not
2071 * part of this output. For algorithms where the
2072 * encrypted data and the authentication tag
2073 * are defined as separate outputs, the
2074 * authentication tag is appended to the
2075 * encrypted data.
2076 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2077 * This must be at least
2078 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2079 * \p plaintext_length).
Antonio de Angelis377a1552018-11-22 17:02:40 +00002080 * \param[out] ciphertext_length On success, the size of the output
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002081 * in the \p ciphertext buffer.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002082 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002083 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01002084 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002085 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00002086 * \retval #PSA_ERROR_NOT_PERMITTED
2087 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00002088 * \p key is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002089 * \retval #PSA_ERROR_NOT_SUPPORTED
2090 * \p alg is not supported or is not an AEAD algorithm.
2091 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002092 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2093 * \p ciphertext_size is too small
Antonio de Angelis377a1552018-11-22 17:02:40 +00002094 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2095 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002096 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2097 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002098 * \retval #PSA_ERROR_BAD_STATE
2099 * The library has not been previously initialized by psa_crypto_init().
2100 * It is implementation-dependent whether a failure to initialize
2101 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002102 */
Maulik Patel28659c42021-01-06 14:09:22 +00002103psa_status_t psa_aead_encrypt(psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +00002104 psa_algorithm_t alg,
2105 const uint8_t *nonce,
2106 size_t nonce_length,
2107 const uint8_t *additional_data,
2108 size_t additional_data_length,
2109 const uint8_t *plaintext,
2110 size_t plaintext_length,
2111 uint8_t *ciphertext,
2112 size_t ciphertext_size,
2113 size_t *ciphertext_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002114
2115/** Process an authenticated decryption operation.
2116 *
Maulik Patel28659c42021-01-06 14:09:22 +00002117 * \param key Identifier of the key to use for the
2118 * operation. It must allow the usage
2119 * #PSA_KEY_USAGE_DECRYPT.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002120 * \param alg The AEAD algorithm to compute
2121 * (\c PSA_ALG_XXX value such that
Antonio de Angelis377a1552018-11-22 17:02:40 +00002122 * #PSA_ALG_IS_AEAD(\p alg) is true).
2123 * \param[in] nonce Nonce or IV to use.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002124 * \param nonce_length Size of the \p nonce buffer in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002125 * \param[in] additional_data Additional data that has been authenticated
Antonio de Angelis14276e92018-07-10 14:35:43 +01002126 * but not encrypted.
2127 * \param additional_data_length Size of \p additional_data in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002128 * \param[in] ciphertext Data that has been authenticated and
Antonio de Angelis14276e92018-07-10 14:35:43 +01002129 * encrypted. For algorithms where the
2130 * encrypted data and the authentication tag
2131 * are defined as separate inputs, the buffer
2132 * must contain the encrypted data followed
2133 * by the authentication tag.
2134 * \param ciphertext_length Size of \p ciphertext in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002135 * \param[out] plaintext Output buffer for the decrypted data.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002136 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2137 * This must be at least
2138 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2139 * \p ciphertext_length).
Antonio de Angelis377a1552018-11-22 17:02:40 +00002140 * \param[out] plaintext_length On success, the size of the output
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002141 * in the \p plaintext buffer.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002142 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002143 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01002144 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002145 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00002146 * \retval #PSA_ERROR_INVALID_SIGNATURE
Antonio de Angelis14276e92018-07-10 14:35:43 +01002147 * The ciphertext is not authentic.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002148 * \retval #PSA_ERROR_NOT_PERMITTED
2149 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00002150 * \p key is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002151 * \retval #PSA_ERROR_NOT_SUPPORTED
2152 * \p alg is not supported or is not an AEAD algorithm.
2153 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002154 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2155 * \p plaintext_size or \p nonce_length is too small
Antonio de Angelis377a1552018-11-22 17:02:40 +00002156 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2157 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002158 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2159 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002160 * \retval #PSA_ERROR_BAD_STATE
2161 * The library has not been previously initialized by psa_crypto_init().
2162 * It is implementation-dependent whether a failure to initialize
2163 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002164 */
Maulik Patel28659c42021-01-06 14:09:22 +00002165psa_status_t psa_aead_decrypt(psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +00002166 psa_algorithm_t alg,
2167 const uint8_t *nonce,
2168 size_t nonce_length,
2169 const uint8_t *additional_data,
2170 size_t additional_data_length,
2171 const uint8_t *ciphertext,
2172 size_t ciphertext_length,
2173 uint8_t *plaintext,
2174 size_t plaintext_size,
2175 size_t *plaintext_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002176
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002177/** The type of the state data structure for multipart AEAD operations.
2178 *
2179 * Before calling any function on an AEAD operation object, the application
2180 * must initialize it by any of the following means:
2181 * - Set the structure to all-bits-zero, for example:
2182 * \code
2183 * psa_aead_operation_t operation;
2184 * memset(&operation, 0, sizeof(operation));
2185 * \endcode
2186 * - Initialize the structure to logical zero values, for example:
2187 * \code
2188 * psa_aead_operation_t operation = {0};
2189 * \endcode
2190 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2191 * for example:
2192 * \code
2193 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2194 * \endcode
2195 * - Assign the result of the function psa_aead_operation_init()
2196 * to the structure, for example:
2197 * \code
2198 * psa_aead_operation_t operation;
2199 * operation = psa_aead_operation_init();
2200 * \endcode
2201 *
2202 * This is an implementation-defined \c struct. Applications should not
2203 * make any assumptions about the content of this structure except
2204 * as directed by the documentation of a specific implementation. */
2205typedef struct psa_aead_operation_s psa_aead_operation_t;
2206
2207/** \def PSA_AEAD_OPERATION_INIT
2208 *
2209 * This macro returns a suitable initializer for an AEAD operation object of
2210 * type #psa_aead_operation_t.
2211 */
2212#ifdef __DOXYGEN_ONLY__
2213/* This is an example definition for documentation purposes.
2214 * Implementations should define a suitable value in `crypto_struct.h`.
2215 */
2216#define PSA_AEAD_OPERATION_INIT {0}
2217#endif
2218
2219/** Return an initial value for an AEAD operation object.
2220 */
2221static psa_aead_operation_t psa_aead_operation_init(void);
2222
2223/** Set the key for a multipart authenticated encryption operation.
2224 *
2225 * The sequence of operations to encrypt a message with authentication
2226 * is as follows:
2227 * -# Allocate an operation object which will be passed to all the functions
2228 * listed here.
2229 * -# Initialize the operation object with one of the methods described in the
2230 * documentation for #psa_aead_operation_t, e.g.
2231 * #PSA_AEAD_OPERATION_INIT.
2232 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
2233 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2234 * inputs to the subsequent calls to psa_aead_update_ad() and
2235 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2236 * for details.
2237 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2238 * generate or set the nonce. You should use
2239 * psa_aead_generate_nonce() unless the protocol you are implementing
2240 * requires a specific nonce value.
2241 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2242 * of the non-encrypted additional authenticated data each time.
2243 * -# Call psa_aead_update() zero, one or more times, passing a fragment
2244 * of the message to encrypt each time.
2245 * -# Call psa_aead_finish().
2246 *
2247 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2248 * the operation will need to be reset by a call to psa_aead_abort(). The
2249 * application may call psa_aead_abort() at any time after the operation
2250 * has been initialized.
2251 *
2252 * After a successful call to psa_aead_encrypt_setup(), the application must
2253 * eventually terminate the operation. The following events terminate an
2254 * operation:
2255 * - A successful call to psa_aead_finish().
2256 * - A call to psa_aead_abort().
2257 *
2258 * \param[in,out] operation The operation object to set up. It must have
2259 * been initialized as per the documentation for
2260 * #psa_aead_operation_t and not yet in use.
Maulik Patel28659c42021-01-06 14:09:22 +00002261 * \param key Identifier of the key to use for the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002262 * It must remain valid until the operation
Maulik Patel28659c42021-01-06 14:09:22 +00002263 * terminates. It must allow the usage
2264 * #PSA_KEY_USAGE_ENCRYPT.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002265 * \param alg The AEAD algorithm to compute
2266 * (\c PSA_ALG_XXX value such that
2267 * #PSA_ALG_IS_AEAD(\p alg) is true).
2268 *
2269 * \retval #PSA_SUCCESS
2270 * Success.
2271 * \retval #PSA_ERROR_BAD_STATE
2272 * The operation state is not valid (it must be inactive).
Maulik Patel28659c42021-01-06 14:09:22 +00002273 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002274 * \retval #PSA_ERROR_NOT_PERMITTED
2275 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00002276 * \p key is not compatible with \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002277 * \retval #PSA_ERROR_NOT_SUPPORTED
2278 * \p alg is not supported or is not an AEAD algorithm.
2279 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2280 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2281 * \retval #PSA_ERROR_HARDWARE_FAILURE
2282 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2283 * \retval #PSA_ERROR_STORAGE_FAILURE
2284 * \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.
2288 */
2289psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00002290 psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002291 psa_algorithm_t alg);
2292
2293/** Set the key for a multipart authenticated decryption operation.
2294 *
2295 * The sequence of operations to decrypt a message with authentication
2296 * is as follows:
2297 * -# Allocate an operation object which will be passed to all the functions
2298 * listed here.
2299 * -# Initialize the operation object with one of the methods described in the
2300 * documentation for #psa_aead_operation_t, e.g.
2301 * #PSA_AEAD_OPERATION_INIT.
2302 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
2303 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2304 * inputs to the subsequent calls to psa_aead_update_ad() and
2305 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2306 * for details.
2307 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2308 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2309 * of the non-encrypted additional authenticated data each time.
2310 * -# Call psa_aead_update() zero, one or more times, passing a fragment
2311 * of the ciphertext to decrypt each time.
2312 * -# Call psa_aead_verify().
2313 *
2314 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2315 * the operation will need to be reset by a call to psa_aead_abort(). The
2316 * application may call psa_aead_abort() at any time after the operation
2317 * has been initialized.
2318 *
2319 * After a successful call to psa_aead_decrypt_setup(), the application must
2320 * eventually terminate the operation. The following events terminate an
2321 * operation:
2322 * - A successful call to psa_aead_verify().
2323 * - A call to psa_aead_abort().
2324 *
2325 * \param[in,out] operation The operation object to set up. It must have
2326 * been initialized as per the documentation for
2327 * #psa_aead_operation_t and not yet in use.
Maulik Patel28659c42021-01-06 14:09:22 +00002328 * \param key Identifier of the key to use for the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002329 * It must remain valid until the operation
Maulik Patel28659c42021-01-06 14:09:22 +00002330 * terminates. It must allow the usage
2331 * #PSA_KEY_USAGE_DECRYPT.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002332 * \param alg The AEAD algorithm to compute
2333 * (\c PSA_ALG_XXX value such that
2334 * #PSA_ALG_IS_AEAD(\p alg) is true).
2335 *
2336 * \retval #PSA_SUCCESS
2337 * Success.
2338 * \retval #PSA_ERROR_BAD_STATE
2339 * The operation state is not valid (it must be inactive).
Maulik Patel28659c42021-01-06 14:09:22 +00002340 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002341 * \retval #PSA_ERROR_NOT_PERMITTED
2342 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00002343 * \p key is not compatible with \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002344 * \retval #PSA_ERROR_NOT_SUPPORTED
2345 * \p alg is not supported or is not an AEAD algorithm.
2346 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2347 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2348 * \retval #PSA_ERROR_HARDWARE_FAILURE
2349 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2350 * \retval #PSA_ERROR_STORAGE_FAILURE
2351 * \retval #PSA_ERROR_BAD_STATE
2352 * The library has not been previously initialized by psa_crypto_init().
2353 * It is implementation-dependent whether a failure to initialize
2354 * results in this error code.
2355 */
2356psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00002357 psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002358 psa_algorithm_t alg);
2359
2360/** Generate a random nonce for an authenticated encryption operation.
2361 *
2362 * This function generates a random nonce for the authenticated encryption
2363 * operation with an appropriate size for the chosen algorithm, key type
2364 * and key size.
2365 *
2366 * The application must call psa_aead_encrypt_setup() before
2367 * calling this function.
2368 *
2369 * If this function returns an error status, the operation enters an error
2370 * state and must be aborted by calling psa_aead_abort().
2371 *
2372 * \param[in,out] operation Active AEAD operation.
2373 * \param[out] nonce Buffer where the generated nonce is to be
2374 * written.
2375 * \param nonce_size Size of the \p nonce buffer in bytes.
2376 * \param[out] nonce_length On success, the number of bytes of the
2377 * generated nonce.
2378 *
2379 * \retval #PSA_SUCCESS
2380 * Success.
2381 * \retval #PSA_ERROR_BAD_STATE
2382 * The operation state is not valid (it must be an active aead encrypt
Maulik Patel28659c42021-01-06 14:09:22 +00002383 * operation, with no nonce set).
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002384 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2385 * The size of the \p nonce buffer is too small.
2386 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2387 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2388 * \retval #PSA_ERROR_HARDWARE_FAILURE
2389 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2390 * \retval #PSA_ERROR_STORAGE_FAILURE
2391 * \retval #PSA_ERROR_BAD_STATE
2392 * The library has not been previously initialized by psa_crypto_init().
2393 * It is implementation-dependent whether a failure to initialize
2394 * results in this error code.
2395 */
2396psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2397 uint8_t *nonce,
2398 size_t nonce_size,
2399 size_t *nonce_length);
2400
2401/** Set the nonce for an authenticated encryption or decryption operation.
2402 *
2403 * This function sets the nonce for the authenticated
2404 * encryption or decryption operation.
2405 *
2406 * The application must call psa_aead_encrypt_setup() or
2407 * psa_aead_decrypt_setup() before calling this function.
2408 *
2409 * If this function returns an error status, the operation enters an error
2410 * state and must be aborted by calling psa_aead_abort().
2411 *
2412 * \note When encrypting, applications should use psa_aead_generate_nonce()
2413 * instead of this function, unless implementing a protocol that requires
2414 * a non-random IV.
2415 *
2416 * \param[in,out] operation Active AEAD operation.
2417 * \param[in] nonce Buffer containing the nonce to use.
2418 * \param nonce_length Size of the nonce in bytes.
2419 *
2420 * \retval #PSA_SUCCESS
2421 * Success.
2422 * \retval #PSA_ERROR_BAD_STATE
2423 * The operation state is not valid (it must be active, with no nonce
2424 * set).
2425 * \retval #PSA_ERROR_INVALID_ARGUMENT
2426 * The size of \p nonce is not acceptable for the chosen algorithm.
2427 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2428 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2429 * \retval #PSA_ERROR_HARDWARE_FAILURE
2430 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2431 * \retval #PSA_ERROR_STORAGE_FAILURE
2432 * \retval #PSA_ERROR_BAD_STATE
2433 * The library has not been previously initialized by psa_crypto_init().
2434 * It is implementation-dependent whether a failure to initialize
2435 * results in this error code.
2436 */
2437psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2438 const uint8_t *nonce,
2439 size_t nonce_length);
2440
2441/** Declare the lengths of the message and additional data for AEAD.
2442 *
2443 * The application must call this function before calling
2444 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2445 * the operation requires it. If the algorithm does not require it,
2446 * calling this function is optional, but if this function is called
2447 * then the implementation must enforce the lengths.
2448 *
2449 * You may call this function before or after setting the nonce with
2450 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2451 *
2452 * - For #PSA_ALG_CCM, calling this function is required.
2453 * - For the other AEAD algorithms defined in this specification, calling
2454 * this function is not required.
2455 * - For vendor-defined algorithm, refer to the vendor documentation.
2456 *
2457 * If this function returns an error status, the operation enters an error
2458 * state and must be aborted by calling psa_aead_abort().
2459 *
2460 * \param[in,out] operation Active AEAD operation.
2461 * \param ad_length Size of the non-encrypted additional
2462 * authenticated data in bytes.
2463 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2464 *
2465 * \retval #PSA_SUCCESS
2466 * Success.
2467 * \retval #PSA_ERROR_BAD_STATE
2468 * The operation state is not valid (it must be active, and
2469 * psa_aead_update_ad() and psa_aead_update() must not have been
2470 * called yet).
2471 * \retval #PSA_ERROR_INVALID_ARGUMENT
2472 * At least one of the lengths is not acceptable for the chosen
2473 * algorithm.
2474 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2475 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2476 * \retval #PSA_ERROR_HARDWARE_FAILURE
2477 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2478 * \retval #PSA_ERROR_BAD_STATE
2479 * The library has not been previously initialized by psa_crypto_init().
2480 * It is implementation-dependent whether a failure to initialize
2481 * results in this error code.
2482 */
2483psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2484 size_t ad_length,
2485 size_t plaintext_length);
2486
2487/** Pass additional data to an active AEAD operation.
2488 *
2489 * Additional data is authenticated, but not encrypted.
2490 *
2491 * You may call this function multiple times to pass successive fragments
2492 * of the additional data. You may not call this function after passing
2493 * data to encrypt or decrypt with psa_aead_update().
2494 *
2495 * Before calling this function, you must:
2496 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2497 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2498 *
2499 * If this function returns an error status, the operation enters an error
2500 * state and must be aborted by calling psa_aead_abort().
2501 *
2502 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2503 * there is no guarantee that the input is valid. Therefore, until
2504 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2505 * treat the input as untrusted and prepare to undo any action that
2506 * depends on the input if psa_aead_verify() returns an error status.
2507 *
2508 * \param[in,out] operation Active AEAD operation.
2509 * \param[in] input Buffer containing the fragment of
2510 * additional data.
2511 * \param input_length Size of the \p input buffer in bytes.
2512 *
2513 * \retval #PSA_SUCCESS
2514 * Success.
2515 * \retval #PSA_ERROR_BAD_STATE
2516 * The operation state is not valid (it must be active, have a nonce
2517 * set, have lengths set if required by the algorithm, and
2518 * psa_aead_update() must not have been called yet).
2519 * \retval #PSA_ERROR_INVALID_ARGUMENT
2520 * The total input length overflows the additional data length that
2521 * was previously specified with psa_aead_set_lengths().
2522 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2523 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2524 * \retval #PSA_ERROR_HARDWARE_FAILURE
2525 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2526 * \retval #PSA_ERROR_STORAGE_FAILURE
2527 * \retval #PSA_ERROR_BAD_STATE
2528 * The library has not been previously initialized by psa_crypto_init().
2529 * It is implementation-dependent whether a failure to initialize
2530 * results in this error code.
2531 */
2532psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2533 const uint8_t *input,
2534 size_t input_length);
2535
2536/** Encrypt or decrypt a message fragment in an active AEAD operation.
2537 *
2538 * Before calling this function, you must:
2539 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2540 * The choice of setup function determines whether this function
2541 * encrypts or decrypts its input.
2542 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2543 * 3. Call psa_aead_update_ad() to pass all the additional data.
2544 *
2545 * If this function returns an error status, the operation enters an error
2546 * state and must be aborted by calling psa_aead_abort().
2547 *
2548 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2549 * there is no guarantee that the input is valid. Therefore, until
2550 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2551 * - Do not use the output in any way other than storing it in a
2552 * confidential location. If you take any action that depends
2553 * on the tentative decrypted data, this action will need to be
2554 * undone if the input turns out not to be valid. Furthermore,
2555 * if an adversary can observe that this action took place
2556 * (for example through timing), they may be able to use this
2557 * fact as an oracle to decrypt any message encrypted with the
2558 * same key.
2559 * - In particular, do not copy the output anywhere but to a
2560 * memory or storage space that you have exclusive access to.
2561 *
2562 * This function does not require the input to be aligned to any
2563 * particular block boundary. If the implementation can only process
2564 * a whole block at a time, it must consume all the input provided, but
2565 * it may delay the end of the corresponding output until a subsequent
2566 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2567 * provides sufficient input. The amount of data that can be delayed
2568 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
2569 *
2570 * \param[in,out] operation Active AEAD operation.
2571 * \param[in] input Buffer containing the message fragment to
2572 * encrypt or decrypt.
2573 * \param input_length Size of the \p input buffer in bytes.
2574 * \param[out] output Buffer where the output is to be written.
2575 * \param output_size Size of the \p output buffer in bytes.
2576 * This must be at least
2577 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2578 * \p input_length) where \c alg is the
2579 * algorithm that is being calculated.
2580 * \param[out] output_length On success, the number of bytes
2581 * that make up the returned output.
2582 *
2583 * \retval #PSA_SUCCESS
2584 * Success.
2585 * \retval #PSA_ERROR_BAD_STATE
2586 * The operation state is not valid (it must be active, have a nonce
2587 * set, and have lengths set if required by the algorithm).
2588 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2589 * The size of the \p output buffer is too small.
2590 * You can determine a sufficient buffer size by calling
2591 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2592 * where \c alg is the algorithm that is being calculated.
2593 * \retval #PSA_ERROR_INVALID_ARGUMENT
2594 * The total length of input to psa_aead_update_ad() so far is
2595 * less than the additional data length that was previously
2596 * specified with psa_aead_set_lengths().
2597 * \retval #PSA_ERROR_INVALID_ARGUMENT
2598 * The total input length overflows the plaintext length that
2599 * was previously specified with psa_aead_set_lengths().
2600 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2601 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2602 * \retval #PSA_ERROR_HARDWARE_FAILURE
2603 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2604 * \retval #PSA_ERROR_STORAGE_FAILURE
2605 * \retval #PSA_ERROR_BAD_STATE
2606 * The library has not been previously initialized by psa_crypto_init().
2607 * It is implementation-dependent whether a failure to initialize
2608 * results in this error code.
2609 */
2610psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2611 const uint8_t *input,
2612 size_t input_length,
2613 uint8_t *output,
2614 size_t output_size,
2615 size_t *output_length);
2616
2617/** Finish encrypting a message in an AEAD operation.
2618 *
2619 * The operation must have been set up with psa_aead_encrypt_setup().
2620 *
2621 * This function finishes the authentication of the additional data
2622 * formed by concatenating the inputs passed to preceding calls to
2623 * psa_aead_update_ad() with the plaintext formed by concatenating the
2624 * inputs passed to preceding calls to psa_aead_update().
2625 *
2626 * This function has two output buffers:
2627 * - \p ciphertext contains trailing ciphertext that was buffered from
2628 * preceding calls to psa_aead_update().
2629 * - \p tag contains the authentication tag. Its length is always
2630 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
2631 * that the operation performs.
2632 *
2633 * When this function returns successfuly, the operation becomes inactive.
2634 * If this function returns an error status, the operation enters an error
2635 * state and must be aborted by calling psa_aead_abort().
2636 *
2637 * \param[in,out] operation Active AEAD operation.
2638 * \param[out] ciphertext Buffer where the last part of the ciphertext
2639 * is to be written.
2640 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2641 * This must be at least
2642 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2643 * \c alg is the algorithm that is being
2644 * calculated.
2645 * \param[out] ciphertext_length On success, the number of bytes of
2646 * returned ciphertext.
2647 * \param[out] tag Buffer where the authentication tag is
2648 * to be written.
2649 * \param tag_size Size of the \p tag buffer in bytes.
2650 * This must be at least
2651 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2652 * the algorithm that is being calculated.
2653 * \param[out] tag_length On success, the number of bytes
2654 * that make up the returned tag.
2655 *
2656 * \retval #PSA_SUCCESS
2657 * Success.
2658 * \retval #PSA_ERROR_BAD_STATE
2659 * The operation state is not valid (it must be an active encryption
2660 * operation with a nonce set).
2661 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2662 * The size of the \p ciphertext or \p tag buffer is too small.
2663 * You can determine a sufficient buffer size for \p ciphertext by
2664 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2665 * where \c alg is the algorithm that is being calculated.
2666 * You can determine a sufficient buffer size for \p tag by
2667 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
2668 * \retval #PSA_ERROR_INVALID_ARGUMENT
2669 * The total length of input to psa_aead_update_ad() so far is
2670 * less than the additional data length that was previously
2671 * specified with psa_aead_set_lengths().
2672 * \retval #PSA_ERROR_INVALID_ARGUMENT
2673 * The total length of input to psa_aead_update() so far is
2674 * less than the plaintext length that was previously
2675 * specified with psa_aead_set_lengths().
2676 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2677 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2678 * \retval #PSA_ERROR_HARDWARE_FAILURE
2679 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2680 * \retval #PSA_ERROR_STORAGE_FAILURE
2681 * \retval #PSA_ERROR_BAD_STATE
2682 * The library has not been previously initialized by psa_crypto_init().
2683 * It is implementation-dependent whether a failure to initialize
2684 * results in this error code.
2685 */
2686psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
2687 uint8_t *ciphertext,
2688 size_t ciphertext_size,
2689 size_t *ciphertext_length,
2690 uint8_t *tag,
2691 size_t tag_size,
2692 size_t *tag_length);
2693
2694/** Finish authenticating and decrypting a message in an AEAD operation.
2695 *
2696 * The operation must have been set up with psa_aead_decrypt_setup().
2697 *
2698 * This function finishes the authenticated decryption of the message
2699 * components:
2700 *
2701 * - The additional data consisting of the concatenation of the inputs
2702 * passed to preceding calls to psa_aead_update_ad().
2703 * - The ciphertext consisting of the concatenation of the inputs passed to
2704 * preceding calls to psa_aead_update().
2705 * - The tag passed to this function call.
2706 *
2707 * If the authentication tag is correct, this function outputs any remaining
2708 * plaintext and reports success. If the authentication tag is not correct,
2709 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
2710 *
2711 * When this function returns successfuly, the operation becomes inactive.
2712 * If this function returns an error status, the operation enters an error
2713 * state and must be aborted by calling psa_aead_abort().
2714 *
2715 * \note Implementations shall make the best effort to ensure that the
2716 * comparison between the actual tag and the expected tag is performed
2717 * in constant time.
2718 *
2719 * \param[in,out] operation Active AEAD operation.
2720 * \param[out] plaintext Buffer where the last part of the plaintext
2721 * is to be written. This is the remaining data
2722 * from previous calls to psa_aead_update()
2723 * that could not be processed until the end
2724 * of the input.
2725 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2726 * This must be at least
2727 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2728 * \c alg is the algorithm that is being
2729 * calculated.
2730 * \param[out] plaintext_length On success, the number of bytes of
2731 * returned plaintext.
2732 * \param[in] tag Buffer containing the authentication tag.
2733 * \param tag_length Size of the \p tag buffer in bytes.
2734 *
2735 * \retval #PSA_SUCCESS
2736 * Success.
2737 * \retval #PSA_ERROR_INVALID_SIGNATURE
2738 * The calculations were successful, but the authentication tag is
2739 * not correct.
2740 * \retval #PSA_ERROR_BAD_STATE
2741 * The operation state is not valid (it must be an active decryption
2742 * operation with a nonce set).
2743 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2744 * The size of the \p plaintext buffer is too small.
2745 * You can determine a sufficient buffer size for \p plaintext by
2746 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2747 * where \c alg is the algorithm that is being calculated.
2748 * \retval #PSA_ERROR_INVALID_ARGUMENT
2749 * The total length of input to psa_aead_update_ad() so far is
2750 * less than the additional data length that was previously
2751 * specified with psa_aead_set_lengths().
2752 * \retval #PSA_ERROR_INVALID_ARGUMENT
2753 * The total length of input to psa_aead_update() so far is
2754 * less than the plaintext length that was previously
2755 * specified with psa_aead_set_lengths().
2756 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2757 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2758 * \retval #PSA_ERROR_HARDWARE_FAILURE
2759 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2760 * \retval #PSA_ERROR_STORAGE_FAILURE
2761 * \retval #PSA_ERROR_BAD_STATE
2762 * The library has not been previously initialized by psa_crypto_init().
2763 * It is implementation-dependent whether a failure to initialize
2764 * results in this error code.
2765 */
2766psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2767 uint8_t *plaintext,
2768 size_t plaintext_size,
2769 size_t *plaintext_length,
2770 const uint8_t *tag,
2771 size_t tag_length);
2772
2773/** Abort an AEAD operation.
2774 *
2775 * Aborting an operation frees all associated resources except for the
2776 * \p operation structure itself. Once aborted, the operation object
2777 * can be reused for another operation by calling
2778 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2779 *
2780 * You may call this function any time after the operation object has
2781 * been initialized as described in #psa_aead_operation_t.
2782 *
2783 * In particular, calling psa_aead_abort() after the operation has been
2784 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2785 * psa_aead_verify() is safe and has no effect.
2786 *
2787 * \param[in,out] operation Initialized AEAD operation.
2788 *
2789 * \retval #PSA_SUCCESS
2790 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2791 * \retval #PSA_ERROR_HARDWARE_FAILURE
2792 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2793 * \retval #PSA_ERROR_BAD_STATE
2794 * The library has not been previously initialized by psa_crypto_init().
2795 * It is implementation-dependent whether a failure to initialize
2796 * results in this error code.
2797 */
2798psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2799
Antonio de Angelis14276e92018-07-10 14:35:43 +01002800/**@}*/
2801
2802/** \defgroup asymmetric Asymmetric cryptography
2803 * @{
2804 */
2805
2806/**
Antonio de Angelis14276e92018-07-10 14:35:43 +01002807 * \brief Sign a hash or short message with a private key.
2808 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002809 * Note that to perform a hash-and-sign signature algorithm, you must
2810 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
2811 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2812 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2813 * to determine the hash algorithm to use.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002814 *
Maulik Patel28659c42021-01-06 14:09:22 +00002815 * \param key Identifier of the key to use for the operation.
2816 * It must be an asymmetric key pair. The key must
2817 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002818 * \param alg A signature algorithm that is compatible with
Maulik Patel28659c42021-01-06 14:09:22 +00002819 * the type of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002820 * \param[in] hash The hash or message to sign.
2821 * \param hash_length Size of the \p hash buffer in bytes.
2822 * \param[out] signature Buffer where the signature is to be written.
2823 * \param signature_size Size of the \p signature buffer in bytes.
2824 * \param[out] signature_length On success, the number of bytes
2825 * that make up the returned signature value.
2826 *
2827 * \retval #PSA_SUCCESS
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002828 * \retval #PSA_ERROR_INVALID_HANDLE
2829 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis377a1552018-11-22 17:02:40 +00002830 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2831 * The size of the \p signature buffer is too small. You can
Antonio de Angelis14276e92018-07-10 14:35:43 +01002832 * determine a sufficient buffer size by calling
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002833 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01002834 * where \c key_type and \c key_bits are the type and bit-size
Maulik Patel28659c42021-01-06 14:09:22 +00002835 * respectively of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002836 * \retval #PSA_ERROR_NOT_SUPPORTED
2837 * \retval #PSA_ERROR_INVALID_ARGUMENT
2838 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2839 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2840 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002841 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2842 * \retval #PSA_ERROR_STORAGE_FAILURE
Antonio de Angelis377a1552018-11-22 17:02:40 +00002843 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002844 * \retval #PSA_ERROR_BAD_STATE
2845 * The library has not been previously initialized by psa_crypto_init().
2846 * It is implementation-dependent whether a failure to initialize
2847 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002848 */
Maulik Patel28659c42021-01-06 14:09:22 +00002849psa_status_t psa_sign_hash(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002850 psa_algorithm_t alg,
2851 const uint8_t *hash,
2852 size_t hash_length,
2853 uint8_t *signature,
2854 size_t signature_size,
2855 size_t *signature_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002856
2857/**
2858 * \brief Verify the signature a hash or short message using a public key.
2859 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002860 * Note that to perform a hash-and-sign signature algorithm, you must
2861 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
2862 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2863 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2864 * to determine the hash algorithm to use.
2865 *
Maulik Patel28659c42021-01-06 14:09:22 +00002866 * \param key Identifier of the key to use for the operation. It
2867 * must be a public key or an asymmetric key pair. The
2868 * key must allow the usage
2869 * #PSA_KEY_USAGE_VERIFY_HASH.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002870 * \param alg A signature algorithm that is compatible with
Maulik Patel28659c42021-01-06 14:09:22 +00002871 * the type of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002872 * \param[in] hash The hash or message whose signature is to be
2873 * verified.
2874 * \param hash_length Size of the \p hash buffer in bytes.
2875 * \param[in] signature Buffer containing the signature to verify.
2876 * \param signature_length Size of the \p signature buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002877 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002878 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01002879 * The signature is valid.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002880 * \retval #PSA_ERROR_INVALID_HANDLE
2881 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis377a1552018-11-22 17:02:40 +00002882 * \retval #PSA_ERROR_INVALID_SIGNATURE
Antonio de Angelis14276e92018-07-10 14:35:43 +01002883 * The calculation was perfomed successfully, but the passed
2884 * signature is not a valid signature.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002885 * \retval #PSA_ERROR_NOT_SUPPORTED
2886 * \retval #PSA_ERROR_INVALID_ARGUMENT
2887 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2888 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2889 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002890 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2891 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002892 * \retval #PSA_ERROR_BAD_STATE
2893 * The library has not been previously initialized by psa_crypto_init().
2894 * It is implementation-dependent whether a failure to initialize
2895 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002896 */
Maulik Patel28659c42021-01-06 14:09:22 +00002897psa_status_t psa_verify_hash(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002898 psa_algorithm_t alg,
2899 const uint8_t *hash,
2900 size_t hash_length,
2901 const uint8_t *signature,
2902 size_t signature_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002903
Antonio de Angelis14276e92018-07-10 14:35:43 +01002904/**
2905 * \brief Encrypt a short message with a public key.
2906 *
Maulik Patel28659c42021-01-06 14:09:22 +00002907 * \param key Identifer of the key to use for the operation.
2908 * It must be a public key or an asymmetric key
2909 * pair. It must allow the usage
2910 * #PSA_KEY_USAGE_ENCRYPT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002911 * \param alg An asymmetric encryption algorithm that is
Maulik Patel28659c42021-01-06 14:09:22 +00002912 * compatible with the type of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002913 * \param[in] input The message to encrypt.
2914 * \param input_length Size of the \p input buffer in bytes.
2915 * \param[in] salt A salt or label, if supported by the
2916 * encryption algorithm.
2917 * If the algorithm does not support a
2918 * salt, pass \c NULL.
2919 * If the algorithm supports an optional
2920 * salt and you do not want to pass a salt,
2921 * pass \c NULL.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002922 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002923 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2924 * supported.
2925 * \param salt_length Size of the \p salt buffer in bytes.
2926 * If \p salt is \c NULL, pass 0.
2927 * \param[out] output Buffer where the encrypted message is to
2928 * be written.
2929 * \param output_size Size of the \p output buffer in bytes.
2930 * \param[out] output_length On success, the number of bytes
2931 * that make up the returned output.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002932 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002933 * \retval #PSA_SUCCESS
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002934 * \retval #PSA_ERROR_INVALID_HANDLE
2935 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis377a1552018-11-22 17:02:40 +00002936 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2937 * The size of the \p output buffer is too small. You can
Antonio de Angelis14276e92018-07-10 14:35:43 +01002938 * determine a sufficient buffer size by calling
Antonio de Angelis377a1552018-11-22 17:02:40 +00002939 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01002940 * where \c key_type and \c key_bits are the type and bit-size
Maulik Patel28659c42021-01-06 14:09:22 +00002941 * respectively of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002942 * \retval #PSA_ERROR_NOT_SUPPORTED
2943 * \retval #PSA_ERROR_INVALID_ARGUMENT
2944 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2945 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2946 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002947 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2948 * \retval #PSA_ERROR_STORAGE_FAILURE
Antonio de Angelis377a1552018-11-22 17:02:40 +00002949 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002950 * \retval #PSA_ERROR_BAD_STATE
2951 * The library has not been previously initialized by psa_crypto_init().
2952 * It is implementation-dependent whether a failure to initialize
2953 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002954 */
Maulik Patel28659c42021-01-06 14:09:22 +00002955psa_status_t psa_asymmetric_encrypt(psa_key_id_t key,
Antonio de Angelis14276e92018-07-10 14:35:43 +01002956 psa_algorithm_t alg,
2957 const uint8_t *input,
2958 size_t input_length,
2959 const uint8_t *salt,
2960 size_t salt_length,
2961 uint8_t *output,
2962 size_t output_size,
2963 size_t *output_length);
2964
2965/**
2966 * \brief Decrypt a short message with a private key.
2967 *
Maulik Patel28659c42021-01-06 14:09:22 +00002968 * \param key Identifier of the key to use for the operation.
2969 * It must be an asymmetric key pair. It must
2970 * allow the usage #PSA_KEY_USAGE_DECRYPT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002971 * \param alg An asymmetric encryption algorithm that is
Maulik Patel28659c42021-01-06 14:09:22 +00002972 * compatible with the type of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002973 * \param[in] input The message to decrypt.
2974 * \param input_length Size of the \p input buffer in bytes.
2975 * \param[in] salt A salt or label, if supported by the
2976 * encryption algorithm.
2977 * If the algorithm does not support a
2978 * salt, pass \c NULL.
2979 * If the algorithm supports an optional
2980 * salt and you do not want to pass a salt,
2981 * pass \c NULL.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002982 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002983 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2984 * supported.
2985 * \param salt_length Size of the \p salt buffer in bytes.
2986 * If \p salt is \c NULL, pass 0.
2987 * \param[out] output Buffer where the decrypted message is to
2988 * be written.
2989 * \param output_size Size of the \c output buffer in bytes.
2990 * \param[out] output_length On success, the number of bytes
2991 * that make up the returned output.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002992 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002993 * \retval #PSA_SUCCESS
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002994 * \retval #PSA_ERROR_INVALID_HANDLE
2995 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis377a1552018-11-22 17:02:40 +00002996 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2997 * The size of the \p output buffer is too small. You can
Antonio de Angelis14276e92018-07-10 14:35:43 +01002998 * determine a sufficient buffer size by calling
Antonio de Angelis377a1552018-11-22 17:02:40 +00002999 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01003000 * where \c key_type and \c key_bits are the type and bit-size
Maulik Patel28659c42021-01-06 14:09:22 +00003001 * respectively of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003002 * \retval #PSA_ERROR_NOT_SUPPORTED
3003 * \retval #PSA_ERROR_INVALID_ARGUMENT
3004 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3005 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3006 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003007 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3008 * \retval #PSA_ERROR_STORAGE_FAILURE
Antonio de Angelis377a1552018-11-22 17:02:40 +00003009 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3010 * \retval #PSA_ERROR_INVALID_PADDING
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003011 * \retval #PSA_ERROR_BAD_STATE
3012 * The library has not been previously initialized by psa_crypto_init().
3013 * It is implementation-dependent whether a failure to initialize
3014 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003015 */
Maulik Patel28659c42021-01-06 14:09:22 +00003016psa_status_t psa_asymmetric_decrypt(psa_key_id_t key,
Antonio de Angelis14276e92018-07-10 14:35:43 +01003017 psa_algorithm_t alg,
3018 const uint8_t *input,
3019 size_t input_length,
3020 const uint8_t *salt,
3021 size_t salt_length,
3022 uint8_t *output,
3023 size_t output_size,
3024 size_t *output_length);
3025
3026/**@}*/
3027
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003028/** \defgroup key_derivation Key derivation and pseudorandom generation
Antonio de Angelis377a1552018-11-22 17:02:40 +00003029 * @{
3030 */
3031
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003032/** The type of the state data structure for key derivation operations.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003033 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003034 * Before calling any function on a key derivation operation object, the
3035 * application must initialize it by any of the following means:
Antonio de Angelis377a1552018-11-22 17:02:40 +00003036 * - Set the structure to all-bits-zero, for example:
3037 * \code
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003038 * psa_key_derivation_operation_t operation;
3039 * memset(&operation, 0, sizeof(operation));
Antonio de Angelis377a1552018-11-22 17:02:40 +00003040 * \endcode
3041 * - Initialize the structure to logical zero values, for example:
3042 * \code
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003043 * psa_key_derivation_operation_t operation = {0};
Antonio de Angelis377a1552018-11-22 17:02:40 +00003044 * \endcode
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003045 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Antonio de Angelis377a1552018-11-22 17:02:40 +00003046 * for example:
3047 * \code
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003048 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Antonio de Angelis377a1552018-11-22 17:02:40 +00003049 * \endcode
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003050 * - Assign the result of the function psa_key_derivation_operation_init()
Antonio de Angelis377a1552018-11-22 17:02:40 +00003051 * to the structure, for example:
3052 * \code
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003053 * psa_key_derivation_operation_t operation;
3054 * operation = psa_key_derivation_operation_init();
Antonio de Angelis377a1552018-11-22 17:02:40 +00003055 * \endcode
3056 *
3057 * This is an implementation-defined \c struct. Applications should not
3058 * make any assumptions about the content of this structure except
3059 * as directed by the documentation of a specific implementation.
3060 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003061typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Antonio de Angelis377a1552018-11-22 17:02:40 +00003062
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003063/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Antonio de Angelis377a1552018-11-22 17:02:40 +00003064 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003065 * This macro returns a suitable initializer for a key derivation operation
3066 * object of type #psa_key_derivation_operation_t.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003067 */
3068#ifdef __DOXYGEN_ONLY__
3069/* This is an example definition for documentation purposes.
3070 * Implementations should define a suitable value in `crypto_struct.h`.
3071 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003072#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Antonio de Angelis377a1552018-11-22 17:02:40 +00003073#endif
3074
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003075/** Return an initial value for a key derivation operation object.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003076 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003077static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Antonio de Angelis377a1552018-11-22 17:02:40 +00003078
3079/** Set up a key derivation operation.
3080 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003081 * A key derivation algorithm takes some inputs and uses them to generate
3082 * a byte stream in a deterministic way.
3083 * This byte stream can be used to produce keys and other
3084 * cryptographic material.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003085 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003086 * To derive a key:
3087 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3088 * -# Call psa_key_derivation_setup() to select the algorithm.
3089 * -# Provide the inputs for the key derivation by calling
3090 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3091 * as appropriate. Which inputs are needed, in what order, and whether
3092 * they may be keys and if so of what type depends on the algorithm.
3093 * -# Optionally set the operation's maximum capacity with
3094 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3095 * of or after providing inputs. For some algorithms, this step is mandatory
3096 * because the output depends on the maximum capacity.
3097 * -# To derive a key, call psa_key_derivation_output_key().
3098 * To derive a byte string for a different purpose, call
3099 * psa_key_derivation_output_bytes().
3100 * Successive calls to these functions use successive output bytes
3101 * calculated by the key derivation algorithm.
3102 * -# Clean up the key derivation operation object with
3103 * psa_key_derivation_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00003104 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003105 * If this function returns an error, the key derivation operation object is
3106 * not changed.
3107 *
3108 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3109 * the operation will need to be reset by a call to psa_key_derivation_abort().
3110 *
3111 * Implementations must reject an attempt to derive a key of size 0.
3112 *
3113 * \param[in,out] operation The key derivation operation object
3114 * to set up. It must
3115 * have been initialized but not set up yet.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003116 * \param alg The key derivation algorithm to compute
3117 * (\c PSA_ALG_XXX value such that
3118 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +00003119 *
3120 * \retval #PSA_SUCCESS
3121 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003122 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003123 * \c alg is not a key derivation algorithm.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003124 * \retval #PSA_ERROR_NOT_SUPPORTED
3125 * \c alg is not supported or is not a key derivation algorithm.
3126 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3127 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3128 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003129 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3130 * \retval #PSA_ERROR_STORAGE_FAILURE
3131 * \retval #PSA_ERROR_BAD_STATE
3132 * The operation state is not valid (it must be inactive).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003133 * \retval #PSA_ERROR_BAD_STATE
3134 * The library has not been previously initialized by psa_crypto_init().
3135 * It is implementation-dependent whether a failure to initialize
3136 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003137 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003138psa_status_t psa_key_derivation_setup(
3139 psa_key_derivation_operation_t *operation,
3140 psa_algorithm_t alg);
Antonio de Angelis377a1552018-11-22 17:02:40 +00003141
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003142/** Retrieve the current capacity of a key derivation operation.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003143 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003144 * The capacity of a key derivation is the maximum number of bytes that it can
3145 * return. When you get *N* bytes of output from a key derivation operation,
3146 * this reduces its capacity by *N*.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003147 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003148 * \param[in] operation The operation to query.
3149 * \param[out] capacity On success, the capacity of the operation.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003150 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003151 * \retval #PSA_SUCCESS
3152 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3153 * \retval #PSA_ERROR_BAD_STATE
3154 * The operation state is not valid (it must be active).
3155 * \retval #PSA_ERROR_HARDWARE_FAILURE
3156 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3157 * \retval #PSA_ERROR_BAD_STATE
3158 * The library has not been previously initialized by psa_crypto_init().
3159 * It is implementation-dependent whether a failure to initialize
3160 * results in this error code.
3161 */
3162psa_status_t psa_key_derivation_get_capacity(
3163 const psa_key_derivation_operation_t *operation,
3164 size_t *capacity);
3165
3166/** Set the maximum capacity of a key derivation operation.
3167 *
3168 * The capacity of a key derivation operation is the maximum number of bytes
3169 * that the key derivation operation can return from this point onwards.
3170 *
3171 * \param[in,out] operation The key derivation operation object to modify.
3172 * \param capacity The new capacity of the operation.
3173 * It must be less or equal to the operation's
3174 * current capacity.
3175 *
3176 * \retval #PSA_SUCCESS
3177 * \retval #PSA_ERROR_INVALID_ARGUMENT
3178 * \p capacity is larger than the operation's current capacity.
3179 * In this case, the operation object remains valid and its capacity
3180 * remains unchanged.
3181 * \retval #PSA_ERROR_BAD_STATE
3182 * The operation state is not valid (it must be active).
3183 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3184 * \retval #PSA_ERROR_HARDWARE_FAILURE
3185 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3186 * \retval #PSA_ERROR_BAD_STATE
3187 * The library has not been previously initialized by psa_crypto_init().
3188 * It is implementation-dependent whether a failure to initialize
3189 * results in this error code.
3190 */
3191psa_status_t psa_key_derivation_set_capacity(
3192 psa_key_derivation_operation_t *operation,
3193 size_t capacity);
3194
3195/** Use the maximum possible capacity for a key derivation operation.
3196 *
3197 * Use this value as the capacity argument when setting up a key derivation
3198 * to indicate that the operation should have the maximum possible capacity.
3199 * The value of the maximum possible capacity depends on the key derivation
3200 * algorithm.
3201 */
3202#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3203
3204/** Provide an input for key derivation or key agreement.
3205 *
3206 * Which inputs are required and in what order depends on the algorithm.
3207 * Refer to the documentation of each key derivation or key agreement
3208 * algorithm for information.
3209 *
3210 * This function passes direct inputs, which is usually correct for
3211 * non-secret inputs. To pass a secret input, which should be in a key
3212 * object, call psa_key_derivation_input_key() instead of this function.
3213 * Refer to the documentation of individual step types
3214 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3215 * for more information.
3216 *
3217 * If this function returns an error status, the operation enters an error
3218 * state and must be aborted by calling psa_key_derivation_abort().
3219 *
3220 * \param[in,out] operation The key derivation operation object to use.
3221 * It must have been set up with
3222 * psa_key_derivation_setup() and must not
3223 * have produced any output yet.
3224 * \param step Which step the input data is for.
3225 * \param[in] data Input data to use.
3226 * \param data_length Size of the \p data buffer in bytes.
3227 *
3228 * \retval #PSA_SUCCESS
3229 * Success.
3230 * \retval #PSA_ERROR_INVALID_ARGUMENT
3231 * \c step is not compatible with the operation's algorithm.
3232 * \retval #PSA_ERROR_INVALID_ARGUMENT
3233 * \c step does not allow direct inputs.
3234 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3235 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3236 * \retval #PSA_ERROR_HARDWARE_FAILURE
3237 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3238 * \retval #PSA_ERROR_STORAGE_FAILURE
3239 * \retval #PSA_ERROR_BAD_STATE
3240 * The operation state is not valid for this input \p step.
3241 * \retval #PSA_ERROR_BAD_STATE
3242 * The library has not been previously initialized by psa_crypto_init().
3243 * It is implementation-dependent whether a failure to initialize
3244 * results in this error code.
3245 */
3246psa_status_t psa_key_derivation_input_bytes(
3247 psa_key_derivation_operation_t *operation,
3248 psa_key_derivation_step_t step,
3249 const uint8_t *data,
3250 size_t data_length);
3251
3252/** Provide an input for key derivation in the form of a key.
3253 *
3254 * Which inputs are required and in what order depends on the algorithm.
3255 * Refer to the documentation of each key derivation or key agreement
3256 * algorithm for information.
3257 *
3258 * This function obtains input from a key object, which is usually correct for
3259 * secret inputs or for non-secret personalization strings kept in the key
3260 * store. To pass a non-secret parameter which is not in the key store,
3261 * call psa_key_derivation_input_bytes() instead of this function.
3262 * Refer to the documentation of individual step types
3263 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3264 * for more information.
3265 *
3266 * If this function returns an error status, the operation enters an error
3267 * state and must be aborted by calling psa_key_derivation_abort().
3268 *
3269 * \param[in,out] operation The key derivation operation object to use.
3270 * It must have been set up with
3271 * psa_key_derivation_setup() and must not
3272 * have produced any output yet.
3273 * \param step Which step the input data is for.
Maulik Patel28659c42021-01-06 14:09:22 +00003274 * \param key Identifier of the key. It must have an
3275 * appropriate type for step and must allow the
3276 * usage #PSA_KEY_USAGE_DERIVE.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003277 *
3278 * \retval #PSA_SUCCESS
3279 * Success.
3280 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003281 * \retval #PSA_ERROR_NOT_PERMITTED
3282 * \retval #PSA_ERROR_INVALID_ARGUMENT
3283 * \c step is not compatible with the operation's algorithm.
3284 * \retval #PSA_ERROR_INVALID_ARGUMENT
3285 * \c step does not allow key inputs of the given type
3286 * or does not allow key inputs at all.
3287 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3288 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3289 * \retval #PSA_ERROR_HARDWARE_FAILURE
3290 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3291 * \retval #PSA_ERROR_STORAGE_FAILURE
3292 * \retval #PSA_ERROR_BAD_STATE
3293 * The operation state is not valid for this input \p step.
3294 * \retval #PSA_ERROR_BAD_STATE
3295 * The library has not been previously initialized by psa_crypto_init().
3296 * It is implementation-dependent whether a failure to initialize
3297 * results in this error code.
3298 */
3299psa_status_t psa_key_derivation_input_key(
3300 psa_key_derivation_operation_t *operation,
3301 psa_key_derivation_step_t step,
Maulik Patel28659c42021-01-06 14:09:22 +00003302 psa_key_id_t key);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003303
3304/** Perform a key agreement and use the shared secret as input to a key
3305 * derivation.
3306 *
3307 * A key agreement algorithm takes two inputs: a private key \p private_key
3308 * a public key \p peer_key.
3309 * The result of this function is passed as input to a key derivation.
3310 * The output of this key derivation can be extracted by reading from the
3311 * resulting operation to produce keys and other cryptographic material.
3312 *
3313 * If this function returns an error status, the operation enters an error
3314 * state and must be aborted by calling psa_key_derivation_abort().
3315 *
3316 * \param[in,out] operation The key derivation operation object to use.
3317 * It must have been set up with
3318 * psa_key_derivation_setup() with a
3319 * key agreement and derivation algorithm
3320 * \c alg (\c PSA_ALG_XXX value such that
3321 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3322 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3323 * is false).
3324 * The operation must be ready for an
3325 * input of the type given by \p step.
3326 * \param step Which step the input data is for.
Maulik Patel28659c42021-01-06 14:09:22 +00003327 * \param private_key Identifier of the private key to use. It must
3328 * allow the usage #PSA_KEY_USAGE_DERIVE.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003329 * \param[in] peer_key Public key of the peer. The peer key must be in the
3330 * same format that psa_import_key() accepts for the
3331 * public key type corresponding to the type of
3332 * private_key. That is, this function performs the
3333 * equivalent of
3334 * #psa_import_key(...,
3335 * `peer_key`, `peer_key_length`) where
3336 * with key attributes indicating the public key
3337 * type corresponding to the type of `private_key`.
3338 * For example, for EC keys, this means that peer_key
3339 * is interpreted as a point on the curve that the
3340 * private key is on. The standard formats for public
3341 * keys are documented in the documentation of
3342 * psa_export_public_key().
3343 * \param peer_key_length Size of \p peer_key in bytes.
3344 *
3345 * \retval #PSA_SUCCESS
3346 * Success.
3347 * \retval #PSA_ERROR_BAD_STATE
3348 * The operation state is not valid for this key agreement \p step.
3349 * \retval #PSA_ERROR_INVALID_HANDLE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003350 * \retval #PSA_ERROR_NOT_PERMITTED
3351 * \retval #PSA_ERROR_INVALID_ARGUMENT
3352 * \c private_key is not compatible with \c alg,
3353 * or \p peer_key is not valid for \c alg or not compatible with
3354 * \c private_key.
3355 * \retval #PSA_ERROR_NOT_SUPPORTED
3356 * \c alg is not supported or is not a key derivation algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003357 * \retval #PSA_ERROR_INVALID_ARGUMENT
3358 * \c step does not allow an input resulting from a key agreement.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003359 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3360 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3361 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003362 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3363 * \retval #PSA_ERROR_STORAGE_FAILURE
3364 * \retval #PSA_ERROR_BAD_STATE
3365 * The library has not been previously initialized by psa_crypto_init().
3366 * It is implementation-dependent whether a failure to initialize
3367 * results in this error code.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003368 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003369psa_status_t psa_key_derivation_key_agreement(
3370 psa_key_derivation_operation_t *operation,
3371 psa_key_derivation_step_t step,
Maulik Patel28659c42021-01-06 14:09:22 +00003372 psa_key_id_t private_key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003373 const uint8_t *peer_key,
3374 size_t peer_key_length);
3375
3376/** Read some data from a key derivation operation.
3377 *
3378 * This function calculates output bytes from a key derivation algorithm and
3379 * return those bytes.
3380 * If you view the key derivation's output as a stream of bytes, this
3381 * function destructively reads the requested number of bytes from the
3382 * stream.
3383 * The operation's capacity decreases by the number of bytes read.
3384 *
3385 * If this function returns an error status other than
3386 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
3387 * state and must be aborted by calling psa_key_derivation_abort().
3388 *
3389 * \param[in,out] operation The key derivation operation object to read from.
3390 * \param[out] output Buffer where the output will be written.
3391 * \param output_length Number of bytes to output.
3392 *
3393 * \retval #PSA_SUCCESS
3394 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3395 * The operation's capacity was less than
3396 * \p output_length bytes. Note that in this case,
3397 * no output is written to the output buffer.
3398 * The operation's capacity is set to 0, thus
3399 * subsequent calls to this function will not
3400 * succeed, even with a smaller output buffer.
3401 * \retval #PSA_ERROR_BAD_STATE
3402 * The operation state is not valid (it must be active and completed
3403 * all required input steps).
3404 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3405 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3406 * \retval #PSA_ERROR_HARDWARE_FAILURE
3407 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3408 * \retval #PSA_ERROR_STORAGE_FAILURE
3409 * \retval #PSA_ERROR_BAD_STATE
3410 * The library has not been previously initialized by psa_crypto_init().
3411 * It is implementation-dependent whether a failure to initialize
3412 * results in this error code.
3413 */
3414psa_status_t psa_key_derivation_output_bytes(
3415 psa_key_derivation_operation_t *operation,
3416 uint8_t *output,
3417 size_t output_length);
3418
3419/** Derive a key from an ongoing key derivation operation.
3420 *
3421 * This function calculates output bytes from a key derivation algorithm
3422 * and uses those bytes to generate a key deterministically.
3423 * The key's location, usage policy, type and size are taken from
3424 * \p attributes.
3425 *
3426 * If you view the key derivation's output as a stream of bytes, this
3427 * function destructively reads as many bytes as required from the
3428 * stream.
3429 * The operation's capacity decreases by the number of bytes read.
3430 *
3431 * If this function returns an error status other than
3432 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
3433 * state and must be aborted by calling psa_key_derivation_abort().
3434 *
3435 * How much output is produced and consumed from the operation, and how
3436 * the key is derived, depends on the key type:
3437 *
3438 * - For key types for which the key is an arbitrary sequence of bytes
3439 * of a given size, this function is functionally equivalent to
3440 * calling #psa_key_derivation_output_bytes
3441 * and passing the resulting output to #psa_import_key.
3442 * However, this function has a security benefit:
3443 * if the implementation provides an isolation boundary then
3444 * the key material is not exposed outside the isolation boundary.
3445 * As a consequence, for these key types, this function always consumes
3446 * exactly (\p bits / 8) bytes from the operation.
3447 * The following key types defined in this specification follow this scheme:
3448 *
3449 * - #PSA_KEY_TYPE_AES;
3450 * - #PSA_KEY_TYPE_ARC4;
3451 * - #PSA_KEY_TYPE_CAMELLIA;
3452 * - #PSA_KEY_TYPE_DERIVE;
3453 * - #PSA_KEY_TYPE_HMAC.
3454 *
3455 * - For ECC keys on a Montgomery elliptic curve
3456 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
3457 * Montgomery curve), this function always draws a byte string whose
3458 * length is determined by the curve, and sets the mandatory bits
3459 * accordingly. That is:
3460 *
Summer Qin0e5b2e02020-10-22 11:23:39 +08003461 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Soby Mathew07ef6e42020-07-20 21:09:23 +01003462 * string and process it as specified in RFC 7748 &sect;5.
Summer Qin0e5b2e02020-10-22 11:23:39 +08003463 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Soby Mathew07ef6e42020-07-20 21:09:23 +01003464 * string and process it as specified in RFC 7748 &sect;5.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003465 *
3466 * - For key types for which the key is represented by a single sequence of
3467 * \p bits bits with constraints as to which bit sequences are acceptable,
3468 * this function draws a byte string of length (\p bits / 8) bytes rounded
3469 * up to the nearest whole number of bytes. If the resulting byte string
3470 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3471 * This process is repeated until an acceptable byte string is drawn.
3472 * The byte string drawn from the operation is interpreted as specified
3473 * for the output produced by psa_export_key().
3474 * The following key types defined in this specification follow this scheme:
3475 *
3476 * - #PSA_KEY_TYPE_DES.
3477 * Force-set the parity bits, but discard forbidden weak keys.
3478 * For 2-key and 3-key triple-DES, the three keys are generated
3479 * successively (for example, for 3-key triple-DES,
3480 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3481 * discard the first 8 bytes, use the next 8 bytes as the first key,
3482 * and continue reading output from the operation to derive the other
3483 * two keys).
3484 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
3485 * where \c group designates any Diffie-Hellman group) and
3486 * ECC keys on a Weierstrass elliptic curve
3487 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
3488 * Weierstrass curve).
3489 * For these key types, interpret the byte string as integer
3490 * in big-endian order. Discard it if it is not in the range
3491 * [0, *N* - 2] where *N* is the boundary of the private key domain
3492 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
3493 * or the order of the curve's base point for ECC).
3494 * Add 1 to the resulting integer and use this as the private key *x*.
3495 * This method allows compliance to NIST standards, specifically
3496 * the methods titled "key-pair generation by testing candidates"
3497 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3498 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3499 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3500 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
3501 *
3502 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
3503 * the way in which the operation output is consumed is
3504 * implementation-defined.
3505 *
3506 * In all cases, the data that is read is discarded from the operation.
3507 * The operation's capacity is decreased by the number of bytes read.
3508 *
3509 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3510 * the input to that step must be provided with psa_key_derivation_input_key().
3511 * Future versions of this specification may include additional restrictions
3512 * on the derived key based on the attributes and strength of the secret key.
3513 *
3514 * \param[in] attributes The attributes for the new key.
3515 * \param[in,out] operation The key derivation operation object to read from.
Maulik Patel28659c42021-01-06 14:09:22 +00003516 * \param[out] key On success, an identifier for the newly created
3517 * key. For persistent keys, this is the key
3518 * identifier defined in \p attributes.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003519 * \c 0 on failure.
3520 *
3521 * \retval #PSA_SUCCESS
3522 * Success.
3523 * If the key is persistent, the key material and the key's metadata
3524 * have been saved to persistent storage.
3525 * \retval #PSA_ERROR_ALREADY_EXISTS
3526 * This is an attempt to create a persistent key, and there is
3527 * already a persistent key with the given identifier.
3528 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3529 * There was not enough data to create the desired key.
3530 * Note that in this case, no output is written to the output buffer.
3531 * The operation's capacity is set to 0, thus subsequent calls to
3532 * this function will not succeed, even with a smaller output buffer.
3533 * \retval #PSA_ERROR_NOT_SUPPORTED
3534 * The key type or key size is not supported, either by the
3535 * implementation in general or in this particular location.
3536 * \retval #PSA_ERROR_INVALID_ARGUMENT
3537 * The provided key attributes are not valid for the operation.
3538 * \retval #PSA_ERROR_NOT_PERMITTED
3539 * The #PSA_KEY_DERIVATION_INPUT_SECRET input was not provided through
3540 * a key.
3541 * \retval #PSA_ERROR_BAD_STATE
3542 * The operation state is not valid (it must be active and completed
3543 * all required input steps).
3544 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3545 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3546 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3547 * \retval #PSA_ERROR_HARDWARE_FAILURE
3548 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Maulik Patel13b27cf2021-05-14 11:44:53 +01003549 * \retval #PSA_ERROR_DATA_INVALID
3550 * \retval #PSA_ERROR_DATA_CORRUPT
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003551 * \retval #PSA_ERROR_STORAGE_FAILURE
3552 * \retval #PSA_ERROR_BAD_STATE
3553 * The library has not been previously initialized by psa_crypto_init().
3554 * It is implementation-dependent whether a failure to initialize
3555 * results in this error code.
3556 */
3557psa_status_t psa_key_derivation_output_key(
3558 const psa_key_attributes_t *attributes,
3559 psa_key_derivation_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00003560 psa_key_id_t *key);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003561
3562/** Abort a key derivation operation.
3563 *
3564 * Aborting an operation frees all associated resources except for the \c
3565 * operation structure itself. Once aborted, the operation object can be reused
3566 * for another operation by calling psa_key_derivation_setup() again.
3567 *
3568 * This function may be called at any time after the operation
3569 * object has been initialized as described in #psa_key_derivation_operation_t.
3570 *
3571 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3572 * call psa_key_derivation_abort() on an operation that has not been set up.
3573 *
3574 * \param[in,out] operation The operation to abort.
3575 *
3576 * \retval #PSA_SUCCESS
3577 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3578 * \retval #PSA_ERROR_HARDWARE_FAILURE
3579 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3580 * \retval #PSA_ERROR_BAD_STATE
3581 * The library has not been previously initialized by psa_crypto_init().
3582 * It is implementation-dependent whether a failure to initialize
3583 * results in this error code.
3584 */
3585psa_status_t psa_key_derivation_abort(
3586 psa_key_derivation_operation_t *operation);
3587
3588/** Perform a key agreement and return the raw shared secret.
3589 *
3590 * \warning The raw result of a key agreement algorithm such as finite-field
3591 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3592 * not be used directly as key material. It should instead be passed as
3593 * input to a key derivation algorithm. To chain a key agreement with
3594 * a key derivation, use psa_key_derivation_key_agreement() and other
3595 * functions from the key derivation interface.
3596 *
3597 * \param alg The key agreement algorithm to compute
3598 * (\c PSA_ALG_XXX value such that
3599 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3600 * is true).
Maulik Patel28659c42021-01-06 14:09:22 +00003601 * \param private_key Identifier of the private key to use. It must
3602 * allow the usage #PSA_KEY_USAGE_DERIVE.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003603 * \param[in] peer_key Public key of the peer. It must be
3604 * in the same format that psa_import_key()
3605 * accepts. The standard formats for public
3606 * keys are documented in the documentation
3607 * of psa_export_public_key().
3608 * \param peer_key_length Size of \p peer_key in bytes.
3609 * \param[out] output Buffer where the decrypted message is to
3610 * be written.
3611 * \param output_size Size of the \c output buffer in bytes.
3612 * \param[out] output_length On success, the number of bytes
3613 * that make up the returned output.
3614 *
3615 * \retval #PSA_SUCCESS
3616 * Success.
3617 * \retval #PSA_ERROR_INVALID_HANDLE
3618 * \retval #PSA_ERROR_NOT_PERMITTED
3619 * \retval #PSA_ERROR_INVALID_ARGUMENT
3620 * \p alg is not a key agreement algorithm
3621 * \retval #PSA_ERROR_INVALID_ARGUMENT
3622 * \p private_key is not compatible with \p alg,
3623 * or \p peer_key is not valid for \p alg or not compatible with
3624 * \p private_key.
3625 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3626 * \p output_size is too small
3627 * \retval #PSA_ERROR_NOT_SUPPORTED
3628 * \p alg is not a supported key agreement algorithm.
3629 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3630 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3631 * \retval #PSA_ERROR_HARDWARE_FAILURE
3632 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3633 * \retval #PSA_ERROR_STORAGE_FAILURE
3634 * \retval #PSA_ERROR_BAD_STATE
3635 * The library has not been previously initialized by psa_crypto_init().
3636 * It is implementation-dependent whether a failure to initialize
3637 * results in this error code.
3638 */
3639psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Maulik Patel28659c42021-01-06 14:09:22 +00003640 psa_key_id_t private_key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003641 const uint8_t *peer_key,
3642 size_t peer_key_length,
3643 uint8_t *output,
3644 size_t output_size,
3645 size_t *output_length);
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003646
Antonio de Angelis377a1552018-11-22 17:02:40 +00003647/**@}*/
3648
3649/** \defgroup random Random generation
Antonio de Angelis14276e92018-07-10 14:35:43 +01003650 * @{
3651 */
3652
3653/**
3654 * \brief Generate random bytes.
3655 *
3656 * \warning This function **can** fail! Callers MUST check the return status
3657 * and MUST NOT use the content of the output buffer if the return
3658 * status is not #PSA_SUCCESS.
3659 *
3660 * \note To generate a key, use psa_generate_key() instead.
3661 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003662 * \param[out] output Output buffer for the generated data.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003663 * \param output_size Number of bytes to generate and output.
3664 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003665 * \retval #PSA_SUCCESS
3666 * \retval #PSA_ERROR_NOT_SUPPORTED
3667 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003668 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Antonio de Angelis377a1552018-11-22 17:02:40 +00003669 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3670 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003671 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003672 * \retval #PSA_ERROR_BAD_STATE
3673 * The library has not been previously initialized by psa_crypto_init().
3674 * It is implementation-dependent whether a failure to initialize
3675 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003676 */
3677psa_status_t psa_generate_random(uint8_t *output,
3678 size_t output_size);
3679
3680/**
3681 * \brief Generate a key or key pair.
3682 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003683 * The key is generated randomly.
3684 * Its location, usage policy, type and size are taken from \p attributes.
3685 *
3686 * Implementations must reject an attempt to generate a key of size 0.
3687 *
3688 * The following type-specific considerations apply:
3689 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
3690 * the public exponent is 65537.
3691 * The modulus is a product of two probabilistic primes
3692 * between 2^{n-1} and 2^n where n is the bit size specified in the
3693 * attributes.
3694 *
3695 * \param[in] attributes The attributes for the new key.
Maulik Patel28659c42021-01-06 14:09:22 +00003696 * \param[out] key On success, an identifier for the newly created
3697 * key. For persistent keys, this is the key
3698 * identifier defined in \p attributes.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003699 * \c 0 on failure.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003700 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003701 * \retval #PSA_SUCCESS
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003702 * Success.
3703 * If the key is persistent, the key material and the key's metadata
3704 * have been saved to persistent storage.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003705 * \retval #PSA_ERROR_ALREADY_EXISTS
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003706 * This is an attempt to create a persistent key, and there is
3707 * already a persistent key with the given identifier.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003708 * \retval #PSA_ERROR_NOT_SUPPORTED
3709 * \retval #PSA_ERROR_INVALID_ARGUMENT
3710 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3711 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3712 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3713 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003714 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3715 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Maulik Patel13b27cf2021-05-14 11:44:53 +01003716 * \retval #PSA_ERROR_DATA_INVALID
3717 * \retval #PSA_ERROR_DATA_CORRUPT
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003718 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003719 * \retval #PSA_ERROR_BAD_STATE
3720 * The library has not been previously initialized by psa_crypto_init().
3721 * It is implementation-dependent whether a failure to initialize
3722 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003723 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003724psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Maulik Patel28659c42021-01-06 14:09:22 +00003725 psa_key_id_t *key);
Antonio de Angelis14276e92018-07-10 14:35:43 +01003726
3727/**@}*/
3728
3729#ifdef __cplusplus
3730}
3731#endif
3732
Antonio de Angelis377a1552018-11-22 17:02:40 +00003733/* The file "crypto_sizes.h" contains definitions for size calculation
3734 * macros whose definitions are implementation-specific. */
Jamie Foxcc31d402019-01-28 17:13:52 +00003735#include "psa/crypto_sizes.h"
Antonio de Angelis377a1552018-11-22 17:02:40 +00003736
Soby Mathewd7b79f22020-05-21 15:06:54 +01003737/* The file "crypto_client_struct.h" contains definitions for structures
3738 * whose definitions differ in the client view and the PSA server
3739 * implementation in TF-M. */
3740#include "psa/crypto_client_struct.h"
3741
3742
Antonio de Angelis377a1552018-11-22 17:02:40 +00003743/* The file "crypto_struct.h" contains definitions for
3744 * implementation-specific structs that are declared above. */
Jamie Foxcc31d402019-01-28 17:13:52 +00003745#include "psa/crypto_struct.h"
Antonio de Angelis377a1552018-11-22 17:02:40 +00003746
3747/* The file "crypto_extra.h" contains vendor-specific definitions. This
3748 * can include vendor-defined algorithms, extra functions, etc. */
Jamie Foxcc31d402019-01-28 17:13:52 +00003749#include "psa/crypto_extra.h"
Antonio de Angelis8908f472018-08-31 15:44:25 +01003750
Antonio de Angelis14276e92018-07-10 14:35:43 +01003751#endif /* PSA_CRYPTO_H */