blob: 8dd89fa99a6a9b89ef9de2d71c166f3a55fc79ee [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
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
39/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010040#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010041
Gilles Peskinee59236f2018-01-27 23:32:46 +010042#ifdef __cplusplus
43extern "C" {
44#endif
45
Gilles Peskinef3b731e2018-12-12 13:38:31 +010046/* The file "crypto_types.h" declares types that encode errors,
47 * algorithms, key types, policies, etc. */
48#include "crypto_types.h"
49
Andrew Thoelke02b372b2019-10-02 09:32:21 +010050/** \defgroup version API version
Adrian L. Shawd89338a2019-09-19 13:32:57 +010051 * @{
52 */
53
54/**
55 * The major version of this implementation of the PSA Crypto API
56 */
57#define PSA_CRYPTO_API_VERSION_MAJOR 1
58
59/**
60 * The minor version of this implementation of the PSA Crypto API
61 */
62#define PSA_CRYPTO_API_VERSION_MINOR 0
63
64/**@}*/
65
Gilles Peskinef3b731e2018-12-12 13:38:31 +010066/* The file "crypto_values.h" declares macros to build and analyze values
67 * of integral types defined in "crypto_types.h". */
68#include "crypto_values.h"
69
70/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010071 * @{
72 */
73
74/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010075 * \brief Library initialization.
76 *
77 * Applications must call this function before calling any other
78 * function in this module.
79 *
80 * Applications may call this function more than once. Once a call
81 * succeeds, subsequent calls are guaranteed to succeed.
82 *
itayzafrir18617092018-09-16 12:22:41 +030083 * If the application calls other functions before calling psa_crypto_init(),
84 * the behavior is undefined. Implementations are encouraged to either perform
85 * the operation as if the library had been initialized or to return
86 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
87 * implementations should not return a success status if the lack of
88 * initialization may have security implications, for example due to improper
89 * seeding of the random number generator.
90 *
Gilles Peskineed733552023-02-14 19:21:09 +010091 * \retval #PSA_SUCCESS \emptydescription
92 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
93 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
94 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
95 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
96 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
97 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
98 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
99 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
100 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
Gilles Peskinee59236f2018-01-27 23:32:46 +0100101 */
102psa_status_t psa_crypto_init(void);
103
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100104/**@}*/
105
Gilles Peskine105f67f2019-07-23 18:16:05 +0200106/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +0200107 * @{
108 */
109
Gilles Peskinea0c06552019-05-21 15:54:54 +0200110/** \def PSA_KEY_ATTRIBUTES_INIT
111 *
112 * This macro returns a suitable initializer for a key attribute structure
113 * of type #psa_key_attributes_t.
114 */
Gilles Peskinea0c06552019-05-21 15:54:54 +0200115
116/** Return an initial value for a key attributes structure.
117 */
118static psa_key_attributes_t psa_key_attributes_init(void);
119
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200120/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200121 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200122 * If the attribute structure currently declares the key as volatile (which
123 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200124 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200125 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200126 * This function does not access storage, it merely stores the given
127 * value in the structure.
128 * The persistent key will be written to storage when the attribute
129 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200130 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200131 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200132 *
Gilles Peskine20628592019-04-19 19:29:50 +0200133 * This function may be declared as `static` (i.e. without external
134 * linkage). This function may be provided as a function-like macro,
135 * but in this case it must evaluate each of its arguments exactly once.
136 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200137 * \param[out] attributes The attribute structure to write to.
138 * \param key The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200139 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100140static void psa_set_key_id(psa_key_attributes_t *attributes,
141 mbedtls_svc_key_id_t key);
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200142
Ronald Cron6b5ff532020-10-16 14:38:19 +0200143#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
144/** Set the owner identifier of a key.
145 *
146 * When key identifiers encode key owner identifiers, psa_set_key_id() does
147 * not allow to define in key attributes the owner of volatile keys as
148 * psa_set_key_id() enforces the key to be persistent.
149 *
150 * This function allows to set in key attributes the owner identifier of a
151 * key. It is intended to be used for volatile keys. For persistent keys,
152 * it is recommended to use the PSA Cryptography API psa_set_key_id() to define
153 * the owner of a key.
154 *
155 * \param[out] attributes The attribute structure to write to.
Antonio de Angelise2b68662021-11-30 12:21:44 +0000156 * \param owner The key owner identifier.
Ronald Cron6b5ff532020-10-16 14:38:19 +0200157 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100158static void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,
159 mbedtls_key_owner_id_t owner);
Ronald Cron6b5ff532020-10-16 14:38:19 +0200160#endif
161
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200162/** Set the location of a persistent key.
163 *
164 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200165 * with psa_set_key_id(). By default, a key that has a persistent identifier
166 * is stored in the default storage area identifier by
167 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
168 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200169 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200170 * This function does not access storage, it merely stores the given
171 * value in the structure.
172 * The persistent key will be written to storage when the attribute
173 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200174 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200175 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200176 *
177 * This function may be declared as `static` (i.e. without external
178 * linkage). This function may be provided as a function-like macro,
179 * but in this case it must evaluate each of its arguments exactly once.
180 *
181 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200182 * \param lifetime The lifetime for the key.
183 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200184 * key will be volatile, and the key identifier
185 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200186 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200187static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
188 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200189
Gilles Peskine20628592019-04-19 19:29:50 +0200190/** Retrieve the key identifier from key attributes.
191 *
192 * This function may be declared as `static` (i.e. without external
193 * linkage). This function may be provided as a function-like macro,
194 * but in this case it must evaluate its argument exactly once.
195 *
196 * \param[in] attributes The key attribute structure to query.
197 *
198 * \return The persistent identifier stored in the attribute structure.
199 * This value is unspecified if the attribute structure declares
200 * the key as volatile.
201 */
Ronald Cron71016a92020-08-28 19:01:50 +0200202static mbedtls_svc_key_id_t psa_get_key_id(
203 const psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200204
Gilles Peskine20628592019-04-19 19:29:50 +0200205/** Retrieve the lifetime from key attributes.
206 *
207 * This function may be declared as `static` (i.e. without external
208 * linkage). This function may be provided as a function-like macro,
209 * but in this case it must evaluate its argument exactly once.
210 *
211 * \param[in] attributes The key attribute structure to query.
212 *
213 * \return The lifetime value stored in the attribute structure.
214 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200215static psa_key_lifetime_t psa_get_key_lifetime(
216 const psa_key_attributes_t *attributes);
217
Gilles Peskine20628592019-04-19 19:29:50 +0200218/** Declare usage flags for a key.
219 *
220 * Usage flags are part of a key's usage policy. They encode what
221 * kind of operations are permitted on the key. For more details,
222 * refer to the documentation of the type #psa_key_usage_t.
223 *
224 * This function overwrites any usage flags
225 * previously set in \p attributes.
226 *
227 * This function may be declared as `static` (i.e. without external
228 * linkage). This function may be provided as a function-like macro,
229 * but in this case it must evaluate each of its arguments exactly once.
230 *
231 * \param[out] attributes The attribute structure to write to.
232 * \param usage_flags The usage flags to write.
233 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200234static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
235 psa_key_usage_t usage_flags);
236
Gilles Peskine20628592019-04-19 19:29:50 +0200237/** Retrieve the usage flags from key attributes.
238 *
239 * This function may be declared as `static` (i.e. without external
240 * linkage). This function may be provided as a function-like macro,
241 * but in this case it must evaluate its argument exactly once.
242 *
243 * \param[in] attributes The key attribute structure to query.
244 *
245 * \return The usage flags stored in the attribute structure.
246 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200247static psa_key_usage_t psa_get_key_usage_flags(
248 const psa_key_attributes_t *attributes);
249
Gilles Peskine20628592019-04-19 19:29:50 +0200250/** Declare the permitted algorithm policy for a key.
251 *
252 * The permitted algorithm policy of a key encodes which algorithm or
Gilles Peskinea170d922019-09-12 16:59:37 +0200253 * algorithms are permitted to be used with this key. The following
254 * algorithm policies are supported:
255 * - 0 does not allow any cryptographic operation with the key. The key
256 * may be used for non-cryptographic actions such as exporting (if
257 * permitted by the usage flags).
258 * - An algorithm value permits this particular algorithm.
259 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
260 * signature scheme with any hash algorithm.
Steven Cooremancaad4932021-02-18 11:28:17 +0100261 * - An algorithm built from #PSA_ALG_AT_LEAST_THIS_LENGTH_MAC allows
Steven Cooremanee18b1f2021-02-08 11:44:21 +0100262 * any MAC algorithm from the same base class (e.g. CMAC) which
263 * generates/verifies a MAC length greater than or equal to the length
264 * encoded in the wildcard algorithm.
Steven Cooreman5d814812021-02-18 12:11:39 +0100265 * - An algorithm built from #PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG
266 * allows any AEAD algorithm from the same base class (e.g. CCM) which
Steven Cooremanee18b1f2021-02-08 11:44:21 +0100267 * generates/verifies a tag length greater than or equal to the length
268 * encoded in the wildcard algorithm.
Gilles Peskine20628592019-04-19 19:29:50 +0200269 *
270 * This function overwrites any algorithm policy
271 * previously set in \p attributes.
272 *
273 * This function may be declared as `static` (i.e. without external
274 * linkage). This function may be provided as a function-like macro,
275 * but in this case it must evaluate each of its arguments exactly once.
276 *
277 * \param[out] attributes The attribute structure to write to.
278 * \param alg The permitted algorithm policy to write.
279 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200280static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
281 psa_algorithm_t alg);
282
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100283
Gilles Peskine20628592019-04-19 19:29:50 +0200284/** Retrieve the algorithm policy from key attributes.
285 *
286 * This function may be declared as `static` (i.e. without external
287 * linkage). This function may be provided as a function-like macro,
288 * but in this case it must evaluate its argument exactly once.
289 *
290 * \param[in] attributes The key attribute structure to query.
291 *
292 * \return The algorithm stored in the attribute structure.
293 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200294static psa_algorithm_t psa_get_key_algorithm(
295 const psa_key_attributes_t *attributes);
296
Gilles Peskine20628592019-04-19 19:29:50 +0200297/** Declare the type of a key.
298 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200299 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200300 * previously set in \p attributes.
301 *
302 * This function may be declared as `static` (i.e. without external
303 * linkage). This function may be provided as a function-like macro,
304 * but in this case it must evaluate each of its arguments exactly once.
305 *
306 * \param[out] attributes The attribute structure to write to.
307 * \param type The key type to write.
Gilles Peskinea170d922019-09-12 16:59:37 +0200308 * If this is 0, the key type in \p attributes
309 * becomes unspecified.
Gilles Peskine20628592019-04-19 19:29:50 +0200310 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200311static void psa_set_key_type(psa_key_attributes_t *attributes,
312 psa_key_type_t type);
313
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100314
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200315/** Declare the size of a key.
316 *
317 * This function overwrites any key size previously set in \p attributes.
318 *
319 * This function may be declared as `static` (i.e. without external
320 * linkage). This function may be provided as a function-like macro,
321 * but in this case it must evaluate each of its arguments exactly once.
322 *
323 * \param[out] attributes The attribute structure to write to.
324 * \param bits The key size in bits.
Gilles Peskinea170d922019-09-12 16:59:37 +0200325 * If this is 0, the key size in \p attributes
Gilles Peskine05c900b2019-09-12 18:29:43 +0200326 * becomes unspecified. Keys of size 0 are
327 * not supported.
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200328 */
329static void psa_set_key_bits(psa_key_attributes_t *attributes,
330 size_t bits);
331
Gilles Peskine20628592019-04-19 19:29:50 +0200332/** Retrieve the key type from key attributes.
333 *
334 * This function may be declared as `static` (i.e. without external
335 * linkage). This function may be provided as a function-like macro,
336 * but in this case it must evaluate its argument exactly once.
337 *
338 * \param[in] attributes The key attribute structure to query.
339 *
340 * \return The key type stored in the attribute structure.
341 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200342static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
343
Gilles Peskine20628592019-04-19 19:29:50 +0200344/** Retrieve the key size from key attributes.
345 *
346 * This function may be declared as `static` (i.e. without external
347 * linkage). This function may be provided as a function-like macro,
348 * but in this case it must evaluate its argument exactly once.
349 *
350 * \param[in] attributes The key attribute structure to query.
351 *
352 * \return The key size stored in the attribute structure, in bits.
353 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200354static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
355
Gilles Peskine20628592019-04-19 19:29:50 +0200356/** Retrieve the attributes of a key.
357 *
358 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200359 * psa_reset_key_attributes(). It then copies the attributes of
360 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200361 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200362 * \note This function may allocate memory or other resources.
363 * Once you have called this function on an attribute structure,
364 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200365 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200366 * \param[in] key Identifier of the key to query.
Gilles Peskine20628592019-04-19 19:29:50 +0200367 * \param[in,out] attributes On success, the attributes of the key.
368 * On failure, equivalent to a
369 * freshly-initialized structure.
370 *
Gilles Peskineed733552023-02-14 19:21:09 +0100371 * \retval #PSA_SUCCESS \emptydescription
372 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
373 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
374 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
375 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
376 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
377 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
378 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100379 * \retval #PSA_ERROR_BAD_STATE
380 * The library has not been previously initialized by psa_crypto_init().
381 * It is implementation-dependent whether a failure to initialize
382 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200383 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200384psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
Gilles Peskine4747d192019-04-17 15:05:45 +0200385 psa_key_attributes_t *attributes);
386
Gilles Peskine20628592019-04-19 19:29:50 +0200387/** Reset a key attribute structure to a freshly initialized state.
388 *
389 * You must initialize the attribute structure as described in the
390 * documentation of the type #psa_key_attributes_t before calling this
391 * function. Once the structure has been initialized, you may call this
392 * function at any time.
393 *
394 * This function frees any auxiliary resources that the structure
395 * may contain.
396 *
397 * \param[in,out] attributes The attribute structure to reset.
398 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200399void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200400
Gilles Peskine87a5e562019-04-17 12:28:25 +0200401/**@}*/
402
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100403/** \defgroup key_management Key management
404 * @{
405 */
406
Ronald Cron277a85f2020-08-04 15:49:48 +0200407/** Remove non-essential copies of key material from memory.
408 *
409 * If the key identifier designates a volatile key, this functions does not do
410 * anything and returns successfully.
411 *
412 * If the key identifier designates a persistent key, then this function will
413 * free all resources associated with the key in volatile memory. The key
414 * data in persistent storage is not affected and the key can still be used.
415 *
416 * \param key Identifier of the key to purge.
417 *
418 * \retval #PSA_SUCCESS
419 * The key material will have been removed from memory if it is not
420 * currently required.
421 * \retval #PSA_ERROR_INVALID_ARGUMENT
422 * \p key is not a valid key identifier.
423 * \retval #PSA_ERROR_BAD_STATE
424 * The library has not been previously initialized by psa_crypto_init().
425 * It is implementation-dependent whether a failure to initialize
426 * results in this error code.
427 */
428psa_status_t psa_purge_key(mbedtls_svc_key_id_t key);
429
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100430/** Make a copy of a key.
431 *
432 * Copy key material from one location to another.
433 *
434 * This function is primarily useful to copy a key from one location
435 * to another, since it populates a key using the material from
436 * another key which may have a different lifetime.
437 *
438 * This function may be used to share a key with a different party,
439 * subject to implementation-defined restrictions on key sharing.
440 *
441 * The policy on the source key must have the usage flag
442 * #PSA_KEY_USAGE_COPY set.
443 * This flag is sufficient to permit the copy if the key has the lifetime
444 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
445 * Some secure elements do not provide a way to copy a key without
446 * making it extractable from the secure element. If a key is located
447 * in such a secure element, then the key must have both usage flags
448 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
449 * a copy of the key outside the secure element.
450 *
451 * The resulting key may only be used in a way that conforms to
452 * both the policy of the original key and the policy specified in
453 * the \p attributes parameter:
454 * - The usage flags on the resulting key are the bitwise-and of the
455 * usage flags on the source policy and the usage flags in \p attributes.
456 * - If both allow the same algorithm or wildcard-based
457 * algorithm policy, the resulting key has the same algorithm policy.
458 * - If either of the policies allows an algorithm and the other policy
459 * allows a wildcard-based algorithm policy that includes this algorithm,
460 * the resulting key allows the same algorithm.
461 * - If the policies do not allow any algorithm in common, this function
462 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
463 *
464 * The effect of this function on implementation-defined attributes is
465 * implementation-defined.
466 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200467 * \param source_key The key to copy. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +0200468 * #PSA_KEY_USAGE_COPY. If a private or secret key is
Ronald Croncf56a0a2020-08-04 09:51:30 +0200469 * being copied outside of a secure element it must
Ronald Cron96783552020-10-19 12:06:30 +0200470 * also allow #PSA_KEY_USAGE_EXPORT.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100471 * \param[in] attributes The attributes for the new key.
472 * They are used as follows:
473 * - The key type and size may be 0. If either is
474 * nonzero, it must match the corresponding
475 * attribute of the source key.
476 * - The key location (the lifetime and, for
477 * persistent keys, the key identifier) is
478 * used directly.
479 * - The policy constraints (usage flags and
480 * algorithm policy) are combined from
481 * the source key and \p attributes so that
482 * both sets of restrictions apply, as
483 * described in the documentation of this function.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200484 * \param[out] target_key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +0200485 * key. For persistent keys, this is the key
486 * identifier defined in \p attributes.
487 * \c 0 on failure.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100488 *
Gilles Peskineed733552023-02-14 19:21:09 +0100489 * \retval #PSA_SUCCESS \emptydescription
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100490 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200491 * \p source_key is invalid.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100492 * \retval #PSA_ERROR_ALREADY_EXISTS
493 * This is an attempt to create a persistent key, and there is
494 * already a persistent key with the given identifier.
495 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500496 * The lifetime or identifier in \p attributes are invalid, or
497 * the policy constraints on the source and specified in
498 * \p attributes are incompatible, or
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100499 * \p attributes specifies a key type or key size
500 * which does not match the attributes of the source key.
501 * \retval #PSA_ERROR_NOT_PERMITTED
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500502 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag, or
503 * the source key is not exportable and its lifetime does not
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100504 * allow copying it to the target's lifetime.
Gilles Peskineed733552023-02-14 19:21:09 +0100505 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
506 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
507 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
508 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
509 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
510 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
511 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
512 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100513 * \retval #PSA_ERROR_BAD_STATE
514 * The library has not been previously initialized by psa_crypto_init().
515 * It is implementation-dependent whether a failure to initialize
516 * results in this error code.
517 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200518psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100519 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200520 mbedtls_svc_key_id_t *target_key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100521
522
523/**
524 * \brief Destroy a key.
525 *
526 * This function destroys a key from both volatile
527 * memory and, if applicable, non-volatile storage. Implementations shall
Tom Cosgrove1797b052022-12-04 17:19:59 +0000528 * make a best effort to ensure that the key material cannot be recovered.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100529 *
530 * This function also erases any metadata such as policies and frees
Ronald Croncf56a0a2020-08-04 09:51:30 +0200531 * resources associated with the key.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100532 *
533 * If a key is currently in use in a multipart operation, then destroying the
534 * key will cause the multipart operation to fail.
535 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200536 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
Ronald Cron96783552020-10-19 12:06:30 +0200537 * return #PSA_SUCCESS.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100538 *
539 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +0200540 * \p key was a valid identifier and the key material that it
541 * referred to has been erased. Alternatively, \p key is \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100542 * \retval #PSA_ERROR_NOT_PERMITTED
543 * The key cannot be erased because it is
544 * read-only, either due to a policy or due to physical restrictions.
545 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200546 * \p key is not a valid identifier nor \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100547 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100548 * There was a failure in communication with the cryptoprocessor.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100549 * The key material may still be present in the cryptoprocessor.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100550 * \retval #PSA_ERROR_DATA_INVALID
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100551 * This error is typically a result of either storage corruption on a
552 * cleartext storage backend, or an attempt to read data that was
553 * written by an incompatible version of the library.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100554 * \retval #PSA_ERROR_STORAGE_FAILURE
555 * The storage is corrupted. Implementations shall make a best effort
556 * to erase key material even in this stage, however applications
557 * should be aware that it may be impossible to guarantee that the
558 * key material is not recoverable in such cases.
559 * \retval #PSA_ERROR_CORRUPTION_DETECTED
560 * An unexpected condition which is not a storage corruption or
561 * a communication failure occurred. The cryptoprocessor may have
562 * been compromised.
563 * \retval #PSA_ERROR_BAD_STATE
564 * The library has not been previously initialized by psa_crypto_init().
565 * It is implementation-dependent whether a failure to initialize
566 * results in this error code.
567 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200568psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100569
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100570/**@}*/
571
572/** \defgroup import_export Key import and export
573 * @{
574 */
575
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100576/**
577 * \brief Import a key in binary format.
578 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100579 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100580 * documentation of psa_export_public_key() for the format of public keys
581 * and to the documentation of psa_export_key() for the format for
582 * other key types.
583 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200584 * The key data determines the key size. The attributes may optionally
585 * specify a key size; in this case it must match the size determined
586 * from the key data. A key size of 0 in \p attributes indicates that
587 * the key size is solely determined by the key data.
588 *
589 * Implementations must reject an attempt to import a key of size 0.
590 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100591 * This specification supports a single format for each key type.
592 * Implementations may support other formats as long as the standard
593 * format is supported. Implementations that support other formats
594 * should ensure that the formats are clearly unambiguous so as to
595 * minimize the risk that an invalid input is accidentally interpreted
596 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100597 *
Gilles Peskine20628592019-04-19 19:29:50 +0200598 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200599 * The key size is always determined from the
600 * \p data buffer.
601 * If the key size in \p attributes is nonzero,
602 * it must be equal to the size from \p data.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200603 * \param[out] key On success, an identifier to the newly created key.
Ronald Cron4067d1c2020-10-19 13:34:38 +0200604 * For persistent keys, this is the key identifier
605 * defined in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200606 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100607 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200608 * buffer is interpreted according to the type declared
609 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200610 * All implementations must support at least the format
611 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100612 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200613 * the chosen type. Implementations may allow other
614 * formats, but should be conservative: implementations
615 * should err on the side of rejecting content if it
616 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200617 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100618 *
Gilles Peskine28538492018-07-11 17:34:00 +0200619 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100620 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100621 * If the key is persistent, the key material and the key's metadata
622 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200623 * \retval #PSA_ERROR_ALREADY_EXISTS
624 * This is an attempt to create a persistent key, and there is
625 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200626 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200627 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200628 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200629 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500630 * The key attributes, as a whole, are invalid, or
631 * the key data is not correctly formatted, or
632 * the size in \p attributes is nonzero and does not match the size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200633 * of the key data.
Gilles Peskineed733552023-02-14 19:21:09 +0100634 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
635 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
636 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
637 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
638 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
639 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
640 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
641 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +0300642 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300643 * The library has not been previously initialized by psa_crypto_init().
644 * It is implementation-dependent whether a failure to initialize
645 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100646 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200647psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100648 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200649 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200650 mbedtls_svc_key_id_t *key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100651
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100652
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100653
654/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100655 * \brief Export a key in binary format.
656 *
657 * The output of this function can be passed to psa_import_key() to
658 * create an equivalent object.
659 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100660 * If the implementation of psa_import_key() supports other formats
661 * beyond the format specified here, the output from psa_export_key()
662 * must use the representation specified here, not the original
663 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100664 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100665 * For standard key types, the output format is as follows:
666 *
667 * - For symmetric keys (including MAC keys), the format is the
668 * raw bytes of the key.
669 * - For DES, the key data consists of 8 bytes. The parity bits must be
670 * correct.
671 * - For Triple-DES, the format is the concatenation of the
672 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200673 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200674 * is the non-encrypted DER encoding of the representation defined by
675 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
676 * ```
677 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200678 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200679 * modulus INTEGER, -- n
680 * publicExponent INTEGER, -- e
681 * privateExponent INTEGER, -- d
682 * prime1 INTEGER, -- p
683 * prime2 INTEGER, -- q
684 * exponent1 INTEGER, -- d mod (p-1)
685 * exponent2 INTEGER, -- d mod (q-1)
686 * coefficient INTEGER, -- (inverse of q) mod p
687 * }
688 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200689 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200690 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100691 * a representation of the private value as a `ceiling(m/8)`-byte string
692 * where `m` is the bit size associated with the curve, i.e. the bit size
693 * of the order of the curve's coordinate field. This byte string is
694 * in little-endian order for Montgomery curves (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +0100695 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
696 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
697 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
Steven Cooreman6f5cc712020-06-11 16:40:41 +0200698 * For Weierstrass curves, this is the content of the `privateKey` field of
699 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
700 * the format is defined by RFC 7748, and output is masked according to §5.
Gilles Peskine67546802021-02-24 21:49:40 +0100701 * For twisted Edwards curves, the private key is as defined by RFC 8032
702 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200703 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200704 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000705 * format is the representation of the private key `x` as a big-endian byte
706 * string. The length of the byte string is the private key size in bytes
707 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200708 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
709 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100710 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200711 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
712 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200713 * \param key Identifier of the key to export. It must allow the
Ronald Cron96783552020-10-19 12:06:30 +0200714 * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
Ronald Croncf56a0a2020-08-04 09:51:30 +0200715 * key.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200716 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200717 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200718 * \param[out] data_length On success, the number of bytes
719 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100720 *
Gilles Peskineed733552023-02-14 19:21:09 +0100721 * \retval #PSA_SUCCESS \emptydescription
722 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +0200723 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200724 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Gilles Peskineed733552023-02-14 19:21:09 +0100725 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Gilles Peskine1be949b2018-08-10 19:06:59 +0200726 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
727 * The size of the \p data buffer is too small. You can determine a
728 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100729 * #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200730 * where \c type is the key type
731 * and \c bits is the key size in bits.
Gilles Peskineed733552023-02-14 19:21:09 +0100732 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
733 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
734 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
735 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
736 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +0300737 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300738 * The library has not been previously initialized by psa_crypto_init().
739 * It is implementation-dependent whether a failure to initialize
740 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100741 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200742psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 uint8_t *data,
744 size_t data_size,
745 size_t *data_length);
746
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100747/**
748 * \brief Export a public key or the public part of a key pair in binary format.
749 *
750 * The output of this function can be passed to psa_import_key() to
751 * create an object that is equivalent to the public key.
752 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000753 * This specification supports a single format for each key type.
754 * Implementations may support other formats as long as the standard
755 * format is supported. Implementations that support other formats
756 * should ensure that the formats are clearly unambiguous so as to
757 * minimize the risk that an invalid input is accidentally interpreted
758 * according to a different format.
759 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000760 * For standard key types, the output format is as follows:
761 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
762 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
763 * ```
764 * RSAPublicKey ::= SEQUENCE {
765 * modulus INTEGER, -- n
766 * publicExponent INTEGER } -- e
767 * ```
Gilles Peskine67546802021-02-24 21:49:40 +0100768 * - For elliptic curve keys on a twisted Edwards curve (key types for which
Bence Szépkúti3b1cba82021-04-08 15:49:07 +0200769 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true and #PSA_KEY_TYPE_ECC_GET_FAMILY
Gilles Peskine67546802021-02-24 21:49:40 +0100770 * returns #PSA_ECC_FAMILY_TWISTED_EDWARDS), the public key is as defined
771 * by RFC 8032
772 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
773 * - For other elliptic curve public keys (key types for which
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000774 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
775 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
776 * Let `m` be the bit size associated with the curve, i.e. the bit size of
777 * `q` for a curve over `F_q`. The representation consists of:
778 * - The byte 0x04;
779 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
780 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200781 * - For Diffie-Hellman key exchange public keys (key types for which
782 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000783 * the format is the representation of the public key `y = g^x mod p` as a
784 * big-endian byte string. The length of the byte string is the length of the
785 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100786 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200787 * Exporting a public key object or the public part of a key pair is
788 * always permitted, regardless of the key's usage flags.
789 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200790 * \param key Identifier of the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200791 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200792 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200793 * \param[out] data_length On success, the number of bytes
794 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100795 *
Gilles Peskineed733552023-02-14 19:21:09 +0100796 * \retval #PSA_SUCCESS \emptydescription
797 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +0200798 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200799 * The key is neither a public key nor a key pair.
Gilles Peskineed733552023-02-14 19:21:09 +0100800 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Gilles Peskine1be949b2018-08-10 19:06:59 +0200801 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
802 * The size of the \p data buffer is too small. You can determine a
803 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100804 * #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200805 * where \c type is the key type
806 * and \c bits is the key size in bits.
Gilles Peskineed733552023-02-14 19:21:09 +0100807 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
808 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
809 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
810 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
811 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +0300812 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300813 * The library has not been previously initialized by psa_crypto_init().
814 * It is implementation-dependent whether a failure to initialize
815 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100816 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200817psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100818 uint8_t *data,
819 size_t data_size,
820 size_t *data_length);
821
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100822
Gilles Peskine20035e32018-02-03 22:44:14 +0100823
824/**@}*/
825
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100826/** \defgroup hash Message digests
827 * @{
828 */
829
Gilles Peskine69647a42019-01-14 20:18:12 +0100830/** Calculate the hash (digest) of a message.
831 *
832 * \note To verify the hash of a message against an
833 * expected value, use psa_hash_compare() instead.
834 *
835 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
836 * such that #PSA_ALG_IS_HASH(\p alg) is true).
837 * \param[in] input Buffer containing the message to hash.
838 * \param input_length Size of the \p input buffer in bytes.
839 * \param[out] hash Buffer where the hash is to be written.
840 * \param hash_size Size of the \p hash buffer in bytes.
841 * \param[out] hash_length On success, the number of bytes
842 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100843 * #PSA_HASH_LENGTH(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100844 *
845 * \retval #PSA_SUCCESS
846 * Success.
847 * \retval #PSA_ERROR_NOT_SUPPORTED
848 * \p alg is not supported or is not a hash algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +0100849 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100850 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
851 * \p hash_size is too small
Gilles Peskineed733552023-02-14 19:21:09 +0100852 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
853 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
854 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
855 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100856 * \retval #PSA_ERROR_BAD_STATE
857 * The library has not been previously initialized by psa_crypto_init().
858 * It is implementation-dependent whether a failure to initialize
859 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100860 */
861psa_status_t psa_hash_compute(psa_algorithm_t alg,
862 const uint8_t *input,
863 size_t input_length,
864 uint8_t *hash,
865 size_t hash_size,
866 size_t *hash_length);
867
868/** Calculate the hash (digest) of a message and compare it with a
869 * reference value.
870 *
871 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
872 * such that #PSA_ALG_IS_HASH(\p alg) is true).
873 * \param[in] input Buffer containing the message to hash.
874 * \param input_length Size of the \p input buffer in bytes.
875 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100876 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100877 *
878 * \retval #PSA_SUCCESS
879 * The expected hash is identical to the actual hash of the input.
880 * \retval #PSA_ERROR_INVALID_SIGNATURE
881 * The hash of the message was calculated successfully, but it
882 * differs from the expected hash.
883 * \retval #PSA_ERROR_NOT_SUPPORTED
884 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100885 * \retval #PSA_ERROR_INVALID_ARGUMENT
886 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskineed733552023-02-14 19:21:09 +0100887 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
888 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
889 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
890 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100891 * \retval #PSA_ERROR_BAD_STATE
892 * The library has not been previously initialized by psa_crypto_init().
893 * It is implementation-dependent whether a failure to initialize
894 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100895 */
896psa_status_t psa_hash_compare(psa_algorithm_t alg,
897 const uint8_t *input,
898 size_t input_length,
899 const uint8_t *hash,
Gilles Peskinefa710f52019-12-02 14:31:48 +0100900 size_t hash_length);
Gilles Peskine69647a42019-01-14 20:18:12 +0100901
Gilles Peskine308b91d2018-02-08 09:47:44 +0100902/** The type of the state data structure for multipart hash operations.
903 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000904 * Before calling any function on a hash operation object, the application must
905 * initialize it by any of the following means:
906 * - Set the structure to all-bits-zero, for example:
907 * \code
908 * psa_hash_operation_t operation;
909 * memset(&operation, 0, sizeof(operation));
910 * \endcode
911 * - Initialize the structure to logical zero values, for example:
912 * \code
913 * psa_hash_operation_t operation = {0};
914 * \endcode
915 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
916 * for example:
917 * \code
918 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
919 * \endcode
920 * - Assign the result of the function psa_hash_operation_init()
921 * to the structure, for example:
922 * \code
923 * psa_hash_operation_t operation;
924 * operation = psa_hash_operation_init();
925 * \endcode
926 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100927 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +0100928 * make any assumptions about the content of this structure.
929 * Implementation details can change in future versions without notice. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100930typedef struct psa_hash_operation_s psa_hash_operation_t;
931
Jaeden Amero6a25b412019-01-04 11:47:44 +0000932/** \def PSA_HASH_OPERATION_INIT
933 *
934 * This macro returns a suitable initializer for a hash operation object
935 * of type #psa_hash_operation_t.
936 */
Jaeden Amero6a25b412019-01-04 11:47:44 +0000937
938/** Return an initial value for a hash operation object.
939 */
940static psa_hash_operation_t psa_hash_operation_init(void);
941
Gilles Peskinef45adda2019-01-14 18:29:18 +0100942/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100943 *
944 * The sequence of operations to calculate a hash (message digest)
945 * is as follows:
946 * -# Allocate an operation object which will be passed to all the functions
947 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000948 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100949 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200950 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100951 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100952 * of the message each time. The hash that is calculated is the hash
953 * of the concatenation of these messages in order.
954 * -# To calculate the hash, call psa_hash_finish().
955 * To compare the hash with an expected value, call psa_hash_verify().
956 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100957 * If an error occurs at any step after a call to psa_hash_setup(), the
958 * operation will need to be reset by a call to psa_hash_abort(). The
959 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000960 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100961 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200962 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100963 * eventually terminate the operation. The following events terminate an
964 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100965 * - A successful call to psa_hash_finish() or psa_hash_verify().
966 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100967 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000968 * \param[in,out] operation The operation object to set up. It must have
969 * been initialized as per the documentation for
970 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200971 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
972 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100973 *
Gilles Peskine28538492018-07-11 17:34:00 +0200974 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100975 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200976 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100977 * \p alg is not a supported hash algorithm.
978 * \retval #PSA_ERROR_INVALID_ARGUMENT
979 * \p alg is not a hash algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +0100980 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
981 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
982 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
983 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100984 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -0500985 * The operation state is not valid (it must be inactive), or
986 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100987 * It is implementation-dependent whether a failure to initialize
988 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100989 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200990psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100991 psa_algorithm_t alg);
992
Gilles Peskine308b91d2018-02-08 09:47:44 +0100993/** Add a message fragment to a multipart hash operation.
994 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200995 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100996 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100997 * If this function returns an error status, the operation enters an error
998 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100999 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001000 * \param[in,out] operation Active hash operation.
1001 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001002 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001003 *
Gilles Peskine28538492018-07-11 17:34:00 +02001004 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001005 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001006 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1007 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1008 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1009 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001010 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001011 * The operation state is not valid (it must be active), or
1012 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001013 * It is implementation-dependent whether a failure to initialize
1014 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001015 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001016psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1017 const uint8_t *input,
1018 size_t input_length);
1019
Gilles Peskine308b91d2018-02-08 09:47:44 +01001020/** Finish the calculation of the hash of a message.
1021 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001022 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001023 * This function calculates the hash of the message formed by concatenating
1024 * the inputs passed to preceding calls to psa_hash_update().
1025 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001026 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001027 * If this function returns an error status, the operation enters an error
1028 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001029 *
1030 * \warning Applications should not call this function if they expect
1031 * a specific value for the hash. Call psa_hash_verify() instead.
1032 * Beware that comparing integrity or authenticity data such as
1033 * hash values with a function such as \c memcmp is risky
1034 * because the time taken by the comparison may leak information
1035 * about the hashed data which could allow an attacker to guess
1036 * a valid hash and thereby bypass security controls.
1037 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001038 * \param[in,out] operation Active hash operation.
1039 * \param[out] hash Buffer where the hash is to be written.
1040 * \param hash_size Size of the \p hash buffer in bytes.
1041 * \param[out] hash_length On success, the number of bytes
1042 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001043 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001044 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001045 *
Gilles Peskine28538492018-07-11 17:34:00 +02001046 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001047 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001048 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001049 * The size of the \p hash buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001050 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001051 * where \c alg is the hash algorithm that is calculated.
Gilles Peskineed733552023-02-14 19:21:09 +01001052 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1053 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1054 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1055 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001056 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001057 * The operation state is not valid (it must be active), or
1058 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001059 * It is implementation-dependent whether a failure to initialize
1060 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001061 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001062psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1063 uint8_t *hash,
1064 size_t hash_size,
1065 size_t *hash_length);
1066
Gilles Peskine308b91d2018-02-08 09:47:44 +01001067/** Finish the calculation of the hash of a message and compare it with
1068 * an expected value.
1069 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001070 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001071 * This function calculates the hash of the message formed by concatenating
1072 * the inputs passed to preceding calls to psa_hash_update(). It then
1073 * compares the calculated hash with the expected hash passed as a
1074 * parameter to this function.
1075 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001076 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001077 * If this function returns an error status, the operation enters an error
1078 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001079 *
Gilles Peskine19067982018-03-20 17:54:53 +01001080 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001081 * comparison between the actual hash and the expected hash is performed
1082 * in constant time.
1083 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001084 * \param[in,out] operation Active hash operation.
1085 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001086 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001087 *
Gilles Peskine28538492018-07-11 17:34:00 +02001088 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001089 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001090 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001091 * The hash of the message was calculated successfully, but it
1092 * differs from the expected hash.
Gilles Peskineed733552023-02-14 19:21:09 +01001093 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1094 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1095 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1096 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001097 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001098 * The operation state is not valid (it must be active), or
1099 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001100 * It is implementation-dependent whether a failure to initialize
1101 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001102 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001103psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1104 const uint8_t *hash,
1105 size_t hash_length);
1106
Gilles Peskine308b91d2018-02-08 09:47:44 +01001107/** Abort a hash operation.
1108 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001109 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001110 * \p operation structure itself. Once aborted, the operation object
1111 * can be reused for another operation by calling
1112 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001113 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001114 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001115 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001116 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001117 * In particular, calling psa_hash_abort() after the operation has been
1118 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1119 * psa_hash_verify() is safe and has no effect.
1120 *
1121 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001122 *
Gilles Peskineed733552023-02-14 19:21:09 +01001123 * \retval #PSA_SUCCESS \emptydescription
1124 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1125 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1126 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001127 * \retval #PSA_ERROR_BAD_STATE
1128 * The library has not been previously initialized by psa_crypto_init().
1129 * It is implementation-dependent whether a failure to initialize
1130 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001131 */
1132psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001133
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001134/** Clone a hash operation.
1135 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001136 * This function copies the state of an ongoing hash operation to
1137 * a new operation object. In other words, this function is equivalent
1138 * to calling psa_hash_setup() on \p target_operation with the same
1139 * algorithm that \p source_operation was set up for, then
1140 * psa_hash_update() on \p target_operation with the same input that
1141 * that was passed to \p source_operation. After this function returns, the
1142 * two objects are independent, i.e. subsequent calls involving one of
1143 * the objects do not affect the other object.
1144 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001145 * \param[in] source_operation The active hash operation to clone.
1146 * \param[in,out] target_operation The operation object to set up.
1147 * It must be initialized but not active.
1148 *
Gilles Peskineed733552023-02-14 19:21:09 +01001149 * \retval #PSA_SUCCESS \emptydescription
1150 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1151 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1152 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1153 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001154 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001155 * The \p source_operation state is not valid (it must be active), or
1156 * the \p target_operation state is not valid (it must be inactive), or
1157 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001158 * It is implementation-dependent whether a failure to initialize
1159 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001160 */
1161psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1162 psa_hash_operation_t *target_operation);
1163
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001164/**@}*/
1165
Gilles Peskine8c9def32018-02-08 10:02:12 +01001166/** \defgroup MAC Message authentication codes
1167 * @{
1168 */
1169
Gilles Peskine69647a42019-01-14 20:18:12 +01001170/** Calculate the MAC (message authentication code) of a message.
1171 *
1172 * \note To verify the MAC of a message against an
1173 * expected value, use psa_mac_verify() instead.
1174 * Beware that comparing integrity or authenticity data such as
1175 * MAC values with a function such as \c memcmp is risky
1176 * because the time taken by the comparison may leak information
1177 * about the MAC value which could allow an attacker to guess
1178 * a valid MAC and thereby bypass security controls.
1179 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001180 * \param key Identifier of the key to use for the operation. It
1181 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001182 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001183 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001184 * \param[in] input Buffer containing the input message.
1185 * \param input_length Size of the \p input buffer in bytes.
1186 * \param[out] mac Buffer where the MAC value is to be written.
1187 * \param mac_size Size of the \p mac buffer in bytes.
1188 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001189 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001190 *
1191 * \retval #PSA_SUCCESS
1192 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001193 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1194 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001195 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001196 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001197 * \retval #PSA_ERROR_NOT_SUPPORTED
1198 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001199 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1200 * \p mac_size is too small
Gilles Peskineed733552023-02-14 19:21:09 +01001201 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1202 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1203 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1204 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001205 * \retval #PSA_ERROR_STORAGE_FAILURE
1206 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001207 * \retval #PSA_ERROR_BAD_STATE
1208 * The library has not been previously initialized by psa_crypto_init().
1209 * It is implementation-dependent whether a failure to initialize
1210 * results in this error code.
1211 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001212psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001213 psa_algorithm_t alg,
1214 const uint8_t *input,
1215 size_t input_length,
1216 uint8_t *mac,
1217 size_t mac_size,
1218 size_t *mac_length);
1219
1220/** Calculate the MAC of a message and compare it with a reference value.
1221 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001222 * \param key Identifier of the key to use for the operation. It
1223 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001224 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001225 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001226 * \param[in] input Buffer containing the input message.
1227 * \param input_length Size of the \p input buffer in bytes.
1228 * \param[out] mac Buffer containing the expected MAC value.
1229 * \param mac_length Size of the \p mac buffer in bytes.
1230 *
1231 * \retval #PSA_SUCCESS
1232 * The expected MAC is identical to the actual MAC of the input.
1233 * \retval #PSA_ERROR_INVALID_SIGNATURE
1234 * The MAC of the message was calculated successfully, but it
1235 * differs from the expected value.
Gilles Peskineed733552023-02-14 19:21:09 +01001236 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1237 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001238 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001239 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001240 * \retval #PSA_ERROR_NOT_SUPPORTED
1241 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001242 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1243 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1244 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1245 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001246 * \retval #PSA_ERROR_STORAGE_FAILURE
1247 * The key could not be retrieved from storage.
1248 * \retval #PSA_ERROR_BAD_STATE
1249 * The library has not been previously initialized by psa_crypto_init().
1250 * It is implementation-dependent whether a failure to initialize
1251 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001252 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001253psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
Gilles Peskinea05602d2019-01-17 15:25:52 +01001254 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001255 const uint8_t *input,
1256 size_t input_length,
1257 const uint8_t *mac,
Gilles Peskine13faa2d2020-01-30 16:32:21 +01001258 size_t mac_length);
Gilles Peskine69647a42019-01-14 20:18:12 +01001259
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001260/** The type of the state data structure for multipart MAC operations.
1261 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001262 * Before calling any function on a MAC operation object, the application must
1263 * initialize it by any of the following means:
1264 * - Set the structure to all-bits-zero, for example:
1265 * \code
1266 * psa_mac_operation_t operation;
1267 * memset(&operation, 0, sizeof(operation));
1268 * \endcode
1269 * - Initialize the structure to logical zero values, for example:
1270 * \code
1271 * psa_mac_operation_t operation = {0};
1272 * \endcode
1273 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1274 * for example:
1275 * \code
1276 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1277 * \endcode
1278 * - Assign the result of the function psa_mac_operation_init()
1279 * to the structure, for example:
1280 * \code
1281 * psa_mac_operation_t operation;
1282 * operation = psa_mac_operation_init();
1283 * \endcode
1284 *
Janos Follath2ba60792021-04-28 09:37:34 +01001285 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001286 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01001287 * make any assumptions about the content of this structure.
1288 * Implementation details can change in future versions without notice. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001289typedef struct psa_mac_operation_s psa_mac_operation_t;
1290
Jaeden Amero769ce272019-01-04 11:48:03 +00001291/** \def PSA_MAC_OPERATION_INIT
1292 *
1293 * This macro returns a suitable initializer for a MAC operation object of type
1294 * #psa_mac_operation_t.
1295 */
Jaeden Amero769ce272019-01-04 11:48:03 +00001296
1297/** Return an initial value for a MAC operation object.
1298 */
1299static psa_mac_operation_t psa_mac_operation_init(void);
1300
Gilles Peskinef45adda2019-01-14 18:29:18 +01001301/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001302 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001303 * This function sets up the calculation of the MAC
1304 * (message authentication code) of a byte string.
1305 * To verify the MAC of a message against an
1306 * expected value, use psa_mac_verify_setup() instead.
1307 *
1308 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001309 * -# Allocate an operation object which will be passed to all the functions
1310 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001311 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001312 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001313 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001314 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1315 * of the message each time. The MAC that is calculated is the MAC
1316 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001317 * -# At the end of the message, call psa_mac_sign_finish() to finish
1318 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001319 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001320 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1321 * operation will need to be reset by a call to psa_mac_abort(). The
1322 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001323 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001324 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001325 * After a successful call to psa_mac_sign_setup(), the application must
1326 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001327 * - A successful call to psa_mac_sign_finish().
1328 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001329 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001330 * \param[in,out] operation The operation object to set up. It must have
1331 * been initialized as per the documentation for
1332 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001333 * \param key Identifier of the key to use for the operation. It
1334 * must remain valid until the operation terminates.
1335 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001336 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001337 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001338 *
Gilles Peskine28538492018-07-11 17:34:00 +02001339 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001340 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001341 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1342 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001343 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001344 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001345 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001346 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001347 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1348 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1349 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1350 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001351 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001352 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001353 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001354 * The operation state is not valid (it must be inactive), or
1355 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001356 * It is implementation-dependent whether a failure to initialize
1357 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001358 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001359psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001360 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001361 psa_algorithm_t alg);
1362
Gilles Peskinef45adda2019-01-14 18:29:18 +01001363/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001364 *
1365 * This function sets up the verification of the MAC
1366 * (message authentication code) of a byte string against an expected value.
1367 *
1368 * The sequence of operations to verify a MAC is as follows:
1369 * -# Allocate an operation object which will be passed to all the functions
1370 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001371 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001372 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001373 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001374 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1375 * of the message each time. The MAC that is calculated is the MAC
1376 * of the concatenation of these messages in order.
1377 * -# At the end of the message, call psa_mac_verify_finish() to finish
1378 * calculating the actual MAC of the message and verify it against
1379 * the expected value.
1380 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001381 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1382 * operation will need to be reset by a call to psa_mac_abort(). The
1383 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001384 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001385 *
1386 * After a successful call to psa_mac_verify_setup(), the application must
1387 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001388 * - A successful call to psa_mac_verify_finish().
1389 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001390 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001391 * \param[in,out] operation The operation object to set up. It must have
1392 * been initialized as per the documentation for
1393 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001394 * \param key Identifier of the key to use for the operation. It
1395 * must remain valid until the operation terminates.
1396 * It must allow the usage
1397 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001398 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1399 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001400 *
Gilles Peskine28538492018-07-11 17:34:00 +02001401 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001402 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001403 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1404 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001405 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001406 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001407 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001408 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001409 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1410 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1411 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1412 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001413 * \retval #PSA_ERROR_STORAGE_FAILURE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001414 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001415 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001416 * The operation state is not valid (it must be inactive), or
1417 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001418 * It is implementation-dependent whether a failure to initialize
1419 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001420 */
1421psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001422 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001423 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001424
Gilles Peskinedcd14942018-07-12 00:30:52 +02001425/** Add a message fragment to a multipart MAC operation.
1426 *
1427 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1428 * before calling this function.
1429 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001430 * If this function returns an error status, the operation enters an error
1431 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001432 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001433 * \param[in,out] operation Active MAC operation.
1434 * \param[in] input Buffer containing the message fragment to add to
1435 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001436 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001437 *
1438 * \retval #PSA_SUCCESS
1439 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001440 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1441 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1442 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1443 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1444 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001445 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001446 * The operation state is not valid (it must be active), or
1447 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001448 * It is implementation-dependent whether a failure to initialize
1449 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001450 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001451psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1452 const uint8_t *input,
1453 size_t input_length);
1454
Gilles Peskinedcd14942018-07-12 00:30:52 +02001455/** Finish the calculation of the MAC of a message.
1456 *
1457 * The application must call psa_mac_sign_setup() before calling this function.
1458 * This function calculates the MAC of the message formed by concatenating
1459 * the inputs passed to preceding calls to psa_mac_update().
1460 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001461 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001462 * If this function returns an error status, the operation enters an error
1463 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001464 *
1465 * \warning Applications should not call this function if they expect
1466 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1467 * Beware that comparing integrity or authenticity data such as
1468 * MAC values with a function such as \c memcmp is risky
1469 * because the time taken by the comparison may leak information
1470 * about the MAC value which could allow an attacker to guess
1471 * a valid MAC and thereby bypass security controls.
1472 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001473 * \param[in,out] operation Active MAC operation.
1474 * \param[out] mac Buffer where the MAC value is to be written.
1475 * \param mac_size Size of the \p mac buffer in bytes.
1476 * \param[out] mac_length On success, the number of bytes
1477 * that make up the MAC value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001478 * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001479 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001480 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001481 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001482 *
1483 * \retval #PSA_SUCCESS
1484 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001485 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001486 * The size of the \p mac buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001487 * sufficient buffer size by calling PSA_MAC_LENGTH().
Gilles Peskineed733552023-02-14 19:21:09 +01001488 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1489 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1490 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1491 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1492 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001493 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001494 * The operation state is not valid (it must be an active mac sign
1495 * operation), or the library has not been previously initialized
1496 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001497 * It is implementation-dependent whether a failure to initialize
1498 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001499 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001500psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1501 uint8_t *mac,
1502 size_t mac_size,
1503 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001504
Gilles Peskinedcd14942018-07-12 00:30:52 +02001505/** Finish the calculation of the MAC of a message and compare it with
1506 * an expected value.
1507 *
1508 * The application must call psa_mac_verify_setup() before calling this function.
1509 * This function calculates the MAC of the message formed by concatenating
1510 * the inputs passed to preceding calls to psa_mac_update(). It then
1511 * compares the calculated MAC with the expected MAC passed as a
1512 * parameter to this function.
1513 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001514 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001515 * If this function returns an error status, the operation enters an error
1516 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001517 *
1518 * \note Implementations shall make the best effort to ensure that the
1519 * comparison between the actual MAC and the expected MAC is performed
1520 * in constant time.
1521 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001522 * \param[in,out] operation Active MAC operation.
1523 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001524 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001525 *
1526 * \retval #PSA_SUCCESS
1527 * The expected MAC is identical to the actual MAC of the message.
1528 * \retval #PSA_ERROR_INVALID_SIGNATURE
1529 * The MAC of the message was calculated successfully, but it
1530 * differs from the expected MAC.
Gilles Peskineed733552023-02-14 19:21:09 +01001531 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1532 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1533 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1534 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1535 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001536 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001537 * The operation state is not valid (it must be an active mac verify
1538 * operation), or the library has not been previously initialized
1539 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001540 * It is implementation-dependent whether a failure to initialize
1541 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001542 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001543psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1544 const uint8_t *mac,
1545 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001546
Gilles Peskinedcd14942018-07-12 00:30:52 +02001547/** Abort a MAC operation.
1548 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001549 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001550 * \p operation structure itself. Once aborted, the operation object
1551 * can be reused for another operation by calling
1552 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001553 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001554 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001555 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001556 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001557 * In particular, calling psa_mac_abort() after the operation has been
1558 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1559 * psa_mac_verify_finish() is safe and has no effect.
1560 *
1561 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001562 *
Gilles Peskineed733552023-02-14 19:21:09 +01001563 * \retval #PSA_SUCCESS \emptydescription
1564 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1565 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1566 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001567 * \retval #PSA_ERROR_BAD_STATE
1568 * The library has not been previously initialized by psa_crypto_init().
1569 * It is implementation-dependent whether a failure to initialize
1570 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001571 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001572psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1573
1574/**@}*/
1575
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001576/** \defgroup cipher Symmetric ciphers
1577 * @{
1578 */
1579
Gilles Peskine69647a42019-01-14 20:18:12 +01001580/** Encrypt a message using a symmetric cipher.
1581 *
1582 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001583 * vector). Use the multipart operation interface with a
1584 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001585 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001586 * \param key Identifier of the key to use for the operation.
Ronald Cron96783552020-10-19 12:06:30 +02001587 * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001588 * \param alg The cipher algorithm to compute
1589 * (\c PSA_ALG_XXX value such that
1590 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1591 * \param[in] input Buffer containing the message to encrypt.
1592 * \param input_length Size of the \p input buffer in bytes.
1593 * \param[out] output Buffer where the output is to be written.
1594 * The output contains the IV followed by
1595 * the ciphertext proper.
1596 * \param output_size Size of the \p output buffer in bytes.
1597 * \param[out] output_length On success, the number of bytes
1598 * that make up the output.
1599 *
1600 * \retval #PSA_SUCCESS
1601 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001602 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1603 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001604 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001605 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001606 * \retval #PSA_ERROR_NOT_SUPPORTED
1607 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001608 * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
1609 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1610 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1611 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1612 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1613 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001614 * \retval #PSA_ERROR_BAD_STATE
1615 * The library has not been previously initialized by psa_crypto_init().
1616 * It is implementation-dependent whether a failure to initialize
1617 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001618 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001619psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001620 psa_algorithm_t alg,
1621 const uint8_t *input,
1622 size_t input_length,
1623 uint8_t *output,
1624 size_t output_size,
1625 size_t *output_length);
1626
1627/** Decrypt a message using a symmetric cipher.
1628 *
1629 * This function decrypts a message encrypted with a symmetric cipher.
1630 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001631 * \param key Identifier of the key to use for the operation.
Gilles Peskine69647a42019-01-14 20:18:12 +01001632 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001633 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001634 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001635 * \param alg The cipher algorithm to compute
1636 * (\c PSA_ALG_XXX value such that
1637 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1638 * \param[in] input Buffer containing the message to decrypt.
1639 * This consists of the IV followed by the
1640 * ciphertext proper.
1641 * \param input_length Size of the \p input buffer in bytes.
1642 * \param[out] output Buffer where the plaintext is to be written.
1643 * \param output_size Size of the \p output buffer in bytes.
1644 * \param[out] output_length On success, the number of bytes
1645 * that make up the output.
1646 *
1647 * \retval #PSA_SUCCESS
1648 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001649 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1650 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine69647a42019-01-14 20:18:12 +01001651 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001652 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001653 * \retval #PSA_ERROR_NOT_SUPPORTED
1654 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001655 * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
1656 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1657 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1658 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1659 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1660 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001661 * \retval #PSA_ERROR_BAD_STATE
1662 * The library has not been previously initialized by psa_crypto_init().
1663 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001664 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001665 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001666psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001667 psa_algorithm_t alg,
1668 const uint8_t *input,
1669 size_t input_length,
1670 uint8_t *output,
1671 size_t output_size,
1672 size_t *output_length);
1673
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001674/** The type of the state data structure for multipart cipher operations.
1675 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001676 * Before calling any function on a cipher operation object, the application
1677 * must initialize it by any of the following means:
1678 * - Set the structure to all-bits-zero, for example:
1679 * \code
1680 * psa_cipher_operation_t operation;
1681 * memset(&operation, 0, sizeof(operation));
1682 * \endcode
1683 * - Initialize the structure to logical zero values, for example:
1684 * \code
1685 * psa_cipher_operation_t operation = {0};
1686 * \endcode
1687 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1688 * for example:
1689 * \code
1690 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1691 * \endcode
1692 * - Assign the result of the function psa_cipher_operation_init()
1693 * to the structure, for example:
1694 * \code
1695 * psa_cipher_operation_t operation;
1696 * operation = psa_cipher_operation_init();
1697 * \endcode
1698 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001699 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01001700 * make any assumptions about the content of this structure.
1701 * Implementation details can change in future versions without notice. */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001702typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1703
Jaeden Amero5bae2272019-01-04 11:48:27 +00001704/** \def PSA_CIPHER_OPERATION_INIT
1705 *
1706 * This macro returns a suitable initializer for a cipher operation object of
1707 * type #psa_cipher_operation_t.
1708 */
Jaeden Amero5bae2272019-01-04 11:48:27 +00001709
1710/** Return an initial value for a cipher operation object.
1711 */
1712static psa_cipher_operation_t psa_cipher_operation_init(void);
1713
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001714/** Set the key for a multipart symmetric encryption operation.
1715 *
1716 * The sequence of operations to encrypt a message with a symmetric cipher
1717 * is as follows:
1718 * -# Allocate an operation object which will be passed to all the functions
1719 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001720 * -# Initialize the operation object with one of the methods described in the
1721 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001722 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001723 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001724 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001725 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001726 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001727 * requires a specific IV value.
1728 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1729 * of the message each time.
1730 * -# Call psa_cipher_finish().
1731 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001732 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1733 * the operation will need to be reset by a call to psa_cipher_abort(). The
1734 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001735 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001736 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001737 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001738 * eventually terminate the operation. The following events terminate an
1739 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001740 * - A successful call to psa_cipher_finish().
1741 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001742 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001743 * \param[in,out] operation The operation object to set up. It must have
1744 * been initialized as per the documentation for
1745 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001746 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001747 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001748 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001749 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001750 * \param alg The cipher algorithm to compute
1751 * (\c PSA_ALG_XXX value such that
1752 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001753 *
Gilles Peskine28538492018-07-11 17:34:00 +02001754 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001755 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001756 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1757 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001758 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001759 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001760 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001761 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001762 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1763 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1764 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1765 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1766 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03001767 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001768 * The operation state is not valid (it must be inactive), or
1769 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001770 * It is implementation-dependent whether a failure to initialize
1771 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001772 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001773psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001774 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001775 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001776
1777/** Set the key for a multipart symmetric decryption operation.
1778 *
1779 * The sequence of operations to decrypt a message with a symmetric cipher
1780 * is as follows:
1781 * -# Allocate an operation object which will be passed to all the functions
1782 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001783 * -# Initialize the operation object with one of the methods described in the
1784 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001785 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001786 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001787 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001788 * decryption. If the IV is prepended to the ciphertext, you can call
1789 * psa_cipher_update() on a buffer containing the IV followed by the
1790 * beginning of the message.
1791 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1792 * of the message each time.
1793 * -# Call psa_cipher_finish().
1794 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001795 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1796 * the operation will need to be reset by a call to psa_cipher_abort(). The
1797 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001798 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001799 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001800 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001801 * eventually terminate the operation. The following events terminate an
1802 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001803 * - A successful call to psa_cipher_finish().
1804 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001805 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001806 * \param[in,out] operation The operation object to set up. It must have
1807 * been initialized as per the documentation for
1808 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001809 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001810 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001811 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001812 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001813 * \param alg The cipher algorithm to compute
1814 * (\c PSA_ALG_XXX value such that
1815 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001816 *
Gilles Peskine28538492018-07-11 17:34:00 +02001817 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001818 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01001819 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
1820 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02001821 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001822 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001823 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001824 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01001825 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1826 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1827 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1828 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1829 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03001830 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001831 * The operation state is not valid (it must be inactive), or
1832 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001833 * It is implementation-dependent whether a failure to initialize
1834 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001835 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001836psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001837 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001838 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001839
Gilles Peskinedcd14942018-07-12 00:30:52 +02001840/** Generate an IV for a symmetric encryption operation.
1841 *
1842 * This function generates a random IV (initialization vector), nonce
1843 * or initial counter value for the encryption operation as appropriate
1844 * for the chosen algorithm, key type and key size.
1845 *
1846 * The application must call psa_cipher_encrypt_setup() before
1847 * calling this function.
1848 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001849 * If this function returns an error status, the operation enters an error
1850 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001851 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001852 * \param[in,out] operation Active cipher operation.
1853 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001854 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001855 * \param[out] iv_length On success, the number of bytes of the
1856 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001857 *
1858 * \retval #PSA_SUCCESS
1859 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001860 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001861 * The size of the \p iv buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01001862 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1863 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1864 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1865 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1866 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001867 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001868 * The operation state is not valid (it must be active, with no IV set),
1869 * or the library has not been previously initialized
1870 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001871 * It is implementation-dependent whether a failure to initialize
1872 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001873 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001874psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001875 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001876 size_t iv_size,
1877 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001878
Gilles Peskinedcd14942018-07-12 00:30:52 +02001879/** Set the IV for a symmetric encryption or decryption operation.
1880 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001881 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001882 * or initial counter value for the encryption or decryption operation.
1883 *
1884 * The application must call psa_cipher_encrypt_setup() before
1885 * calling this function.
1886 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001887 * If this function returns an error status, the operation enters an error
1888 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001889 *
1890 * \note When encrypting, applications should use psa_cipher_generate_iv()
1891 * instead of this function, unless implementing a protocol that requires
1892 * a non-random IV.
1893 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001894 * \param[in,out] operation Active cipher operation.
1895 * \param[in] iv Buffer containing the IV to use.
1896 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001897 *
1898 * \retval #PSA_SUCCESS
1899 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001900 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001901 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001902 * or the chosen algorithm does not use an IV.
Gilles Peskineed733552023-02-14 19:21:09 +01001903 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1904 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1905 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1906 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1907 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001908 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001909 * The operation state is not valid (it must be an active cipher
1910 * encrypt operation, with no IV set), or the library has not been
1911 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001912 * It is implementation-dependent whether a failure to initialize
1913 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001914 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001915psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001916 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001917 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001918
Gilles Peskinedcd14942018-07-12 00:30:52 +02001919/** Encrypt or decrypt a message fragment in an active cipher operation.
1920 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001921 * Before calling this function, you must:
1922 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1923 * The choice of setup function determines whether this function
1924 * encrypts or decrypts its input.
1925 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1926 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001927 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001928 * If this function returns an error status, the operation enters an error
1929 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001930 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001931 * \param[in,out] operation Active cipher operation.
1932 * \param[in] input Buffer containing the message fragment to
1933 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001934 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001935 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001936 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001937 * \param[out] output_length On success, the number of bytes
1938 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001939 *
1940 * \retval #PSA_SUCCESS
1941 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001942 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1943 * The size of the \p output buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01001944 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1945 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1946 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1947 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1948 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001949 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05001950 * The operation state is not valid (it must be active, with an IV set
1951 * if required for the algorithm), or the library has not been
1952 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001953 * It is implementation-dependent whether a failure to initialize
1954 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001955 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001956psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1957 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001958 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001959 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001960 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001961 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001962
Gilles Peskinedcd14942018-07-12 00:30:52 +02001963/** Finish encrypting or decrypting a message in a cipher operation.
1964 *
1965 * The application must call psa_cipher_encrypt_setup() or
1966 * psa_cipher_decrypt_setup() before calling this function. The choice
1967 * of setup function determines whether this function encrypts or
1968 * decrypts its input.
1969 *
1970 * This function finishes the encryption or decryption of the message
1971 * formed by concatenating the inputs passed to preceding calls to
1972 * psa_cipher_update().
1973 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001974 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001975 * If this function returns an error status, the operation enters an error
1976 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001977 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001978 * \param[in,out] operation Active cipher operation.
1979 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001980 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001981 * \param[out] output_length On success, the number of bytes
1982 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001983 *
1984 * \retval #PSA_SUCCESS
1985 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02001986 * \retval #PSA_ERROR_INVALID_ARGUMENT
1987 * The total input size passed to this operation is not valid for
1988 * this particular algorithm. For example, the algorithm is a based
1989 * on block cipher and requires a whole number of blocks, but the
1990 * total input size is not a multiple of the block size.
1991 * \retval #PSA_ERROR_INVALID_PADDING
1992 * This is a decryption operation for an algorithm that includes
1993 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001994 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1995 * The size of the \p output buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01001996 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1997 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1998 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1999 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2000 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002001 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002002 * The operation state is not valid (it must be active, with an IV set
2003 * if required for the algorithm), or the library has not been
2004 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002005 * It is implementation-dependent whether a failure to initialize
2006 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002007 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002008psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002009 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002010 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002011 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002012
Gilles Peskinedcd14942018-07-12 00:30:52 +02002013/** Abort a cipher operation.
2014 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002015 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002016 * \p operation structure itself. Once aborted, the operation object
2017 * can be reused for another operation by calling
2018 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002019 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002020 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002021 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002022 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002023 * In particular, calling psa_cipher_abort() after the operation has been
2024 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2025 * is safe and has no effect.
2026 *
2027 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002028 *
Gilles Peskineed733552023-02-14 19:21:09 +01002029 * \retval #PSA_SUCCESS \emptydescription
2030 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2031 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2032 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002033 * \retval #PSA_ERROR_BAD_STATE
2034 * The library has not been previously initialized by psa_crypto_init().
2035 * It is implementation-dependent whether a failure to initialize
2036 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002037 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002038psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2039
2040/**@}*/
2041
Gilles Peskine3b555712018-03-03 21:27:57 +01002042/** \defgroup aead Authenticated encryption with associated data (AEAD)
2043 * @{
2044 */
2045
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002046/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002047 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002048 * \param key Identifier of the key to use for the
2049 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002050 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002051 * \param alg The AEAD algorithm to compute
2052 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002053 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002054 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002055 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002056 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002057 * but not encrypted.
2058 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002059 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002060 * encrypted.
2061 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002062 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002063 * encrypted data. The additional data is not
2064 * part of this output. For algorithms where the
2065 * encrypted data and the authentication tag
2066 * are defined as separate outputs, the
2067 * authentication tag is appended to the
2068 * encrypted data.
2069 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002070 * This must be appropriate for the selected
2071 * algorithm and key:
2072 * - A sufficient output size is
2073 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type,
2074 * \p alg, \p plaintext_length) where
2075 * \c key_type is the type of \p key.
2076 * - #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p
2077 * plaintext_length) evaluates to the maximum
2078 * ciphertext size of any supported AEAD
2079 * encryption.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002080 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002081 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002082 *
Gilles Peskine28538492018-07-11 17:34:00 +02002083 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002084 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01002085 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2086 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002087 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002088 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002089 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002090 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002091 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002092 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Bence Szépkútibd98df72021-04-27 04:37:18 +02002093 * \p ciphertext_size is too small.
2094 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2095 * \p plaintext_length) or
2096 * #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length) can be used to
2097 * determine the required buffer size.
Gilles Peskineed733552023-02-14 19:21:09 +01002098 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2099 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2100 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2101 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03002102 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002103 * The library has not been previously initialized by psa_crypto_init().
2104 * It is implementation-dependent whether a failure to initialize
2105 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002106 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002107psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002108 psa_algorithm_t alg,
2109 const uint8_t *nonce,
2110 size_t nonce_length,
2111 const uint8_t *additional_data,
2112 size_t additional_data_length,
2113 const uint8_t *plaintext,
2114 size_t plaintext_length,
2115 uint8_t *ciphertext,
2116 size_t ciphertext_size,
2117 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002118
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002119/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002120 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002121 * \param key Identifier of the key to use for the
2122 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002123 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002124 * \param alg The AEAD algorithm to compute
2125 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002126 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002127 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002128 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002129 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002130 * but not encrypted.
2131 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002132 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002133 * encrypted. For algorithms where the
2134 * encrypted data and the authentication tag
2135 * are defined as separate inputs, the buffer
2136 * must contain the encrypted data followed
2137 * by the authentication tag.
2138 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002139 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002140 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002141 * This must be appropriate for the selected
2142 * algorithm and key:
2143 * - A sufficient output size is
2144 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type,
2145 * \p alg, \p ciphertext_length) where
2146 * \c key_type is the type of \p key.
2147 * - #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p
2148 * ciphertext_length) evaluates to the maximum
2149 * plaintext size of any supported AEAD
2150 * decryption.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002151 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002152 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002153 *
Gilles Peskine28538492018-07-11 17:34:00 +02002154 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002155 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01002156 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002157 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002158 * The ciphertext is not authentic.
Gilles Peskineed733552023-02-14 19:21:09 +01002159 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002160 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002161 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002162 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002163 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002164 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002165 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Bence Szépkútibd98df72021-04-27 04:37:18 +02002166 * \p plaintext_size is too small.
2167 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2168 * \p ciphertext_length) or
2169 * #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length) can be used
2170 * to determine the required buffer size.
Gilles Peskineed733552023-02-14 19:21:09 +01002171 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2172 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2173 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2174 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03002175 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002176 * The library has not been previously initialized by psa_crypto_init().
2177 * It is implementation-dependent whether a failure to initialize
2178 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002179 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002180psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002181 psa_algorithm_t alg,
2182 const uint8_t *nonce,
2183 size_t nonce_length,
2184 const uint8_t *additional_data,
2185 size_t additional_data_length,
2186 const uint8_t *ciphertext,
2187 size_t ciphertext_length,
2188 uint8_t *plaintext,
2189 size_t plaintext_size,
2190 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002191
Gilles Peskine30a9e412019-01-14 18:36:12 +01002192/** The type of the state data structure for multipart AEAD operations.
2193 *
2194 * Before calling any function on an AEAD operation object, the application
2195 * must initialize it by any of the following means:
2196 * - Set the structure to all-bits-zero, for example:
2197 * \code
2198 * psa_aead_operation_t operation;
2199 * memset(&operation, 0, sizeof(operation));
2200 * \endcode
2201 * - Initialize the structure to logical zero values, for example:
2202 * \code
2203 * psa_aead_operation_t operation = {0};
2204 * \endcode
2205 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2206 * for example:
2207 * \code
2208 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2209 * \endcode
2210 * - Assign the result of the function psa_aead_operation_init()
2211 * to the structure, for example:
2212 * \code
2213 * psa_aead_operation_t operation;
2214 * operation = psa_aead_operation_init();
2215 * \endcode
2216 *
2217 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01002218 * make any assumptions about the content of this structure.
2219 * Implementation details can change in future versions without notice. */
Gilles Peskine30a9e412019-01-14 18:36:12 +01002220typedef struct psa_aead_operation_s psa_aead_operation_t;
2221
2222/** \def PSA_AEAD_OPERATION_INIT
2223 *
2224 * This macro returns a suitable initializer for an AEAD operation object of
2225 * type #psa_aead_operation_t.
2226 */
Gilles Peskine30a9e412019-01-14 18:36:12 +01002227
2228/** Return an initial value for an AEAD operation object.
2229 */
2230static psa_aead_operation_t psa_aead_operation_init(void);
2231
2232/** Set the key for a multipart authenticated encryption operation.
2233 *
2234 * The sequence of operations to encrypt a message with authentication
2235 * is as follows:
2236 * -# Allocate an operation object which will be passed to all the functions
2237 * listed here.
2238 * -# Initialize the operation object with one of the methods described in the
2239 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002240 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002241 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002242 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2243 * inputs to the subsequent calls to psa_aead_update_ad() and
2244 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2245 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002246 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2247 * generate or set the nonce. You should use
2248 * psa_aead_generate_nonce() unless the protocol you are implementing
2249 * requires a specific nonce value.
2250 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2251 * of the non-encrypted additional authenticated data each time.
2252 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002253 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002254 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002255 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002256 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2257 * the operation will need to be reset by a call to psa_aead_abort(). The
2258 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002259 * has been initialized.
2260 *
2261 * After a successful call to psa_aead_encrypt_setup(), the application must
2262 * eventually terminate the operation. The following events terminate an
2263 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002264 * - A successful call to psa_aead_finish().
2265 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002266 *
2267 * \param[in,out] operation The operation object to set up. It must have
2268 * been initialized as per the documentation for
2269 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002270 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002271 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002272 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002273 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002274 * \param alg The AEAD algorithm to compute
2275 * (\c PSA_ALG_XXX value such that
2276 * #PSA_ALG_IS_AEAD(\p alg) is true).
2277 *
2278 * \retval #PSA_SUCCESS
2279 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002280 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002281 * The operation state is not valid (it must be inactive), or
2282 * the library has not been previously initialized by psa_crypto_init().
Gilles Peskineed733552023-02-14 19:21:09 +01002283 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2284 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine30a9e412019-01-14 18:36:12 +01002285 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002286 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002287 * \retval #PSA_ERROR_NOT_SUPPORTED
2288 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002289 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2290 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2291 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2292 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002293 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002294 * The library has not been previously initialized by psa_crypto_init().
2295 * It is implementation-dependent whether a failure to initialize
2296 * results in this error code.
2297 */
2298psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002299 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002300 psa_algorithm_t alg);
2301
2302/** Set the key for a multipart authenticated decryption operation.
2303 *
2304 * The sequence of operations to decrypt a message with authentication
2305 * is as follows:
2306 * -# Allocate an operation object which will be passed to all the functions
2307 * listed here.
2308 * -# Initialize the operation object with one of the methods described in the
2309 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002310 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002311 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002312 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2313 * inputs to the subsequent calls to psa_aead_update_ad() and
2314 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2315 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002316 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2317 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2318 * of the non-encrypted additional authenticated data each time.
2319 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002320 * of the ciphertext to decrypt each time.
2321 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002322 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002323 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2324 * the operation will need to be reset by a call to psa_aead_abort(). The
2325 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002326 * has been initialized.
2327 *
2328 * After a successful call to psa_aead_decrypt_setup(), the application must
2329 * eventually terminate the operation. The following events terminate an
2330 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002331 * - A successful call to psa_aead_verify().
2332 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002333 *
2334 * \param[in,out] operation The operation object to set up. It must have
2335 * been initialized as per the documentation for
2336 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002337 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002338 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002339 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002340 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002341 * \param alg The AEAD algorithm to compute
2342 * (\c PSA_ALG_XXX value such that
2343 * #PSA_ALG_IS_AEAD(\p alg) is true).
2344 *
2345 * \retval #PSA_SUCCESS
2346 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01002347 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2348 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine30a9e412019-01-14 18:36:12 +01002349 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002350 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002351 * \retval #PSA_ERROR_NOT_SUPPORTED
2352 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002353 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2354 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2355 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2356 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2357 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine30a9e412019-01-14 18:36:12 +01002358 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002359 * The operation state is not valid (it must be inactive), or the
2360 * library has not been previously initialized by psa_crypto_init().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002361 * It is implementation-dependent whether a failure to initialize
2362 * results in this error code.
2363 */
2364psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002365 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002366 psa_algorithm_t alg);
2367
2368/** Generate a random nonce for an authenticated encryption operation.
2369 *
2370 * This function generates a random nonce for the authenticated encryption
2371 * operation with an appropriate size for the chosen algorithm, key type
2372 * and key size.
2373 *
2374 * The application must call psa_aead_encrypt_setup() before
2375 * calling this function.
2376 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002377 * If this function returns an error status, the operation enters an error
2378 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002379 *
2380 * \param[in,out] operation Active AEAD operation.
2381 * \param[out] nonce Buffer where the generated nonce is to be
2382 * written.
2383 * \param nonce_size Size of the \p nonce buffer in bytes.
2384 * \param[out] nonce_length On success, the number of bytes of the
2385 * generated nonce.
2386 *
2387 * \retval #PSA_SUCCESS
2388 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002389 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2390 * The size of the \p nonce buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +01002391 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2392 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2393 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2394 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2395 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002396 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002397 * The operation state is not valid (it must be an active aead encrypt
2398 * operation, with no nonce set), or the library has not been
2399 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002400 * It is implementation-dependent whether a failure to initialize
2401 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002402 */
2403psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002404 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002405 size_t nonce_size,
2406 size_t *nonce_length);
2407
2408/** Set the nonce for an authenticated encryption or decryption operation.
2409 *
2410 * This function sets the nonce for the authenticated
2411 * encryption or decryption operation.
2412 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002413 * The application must call psa_aead_encrypt_setup() or
2414 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002415 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002416 * If this function returns an error status, the operation enters an error
2417 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002418 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002419 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002420 * instead of this function, unless implementing a protocol that requires
2421 * a non-random IV.
2422 *
2423 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002424 * \param[in] nonce Buffer containing the nonce to use.
2425 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002426 *
2427 * \retval #PSA_SUCCESS
2428 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002429 * \retval #PSA_ERROR_INVALID_ARGUMENT
2430 * The size of \p nonce is not acceptable for the chosen algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002431 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2432 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2433 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2434 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2435 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002436 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002437 * The operation state is not valid (it must be active, with no nonce
2438 * set), or the library has not been previously initialized
2439 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002440 * It is implementation-dependent whether a failure to initialize
2441 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002442 */
2443psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002444 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002445 size_t nonce_length);
2446
Gilles Peskinebc59c852019-01-17 15:26:08 +01002447/** Declare the lengths of the message and additional data for AEAD.
2448 *
2449 * The application must call this function before calling
2450 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2451 * the operation requires it. If the algorithm does not require it,
2452 * calling this function is optional, but if this function is called
2453 * then the implementation must enforce the lengths.
2454 *
2455 * You may call this function before or after setting the nonce with
2456 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2457 *
2458 * - For #PSA_ALG_CCM, calling this function is required.
2459 * - For the other AEAD algorithms defined in this specification, calling
2460 * this function is not required.
2461 * - For vendor-defined algorithm, refer to the vendor documentation.
2462 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002463 * If this function returns an error status, the operation enters an error
2464 * state and must be aborted by calling psa_aead_abort().
2465 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002466 * \param[in,out] operation Active AEAD operation.
2467 * \param ad_length Size of the non-encrypted additional
2468 * authenticated data in bytes.
2469 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2470 *
2471 * \retval #PSA_SUCCESS
2472 * Success.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002473 * \retval #PSA_ERROR_INVALID_ARGUMENT
2474 * At least one of the lengths is not acceptable for the chosen
2475 * algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01002476 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2477 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2478 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2479 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002480 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002481 * The operation state is not valid (it must be active, and
2482 * psa_aead_update_ad() and psa_aead_update() must not have been
2483 * called yet), or the library has not been previously initialized
2484 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002485 * It is implementation-dependent whether a failure to initialize
2486 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002487 */
2488psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2489 size_t ad_length,
2490 size_t plaintext_length);
2491
Gilles Peskine30a9e412019-01-14 18:36:12 +01002492/** Pass additional data to an active AEAD operation.
2493 *
2494 * Additional data is authenticated, but not encrypted.
2495 *
2496 * You may call this function multiple times to pass successive fragments
2497 * of the additional data. You may not call this function after passing
2498 * data to encrypt or decrypt with psa_aead_update().
2499 *
2500 * Before calling this function, you must:
2501 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2502 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2503 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002504 * If this function returns an error status, the operation enters an error
2505 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002506 *
2507 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2508 * there is no guarantee that the input is valid. Therefore, until
2509 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2510 * treat the input as untrusted and prepare to undo any action that
2511 * depends on the input if psa_aead_verify() returns an error status.
2512 *
2513 * \param[in,out] operation Active AEAD operation.
2514 * \param[in] input Buffer containing the fragment of
2515 * additional data.
2516 * \param input_length Size of the \p input buffer in bytes.
2517 *
2518 * \retval #PSA_SUCCESS
2519 * Success.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002520 * \retval #PSA_ERROR_INVALID_ARGUMENT
2521 * The total input length overflows the additional data length that
2522 * was previously specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002523 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2524 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2525 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2526 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2527 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002528 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002529 * The operation state is not valid (it must be active, have a nonce
2530 * set, have lengths set if required by the algorithm, and
2531 * psa_aead_update() must not have been called yet), or the library
2532 * has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002533 * It is implementation-dependent whether a failure to initialize
2534 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002535 */
2536psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2537 const uint8_t *input,
2538 size_t input_length);
2539
2540/** Encrypt or decrypt a message fragment in an active AEAD operation.
2541 *
2542 * Before calling this function, you must:
2543 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2544 * The choice of setup function determines whether this function
2545 * encrypts or decrypts its input.
2546 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2547 * 3. Call psa_aead_update_ad() to pass all the additional data.
2548 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002549 * If this function returns an error status, the operation enters an error
2550 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002551 *
2552 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2553 * there is no guarantee that the input is valid. Therefore, until
2554 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2555 * - Do not use the output in any way other than storing it in a
2556 * confidential location. If you take any action that depends
2557 * on the tentative decrypted data, this action will need to be
2558 * undone if the input turns out not to be valid. Furthermore,
2559 * if an adversary can observe that this action took place
2560 * (for example through timing), they may be able to use this
2561 * fact as an oracle to decrypt any message encrypted with the
2562 * same key.
2563 * - In particular, do not copy the output anywhere but to a
2564 * memory or storage space that you have exclusive access to.
2565 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002566 * This function does not require the input to be aligned to any
2567 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002568 * a whole block at a time, it must consume all the input provided, but
2569 * it may delay the end of the corresponding output until a subsequent
2570 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2571 * provides sufficient input. The amount of data that can be delayed
2572 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002573 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002574 * \param[in,out] operation Active AEAD operation.
2575 * \param[in] input Buffer containing the message fragment to
2576 * encrypt or decrypt.
2577 * \param input_length Size of the \p input buffer in bytes.
2578 * \param[out] output Buffer where the output is to be written.
2579 * \param output_size Size of the \p output buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002580 * This must be appropriate for the selected
2581 * algorithm and key:
2582 * - A sufficient output size is
2583 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,
2584 * \c alg, \p input_length) where
2585 * \c key_type is the type of key and \c alg is
2586 * the algorithm that were used to set up the
2587 * operation.
2588 * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p
2589 * input_length) evaluates to the maximum
2590 * output size of any supported AEAD
2591 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002592 * \param[out] output_length On success, the number of bytes
2593 * that make up the returned output.
2594 *
2595 * \retval #PSA_SUCCESS
2596 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002597 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2598 * The size of the \p output buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002599 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or
2600 * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to
2601 * determine the required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002602 * \retval #PSA_ERROR_INVALID_ARGUMENT
2603 * The total length of input to psa_aead_update_ad() so far is
2604 * less than the additional data length that was previously
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002605 * specified with psa_aead_set_lengths(), or
2606 * the total input length overflows the plaintext length that
Gilles Peskinebc59c852019-01-17 15:26:08 +01002607 * was previously specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002608 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2609 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2610 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2611 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2612 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002613 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002614 * The operation state is not valid (it must be active, have a nonce
2615 * set, and have lengths set if required by the algorithm), or the
2616 * library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002617 * It is implementation-dependent whether a failure to initialize
2618 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002619 */
2620psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2621 const uint8_t *input,
2622 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002623 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002624 size_t output_size,
2625 size_t *output_length);
2626
2627/** Finish encrypting a message in an AEAD operation.
2628 *
2629 * The operation must have been set up with psa_aead_encrypt_setup().
2630 *
2631 * This function finishes the authentication of the additional data
2632 * formed by concatenating the inputs passed to preceding calls to
2633 * psa_aead_update_ad() with the plaintext formed by concatenating the
2634 * inputs passed to preceding calls to psa_aead_update().
2635 *
2636 * This function has two output buffers:
2637 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002638 * preceding calls to psa_aead_update().
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002639 * - \p tag contains the authentication tag.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002640 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002641 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002642 * If this function returns an error status, the operation enters an error
2643 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002644 *
2645 * \param[in,out] operation Active AEAD operation.
2646 * \param[out] ciphertext Buffer where the last part of the ciphertext
2647 * is to be written.
2648 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002649 * This must be appropriate for the selected
2650 * algorithm and key:
2651 * - A sufficient output size is
2652 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type,
2653 * \c alg) where \c key_type is the type of key
2654 * and \c alg is the algorithm that were used to
2655 * set up the operation.
2656 * - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to
2657 * the maximum output size of any supported AEAD
2658 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002659 * \param[out] ciphertext_length On success, the number of bytes of
2660 * returned ciphertext.
2661 * \param[out] tag Buffer where the authentication tag is
2662 * to be written.
2663 * \param tag_size Size of the \p tag buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002664 * This must be appropriate for the selected
2665 * algorithm and key:
2666 * - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c
2667 * key_type, \c key_bits, \c alg) where
2668 * \c key_type and \c key_bits are the type and
2669 * bit-size of the key, and \c alg is the
2670 * algorithm that were used in the call to
2671 * psa_aead_encrypt_setup().
2672 * - #PSA_AEAD_TAG_MAX_SIZE evaluates to the
2673 * maximum tag size of any supported AEAD
2674 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002675 * \param[out] tag_length On success, the number of bytes
2676 * that make up the returned tag.
2677 *
2678 * \retval #PSA_SUCCESS
2679 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002680 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002681 * The size of the \p ciphertext or \p tag buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002682 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or
2683 * #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the
2684 * required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type,
2685 * \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to
2686 * determine the required \p tag buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002687 * \retval #PSA_ERROR_INVALID_ARGUMENT
2688 * The total length of input to psa_aead_update_ad() so far is
2689 * less than the additional data length that was previously
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002690 * specified with psa_aead_set_lengths(), or
2691 * the total length of input to psa_aead_update() so far is
Gilles Peskinebc59c852019-01-17 15:26:08 +01002692 * less than the plaintext length that was previously
2693 * specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002694 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2695 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2696 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2697 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2698 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002699 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002700 * The operation state is not valid (it must be an active encryption
2701 * operation with a nonce set), or the library has not been previously
2702 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002703 * It is implementation-dependent whether a failure to initialize
2704 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002705 */
2706psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002707 uint8_t *ciphertext,
2708 size_t ciphertext_size,
2709 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002710 uint8_t *tag,
2711 size_t tag_size,
2712 size_t *tag_length);
2713
2714/** Finish authenticating and decrypting a message in an AEAD operation.
2715 *
2716 * The operation must have been set up with psa_aead_decrypt_setup().
2717 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002718 * This function finishes the authenticated decryption of the message
2719 * components:
2720 *
2721 * - The additional data consisting of the concatenation of the inputs
2722 * passed to preceding calls to psa_aead_update_ad().
2723 * - The ciphertext consisting of the concatenation of the inputs passed to
2724 * preceding calls to psa_aead_update().
2725 * - The tag passed to this function call.
2726 *
2727 * If the authentication tag is correct, this function outputs any remaining
2728 * plaintext and reports success. If the authentication tag is not correct,
2729 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002730 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002731 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002732 * If this function returns an error status, the operation enters an error
2733 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002734 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002735 * \note Implementations shall make the best effort to ensure that the
2736 * comparison between the actual tag and the expected tag is performed
2737 * in constant time.
2738 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002739 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002740 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002741 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002742 * from previous calls to psa_aead_update()
2743 * that could not be processed until the end
2744 * of the input.
2745 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002746 * This must be appropriate for the selected algorithm and key:
2747 * - A sufficient output size is
2748 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type,
2749 * \c alg) where \c key_type is the type of key
2750 * and \c alg is the algorithm that were used to
2751 * set up the operation.
2752 * - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to
2753 * the maximum output size of any supported AEAD
2754 * algorithm.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002755 * \param[out] plaintext_length On success, the number of bytes of
2756 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002757 * \param[in] tag Buffer containing the authentication tag.
2758 * \param tag_length Size of the \p tag buffer in bytes.
2759 *
2760 * \retval #PSA_SUCCESS
2761 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002762 * \retval #PSA_ERROR_INVALID_SIGNATURE
2763 * The calculations were successful, but the authentication tag is
2764 * not correct.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002765 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2766 * The size of the \p plaintext buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002767 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or
2768 * #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the
2769 * required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002770 * \retval #PSA_ERROR_INVALID_ARGUMENT
2771 * The total length of input to psa_aead_update_ad() so far is
2772 * less than the additional data length that was previously
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002773 * specified with psa_aead_set_lengths(), or
2774 * the total length of input to psa_aead_update() so far is
Gilles Peskinebc59c852019-01-17 15:26:08 +01002775 * less than the plaintext length that was previously
2776 * specified with psa_aead_set_lengths().
Gilles Peskineed733552023-02-14 19:21:09 +01002777 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2778 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2779 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2780 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2781 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002782 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05002783 * The operation state is not valid (it must be an active decryption
2784 * operation with a nonce set), or the library has not been previously
2785 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002786 * It is implementation-dependent whether a failure to initialize
2787 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002788 */
2789psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002790 uint8_t *plaintext,
2791 size_t plaintext_size,
2792 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002793 const uint8_t *tag,
2794 size_t tag_length);
2795
2796/** Abort an AEAD operation.
2797 *
2798 * Aborting an operation frees all associated resources except for the
2799 * \p operation structure itself. Once aborted, the operation object
2800 * can be reused for another operation by calling
2801 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2802 *
2803 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002804 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002805 *
2806 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002807 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2808 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002809 *
2810 * \param[in,out] operation Initialized AEAD operation.
2811 *
Gilles Peskineed733552023-02-14 19:21:09 +01002812 * \retval #PSA_SUCCESS \emptydescription
2813 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2814 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2815 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002816 * \retval #PSA_ERROR_BAD_STATE
2817 * The library has not been previously initialized by psa_crypto_init().
2818 * It is implementation-dependent whether a failure to initialize
2819 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002820 */
2821psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2822
Gilles Peskine3b555712018-03-03 21:27:57 +01002823/**@}*/
2824
Gilles Peskine20035e32018-02-03 22:44:14 +01002825/** \defgroup asymmetric Asymmetric cryptography
2826 * @{
2827 */
2828
2829/**
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002830 * \brief Sign a message with a private key. For hash-and-sign algorithms,
2831 * this includes the hashing step.
2832 *
2833 * \note To perform a multi-part hash-and-sign signature algorithm, first use
2834 * a multi-part hash operation and then pass the resulting hash to
2835 * psa_sign_hash(). PSA_ALG_GET_HASH(\p alg) can be used to determine the
2836 * hash algorithm to use.
2837 *
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002838 * \param[in] key Identifier of the key to use for the operation.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002839 * It must be an asymmetric key pair. The key must
2840 * allow the usage #PSA_KEY_USAGE_SIGN_MESSAGE.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002841 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002842 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2843 * is true), that is compatible with the type of
2844 * \p key.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002845 * \param[in] input The input message to sign.
2846 * \param[in] input_length Size of the \p input buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002847 * \param[out] signature Buffer where the signature is to be written.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002848 * \param[in] signature_size Size of the \p signature buffer in bytes. This
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002849 * must be appropriate for the selected
2850 * algorithm and key:
2851 * - The required signature size is
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002852 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2853 * where \c key_type and \c key_bits are the type and
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002854 * bit-size respectively of key.
2855 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
2856 * maximum signature size of any supported
2857 * signature algorithm.
2858 * \param[out] signature_length On success, the number of bytes that make up
2859 * the returned signature value.
2860 *
Gilles Peskineed733552023-02-14 19:21:09 +01002861 * \retval #PSA_SUCCESS \emptydescription
2862 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002863 * \retval #PSA_ERROR_NOT_PERMITTED
2864 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2865 * or it does not permit the requested algorithm.
2866 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2867 * The size of the \p signature buffer is too small. You can
2868 * determine a sufficient buffer size by calling
2869 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2870 * where \c key_type and \c key_bits are the type and bit-size
2871 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01002872 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2873 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2874 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2875 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2876 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2877 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2878 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2879 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
2880 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
2881 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002882 * \retval #PSA_ERROR_BAD_STATE
2883 * The library has not been previously initialized by psa_crypto_init().
2884 * It is implementation-dependent whether a failure to initialize
2885 * results in this error code.
2886 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002887psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2888 psa_algorithm_t alg,
2889 const uint8_t *input,
2890 size_t input_length,
2891 uint8_t *signature,
2892 size_t signature_size,
2893 size_t *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002894
2895/** \brief Verify the signature of a message with a public key, using
2896 * a hash-and-sign verification algorithm.
2897 *
2898 * \note To perform a multi-part hash-and-sign signature verification
2899 * algorithm, first use a multi-part hash operation to hash the message
2900 * and then pass the resulting hash to psa_verify_hash().
2901 * PSA_ALG_GET_HASH(\p alg) can be used to determine the hash algorithm
2902 * to use.
2903 *
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002904 * \param[in] key Identifier of the key to use for the operation.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002905 * It must be a public key or an asymmetric key
2906 * pair. The key must allow the usage
2907 * #PSA_KEY_USAGE_VERIFY_MESSAGE.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002908 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002909 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2910 * is true), that is compatible with the type of
2911 * \p key.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002912 * \param[in] input The message whose signature is to be verified.
2913 * \param[in] input_length Size of the \p input buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002914 * \param[out] signature Buffer containing the signature to verify.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002915 * \param[in] signature_length Size of the \p signature buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002916 *
Gilles Peskineed733552023-02-14 19:21:09 +01002917 * \retval #PSA_SUCCESS \emptydescription
2918 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002919 * \retval #PSA_ERROR_NOT_PERMITTED
2920 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2921 * or it does not permit the requested algorithm.
2922 * \retval #PSA_ERROR_INVALID_SIGNATURE
2923 * The calculation was performed successfully, but the passed signature
2924 * is not a valid signature.
Gilles Peskineed733552023-02-14 19:21:09 +01002925 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2926 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2927 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2928 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2929 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2930 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2931 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2932 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
2933 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002934 * \retval #PSA_ERROR_BAD_STATE
2935 * The library has not been previously initialized by psa_crypto_init().
2936 * It is implementation-dependent whether a failure to initialize
2937 * results in this error code.
2938 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002939psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2940 psa_algorithm_t alg,
2941 const uint8_t *input,
2942 size_t input_length,
2943 const uint8_t *signature,
2944 size_t signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002945
2946/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002947 * \brief Sign a hash or short message with a private key.
2948 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002949 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002950 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01002951 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
2952 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02002953 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2954 * to determine the hash algorithm to use.
2955 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002956 * \param key Identifier of the key to use for the operation.
2957 * It must be an asymmetric key pair. The key must
Ronald Cron96783552020-10-19 12:06:30 +02002958 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
Mateusz Starzyke6d3eda2021-08-26 11:46:14 +02002959 * \param alg A signature algorithm (PSA_ALG_XXX
2960 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
2961 * is true), that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002962 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002963 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002964 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002965 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002966 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002967 * \param[out] signature_length On success, the number of bytes
2968 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002969 *
Gilles Peskineed733552023-02-14 19:21:09 +01002970 * \retval #PSA_SUCCESS \emptydescription
2971 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
2972 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02002973 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002974 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002975 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002976 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002977 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002978 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01002979 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
2980 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
2981 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
2982 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
2983 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
2984 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
2985 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
2986 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03002987 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002988 * The library has not been previously initialized by psa_crypto_init().
2989 * It is implementation-dependent whether a failure to initialize
2990 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002991 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002992psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002993 psa_algorithm_t alg,
2994 const uint8_t *hash,
2995 size_t hash_length,
2996 uint8_t *signature,
2997 size_t signature_size,
2998 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002999
3000/**
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003001 * \brief Verify the signature of a hash or short message using a public key.
Gilles Peskine20035e32018-02-03 22:44:14 +01003002 *
Gilles Peskine08bac712018-06-26 16:14:46 +02003003 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02003004 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01003005 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
3006 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02003007 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
3008 * to determine the hash algorithm to use.
3009 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003010 * \param key Identifier of the key to use for the operation. It
3011 * must be a public key or an asymmetric key pair. The
Ronald Cron96783552020-10-19 12:06:30 +02003012 * key must allow the usage
3013 * #PSA_KEY_USAGE_VERIFY_HASH.
Mateusz Starzyke6d3eda2021-08-26 11:46:14 +02003014 * \param alg A signature algorithm (PSA_ALG_XXX
3015 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
3016 * is true), that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02003017 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003018 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02003019 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003020 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003021 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003022 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003023 *
Gilles Peskine28538492018-07-11 17:34:00 +02003024 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01003025 * The signature is valid.
Gilles Peskineed733552023-02-14 19:21:09 +01003026 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3027 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02003028 * \retval #PSA_ERROR_INVALID_SIGNATURE
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003029 * The calculation was performed successfully, but the passed
Gilles Peskine308b91d2018-02-08 09:47:44 +01003030 * signature is not a valid signature.
Gilles Peskineed733552023-02-14 19:21:09 +01003031 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3032 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3033 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3034 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3035 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3036 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3037 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003038 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003039 * The library has not been previously initialized by psa_crypto_init().
3040 * It is implementation-dependent whether a failure to initialize
3041 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01003042 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003043psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003044 psa_algorithm_t alg,
3045 const uint8_t *hash,
3046 size_t hash_length,
3047 const uint8_t *signature,
3048 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003049
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003050/**
3051 * \brief Encrypt a short message with a public key.
3052 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003053 * \param key Identifier of the key to use for the operation.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003054 * It must be a public key or an asymmetric key
3055 * pair. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02003056 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003057 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003058 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003059 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003060 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003061 * \param[in] salt A salt or label, if supported by the
3062 * encryption algorithm.
3063 * If the algorithm does not support a
3064 * salt, pass \c NULL.
3065 * If the algorithm supports an optional
3066 * salt and you do not want to pass a salt,
3067 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003068 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003069 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3070 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003071 * \param salt_length Size of the \p salt buffer in bytes.
3072 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003073 * \param[out] output Buffer where the encrypted message is to
3074 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003075 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003076 * \param[out] output_length On success, the number of bytes
3077 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003078 *
Gilles Peskineed733552023-02-14 19:21:09 +01003079 * \retval #PSA_SUCCESS \emptydescription
3080 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3081 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02003082 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003083 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003084 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02003085 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003086 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003087 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01003088 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3089 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3090 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3091 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3092 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3093 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3094 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3095 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003096 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003097 * The library has not been previously initialized by psa_crypto_init().
3098 * It is implementation-dependent whether a failure to initialize
3099 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003100 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003101psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003102 psa_algorithm_t alg,
3103 const uint8_t *input,
3104 size_t input_length,
3105 const uint8_t *salt,
3106 size_t salt_length,
3107 uint8_t *output,
3108 size_t output_size,
3109 size_t *output_length);
3110
3111/**
3112 * \brief Decrypt a short message with a private key.
3113 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003114 * \param key Identifier of the key to use for the operation.
3115 * It must be an asymmetric key pair. It must
Ronald Cron96783552020-10-19 12:06:30 +02003116 * allow the usage #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003117 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003118 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003119 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003120 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003121 * \param[in] salt A salt or label, if supported by the
3122 * encryption algorithm.
3123 * If the algorithm does not support a
3124 * salt, pass \c NULL.
3125 * If the algorithm supports an optional
3126 * salt and you do not want to pass a salt,
3127 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003128 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003129 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3130 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003131 * \param salt_length Size of the \p salt buffer in bytes.
3132 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003133 * \param[out] output Buffer where the decrypted message is to
3134 * be written.
3135 * \param output_size Size of the \c output buffer in bytes.
3136 * \param[out] output_length On success, the number of bytes
3137 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003138 *
Gilles Peskineed733552023-02-14 19:21:09 +01003139 * \retval #PSA_SUCCESS \emptydescription
3140 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3141 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine28538492018-07-11 17:34:00 +02003142 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003143 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003144 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003145 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003146 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003147 * respectively of \p key.
Gilles Peskineed733552023-02-14 19:21:09 +01003148 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3149 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
3150 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3151 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3152 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3153 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3154 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
3155 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3156 * \retval #PSA_ERROR_INVALID_PADDING \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003157 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003158 * The library has not been previously initialized by psa_crypto_init().
3159 * It is implementation-dependent whether a failure to initialize
3160 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003161 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003162psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003163 psa_algorithm_t alg,
3164 const uint8_t *input,
3165 size_t input_length,
3166 const uint8_t *salt,
3167 size_t salt_length,
3168 uint8_t *output,
3169 size_t output_size,
3170 size_t *output_length);
3171
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003172/**@}*/
3173
Gilles Peskine35675b62019-05-16 17:26:11 +02003174/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003175 * @{
3176 */
3177
Gilles Peskine35675b62019-05-16 17:26:11 +02003178/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003179 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003180 * Before calling any function on a key derivation operation object, the
3181 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003182 * - Set the structure to all-bits-zero, for example:
3183 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003184 * psa_key_derivation_operation_t operation;
3185 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003186 * \endcode
3187 * - Initialize the structure to logical zero values, for example:
3188 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003189 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003190 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003191 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003192 * for example:
3193 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003194 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003195 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003196 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003197 * to the structure, for example:
3198 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003199 * psa_key_derivation_operation_t operation;
3200 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003201 * \endcode
3202 *
3203 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01003204 * make any assumptions about the content of this structure.
3205 * Implementation details can change in future versions without notice.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003206 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003207typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003208
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003209/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003210 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003211 * This macro returns a suitable initializer for a key derivation operation
3212 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003213 */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003214
Gilles Peskine35675b62019-05-16 17:26:11 +02003215/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003216 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003217static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003218
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003219/** Set up a key derivation operation.
3220 *
3221 * A key derivation algorithm takes some inputs and uses them to generate
3222 * a byte stream in a deterministic way.
3223 * This byte stream can be used to produce keys and other
3224 * cryptographic material.
3225 *
3226 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003227 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3228 * -# Call psa_key_derivation_setup() to select the algorithm.
3229 * -# Provide the inputs for the key derivation by calling
3230 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3231 * as appropriate. Which inputs are needed, in what order, and whether
3232 * they may be keys and if so of what type depends on the algorithm.
3233 * -# Optionally set the operation's maximum capacity with
3234 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3235 * of or after providing inputs. For some algorithms, this step is mandatory
3236 * because the output depends on the maximum capacity.
3237 * -# To derive a key, call psa_key_derivation_output_key().
3238 * To derive a byte string for a different purpose, call
3239 * psa_key_derivation_output_bytes().
3240 * Successive calls to these functions use successive output bytes
3241 * calculated by the key derivation algorithm.
3242 * -# Clean up the key derivation operation object with
3243 * psa_key_derivation_abort().
3244 *
3245 * If this function returns an error, the key derivation operation object is
3246 * not changed.
3247 *
3248 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3249 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003250 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003251 * Implementations must reject an attempt to derive a key of size 0.
3252 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003253 * \param[in,out] operation The key derivation operation object
3254 * to set up. It must
3255 * have been initialized but not set up yet.
3256 * \param alg The key derivation algorithm to compute
3257 * (\c PSA_ALG_XXX value such that
3258 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3259 *
3260 * \retval #PSA_SUCCESS
3261 * Success.
3262 * \retval #PSA_ERROR_INVALID_ARGUMENT
3263 * \c alg is not a key derivation algorithm.
3264 * \retval #PSA_ERROR_NOT_SUPPORTED
3265 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01003266 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3267 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3268 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3269 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3270 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003271 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003272 * The operation state is not valid (it must be inactive), or
3273 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003274 * It is implementation-dependent whether a failure to initialize
3275 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003276 */
3277psa_status_t psa_key_derivation_setup(
3278 psa_key_derivation_operation_t *operation,
3279 psa_algorithm_t alg);
3280
Gilles Peskine35675b62019-05-16 17:26:11 +02003281/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003282 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003283 * The capacity of a key derivation is the maximum number of bytes that it can
3284 * return. When you get *N* bytes of output from a key derivation operation,
3285 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003286 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003287 * \param[in] operation The operation to query.
3288 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003289 *
Gilles Peskineed733552023-02-14 19:21:09 +01003290 * \retval #PSA_SUCCESS \emptydescription
3291 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3292 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3293 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003294 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003295 * The operation state is not valid (it must be active), or
3296 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003297 * It is implementation-dependent whether a failure to initialize
3298 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003299 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003300psa_status_t psa_key_derivation_get_capacity(
3301 const psa_key_derivation_operation_t *operation,
3302 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003303
Gilles Peskine35675b62019-05-16 17:26:11 +02003304/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003305 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003306 * The capacity of a key derivation operation is the maximum number of bytes
3307 * that the key derivation operation can return from this point onwards.
3308 *
3309 * \param[in,out] operation The key derivation operation object to modify.
3310 * \param capacity The new capacity of the operation.
3311 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003312 * current capacity.
3313 *
Gilles Peskineed733552023-02-14 19:21:09 +01003314 * \retval #PSA_SUCCESS \emptydescription
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003315 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003316 * \p capacity is larger than the operation's current capacity.
3317 * In this case, the operation object remains valid and its capacity
3318 * remains unchanged.
Gilles Peskineed733552023-02-14 19:21:09 +01003319 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3320 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3321 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003322 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003323 * The operation state is not valid (it must be active), or the
3324 * library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003325 * It is implementation-dependent whether a failure to initialize
3326 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003327 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003328psa_status_t psa_key_derivation_set_capacity(
3329 psa_key_derivation_operation_t *operation,
3330 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003331
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003332/** Use the maximum possible capacity for a key derivation operation.
3333 *
3334 * Use this value as the capacity argument when setting up a key derivation
3335 * to indicate that the operation should have the maximum possible capacity.
3336 * The value of the maximum possible capacity depends on the key derivation
3337 * algorithm.
3338 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003339#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t) (-1))
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003340
3341/** Provide an input for key derivation or key agreement.
3342 *
3343 * Which inputs are required and in what order depends on the algorithm.
3344 * Refer to the documentation of each key derivation or key agreement
3345 * algorithm for information.
3346 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003347 * This function passes direct inputs, which is usually correct for
3348 * non-secret inputs. To pass a secret input, which should be in a key
3349 * object, call psa_key_derivation_input_key() instead of this function.
3350 * Refer to the documentation of individual step types
3351 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3352 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003353 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003354 * If this function returns an error status, the operation enters an error
3355 * state and must be aborted by calling psa_key_derivation_abort().
3356 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003357 * \param[in,out] operation The key derivation operation object to use.
3358 * It must have been set up with
3359 * psa_key_derivation_setup() and must not
3360 * have produced any output yet.
3361 * \param step Which step the input data is for.
3362 * \param[in] data Input data to use.
3363 * \param data_length Size of the \p data buffer in bytes.
3364 *
3365 * \retval #PSA_SUCCESS
3366 * Success.
3367 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003368 * \c step is not compatible with the operation's algorithm, or
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003369 * \c step does not allow direct inputs.
Gilles Peskineed733552023-02-14 19:21:09 +01003370 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3371 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3372 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3373 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3374 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003375 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003376 * The operation state is not valid for this input \p step, or
3377 * the library has not been previously initialized by psa_crypto_init().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003378 * It is implementation-dependent whether a failure to initialize
3379 * results in this error code.
3380 */
3381psa_status_t psa_key_derivation_input_bytes(
3382 psa_key_derivation_operation_t *operation,
3383 psa_key_derivation_step_t step,
3384 const uint8_t *data,
3385 size_t data_length);
3386
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003387/** Provide a numeric input for key derivation or key agreement.
3388 *
3389 * Which inputs are required and in what order depends on the algorithm.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003390 * However, when an algorithm requires a particular order, numeric inputs
3391 * usually come first as they tend to be configuration parameters.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003392 * Refer to the documentation of each key derivation or key agreement
3393 * algorithm for information.
3394 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003395 * This function is used for inputs which are fixed-size non-negative
3396 * integers.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003397 *
3398 * If this function returns an error status, the operation enters an error
3399 * state and must be aborted by calling psa_key_derivation_abort().
3400 *
3401 * \param[in,out] operation The key derivation operation object to use.
3402 * It must have been set up with
3403 * psa_key_derivation_setup() and must not
3404 * have produced any output yet.
3405 * \param step Which step the input data is for.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003406 * \param[in] value The value of the numeric input.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003407 *
3408 * \retval #PSA_SUCCESS
3409 * Success.
3410 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003411 * \c step is not compatible with the operation's algorithm, or
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003412 * \c step does not allow numeric inputs.
Gilles Peskineed733552023-02-14 19:21:09 +01003413 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3414 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3415 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3416 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3417 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003418 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003419 * The operation state is not valid for this input \p step, or
3420 * the library has not been previously initialized by psa_crypto_init().
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003421 * It is implementation-dependent whether a failure to initialize
3422 * results in this error code.
3423 */
3424psa_status_t psa_key_derivation_input_integer(
3425 psa_key_derivation_operation_t *operation,
3426 psa_key_derivation_step_t step,
3427 uint64_t value);
3428
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003429/** Provide an input for key derivation in the form of a key.
3430 *
3431 * Which inputs are required and in what order depends on the algorithm.
3432 * Refer to the documentation of each key derivation or key agreement
3433 * algorithm for information.
3434 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003435 * This function obtains input from a key object, which is usually correct for
3436 * secret inputs or for non-secret personalization strings kept in the key
3437 * store. To pass a non-secret parameter which is not in the key store,
3438 * call psa_key_derivation_input_bytes() instead of this function.
3439 * Refer to the documentation of individual step types
3440 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3441 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003442 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003443 * If this function returns an error status, the operation enters an error
3444 * state and must be aborted by calling psa_key_derivation_abort().
3445 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003446 * \param[in,out] operation The key derivation operation object to use.
3447 * It must have been set up with
3448 * psa_key_derivation_setup() and must not
3449 * have produced any output yet.
3450 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003451 * \param key Identifier of the key. It must have an
3452 * appropriate type for step and must allow the
Manuel Pégourié-Gonnardacfde462021-05-05 09:54:22 +02003453 * usage #PSA_KEY_USAGE_DERIVE or
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003454 * #PSA_KEY_USAGE_VERIFY_DERIVATION (see note)
3455 * and the algorithm used by the operation.
3456 *
3457 * \note Once all inputs steps are completed, the operations will allow:
3458 * - psa_key_derivation_output_bytes() if each input was either a direct input
3459 * or a key with #PSA_KEY_USAGE_DERIVE set;
Manuel Pégourié-Gonnarde88511d2021-05-07 12:19:03 +02003460 * - psa_key_derivation_output_key() if the input for step
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003461 * #PSA_KEY_DERIVATION_INPUT_SECRET or #PSA_KEY_DERIVATION_INPUT_PASSWORD
Manuel Pégourié-Gonnarde88511d2021-05-07 12:19:03 +02003462 * was from a key slot with #PSA_KEY_USAGE_DERIVE and each other input was
3463 * either a direct input or a key with #PSA_KEY_USAGE_DERIVE set;
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003464 * - psa_key_derivation_verify_bytes() if each input was either a direct input
3465 * or a key with #PSA_KEY_USAGE_VERIFY_DERIVATION set;
Manuel Pégourié-Gonnard4feb6112021-05-07 12:22:21 +02003466 * - psa_key_derivation_verify_key() under the same conditions as
3467 * psa_key_derivation_verify_bytes().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003468 *
3469 * \retval #PSA_SUCCESS
3470 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01003471 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003472 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003473 * The key allows neither #PSA_KEY_USAGE_DERIVE nor
3474 * #PSA_KEY_USAGE_VERIFY_DERIVATION, or it doesn't allow this
3475 * algorithm.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003476 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003477 * \c step is not compatible with the operation's algorithm, or
Gilles Peskine224b0d62019-09-23 18:13:17 +02003478 * \c step does not allow key inputs of the given type
3479 * or does not allow key inputs at all.
Gilles Peskineed733552023-02-14 19:21:09 +01003480 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3481 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3482 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3483 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3484 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003485 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003486 * The operation state is not valid for this input \p step, or
3487 * the library has not been previously initialized by psa_crypto_init().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003488 * It is implementation-dependent whether a failure to initialize
3489 * results in this error code.
3490 */
3491psa_status_t psa_key_derivation_input_key(
3492 psa_key_derivation_operation_t *operation,
3493 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003494 mbedtls_svc_key_id_t key);
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003495
3496/** Perform a key agreement and use the shared secret as input to a key
3497 * derivation.
3498 *
3499 * A key agreement algorithm takes two inputs: a private key \p private_key
3500 * a public key \p peer_key.
3501 * The result of this function is passed as input to a key derivation.
3502 * The output of this key derivation can be extracted by reading from the
3503 * resulting operation to produce keys and other cryptographic material.
3504 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003505 * If this function returns an error status, the operation enters an error
3506 * state and must be aborted by calling psa_key_derivation_abort().
3507 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003508 * \param[in,out] operation The key derivation operation object to use.
3509 * It must have been set up with
3510 * psa_key_derivation_setup() with a
3511 * key agreement and derivation algorithm
3512 * \c alg (\c PSA_ALG_XXX value such that
3513 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3514 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3515 * is false).
3516 * The operation must be ready for an
3517 * input of the type given by \p step.
3518 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003519 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003520 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003521 * \param[in] peer_key Public key of the peer. The peer key must be in the
3522 * same format that psa_import_key() accepts for the
3523 * public key type corresponding to the type of
3524 * private_key. That is, this function performs the
3525 * equivalent of
3526 * #psa_import_key(...,
3527 * `peer_key`, `peer_key_length`) where
3528 * with key attributes indicating the public key
3529 * type corresponding to the type of `private_key`.
3530 * For example, for EC keys, this means that peer_key
3531 * is interpreted as a point on the curve that the
3532 * private key is on. The standard formats for public
3533 * keys are documented in the documentation of
3534 * psa_export_public_key().
3535 * \param peer_key_length Size of \p peer_key in bytes.
3536 *
3537 * \retval #PSA_SUCCESS
3538 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01003539 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3540 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003541 * \retval #PSA_ERROR_INVALID_ARGUMENT
3542 * \c private_key is not compatible with \c alg,
3543 * or \p peer_key is not valid for \c alg or not compatible with
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003544 * \c private_key, or \c step does not allow an input resulting
3545 * from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003546 * \retval #PSA_ERROR_NOT_SUPPORTED
3547 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01003548 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3549 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3550 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3551 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3552 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003553 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003554 * The operation state is not valid for this key agreement \p step,
3555 * or the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003556 * It is implementation-dependent whether a failure to initialize
3557 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003558 */
3559psa_status_t psa_key_derivation_key_agreement(
3560 psa_key_derivation_operation_t *operation,
3561 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003562 mbedtls_svc_key_id_t private_key,
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003563 const uint8_t *peer_key,
3564 size_t peer_key_length);
3565
Gilles Peskine35675b62019-05-16 17:26:11 +02003566/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003567 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003568 * This function calculates output bytes from a key derivation algorithm and
3569 * return those bytes.
3570 * If you view the key derivation's output as a stream of bytes, this
3571 * function destructively reads the requested number of bytes from the
3572 * stream.
3573 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003574 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003575 * If this function returns an error status other than
3576 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003577 * state and must be aborted by calling psa_key_derivation_abort().
3578 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003579 * \param[in,out] operation The key derivation operation object to read from.
3580 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003581 * \param output_length Number of bytes to output.
3582 *
Gilles Peskineed733552023-02-14 19:21:09 +01003583 * \retval #PSA_SUCCESS \emptydescription
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003584 * \retval #PSA_ERROR_NOT_PERMITTED
3585 * One of the inputs was a key whose policy didn't allow
3586 * #PSA_KEY_USAGE_DERIVE.
David Saadab4ecc272019-02-14 13:48:10 +02003587 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003588 * The operation's capacity was less than
3589 * \p output_length bytes. Note that in this case,
3590 * no output is written to the output buffer.
3591 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003592 * subsequent calls to this function will not
3593 * succeed, even with a smaller output buffer.
Gilles Peskineed733552023-02-14 19:21:09 +01003594 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3595 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3596 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3597 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3598 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003599 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003600 * The operation state is not valid (it must be active and completed
3601 * all required input steps), or the library has not been previously
3602 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003603 * It is implementation-dependent whether a failure to initialize
3604 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003605 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003606psa_status_t psa_key_derivation_output_bytes(
3607 psa_key_derivation_operation_t *operation,
3608 uint8_t *output,
3609 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003610
Gilles Peskine35675b62019-05-16 17:26:11 +02003611/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003612 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003613 * This function calculates output bytes from a key derivation algorithm
3614 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003615 * The key's location, usage policy, type and size are taken from
3616 * \p attributes.
3617 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003618 * If you view the key derivation's output as a stream of bytes, this
3619 * function destructively reads as many bytes as required from the
3620 * stream.
3621 * The operation's capacity decreases by the number of bytes read.
3622 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003623 * If this function returns an error status other than
3624 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003625 * state and must be aborted by calling psa_key_derivation_abort().
3626 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003627 * How much output is produced and consumed from the operation, and how
Gilles Peskine364d12c2021-03-08 17:23:47 +01003628 * the key is derived, depends on the key type and on the key size
3629 * (denoted \c bits below):
Gilles Peskineeab56e42018-07-12 17:12:33 +02003630 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003631 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003632 * of a given size, this function is functionally equivalent to
3633 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003634 * and passing the resulting output to #psa_import_key.
3635 * However, this function has a security benefit:
3636 * if the implementation provides an isolation boundary then
3637 * the key material is not exposed outside the isolation boundary.
3638 * As a consequence, for these key types, this function always consumes
Gilles Peskine364d12c2021-03-08 17:23:47 +01003639 * exactly (\c bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003640 * The following key types defined in this specification follow this scheme:
3641 *
3642 * - #PSA_KEY_TYPE_AES;
Gilles Peskine6c12a1e2021-09-21 11:59:39 +02003643 * - #PSA_KEY_TYPE_ARIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003644 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003645 * - #PSA_KEY_TYPE_DERIVE;
Manuel Pégourié-Gonnard2171e422021-05-03 10:49:54 +02003646 * - #PSA_KEY_TYPE_HMAC;
3647 * - #PSA_KEY_TYPE_PASSWORD_HASH.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003648 *
3649 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003650 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003651 * Montgomery curve), this function always draws a byte string whose
3652 * length is determined by the curve, and sets the mandatory bits
3653 * accordingly. That is:
3654 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01003655 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003656 * string and process it as specified in RFC 7748 &sect;5.
Paul Elliott8ff510a2020-06-02 17:19:28 +01003657 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003658 * string and process it as specified in RFC 7748 &sect;5.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003659 *
3660 * - For key types for which the key is represented by a single sequence of
Gilles Peskine364d12c2021-03-08 17:23:47 +01003661 * \c bits bits with constraints as to which bit sequences are acceptable,
3662 * this function draws a byte string of length (\c bits / 8) bytes rounded
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003663 * up to the nearest whole number of bytes. If the resulting byte string
3664 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3665 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003666 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003667 * for the output produced by psa_export_key().
3668 * The following key types defined in this specification follow this scheme:
3669 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003670 * - #PSA_KEY_TYPE_DES.
3671 * Force-set the parity bits, but discard forbidden weak keys.
3672 * For 2-key and 3-key triple-DES, the three keys are generated
3673 * successively (for example, for 3-key triple-DES,
3674 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3675 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003676 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003677 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003678 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003679 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003680 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003681 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003682 * Weierstrass curve).
3683 * For these key types, interpret the byte string as integer
3684 * in big-endian order. Discard it if it is not in the range
3685 * [0, *N* - 2] where *N* is the boundary of the private key domain
3686 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003687 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003688 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003689 * This method allows compliance to NIST standards, specifically
3690 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003691 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3692 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3693 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3694 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003695 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003696 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003697 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003698 * implementation-defined.
3699 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003700 * In all cases, the data that is read is discarded from the operation.
3701 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003702 *
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003703 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3704 * the input to that step must be provided with psa_key_derivation_input_key().
3705 * Future versions of this specification may include additional restrictions
3706 * on the derived key based on the attributes and strength of the secret key.
3707 *
Gilles Peskine20628592019-04-19 19:29:50 +02003708 * \param[in] attributes The attributes for the new key.
Manuel Pégourié-Gonnard2c44daf2021-05-10 12:53:30 +02003709 * If the key type to be created is
3710 * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
3711 * the policy must be the same as in the current
3712 * operation.
Gilles Peskine35675b62019-05-16 17:26:11 +02003713 * \param[in,out] operation The key derivation operation object to read from.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003714 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003715 * key. For persistent keys, this is the key
3716 * identifier defined in \p attributes.
3717 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003718 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003719 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003720 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003721 * If the key is persistent, the key material and the key's metadata
3722 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003723 * \retval #PSA_ERROR_ALREADY_EXISTS
3724 * This is an attempt to create a persistent key, and there is
3725 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003726 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003727 * There was not enough data to create the desired key.
3728 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003729 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003730 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003731 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003732 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003733 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003734 * \retval #PSA_ERROR_INVALID_ARGUMENT
3735 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003736 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003737 * The #PSA_KEY_DERIVATION_INPUT_SECRET or
3738 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
3739 * key; or one of the inputs was a key whose policy didn't allow
3740 * #PSA_KEY_USAGE_DERIVE.
Gilles Peskineed733552023-02-14 19:21:09 +01003741 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3742 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
3743 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3744 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3745 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3746 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
3747 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
3748 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03003749 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003750 * The operation state is not valid (it must be active and completed
3751 * all required input steps), or the library has not been previously
3752 * initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03003753 * It is implementation-dependent whether a failure to initialize
3754 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003755 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003756psa_status_t psa_key_derivation_output_key(
3757 const psa_key_attributes_t *attributes,
3758 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003759 mbedtls_svc_key_id_t *key);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003760
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003761/** Compare output data from a key derivation operation to an expected value.
3762 *
3763 * This function calculates output bytes from a key derivation algorithm and
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003764 * compares those bytes to an expected value in constant time.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003765 * If you view the key derivation's output as a stream of bytes, this
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003766 * function destructively reads the expected number of bytes from the
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003767 * stream before comparing them.
3768 * The operation's capacity decreases by the number of bytes read.
3769 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003770 * This is functionally equivalent to the following code:
3771 * \code
3772 * psa_key_derivation_output_bytes(operation, tmp, output_length);
3773 * if (memcmp(output, tmp, output_length) != 0)
3774 * return PSA_ERROR_INVALID_SIGNATURE;
3775 * \endcode
3776 * except (1) it works even if the key's policy does not allow outputting the
3777 * bytes, and (2) the comparison will be done in constant time.
3778 *
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003779 * If this function returns an error status other than
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003780 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3781 * the operation enters an error state and must be aborted by calling
3782 * psa_key_derivation_abort().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003783 *
3784 * \param[in,out] operation The key derivation operation object to read from.
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003785 * \param[in] expected_output Buffer containing the expected derivation output.
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003786 * \param output_length Length of the expected output; this is also the
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003787 * number of bytes that will be read.
3788 *
Gilles Peskineed733552023-02-14 19:21:09 +01003789 * \retval #PSA_SUCCESS \emptydescription
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003790 * \retval #PSA_ERROR_INVALID_SIGNATURE
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003791 * The output was read successfully, but it differs from the expected
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003792 * output.
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003793 * \retval #PSA_ERROR_NOT_PERMITTED
3794 * One of the inputs was a key whose policy didn't allow
3795 * #PSA_KEY_USAGE_VERIFY_DERIVATION.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003796 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3797 * The operation's capacity was less than
3798 * \p output_length bytes. Note that in this case,
3799 * the operation's capacity is set to 0, thus
3800 * subsequent calls to this function will not
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003801 * succeed, even with a smaller expected output.
Gilles Peskineed733552023-02-14 19:21:09 +01003802 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3803 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3804 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3805 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3806 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003807 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003808 * The operation state is not valid (it must be active and completed
3809 * all required input steps), or the library has not been previously
3810 * initialized by psa_crypto_init().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003811 * It is implementation-dependent whether a failure to initialize
3812 * results in this error code.
3813 */
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003814psa_status_t psa_key_derivation_verify_bytes(
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003815 psa_key_derivation_operation_t *operation,
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003816 const uint8_t *expected_output,
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003817 size_t output_length);
3818
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003819/** Compare output data from a key derivation operation to an expected value
3820 * stored in a key object.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003821 *
3822 * This function calculates output bytes from a key derivation algorithm and
3823 * compares those bytes to an expected value, provided as key of type
Manuel Pégourié-Gonnard4e02f012021-05-12 10:05:45 +02003824 * #PSA_KEY_TYPE_PASSWORD_HASH.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003825 * If you view the key derivation's output as a stream of bytes, this
Tom Cosgrove1797b052022-12-04 17:19:59 +00003826 * function destructively reads the number of bytes corresponding to the
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003827 * length of the expected value from the stream before comparing them.
3828 * The operation's capacity decreases by the number of bytes read.
3829 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003830 * This is functionally equivalent to exporting the key and calling
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003831 * psa_key_derivation_verify_bytes() on the result, except that it
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003832 * works even if the key cannot be exported.
3833 *
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003834 * If this function returns an error status other than
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003835 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3836 * the operation enters an error state and must be aborted by calling
3837 * psa_key_derivation_abort().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003838 *
3839 * \param[in,out] operation The key derivation operation object to read from.
Manuel Pégourié-Gonnard2171e422021-05-03 10:49:54 +02003840 * \param[in] expected A key of type #PSA_KEY_TYPE_PASSWORD_HASH
3841 * containing the expected output. Its policy must
3842 * include the #PSA_KEY_USAGE_VERIFY_DERIVATION flag
3843 * and the permitted algorithm must match the
3844 * operation. The value of this key was likely
3845 * computed by a previous call to
3846 * psa_key_derivation_output_key().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003847 *
Gilles Peskineed733552023-02-14 19:21:09 +01003848 * \retval #PSA_SUCCESS \emptydescription
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003849 * \retval #PSA_ERROR_INVALID_SIGNATURE
3850 * The output was read successfully, but if differs from the expected
3851 * output.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003852 * \retval #PSA_ERROR_INVALID_HANDLE
3853 * The key passed as the expected value does not exist.
3854 * \retval #PSA_ERROR_INVALID_ARGUMENT
3855 * The key passed as the expected value has an invalid type.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003856 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnard88658be2021-05-03 10:28:57 +02003857 * The key passed as the expected value does not allow this usage or
Manuel Pégourié-Gonnard4feb6112021-05-07 12:22:21 +02003858 * this algorithm; or one of the inputs was a key whose policy didn't
3859 * allow #PSA_KEY_USAGE_VERIFY_DERIVATION.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003860 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3861 * The operation's capacity was less than
3862 * the length of the expected value. In this case,
3863 * the operation's capacity is set to 0, thus
3864 * subsequent calls to this function will not
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003865 * succeed, even with a smaller expected output.
Gilles Peskineed733552023-02-14 19:21:09 +01003866 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3867 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3868 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3869 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3870 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003871 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003872 * The operation state is not valid (it must be active and completed
3873 * all required input steps), or the library has not been previously
3874 * initialized by psa_crypto_init().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003875 * It is implementation-dependent whether a failure to initialize
3876 * results in this error code.
3877 */
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003878psa_status_t psa_key_derivation_verify_key(
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003879 psa_key_derivation_operation_t *operation,
3880 psa_key_id_t expected);
3881
Gilles Peskine35675b62019-05-16 17:26:11 +02003882/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003883 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003884 * Aborting an operation frees all associated resources except for the \c
3885 * operation structure itself. Once aborted, the operation object can be reused
3886 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003887 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003888 * This function may be called at any time after the operation
3889 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003890 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003891 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3892 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003893 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003894 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003895 *
Gilles Peskineed733552023-02-14 19:21:09 +01003896 * \retval #PSA_SUCCESS \emptydescription
3897 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3898 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3899 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003900 * \retval #PSA_ERROR_BAD_STATE
3901 * The library has not been previously initialized by psa_crypto_init().
3902 * It is implementation-dependent whether a failure to initialize
3903 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003904 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003905psa_status_t psa_key_derivation_abort(
3906 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003907
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003908/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003909 *
3910 * \warning The raw result of a key agreement algorithm such as finite-field
3911 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3912 * not be used directly as key material. It should instead be passed as
3913 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003914 * a key derivation, use psa_key_derivation_key_agreement() and other
3915 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003916 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003917 * \param alg The key agreement algorithm to compute
3918 * (\c PSA_ALG_XXX value such that
3919 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3920 * is true).
Ronald Croncf56a0a2020-08-04 09:51:30 +02003921 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003922 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003923 * \param[in] peer_key Public key of the peer. It must be
3924 * in the same format that psa_import_key()
3925 * accepts. The standard formats for public
3926 * keys are documented in the documentation
3927 * of psa_export_public_key().
3928 * \param peer_key_length Size of \p peer_key in bytes.
3929 * \param[out] output Buffer where the decrypted message is to
3930 * be written.
3931 * \param output_size Size of the \c output buffer in bytes.
3932 * \param[out] output_length On success, the number of bytes
3933 * that make up the returned output.
3934 *
3935 * \retval #PSA_SUCCESS
3936 * Success.
Gilles Peskineed733552023-02-14 19:21:09 +01003937 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
3938 * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
Gilles Peskine769c7a62019-01-18 16:42:29 +01003939 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekf7c1f742022-02-03 11:30:54 -05003940 * \p alg is not a key agreement algorithm, or
Gilles Peskine769c7a62019-01-18 16:42:29 +01003941 * \p private_key is not compatible with \p alg,
3942 * or \p peer_key is not valid for \p alg or not compatible with
3943 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003944 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3945 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003946 * \retval #PSA_ERROR_NOT_SUPPORTED
3947 * \p alg is not a supported key agreement algorithm.
Gilles Peskineed733552023-02-14 19:21:09 +01003948 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3949 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3950 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3951 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
3952 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003953 * \retval #PSA_ERROR_BAD_STATE
3954 * The library has not been previously initialized by psa_crypto_init().
3955 * It is implementation-dependent whether a failure to initialize
3956 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003957 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003958psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003959 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02003960 const uint8_t *peer_key,
3961 size_t peer_key_length,
3962 uint8_t *output,
3963 size_t output_size,
3964 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003965
Gilles Peskineea0fb492018-07-12 17:17:20 +02003966/**@}*/
3967
Gilles Peskineedd76872018-07-20 17:42:05 +02003968/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003969 * @{
3970 */
3971
3972/**
3973 * \brief Generate random bytes.
3974 *
3975 * \warning This function **can** fail! Callers MUST check the return status
3976 * and MUST NOT use the content of the output buffer if the return
3977 * status is not #PSA_SUCCESS.
3978 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003979 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003980 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003981 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003982 * \param output_size Number of bytes to generate and output.
3983 *
Gilles Peskineed733552023-02-14 19:21:09 +01003984 * \retval #PSA_SUCCESS \emptydescription
3985 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
3986 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
3987 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
3988 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
3989 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
3990 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
itayzafrir0adf0fc2018-09-06 16:24:41 +03003991 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003992 * The library has not been previously initialized by psa_crypto_init().
3993 * It is implementation-dependent whether a failure to initialize
3994 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003995 */
3996psa_status_t psa_generate_random(uint8_t *output,
3997 size_t output_size);
3998
3999/**
4000 * \brief Generate a key or key pair.
4001 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02004002 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02004003 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02004004 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02004005 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02004006 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02004007 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004008 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02004009 * the public exponent is 65537.
4010 * The modulus is a product of two probabilistic primes
4011 * between 2^{n-1} and 2^n where n is the bit size specified in the
4012 * attributes.
4013 *
Gilles Peskine20628592019-04-19 19:29:50 +02004014 * \param[in] attributes The attributes for the new key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02004015 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02004016 * key. For persistent keys, this is the key
4017 * identifier defined in \p attributes.
4018 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004019 *
Gilles Peskine28538492018-07-11 17:34:00 +02004020 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01004021 * Success.
4022 * If the key is persistent, the key material and the key's metadata
4023 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02004024 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02004025 * This is an attempt to create a persistent key, and there is
4026 * already a persistent key with the given identifier.
Gilles Peskineed733552023-02-14 19:21:09 +01004027 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4028 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4029 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4030 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
4031 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4032 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4033 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4034 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
4035 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4036 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4037 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
itayzafrir90d8c7a2018-09-12 11:44:52 +03004038 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03004039 * The library has not been previously initialized by psa_crypto_init().
4040 * It is implementation-dependent whether a failure to initialize
4041 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004042 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004043psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004044 mbedtls_svc_key_id_t *key);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004045
4046/**@}*/
4047
Paul Elliott1265f002022-09-09 17:15:43 +01004048/** \defgroup interruptible_hash Interruptible sign/verify hash
4049 * @{
4050 */
4051
4052/** The type of the state data structure for interruptible hash
4053 * signing operations.
4054 *
4055 * Before calling any function on a sign hash operation object, the
4056 * application must initialize it by any of the following means:
4057 * - Set the structure to all-bits-zero, for example:
4058 * \code
4059 * psa_sign_hash_interruptible_operation_t operation;
4060 * memset(&operation, 0, sizeof(operation));
4061 * \endcode
4062 * - Initialize the structure to logical zero values, for example:
4063 * \code
4064 * psa_sign_hash_interruptible_operation_t operation = {0};
4065 * \endcode
4066 * - Initialize the structure to the initializer
4067 * #PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:
4068 * \code
4069 * psa_sign_hash_interruptible_operation_t operation =
4070 * PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT;
4071 * \endcode
4072 * - Assign the result of the function
4073 * psa_sign_hash_interruptible_operation_init() to the structure, for
4074 * example:
4075 * \code
4076 * psa_sign_hash_interruptible_operation_t operation;
4077 * operation = psa_sign_hash_interruptible_operation_init();
4078 * \endcode
4079 *
4080 * This is an implementation-defined \c struct. Applications should not
4081 * make any assumptions about the content of this structure.
4082 * Implementation details can change in future versions without notice. */
4083typedef struct psa_sign_hash_interruptible_operation_s psa_sign_hash_interruptible_operation_t;
4084
4085/** The type of the state data structure for interruptible hash
4086 * verification operations.
4087 *
4088 * Before calling any function on a sign hash operation object, the
4089 * application must initialize it by any of the following means:
4090 * - Set the structure to all-bits-zero, for example:
4091 * \code
4092 * psa_verify_hash_interruptible_operation_t operation;
4093 * memset(&operation, 0, sizeof(operation));
4094 * \endcode
4095 * - Initialize the structure to logical zero values, for example:
4096 * \code
4097 * psa_verify_hash_interruptible_operation_t operation = {0};
4098 * \endcode
4099 * - Initialize the structure to the initializer
4100 * #PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT, for example:
4101 * \code
4102 * psa_verify_hash_interruptible_operation_t operation =
4103 * PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT;
4104 * \endcode
4105 * - Assign the result of the function
4106 * psa_verify_hash_interruptible_operation_init() to the structure, for
4107 * example:
4108 * \code
4109 * psa_verify_hash_interruptible_operation_t operation;
4110 * operation = psa_verify_hash_interruptible_operation_init();
4111 * \endcode
4112 *
4113 * This is an implementation-defined \c struct. Applications should not
4114 * make any assumptions about the content of this structure.
4115 * Implementation details can change in future versions without notice. */
4116typedef struct psa_verify_hash_interruptible_operation_s psa_verify_hash_interruptible_operation_t;
4117
4118/**
4119 * \brief Set the maximum number of ops allowed to be
4120 * executed by an interruptible function in a
4121 * single call.
4122 *
4123 * \warning This is a beta API, and thus subject to change
4124 * at any point. It is not bound by the usual
4125 * interface stability promises.
4126 *
4127 * \note The time taken to execute a single op is
4128 * implementation specific and depends on
4129 * software, hardware, the algorithm, key type and
4130 * curve chosen. Even within a single operation,
4131 * successive ops can take differing amounts of
4132 * time. The only guarantee is that lower values
4133 * for \p max_ops means functions will block for a
4134 * lesser maximum amount of time. The functions
4135 * \c psa_sign_interruptible_get_num_ops() and
4136 * \c psa_verify_interruptible_get_num_ops() are
4137 * provided to help with tuning this value.
4138 *
4139 * \note This value defaults to
4140 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, which
4141 * means the whole operation will be done in one
4142 * go, regardless of the number of ops required.
4143 *
4144 * \note If more ops are needed to complete a
4145 * computation, #PSA_OPERATION_INCOMPLETE will be
4146 * returned by the function performing the
4147 * computation. It is then the caller's
4148 * responsibility to either call again with the
4149 * same operation context until it returns 0 or an
4150 * error code; or to call the relevant abort
4151 * function if the answer is no longer required.
4152 *
4153 * \note The interpretation of \p max_ops is also
4154 * implementation defined. On a hard real time
4155 * system, this can indicate a hard deadline, as a
4156 * real-time system needs a guarantee of not
4157 * spending more than X time, however care must be
4158 * taken in such an implementation to avoid the
4159 * situation whereby calls just return, not being
4160 * able to do any actual work within the allotted
4161 * time. On a non-real-time system, the
4162 * implementation can be more relaxed, but again
4163 * whether this number should be interpreted as as
4164 * hard or soft limit or even whether a less than
4165 * or equals as regards to ops executed in a
4166 * single call is implementation defined.
4167 *
Paul Elliott21c39512023-02-15 19:47:39 +00004168 * \note For keys in local storage when no accelerator
4169 * driver applies, please see also the
4170 * documentation for \c mbedtls_ecp_set_max_ops(),
4171 * which is the internal implementation in these
4172 * cases.
4173 *
Paul Elliott1265f002022-09-09 17:15:43 +01004174 * \warning With implementations that interpret this number
4175 * as a hard limit, setting this number too small
4176 * may result in an infinite loop, whereby each
4177 * call results in immediate return with no ops
4178 * done (as there is not enough time to execute
4179 * any), and thus no result will ever be achieved.
4180 *
4181 * \note This only applies to functions whose
4182 * documentation mentions they may return
4183 * #PSA_OPERATION_INCOMPLETE.
4184 *
4185 * \param max_ops The maximum number of ops to be executed in a
4186 * single call. This can be a number from 0 to
4187 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, where 0
4188 * is the least amount of work done per call.
4189 */
4190void psa_interruptible_set_max_ops(uint32_t max_ops);
4191
4192/**
4193 * \brief Get the maximum number of ops allowed to be
4194 * executed by an interruptible function in a
4195 * single call. This will return the last
4196 * value set by
4197 * \c psa_interruptible_set_max_ops() or
4198 * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED if
4199 * that function has never been called.
4200 *
4201 * \warning This is a beta API, and thus subject to change
4202 * at any point. It is not bound by the usual
4203 * interface stability promises.
4204 *
4205 * \return Maximum number of ops allowed to be
4206 * executed by an interruptible function in a
4207 * single call.
4208 */
4209uint32_t psa_interruptible_get_max_ops(void);
4210
4211/**
4212 * \brief Get the number of ops that a hash signing
4213 * operation has taken so far. If the operation
4214 * has completed, then this will represent the
4215 * number of ops required for the entire
4216 * operation. After initialization or calling
4217 * \c psa_sign_hash_interruptible_abort() on
4218 * the operation, a value of 0 will be returned.
4219 *
4220 * \warning This is a beta API, and thus subject to change
4221 * at any point. It is not bound by the usual
4222 * interface stability promises.
4223 *
4224 * This is a helper provided to help you tune the
4225 * value passed to \c
4226 * psa_interruptible_set_max_ops().
4227 *
4228 * \param operation The \c psa_sign_hash_interruptible_operation_t
4229 * to use. This must be initialized first.
4230 *
4231 * \return Number of ops that the operation has taken so
4232 * far.
4233 */
4234uint32_t psa_sign_hash_get_num_ops(
4235 const psa_sign_hash_interruptible_operation_t *operation);
4236
4237/**
4238 * \brief Get the number of ops that a hash verification
4239 * operation has taken so far. If the operation
4240 * has completed, then this will represent the
4241 * number of ops required for the entire
4242 * operation. After initialization or calling \c
4243 * psa_verify_hash_interruptible_abort() on the
4244 * operation, a value of 0 will be returned.
4245 *
4246 * \warning This is a beta API, and thus subject to change
4247 * at any point. It is not bound by the usual
4248 * interface stability promises.
4249 *
4250 * This is a helper provided to help you tune the
4251 * value passed to \c
4252 * psa_interruptible_set_max_ops().
4253 *
4254 * \param operation The \c
4255 * psa_verify_hash_interruptible_operation_t to
4256 * use. This must be initialized first.
4257 *
4258 * \return Number of ops that the operation has taken so
4259 * far.
4260 */
4261uint32_t psa_verify_hash_get_num_ops(
4262 const psa_verify_hash_interruptible_operation_t *operation);
4263
4264/**
4265 * \brief Start signing a hash or short message with a
4266 * private key, in an interruptible manner.
4267 *
4268 * \see \c psa_sign_hash_complete()
4269 *
4270 * \warning This is a beta API, and thus subject to change
4271 * at any point. It is not bound by the usual
4272 * interface stability promises.
4273 *
4274 * \note This function combined with \c
4275 * psa_sign_hash_complete() is equivalent to
4276 * \c psa_sign_hash() but
4277 * \c psa_sign_hash_complete() can return early and
4278 * resume according to the limit set with \c
4279 * psa_interruptible_set_max_ops() to reduce the
4280 * maximum time spent in a function call.
4281 *
4282 * \note Users should call \c psa_sign_hash_complete()
4283 * repeatedly on the same context after a
4284 * successful call to this function until \c
4285 * psa_sign_hash_complete() either returns 0 or an
4286 * error. \c psa_sign_hash_complete() will return
4287 * #PSA_OPERATION_INCOMPLETE if there is more work
4288 * to do. Alternatively users can call
4289 * \c psa_sign_hash_abort() at any point if they no
4290 * longer want the result.
4291 *
4292 * \note If this function returns an error status, the
4293 * operation enters an error state and must be
4294 * aborted by calling \c psa_sign_hash_abort().
4295 *
4296 * \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t
4297 * to use. This must be initialized first.
4298 *
4299 * \param key Identifier of the key to use for the operation.
4300 * It must be an asymmetric key pair. The key must
4301 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
4302 * \param alg A signature algorithm (\c PSA_ALG_XXX
4303 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
4304 * is true), that is compatible with
4305 * the type of \p key.
4306 * \param[in] hash The hash or message to sign.
4307 * \param hash_length Size of the \p hash buffer in bytes.
4308 *
4309 * \retval #PSA_SUCCESS
4310 * The operation started successfully - call \c psa_sign_hash_complete()
4311 * with the same context to complete the operation
4312 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004313 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004314 * \retval #PSA_ERROR_NOT_PERMITTED
4315 * The key does not have the #PSA_KEY_USAGE_SIGN_HASH flag, or it does
4316 * not permit the requested algorithm.
4317 * \retval #PSA_ERROR_BAD_STATE
4318 * An operation has previously been started on this context, and is
4319 * still in progress.
Paul Elliott15d7d432023-02-27 17:17:56 +00004320 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4321 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4322 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4323 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4324 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4325 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4326 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4327 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4328 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4329 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004330 * \retval #PSA_ERROR_BAD_STATE
4331 * The library has not been previously initialized by psa_crypto_init().
4332 * It is implementation-dependent whether a failure to initialize
4333 * results in this error code.
4334 */
4335psa_status_t psa_sign_hash_start(
4336 psa_sign_hash_interruptible_operation_t *operation,
4337 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
4338 const uint8_t *hash, size_t hash_length);
4339
4340/**
4341 * \brief Continue and eventually complete the action of
4342 * signing a hash or short message with a private
4343 * key, in an interruptible manner.
4344 *
4345 * \see \c psa_sign_hash_start()
4346 *
4347 * \warning This is a beta API, and thus subject to change
4348 * at any point. It is not bound by the usual
4349 * interface stability promises.
4350 *
4351 * \note This function combined with \c
4352 * psa_sign_hash_start() is equivalent to
4353 * \c psa_sign_hash() but this function can return
4354 * early and resume according to the limit set with
4355 * \c psa_interruptible_set_max_ops() to reduce the
4356 * maximum time spent in a function call.
4357 *
4358 * \note Users should call this function on the same
4359 * operation object repeatedly until it either
4360 * returns 0 or an error. This function will return
4361 * #PSA_OPERATION_INCOMPLETE if there is more work
4362 * to do. Alternatively users can call
4363 * \c psa_sign_hash_abort() at any point if they no
4364 * longer want the result.
4365 *
4366 * \note When this function returns successfully, the
4367 * operation becomes inactive. If this function
4368 * returns an error status, the operation enters an
4369 * error state and must be aborted by calling
4370 * \c psa_sign_hash_abort().
4371 *
4372 * \param[in, out] operation The \c psa_sign_hash_interruptible_operation_t
4373 * to use. This must be initialized first, and have
4374 * had \c psa_sign_hash_start() called with it
4375 * first.
4376 *
4377 * \param[out] signature Buffer where the signature is to be written.
4378 * \param signature_size Size of the \p signature buffer in bytes. This
4379 * must be appropriate for the selected
4380 * algorithm and key:
4381 * - The required signature size is
4382 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c
4383 * key_bits, \c alg) where \c key_type and \c
4384 * key_bits are the type and bit-size
4385 * respectively of key.
4386 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
4387 * maximum signature size of any supported
4388 * signature algorithm.
4389 * \param[out] signature_length On success, the number of bytes that make up
4390 * the returned signature value.
4391 *
4392 * \retval #PSA_SUCCESS
4393 * Operation completed successfully
4394 *
4395 * \retval #PSA_OPERATION_INCOMPLETE
4396 * Operation was interrupted due to the setting of \c
4397 * psa_interruptible_set_max_ops(). There is still work to be done.
4398 * Call this function again with the same operation object.
4399 *
4400 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
4401 * The size of the \p signature buffer is too small. You can
4402 * determine a sufficient buffer size by calling
4403 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
4404 * where \c key_type and \c key_bits are the type and bit-size
4405 * respectively of \p key.
4406 *
4407 * \retval #PSA_ERROR_BAD_STATE
4408 * An operation was not previously started on this context via
4409 * \c psa_sign_hash_start().
4410 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004411 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4412 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4413 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4414 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4415 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4416 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4417 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4418 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4419 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4420 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004421 * \retval #PSA_ERROR_BAD_STATE
4422 * The library has either not been previously initialized by
Paul Elliott15d7d432023-02-27 17:17:56 +00004423 * psa_crypto_init() or you did not previously call
Paul Elliott1265f002022-09-09 17:15:43 +01004424 * psa_sign_hash_start() with this operation object. It is
4425 * implementation-dependent whether a failure to initialize results in
4426 * this error code.
4427 */
4428psa_status_t psa_sign_hash_complete(
4429 psa_sign_hash_interruptible_operation_t *operation,
4430 uint8_t *signature, size_t signature_size,
4431 size_t *signature_length);
4432
4433/**
4434 * \brief Abort a sign hash operation.
4435 *
4436 * \warning This is a beta API, and thus subject to change
4437 * at any point. It is not bound by the usual
4438 * interface stability promises.
Paul Elliott1b49ef52023-02-03 14:27:32 +00004439 *
4440 * \note This function is the only function that clears
4441 * the number of ops completed as part of the
4442 * operation. Please ensure you copy this value via
4443 * \c psa_sign_hash_get_num_ops() if required
4444 * before calling.
4445 *
Paul Elliott1265f002022-09-09 17:15:43 +01004446 * \note Aborting an operation frees all associated
4447 * resources except for the \p operation structure
4448 * itself. Once aborted, the operation object can
4449 * be reused for another operation by calling \c
4450 * psa_sign_hash_start() again.
4451 *
4452 * \note You may call this function any time after the
4453 * operation object has been initialized. In
4454 * particular, calling \c psa_sign_hash_abort()
4455 * after the operation has already been terminated
4456 * by a call to \c psa_sign_hash_abort() or
Paul Elliott1b49ef52023-02-03 14:27:32 +00004457 * psa_sign_hash_complete() is safe.
Paul Elliott1265f002022-09-09 17:15:43 +01004458 *
4459 * \param[in,out] operation Initialized sign hash operation.
4460 *
4461 * \retval #PSA_SUCCESS
4462 * The operation was aborted successfully.
4463 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004464 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004465 * \retval #PSA_ERROR_BAD_STATE
4466 * The library has not been previously initialized by psa_crypto_init().
4467 * It is implementation-dependent whether a failure to initialize
4468 * results in this error code.
4469 */
4470psa_status_t psa_sign_hash_abort(
4471 psa_sign_hash_interruptible_operation_t *operation);
4472
4473/**
4474 * \brief Start reading and verifying a hash or short
4475 * message, in an interruptible manner.
4476 *
4477 * \see \c psa_verify_hash_complete()
4478 *
4479 * \warning This is a beta API, and thus subject to change
4480 * at any point. It is not bound by the usual
4481 * interface stability promises.
4482 *
4483 * \note This function combined with \c
4484 * psa_verify_hash_complete() is equivalent to
4485 * \c psa_verify_hash() but \c
4486 * psa_verify_hash_complete() can return early and
4487 * resume according to the limit set with \c
4488 * psa_interruptible_set_max_ops() to reduce the
4489 * maximum time spent in a function.
4490 *
4491 * \note Users should call \c psa_verify_hash_complete()
4492 * repeatedly on the same operation object after a
4493 * successful call to this function until \c
4494 * psa_verify_hash_complete() either returns 0 or
4495 * an error. \c psa_verify_hash_complete() will
4496 * return #PSA_OPERATION_INCOMPLETE if there is
4497 * more work to do. Alternatively users can call
4498 * \c psa_verify_hash_abort() at any point if they
4499 * no longer want the result.
4500 *
4501 * \note If this function returns an error status, the
4502 * operation enters an error state and must be
4503 * aborted by calling \c psa_verify_hash_abort().
4504 *
4505 * \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t
4506 * to use. This must be initialized first.
4507 *
4508 * \param key Identifier of the key to use for the operation.
4509 * The key must allow the usage
4510 * #PSA_KEY_USAGE_VERIFY_HASH.
4511 * \param alg A signature algorithm (\c PSA_ALG_XXX
4512 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
4513 * is true), that is compatible with
4514 * the type of \p key.
4515 * \param[in] hash The hash whose signature is to be verified.
4516 * \param hash_length Size of the \p hash buffer in bytes.
4517 * \param[in] signature Buffer containing the signature to verify.
4518 * \param signature_length Size of the \p signature buffer in bytes.
4519 *
4520 * \retval #PSA_SUCCESS
4521 * The operation started successfully - please call \c
4522 * psa_verify_hash_complete() with the same context to complete the
4523 * operation.
4524 *
4525 * \retval #PSA_ERROR_BAD_STATE
4526 * Another operation has already been started on this context, and is
4527 * still in progress.
4528 *
4529 * \retval #PSA_ERROR_NOT_PERMITTED
4530 * The key does not have the #PSA_KEY_USAGE_VERIFY_HASH flag, or it does
4531 * not permit the requested algorithm.
4532 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004533 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4534 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4535 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4536 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4537 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4538 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4539 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4540 * \retval PSA_ERROR_DATA_CORRUPT \emptydescription
4541 * \retval PSA_ERROR_DATA_INVALID \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004542 * \retval #PSA_ERROR_BAD_STATE
4543 * The library has not been previously initialized by psa_crypto_init().
4544 * It is implementation-dependent whether a failure to initialize
4545 * results in this error code.
4546 */
4547psa_status_t psa_verify_hash_start(
4548 psa_verify_hash_interruptible_operation_t *operation,
4549 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
4550 const uint8_t *hash, size_t hash_length,
4551 const uint8_t *signature, size_t signature_length);
4552
4553/**
4554 * \brief Continue and eventually complete the action of
4555 * reading and verifying a hash or short message
4556 * signed with a private key, in an interruptible
4557 * manner.
4558 *
4559 * \see \c psa_verify_hash_start()
4560 *
4561 * \warning This is a beta API, and thus subject to change
4562 * at any point. It is not bound by the usual
4563 * interface stability promises.
4564 *
4565 * \note This function combined with \c
4566 * psa_verify_hash_start() is equivalent to
4567 * \c psa_verify_hash() but this function can
4568 * return early and resume according to the limit
4569 * set with \c psa_interruptible_set_max_ops() to
4570 * reduce the maximum time spent in a function
4571 * call.
4572 *
4573 * \note Users should call this function on the same
4574 * operation object repeatedly until it either
4575 * returns 0 or an error. This function will return
4576 * #PSA_OPERATION_INCOMPLETE if there is more work
4577 * to do. Alternatively users can call
4578 * \c psa_verify_hash_abort() at any point if they
4579 * no longer want the result.
4580 *
4581 * \note When this function returns successfully, the
4582 * operation becomes inactive. If this function
4583 * returns an error status, the operation enters an
4584 * error state and must be aborted by calling
4585 * \c psa_verify_hash_abort().
4586 *
4587 * \param[in, out] operation The \c psa_verify_hash_interruptible_operation_t
4588 * to use. This must be initialized first, and have
4589 * had \c psa_verify_hash_start() called with it
4590 * first.
4591 *
4592 * \retval #PSA_SUCCESS
4593 * Operation completed successfully, and the passed signature is valid.
4594 *
4595 * \retval #PSA_OPERATION_INCOMPLETE
4596 * Operation was interrupted due to the setting of \c
4597 * psa_interruptible_set_max_ops(). There is still work to be done.
4598 * Call this function again with the same operation object.
4599 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004600 * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004601 * \retval #PSA_ERROR_INVALID_SIGNATURE
4602 * The calculation was performed successfully, but the passed
4603 * signature is not a valid signature.
Paul Elliott15d7d432023-02-27 17:17:56 +00004604 * \retval #PSA_ERROR_BAD_STATE
Paul Elliott1265f002022-09-09 17:15:43 +01004605 * An operation was not previously started on this context via
4606 * \c psa_verify_hash_start().
Paul Elliott15d7d432023-02-27 17:17:56 +00004607 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
4608 * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
4609 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
4610 * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
4611 * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
4612 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
4613 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
4614 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
4615 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
4616 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004617 * \retval #PSA_ERROR_BAD_STATE
4618 * The library has either not been previously initialized by
4619 * psa_crypto_init() or you did not previously call
4620 * psa_verify_hash_start() on this object. It is
4621 * implementation-dependent whether a failure to initialize results in
4622 * this error code.
4623 */
4624psa_status_t psa_verify_hash_complete(
4625 psa_verify_hash_interruptible_operation_t *operation);
4626
4627/**
4628 * \brief Abort a verify hash operation.
4629 *
4630 * \warning This is a beta API, and thus subject to change at
4631 * any point. It is not bound by the usual interface
4632 * stability promises.
4633 *
Paul Elliott1b49ef52023-02-03 14:27:32 +00004634 * \note This function is the only function that clears the
4635 * number of ops completed as part of the operation.
4636 * Please ensure you copy this value via
4637 * \c psa_verify_hash_get_num_ops() if required
4638 * before calling.
4639 *
Paul Elliott1265f002022-09-09 17:15:43 +01004640 * \note Aborting an operation frees all associated
4641 * resources except for the operation structure
4642 * itself. Once aborted, the operation object can be
4643 * reused for another operation by calling \c
4644 * psa_verify_hash_start() again.
4645 *
4646 * \note You may call this function any time after the
4647 * operation object has been initialized.
4648 * In particular, calling \c psa_verify_hash_abort()
4649 * after the operation has already been terminated by
4650 * a call to \c psa_verify_hash_abort() or
Paul Elliott1b49ef52023-02-03 14:27:32 +00004651 * psa_verify_hash_complete() is safe.
Paul Elliott1265f002022-09-09 17:15:43 +01004652 *
4653 * \param[in,out] operation Initialized verify hash operation.
4654 *
4655 * \retval #PSA_SUCCESS
4656 * The operation was aborted successfully.
4657 *
Paul Elliott15d7d432023-02-27 17:17:56 +00004658 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
Paul Elliott1265f002022-09-09 17:15:43 +01004659 * \retval #PSA_ERROR_BAD_STATE
4660 * The library has not been previously initialized by psa_crypto_init().
4661 * It is implementation-dependent whether a failure to initialize
4662 * results in this error code.
4663 */
4664psa_status_t psa_verify_hash_abort(
4665 psa_verify_hash_interruptible_operation_t *operation);
4666
4667
4668/**@}*/
4669
Gilles Peskinee59236f2018-01-27 23:32:46 +01004670#ifdef __cplusplus
4671}
4672#endif
4673
Gilles Peskine0cad07c2018-06-27 19:49:02 +02004674/* The file "crypto_sizes.h" contains definitions for size calculation
4675 * macros whose definitions are implementation-specific. */
4676#include "crypto_sizes.h"
4677
Gilles Peskine9ef733f2018-02-07 21:05:37 +01004678/* The file "crypto_struct.h" contains definitions for
4679 * implementation-specific structs that are declared above. */
4680#include "crypto_struct.h"
4681
4682/* The file "crypto_extra.h" contains vendor-specific definitions. This
4683 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004684#include "crypto_extra.h"
4685
4686#endif /* PSA_CRYPTO_H */