blob: 6b06187bfa7ca3ea0d435eab091e36b5b6dab8d5 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02006 * Copyright The Mbed TLS Contributors
Jaeden Amerocab54942018-07-25 13:26:13 +01007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
Gilles Peskineb1176f22023-02-22 22:07:28 +010025#if defined(MBEDTLS_PSA_CRYPTO_PLATFORM_FILE)
26#include MBEDTLS_PSA_CRYPTO_PLATFORM_FILE
27#else
Gilles Peskinee59236f2018-01-27 23:32:46 +010028#include "crypto_platform.h"
Gilles Peskineb1176f22023-02-22 22:07:28 +010029#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010030
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010031#include <stddef.h>
32
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010033#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010034/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
35 * must be defined in the crypto_platform.h header. These mock definitions
36 * are present in this file as a convenience to generate pretty-printed
37 * documentation that includes those definitions. */
38
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010039/** \defgroup platform Implementation-specific definitions
40 * @{
41 */
42
43/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010044#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010045
Gilles Peskinee59236f2018-01-27 23:32:46 +010046#ifdef __cplusplus
47extern "C" {
48#endif
49
Gilles Peskinef3b731e2018-12-12 13:38:31 +010050/* The file "crypto_types.h" declares types that encode errors,
51 * algorithms, key types, policies, etc. */
52#include "crypto_types.h"
53
Andrew Thoelke02b372b2019-10-02 09:32:21 +010054/** \defgroup version API version
Adrian L. Shawd89338a2019-09-19 13:32:57 +010055 * @{
56 */
57
58/**
59 * The major version of this implementation of the PSA Crypto API
60 */
61#define PSA_CRYPTO_API_VERSION_MAJOR 1
62
63/**
64 * The minor version of this implementation of the PSA Crypto API
65 */
66#define PSA_CRYPTO_API_VERSION_MINOR 0
67
68/**@}*/
69
Gilles Peskinef3b731e2018-12-12 13:38:31 +010070/* The file "crypto_values.h" declares macros to build and analyze values
71 * of integral types defined in "crypto_types.h". */
72#include "crypto_values.h"
73
74/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010075 * @{
76 */
77
78/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010079 * \brief Library initialization.
80 *
81 * Applications must call this function before calling any other
82 * function in this module.
83 *
84 * Applications may call this function more than once. Once a call
85 * succeeds, subsequent calls are guaranteed to succeed.
86 *
itayzafrir18617092018-09-16 12:22:41 +030087 * If the application calls other functions before calling psa_crypto_init(),
88 * the behavior is undefined. Implementations are encouraged to either perform
89 * the operation as if the library had been initialized or to return
90 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
91 * implementations should not return a success status if the lack of
92 * initialization may have security implications, for example due to improper
93 * seeding of the random number generator.
94 *
Gilles Peskineed733552023-02-14 19:21:09 +010095 * \retval #PSA_SUCCESS \emptydescription
96 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
97 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
98 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
99 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
100 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
101 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
102 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
103 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
104 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
Gilles Peskinee59236f2018-01-27 23:32:46 +0100105 */
106psa_status_t psa_crypto_init(void);
107
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100108/**@}*/
109
Gilles Peskine105f67f2019-07-23 18:16:05 +0200110/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +0200111 * @{
112 */
113
Gilles Peskinea0c06552019-05-21 15:54:54 +0200114/** \def PSA_KEY_ATTRIBUTES_INIT
115 *
116 * This macro returns a suitable initializer for a key attribute structure
117 * of type #psa_key_attributes_t.
118 */
Gilles Peskinea0c06552019-05-21 15:54:54 +0200119
120/** Return an initial value for a key attributes structure.
121 */
122static psa_key_attributes_t psa_key_attributes_init(void);
123
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200124/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200125 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200126 * If the attribute structure currently declares the key as volatile (which
127 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200128 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200129 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200130 * This function does not access storage, it merely stores the given
131 * value in the structure.
132 * The persistent key will be written to storage when the attribute
133 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200134 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200135 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200136 *
Gilles Peskine20628592019-04-19 19:29:50 +0200137 * This function may be declared as `static` (i.e. without external
138 * linkage). This function may be provided as a function-like macro,
139 * but in this case it must evaluate each of its arguments exactly once.
140 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200141 * \param[out] attributes The attribute structure to write to.
142 * \param key The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200143 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100144static void psa_set_key_id(psa_key_attributes_t *attributes,
145 mbedtls_svc_key_id_t key);
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200146
Ronald Cron6b5ff532020-10-16 14:38:19 +0200147#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
148/** Set the owner identifier of a key.
149 *
150 * When key identifiers encode key owner identifiers, psa_set_key_id() does
151 * not allow to define in key attributes the owner of volatile keys as
152 * psa_set_key_id() enforces the key to be persistent.
153 *
154 * This function allows to set in key attributes the owner identifier of a
155 * key. It is intended to be used for volatile keys. For persistent keys,
156 * it is recommended to use the PSA Cryptography API psa_set_key_id() to define
157 * the owner of a key.
158 *
159 * \param[out] attributes The attribute structure to write to.
Antonio de Angelise2b68662021-11-30 12:21:44 +0000160 * \param owner The key owner identifier.
Ronald Cron6b5ff532020-10-16 14:38:19 +0200161 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162static void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,
163 mbedtls_key_owner_id_t owner);
Ronald Cron6b5ff532020-10-16 14:38:19 +0200164#endif
165
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200166/** Set the location of a persistent key.
167 *
168 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200169 * with psa_set_key_id(). By default, a key that has a persistent identifier
170 * is stored in the default storage area identifier by
171 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
172 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200173 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200174 * This function does not access storage, it merely stores the given
175 * value in the structure.
176 * The persistent key will be written to storage when the attribute
177 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200178 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200179 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200180 *
181 * 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 each of its arguments exactly once.
184 *
185 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200186 * \param lifetime The lifetime for the key.
187 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200188 * key will be volatile, and the key identifier
189 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200190 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200191static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
192 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200193
Gilles Peskine20628592019-04-19 19:29:50 +0200194/** Retrieve the key identifier from key attributes.
195 *
196 * This function may be declared as `static` (i.e. without external
197 * linkage). This function may be provided as a function-like macro,
198 * but in this case it must evaluate its argument exactly once.
199 *
200 * \param[in] attributes The key attribute structure to query.
201 *
202 * \return The persistent identifier stored in the attribute structure.
203 * This value is unspecified if the attribute structure declares
204 * the key as volatile.
205 */
Ronald Cron71016a92020-08-28 19:01:50 +0200206static mbedtls_svc_key_id_t psa_get_key_id(
207 const psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200208
Gilles Peskine20628592019-04-19 19:29:50 +0200209/** Retrieve the lifetime from key attributes.
210 *
211 * This function may be declared as `static` (i.e. without external
212 * linkage). This function may be provided as a function-like macro,
213 * but in this case it must evaluate its argument exactly once.
214 *
215 * \param[in] attributes The key attribute structure to query.
216 *
217 * \return The lifetime value stored in the attribute structure.
218 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200219static psa_key_lifetime_t psa_get_key_lifetime(
220 const psa_key_attributes_t *attributes);
221
Gilles Peskine20628592019-04-19 19:29:50 +0200222/** Declare usage flags for a key.
223 *
224 * Usage flags are part of a key's usage policy. They encode what
225 * kind of operations are permitted on the key. For more details,
226 * refer to the documentation of the type #psa_key_usage_t.
227 *
228 * This function overwrites any usage flags
229 * previously set in \p attributes.
230 *
231 * This function may be declared as `static` (i.e. without external
232 * linkage). This function may be provided as a function-like macro,
233 * but in this case it must evaluate each of its arguments exactly once.
234 *
235 * \param[out] attributes The attribute structure to write to.
236 * \param usage_flags The usage flags to write.
237 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200238static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
239 psa_key_usage_t usage_flags);
240
Gilles Peskine20628592019-04-19 19:29:50 +0200241/** Retrieve the usage flags from key attributes.
242 *
243 * This function may be declared as `static` (i.e. without external
244 * linkage). This function may be provided as a function-like macro,
245 * but in this case it must evaluate its argument exactly once.
246 *
247 * \param[in] attributes The key attribute structure to query.
248 *
249 * \return The usage flags stored in the attribute structure.
250 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200251static psa_key_usage_t psa_get_key_usage_flags(
252 const psa_key_attributes_t *attributes);
253
Gilles Peskine20628592019-04-19 19:29:50 +0200254/** Declare the permitted algorithm policy for a key.
255 *
256 * The permitted algorithm policy of a key encodes which algorithm or
Gilles Peskinea170d922019-09-12 16:59:37 +0200257 * algorithms are permitted to be used with this key. The following
258 * algorithm policies are supported:
259 * - 0 does not allow any cryptographic operation with the key. The key
260 * may be used for non-cryptographic actions such as exporting (if
261 * permitted by the usage flags).
262 * - An algorithm value permits this particular algorithm.
263 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
264 * signature scheme with any hash algorithm.
Steven Cooremancaad4932021-02-18 11:28:17 +0100265 * - An algorithm built from #PSA_ALG_AT_LEAST_THIS_LENGTH_MAC allows
Steven Cooremanee18b1f2021-02-08 11:44:21 +0100266 * any MAC algorithm from the same base class (e.g. CMAC) which
267 * generates/verifies a MAC length greater than or equal to the length
268 * encoded in the wildcard algorithm.
Steven Cooreman5d814812021-02-18 12:11:39 +0100269 * - An algorithm built from #PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG
270 * allows any AEAD algorithm from the same base class (e.g. CCM) which
Steven Cooremanee18b1f2021-02-08 11:44:21 +0100271 * generates/verifies a tag length greater than or equal to the length
272 * encoded in the wildcard algorithm.
Gilles Peskine20628592019-04-19 19:29:50 +0200273 *
274 * This function overwrites any algorithm policy
275 * previously set in \p attributes.
276 *
277 * This function may be declared as `static` (i.e. without external
278 * linkage). This function may be provided as a function-like macro,
279 * but in this case it must evaluate each of its arguments exactly once.
280 *
281 * \param[out] attributes The attribute structure to write to.
282 * \param alg The permitted algorithm policy to write.
283 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200284static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
285 psa_algorithm_t alg);
286
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100287
Gilles Peskine20628592019-04-19 19:29:50 +0200288/** Retrieve the algorithm policy from key attributes.
289 *
290 * This function may be declared as `static` (i.e. without external
291 * linkage). This function may be provided as a function-like macro,
292 * but in this case it must evaluate its argument exactly once.
293 *
294 * \param[in] attributes The key attribute structure to query.
295 *
296 * \return The algorithm stored in the attribute structure.
297 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200298static psa_algorithm_t psa_get_key_algorithm(
299 const psa_key_attributes_t *attributes);
300
Gilles Peskine20628592019-04-19 19:29:50 +0200301/** Declare the type of a key.
302 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200303 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200304 * previously set in \p attributes.
305 *
306 * This function may be declared as `static` (i.e. without external
307 * linkage). This function may be provided as a function-like macro,
308 * but in this case it must evaluate each of its arguments exactly once.
309 *
310 * \param[out] attributes The attribute structure to write to.
311 * \param type The key type to write.
Gilles Peskinea170d922019-09-12 16:59:37 +0200312 * If this is 0, the key type in \p attributes
313 * becomes unspecified.
Gilles Peskine20628592019-04-19 19:29:50 +0200314 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200315static void psa_set_key_type(psa_key_attributes_t *attributes,
316 psa_key_type_t type);
317
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100318
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200319/** Declare the size of a key.
320 *
321 * This function overwrites any key size previously set in \p attributes.
322 *
323 * This function may be declared as `static` (i.e. without external
324 * linkage). This function may be provided as a function-like macro,
325 * but in this case it must evaluate each of its arguments exactly once.
326 *
327 * \param[out] attributes The attribute structure to write to.
328 * \param bits The key size in bits.
Gilles Peskinea170d922019-09-12 16:59:37 +0200329 * If this is 0, the key size in \p attributes
Gilles Peskine05c900b2019-09-12 18:29:43 +0200330 * becomes unspecified. Keys of size 0 are
331 * not supported.
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200332 */
333static void psa_set_key_bits(psa_key_attributes_t *attributes,
334 size_t bits);
335
Gilles Peskine20628592019-04-19 19:29:50 +0200336/** Retrieve the key type from key attributes.
337 *
338 * This function may be declared as `static` (i.e. without external
339 * linkage). This function may be provided as a function-like macro,
340 * but in this case it must evaluate its argument exactly once.
341 *
342 * \param[in] attributes The key attribute structure to query.
343 *
344 * \return The key type stored in the attribute structure.
345 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200346static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
347
Gilles Peskine20628592019-04-19 19:29:50 +0200348/** Retrieve the key size from key attributes.
349 *
350 * This function may be declared as `static` (i.e. without external
351 * linkage). This function may be provided as a function-like macro,
352 * but in this case it must evaluate its argument exactly once.
353 *
354 * \param[in] attributes The key attribute structure to query.
355 *
356 * \return The key size stored in the attribute structure, in bits.
357 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200358static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
359
Gilles Peskine20628592019-04-19 19:29:50 +0200360/** Retrieve the attributes of a key.
361 *
362 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200363 * psa_reset_key_attributes(). It then copies the attributes of
364 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200365 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200366 * \note This function may allocate memory or other resources.
367 * Once you have called this function on an attribute structure,
368 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200369 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200370 * \param[in] key Identifier of the key to query.
Gilles Peskine20628592019-04-19 19:29:50 +0200371 * \param[in,out] attributes On success, the attributes of the key.
372 * On failure, equivalent to a
373 * freshly-initialized structure.
374 *
Gilles Peskineed733552023-02-14 19:21:09 +0100375 * \retval #PSA_SUCCESS \emptydescription
376 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
377 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
378 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
379 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
380 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
381 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
382 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100383 * \retval #PSA_ERROR_BAD_STATE
384 * The library has not been previously initialized by psa_crypto_init().
385 * It is implementation-dependent whether a failure to initialize
386 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200387 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200388psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
Gilles Peskine4747d192019-04-17 15:05:45 +0200389 psa_key_attributes_t *attributes);
390
Gilles Peskine20628592019-04-19 19:29:50 +0200391/** Reset a key attribute structure to a freshly initialized state.
392 *
393 * You must initialize the attribute structure as described in the
394 * documentation of the type #psa_key_attributes_t before calling this
395 * function. Once the structure has been initialized, you may call this
396 * function at any time.
397 *
398 * This function frees any auxiliary resources that the structure
399 * may contain.
400 *
401 * \param[in,out] attributes The attribute structure to reset.
402 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200403void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200404
Gilles Peskine87a5e562019-04-17 12:28:25 +0200405/**@}*/
406
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100407/** \defgroup key_management Key management
408 * @{
409 */
410
Ronald Cron277a85f2020-08-04 15:49:48 +0200411/** Remove non-essential copies of key material from memory.
412 *
413 * If the key identifier designates a volatile key, this functions does not do
414 * anything and returns successfully.
415 *
416 * If the key identifier designates a persistent key, then this function will
417 * free all resources associated with the key in volatile memory. The key
418 * data in persistent storage is not affected and the key can still be used.
419 *
420 * \param key Identifier of the key to purge.
421 *
422 * \retval #PSA_SUCCESS
423 * The key material will have been removed from memory if it is not
424 * currently required.
425 * \retval #PSA_ERROR_INVALID_ARGUMENT
426 * \p key is not a valid key identifier.
427 * \retval #PSA_ERROR_BAD_STATE
428 * The library has not been previously initialized by psa_crypto_init().
429 * It is implementation-dependent whether a failure to initialize
430 * results in this error code.
431 */
432psa_status_t psa_purge_key(mbedtls_svc_key_id_t key);
433
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100434/** Make a copy of a key.
435 *
436 * Copy key material from one location to another.
437 *
438 * This function is primarily useful to copy a key from one location
439 * to another, since it populates a key using the material from
440 * another key which may have a different lifetime.
441 *
442 * This function may be used to share a key with a different party,
443 * subject to implementation-defined restrictions on key sharing.
444 *
445 * The policy on the source key must have the usage flag
446 * #PSA_KEY_USAGE_COPY set.
447 * This flag is sufficient to permit the copy if the key has the lifetime
448 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
449 * Some secure elements do not provide a way to copy a key without
450 * making it extractable from the secure element. If a key is located
451 * in such a secure element, then the key must have both usage flags
452 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
453 * a copy of the key outside the secure element.
454 *
455 * The resulting key may only be used in a way that conforms to
456 * both the policy of the original key and the policy specified in
457 * the \p attributes parameter:
458 * - The usage flags on the resulting key are the bitwise-and of the
459 * usage flags on the source policy and the usage flags in \p attributes.
460 * - If both allow the same algorithm or wildcard-based
461 * algorithm policy, the resulting key has the same algorithm policy.
462 * - If either of the policies allows an algorithm and the other policy
463 * allows a wildcard-based algorithm policy that includes this algorithm,
464 * the resulting key allows the same algorithm.
465 * - If the policies do not allow any algorithm in common, this function
466 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
467 *
468 * The effect of this function on implementation-defined attributes is
469 * implementation-defined.
470 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200471 * \param source_key The key to copy. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +0200472 * #PSA_KEY_USAGE_COPY. If a private or secret key is
Ronald Croncf56a0a2020-08-04 09:51:30 +0200473 * being copied outside of a secure element it must
Ronald Cron96783552020-10-19 12:06:30 +0200474 * also allow #PSA_KEY_USAGE_EXPORT.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100475 * \param[in] attributes The attributes for the new key.
476 * They are used as follows:
477 * - The key type and size may be 0. If either is
478 * nonzero, it must match the corresponding
479 * attribute of the source key.
480 * - The key location (the lifetime and, for
481 * persistent keys, the key identifier) is
482 * used directly.
483 * - The policy constraints (usage flags and
484 * algorithm policy) are combined from
485 * the source key and \p attributes so that
486 * both sets of restrictions apply, as
487 * described in the documentation of this function.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200488 * \param[out] target_key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +0200489 * key. For persistent keys, this is the key
490 * identifier defined in \p attributes.
491 * \c 0 on failure.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100492 *
Gilles Peskineed733552023-02-14 19:21:09 +0100493 * \retval #PSA_SUCCESS \emptydescription
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100494 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200495 * \p source_key is invalid.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100496 * \retval #PSA_ERROR_ALREADY_EXISTS
497 * This is an attempt to create a persistent key, and there is
498 * already a persistent key with the given identifier.
499 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500500 * The lifetime or identifier in \p attributes are invalid, or
501 * the policy constraints on the source and specified in
502 * \p attributes are incompatible, or
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100503 * \p attributes specifies a key type or key size
504 * which does not match the attributes of the source key.
505 * \retval #PSA_ERROR_NOT_PERMITTED
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500506 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag, or
507 * the source key is not exportable and its lifetime does not
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100508 * allow copying it to the target's lifetime.
Gilles Peskineed733552023-02-14 19:21:09 +0100509 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
510 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
511 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
512 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
513 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
514 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
515 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
516 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100517 * \retval #PSA_ERROR_BAD_STATE
518 * The library has not been previously initialized by psa_crypto_init().
519 * It is implementation-dependent whether a failure to initialize
520 * results in this error code.
521 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200522psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100523 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200524 mbedtls_svc_key_id_t *target_key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100525
526
527/**
528 * \brief Destroy a key.
529 *
530 * This function destroys a key from both volatile
531 * memory and, if applicable, non-volatile storage. Implementations shall
Tom Cosgrove1797b052022-12-04 17:19:59 +0000532 * make a best effort to ensure that the key material cannot be recovered.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100533 *
534 * This function also erases any metadata such as policies and frees
Ronald Croncf56a0a2020-08-04 09:51:30 +0200535 * resources associated with the key.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100536 *
537 * If a key is currently in use in a multipart operation, then destroying the
538 * key will cause the multipart operation to fail.
539 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200540 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
Ronald Cron96783552020-10-19 12:06:30 +0200541 * return #PSA_SUCCESS.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100542 *
543 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +0200544 * \p key was a valid identifier and the key material that it
545 * referred to has been erased. Alternatively, \p key is \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100546 * \retval #PSA_ERROR_NOT_PERMITTED
547 * The key cannot be erased because it is
548 * read-only, either due to a policy or due to physical restrictions.
549 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200550 * \p key is not a valid identifier nor \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100551 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100552 * There was a failure in communication with the cryptoprocessor.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100553 * The key material may still be present in the cryptoprocessor.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100554 * \retval #PSA_ERROR_DATA_INVALID
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100555 * This error is typically a result of either storage corruption on a
556 * cleartext storage backend, or an attempt to read data that was
557 * written by an incompatible version of the library.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100558 * \retval #PSA_ERROR_STORAGE_FAILURE
559 * The storage is corrupted. Implementations shall make a best effort
560 * to erase key material even in this stage, however applications
561 * should be aware that it may be impossible to guarantee that the
562 * key material is not recoverable in such cases.
563 * \retval #PSA_ERROR_CORRUPTION_DETECTED
564 * An unexpected condition which is not a storage corruption or
565 * a communication failure occurred. The cryptoprocessor may have
566 * been compromised.
567 * \retval #PSA_ERROR_BAD_STATE
568 * The library has not been previously initialized by psa_crypto_init().
569 * It is implementation-dependent whether a failure to initialize
570 * results in this error code.
571 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200572psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100573
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100574/**@}*/
575
576/** \defgroup import_export Key import and export
577 * @{
578 */
579
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100580/**
581 * \brief Import a key in binary format.
582 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100583 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100584 * documentation of psa_export_public_key() for the format of public keys
585 * and to the documentation of psa_export_key() for the format for
586 * other key types.
587 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200588 * The key data determines the key size. The attributes may optionally
589 * specify a key size; in this case it must match the size determined
590 * from the key data. A key size of 0 in \p attributes indicates that
591 * the key size is solely determined by the key data.
592 *
593 * Implementations must reject an attempt to import a key of size 0.
594 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100595 * This specification supports a single format for each key type.
596 * Implementations may support other formats as long as the standard
597 * format is supported. Implementations that support other formats
598 * should ensure that the formats are clearly unambiguous so as to
599 * minimize the risk that an invalid input is accidentally interpreted
600 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100601 *
Gilles Peskine20628592019-04-19 19:29:50 +0200602 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200603 * The key size is always determined from the
604 * \p data buffer.
605 * If the key size in \p attributes is nonzero,
606 * it must be equal to the size from \p data.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200607 * \param[out] key On success, an identifier to the newly created key.
Ronald Cron4067d1c2020-10-19 13:34:38 +0200608 * For persistent keys, this is the key identifier
609 * defined in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200610 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100611 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200612 * buffer is interpreted according to the type declared
613 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200614 * All implementations must support at least the format
615 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100616 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200617 * the chosen type. Implementations may allow other
618 * formats, but should be conservative: implementations
619 * should err on the side of rejecting content if it
620 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200621 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100622 *
Gilles Peskine28538492018-07-11 17:34:00 +0200623 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100624 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100625 * If the key is persistent, the key material and the key's metadata
626 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200627 * \retval #PSA_ERROR_ALREADY_EXISTS
628 * This is an attempt to create a persistent key, and there is
629 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200630 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200631 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200632 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200633 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500634 * The key attributes, as a whole, are invalid, or
635 * the key data is not correctly formatted, or
636 * the size in \p attributes is nonzero and does not match the size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200637 * of the key data.
Gilles Peskineed733552023-02-14 19:21:09 +0100638 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
639 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
640 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
641 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
642 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
643 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
644 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
645 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +0300646 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300647 * The library has not been previously initialized by psa_crypto_init().
648 * It is implementation-dependent whether a failure to initialize
649 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100650 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200651psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100652 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200653 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200654 mbedtls_svc_key_id_t *key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100655
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100656
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100657
658/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100659 * \brief Export a key in binary format.
660 *
661 * The output of this function can be passed to psa_import_key() to
662 * create an equivalent object.
663 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100664 * If the implementation of psa_import_key() supports other formats
665 * beyond the format specified here, the output from psa_export_key()
666 * must use the representation specified here, not the original
667 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100668 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100669 * For standard key types, the output format is as follows:
670 *
671 * - For symmetric keys (including MAC keys), the format is the
672 * raw bytes of the key.
673 * - For DES, the key data consists of 8 bytes. The parity bits must be
674 * correct.
675 * - For Triple-DES, the format is the concatenation of the
676 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200677 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200678 * is the non-encrypted DER encoding of the representation defined by
679 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
680 * ```
681 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200682 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200683 * modulus INTEGER, -- n
684 * publicExponent INTEGER, -- e
685 * privateExponent INTEGER, -- d
686 * prime1 INTEGER, -- p
687 * prime2 INTEGER, -- q
688 * exponent1 INTEGER, -- d mod (p-1)
689 * exponent2 INTEGER, -- d mod (q-1)
690 * coefficient INTEGER, -- (inverse of q) mod p
691 * }
692 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200693 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200694 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100695 * a representation of the private value as a `ceiling(m/8)`-byte string
696 * where `m` is the bit size associated with the curve, i.e. the bit size
697 * of the order of the curve's coordinate field. This byte string is
698 * in little-endian order for Montgomery curves (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +0100699 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
700 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
701 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
Steven Cooreman6f5cc712020-06-11 16:40:41 +0200702 * For Weierstrass curves, this is the content of the `privateKey` field of
703 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
704 * the format is defined by RFC 7748, and output is masked according to §5.
Gilles Peskine67546802021-02-24 21:49:40 +0100705 * For twisted Edwards curves, the private key is as defined by RFC 8032
706 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200707 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200708 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000709 * format is the representation of the private key `x` as a big-endian byte
710 * string. The length of the byte string is the private key size in bytes
711 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200712 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
713 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100714 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200715 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
716 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200717 * \param key Identifier of the key to export. It must allow the
Ronald Cron96783552020-10-19 12:06:30 +0200718 * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
Ronald Croncf56a0a2020-08-04 09:51:30 +0200719 * key.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200720 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200721 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200722 * \param[out] data_length On success, the number of bytes
723 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100724 *
Gilles Peskineed733552023-02-14 19:21:09 +0100725 * \retval #PSA_SUCCESS \emptydescription
726 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +0200727 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200728 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Gilles Peskineed733552023-02-14 19:21:09 +0100729 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Gilles Peskine1be949b2018-08-10 19:06:59 +0200730 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
731 * The size of the \p data buffer is too small. You can determine a
732 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100733 * #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200734 * where \c type is the key type
735 * and \c bits is the key size in bits.
Gilles Peskineed733552023-02-14 19:21:09 +0100736 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
737 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
738 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
739 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
740 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +0300741 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300742 * The library has not been previously initialized by psa_crypto_init().
743 * It is implementation-dependent whether a failure to initialize
744 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100745 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200746psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100747 uint8_t *data,
748 size_t data_size,
749 size_t *data_length);
750
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100751/**
752 * \brief Export a public key or the public part of a key pair in binary format.
753 *
754 * The output of this function can be passed to psa_import_key() to
755 * create an object that is equivalent to the public key.
756 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000757 * This specification supports a single format for each key type.
758 * Implementations may support other formats as long as the standard
759 * format is supported. Implementations that support other formats
760 * should ensure that the formats are clearly unambiguous so as to
761 * minimize the risk that an invalid input is accidentally interpreted
762 * according to a different format.
763 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000764 * For standard key types, the output format is as follows:
765 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
766 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
767 * ```
768 * RSAPublicKey ::= SEQUENCE {
769 * modulus INTEGER, -- n
770 * publicExponent INTEGER } -- e
771 * ```
Gilles Peskine67546802021-02-24 21:49:40 +0100772 * - For elliptic curve keys on a twisted Edwards curve (key types for which
Bence Szépkúti3b1cba82021-04-08 15:49:07 +0200773 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true and #PSA_KEY_TYPE_ECC_GET_FAMILY
Gilles Peskine67546802021-02-24 21:49:40 +0100774 * returns #PSA_ECC_FAMILY_TWISTED_EDWARDS), the public key is as defined
775 * by RFC 8032
776 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
777 * - For other elliptic curve public keys (key types for which
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000778 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
779 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
780 * Let `m` be the bit size associated with the curve, i.e. the bit size of
781 * `q` for a curve over `F_q`. The representation consists of:
782 * - The byte 0x04;
783 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
784 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200785 * - For Diffie-Hellman key exchange public keys (key types for which
786 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000787 * the format is the representation of the public key `y = g^x mod p` as a
788 * big-endian byte string. The length of the byte string is the length of the
789 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100790 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200791 * Exporting a public key object or the public part of a key pair is
792 * always permitted, regardless of the key's usage flags.
793 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200794 * \param key Identifier of the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200795 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200796 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200797 * \param[out] data_length On success, the number of bytes
798 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100799 *
Gilles Peskineed733552023-02-14 19:21:09 +0100800 * \retval #PSA_SUCCESS \emptydescription
801 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +0200802 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200803 * The key is neither a public key nor a key pair.
Gilles Peskineed733552023-02-14 19:21:09 +0100804 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Gilles Peskine1be949b2018-08-10 19:06:59 +0200805 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
806 * The size of the \p data buffer is too small. You can determine a
807 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100808 * #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200809 * where \c type is the key type
810 * and \c bits is the key size in bits.
Gilles Peskineed733552023-02-14 19:21:09 +0100811 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
812 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
813 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
814 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
815 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +0300816 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300817 * The library has not been previously initialized by psa_crypto_init().
818 * It is implementation-dependent whether a failure to initialize
819 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100820 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200821psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100822 uint8_t *data,
823 size_t data_size,
824 size_t *data_length);
825
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100826
Gilles Peskine20035e32018-02-03 22:44:14 +0100827
828/**@}*/
829
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100830/** \defgroup hash Message digests
831 * @{
832 */
833
Gilles Peskine69647a42019-01-14 20:18:12 +0100834/** Calculate the hash (digest) of a message.
835 *
836 * \note To verify the hash of a message against an
837 * expected value, use psa_hash_compare() instead.
838 *
839 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
840 * such that #PSA_ALG_IS_HASH(\p alg) is true).
841 * \param[in] input Buffer containing the message to hash.
842 * \param input_length Size of the \p input buffer in bytes.
843 * \param[out] hash Buffer where the hash is to be written.
844 * \param hash_size Size of the \p hash buffer in bytes.
845 * \param[out] hash_length On success, the number of bytes
846 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100847 * #PSA_HASH_LENGTH(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100848 *
849 * \retval #PSA_SUCCESS
850 * Success.
851 * \retval #PSA_ERROR_NOT_SUPPORTED
852 * \p alg is not supported or is not a hash algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +0100853 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100854 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
855 * \p hash_size is too small
Gilles Peskineed733552023-02-14 19:21:09 +0100856 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
857 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
858 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
859 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100860 * \retval #PSA_ERROR_BAD_STATE
861 * The library has not been previously initialized by psa_crypto_init().
862 * It is implementation-dependent whether a failure to initialize
863 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100864 */
865psa_status_t psa_hash_compute(psa_algorithm_t alg,
866 const uint8_t *input,
867 size_t input_length,
868 uint8_t *hash,
869 size_t hash_size,
870 size_t *hash_length);
871
872/** Calculate the hash (digest) of a message and compare it with a
873 * reference value.
874 *
875 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
876 * such that #PSA_ALG_IS_HASH(\p alg) is true).
877 * \param[in] input Buffer containing the message to hash.
878 * \param input_length Size of the \p input buffer in bytes.
879 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100880 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100881 *
882 * \retval #PSA_SUCCESS
883 * The expected hash is identical to the actual hash of the input.
884 * \retval #PSA_ERROR_INVALID_SIGNATURE
885 * The hash of the message was calculated successfully, but it
886 * differs from the expected hash.
887 * \retval #PSA_ERROR_NOT_SUPPORTED
888 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100889 * \retval #PSA_ERROR_INVALID_ARGUMENT
890 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskineed733552023-02-14 19:21:09 +0100891 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
892 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
893 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
894 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100895 * \retval #PSA_ERROR_BAD_STATE
896 * The library has not been previously initialized by psa_crypto_init().
897 * It is implementation-dependent whether a failure to initialize
898 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100899 */
900psa_status_t psa_hash_compare(psa_algorithm_t alg,
901 const uint8_t *input,
902 size_t input_length,
903 const uint8_t *hash,
Gilles Peskinefa710f52019-12-02 14:31:48 +0100904 size_t hash_length);
Gilles Peskine69647a42019-01-14 20:18:12 +0100905
Gilles Peskine308b91d2018-02-08 09:47:44 +0100906/** The type of the state data structure for multipart hash operations.
907 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000908 * Before calling any function on a hash operation object, the application must
909 * initialize it by any of the following means:
910 * - Set the structure to all-bits-zero, for example:
911 * \code
912 * psa_hash_operation_t operation;
913 * memset(&operation, 0, sizeof(operation));
914 * \endcode
915 * - Initialize the structure to logical zero values, for example:
916 * \code
917 * psa_hash_operation_t operation = {0};
918 * \endcode
919 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
920 * for example:
921 * \code
922 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
923 * \endcode
924 * - Assign the result of the function psa_hash_operation_init()
925 * to the structure, for example:
926 * \code
927 * psa_hash_operation_t operation;
928 * operation = psa_hash_operation_init();
929 * \endcode
930 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100931 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +0100932 * make any assumptions about the content of this structure.
933 * Implementation details can change in future versions without notice. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100934typedef struct psa_hash_operation_s psa_hash_operation_t;
935
Jaeden Amero6a25b412019-01-04 11:47:44 +0000936/** \def PSA_HASH_OPERATION_INIT
937 *
938 * This macro returns a suitable initializer for a hash operation object
939 * of type #psa_hash_operation_t.
940 */
Jaeden Amero6a25b412019-01-04 11:47:44 +0000941
942/** Return an initial value for a hash operation object.
943 */
944static psa_hash_operation_t psa_hash_operation_init(void);
945
Gilles Peskinef45adda2019-01-14 18:29:18 +0100946/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100947 *
948 * The sequence of operations to calculate a hash (message digest)
949 * is as follows:
950 * -# Allocate an operation object which will be passed to all the functions
951 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000952 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100953 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200954 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100955 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100956 * of the message each time. The hash that is calculated is the hash
957 * of the concatenation of these messages in order.
958 * -# To calculate the hash, call psa_hash_finish().
959 * To compare the hash with an expected value, call psa_hash_verify().
960 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100961 * If an error occurs at any step after a call to psa_hash_setup(), the
962 * operation will need to be reset by a call to psa_hash_abort(). The
963 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000964 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100965 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200966 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100967 * eventually terminate the operation. The following events terminate an
968 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100969 * - A successful call to psa_hash_finish() or psa_hash_verify().
970 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100971 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000972 * \param[in,out] operation The operation object to set up. It must have
973 * been initialized as per the documentation for
974 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200975 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
976 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100977 *
Gilles Peskine28538492018-07-11 17:34:00 +0200978 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100979 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200980 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100981 * \p alg is not a supported hash algorithm.
982 * \retval #PSA_ERROR_INVALID_ARGUMENT
983 * \p alg is not a hash algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +0100984 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
985 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
986 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
987 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100988 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500989 * The operation state is not valid (it must be inactive), or
990 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100991 * It is implementation-dependent whether a failure to initialize
992 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100993 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200994psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100995 psa_algorithm_t alg);
996
Gilles Peskine308b91d2018-02-08 09:47:44 +0100997/** Add a message fragment to a multipart hash operation.
998 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200999 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001000 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001001 * If this function returns an error status, the operation enters an error
1002 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001003 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001004 * \param[in,out] operation Active hash operation.
1005 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001006 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001007 *
Gilles Peskine28538492018-07-11 17:34:00 +02001008 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001009 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001010 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1011 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1012 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1013 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001014 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001015 * The operation state is not valid (it must be active), or
1016 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001017 * It is implementation-dependent whether a failure to initialize
1018 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001019 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001020psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1021 const uint8_t *input,
1022 size_t input_length);
1023
Gilles Peskine308b91d2018-02-08 09:47:44 +01001024/** Finish the calculation of the hash of a message.
1025 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001026 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001027 * This function calculates the hash of the message formed by concatenating
1028 * the inputs passed to preceding calls to psa_hash_update().
1029 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001030 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001031 * If this function returns an error status, the operation enters an error
1032 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001033 *
1034 * \warning Applications should not call this function if they expect
1035 * a specific value for the hash. Call psa_hash_verify() instead.
1036 * Beware that comparing integrity or authenticity data such as
1037 * hash values with a function such as \c memcmp is risky
1038 * because the time taken by the comparison may leak information
1039 * about the hashed data which could allow an attacker to guess
1040 * a valid hash and thereby bypass security controls.
1041 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001042 * \param[in,out] operation Active hash operation.
1043 * \param[out] hash Buffer where the hash is to be written.
1044 * \param hash_size Size of the \p hash buffer in bytes.
1045 * \param[out] hash_length On success, the number of bytes
1046 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001047 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001048 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001049 *
Gilles Peskine28538492018-07-11 17:34:00 +02001050 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001051 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001052 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001053 * The size of the \p hash buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001054 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001055 * where \c alg is the hash algorithm that is calculated.
Gilles Peskineed733552023-02-14 19:21:09 +01001056 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1057 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1058 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1059 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001060 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001061 * The operation state is not valid (it must be active), or
1062 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001063 * It is implementation-dependent whether a failure to initialize
1064 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001065 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001066psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1067 uint8_t *hash,
1068 size_t hash_size,
1069 size_t *hash_length);
1070
Gilles Peskine308b91d2018-02-08 09:47:44 +01001071/** Finish the calculation of the hash of a message and compare it with
1072 * an expected value.
1073 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001074 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001075 * This function calculates the hash of the message formed by concatenating
1076 * the inputs passed to preceding calls to psa_hash_update(). It then
1077 * compares the calculated hash with the expected hash passed as a
1078 * parameter to this function.
1079 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001080 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001081 * If this function returns an error status, the operation enters an error
1082 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001083 *
Gilles Peskine19067982018-03-20 17:54:53 +01001084 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001085 * comparison between the actual hash and the expected hash is performed
1086 * in constant time.
1087 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001088 * \param[in,out] operation Active hash operation.
1089 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001090 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001091 *
Gilles Peskine28538492018-07-11 17:34:00 +02001092 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001093 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001094 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001095 * The hash of the message was calculated successfully, but it
1096 * differs from the expected hash.
Gilles Peskineed733552023-02-14 19:21:09 +01001097 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1098 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1099 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1100 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001101 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001102 * The operation state is not valid (it must be active), or
1103 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001104 * It is implementation-dependent whether a failure to initialize
1105 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001106 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001107psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1108 const uint8_t *hash,
1109 size_t hash_length);
1110
Gilles Peskine308b91d2018-02-08 09:47:44 +01001111/** Abort a hash operation.
1112 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001113 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001114 * \p operation structure itself. Once aborted, the operation object
1115 * can be reused for another operation by calling
1116 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001117 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001118 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001119 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001120 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001121 * In particular, calling psa_hash_abort() after the operation has been
1122 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1123 * psa_hash_verify() is safe and has no effect.
1124 *
1125 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001126 *
Gilles Peskineed733552023-02-14 19:21:09 +01001127 * \retval #PSA_SUCCESS \emptydescription
1128 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1129 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1130 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001131 * \retval #PSA_ERROR_BAD_STATE
1132 * The library has not been previously initialized by psa_crypto_init().
1133 * It is implementation-dependent whether a failure to initialize
1134 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001135 */
1136psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001137
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001138/** Clone a hash operation.
1139 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001140 * This function copies the state of an ongoing hash operation to
1141 * a new operation object. In other words, this function is equivalent
1142 * to calling psa_hash_setup() on \p target_operation with the same
1143 * algorithm that \p source_operation was set up for, then
1144 * psa_hash_update() on \p target_operation with the same input that
1145 * that was passed to \p source_operation. After this function returns, the
1146 * two objects are independent, i.e. subsequent calls involving one of
1147 * the objects do not affect the other object.
1148 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001149 * \param[in] source_operation The active hash operation to clone.
1150 * \param[in,out] target_operation The operation object to set up.
1151 * It must be initialized but not active.
1152 *
Gilles Peskineed733552023-02-14 19:21:09 +01001153 * \retval #PSA_SUCCESS \emptydescription
1154 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1155 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1156 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1157 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001158 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001159 * The \p source_operation state is not valid (it must be active), or
1160 * the \p target_operation state is not valid (it must be inactive), or
1161 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001162 * It is implementation-dependent whether a failure to initialize
1163 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001164 */
1165psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1166 psa_hash_operation_t *target_operation);
1167
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001168/**@}*/
1169
Gilles Peskine8c9def32018-02-08 10:02:12 +01001170/** \defgroup MAC Message authentication codes
1171 * @{
1172 */
1173
Gilles Peskine69647a42019-01-14 20:18:12 +01001174/** Calculate the MAC (message authentication code) of a message.
1175 *
1176 * \note To verify the MAC of a message against an
1177 * expected value, use psa_mac_verify() instead.
1178 * Beware that comparing integrity or authenticity data such as
1179 * MAC values with a function such as \c memcmp is risky
1180 * because the time taken by the comparison may leak information
1181 * about the MAC value which could allow an attacker to guess
1182 * a valid MAC and thereby bypass security controls.
1183 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001184 * \param key Identifier of the key to use for the operation. It
1185 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001186 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001187 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001188 * \param[in] input Buffer containing the input message.
1189 * \param input_length Size of the \p input buffer in bytes.
1190 * \param[out] mac Buffer where the MAC value is to be written.
1191 * \param mac_size Size of the \p mac buffer in bytes.
1192 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001193 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001194 *
1195 * \retval #PSA_SUCCESS
1196 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001197 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1198 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001199 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001200 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001201 * \retval #PSA_ERROR_NOT_SUPPORTED
1202 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001203 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1204 * \p mac_size is too small
Gilles Peskineed733552023-02-14 19:21:09 +01001205 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1206 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1207 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1208 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001209 * \retval #PSA_ERROR_STORAGE_FAILURE
1210 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001211 * \retval #PSA_ERROR_BAD_STATE
1212 * The library has not been previously initialized by psa_crypto_init().
1213 * It is implementation-dependent whether a failure to initialize
1214 * results in this error code.
1215 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001216psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001217 psa_algorithm_t alg,
1218 const uint8_t *input,
1219 size_t input_length,
1220 uint8_t *mac,
1221 size_t mac_size,
1222 size_t *mac_length);
1223
1224/** Calculate the MAC of a message and compare it with a reference value.
1225 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001226 * \param key Identifier of the key to use for the operation. It
1227 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001228 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001229 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001230 * \param[in] input Buffer containing the input message.
1231 * \param input_length Size of the \p input buffer in bytes.
1232 * \param[out] mac Buffer containing the expected MAC value.
1233 * \param mac_length Size of the \p mac buffer in bytes.
1234 *
1235 * \retval #PSA_SUCCESS
1236 * The expected MAC is identical to the actual MAC of the input.
1237 * \retval #PSA_ERROR_INVALID_SIGNATURE
1238 * The MAC of the message was calculated successfully, but it
1239 * differs from the expected value.
Gilles Peskineed733552023-02-14 19:21:09 +01001240 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1241 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001242 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001243 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001244 * \retval #PSA_ERROR_NOT_SUPPORTED
1245 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001246 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1247 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1248 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1249 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001250 * \retval #PSA_ERROR_STORAGE_FAILURE
1251 * The key could not be retrieved from storage.
1252 * \retval #PSA_ERROR_BAD_STATE
1253 * The library has not been previously initialized by psa_crypto_init().
1254 * It is implementation-dependent whether a failure to initialize
1255 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001256 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001257psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
Gilles Peskinea05602d2019-01-17 15:25:52 +01001258 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001259 const uint8_t *input,
1260 size_t input_length,
1261 const uint8_t *mac,
Gilles Peskine13faa2d2020-01-30 16:32:21 +01001262 size_t mac_length);
Gilles Peskine69647a42019-01-14 20:18:12 +01001263
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001264/** The type of the state data structure for multipart MAC operations.
1265 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001266 * Before calling any function on a MAC operation object, the application must
1267 * initialize it by any of the following means:
1268 * - Set the structure to all-bits-zero, for example:
1269 * \code
1270 * psa_mac_operation_t operation;
1271 * memset(&operation, 0, sizeof(operation));
1272 * \endcode
1273 * - Initialize the structure to logical zero values, for example:
1274 * \code
1275 * psa_mac_operation_t operation = {0};
1276 * \endcode
1277 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1278 * for example:
1279 * \code
1280 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1281 * \endcode
1282 * - Assign the result of the function psa_mac_operation_init()
1283 * to the structure, for example:
1284 * \code
1285 * psa_mac_operation_t operation;
1286 * operation = psa_mac_operation_init();
1287 * \endcode
1288 *
Janos Follath2ba60792021-04-28 09:37:34 +01001289 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001290 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01001291 * make any assumptions about the content of this structure.
1292 * Implementation details can change in future versions without notice. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001293typedef struct psa_mac_operation_s psa_mac_operation_t;
1294
Jaeden Amero769ce272019-01-04 11:48:03 +00001295/** \def PSA_MAC_OPERATION_INIT
1296 *
1297 * This macro returns a suitable initializer for a MAC operation object of type
1298 * #psa_mac_operation_t.
1299 */
Jaeden Amero769ce272019-01-04 11:48:03 +00001300
1301/** Return an initial value for a MAC operation object.
1302 */
1303static psa_mac_operation_t psa_mac_operation_init(void);
1304
Gilles Peskinef45adda2019-01-14 18:29:18 +01001305/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001306 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001307 * This function sets up the calculation of the MAC
1308 * (message authentication code) of a byte string.
1309 * To verify the MAC of a message against an
1310 * expected value, use psa_mac_verify_setup() instead.
1311 *
1312 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001313 * -# Allocate an operation object which will be passed to all the functions
1314 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001315 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001316 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001317 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001318 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1319 * of the message each time. The MAC that is calculated is the MAC
1320 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001321 * -# At the end of the message, call psa_mac_sign_finish() to finish
1322 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001323 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001324 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1325 * operation will need to be reset by a call to psa_mac_abort(). The
1326 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001327 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001328 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001329 * After a successful call to psa_mac_sign_setup(), the application must
1330 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001331 * - A successful call to psa_mac_sign_finish().
1332 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001333 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001334 * \param[in,out] operation The operation object to set up. It must have
1335 * been initialized as per the documentation for
1336 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001337 * \param key Identifier of the key to use for the operation. It
1338 * must remain valid until the operation terminates.
1339 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001340 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001341 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001342 *
Gilles Peskine28538492018-07-11 17:34:00 +02001343 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001344 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001345 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1346 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001347 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001348 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001349 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001350 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001351 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1352 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1353 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1354 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001355 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001356 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001357 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001358 * The operation state is not valid (it must be inactive), or
1359 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001360 * It is implementation-dependent whether a failure to initialize
1361 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001362 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001363psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001364 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001365 psa_algorithm_t alg);
1366
Gilles Peskinef45adda2019-01-14 18:29:18 +01001367/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001368 *
1369 * This function sets up the verification of the MAC
1370 * (message authentication code) of a byte string against an expected value.
1371 *
1372 * The sequence of operations to verify a MAC is as follows:
1373 * -# Allocate an operation object which will be passed to all the functions
1374 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001375 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001376 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001377 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001378 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1379 * of the message each time. The MAC that is calculated is the MAC
1380 * of the concatenation of these messages in order.
1381 * -# At the end of the message, call psa_mac_verify_finish() to finish
1382 * calculating the actual MAC of the message and verify it against
1383 * the expected value.
1384 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001385 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1386 * operation will need to be reset by a call to psa_mac_abort(). The
1387 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001388 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001389 *
1390 * After a successful call to psa_mac_verify_setup(), the application must
1391 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001392 * - A successful call to psa_mac_verify_finish().
1393 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001394 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001395 * \param[in,out] operation The operation object to set up. It must have
1396 * been initialized as per the documentation for
1397 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001398 * \param key Identifier of the key to use for the operation. It
1399 * must remain valid until the operation terminates.
1400 * It must allow the usage
1401 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001402 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1403 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001404 *
Gilles Peskine28538492018-07-11 17:34:00 +02001405 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001406 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001407 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1408 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001409 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001410 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001411 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001412 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001413 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1414 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1415 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1416 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001417 * \retval #PSA_ERROR_STORAGE_FAILURE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001418 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001419 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001420 * The operation state is not valid (it must be inactive), or
1421 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001422 * It is implementation-dependent whether a failure to initialize
1423 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001424 */
1425psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001426 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001427 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001428
Gilles Peskinedcd14942018-07-12 00:30:52 +02001429/** Add a message fragment to a multipart MAC operation.
1430 *
1431 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1432 * before calling this function.
1433 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001434 * If this function returns an error status, the operation enters an error
1435 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001436 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001437 * \param[in,out] operation Active MAC operation.
1438 * \param[in] input Buffer containing the message fragment to add to
1439 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001440 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001441 *
1442 * \retval #PSA_SUCCESS
1443 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001444 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1445 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1446 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1447 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1448 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001449 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001450 * The operation state is not valid (it must be active), or
1451 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001452 * It is implementation-dependent whether a failure to initialize
1453 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001454 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001455psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1456 const uint8_t *input,
1457 size_t input_length);
1458
Gilles Peskinedcd14942018-07-12 00:30:52 +02001459/** Finish the calculation of the MAC of a message.
1460 *
1461 * The application must call psa_mac_sign_setup() before calling this function.
1462 * This function calculates the MAC of the message formed by concatenating
1463 * the inputs passed to preceding calls to psa_mac_update().
1464 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001465 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001466 * If this function returns an error status, the operation enters an error
1467 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001468 *
1469 * \warning Applications should not call this function if they expect
1470 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1471 * Beware that comparing integrity or authenticity data such as
1472 * MAC values with a function such as \c memcmp is risky
1473 * because the time taken by the comparison may leak information
1474 * about the MAC value which could allow an attacker to guess
1475 * a valid MAC and thereby bypass security controls.
1476 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001477 * \param[in,out] operation Active MAC operation.
1478 * \param[out] mac Buffer where the MAC value is to be written.
1479 * \param mac_size Size of the \p mac buffer in bytes.
1480 * \param[out] mac_length On success, the number of bytes
1481 * that make up the MAC value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001482 * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001483 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001484 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001485 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001486 *
1487 * \retval #PSA_SUCCESS
1488 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001489 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001490 * The size of the \p mac buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001491 * sufficient buffer size by calling PSA_MAC_LENGTH().
Gilles Peskineed733552023-02-14 19:21:09 +01001492 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1493 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1494 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1495 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1496 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001497 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001498 * The operation state is not valid (it must be an active mac sign
1499 * operation), or the library has not been previously initialized
1500 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001501 * It is implementation-dependent whether a failure to initialize
1502 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001503 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001504psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1505 uint8_t *mac,
1506 size_t mac_size,
1507 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001508
Gilles Peskinedcd14942018-07-12 00:30:52 +02001509/** Finish the calculation of the MAC of a message and compare it with
1510 * an expected value.
1511 *
1512 * The application must call psa_mac_verify_setup() before calling this function.
1513 * This function calculates the MAC of the message formed by concatenating
1514 * the inputs passed to preceding calls to psa_mac_update(). It then
1515 * compares the calculated MAC with the expected MAC passed as a
1516 * parameter to this function.
1517 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001518 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001519 * If this function returns an error status, the operation enters an error
1520 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001521 *
1522 * \note Implementations shall make the best effort to ensure that the
1523 * comparison between the actual MAC and the expected MAC is performed
1524 * in constant time.
1525 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001526 * \param[in,out] operation Active MAC operation.
1527 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001528 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001529 *
1530 * \retval #PSA_SUCCESS
1531 * The expected MAC is identical to the actual MAC of the message.
1532 * \retval #PSA_ERROR_INVALID_SIGNATURE
1533 * The MAC of the message was calculated successfully, but it
1534 * differs from the expected MAC.
Gilles Peskineed733552023-02-14 19:21:09 +01001535 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1536 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1537 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1538 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1539 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001540 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001541 * The operation state is not valid (it must be an active mac verify
1542 * operation), or the library has not been previously initialized
1543 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001544 * It is implementation-dependent whether a failure to initialize
1545 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001546 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001547psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1548 const uint8_t *mac,
1549 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001550
Gilles Peskinedcd14942018-07-12 00:30:52 +02001551/** Abort a MAC operation.
1552 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001553 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001554 * \p operation structure itself. Once aborted, the operation object
1555 * can be reused for another operation by calling
1556 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001557 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001558 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001559 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001560 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001561 * In particular, calling psa_mac_abort() after the operation has been
1562 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1563 * psa_mac_verify_finish() is safe and has no effect.
1564 *
1565 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001566 *
Gilles Peskineed733552023-02-14 19:21:09 +01001567 * \retval #PSA_SUCCESS \emptydescription
1568 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1569 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1570 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001571 * \retval #PSA_ERROR_BAD_STATE
1572 * The library has not been previously initialized by psa_crypto_init().
1573 * It is implementation-dependent whether a failure to initialize
1574 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001575 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001576psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1577
1578/**@}*/
1579
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001580/** \defgroup cipher Symmetric ciphers
1581 * @{
1582 */
1583
Gilles Peskine69647a42019-01-14 20:18:12 +01001584/** Encrypt a message using a symmetric cipher.
1585 *
1586 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001587 * vector). Use the multipart operation interface with a
1588 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001589 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001590 * \param key Identifier of the key to use for the operation.
Ronald Cron96783552020-10-19 12:06:30 +02001591 * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001592 * \param alg The cipher algorithm to compute
1593 * (\c PSA_ALG_XXX value such that
1594 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1595 * \param[in] input Buffer containing the message to encrypt.
1596 * \param input_length Size of the \p input buffer in bytes.
1597 * \param[out] output Buffer where the output is to be written.
1598 * The output contains the IV followed by
1599 * the ciphertext proper.
1600 * \param output_size Size of the \p output buffer in bytes.
1601 * \param[out] output_length On success, the number of bytes
1602 * that make up the output.
1603 *
1604 * \retval #PSA_SUCCESS
1605 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001606 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1607 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001608 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001609 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001610 * \retval #PSA_ERROR_NOT_SUPPORTED
1611 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001612 * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
1613 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1614 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1615 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1616 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1617 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001618 * \retval #PSA_ERROR_BAD_STATE
1619 * The library has not been previously initialized by psa_crypto_init().
1620 * It is implementation-dependent whether a failure to initialize
1621 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001622 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001623psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001624 psa_algorithm_t alg,
1625 const uint8_t *input,
1626 size_t input_length,
1627 uint8_t *output,
1628 size_t output_size,
1629 size_t *output_length);
1630
1631/** Decrypt a message using a symmetric cipher.
1632 *
1633 * This function decrypts a message encrypted with a symmetric cipher.
1634 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001635 * \param key Identifier of the key to use for the operation.
Gilles Peskine69647a42019-01-14 20:18:12 +01001636 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001637 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001638 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001639 * \param alg The cipher algorithm to compute
1640 * (\c PSA_ALG_XXX value such that
1641 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1642 * \param[in] input Buffer containing the message to decrypt.
1643 * This consists of the IV followed by the
1644 * ciphertext proper.
1645 * \param input_length Size of the \p input buffer in bytes.
1646 * \param[out] output Buffer where the plaintext is to be written.
1647 * \param output_size Size of the \p output buffer in bytes.
1648 * \param[out] output_length On success, the number of bytes
1649 * that make up the output.
1650 *
1651 * \retval #PSA_SUCCESS
1652 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001653 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1654 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001655 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001656 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001657 * \retval #PSA_ERROR_NOT_SUPPORTED
1658 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001659 * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
1660 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1661 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1662 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1663 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1664 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001665 * \retval #PSA_ERROR_BAD_STATE
1666 * The library has not been previously initialized by psa_crypto_init().
1667 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001668 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001669 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001670psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001671 psa_algorithm_t alg,
1672 const uint8_t *input,
1673 size_t input_length,
1674 uint8_t *output,
1675 size_t output_size,
1676 size_t *output_length);
1677
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001678/** The type of the state data structure for multipart cipher operations.
1679 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001680 * Before calling any function on a cipher operation object, the application
1681 * must initialize it by any of the following means:
1682 * - Set the structure to all-bits-zero, for example:
1683 * \code
1684 * psa_cipher_operation_t operation;
1685 * memset(&operation, 0, sizeof(operation));
1686 * \endcode
1687 * - Initialize the structure to logical zero values, for example:
1688 * \code
1689 * psa_cipher_operation_t operation = {0};
1690 * \endcode
1691 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1692 * for example:
1693 * \code
1694 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1695 * \endcode
1696 * - Assign the result of the function psa_cipher_operation_init()
1697 * to the structure, for example:
1698 * \code
1699 * psa_cipher_operation_t operation;
1700 * operation = psa_cipher_operation_init();
1701 * \endcode
1702 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001703 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01001704 * make any assumptions about the content of this structure.
1705 * Implementation details can change in future versions without notice. */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001706typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1707
Jaeden Amero5bae2272019-01-04 11:48:27 +00001708/** \def PSA_CIPHER_OPERATION_INIT
1709 *
1710 * This macro returns a suitable initializer for a cipher operation object of
1711 * type #psa_cipher_operation_t.
1712 */
Jaeden Amero5bae2272019-01-04 11:48:27 +00001713
1714/** Return an initial value for a cipher operation object.
1715 */
1716static psa_cipher_operation_t psa_cipher_operation_init(void);
1717
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001718/** Set the key for a multipart symmetric encryption operation.
1719 *
1720 * The sequence of operations to encrypt a message with a symmetric cipher
1721 * is as follows:
1722 * -# Allocate an operation object which will be passed to all the functions
1723 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001724 * -# Initialize the operation object with one of the methods described in the
1725 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001726 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001727 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001728 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001729 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001730 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001731 * requires a specific IV value.
1732 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1733 * of the message each time.
1734 * -# Call psa_cipher_finish().
1735 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001736 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1737 * the operation will need to be reset by a call to psa_cipher_abort(). The
1738 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001739 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001740 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001741 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001742 * eventually terminate the operation. The following events terminate an
1743 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001744 * - A successful call to psa_cipher_finish().
1745 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001746 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001747 * \param[in,out] operation The operation object to set up. It must have
1748 * been initialized as per the documentation for
1749 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001750 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001751 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001752 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001753 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001754 * \param alg The cipher algorithm to compute
1755 * (\c PSA_ALG_XXX value such that
1756 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001757 *
Gilles Peskine28538492018-07-11 17:34:00 +02001758 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001759 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001760 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1761 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001762 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001763 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001764 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001765 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001766 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1767 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1768 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1769 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1770 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03001771 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001772 * The operation state is not valid (it must be inactive), or
1773 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001774 * It is implementation-dependent whether a failure to initialize
1775 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001776 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001777psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001778 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001779 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001780
1781/** Set the key for a multipart symmetric decryption operation.
1782 *
1783 * The sequence of operations to decrypt a message with a symmetric cipher
1784 * is as follows:
1785 * -# Allocate an operation object which will be passed to all the functions
1786 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001787 * -# Initialize the operation object with one of the methods described in the
1788 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001789 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001790 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001791 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001792 * decryption. If the IV is prepended to the ciphertext, you can call
1793 * psa_cipher_update() on a buffer containing the IV followed by the
1794 * beginning of the message.
1795 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1796 * of the message each time.
1797 * -# Call psa_cipher_finish().
1798 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001799 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1800 * the operation will need to be reset by a call to psa_cipher_abort(). The
1801 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001802 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001803 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001804 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001805 * eventually terminate the operation. The following events terminate an
1806 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001807 * - A successful call to psa_cipher_finish().
1808 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001809 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001810 * \param[in,out] operation The operation object to set up. It must have
1811 * been initialized as per the documentation for
1812 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001813 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001814 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001815 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001816 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001817 * \param alg The cipher algorithm to compute
1818 * (\c PSA_ALG_XXX value such that
1819 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001820 *
Gilles Peskine28538492018-07-11 17:34:00 +02001821 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001822 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001823 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1824 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001825 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001826 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001827 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001828 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001829 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1830 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1831 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1832 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1833 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03001834 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001835 * The operation state is not valid (it must be inactive), or
1836 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001837 * It is implementation-dependent whether a failure to initialize
1838 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001839 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001840psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001841 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001842 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001843
Gilles Peskinedcd14942018-07-12 00:30:52 +02001844/** 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 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001853 * If this function returns an error status, the operation enters an error
1854 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001855 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001856 * \param[in,out] operation Active cipher operation.
1857 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001858 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001859 * \param[out] iv_length On success, the number of bytes of the
1860 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001861 *
1862 * \retval #PSA_SUCCESS
1863 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001864 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001865 * The size of the \p iv buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01001866 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1867 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1868 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1869 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1870 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001871 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001872 * The operation state is not valid (it must be active, with no IV set),
1873 * or the library has not been previously initialized
1874 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001875 * It is implementation-dependent whether a failure to initialize
1876 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001877 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001878psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001879 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001880 size_t iv_size,
1881 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001882
Gilles Peskinedcd14942018-07-12 00:30:52 +02001883/** Set the IV for a symmetric encryption or decryption operation.
1884 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001885 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001886 * 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 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001891 * If this function returns an error status, the operation enters an error
1892 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001893 *
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 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001898 * \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.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001901 *
1902 * \retval #PSA_SUCCESS
1903 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001904 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001905 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001906 * or the chosen algorithm does not use an IV.
Gilles Peskineed733552023-02-14 19:21:09 +01001907 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1908 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1909 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1910 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1911 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001912 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001913 * The operation state is not valid (it must be an active cipher
1914 * encrypt operation, with no IV set), or the library has not been
1915 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001916 * It is implementation-dependent whether a failure to initialize
1917 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001918 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001919psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001920 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001921 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001922
Gilles Peskinedcd14942018-07-12 00:30:52 +02001923/** Encrypt or decrypt a message fragment in an active cipher operation.
1924 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001925 * Before calling this function, you must:
1926 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1927 * The choice of setup function determines whether this function
1928 * encrypts or decrypts its input.
1929 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1930 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001931 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001932 * If this function returns an error status, the operation enters an error
1933 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001934 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001935 * \param[in,out] operation Active cipher operation.
1936 * \param[in] input Buffer containing the message fragment to
1937 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001938 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001939 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001940 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001941 * \param[out] output_length On success, the number of bytes
1942 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001943 *
1944 * \retval #PSA_SUCCESS
1945 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001946 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1947 * The size of the \p output buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01001948 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1949 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1950 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1951 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1952 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001953 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001954 * The operation state is not valid (it must be active, with an IV set
1955 * if required for the algorithm), or the library has not been
1956 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001957 * It is implementation-dependent whether a failure to initialize
1958 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001959 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001960psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1961 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001962 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001963 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001964 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001965 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001966
Gilles Peskinedcd14942018-07-12 00:30:52 +02001967/** Finish encrypting or decrypting a message in a cipher operation.
1968 *
1969 * The application must call psa_cipher_encrypt_setup() or
1970 * psa_cipher_decrypt_setup() before calling this function. The choice
1971 * of setup function determines whether this function encrypts or
1972 * decrypts its input.
1973 *
1974 * This function finishes the encryption or decryption of the message
1975 * formed by concatenating the inputs passed to preceding calls to
1976 * psa_cipher_update().
1977 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001978 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001979 * If this function returns an error status, the operation enters an error
1980 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001981 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001982 * \param[in,out] operation Active cipher operation.
1983 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001984 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001985 * \param[out] output_length On success, the number of bytes
1986 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001987 *
1988 * \retval #PSA_SUCCESS
1989 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02001990 * \retval #PSA_ERROR_INVALID_ARGUMENT
1991 * The total input size passed to this operation is not valid for
1992 * this particular algorithm. For example, the algorithm is a based
1993 * on block cipher and requires a whole number of blocks, but the
1994 * total input size is not a multiple of the block size.
1995 * \retval #PSA_ERROR_INVALID_PADDING
1996 * This is a decryption operation for an algorithm that includes
1997 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001998 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1999 * The size of the \p output buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01002000 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2001 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2002 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2003 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2004 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002005 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002006 * The operation state is not valid (it must be active, with an IV set
2007 * if required for the algorithm), or the library has not been
2008 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002009 * It is implementation-dependent whether a failure to initialize
2010 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002011 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002012psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002013 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002014 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002015 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002016
Gilles Peskinedcd14942018-07-12 00:30:52 +02002017/** Abort a cipher operation.
2018 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002019 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002020 * \p operation structure itself. Once aborted, the operation object
2021 * can be reused for another operation by calling
2022 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002023 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002024 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002025 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002026 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002027 * In particular, calling psa_cipher_abort() after the operation has been
2028 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2029 * is safe and has no effect.
2030 *
2031 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002032 *
Gilles Peskineed733552023-02-14 19:21:09 +01002033 * \retval #PSA_SUCCESS \emptydescription
2034 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2035 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2036 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002037 * \retval #PSA_ERROR_BAD_STATE
2038 * The library has not been previously initialized by psa_crypto_init().
2039 * It is implementation-dependent whether a failure to initialize
2040 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002041 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002042psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2043
2044/**@}*/
2045
Gilles Peskine3b555712018-03-03 21:27:57 +01002046/** \defgroup aead Authenticated encryption with associated data (AEAD)
2047 * @{
2048 */
2049
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002050/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002051 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002052 * \param key Identifier of the key to use for the
2053 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002054 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002055 * \param alg The AEAD algorithm to compute
2056 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002057 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002058 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002059 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002060 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002061 * but not encrypted.
2062 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002063 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002064 * encrypted.
2065 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002066 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002067 * encrypted data. The additional data is not
2068 * part of this output. For algorithms where the
2069 * encrypted data and the authentication tag
2070 * are defined as separate outputs, the
2071 * authentication tag is appended to the
2072 * encrypted data.
2073 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002074 * This must be appropriate for the selected
2075 * algorithm and key:
2076 * - A sufficient output size is
2077 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type,
2078 * \p alg, \p plaintext_length) where
2079 * \c key_type is the type of \p key.
2080 * - #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p
2081 * plaintext_length) evaluates to the maximum
2082 * ciphertext size of any supported AEAD
2083 * encryption.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002084 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002085 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002086 *
Gilles Peskine28538492018-07-11 17:34:00 +02002087 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002088 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01002089 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2090 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002091 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002092 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002093 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002094 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002095 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002096 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Bence Szépkútibd98df72021-04-27 04:37:18 +02002097 * \p ciphertext_size is too small.
2098 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2099 * \p plaintext_length) or
2100 * #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length) can be used to
2101 * determine the required buffer size.
Gilles Peskineed733552023-02-14 19:21:09 +01002102 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2103 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2104 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2105 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03002106 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002107 * The library has not been previously initialized by psa_crypto_init().
2108 * It is implementation-dependent whether a failure to initialize
2109 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002110 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002111psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002112 psa_algorithm_t alg,
2113 const uint8_t *nonce,
2114 size_t nonce_length,
2115 const uint8_t *additional_data,
2116 size_t additional_data_length,
2117 const uint8_t *plaintext,
2118 size_t plaintext_length,
2119 uint8_t *ciphertext,
2120 size_t ciphertext_size,
2121 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002122
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002123/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002124 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002125 * \param key Identifier of the key to use for the
2126 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002127 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002128 * \param alg The AEAD algorithm to compute
2129 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002130 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002131 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002132 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002133 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002134 * but not encrypted.
2135 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002136 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002137 * encrypted. For algorithms where the
2138 * encrypted data and the authentication tag
2139 * are defined as separate inputs, the buffer
2140 * must contain the encrypted data followed
2141 * by the authentication tag.
2142 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002143 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002144 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002145 * This must be appropriate for the selected
2146 * algorithm and key:
2147 * - A sufficient output size is
2148 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type,
2149 * \p alg, \p ciphertext_length) where
2150 * \c key_type is the type of \p key.
2151 * - #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p
2152 * ciphertext_length) evaluates to the maximum
2153 * plaintext size of any supported AEAD
2154 * decryption.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002155 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002156 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002157 *
Gilles Peskine28538492018-07-11 17:34:00 +02002158 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002159 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01002160 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002161 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002162 * The ciphertext is not authentic.
Gilles Peskineed733552023-02-14 19:21:09 +01002163 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002164 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002165 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002166 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002167 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002168 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002169 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Bence Szépkútibd98df72021-04-27 04:37:18 +02002170 * \p plaintext_size is too small.
2171 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2172 * \p ciphertext_length) or
2173 * #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length) can be used
2174 * to determine the required buffer size.
Gilles Peskineed733552023-02-14 19:21:09 +01002175 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2176 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2177 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2178 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03002179 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002180 * The library has not been previously initialized by psa_crypto_init().
2181 * It is implementation-dependent whether a failure to initialize
2182 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002183 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002184psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002185 psa_algorithm_t alg,
2186 const uint8_t *nonce,
2187 size_t nonce_length,
2188 const uint8_t *additional_data,
2189 size_t additional_data_length,
2190 const uint8_t *ciphertext,
2191 size_t ciphertext_length,
2192 uint8_t *plaintext,
2193 size_t plaintext_size,
2194 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002195
Gilles Peskine30a9e412019-01-14 18:36:12 +01002196/** The type of the state data structure for multipart AEAD operations.
2197 *
2198 * Before calling any function on an AEAD operation object, the application
2199 * must initialize it by any of the following means:
2200 * - Set the structure to all-bits-zero, for example:
2201 * \code
2202 * psa_aead_operation_t operation;
2203 * memset(&operation, 0, sizeof(operation));
2204 * \endcode
2205 * - Initialize the structure to logical zero values, for example:
2206 * \code
2207 * psa_aead_operation_t operation = {0};
2208 * \endcode
2209 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2210 * for example:
2211 * \code
2212 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2213 * \endcode
2214 * - Assign the result of the function psa_aead_operation_init()
2215 * to the structure, for example:
2216 * \code
2217 * psa_aead_operation_t operation;
2218 * operation = psa_aead_operation_init();
2219 * \endcode
2220 *
2221 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01002222 * make any assumptions about the content of this structure.
2223 * Implementation details can change in future versions without notice. */
Gilles Peskine30a9e412019-01-14 18:36:12 +01002224typedef struct psa_aead_operation_s psa_aead_operation_t;
2225
2226/** \def PSA_AEAD_OPERATION_INIT
2227 *
2228 * This macro returns a suitable initializer for an AEAD operation object of
2229 * type #psa_aead_operation_t.
2230 */
Gilles Peskine30a9e412019-01-14 18:36:12 +01002231
2232/** Return an initial value for an AEAD operation object.
2233 */
2234static psa_aead_operation_t psa_aead_operation_init(void);
2235
2236/** Set the key for a multipart authenticated encryption operation.
2237 *
2238 * The sequence of operations to encrypt a message with authentication
2239 * is as follows:
2240 * -# Allocate an operation object which will be passed to all the functions
2241 * listed here.
2242 * -# Initialize the operation object with one of the methods described in the
2243 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002244 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002245 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002246 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2247 * inputs to the subsequent calls to psa_aead_update_ad() and
2248 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2249 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002250 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2251 * generate or set the nonce. You should use
2252 * psa_aead_generate_nonce() unless the protocol you are implementing
2253 * requires a specific nonce value.
2254 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2255 * of the non-encrypted additional authenticated data each time.
2256 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002257 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002258 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002259 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002260 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2261 * the operation will need to be reset by a call to psa_aead_abort(). The
2262 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002263 * has been initialized.
2264 *
2265 * After a successful call to psa_aead_encrypt_setup(), the application must
2266 * eventually terminate the operation. The following events terminate an
2267 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002268 * - A successful call to psa_aead_finish().
2269 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002270 *
2271 * \param[in,out] operation The operation object to set up. It must have
2272 * been initialized as per the documentation for
2273 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002274 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002275 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002276 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002277 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002278 * \param alg The AEAD algorithm to compute
2279 * (\c PSA_ALG_XXX value such that
2280 * #PSA_ALG_IS_AEAD(\p alg) is true).
2281 *
2282 * \retval #PSA_SUCCESS
2283 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002284 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002285 * The operation state is not valid (it must be inactive), or
2286 * the library has not been previously initialized by psa_crypto_init().
Gilles Peskineed733552023-02-14 19:21:09 +01002287 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2288 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine30a9e412019-01-14 18:36:12 +01002289 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002290 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002291 * \retval #PSA_ERROR_NOT_SUPPORTED
2292 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002293 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2294 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2295 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2296 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002297 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002298 * The library has not been previously initialized by psa_crypto_init().
2299 * It is implementation-dependent whether a failure to initialize
2300 * results in this error code.
2301 */
2302psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002303 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002304 psa_algorithm_t alg);
2305
2306/** Set the key for a multipart authenticated decryption operation.
2307 *
2308 * The sequence of operations to decrypt a message with authentication
2309 * is as follows:
2310 * -# Allocate an operation object which will be passed to all the functions
2311 * listed here.
2312 * -# Initialize the operation object with one of the methods described in the
2313 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002314 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002315 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002316 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2317 * inputs to the subsequent calls to psa_aead_update_ad() and
2318 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2319 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002320 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2321 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2322 * of the non-encrypted additional authenticated data each time.
2323 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002324 * of the ciphertext to decrypt each time.
2325 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002326 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002327 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2328 * the operation will need to be reset by a call to psa_aead_abort(). The
2329 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002330 * has been initialized.
2331 *
2332 * After a successful call to psa_aead_decrypt_setup(), the application must
2333 * eventually terminate the operation. The following events terminate an
2334 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002335 * - A successful call to psa_aead_verify().
2336 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002337 *
2338 * \param[in,out] operation The operation object to set up. It must have
2339 * been initialized as per the documentation for
2340 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002341 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002342 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002343 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002344 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002345 * \param alg The AEAD algorithm to compute
2346 * (\c PSA_ALG_XXX value such that
2347 * #PSA_ALG_IS_AEAD(\p alg) is true).
2348 *
2349 * \retval #PSA_SUCCESS
2350 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01002351 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2352 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine30a9e412019-01-14 18:36:12 +01002353 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002354 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002355 * \retval #PSA_ERROR_NOT_SUPPORTED
2356 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002357 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2358 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2359 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2360 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2361 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine30a9e412019-01-14 18:36:12 +01002362 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002363 * The operation state is not valid (it must be inactive), or the
2364 * library has not been previously initialized by psa_crypto_init().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002365 * It is implementation-dependent whether a failure to initialize
2366 * results in this error code.
2367 */
2368psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002369 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002370 psa_algorithm_t alg);
2371
2372/** Generate a random nonce for an authenticated encryption operation.
2373 *
2374 * This function generates a random nonce for the authenticated encryption
2375 * operation with an appropriate size for the chosen algorithm, key type
2376 * and key size.
2377 *
2378 * The application must call psa_aead_encrypt_setup() before
2379 * calling this function.
2380 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002381 * If this function returns an error status, the operation enters an error
2382 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002383 *
2384 * \param[in,out] operation Active AEAD operation.
2385 * \param[out] nonce Buffer where the generated nonce is to be
2386 * written.
2387 * \param nonce_size Size of the \p nonce buffer in bytes.
2388 * \param[out] nonce_length On success, the number of bytes of the
2389 * generated nonce.
2390 *
2391 * \retval #PSA_SUCCESS
2392 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002393 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2394 * The size of the \p nonce buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01002395 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2396 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2397 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2398 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2399 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002400 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002401 * The operation state is not valid (it must be an active aead encrypt
2402 * operation, with no nonce set), or the library has not been
2403 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002404 * It is implementation-dependent whether a failure to initialize
2405 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002406 */
2407psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002408 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002409 size_t nonce_size,
2410 size_t *nonce_length);
2411
2412/** Set the nonce for an authenticated encryption or decryption operation.
2413 *
2414 * This function sets the nonce for the authenticated
2415 * encryption or decryption operation.
2416 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002417 * The application must call psa_aead_encrypt_setup() or
2418 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002419 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002420 * If this function returns an error status, the operation enters an error
2421 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002422 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002423 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002424 * instead of this function, unless implementing a protocol that requires
2425 * a non-random IV.
2426 *
2427 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002428 * \param[in] nonce Buffer containing the nonce to use.
2429 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002430 *
2431 * \retval #PSA_SUCCESS
2432 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002433 * \retval #PSA_ERROR_INVALID_ARGUMENT
2434 * The size of \p nonce is not acceptable for the chosen algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002435 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2436 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2437 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2438 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2439 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002440 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002441 * The operation state is not valid (it must be active, with no nonce
2442 * set), or the library has not been previously initialized
2443 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002444 * It is implementation-dependent whether a failure to initialize
2445 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002446 */
2447psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002448 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002449 size_t nonce_length);
2450
Gilles Peskinebc59c852019-01-17 15:26:08 +01002451/** Declare the lengths of the message and additional data for AEAD.
2452 *
2453 * The application must call this function before calling
2454 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2455 * the operation requires it. If the algorithm does not require it,
2456 * calling this function is optional, but if this function is called
2457 * then the implementation must enforce the lengths.
2458 *
2459 * You may call this function before or after setting the nonce with
2460 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2461 *
2462 * - For #PSA_ALG_CCM, calling this function is required.
2463 * - For the other AEAD algorithms defined in this specification, calling
2464 * this function is not required.
2465 * - For vendor-defined algorithm, refer to the vendor documentation.
2466 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002467 * If this function returns an error status, the operation enters an error
2468 * state and must be aborted by calling psa_aead_abort().
2469 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002470 * \param[in,out] operation Active AEAD operation.
2471 * \param ad_length Size of the non-encrypted additional
2472 * authenticated data in bytes.
2473 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2474 *
2475 * \retval #PSA_SUCCESS
2476 * Success.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002477 * \retval #PSA_ERROR_INVALID_ARGUMENT
2478 * At least one of the lengths is not acceptable for the chosen
2479 * algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002480 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2481 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2482 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2483 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002484 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002485 * The operation state is not valid (it must be active, and
2486 * psa_aead_update_ad() and psa_aead_update() must not have been
2487 * called yet), or the library has not been previously initialized
2488 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002489 * It is implementation-dependent whether a failure to initialize
2490 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002491 */
2492psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2493 size_t ad_length,
2494 size_t plaintext_length);
2495
Gilles Peskine30a9e412019-01-14 18:36:12 +01002496/** Pass additional data to an active AEAD operation.
2497 *
2498 * Additional data is authenticated, but not encrypted.
2499 *
2500 * You may call this function multiple times to pass successive fragments
2501 * of the additional data. You may not call this function after passing
2502 * data to encrypt or decrypt with psa_aead_update().
2503 *
2504 * Before calling this function, you must:
2505 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2506 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2507 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002508 * If this function returns an error status, the operation enters an error
2509 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002510 *
2511 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2512 * there is no guarantee that the input is valid. Therefore, until
2513 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2514 * treat the input as untrusted and prepare to undo any action that
2515 * depends on the input if psa_aead_verify() returns an error status.
2516 *
2517 * \param[in,out] operation Active AEAD operation.
2518 * \param[in] input Buffer containing the fragment of
2519 * additional data.
2520 * \param input_length Size of the \p input buffer in bytes.
2521 *
2522 * \retval #PSA_SUCCESS
2523 * Success.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002524 * \retval #PSA_ERROR_INVALID_ARGUMENT
2525 * The total input length overflows the additional data length that
2526 * was previously specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002527 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2528 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2529 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2530 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2531 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002532 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002533 * The operation state is not valid (it must be active, have a nonce
2534 * set, have lengths set if required by the algorithm, and
2535 * psa_aead_update() must not have been called yet), or the library
2536 * has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002537 * It is implementation-dependent whether a failure to initialize
2538 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002539 */
2540psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2541 const uint8_t *input,
2542 size_t input_length);
2543
2544/** Encrypt or decrypt a message fragment in an active AEAD operation.
2545 *
2546 * Before calling this function, you must:
2547 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2548 * The choice of setup function determines whether this function
2549 * encrypts or decrypts its input.
2550 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2551 * 3. Call psa_aead_update_ad() to pass all the additional data.
2552 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002553 * If this function returns an error status, the operation enters an error
2554 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002555 *
2556 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2557 * there is no guarantee that the input is valid. Therefore, until
2558 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2559 * - Do not use the output in any way other than storing it in a
2560 * confidential location. If you take any action that depends
2561 * on the tentative decrypted data, this action will need to be
2562 * undone if the input turns out not to be valid. Furthermore,
2563 * if an adversary can observe that this action took place
2564 * (for example through timing), they may be able to use this
2565 * fact as an oracle to decrypt any message encrypted with the
2566 * same key.
2567 * - In particular, do not copy the output anywhere but to a
2568 * memory or storage space that you have exclusive access to.
2569 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002570 * This function does not require the input to be aligned to any
2571 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002572 * a whole block at a time, it must consume all the input provided, but
2573 * it may delay the end of the corresponding output until a subsequent
2574 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2575 * provides sufficient input. The amount of data that can be delayed
2576 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002577 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002578 * \param[in,out] operation Active AEAD operation.
2579 * \param[in] input Buffer containing the message fragment to
2580 * encrypt or decrypt.
2581 * \param input_length Size of the \p input buffer in bytes.
2582 * \param[out] output Buffer where the output is to be written.
2583 * \param output_size Size of the \p output buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002584 * This must be appropriate for the selected
2585 * algorithm and key:
2586 * - A sufficient output size is
2587 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,
2588 * \c alg, \p input_length) where
2589 * \c key_type is the type of key and \c alg is
2590 * the algorithm that were used to set up the
2591 * operation.
2592 * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p
2593 * input_length) evaluates to the maximum
2594 * output size of any supported AEAD
2595 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002596 * \param[out] output_length On success, the number of bytes
2597 * that make up the returned output.
2598 *
2599 * \retval #PSA_SUCCESS
2600 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002601 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2602 * The size of the \p output buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002603 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or
2604 * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to
2605 * determine the required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002606 * \retval #PSA_ERROR_INVALID_ARGUMENT
2607 * The total length of input to psa_aead_update_ad() so far is
2608 * less than the additional data length that was previously
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002609 * specified with psa_aead_set_lengths(), or
2610 * the total input length overflows the plaintext length that
Gilles Peskinebc59c852019-01-17 15:26:08 +01002611 * was previously specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002612 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2613 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2614 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2615 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2616 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002617 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002618 * The operation state is not valid (it must be active, have a nonce
2619 * set, and have lengths set if required by the algorithm), or the
2620 * library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002621 * It is implementation-dependent whether a failure to initialize
2622 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002623 */
2624psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2625 const uint8_t *input,
2626 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002627 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002628 size_t output_size,
2629 size_t *output_length);
2630
2631/** Finish encrypting a message in an AEAD operation.
2632 *
2633 * The operation must have been set up with psa_aead_encrypt_setup().
2634 *
2635 * This function finishes the authentication of the additional data
2636 * formed by concatenating the inputs passed to preceding calls to
2637 * psa_aead_update_ad() with the plaintext formed by concatenating the
2638 * inputs passed to preceding calls to psa_aead_update().
2639 *
2640 * This function has two output buffers:
2641 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002642 * preceding calls to psa_aead_update().
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002643 * - \p tag contains the authentication tag.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002644 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002645 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002646 * If this function returns an error status, the operation enters an error
2647 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002648 *
2649 * \param[in,out] operation Active AEAD operation.
2650 * \param[out] ciphertext Buffer where the last part of the ciphertext
2651 * is to be written.
2652 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002653 * This must be appropriate for the selected
2654 * algorithm and key:
2655 * - A sufficient output size is
2656 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type,
2657 * \c alg) where \c key_type is the type of key
2658 * and \c alg is the algorithm that were used to
2659 * set up the operation.
2660 * - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to
2661 * the maximum output size of any supported AEAD
2662 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002663 * \param[out] ciphertext_length On success, the number of bytes of
2664 * returned ciphertext.
2665 * \param[out] tag Buffer where the authentication tag is
2666 * to be written.
2667 * \param tag_size Size of the \p tag buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002668 * This must be appropriate for the selected
2669 * algorithm and key:
2670 * - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c
2671 * key_type, \c key_bits, \c alg) where
2672 * \c key_type and \c key_bits are the type and
2673 * bit-size of the key, and \c alg is the
2674 * algorithm that were used in the call to
2675 * psa_aead_encrypt_setup().
2676 * - #PSA_AEAD_TAG_MAX_SIZE evaluates to the
2677 * maximum tag size of any supported AEAD
2678 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002679 * \param[out] tag_length On success, the number of bytes
2680 * that make up the returned tag.
2681 *
2682 * \retval #PSA_SUCCESS
2683 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002684 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002685 * The size of the \p ciphertext or \p tag buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002686 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or
2687 * #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the
2688 * required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type,
2689 * \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to
2690 * determine the required \p tag buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002691 * \retval #PSA_ERROR_INVALID_ARGUMENT
2692 * The total length of input to psa_aead_update_ad() so far is
2693 * less than the additional data length that was previously
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002694 * specified with psa_aead_set_lengths(), or
2695 * the total length of input to psa_aead_update() so far is
Gilles Peskinebc59c852019-01-17 15:26:08 +01002696 * less than the plaintext length that was previously
2697 * specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002698 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2699 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2700 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2701 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2702 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002703 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002704 * The operation state is not valid (it must be an active encryption
2705 * operation with a nonce set), or the library has not been previously
2706 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002707 * It is implementation-dependent whether a failure to initialize
2708 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002709 */
2710psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002711 uint8_t *ciphertext,
2712 size_t ciphertext_size,
2713 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002714 uint8_t *tag,
2715 size_t tag_size,
2716 size_t *tag_length);
2717
2718/** Finish authenticating and decrypting a message in an AEAD operation.
2719 *
2720 * The operation must have been set up with psa_aead_decrypt_setup().
2721 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002722 * This function finishes the authenticated decryption of the message
2723 * components:
2724 *
2725 * - The additional data consisting of the concatenation of the inputs
2726 * passed to preceding calls to psa_aead_update_ad().
2727 * - The ciphertext consisting of the concatenation of the inputs passed to
2728 * preceding calls to psa_aead_update().
2729 * - The tag passed to this function call.
2730 *
2731 * If the authentication tag is correct, this function outputs any remaining
2732 * plaintext and reports success. If the authentication tag is not correct,
2733 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002734 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002735 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002736 * If this function returns an error status, the operation enters an error
2737 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002738 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002739 * \note Implementations shall make the best effort to ensure that the
2740 * comparison between the actual tag and the expected tag is performed
2741 * in constant time.
2742 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002743 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002744 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002745 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002746 * from previous calls to psa_aead_update()
2747 * that could not be processed until the end
2748 * of the input.
2749 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002750 * This must be appropriate for the selected algorithm and key:
2751 * - A sufficient output size is
2752 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type,
2753 * \c alg) where \c key_type is the type of key
2754 * and \c alg is the algorithm that were used to
2755 * set up the operation.
2756 * - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to
2757 * the maximum output size of any supported AEAD
2758 * algorithm.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002759 * \param[out] plaintext_length On success, the number of bytes of
2760 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002761 * \param[in] tag Buffer containing the authentication tag.
2762 * \param tag_length Size of the \p tag buffer in bytes.
2763 *
2764 * \retval #PSA_SUCCESS
2765 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002766 * \retval #PSA_ERROR_INVALID_SIGNATURE
2767 * The calculations were successful, but the authentication tag is
2768 * not correct.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002769 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2770 * The size of the \p plaintext buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002771 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or
2772 * #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the
2773 * required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002774 * \retval #PSA_ERROR_INVALID_ARGUMENT
2775 * The total length of input to psa_aead_update_ad() so far is
2776 * less than the additional data length that was previously
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002777 * specified with psa_aead_set_lengths(), or
2778 * the total length of input to psa_aead_update() so far is
Gilles Peskinebc59c852019-01-17 15:26:08 +01002779 * less than the plaintext length that was previously
2780 * specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002781 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2782 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2783 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2784 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2785 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002786 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002787 * The operation state is not valid (it must be an active decryption
2788 * operation with a nonce set), or the library has not been previously
2789 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002790 * It is implementation-dependent whether a failure to initialize
2791 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002792 */
2793psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002794 uint8_t *plaintext,
2795 size_t plaintext_size,
2796 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002797 const uint8_t *tag,
2798 size_t tag_length);
2799
2800/** Abort an AEAD operation.
2801 *
2802 * Aborting an operation frees all associated resources except for the
2803 * \p operation structure itself. Once aborted, the operation object
2804 * can be reused for another operation by calling
2805 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2806 *
2807 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002808 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002809 *
2810 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002811 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2812 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002813 *
2814 * \param[in,out] operation Initialized AEAD operation.
2815 *
Gilles Peskineed733552023-02-14 19:21:09 +01002816 * \retval #PSA_SUCCESS \emptydescription
2817 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2818 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2819 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002820 * \retval #PSA_ERROR_BAD_STATE
2821 * The library has not been previously initialized by psa_crypto_init().
2822 * It is implementation-dependent whether a failure to initialize
2823 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002824 */
2825psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2826
Gilles Peskine3b555712018-03-03 21:27:57 +01002827/**@}*/
2828
Gilles Peskine20035e32018-02-03 22:44:14 +01002829/** \defgroup asymmetric Asymmetric cryptography
2830 * @{
2831 */
2832
2833/**
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002834 * \brief Sign a message with a private key. For hash-and-sign algorithms,
2835 * this includes the hashing step.
2836 *
2837 * \note To perform a multi-part hash-and-sign signature algorithm, first use
2838 * a multi-part hash operation and then pass the resulting hash to
2839 * psa_sign_hash(). PSA_ALG_GET_HASH(\p alg) can be used to determine the
2840 * hash algorithm to use.
2841 *
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002842 * \param[in] key Identifier of the key to use for the operation.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002843 * It must be an asymmetric key pair. The key must
2844 * allow the usage #PSA_KEY_USAGE_SIGN_MESSAGE.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002845 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002846 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2847 * is true), that is compatible with the type of
2848 * \p key.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002849 * \param[in] input The input message to sign.
2850 * \param[in] input_length Size of the \p input buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002851 * \param[out] signature Buffer where the signature is to be written.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002852 * \param[in] signature_size Size of the \p signature buffer in bytes. This
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002853 * must be appropriate for the selected
2854 * algorithm and key:
2855 * - The required signature size is
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002856 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2857 * where \c key_type and \c key_bits are the type and
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002858 * bit-size respectively of key.
2859 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
2860 * maximum signature size of any supported
2861 * signature algorithm.
2862 * \param[out] signature_length On success, the number of bytes that make up
2863 * the returned signature value.
2864 *
Gilles Peskineed733552023-02-14 19:21:09 +01002865 * \retval #PSA_SUCCESS \emptydescription
2866 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002867 * \retval #PSA_ERROR_NOT_PERMITTED
2868 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2869 * or it does not permit the requested algorithm.
2870 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2871 * The size of the \p signature buffer is too small. You can
2872 * determine a sufficient buffer size by calling
2873 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2874 * where \c key_type and \c key_bits are the type and bit-size
2875 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01002876 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2877 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2878 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2879 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2880 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2881 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2882 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2883 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
2884 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
2885 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002886 * \retval #PSA_ERROR_BAD_STATE
2887 * The library has not been previously initialized by psa_crypto_init().
2888 * It is implementation-dependent whether a failure to initialize
2889 * results in this error code.
2890 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002891psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2892 psa_algorithm_t alg,
2893 const uint8_t *input,
2894 size_t input_length,
2895 uint8_t *signature,
2896 size_t signature_size,
2897 size_t *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002898
2899/** \brief Verify the signature of a message with a public key, using
2900 * a hash-and-sign verification algorithm.
2901 *
2902 * \note To perform a multi-part hash-and-sign signature verification
2903 * algorithm, first use a multi-part hash operation to hash the message
2904 * and then pass the resulting hash to psa_verify_hash().
2905 * PSA_ALG_GET_HASH(\p alg) can be used to determine the hash algorithm
2906 * to use.
2907 *
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002908 * \param[in] key Identifier of the key to use for the operation.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002909 * It must be a public key or an asymmetric key
2910 * pair. The key must allow the usage
2911 * #PSA_KEY_USAGE_VERIFY_MESSAGE.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002912 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002913 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2914 * is true), that is compatible with the type of
2915 * \p key.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002916 * \param[in] input The message whose signature is to be verified.
2917 * \param[in] input_length Size of the \p input buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002918 * \param[out] signature Buffer containing the signature to verify.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002919 * \param[in] signature_length Size of the \p signature buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002920 *
Gilles Peskineed733552023-02-14 19:21:09 +01002921 * \retval #PSA_SUCCESS \emptydescription
2922 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002923 * \retval #PSA_ERROR_NOT_PERMITTED
2924 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2925 * or it does not permit the requested algorithm.
2926 * \retval #PSA_ERROR_INVALID_SIGNATURE
2927 * The calculation was performed successfully, but the passed signature
2928 * is not a valid signature.
Gilles Peskineed733552023-02-14 19:21:09 +01002929 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2930 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2931 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2932 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2933 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2934 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2935 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2936 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
2937 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002938 * \retval #PSA_ERROR_BAD_STATE
2939 * The library has not been previously initialized by psa_crypto_init().
2940 * It is implementation-dependent whether a failure to initialize
2941 * results in this error code.
2942 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002943psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2944 psa_algorithm_t alg,
2945 const uint8_t *input,
2946 size_t input_length,
2947 const uint8_t *signature,
2948 size_t signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002949
2950/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002951 * \brief Sign a hash or short message with a private key.
2952 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002953 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002954 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01002955 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
2956 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02002957 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2958 * to determine the hash algorithm to use.
2959 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002960 * \param key Identifier of the key to use for the operation.
2961 * It must be an asymmetric key pair. The key must
Ronald Cron96783552020-10-19 12:06:30 +02002962 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
Mateusz Starzyke6d3eda2021-08-26 11:46:14 +02002963 * \param alg A signature algorithm (PSA_ALG_XXX
2964 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
2965 * is true), that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002966 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002967 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002968 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002969 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002970 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002971 * \param[out] signature_length On success, the number of bytes
2972 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002973 *
Gilles Peskineed733552023-02-14 19:21:09 +01002974 * \retval #PSA_SUCCESS \emptydescription
2975 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2976 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002977 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002978 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002979 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002980 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002981 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002982 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01002983 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2984 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2985 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2986 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2987 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2988 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2989 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2990 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03002991 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002992 * The library has not been previously initialized by psa_crypto_init().
2993 * It is implementation-dependent whether a failure to initialize
2994 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002995 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002996psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002997 psa_algorithm_t alg,
2998 const uint8_t *hash,
2999 size_t hash_length,
3000 uint8_t *signature,
3001 size_t signature_size,
3002 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003003
3004/**
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003005 * \brief Verify the signature of a hash or short message using a public key.
Gilles Peskine20035e32018-02-03 22:44:14 +01003006 *
Gilles Peskine08bac712018-06-26 16:14:46 +02003007 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02003008 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01003009 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
3010 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02003011 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
3012 * to determine the hash algorithm to use.
3013 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003014 * \param key Identifier of the key to use for the operation. It
3015 * must be a public key or an asymmetric key pair. The
Ronald Cron96783552020-10-19 12:06:30 +02003016 * key must allow the usage
3017 * #PSA_KEY_USAGE_VERIFY_HASH.
Mateusz Starzyke6d3eda2021-08-26 11:46:14 +02003018 * \param alg A signature algorithm (PSA_ALG_XXX
3019 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
3020 * is true), that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02003021 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003022 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02003023 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003024 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003025 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003026 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003027 *
Gilles Peskine28538492018-07-11 17:34:00 +02003028 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01003029 * The signature is valid.
Gilles Peskineed733552023-02-14 19:21:09 +01003030 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3031 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02003032 * \retval #PSA_ERROR_INVALID_SIGNATURE
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003033 * The calculation was performed successfully, but the passed
Gilles Peskine308b91d2018-02-08 09:47:44 +01003034 * signature is not a valid signature.
Gilles Peskineed733552023-02-14 19:21:09 +01003035 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3036 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3037 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3038 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3039 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3040 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3041 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003042 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003043 * The library has not been previously initialized by psa_crypto_init().
3044 * It is implementation-dependent whether a failure to initialize
3045 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01003046 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003047psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003048 psa_algorithm_t alg,
3049 const uint8_t *hash,
3050 size_t hash_length,
3051 const uint8_t *signature,
3052 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003053
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003054/**
3055 * \brief Encrypt a short message with a public key.
3056 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003057 * \param key Identifier of the key to use for the operation.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003058 * It must be a public key or an asymmetric key
3059 * pair. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02003060 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003061 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003062 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003063 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003064 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003065 * \param[in] salt A salt or label, if supported by the
3066 * encryption algorithm.
3067 * If the algorithm does not support a
3068 * salt, pass \c NULL.
3069 * If the algorithm supports an optional
3070 * salt and you do not want to pass a salt,
3071 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003072 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003073 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3074 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003075 * \param salt_length Size of the \p salt buffer in bytes.
3076 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003077 * \param[out] output Buffer where the encrypted message is to
3078 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003079 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003080 * \param[out] output_length On success, the number of bytes
3081 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003082 *
Gilles Peskineed733552023-02-14 19:21:09 +01003083 * \retval #PSA_SUCCESS \emptydescription
3084 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3085 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02003086 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003087 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003088 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02003089 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003090 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003091 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01003092 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3093 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3094 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3095 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3096 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3097 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3098 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3099 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003100 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003101 * The library has not been previously initialized by psa_crypto_init().
3102 * It is implementation-dependent whether a failure to initialize
3103 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003104 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003105psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003106 psa_algorithm_t alg,
3107 const uint8_t *input,
3108 size_t input_length,
3109 const uint8_t *salt,
3110 size_t salt_length,
3111 uint8_t *output,
3112 size_t output_size,
3113 size_t *output_length);
3114
3115/**
3116 * \brief Decrypt a short message with a private key.
3117 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003118 * \param key Identifier of the key to use for the operation.
3119 * It must be an asymmetric key pair. It must
Ronald Cron96783552020-10-19 12:06:30 +02003120 * allow the usage #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003121 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003122 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003123 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003124 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003125 * \param[in] salt A salt or label, if supported by the
3126 * encryption algorithm.
3127 * If the algorithm does not support a
3128 * salt, pass \c NULL.
3129 * If the algorithm supports an optional
3130 * salt and you do not want to pass a salt,
3131 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003132 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003133 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3134 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003135 * \param salt_length Size of the \p salt buffer in bytes.
3136 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003137 * \param[out] output Buffer where the decrypted message is to
3138 * be written.
3139 * \param output_size Size of the \c output buffer in bytes.
3140 * \param[out] output_length On success, the number of bytes
3141 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003142 *
Gilles Peskineed733552023-02-14 19:21:09 +01003143 * \retval #PSA_SUCCESS \emptydescription
3144 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3145 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02003146 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003147 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003148 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003149 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003150 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003151 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01003152 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3153 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3154 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3155 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3156 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3157 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3158 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3159 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3160 * \retval #PSA_ERROR_INVALID_PADDING \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003161 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003162 * The library has not been previously initialized by psa_crypto_init().
3163 * It is implementation-dependent whether a failure to initialize
3164 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003165 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003166psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003167 psa_algorithm_t alg,
3168 const uint8_t *input,
3169 size_t input_length,
3170 const uint8_t *salt,
3171 size_t salt_length,
3172 uint8_t *output,
3173 size_t output_size,
3174 size_t *output_length);
3175
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003176/**@}*/
3177
Gilles Peskine35675b62019-05-16 17:26:11 +02003178/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003179 * @{
3180 */
3181
Gilles Peskine35675b62019-05-16 17:26:11 +02003182/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003183 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003184 * Before calling any function on a key derivation operation object, the
3185 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003186 * - Set the structure to all-bits-zero, for example:
3187 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003188 * psa_key_derivation_operation_t operation;
3189 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003190 * \endcode
3191 * - Initialize the structure to logical zero values, for example:
3192 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003193 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003194 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003195 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003196 * for example:
3197 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003198 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003199 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003200 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003201 * to the structure, for example:
3202 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003203 * psa_key_derivation_operation_t operation;
3204 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003205 * \endcode
3206 *
3207 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01003208 * make any assumptions about the content of this structure.
3209 * Implementation details can change in future versions without notice.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003210 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003211typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003212
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003213/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003214 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003215 * This macro returns a suitable initializer for a key derivation operation
3216 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003217 */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003218
Gilles Peskine35675b62019-05-16 17:26:11 +02003219/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003220 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003221static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003222
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003223/** Set up a key derivation operation.
3224 *
3225 * A key derivation algorithm takes some inputs and uses them to generate
3226 * a byte stream in a deterministic way.
3227 * This byte stream can be used to produce keys and other
3228 * cryptographic material.
3229 *
3230 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003231 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3232 * -# Call psa_key_derivation_setup() to select the algorithm.
3233 * -# Provide the inputs for the key derivation by calling
3234 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3235 * as appropriate. Which inputs are needed, in what order, and whether
3236 * they may be keys and if so of what type depends on the algorithm.
3237 * -# Optionally set the operation's maximum capacity with
3238 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3239 * of or after providing inputs. For some algorithms, this step is mandatory
3240 * because the output depends on the maximum capacity.
3241 * -# To derive a key, call psa_key_derivation_output_key().
3242 * To derive a byte string for a different purpose, call
3243 * psa_key_derivation_output_bytes().
3244 * Successive calls to these functions use successive output bytes
3245 * calculated by the key derivation algorithm.
3246 * -# Clean up the key derivation operation object with
3247 * psa_key_derivation_abort().
3248 *
3249 * If this function returns an error, the key derivation operation object is
3250 * not changed.
3251 *
3252 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3253 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003254 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003255 * Implementations must reject an attempt to derive a key of size 0.
3256 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003257 * \param[in,out] operation The key derivation operation object
3258 * to set up. It must
3259 * have been initialized but not set up yet.
3260 * \param alg The key derivation algorithm to compute
3261 * (\c PSA_ALG_XXX value such that
3262 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3263 *
3264 * \retval #PSA_SUCCESS
3265 * Success.
3266 * \retval #PSA_ERROR_INVALID_ARGUMENT
3267 * \c alg is not a key derivation algorithm.
3268 * \retval #PSA_ERROR_NOT_SUPPORTED
3269 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01003270 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3271 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3272 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3273 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3274 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003275 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003276 * The operation state is not valid (it must be inactive), or
3277 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003278 * It is implementation-dependent whether a failure to initialize
3279 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003280 */
3281psa_status_t psa_key_derivation_setup(
3282 psa_key_derivation_operation_t *operation,
3283 psa_algorithm_t alg);
3284
Gilles Peskine35675b62019-05-16 17:26:11 +02003285/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003286 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003287 * The capacity of a key derivation is the maximum number of bytes that it can
3288 * return. When you get *N* bytes of output from a key derivation operation,
3289 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003290 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003291 * \param[in] operation The operation to query.
3292 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003293 *
Gilles Peskineed733552023-02-14 19:21:09 +01003294 * \retval #PSA_SUCCESS \emptydescription
3295 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3296 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3297 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003298 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003299 * The operation state is not valid (it must be active), or
3300 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003301 * It is implementation-dependent whether a failure to initialize
3302 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003303 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003304psa_status_t psa_key_derivation_get_capacity(
3305 const psa_key_derivation_operation_t *operation,
3306 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003307
Gilles Peskine35675b62019-05-16 17:26:11 +02003308/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003309 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003310 * The capacity of a key derivation operation is the maximum number of bytes
3311 * that the key derivation operation can return from this point onwards.
3312 *
3313 * \param[in,out] operation The key derivation operation object to modify.
3314 * \param capacity The new capacity of the operation.
3315 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003316 * current capacity.
3317 *
Gilles Peskineed733552023-02-14 19:21:09 +01003318 * \retval #PSA_SUCCESS \emptydescription
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003319 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003320 * \p capacity is larger than the operation's current capacity.
3321 * In this case, the operation object remains valid and its capacity
3322 * remains unchanged.
Gilles Peskineed733552023-02-14 19:21:09 +01003323 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3324 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3325 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003326 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003327 * The operation state is not valid (it must be active), or the
3328 * library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003329 * It is implementation-dependent whether a failure to initialize
3330 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003331 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003332psa_status_t psa_key_derivation_set_capacity(
3333 psa_key_derivation_operation_t *operation,
3334 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003335
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003336/** Use the maximum possible capacity for a key derivation operation.
3337 *
3338 * Use this value as the capacity argument when setting up a key derivation
3339 * to indicate that the operation should have the maximum possible capacity.
3340 * The value of the maximum possible capacity depends on the key derivation
3341 * algorithm.
3342 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003343#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t) (-1))
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003344
3345/** Provide an input for key derivation or key agreement.
3346 *
3347 * Which inputs are required and in what order depends on the algorithm.
3348 * Refer to the documentation of each key derivation or key agreement
3349 * algorithm for information.
3350 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003351 * This function passes direct inputs, which is usually correct for
3352 * non-secret inputs. To pass a secret input, which should be in a key
3353 * object, call psa_key_derivation_input_key() instead of this function.
3354 * Refer to the documentation of individual step types
3355 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3356 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003357 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003358 * If this function returns an error status, the operation enters an error
3359 * state and must be aborted by calling psa_key_derivation_abort().
3360 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003361 * \param[in,out] operation The key derivation operation object to use.
3362 * It must have been set up with
3363 * psa_key_derivation_setup() and must not
3364 * have produced any output yet.
3365 * \param step Which step the input data is for.
3366 * \param[in] data Input data to use.
3367 * \param data_length Size of the \p data buffer in bytes.
3368 *
3369 * \retval #PSA_SUCCESS
3370 * Success.
3371 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003372 * \c step is not compatible with the operation's algorithm, or
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003373 * \c step does not allow direct inputs.
Gilles Peskineed733552023-02-14 19:21:09 +01003374 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3375 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3376 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3377 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3378 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003379 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003380 * The operation state is not valid for this input \p step, or
3381 * the library has not been previously initialized by psa_crypto_init().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003382 * It is implementation-dependent whether a failure to initialize
3383 * results in this error code.
3384 */
3385psa_status_t psa_key_derivation_input_bytes(
3386 psa_key_derivation_operation_t *operation,
3387 psa_key_derivation_step_t step,
3388 const uint8_t *data,
3389 size_t data_length);
3390
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003391/** Provide a numeric input for key derivation or key agreement.
3392 *
3393 * Which inputs are required and in what order depends on the algorithm.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003394 * However, when an algorithm requires a particular order, numeric inputs
3395 * usually come first as they tend to be configuration parameters.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003396 * Refer to the documentation of each key derivation or key agreement
3397 * algorithm for information.
3398 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003399 * This function is used for inputs which are fixed-size non-negative
3400 * integers.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003401 *
3402 * If this function returns an error status, the operation enters an error
3403 * state and must be aborted by calling psa_key_derivation_abort().
3404 *
3405 * \param[in,out] operation The key derivation operation object to use.
3406 * It must have been set up with
3407 * psa_key_derivation_setup() and must not
3408 * have produced any output yet.
3409 * \param step Which step the input data is for.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003410 * \param[in] value The value of the numeric input.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003411 *
3412 * \retval #PSA_SUCCESS
3413 * Success.
3414 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003415 * \c step is not compatible with the operation's algorithm, or
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003416 * \c step does not allow numeric inputs.
Gilles Peskineed733552023-02-14 19:21:09 +01003417 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3418 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3419 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3420 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3421 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003422 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003423 * The operation state is not valid for this input \p step, or
3424 * the library has not been previously initialized by psa_crypto_init().
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003425 * It is implementation-dependent whether a failure to initialize
3426 * results in this error code.
3427 */
3428psa_status_t psa_key_derivation_input_integer(
3429 psa_key_derivation_operation_t *operation,
3430 psa_key_derivation_step_t step,
3431 uint64_t value);
3432
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003433/** Provide an input for key derivation in the form of a key.
3434 *
3435 * Which inputs are required and in what order depends on the algorithm.
3436 * Refer to the documentation of each key derivation or key agreement
3437 * algorithm for information.
3438 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003439 * This function obtains input from a key object, which is usually correct for
3440 * secret inputs or for non-secret personalization strings kept in the key
3441 * store. To pass a non-secret parameter which is not in the key store,
3442 * call psa_key_derivation_input_bytes() instead of this function.
3443 * Refer to the documentation of individual step types
3444 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3445 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003446 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003447 * If this function returns an error status, the operation enters an error
3448 * state and must be aborted by calling psa_key_derivation_abort().
3449 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003450 * \param[in,out] operation The key derivation operation object to use.
3451 * It must have been set up with
3452 * psa_key_derivation_setup() and must not
3453 * have produced any output yet.
3454 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003455 * \param key Identifier of the key. It must have an
3456 * appropriate type for step and must allow the
Manuel Pégourié-Gonnardacfde462021-05-05 09:54:22 +02003457 * usage #PSA_KEY_USAGE_DERIVE or
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003458 * #PSA_KEY_USAGE_VERIFY_DERIVATION (see note)
3459 * and the algorithm used by the operation.
3460 *
3461 * \note Once all inputs steps are completed, the operations will allow:
3462 * - psa_key_derivation_output_bytes() if each input was either a direct input
3463 * or a key with #PSA_KEY_USAGE_DERIVE set;
Manuel Pégourié-Gonnarde88511d2021-05-07 12:19:03 +02003464 * - psa_key_derivation_output_key() if the input for step
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003465 * #PSA_KEY_DERIVATION_INPUT_SECRET or #PSA_KEY_DERIVATION_INPUT_PASSWORD
Manuel Pégourié-Gonnarde88511d2021-05-07 12:19:03 +02003466 * was from a key slot with #PSA_KEY_USAGE_DERIVE and each other input was
3467 * either a direct input or a key with #PSA_KEY_USAGE_DERIVE set;
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003468 * - psa_key_derivation_verify_bytes() if each input was either a direct input
3469 * or a key with #PSA_KEY_USAGE_VERIFY_DERIVATION set;
Manuel Pégourié-Gonnard4feb6112021-05-07 12:22:21 +02003470 * - psa_key_derivation_verify_key() under the same conditions as
3471 * psa_key_derivation_verify_bytes().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003472 *
3473 * \retval #PSA_SUCCESS
3474 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01003475 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003476 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003477 * The key allows neither #PSA_KEY_USAGE_DERIVE nor
3478 * #PSA_KEY_USAGE_VERIFY_DERIVATION, or it doesn't allow this
3479 * algorithm.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003480 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003481 * \c step is not compatible with the operation's algorithm, or
Gilles Peskine224b0d62019-09-23 18:13:17 +02003482 * \c step does not allow key inputs of the given type
3483 * or does not allow key inputs at all.
Gilles Peskineed733552023-02-14 19:21:09 +01003484 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3485 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3486 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3487 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3488 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003489 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003490 * The operation state is not valid for this input \p step, or
3491 * the library has not been previously initialized by psa_crypto_init().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003492 * It is implementation-dependent whether a failure to initialize
3493 * results in this error code.
3494 */
3495psa_status_t psa_key_derivation_input_key(
3496 psa_key_derivation_operation_t *operation,
3497 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003498 mbedtls_svc_key_id_t key);
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003499
3500/** Perform a key agreement and use the shared secret as input to a key
3501 * derivation.
3502 *
3503 * A key agreement algorithm takes two inputs: a private key \p private_key
3504 * a public key \p peer_key.
3505 * The result of this function is passed as input to a key derivation.
3506 * The output of this key derivation can be extracted by reading from the
3507 * resulting operation to produce keys and other cryptographic material.
3508 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003509 * If this function returns an error status, the operation enters an error
3510 * state and must be aborted by calling psa_key_derivation_abort().
3511 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003512 * \param[in,out] operation The key derivation operation object to use.
3513 * It must have been set up with
3514 * psa_key_derivation_setup() with a
3515 * key agreement and derivation algorithm
3516 * \c alg (\c PSA_ALG_XXX value such that
3517 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3518 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3519 * is false).
3520 * The operation must be ready for an
3521 * input of the type given by \p step.
3522 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003523 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003524 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003525 * \param[in] peer_key Public key of the peer. The peer key must be in the
3526 * same format that psa_import_key() accepts for the
3527 * public key type corresponding to the type of
3528 * private_key. That is, this function performs the
3529 * equivalent of
3530 * #psa_import_key(...,
3531 * `peer_key`, `peer_key_length`) where
3532 * with key attributes indicating the public key
3533 * type corresponding to the type of `private_key`.
3534 * For example, for EC keys, this means that peer_key
3535 * is interpreted as a point on the curve that the
3536 * private key is on. The standard formats for public
3537 * keys are documented in the documentation of
3538 * psa_export_public_key().
3539 * \param peer_key_length Size of \p peer_key in bytes.
3540 *
3541 * \retval #PSA_SUCCESS
3542 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01003543 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3544 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003545 * \retval #PSA_ERROR_INVALID_ARGUMENT
3546 * \c private_key is not compatible with \c alg,
3547 * or \p peer_key is not valid for \c alg or not compatible with
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003548 * \c private_key, or \c step does not allow an input resulting
3549 * from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003550 * \retval #PSA_ERROR_NOT_SUPPORTED
3551 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01003552 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3553 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3554 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3555 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3556 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003557 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003558 * The operation state is not valid for this key agreement \p step,
3559 * or the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003560 * It is implementation-dependent whether a failure to initialize
3561 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003562 */
3563psa_status_t psa_key_derivation_key_agreement(
3564 psa_key_derivation_operation_t *operation,
3565 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003566 mbedtls_svc_key_id_t private_key,
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003567 const uint8_t *peer_key,
3568 size_t peer_key_length);
3569
Gilles Peskine35675b62019-05-16 17:26:11 +02003570/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003571 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003572 * This function calculates output bytes from a key derivation algorithm and
3573 * return those bytes.
3574 * If you view the key derivation's output as a stream of bytes, this
3575 * function destructively reads the requested number of bytes from the
3576 * stream.
3577 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003578 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003579 * If this function returns an error status other than
3580 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003581 * state and must be aborted by calling psa_key_derivation_abort().
3582 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003583 * \param[in,out] operation The key derivation operation object to read from.
3584 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003585 * \param output_length Number of bytes to output.
3586 *
Gilles Peskineed733552023-02-14 19:21:09 +01003587 * \retval #PSA_SUCCESS \emptydescription
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003588 * \retval #PSA_ERROR_NOT_PERMITTED
3589 * One of the inputs was a key whose policy didn't allow
3590 * #PSA_KEY_USAGE_DERIVE.
David Saadab4ecc272019-02-14 13:48:10 +02003591 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003592 * The operation's capacity was less than
3593 * \p output_length bytes. Note that in this case,
3594 * no output is written to the output buffer.
3595 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003596 * subsequent calls to this function will not
3597 * succeed, even with a smaller output buffer.
Gilles Peskineed733552023-02-14 19:21:09 +01003598 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3599 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3600 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3601 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3602 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003603 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003604 * The operation state is not valid (it must be active and completed
3605 * all required input steps), or the library has not been previously
3606 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003607 * It is implementation-dependent whether a failure to initialize
3608 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003609 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003610psa_status_t psa_key_derivation_output_bytes(
3611 psa_key_derivation_operation_t *operation,
3612 uint8_t *output,
3613 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003614
Gilles Peskine35675b62019-05-16 17:26:11 +02003615/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003616 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003617 * This function calculates output bytes from a key derivation algorithm
3618 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003619 * The key's location, usage policy, type and size are taken from
3620 * \p attributes.
3621 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003622 * If you view the key derivation's output as a stream of bytes, this
3623 * function destructively reads as many bytes as required from the
3624 * stream.
3625 * The operation's capacity decreases by the number of bytes read.
3626 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003627 * If this function returns an error status other than
3628 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003629 * state and must be aborted by calling psa_key_derivation_abort().
3630 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003631 * How much output is produced and consumed from the operation, and how
Gilles Peskine364d12c2021-03-08 17:23:47 +01003632 * the key is derived, depends on the key type and on the key size
3633 * (denoted \c bits below):
Gilles Peskineeab56e42018-07-12 17:12:33 +02003634 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003635 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003636 * of a given size, this function is functionally equivalent to
3637 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003638 * and passing the resulting output to #psa_import_key.
3639 * However, this function has a security benefit:
3640 * if the implementation provides an isolation boundary then
3641 * the key material is not exposed outside the isolation boundary.
3642 * As a consequence, for these key types, this function always consumes
Gilles Peskine364d12c2021-03-08 17:23:47 +01003643 * exactly (\c bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003644 * The following key types defined in this specification follow this scheme:
3645 *
3646 * - #PSA_KEY_TYPE_AES;
Gilles Peskine6c12a1e2021-09-21 11:59:39 +02003647 * - #PSA_KEY_TYPE_ARIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003648 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003649 * - #PSA_KEY_TYPE_DERIVE;
Manuel Pégourié-Gonnard2171e422021-05-03 10:49:54 +02003650 * - #PSA_KEY_TYPE_HMAC;
3651 * - #PSA_KEY_TYPE_PASSWORD_HASH.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003652 *
3653 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003654 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003655 * Montgomery curve), this function always draws a byte string whose
3656 * length is determined by the curve, and sets the mandatory bits
3657 * accordingly. That is:
3658 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01003659 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003660 * string and process it as specified in RFC 7748 &sect;5.
Paul Elliott8ff510a2020-06-02 17:19:28 +01003661 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003662 * string and process it as specified in RFC 7748 &sect;5.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003663 *
3664 * - For key types for which the key is represented by a single sequence of
Gilles Peskine364d12c2021-03-08 17:23:47 +01003665 * \c bits bits with constraints as to which bit sequences are acceptable,
3666 * this function draws a byte string of length (\c bits / 8) bytes rounded
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003667 * up to the nearest whole number of bytes. If the resulting byte string
3668 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3669 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003670 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003671 * for the output produced by psa_export_key().
3672 * The following key types defined in this specification follow this scheme:
3673 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003674 * - #PSA_KEY_TYPE_DES.
3675 * Force-set the parity bits, but discard forbidden weak keys.
3676 * For 2-key and 3-key triple-DES, the three keys are generated
3677 * successively (for example, for 3-key triple-DES,
3678 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3679 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003680 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003681 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003682 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003683 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003684 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003685 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003686 * Weierstrass curve).
3687 * For these key types, interpret the byte string as integer
3688 * in big-endian order. Discard it if it is not in the range
3689 * [0, *N* - 2] where *N* is the boundary of the private key domain
3690 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003691 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003692 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003693 * This method allows compliance to NIST standards, specifically
3694 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003695 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3696 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3697 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3698 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003699 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003700 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003701 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003702 * implementation-defined.
3703 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003704 * In all cases, the data that is read is discarded from the operation.
3705 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003706 *
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003707 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3708 * the input to that step must be provided with psa_key_derivation_input_key().
3709 * Future versions of this specification may include additional restrictions
3710 * on the derived key based on the attributes and strength of the secret key.
3711 *
Gilles Peskine20628592019-04-19 19:29:50 +02003712 * \param[in] attributes The attributes for the new key.
Manuel Pégourié-Gonnard2c44daf2021-05-10 12:53:30 +02003713 * If the key type to be created is
3714 * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
3715 * the policy must be the same as in the current
3716 * operation.
Gilles Peskine35675b62019-05-16 17:26:11 +02003717 * \param[in,out] operation The key derivation operation object to read from.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003718 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003719 * key. For persistent keys, this is the key
3720 * identifier defined in \p attributes.
3721 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003722 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003723 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003724 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003725 * If the key is persistent, the key material and the key's metadata
3726 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003727 * \retval #PSA_ERROR_ALREADY_EXISTS
3728 * This is an attempt to create a persistent key, and there is
3729 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003730 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003731 * There was not enough data to create the desired key.
3732 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003733 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003734 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003735 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003736 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003737 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003738 * \retval #PSA_ERROR_INVALID_ARGUMENT
3739 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003740 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003741 * The #PSA_KEY_DERIVATION_INPUT_SECRET or
3742 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
3743 * key; or one of the inputs was a key whose policy didn't allow
3744 * #PSA_KEY_USAGE_DERIVE.
Gilles Peskineed733552023-02-14 19:21:09 +01003745 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3746 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
3747 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3748 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3749 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3750 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
3751 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
3752 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003753 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003754 * The operation state is not valid (it must be active and completed
3755 * all required input steps), or the library has not been previously
3756 * initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03003757 * It is implementation-dependent whether a failure to initialize
3758 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003759 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003760psa_status_t psa_key_derivation_output_key(
3761 const psa_key_attributes_t *attributes,
3762 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003763 mbedtls_svc_key_id_t *key);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003764
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003765/** Compare output data from a key derivation operation to an expected value.
3766 *
3767 * This function calculates output bytes from a key derivation algorithm and
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003768 * compares those bytes to an expected value in constant time.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003769 * If you view the key derivation's output as a stream of bytes, this
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003770 * function destructively reads the expected number of bytes from the
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003771 * stream before comparing them.
3772 * The operation's capacity decreases by the number of bytes read.
3773 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003774 * This is functionally equivalent to the following code:
3775 * \code
3776 * psa_key_derivation_output_bytes(operation, tmp, output_length);
3777 * if (memcmp(output, tmp, output_length) != 0)
3778 * return PSA_ERROR_INVALID_SIGNATURE;
3779 * \endcode
3780 * except (1) it works even if the key's policy does not allow outputting the
3781 * bytes, and (2) the comparison will be done in constant time.
3782 *
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003783 * If this function returns an error status other than
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003784 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3785 * the operation enters an error state and must be aborted by calling
3786 * psa_key_derivation_abort().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003787 *
3788 * \param[in,out] operation The key derivation operation object to read from.
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003789 * \param[in] expected_output Buffer containing the expected derivation output.
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003790 * \param output_length Length of the expected output; this is also the
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003791 * number of bytes that will be read.
3792 *
Gilles Peskineed733552023-02-14 19:21:09 +01003793 * \retval #PSA_SUCCESS \emptydescription
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003794 * \retval #PSA_ERROR_INVALID_SIGNATURE
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003795 * The output was read successfully, but it differs from the expected
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003796 * output.
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003797 * \retval #PSA_ERROR_NOT_PERMITTED
3798 * One of the inputs was a key whose policy didn't allow
3799 * #PSA_KEY_USAGE_VERIFY_DERIVATION.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003800 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3801 * The operation's capacity was less than
3802 * \p output_length bytes. Note that in this case,
3803 * the operation's capacity is set to 0, thus
3804 * subsequent calls to this function will not
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003805 * succeed, even with a smaller expected output.
Gilles Peskineed733552023-02-14 19:21:09 +01003806 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3807 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3808 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3809 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3810 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003811 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003812 * The operation state is not valid (it must be active and completed
3813 * all required input steps), or the library has not been previously
3814 * initialized by psa_crypto_init().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003815 * It is implementation-dependent whether a failure to initialize
3816 * results in this error code.
3817 */
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003818psa_status_t psa_key_derivation_verify_bytes(
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003819 psa_key_derivation_operation_t *operation,
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003820 const uint8_t *expected_output,
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003821 size_t output_length);
3822
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003823/** Compare output data from a key derivation operation to an expected value
3824 * stored in a key object.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003825 *
3826 * This function calculates output bytes from a key derivation algorithm and
3827 * compares those bytes to an expected value, provided as key of type
Manuel Pégourié-Gonnard4e02f012021-05-12 10:05:45 +02003828 * #PSA_KEY_TYPE_PASSWORD_HASH.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003829 * If you view the key derivation's output as a stream of bytes, this
Tom Cosgrove1797b052022-12-04 17:19:59 +00003830 * function destructively reads the number of bytes corresponding to the
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003831 * length of the expected value from the stream before comparing them.
3832 * The operation's capacity decreases by the number of bytes read.
3833 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003834 * This is functionally equivalent to exporting the key and calling
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003835 * psa_key_derivation_verify_bytes() on the result, except that it
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003836 * works even if the key cannot be exported.
3837 *
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003838 * If this function returns an error status other than
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003839 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3840 * the operation enters an error state and must be aborted by calling
3841 * psa_key_derivation_abort().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003842 *
3843 * \param[in,out] operation The key derivation operation object to read from.
Manuel Pégourié-Gonnard2171e422021-05-03 10:49:54 +02003844 * \param[in] expected A key of type #PSA_KEY_TYPE_PASSWORD_HASH
3845 * containing the expected output. Its policy must
3846 * include the #PSA_KEY_USAGE_VERIFY_DERIVATION flag
3847 * and the permitted algorithm must match the
3848 * operation. The value of this key was likely
3849 * computed by a previous call to
3850 * psa_key_derivation_output_key().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003851 *
Gilles Peskineed733552023-02-14 19:21:09 +01003852 * \retval #PSA_SUCCESS \emptydescription
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003853 * \retval #PSA_ERROR_INVALID_SIGNATURE
3854 * The output was read successfully, but if differs from the expected
3855 * output.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003856 * \retval #PSA_ERROR_INVALID_HANDLE
3857 * The key passed as the expected value does not exist.
3858 * \retval #PSA_ERROR_INVALID_ARGUMENT
3859 * The key passed as the expected value has an invalid type.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003860 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnard88658be2021-05-03 10:28:57 +02003861 * The key passed as the expected value does not allow this usage or
Manuel Pégourié-Gonnard4feb6112021-05-07 12:22:21 +02003862 * this algorithm; or one of the inputs was a key whose policy didn't
3863 * allow #PSA_KEY_USAGE_VERIFY_DERIVATION.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003864 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3865 * The operation's capacity was less than
3866 * the length of the expected value. In this case,
3867 * the operation's capacity is set to 0, thus
3868 * subsequent calls to this function will not
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003869 * succeed, even with a smaller expected output.
Gilles Peskineed733552023-02-14 19:21:09 +01003870 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3871 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3872 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3873 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3874 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003875 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003876 * The operation state is not valid (it must be active and completed
3877 * all required input steps), or the library has not been previously
3878 * initialized by psa_crypto_init().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003879 * It is implementation-dependent whether a failure to initialize
3880 * results in this error code.
3881 */
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003882psa_status_t psa_key_derivation_verify_key(
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003883 psa_key_derivation_operation_t *operation,
3884 psa_key_id_t expected);
3885
Gilles Peskine35675b62019-05-16 17:26:11 +02003886/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003887 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003888 * Aborting an operation frees all associated resources except for the \c
3889 * operation structure itself. Once aborted, the operation object can be reused
3890 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003891 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003892 * This function may be called at any time after the operation
3893 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003894 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003895 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3896 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003897 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003898 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003899 *
Gilles Peskineed733552023-02-14 19:21:09 +01003900 * \retval #PSA_SUCCESS \emptydescription
3901 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3902 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3903 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003904 * \retval #PSA_ERROR_BAD_STATE
3905 * The library has not been previously initialized by psa_crypto_init().
3906 * It is implementation-dependent whether a failure to initialize
3907 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003908 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003909psa_status_t psa_key_derivation_abort(
3910 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003911
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003912/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003913 *
3914 * \warning The raw result of a key agreement algorithm such as finite-field
3915 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3916 * not be used directly as key material. It should instead be passed as
3917 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003918 * a key derivation, use psa_key_derivation_key_agreement() and other
3919 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003920 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003921 * \param alg The key agreement algorithm to compute
3922 * (\c PSA_ALG_XXX value such that
3923 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3924 * is true).
Ronald Croncf56a0a2020-08-04 09:51:30 +02003925 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003926 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003927 * \param[in] peer_key Public key of the peer. It must be
3928 * in the same format that psa_import_key()
3929 * accepts. The standard formats for public
3930 * keys are documented in the documentation
3931 * of psa_export_public_key().
3932 * \param peer_key_length Size of \p peer_key in bytes.
3933 * \param[out] output Buffer where the decrypted message is to
3934 * be written.
3935 * \param output_size Size of the \c output buffer in bytes.
3936 * \param[out] output_length On success, the number of bytes
3937 * that make up the returned output.
3938 *
3939 * \retval #PSA_SUCCESS
3940 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01003941 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3942 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine769c7a62019-01-18 16:42:29 +01003943 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003944 * \p alg is not a key agreement algorithm, or
Gilles Peskine769c7a62019-01-18 16:42:29 +01003945 * \p private_key is not compatible with \p alg,
3946 * or \p peer_key is not valid for \p alg or not compatible with
3947 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003948 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3949 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003950 * \retval #PSA_ERROR_NOT_SUPPORTED
3951 * \p alg is not a supported key agreement algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01003952 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3953 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3954 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3955 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3956 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003957 * \retval #PSA_ERROR_BAD_STATE
3958 * The library has not been previously initialized by psa_crypto_init().
3959 * It is implementation-dependent whether a failure to initialize
3960 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003961 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003962psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003963 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02003964 const uint8_t *peer_key,
3965 size_t peer_key_length,
3966 uint8_t *output,
3967 size_t output_size,
3968 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003969
Gilles Peskineea0fb492018-07-12 17:17:20 +02003970/**@}*/
3971
Gilles Peskineedd76872018-07-20 17:42:05 +02003972/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003973 * @{
3974 */
3975
3976/**
3977 * \brief Generate random bytes.
3978 *
3979 * \warning This function **can** fail! Callers MUST check the return status
3980 * and MUST NOT use the content of the output buffer if the return
3981 * status is not #PSA_SUCCESS.
3982 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003983 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003984 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003985 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003986 * \param output_size Number of bytes to generate and output.
3987 *
Gilles Peskineed733552023-02-14 19:21:09 +01003988 * \retval #PSA_SUCCESS \emptydescription
3989 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3990 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3991 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3992 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3993 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3994 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
itayzafrir0adf0fc2018-09-06 16:24:41 +03003995 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003996 * The library has not been previously initialized by psa_crypto_init().
3997 * It is implementation-dependent whether a failure to initialize
3998 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003999 */
4000psa_status_t psa_generate_random(uint8_t *output,
4001 size_t output_size);
4002
4003/**
4004 * \brief Generate a key or key pair.
4005 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02004006 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02004007 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02004008 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02004009 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02004010 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02004011 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004012 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02004013 * the public exponent is 65537.
4014 * The modulus is a product of two probabilistic primes
4015 * between 2^{n-1} and 2^n where n is the bit size specified in the
4016 * attributes.
4017 *
Gilles Peskine20628592019-04-19 19:29:50 +02004018 * \param[in] attributes The attributes for the new key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02004019 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02004020 * key. For persistent keys, this is the key
4021 * identifier defined in \p attributes.
4022 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004023 *
Gilles Peskine28538492018-07-11 17:34:00 +02004024 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01004025 * Success.
4026 * If the key is persistent, the key material and the key's metadata
4027 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02004028 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02004029 * This is an attempt to create a persistent key, and there is
4030 * already a persistent key with the given identifier.
Gilles Peskineed733552023-02-14 19:21:09 +01004031 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4032 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4033 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4034 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4035 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4036 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4037 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4038 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
4039 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4040 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4041 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03004042 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03004043 * The library has not been previously initialized by psa_crypto_init().
4044 * It is implementation-dependent whether a failure to initialize
4045 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004046 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004047psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004048 mbedtls_svc_key_id_t *key);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004049
4050/**@}*/
4051
Paul Elliott1265f002022-09-09 17:15:43 +01004052/** \defgroup interruptible_hash Interruptible sign/verify hash
4053 * @{
4054 */
4055
4056/** The type of the state data structure for interruptible hash
4057 * signing operations.
4058 *
4059 * Before calling any function on a sign hash operation object, the
4060 * application must initialize it by any of the following means:
4061 * - Set the structure to all-bits-zero, for example:
4062 * \code
4063 * psa_sign_hash_interruptible_operation_t operation;
4064 * memset(&operation, 0, sizeof(operation));
4065 * \endcode
4066 * - Initialize the structure to logical zero values, for example:
4067 * \code
4068 * psa_sign_hash_interruptible_operation_t operation = {0};
4069 * \endcode
4070 * - Initialize the structure to the initializer
4071 * #PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:
4072 * \code
4073 * psa_sign_hash_interruptible_operation_t operation =
4074 * PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT;
4075 * \endcode
4076 * - Assign the result of the function
4077 * psa_sign_hash_interruptible_operation_init() to the structure, for
4078 * example:
4079 * \code
4080 * psa_sign_hash_interruptible_operation_t operation;
4081 * operation = psa_sign_hash_interruptible_operation_init();
4082 * \endcode
4083 *
4084 * This is an implementation-defined \c struct. Applications should not
4085 * make any assumptions about the content of this structure.
4086 * Implementation details can change in future versions without notice. */
4087typedef struct psa_sign_hash_interruptible_operation_s psa_sign_hash_interruptible_operation_t;
4088
4089/** The type of the state data structure for interruptible hash
4090 * verification operations.
4091 *
4092 * Before calling any function on a sign hash operation object, the
4093 * application must initialize it by any of the following means:
4094 * - Set the structure to all-bits-zero, for example:
4095 * \code
4096 * psa_verify_hash_interruptible_operation_t operation;
4097 * memset(&operation, 0, sizeof(operation));
4098 * \endcode
4099 * - Initialize the structure to logical zero values, for example:
4100 * \code
4101 * psa_verify_hash_interruptible_operation_t operation = {0};
4102 * \endcode
4103 * - Initialize the structure to the initializer
4104 * #PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:
4105 * \code
4106 * psa_verify_hash_interruptible_operation_t operation =
4107 * PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT;
4108 * \endcode
4109 * - Assign the result of the function
4110 * psa_verify_hash_interruptible_operation_init() to the structure, for
4111 * example:
4112 * \code
4113 * psa_verify_hash_interruptible_operation_t operation;
4114 * operation = psa_verify_hash_interruptible_operation_init();
4115 * \endcode
4116 *
4117 * This is an implementation-defined \c struct. Applications should not
4118 * make any assumptions about the content of this structure.
4119 * Implementation details can change in future versions without notice. */
4120typedef struct psa_verify_hash_interruptible_operation_s psa_verify_hash_interruptible_operation_t;
4121
4122/**
4123 * \brief Set the maximum number of ops allowed to be
4124 * executed by an interruptible function in a
4125 * single call.
4126 *
4127 * \warning This is a beta API, and thus subject to change
4128 * at any point. It is not bound by the usual
4129 * interface stability promises.
4130 *
4131 * \note The time taken to execute a single op is
4132 * implementation specific and depends on
4133 * software, hardware, the algorithm, key type and
4134 * curve chosen. Even within a single operation,
4135 * successive ops can take differing amounts of
4136 * time. The only guarantee is that lower values
4137 * for \p max_ops means functions will block for a
4138 * lesser maximum amount of time. The functions
4139 * \c psa_sign_interruptible_get_num_ops() and
4140 * \c psa_verify_interruptible_get_num_ops() are
4141 * provided to help with tuning this value.
4142 *
4143 * \note This value defaults to
4144 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, which
4145 * means the whole operation will be done in one
4146 * go, regardless of the number of ops required.
4147 *
4148 * \note If more ops are needed to complete a
4149 * computation, #PSA_OPERATION_INCOMPLETE will be
4150 * returned by the function performing the
4151 * computation. It is then the caller's
4152 * responsibility to either call again with the
4153 * same operation context until it returns 0 or an
4154 * error code; or to call the relevant abort
4155 * function if the answer is no longer required.
4156 *
4157 * \note The interpretation of \p max_ops is also
4158 * implementation defined. On a hard real time
4159 * system, this can indicate a hard deadline, as a
4160 * real-time system needs a guarantee of not
4161 * spending more than X time, however care must be
4162 * taken in such an implementation to avoid the
4163 * situation whereby calls just return, not being
4164 * able to do any actual work within the allotted
4165 * time. On a non-real-time system, the
4166 * implementation can be more relaxed, but again
4167 * whether this number should be interpreted as as
4168 * hard or soft limit or even whether a less than
4169 * or equals as regards to ops executed in a
4170 * single call is implementation defined.
4171 *
Paul Elliott21c39512023-02-15 19:47:39 +00004172 * \note For keys in local storage when no accelerator
4173 * driver applies, please see also the
4174 * documentation for \c mbedtls_ecp_set_max_ops(),
4175 * which is the internal implementation in these
4176 * cases.
4177 *
Paul Elliott1265f002022-09-09 17:15:43 +01004178 * \warning With implementations that interpret this number
4179 * as a hard limit, setting this number too small
4180 * may result in an infinite loop, whereby each
4181 * call results in immediate return with no ops
4182 * done (as there is not enough time to execute
4183 * any), and thus no result will ever be achieved.
4184 *
4185 * \note This only applies to functions whose
4186 * documentation mentions they may return
4187 * #PSA_OPERATION_INCOMPLETE.
4188 *
4189 * \param max_ops The maximum number of ops to be executed in a
4190 * single call. This can be a number from 0 to
4191 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, where 0
4192 * is the least amount of work done per call.
4193 */
4194void psa_interruptible_set_max_ops(uint32_t max_ops);
4195
4196/**
4197 * \brief Get the maximum number of ops allowed to be
4198 * executed by an interruptible function in a
4199 * single call. This will return the last
4200 * value set by
4201 * \c psa_interruptible_set_max_ops() or
4202 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED if
4203 * that function has never been called.
4204 *
4205 * \warning This is a beta API, and thus subject to change
4206 * at any point. It is not bound by the usual
4207 * interface stability promises.
4208 *
4209 * \return Maximum number of ops allowed to be
4210 * executed by an interruptible function in a
4211 * single call.
4212 */
4213uint32_t psa_interruptible_get_max_ops(void);
4214
4215/**
4216 * \brief Get the number of ops that a hash signing
4217 * operation has taken so far. If the operation
4218 * has completed, then this will represent the
4219 * number of ops required for the entire
4220 * operation. After initialization or calling
4221 * \c psa_sign_hash_interruptible_abort() on
4222 * the operation, a value of 0 will be returned.
4223 *
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004224 * \note This interface is guaranteed re-entrant and
4225 * thus may be called from driver code.
4226 *
Paul Elliott1265f002022-09-09 17:15:43 +01004227 * \warning This is a beta API, and thus subject to change
4228 * at any point. It is not bound by the usual
4229 * interface stability promises.
4230 *
4231 * This is a helper provided to help you tune the
4232 * value passed to \c
4233 * psa_interruptible_set_max_ops().
4234 *
4235 * \param operation The \c psa_sign_hash_interruptible_operation_t
4236 * to use. This must be initialized first.
4237 *
4238 * \return Number of ops that the operation has taken so
4239 * far.
4240 */
4241uint32_t psa_sign_hash_get_num_ops(
4242 const psa_sign_hash_interruptible_operation_t *operation);
4243
4244/**
4245 * \brief Get the number of ops that a hash verification
4246 * operation has taken so far. If the operation
4247 * has completed, then this will represent the
4248 * number of ops required for the entire
4249 * operation. After initialization or calling \c
4250 * psa_verify_hash_interruptible_abort() on the
4251 * operation, a value of 0 will be returned.
4252 *
4253 * \warning This is a beta API, and thus subject to change
4254 * at any point. It is not bound by the usual
4255 * interface stability promises.
4256 *
4257 * This is a helper provided to help you tune the
4258 * value passed to \c
4259 * psa_interruptible_set_max_ops().
4260 *
4261 * \param operation The \c
4262 * psa_verify_hash_interruptible_operation_t to
4263 * use. This must be initialized first.
4264 *
4265 * \return Number of ops that the operation has taken so
4266 * far.
4267 */
4268uint32_t psa_verify_hash_get_num_ops(
4269 const psa_verify_hash_interruptible_operation_t *operation);
4270
4271/**
4272 * \brief Start signing a hash or short message with a
4273 * private key, in an interruptible manner.
4274 *
4275 * \see \c psa_sign_hash_complete()
4276 *
4277 * \warning This is a beta API, and thus subject to change
4278 * at any point. It is not bound by the usual
4279 * interface stability promises.
4280 *
4281 * \note This function combined with \c
4282 * psa_sign_hash_complete() is equivalent to
4283 * \c psa_sign_hash() but
4284 * \c psa_sign_hash_complete() can return early and
4285 * resume according to the limit set with \c
4286 * psa_interruptible_set_max_ops() to reduce the
4287 * maximum time spent in a function call.
4288 *
4289 * \note Users should call \c psa_sign_hash_complete()
4290 * repeatedly on the same context after a
4291 * successful call to this function until \c
4292 * psa_sign_hash_complete() either returns 0 or an
4293 * error. \c psa_sign_hash_complete() will return
4294 * #PSA_OPERATION_INCOMPLETE if there is more work
4295 * to do. Alternatively users can call
4296 * \c psa_sign_hash_abort() at any point if they no
4297 * longer want the result.
4298 *
4299 * \note If this function returns an error status, the
4300 * operation enters an error state and must be
4301 * aborted by calling \c psa_sign_hash_abort().
4302 *
4303 * \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t
4304 * to use. This must be initialized first.
4305 *
4306 * \param key Identifier of the key to use for the operation.
4307 * It must be an asymmetric key pair. The key must
4308 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
4309 * \param alg A signature algorithm (\c PSA_ALG_XXX
4310 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
4311 * is true), that is compatible with
4312 * the type of \p key.
4313 * \param[in] hash The hash or message to sign.
4314 * \param hash_length Size of the \p hash buffer in bytes.
4315 *
4316 * \retval #PSA_SUCCESS
4317 * The operation started successfully - call \c psa_sign_hash_complete()
4318 * with the same context to complete the operation
4319 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004320 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004321 * \retval #PSA_ERROR_NOT_PERMITTED
4322 * The key does not have the #PSA_KEY_USAGE_SIGN_HASH flag, or it does
4323 * not permit the requested algorithm.
4324 * \retval #PSA_ERROR_BAD_STATE
4325 * An operation has previously been started on this context, and is
4326 * still in progress.
Paul Elliott15d7d432023-02-27 17:17:56 +00004327 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4328 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4329 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4330 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4331 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4332 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4333 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4334 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4335 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4336 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004337 * \retval #PSA_ERROR_BAD_STATE
4338 * The library has not been previously initialized by psa_crypto_init().
4339 * It is implementation-dependent whether a failure to initialize
4340 * results in this error code.
4341 */
4342psa_status_t psa_sign_hash_start(
4343 psa_sign_hash_interruptible_operation_t *operation,
4344 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
4345 const uint8_t *hash, size_t hash_length);
4346
4347/**
4348 * \brief Continue and eventually complete the action of
4349 * signing a hash or short message with a private
4350 * key, in an interruptible manner.
4351 *
4352 * \see \c psa_sign_hash_start()
4353 *
4354 * \warning This is a beta API, and thus subject to change
4355 * at any point. It is not bound by the usual
4356 * interface stability promises.
4357 *
4358 * \note This function combined with \c
4359 * psa_sign_hash_start() is equivalent to
4360 * \c psa_sign_hash() but this function can return
4361 * early and resume according to the limit set with
4362 * \c psa_interruptible_set_max_ops() to reduce the
4363 * maximum time spent in a function call.
4364 *
4365 * \note Users should call this function on the same
4366 * operation object repeatedly until it either
4367 * returns 0 or an error. This function will return
4368 * #PSA_OPERATION_INCOMPLETE if there is more work
4369 * to do. Alternatively users can call
4370 * \c psa_sign_hash_abort() at any point if they no
4371 * longer want the result.
4372 *
4373 * \note When this function returns successfully, the
4374 * operation becomes inactive. If this function
4375 * returns an error status, the operation enters an
4376 * error state and must be aborted by calling
4377 * \c psa_sign_hash_abort().
4378 *
4379 * \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t
4380 * to use. This must be initialized first, and have
4381 * had \c psa_sign_hash_start() called with it
4382 * first.
4383 *
4384 * \param[out] signature Buffer where the signature is to be written.
4385 * \param signature_size Size of the \p signature buffer in bytes. This
4386 * must be appropriate for the selected
4387 * algorithm and key:
4388 * - The required signature size is
4389 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c
4390 * key_bits, \c alg) where \c key_type and \c
4391 * key_bits are the type and bit-size
4392 * respectively of key.
4393 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
4394 * maximum signature size of any supported
4395 * signature algorithm.
4396 * \param[out] signature_length On success, the number of bytes that make up
4397 * the returned signature value.
4398 *
4399 * \retval #PSA_SUCCESS
4400 * Operation completed successfully
4401 *
4402 * \retval #PSA_OPERATION_INCOMPLETE
4403 * Operation was interrupted due to the setting of \c
4404 * psa_interruptible_set_max_ops(). There is still work to be done.
4405 * Call this function again with the same operation object.
4406 *
4407 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
4408 * The size of the \p signature buffer is too small. You can
4409 * determine a sufficient buffer size by calling
Andrzej Kurek00b54e62023-05-06 09:38:57 -04004410 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \c alg)
Paul Elliott1265f002022-09-09 17:15:43 +01004411 * where \c key_type and \c key_bits are the type and bit-size
Andrzej Kurek00b54e62023-05-06 09:38:57 -04004412 * respectively of \c key.
Paul Elliott1265f002022-09-09 17:15:43 +01004413 *
4414 * \retval #PSA_ERROR_BAD_STATE
4415 * An operation was not previously started on this context via
4416 * \c psa_sign_hash_start().
4417 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004418 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4419 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4420 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4421 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4422 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4423 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4424 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4425 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4426 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4427 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004428 * \retval #PSA_ERROR_BAD_STATE
4429 * The library has either not been previously initialized by
Paul Elliott15d7d432023-02-27 17:17:56 +00004430 * psa_crypto_init() or you did not previously call
Paul Elliott1265f002022-09-09 17:15:43 +01004431 * psa_sign_hash_start() with this operation object. It is
4432 * implementation-dependent whether a failure to initialize results in
4433 * this error code.
4434 */
4435psa_status_t psa_sign_hash_complete(
4436 psa_sign_hash_interruptible_operation_t *operation,
4437 uint8_t *signature, size_t signature_size,
4438 size_t *signature_length);
4439
4440/**
4441 * \brief Abort a sign hash operation.
4442 *
4443 * \warning This is a beta API, and thus subject to change
4444 * at any point. It is not bound by the usual
4445 * interface stability promises.
Paul Elliott1b49ef52023-02-03 14:27:32 +00004446 *
4447 * \note This function is the only function that clears
4448 * the number of ops completed as part of the
4449 * operation. Please ensure you copy this value via
4450 * \c psa_sign_hash_get_num_ops() if required
4451 * before calling.
4452 *
Paul Elliott1265f002022-09-09 17:15:43 +01004453 * \note Aborting an operation frees all associated
4454 * resources except for the \p operation structure
4455 * itself. Once aborted, the operation object can
4456 * be reused for another operation by calling \c
4457 * psa_sign_hash_start() again.
4458 *
4459 * \note You may call this function any time after the
4460 * operation object has been initialized. In
4461 * particular, calling \c psa_sign_hash_abort()
4462 * after the operation has already been terminated
4463 * by a call to \c psa_sign_hash_abort() or
Paul Elliott1b49ef52023-02-03 14:27:32 +00004464 * psa_sign_hash_complete() is safe.
Paul Elliott1265f002022-09-09 17:15:43 +01004465 *
4466 * \param[in,out] operation Initialized sign hash operation.
4467 *
4468 * \retval #PSA_SUCCESS
4469 * The operation was aborted successfully.
4470 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004471 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004472 * \retval #PSA_ERROR_BAD_STATE
4473 * The library has not been previously initialized by psa_crypto_init().
4474 * It is implementation-dependent whether a failure to initialize
4475 * results in this error code.
4476 */
4477psa_status_t psa_sign_hash_abort(
4478 psa_sign_hash_interruptible_operation_t *operation);
4479
4480/**
4481 * \brief Start reading and verifying a hash or short
4482 * message, in an interruptible manner.
4483 *
4484 * \see \c psa_verify_hash_complete()
4485 *
4486 * \warning This is a beta API, and thus subject to change
4487 * at any point. It is not bound by the usual
4488 * interface stability promises.
4489 *
4490 * \note This function combined with \c
4491 * psa_verify_hash_complete() is equivalent to
4492 * \c psa_verify_hash() but \c
4493 * psa_verify_hash_complete() can return early and
4494 * resume according to the limit set with \c
4495 * psa_interruptible_set_max_ops() to reduce the
4496 * maximum time spent in a function.
4497 *
4498 * \note Users should call \c psa_verify_hash_complete()
4499 * repeatedly on the same operation object after a
4500 * successful call to this function until \c
4501 * psa_verify_hash_complete() either returns 0 or
4502 * an error. \c psa_verify_hash_complete() will
4503 * return #PSA_OPERATION_INCOMPLETE if there is
4504 * more work to do. Alternatively users can call
4505 * \c psa_verify_hash_abort() at any point if they
4506 * no longer want the result.
4507 *
4508 * \note If this function returns an error status, the
4509 * operation enters an error state and must be
4510 * aborted by calling \c psa_verify_hash_abort().
4511 *
4512 * \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t
4513 * to use. This must be initialized first.
4514 *
4515 * \param key Identifier of the key to use for the operation.
4516 * The key must allow the usage
4517 * #PSA_KEY_USAGE_VERIFY_HASH.
4518 * \param alg A signature algorithm (\c PSA_ALG_XXX
4519 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
4520 * is true), that is compatible with
4521 * the type of \p key.
4522 * \param[in] hash The hash whose signature is to be verified.
4523 * \param hash_length Size of the \p hash buffer in bytes.
4524 * \param[in] signature Buffer containing the signature to verify.
4525 * \param signature_length Size of the \p signature buffer in bytes.
4526 *
4527 * \retval #PSA_SUCCESS
4528 * The operation started successfully - please call \c
4529 * psa_verify_hash_complete() with the same context to complete the
4530 * operation.
4531 *
4532 * \retval #PSA_ERROR_BAD_STATE
4533 * Another operation has already been started on this context, and is
4534 * still in progress.
4535 *
4536 * \retval #PSA_ERROR_NOT_PERMITTED
4537 * The key does not have the #PSA_KEY_USAGE_VERIFY_HASH flag, or it does
4538 * not permit the requested algorithm.
4539 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004540 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4541 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4542 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4543 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4544 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4545 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4546 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4547 * \retval PSA_ERROR_DATA_CORRUPT \emptydescription
4548 * \retval PSA_ERROR_DATA_INVALID \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004549 * \retval #PSA_ERROR_BAD_STATE
4550 * The library has not been previously initialized by psa_crypto_init().
4551 * It is implementation-dependent whether a failure to initialize
4552 * results in this error code.
4553 */
4554psa_status_t psa_verify_hash_start(
4555 psa_verify_hash_interruptible_operation_t *operation,
4556 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
4557 const uint8_t *hash, size_t hash_length,
4558 const uint8_t *signature, size_t signature_length);
4559
4560/**
4561 * \brief Continue and eventually complete the action of
4562 * reading and verifying a hash or short message
4563 * signed with a private key, in an interruptible
4564 * manner.
4565 *
4566 * \see \c psa_verify_hash_start()
4567 *
4568 * \warning This is a beta API, and thus subject to change
4569 * at any point. It is not bound by the usual
4570 * interface stability promises.
4571 *
4572 * \note This function combined with \c
4573 * psa_verify_hash_start() is equivalent to
4574 * \c psa_verify_hash() but this function can
4575 * return early and resume according to the limit
4576 * set with \c psa_interruptible_set_max_ops() to
4577 * reduce the maximum time spent in a function
4578 * call.
4579 *
4580 * \note Users should call this function on the same
4581 * operation object repeatedly until it either
4582 * returns 0 or an error. This function will return
4583 * #PSA_OPERATION_INCOMPLETE if there is more work
4584 * to do. Alternatively users can call
4585 * \c psa_verify_hash_abort() at any point if they
4586 * no longer want the result.
4587 *
4588 * \note When this function returns successfully, the
4589 * operation becomes inactive. If this function
4590 * returns an error status, the operation enters an
4591 * error state and must be aborted by calling
4592 * \c psa_verify_hash_abort().
4593 *
4594 * \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t
4595 * to use. This must be initialized first, and have
4596 * had \c psa_verify_hash_start() called with it
4597 * first.
4598 *
4599 * \retval #PSA_SUCCESS
4600 * Operation completed successfully, and the passed signature is valid.
4601 *
4602 * \retval #PSA_OPERATION_INCOMPLETE
4603 * Operation was interrupted due to the setting of \c
4604 * psa_interruptible_set_max_ops(). There is still work to be done.
4605 * Call this function again with the same operation object.
4606 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004607 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004608 * \retval #PSA_ERROR_INVALID_SIGNATURE
4609 * The calculation was performed successfully, but the passed
4610 * signature is not a valid signature.
Paul Elliott15d7d432023-02-27 17:17:56 +00004611 * \retval #PSA_ERROR_BAD_STATE
Paul Elliott1265f002022-09-09 17:15:43 +01004612 * An operation was not previously started on this context via
4613 * \c psa_verify_hash_start().
Paul Elliott15d7d432023-02-27 17:17:56 +00004614 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4615 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4616 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4617 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4618 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4619 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4620 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4621 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4622 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4623 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004624 * \retval #PSA_ERROR_BAD_STATE
4625 * The library has either not been previously initialized by
4626 * psa_crypto_init() or you did not previously call
4627 * psa_verify_hash_start() on this object. It is
4628 * implementation-dependent whether a failure to initialize results in
4629 * this error code.
4630 */
4631psa_status_t psa_verify_hash_complete(
4632 psa_verify_hash_interruptible_operation_t *operation);
4633
4634/**
4635 * \brief Abort a verify hash operation.
4636 *
4637 * \warning This is a beta API, and thus subject to change at
4638 * any point. It is not bound by the usual interface
4639 * stability promises.
4640 *
Paul Elliott1b49ef52023-02-03 14:27:32 +00004641 * \note This function is the only function that clears the
4642 * number of ops completed as part of the operation.
4643 * Please ensure you copy this value via
4644 * \c psa_verify_hash_get_num_ops() if required
4645 * before calling.
4646 *
Paul Elliott1265f002022-09-09 17:15:43 +01004647 * \note Aborting an operation frees all associated
4648 * resources except for the operation structure
4649 * itself. Once aborted, the operation object can be
4650 * reused for another operation by calling \c
4651 * psa_verify_hash_start() again.
4652 *
4653 * \note You may call this function any time after the
4654 * operation object has been initialized.
4655 * In particular, calling \c psa_verify_hash_abort()
4656 * after the operation has already been terminated by
4657 * a call to \c psa_verify_hash_abort() or
Paul Elliott1b49ef52023-02-03 14:27:32 +00004658 * psa_verify_hash_complete() is safe.
Paul Elliott1265f002022-09-09 17:15:43 +01004659 *
4660 * \param[in,out] operation Initialized verify hash operation.
4661 *
4662 * \retval #PSA_SUCCESS
4663 * The operation was aborted successfully.
4664 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004665 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004666 * \retval #PSA_ERROR_BAD_STATE
4667 * The library has not been previously initialized by psa_crypto_init().
4668 * It is implementation-dependent whether a failure to initialize
4669 * results in this error code.
4670 */
4671psa_status_t psa_verify_hash_abort(
4672 psa_verify_hash_interruptible_operation_t *operation);
4673
4674
4675/**@}*/
4676
Gilles Peskinee59236f2018-01-27 23:32:46 +01004677#ifdef __cplusplus
4678}
4679#endif
4680
Gilles Peskine0cad07c2018-06-27 19:49:02 +02004681/* The file "crypto_sizes.h" contains definitions for size calculation
4682 * macros whose definitions are implementation-specific. */
4683#include "crypto_sizes.h"
4684
Gilles Peskine9ef733f2018-02-07 21:05:37 +01004685/* The file "crypto_struct.h" contains definitions for
4686 * implementation-specific structs that are declared above. */
Gilles Peskineb1176f22023-02-22 22:07:28 +01004687#if defined(MBEDTLS_PSA_CRYPTO_STRUCT_FILE)
4688#include MBEDTLS_PSA_CRYPTO_STRUCT_FILE
4689#else
Gilles Peskine9ef733f2018-02-07 21:05:37 +01004690#include "crypto_struct.h"
Gilles Peskineb1176f22023-02-22 22:07:28 +01004691#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01004692
4693/* The file "crypto_extra.h" contains vendor-specific definitions. This
4694 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004695#include "crypto_extra.h"
4696
4697#endif /* PSA_CRYPTO_H */