blob: b3ef3631d420e6a96d5a618353be099e8ceb8b57 [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 Peskine28538492018-07-11 17:34:00 +020091 * \retval #PSA_SUCCESS
92 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
gabor-mezei-arm452b0a32020-11-09 17:42:55 +010093 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine28538492018-07-11 17:34:00 +020094 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
95 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +020096 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +020097 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
gabor-mezei-arm452b0a32020-11-09 17:42:55 +010098 * \retval #PSA_ERROR_STORAGE_FAILURE
99 * \retval #PSA_ERROR_DATA_INVALID
100 * \retval #PSA_ERROR_DATA_CORRUPT
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 */
Ronald Cron71016a92020-08-28 19:01:50 +0200140static 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.
156 * \param owner_id The key owner identifier.
157 */
158static void mbedtls_set_key_owner_id( psa_key_attributes_t *attributes,
159 mbedtls_key_owner_id_t owner_id );
160#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 *
371 * \retval #PSA_SUCCESS
372 * \retval #PSA_ERROR_INVALID_HANDLE
373 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
374 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100375 * \retval #PSA_ERROR_CORRUPTION_DETECTED
376 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100377 * \retval #PSA_ERROR_DATA_CORRUPT
378 * \retval #PSA_ERROR_DATA_INVALID
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 *
489 * \retval #PSA_SUCCESS
490 * \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
496 * The lifetime or identifier in \p attributes are invalid.
497 * \retval #PSA_ERROR_INVALID_ARGUMENT
498 * The policy constraints on the source and specified in
499 * \p attributes are incompatible.
500 * \retval #PSA_ERROR_INVALID_ARGUMENT
501 * \p attributes specifies a key type or key size
502 * which does not match the attributes of the source key.
503 * \retval #PSA_ERROR_NOT_PERMITTED
504 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
505 * \retval #PSA_ERROR_NOT_PERMITTED
506 * The source key is not exportable and its lifetime does not
507 * allow copying it to the target's lifetime.
508 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
509 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
510 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
511 * \retval #PSA_ERROR_HARDWARE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100512 * \retval #PSA_ERROR_DATA_INVALID
513 * \retval #PSA_ERROR_DATA_CORRUPT
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100514 * \retval #PSA_ERROR_STORAGE_FAILURE
515 * \retval #PSA_ERROR_CORRUPTION_DETECTED
516 * \retval #PSA_ERROR_BAD_STATE
517 * The library has not been previously initialized by psa_crypto_init().
518 * It is implementation-dependent whether a failure to initialize
519 * results in this error code.
520 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200521psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100522 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200523 mbedtls_svc_key_id_t *target_key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100524
525
526/**
527 * \brief Destroy a key.
528 *
529 * This function destroys a key from both volatile
530 * memory and, if applicable, non-volatile storage. Implementations shall
531 * make a best effort to ensure that that the key material cannot be recovered.
532 *
533 * This function also erases any metadata such as policies and frees
Ronald Croncf56a0a2020-08-04 09:51:30 +0200534 * resources associated with the key.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100535 *
536 * If a key is currently in use in a multipart operation, then destroying the
537 * key will cause the multipart operation to fail.
538 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200539 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
Ronald Cron96783552020-10-19 12:06:30 +0200540 * return #PSA_SUCCESS.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100541 *
542 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +0200543 * \p key was a valid identifier and the key material that it
544 * referred to has been erased. Alternatively, \p key is \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100545 * \retval #PSA_ERROR_NOT_PERMITTED
546 * The key cannot be erased because it is
547 * read-only, either due to a policy or due to physical restrictions.
548 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200549 * \p key is not a valid identifier nor \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100550 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
551 * There was an failure in communication with the cryptoprocessor.
552 * The key material may still be present in the cryptoprocessor.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100553 * \retval #PSA_ERROR_DATA_INVALID
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100554 * This error is typically a result of either storage corruption on a
555 * cleartext storage backend, or an attempt to read data that was
556 * written by an incompatible version of the library.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100557 * \retval #PSA_ERROR_STORAGE_FAILURE
558 * The storage is corrupted. Implementations shall make a best effort
559 * to erase key material even in this stage, however applications
560 * should be aware that it may be impossible to guarantee that the
561 * key material is not recoverable in such cases.
562 * \retval #PSA_ERROR_CORRUPTION_DETECTED
563 * An unexpected condition which is not a storage corruption or
564 * a communication failure occurred. The cryptoprocessor may have
565 * been compromised.
566 * \retval #PSA_ERROR_BAD_STATE
567 * The library has not been previously initialized by psa_crypto_init().
568 * It is implementation-dependent whether a failure to initialize
569 * results in this error code.
570 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200571psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100572
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100573/**@}*/
574
575/** \defgroup import_export Key import and export
576 * @{
577 */
578
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100579/**
580 * \brief Import a key in binary format.
581 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100582 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100583 * documentation of psa_export_public_key() for the format of public keys
584 * and to the documentation of psa_export_key() for the format for
585 * other key types.
586 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200587 * The key data determines the key size. The attributes may optionally
588 * specify a key size; in this case it must match the size determined
589 * from the key data. A key size of 0 in \p attributes indicates that
590 * the key size is solely determined by the key data.
591 *
592 * Implementations must reject an attempt to import a key of size 0.
593 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100594 * This specification supports a single format for each key type.
595 * Implementations may support other formats as long as the standard
596 * format is supported. Implementations that support other formats
597 * should ensure that the formats are clearly unambiguous so as to
598 * minimize the risk that an invalid input is accidentally interpreted
599 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100600 *
Gilles Peskine20628592019-04-19 19:29:50 +0200601 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200602 * The key size is always determined from the
603 * \p data buffer.
604 * If the key size in \p attributes is nonzero,
605 * it must be equal to the size from \p data.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200606 * \param[out] key On success, an identifier to the newly created key.
Ronald Cron4067d1c2020-10-19 13:34:38 +0200607 * For persistent keys, this is the key identifier
608 * defined in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200609 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100610 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200611 * buffer is interpreted according to the type declared
612 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200613 * All implementations must support at least the format
614 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100615 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200616 * the chosen type. Implementations may allow other
617 * formats, but should be conservative: implementations
618 * should err on the side of rejecting content if it
619 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200620 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100621 *
Gilles Peskine28538492018-07-11 17:34:00 +0200622 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100623 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100624 * If the key is persistent, the key material and the key's metadata
625 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200626 * \retval #PSA_ERROR_ALREADY_EXISTS
627 * This is an attempt to create a persistent key, and there is
628 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200629 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200630 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200631 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200632 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200633 * The key attributes, as a whole, are invalid.
634 * \retval #PSA_ERROR_INVALID_ARGUMENT
635 * The key data is not correctly formatted.
636 * \retval #PSA_ERROR_INVALID_ARGUMENT
637 * The size in \p attributes is nonzero and does not match the size
638 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200639 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
640 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
641 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100642 * \retval #PSA_ERROR_DATA_CORRUPT
643 * \retval #PSA_ERROR_DATA_INVALID
Darryl Greend49a4992018-06-18 17:27:26 +0100644 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200645 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200646 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300647 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300648 * The library has not been previously initialized by psa_crypto_init().
649 * It is implementation-dependent whether a failure to initialize
650 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100651 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200652psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100653 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200654 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200655 mbedtls_svc_key_id_t *key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100656
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100657
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100658
659/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100660 * \brief Export a key in binary format.
661 *
662 * The output of this function can be passed to psa_import_key() to
663 * create an equivalent object.
664 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100665 * If the implementation of psa_import_key() supports other formats
666 * beyond the format specified here, the output from psa_export_key()
667 * must use the representation specified here, not the original
668 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100669 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100670 * For standard key types, the output format is as follows:
671 *
672 * - For symmetric keys (including MAC keys), the format is the
673 * raw bytes of the key.
674 * - For DES, the key data consists of 8 bytes. The parity bits must be
675 * correct.
676 * - For Triple-DES, the format is the concatenation of the
677 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200678 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200679 * is the non-encrypted DER encoding of the representation defined by
680 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
681 * ```
682 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200683 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200684 * modulus INTEGER, -- n
685 * publicExponent INTEGER, -- e
686 * privateExponent INTEGER, -- d
687 * prime1 INTEGER, -- p
688 * prime2 INTEGER, -- q
689 * exponent1 INTEGER, -- d mod (p-1)
690 * exponent2 INTEGER, -- d mod (q-1)
691 * coefficient INTEGER, -- (inverse of q) mod p
692 * }
693 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200694 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200695 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100696 * a representation of the private value as a `ceiling(m/8)`-byte string
697 * where `m` is the bit size associated with the curve, i.e. the bit size
698 * of the order of the curve's coordinate field. This byte string is
699 * in little-endian order for Montgomery curves (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +0100700 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
701 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
702 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
Steven Cooreman6f5cc712020-06-11 16:40:41 +0200703 * For Weierstrass curves, this is the content of the `privateKey` field of
704 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
705 * the format is defined by RFC 7748, and output is masked according to §5.
Gilles Peskine67546802021-02-24 21:49:40 +0100706 * For twisted Edwards curves, the private key is as defined by RFC 8032
707 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200708 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200709 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000710 * format is the representation of the private key `x` as a big-endian byte
711 * string. The length of the byte string is the private key size in bytes
712 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200713 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
714 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100715 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200716 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
717 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200718 * \param key Identifier of the key to export. It must allow the
Ronald Cron96783552020-10-19 12:06:30 +0200719 * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
Ronald Croncf56a0a2020-08-04 09:51:30 +0200720 * key.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200721 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200722 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200723 * \param[out] data_length On success, the number of bytes
724 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100725 *
Gilles Peskine28538492018-07-11 17:34:00 +0200726 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100727 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200728 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200729 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100730 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200731 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
732 * The size of the \p data buffer is too small. You can determine a
733 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100734 * #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200735 * where \c type is the key type
736 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200737 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
738 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200739 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100740 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100741 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300742 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300743 * The library has not been previously initialized by psa_crypto_init().
744 * It is implementation-dependent whether a failure to initialize
745 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200747psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100748 uint8_t *data,
749 size_t data_size,
750 size_t *data_length);
751
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100752/**
753 * \brief Export a public key or the public part of a key pair in binary format.
754 *
755 * The output of this function can be passed to psa_import_key() to
756 * create an object that is equivalent to the public key.
757 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000758 * This specification supports a single format for each key type.
759 * Implementations may support other formats as long as the standard
760 * format is supported. Implementations that support other formats
761 * should ensure that the formats are clearly unambiguous so as to
762 * minimize the risk that an invalid input is accidentally interpreted
763 * according to a different format.
764 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000765 * For standard key types, the output format is as follows:
766 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
767 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
768 * ```
769 * RSAPublicKey ::= SEQUENCE {
770 * modulus INTEGER, -- n
771 * publicExponent INTEGER } -- e
772 * ```
Gilles Peskine67546802021-02-24 21:49:40 +0100773 * - For elliptic curve keys on a twisted Edwards curve (key types for which
Bence Szépkúti3b1cba82021-04-08 15:49:07 +0200774 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true and #PSA_KEY_TYPE_ECC_GET_FAMILY
Gilles Peskine67546802021-02-24 21:49:40 +0100775 * returns #PSA_ECC_FAMILY_TWISTED_EDWARDS), the public key is as defined
776 * by RFC 8032
777 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
778 * - For other elliptic curve public keys (key types for which
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000779 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
780 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
781 * Let `m` be the bit size associated with the curve, i.e. the bit size of
782 * `q` for a curve over `F_q`. The representation consists of:
783 * - The byte 0x04;
784 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
785 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200786 * - For Diffie-Hellman key exchange public keys (key types for which
787 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000788 * the format is the representation of the public key `y = g^x mod p` as a
789 * big-endian byte string. The length of the byte string is the length of the
790 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100791 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200792 * Exporting a public key object or the public part of a key pair is
793 * always permitted, regardless of the key's usage flags.
794 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200795 * \param key Identifier of the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200796 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200797 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200798 * \param[out] data_length On success, the number of bytes
799 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100800 *
Gilles Peskine28538492018-07-11 17:34:00 +0200801 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100802 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200803 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200804 * The key is neither a public key nor a key pair.
805 * \retval #PSA_ERROR_NOT_SUPPORTED
806 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
807 * The size of the \p data buffer is too small. You can determine a
808 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100809 * #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200810 * where \c type is the key type
811 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200812 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
813 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200814 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100815 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100816 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300817 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300818 * The library has not been previously initialized by psa_crypto_init().
819 * It is implementation-dependent whether a failure to initialize
820 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100821 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200822psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100823 uint8_t *data,
824 size_t data_size,
825 size_t *data_length);
826
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100827
Gilles Peskine20035e32018-02-03 22:44:14 +0100828
829/**@}*/
830
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100831/** \defgroup hash Message digests
832 * @{
833 */
834
Gilles Peskine69647a42019-01-14 20:18:12 +0100835/** Calculate the hash (digest) of a message.
836 *
837 * \note To verify the hash of a message against an
838 * expected value, use psa_hash_compare() instead.
839 *
840 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
841 * such that #PSA_ALG_IS_HASH(\p alg) is true).
842 * \param[in] input Buffer containing the message to hash.
843 * \param input_length Size of the \p input buffer in bytes.
844 * \param[out] hash Buffer where the hash is to be written.
845 * \param hash_size Size of the \p hash buffer in bytes.
846 * \param[out] hash_length On success, the number of bytes
847 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100848 * #PSA_HASH_LENGTH(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100849 *
850 * \retval #PSA_SUCCESS
851 * Success.
852 * \retval #PSA_ERROR_NOT_SUPPORTED
853 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100854 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100855 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
856 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100857 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
858 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
859 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200860 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100861 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100862 * \retval #PSA_ERROR_BAD_STATE
863 * The library has not been previously initialized by psa_crypto_init().
864 * It is implementation-dependent whether a failure to initialize
865 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100866 */
867psa_status_t psa_hash_compute(psa_algorithm_t alg,
868 const uint8_t *input,
869 size_t input_length,
870 uint8_t *hash,
871 size_t hash_size,
872 size_t *hash_length);
873
874/** Calculate the hash (digest) of a message and compare it with a
875 * reference value.
876 *
877 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
878 * such that #PSA_ALG_IS_HASH(\p alg) is true).
879 * \param[in] input Buffer containing the message to hash.
880 * \param input_length Size of the \p input buffer in bytes.
881 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100882 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100883 *
884 * \retval #PSA_SUCCESS
885 * The expected hash is identical to the actual hash of the input.
886 * \retval #PSA_ERROR_INVALID_SIGNATURE
887 * The hash of the message was calculated successfully, but it
888 * differs from the expected hash.
889 * \retval #PSA_ERROR_NOT_SUPPORTED
890 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100891 * \retval #PSA_ERROR_INVALID_ARGUMENT
892 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100893 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
894 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
895 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200896 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100897 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100898 * \retval #PSA_ERROR_BAD_STATE
899 * The library has not been previously initialized by psa_crypto_init().
900 * It is implementation-dependent whether a failure to initialize
901 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100902 */
903psa_status_t psa_hash_compare(psa_algorithm_t alg,
904 const uint8_t *input,
905 size_t input_length,
906 const uint8_t *hash,
Gilles Peskinefa710f52019-12-02 14:31:48 +0100907 size_t hash_length);
Gilles Peskine69647a42019-01-14 20:18:12 +0100908
Gilles Peskine308b91d2018-02-08 09:47:44 +0100909/** The type of the state data structure for multipart hash operations.
910 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000911 * Before calling any function on a hash operation object, the application must
912 * initialize it by any of the following means:
913 * - Set the structure to all-bits-zero, for example:
914 * \code
915 * psa_hash_operation_t operation;
916 * memset(&operation, 0, sizeof(operation));
917 * \endcode
918 * - Initialize the structure to logical zero values, for example:
919 * \code
920 * psa_hash_operation_t operation = {0};
921 * \endcode
922 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
923 * for example:
924 * \code
925 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
926 * \endcode
927 * - Assign the result of the function psa_hash_operation_init()
928 * to the structure, for example:
929 * \code
930 * psa_hash_operation_t operation;
931 * operation = psa_hash_operation_init();
932 * \endcode
933 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100934 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +0100935 * make any assumptions about the content of this structure.
936 * Implementation details can change in future versions without notice. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100937typedef struct psa_hash_operation_s psa_hash_operation_t;
938
Jaeden Amero6a25b412019-01-04 11:47:44 +0000939/** \def PSA_HASH_OPERATION_INIT
940 *
941 * This macro returns a suitable initializer for a hash operation object
942 * of type #psa_hash_operation_t.
943 */
Jaeden Amero6a25b412019-01-04 11:47:44 +0000944
945/** Return an initial value for a hash operation object.
946 */
947static psa_hash_operation_t psa_hash_operation_init(void);
948
Gilles Peskinef45adda2019-01-14 18:29:18 +0100949/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100950 *
951 * The sequence of operations to calculate a hash (message digest)
952 * is as follows:
953 * -# Allocate an operation object which will be passed to all the functions
954 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000955 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100956 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200957 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100958 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100959 * of the message each time. The hash that is calculated is the hash
960 * of the concatenation of these messages in order.
961 * -# To calculate the hash, call psa_hash_finish().
962 * To compare the hash with an expected value, call psa_hash_verify().
963 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100964 * If an error occurs at any step after a call to psa_hash_setup(), the
965 * operation will need to be reset by a call to psa_hash_abort(). The
966 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000967 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100968 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200969 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100970 * eventually terminate the operation. The following events terminate an
971 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100972 * - A successful call to psa_hash_finish() or psa_hash_verify().
973 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100974 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000975 * \param[in,out] operation The operation object to set up. It must have
976 * been initialized as per the documentation for
977 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200978 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
979 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100980 *
Gilles Peskine28538492018-07-11 17:34:00 +0200981 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100982 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200983 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100984 * \p alg is not a supported hash algorithm.
985 * \retval #PSA_ERROR_INVALID_ARGUMENT
986 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100987 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100988 * The operation state is not valid (it must be inactive).
Gilles Peskine28538492018-07-11 17:34:00 +0200989 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
990 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
991 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200992 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100993 * \retval #PSA_ERROR_BAD_STATE
994 * The library has not been previously initialized by psa_crypto_init().
995 * It is implementation-dependent whether a failure to initialize
996 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100997 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200998psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100999 psa_algorithm_t alg);
1000
Gilles Peskine308b91d2018-02-08 09:47:44 +01001001/** Add a message fragment to a multipart hash operation.
1002 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001003 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001004 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001005 * If this function returns an error status, the operation enters an error
1006 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001007 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001008 * \param[in,out] operation Active hash operation.
1009 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001010 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001011 *
Gilles Peskine28538492018-07-11 17:34:00 +02001012 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001013 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001014 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001015 * The operation state is not valid (it muct be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001016 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1017 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1018 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001019 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001020 * \retval #PSA_ERROR_BAD_STATE
1021 * The library has not been previously initialized by psa_crypto_init().
1022 * It is implementation-dependent whether a failure to initialize
1023 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001024 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001025psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1026 const uint8_t *input,
1027 size_t input_length);
1028
Gilles Peskine308b91d2018-02-08 09:47:44 +01001029/** Finish the calculation of the hash of a message.
1030 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001031 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001032 * This function calculates the hash of the message formed by concatenating
1033 * the inputs passed to preceding calls to psa_hash_update().
1034 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001035 * When this function returns successfuly, the operation becomes inactive.
1036 * If this function returns an error status, the operation enters an error
1037 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001038 *
1039 * \warning Applications should not call this function if they expect
1040 * a specific value for the hash. Call psa_hash_verify() instead.
1041 * Beware that comparing integrity or authenticity data such as
1042 * hash values with a function such as \c memcmp is risky
1043 * because the time taken by the comparison may leak information
1044 * about the hashed data which could allow an attacker to guess
1045 * a valid hash and thereby bypass security controls.
1046 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001047 * \param[in,out] operation Active hash operation.
1048 * \param[out] hash Buffer where the hash is to be written.
1049 * \param hash_size Size of the \p hash buffer in bytes.
1050 * \param[out] hash_length On success, the number of bytes
1051 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001052 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001053 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001054 *
Gilles Peskine28538492018-07-11 17:34:00 +02001055 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001056 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001057 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001058 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001059 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001060 * The size of the \p hash buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001061 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001062 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001063 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1064 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1065 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001066 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001067 * \retval #PSA_ERROR_BAD_STATE
1068 * The library has not been previously initialized by psa_crypto_init().
1069 * It is implementation-dependent whether a failure to initialize
1070 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001071 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001072psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1073 uint8_t *hash,
1074 size_t hash_size,
1075 size_t *hash_length);
1076
Gilles Peskine308b91d2018-02-08 09:47:44 +01001077/** Finish the calculation of the hash of a message and compare it with
1078 * an expected value.
1079 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001080 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001081 * This function calculates the hash of the message formed by concatenating
1082 * the inputs passed to preceding calls to psa_hash_update(). It then
1083 * compares the calculated hash with the expected hash passed as a
1084 * parameter to this function.
1085 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001086 * When this function returns successfuly, the operation becomes inactive.
1087 * If this function returns an error status, the operation enters an error
1088 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001089 *
Gilles Peskine19067982018-03-20 17:54:53 +01001090 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001091 * comparison between the actual hash and the expected hash is performed
1092 * in constant time.
1093 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001094 * \param[in,out] operation Active hash operation.
1095 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001096 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001097 *
Gilles Peskine28538492018-07-11 17:34:00 +02001098 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001099 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001100 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001101 * The hash of the message was calculated successfully, but it
1102 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001103 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001104 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001105 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1106 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1107 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001108 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001109 * \retval #PSA_ERROR_BAD_STATE
1110 * The library has not been previously initialized by psa_crypto_init().
1111 * It is implementation-dependent whether a failure to initialize
1112 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001113 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001114psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1115 const uint8_t *hash,
1116 size_t hash_length);
1117
Gilles Peskine308b91d2018-02-08 09:47:44 +01001118/** Abort a hash operation.
1119 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001120 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001121 * \p operation structure itself. Once aborted, the operation object
1122 * can be reused for another operation by calling
1123 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001124 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001125 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001126 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001127 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001128 * In particular, calling psa_hash_abort() after the operation has been
1129 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1130 * psa_hash_verify() is safe and has no effect.
1131 *
1132 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001133 *
Gilles Peskine28538492018-07-11 17:34:00 +02001134 * \retval #PSA_SUCCESS
Gilles Peskine28538492018-07-11 17:34:00 +02001135 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1136 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001137 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001138 * \retval #PSA_ERROR_BAD_STATE
1139 * The library has not been previously initialized by psa_crypto_init().
1140 * It is implementation-dependent whether a failure to initialize
1141 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001142 */
1143psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001144
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001145/** Clone a hash operation.
1146 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001147 * This function copies the state of an ongoing hash operation to
1148 * a new operation object. In other words, this function is equivalent
1149 * to calling psa_hash_setup() on \p target_operation with the same
1150 * algorithm that \p source_operation was set up for, then
1151 * psa_hash_update() on \p target_operation with the same input that
1152 * that was passed to \p source_operation. After this function returns, the
1153 * two objects are independent, i.e. subsequent calls involving one of
1154 * the objects do not affect the other object.
1155 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001156 * \param[in] source_operation The active hash operation to clone.
1157 * \param[in,out] target_operation The operation object to set up.
1158 * It must be initialized but not active.
1159 *
1160 * \retval #PSA_SUCCESS
1161 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001162 * The \p source_operation state is not valid (it must be active).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001163 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001164 * The \p target_operation state is not valid (it must be inactive).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001165 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1166 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001167 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001168 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001169 * \retval #PSA_ERROR_BAD_STATE
1170 * The library has not been previously initialized by psa_crypto_init().
1171 * It is implementation-dependent whether a failure to initialize
1172 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001173 */
1174psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1175 psa_hash_operation_t *target_operation);
1176
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001177/**@}*/
1178
Gilles Peskine8c9def32018-02-08 10:02:12 +01001179/** \defgroup MAC Message authentication codes
1180 * @{
1181 */
1182
Gilles Peskine69647a42019-01-14 20:18:12 +01001183/** Calculate the MAC (message authentication code) of a message.
1184 *
1185 * \note To verify the MAC of a message against an
1186 * expected value, use psa_mac_verify() instead.
1187 * Beware that comparing integrity or authenticity data such as
1188 * MAC values with a function such as \c memcmp is risky
1189 * because the time taken by the comparison may leak information
1190 * about the MAC value which could allow an attacker to guess
1191 * a valid MAC and thereby bypass security controls.
1192 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001193 * \param key Identifier of the key to use for the operation. It
1194 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001195 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001196 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001197 * \param[in] input Buffer containing the input message.
1198 * \param input_length Size of the \p input buffer in bytes.
1199 * \param[out] mac Buffer where the MAC value is to be written.
1200 * \param mac_size Size of the \p mac buffer in bytes.
1201 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001202 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001203 *
1204 * \retval #PSA_SUCCESS
1205 * Success.
1206 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001207 * \retval #PSA_ERROR_NOT_PERMITTED
1208 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001209 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001210 * \retval #PSA_ERROR_NOT_SUPPORTED
1211 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001212 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1213 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001214 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1215 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1216 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001217 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001218 * \retval #PSA_ERROR_STORAGE_FAILURE
1219 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001220 * \retval #PSA_ERROR_BAD_STATE
1221 * The library has not been previously initialized by psa_crypto_init().
1222 * It is implementation-dependent whether a failure to initialize
1223 * results in this error code.
1224 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001225psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001226 psa_algorithm_t alg,
1227 const uint8_t *input,
1228 size_t input_length,
1229 uint8_t *mac,
1230 size_t mac_size,
1231 size_t *mac_length);
1232
1233/** Calculate the MAC of a message and compare it with a reference value.
1234 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001235 * \param key Identifier of the key to use for the operation. It
1236 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001237 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001238 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001239 * \param[in] input Buffer containing the input message.
1240 * \param input_length Size of the \p input buffer in bytes.
1241 * \param[out] mac Buffer containing the expected MAC value.
1242 * \param mac_length Size of the \p mac buffer in bytes.
1243 *
1244 * \retval #PSA_SUCCESS
1245 * The expected MAC is identical to the actual MAC of the input.
1246 * \retval #PSA_ERROR_INVALID_SIGNATURE
1247 * The MAC of the message was calculated successfully, but it
1248 * differs from the expected value.
1249 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001250 * \retval #PSA_ERROR_NOT_PERMITTED
1251 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001252 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001253 * \retval #PSA_ERROR_NOT_SUPPORTED
1254 * \p alg is not supported or is not a MAC algorithm.
1255 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1256 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1257 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001258 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001259 * \retval #PSA_ERROR_STORAGE_FAILURE
1260 * The key could not be retrieved from storage.
1261 * \retval #PSA_ERROR_BAD_STATE
1262 * The library has not been previously initialized by psa_crypto_init().
1263 * It is implementation-dependent whether a failure to initialize
1264 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001265 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001266psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
Gilles Peskinea05602d2019-01-17 15:25:52 +01001267 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001268 const uint8_t *input,
1269 size_t input_length,
1270 const uint8_t *mac,
Gilles Peskine13faa2d2020-01-30 16:32:21 +01001271 size_t mac_length);
Gilles Peskine69647a42019-01-14 20:18:12 +01001272
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001273/** The type of the state data structure for multipart MAC operations.
1274 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001275 * Before calling any function on a MAC operation object, the application must
1276 * initialize it by any of the following means:
1277 * - Set the structure to all-bits-zero, for example:
1278 * \code
1279 * psa_mac_operation_t operation;
1280 * memset(&operation, 0, sizeof(operation));
1281 * \endcode
1282 * - Initialize the structure to logical zero values, for example:
1283 * \code
1284 * psa_mac_operation_t operation = {0};
1285 * \endcode
1286 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1287 * for example:
1288 * \code
1289 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1290 * \endcode
1291 * - Assign the result of the function psa_mac_operation_init()
1292 * to the structure, for example:
1293 * \code
1294 * psa_mac_operation_t operation;
1295 * operation = psa_mac_operation_init();
1296 * \endcode
1297 *
Janos Follath2ba60792021-04-28 09:37:34 +01001298 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001299 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01001300 * make any assumptions about the content of this structure.
1301 * Implementation details can change in future versions without notice. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001302typedef struct psa_mac_operation_s psa_mac_operation_t;
1303
Jaeden Amero769ce272019-01-04 11:48:03 +00001304/** \def PSA_MAC_OPERATION_INIT
1305 *
1306 * This macro returns a suitable initializer for a MAC operation object of type
1307 * #psa_mac_operation_t.
1308 */
Jaeden Amero769ce272019-01-04 11:48:03 +00001309
1310/** Return an initial value for a MAC operation object.
1311 */
1312static psa_mac_operation_t psa_mac_operation_init(void);
1313
Gilles Peskinef45adda2019-01-14 18:29:18 +01001314/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001315 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001316 * This function sets up the calculation of the MAC
1317 * (message authentication code) of a byte string.
1318 * To verify the MAC of a message against an
1319 * expected value, use psa_mac_verify_setup() instead.
1320 *
1321 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001322 * -# Allocate an operation object which will be passed to all the functions
1323 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001324 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001325 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001326 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001327 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1328 * of the message each time. The MAC that is calculated is the MAC
1329 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001330 * -# At the end of the message, call psa_mac_sign_finish() to finish
1331 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001332 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001333 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1334 * operation will need to be reset by a call to psa_mac_abort(). The
1335 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001336 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001337 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001338 * After a successful call to psa_mac_sign_setup(), the application must
1339 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001340 * - A successful call to psa_mac_sign_finish().
1341 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001342 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001343 * \param[in,out] operation The operation object to set up. It must have
1344 * been initialized as per the documentation for
1345 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001346 * \param key Identifier of the key to use for the operation. It
1347 * must remain valid until the operation terminates.
1348 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001349 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001350 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001351 *
Gilles Peskine28538492018-07-11 17:34:00 +02001352 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001353 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001354 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001355 * \retval #PSA_ERROR_NOT_PERMITTED
1356 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001357 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001358 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001359 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001360 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1361 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1362 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001363 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001364 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001365 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001366 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001367 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001368 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001369 * The library has not been previously initialized by psa_crypto_init().
1370 * It is implementation-dependent whether a failure to initialize
1371 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001372 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001373psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001374 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001375 psa_algorithm_t alg);
1376
Gilles Peskinef45adda2019-01-14 18:29:18 +01001377/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001378 *
1379 * This function sets up the verification of the MAC
1380 * (message authentication code) of a byte string against an expected value.
1381 *
1382 * The sequence of operations to verify a MAC is as follows:
1383 * -# Allocate an operation object which will be passed to all the functions
1384 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001385 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001386 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001387 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001388 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1389 * of the message each time. The MAC that is calculated is the MAC
1390 * of the concatenation of these messages in order.
1391 * -# At the end of the message, call psa_mac_verify_finish() to finish
1392 * calculating the actual MAC of the message and verify it against
1393 * the expected value.
1394 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001395 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1396 * operation will need to be reset by a call to psa_mac_abort(). The
1397 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001398 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001399 *
1400 * After a successful call to psa_mac_verify_setup(), the application must
1401 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001402 * - A successful call to psa_mac_verify_finish().
1403 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001404 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001405 * \param[in,out] operation The operation object to set up. It must have
1406 * been initialized as per the documentation for
1407 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001408 * \param key Identifier of the key to use for the operation. It
1409 * must remain valid until the operation terminates.
1410 * It must allow the usage
1411 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001412 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1413 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001414 *
Gilles Peskine28538492018-07-11 17:34:00 +02001415 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001416 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001417 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001418 * \retval #PSA_ERROR_NOT_PERMITTED
1419 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001420 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001421 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001422 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001423 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1424 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1425 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001426 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001427 * \retval #PSA_ERROR_STORAGE_FAILURE
1428 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001429 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001430 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001431 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001432 * The library has not been previously initialized by psa_crypto_init().
1433 * It is implementation-dependent whether a failure to initialize
1434 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001435 */
1436psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001437 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001438 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001439
Gilles Peskinedcd14942018-07-12 00:30:52 +02001440/** Add a message fragment to a multipart MAC operation.
1441 *
1442 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1443 * before calling this function.
1444 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001445 * If this function returns an error status, the operation enters an error
1446 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001447 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001448 * \param[in,out] operation Active MAC operation.
1449 * \param[in] input Buffer containing the message fragment to add to
1450 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001451 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001452 *
1453 * \retval #PSA_SUCCESS
1454 * Success.
1455 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001456 * The operation state is not valid (it must be active).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001457 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1458 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1459 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001460 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001461 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001462 * \retval #PSA_ERROR_BAD_STATE
1463 * The library has not been previously initialized by psa_crypto_init().
1464 * It is implementation-dependent whether a failure to initialize
1465 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001466 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001467psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1468 const uint8_t *input,
1469 size_t input_length);
1470
Gilles Peskinedcd14942018-07-12 00:30:52 +02001471/** Finish the calculation of the MAC of a message.
1472 *
1473 * The application must call psa_mac_sign_setup() before calling this function.
1474 * This function calculates the MAC of the message formed by concatenating
1475 * the inputs passed to preceding calls to psa_mac_update().
1476 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001477 * When this function returns successfuly, the operation becomes inactive.
1478 * If this function returns an error status, the operation enters an error
1479 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001480 *
1481 * \warning Applications should not call this function if they expect
1482 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1483 * Beware that comparing integrity or authenticity data such as
1484 * MAC values with a function such as \c memcmp is risky
1485 * because the time taken by the comparison may leak information
1486 * about the MAC value which could allow an attacker to guess
1487 * a valid MAC and thereby bypass security controls.
1488 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001489 * \param[in,out] operation Active MAC operation.
1490 * \param[out] mac Buffer where the MAC value is to be written.
1491 * \param mac_size Size of the \p mac buffer in bytes.
1492 * \param[out] mac_length On success, the number of bytes
1493 * that make up the MAC value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001494 * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001495 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001496 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001497 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001498 *
1499 * \retval #PSA_SUCCESS
1500 * Success.
1501 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001502 * The operation state is not valid (it must be an active mac sign
1503 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001504 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001505 * The size of the \p mac buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001506 * sufficient buffer size by calling PSA_MAC_LENGTH().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001507 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1508 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1509 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001510 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001511 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001512 * \retval #PSA_ERROR_BAD_STATE
1513 * The library has not been previously initialized by psa_crypto_init().
1514 * It is implementation-dependent whether a failure to initialize
1515 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001516 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001517psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1518 uint8_t *mac,
1519 size_t mac_size,
1520 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001521
Gilles Peskinedcd14942018-07-12 00:30:52 +02001522/** Finish the calculation of the MAC of a message and compare it with
1523 * an expected value.
1524 *
1525 * The application must call psa_mac_verify_setup() before calling this function.
1526 * This function calculates the MAC of the message formed by concatenating
1527 * the inputs passed to preceding calls to psa_mac_update(). It then
1528 * compares the calculated MAC with the expected MAC passed as a
1529 * parameter to this function.
1530 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001531 * When this function returns successfuly, the operation becomes inactive.
1532 * If this function returns an error status, the operation enters an error
1533 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001534 *
1535 * \note Implementations shall make the best effort to ensure that the
1536 * comparison between the actual MAC and the expected MAC is performed
1537 * in constant time.
1538 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001539 * \param[in,out] operation Active MAC operation.
1540 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001541 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001542 *
1543 * \retval #PSA_SUCCESS
1544 * The expected MAC is identical to the actual MAC of the message.
1545 * \retval #PSA_ERROR_INVALID_SIGNATURE
1546 * The MAC of the message was calculated successfully, but it
1547 * differs from the expected MAC.
1548 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001549 * The operation state is not valid (it must be an active mac verify
1550 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001551 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1552 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1553 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001554 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001555 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001556 * \retval #PSA_ERROR_BAD_STATE
1557 * The library has not been previously initialized by psa_crypto_init().
1558 * It is implementation-dependent whether a failure to initialize
1559 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001560 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001561psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1562 const uint8_t *mac,
1563 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001564
Gilles Peskinedcd14942018-07-12 00:30:52 +02001565/** Abort a MAC operation.
1566 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001567 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001568 * \p operation structure itself. Once aborted, the operation object
1569 * can be reused for another operation by calling
1570 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001571 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001572 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001573 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001574 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001575 * In particular, calling psa_mac_abort() after the operation has been
1576 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1577 * psa_mac_verify_finish() is safe and has no effect.
1578 *
1579 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001580 *
1581 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02001582 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1583 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001584 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001585 * \retval #PSA_ERROR_BAD_STATE
1586 * The library has not been previously initialized by psa_crypto_init().
1587 * It is implementation-dependent whether a failure to initialize
1588 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001589 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001590psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1591
1592/**@}*/
1593
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001594/** \defgroup cipher Symmetric ciphers
1595 * @{
1596 */
1597
Gilles Peskine69647a42019-01-14 20:18:12 +01001598/** Encrypt a message using a symmetric cipher.
1599 *
1600 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001601 * vector). Use the multipart operation interface with a
1602 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001603 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001604 * \param key Identifier of the key to use for the operation.
Ronald Cron96783552020-10-19 12:06:30 +02001605 * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001606 * \param alg The cipher algorithm to compute
1607 * (\c PSA_ALG_XXX value such that
1608 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1609 * \param[in] input Buffer containing the message to encrypt.
1610 * \param input_length Size of the \p input buffer in bytes.
1611 * \param[out] output Buffer where the output is to be written.
1612 * The output contains the IV followed by
1613 * the ciphertext proper.
1614 * \param output_size Size of the \p output buffer in bytes.
1615 * \param[out] output_length On success, the number of bytes
1616 * that make up the output.
1617 *
1618 * \retval #PSA_SUCCESS
1619 * Success.
1620 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001621 * \retval #PSA_ERROR_NOT_PERMITTED
1622 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001623 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001624 * \retval #PSA_ERROR_NOT_SUPPORTED
1625 * \p alg is not supported or is not a cipher algorithm.
1626 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1627 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1628 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1629 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001630 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001631 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001632 * \retval #PSA_ERROR_BAD_STATE
1633 * The library has not been previously initialized by psa_crypto_init().
1634 * It is implementation-dependent whether a failure to initialize
1635 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001636 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001637psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001638 psa_algorithm_t alg,
1639 const uint8_t *input,
1640 size_t input_length,
1641 uint8_t *output,
1642 size_t output_size,
1643 size_t *output_length);
1644
1645/** Decrypt a message using a symmetric cipher.
1646 *
1647 * This function decrypts a message encrypted with a symmetric cipher.
1648 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001649 * \param key Identifier of the key to use for the operation.
Gilles Peskine69647a42019-01-14 20:18:12 +01001650 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001651 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001652 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001653 * \param alg The cipher algorithm to compute
1654 * (\c PSA_ALG_XXX value such that
1655 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1656 * \param[in] input Buffer containing the message to decrypt.
1657 * This consists of the IV followed by the
1658 * ciphertext proper.
1659 * \param input_length Size of the \p input buffer in bytes.
1660 * \param[out] output Buffer where the plaintext is to be written.
1661 * \param output_size Size of the \p output buffer in bytes.
1662 * \param[out] output_length On success, the number of bytes
1663 * that make up the output.
1664 *
1665 * \retval #PSA_SUCCESS
1666 * Success.
1667 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001668 * \retval #PSA_ERROR_NOT_PERMITTED
1669 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001670 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001671 * \retval #PSA_ERROR_NOT_SUPPORTED
1672 * \p alg is not supported or is not a cipher algorithm.
1673 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1674 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1675 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1676 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001677 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001678 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001679 * \retval #PSA_ERROR_BAD_STATE
1680 * The library has not been previously initialized by psa_crypto_init().
1681 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001682 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001683 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001684psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001685 psa_algorithm_t alg,
1686 const uint8_t *input,
1687 size_t input_length,
1688 uint8_t *output,
1689 size_t output_size,
1690 size_t *output_length);
1691
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001692/** The type of the state data structure for multipart cipher operations.
1693 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001694 * Before calling any function on a cipher operation object, the application
1695 * must initialize it by any of the following means:
1696 * - Set the structure to all-bits-zero, for example:
1697 * \code
1698 * psa_cipher_operation_t operation;
1699 * memset(&operation, 0, sizeof(operation));
1700 * \endcode
1701 * - Initialize the structure to logical zero values, for example:
1702 * \code
1703 * psa_cipher_operation_t operation = {0};
1704 * \endcode
1705 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1706 * for example:
1707 * \code
1708 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1709 * \endcode
1710 * - Assign the result of the function psa_cipher_operation_init()
1711 * to the structure, for example:
1712 * \code
1713 * psa_cipher_operation_t operation;
1714 * operation = psa_cipher_operation_init();
1715 * \endcode
1716 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001717 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01001718 * make any assumptions about the content of this structure.
1719 * Implementation details can change in future versions without notice. */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001720typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1721
Jaeden Amero5bae2272019-01-04 11:48:27 +00001722/** \def PSA_CIPHER_OPERATION_INIT
1723 *
1724 * This macro returns a suitable initializer for a cipher operation object of
1725 * type #psa_cipher_operation_t.
1726 */
Jaeden Amero5bae2272019-01-04 11:48:27 +00001727
1728/** Return an initial value for a cipher operation object.
1729 */
1730static psa_cipher_operation_t psa_cipher_operation_init(void);
1731
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001732/** Set the key for a multipart symmetric encryption operation.
1733 *
1734 * The sequence of operations to encrypt a message with a symmetric cipher
1735 * is as follows:
1736 * -# Allocate an operation object which will be passed to all the functions
1737 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001738 * -# Initialize the operation object with one of the methods described in the
1739 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001740 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001741 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001742 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001743 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001744 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001745 * requires a specific IV value.
1746 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1747 * of the message each time.
1748 * -# Call psa_cipher_finish().
1749 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001750 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1751 * the operation will need to be reset by a call to psa_cipher_abort(). The
1752 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001753 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001754 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001755 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001756 * eventually terminate the operation. The following events terminate an
1757 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001758 * - A successful call to psa_cipher_finish().
1759 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001760 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001761 * \param[in,out] operation The operation object to set up. It must have
1762 * been initialized as per the documentation for
1763 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001764 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001765 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001766 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001767 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001768 * \param alg The cipher algorithm to compute
1769 * (\c PSA_ALG_XXX value such that
1770 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001771 *
Gilles Peskine28538492018-07-11 17:34:00 +02001772 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001773 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001774 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001775 * \retval #PSA_ERROR_NOT_PERMITTED
1776 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001777 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001778 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001779 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001780 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1781 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1782 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001783 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001784 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001785 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001786 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001787 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001788 * The library has not been previously initialized by psa_crypto_init().
1789 * It is implementation-dependent whether a failure to initialize
1790 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001791 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001792psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001793 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001794 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001795
1796/** Set the key for a multipart symmetric decryption operation.
1797 *
1798 * The sequence of operations to decrypt a message with a symmetric cipher
1799 * is as follows:
1800 * -# Allocate an operation object which will be passed to all the functions
1801 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001802 * -# Initialize the operation object with one of the methods described in the
1803 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001804 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001805 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001806 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001807 * decryption. If the IV is prepended to the ciphertext, you can call
1808 * psa_cipher_update() on a buffer containing the IV followed by the
1809 * beginning of the message.
1810 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1811 * of the message each time.
1812 * -# Call psa_cipher_finish().
1813 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001814 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1815 * the operation will need to be reset by a call to psa_cipher_abort(). The
1816 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001817 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001818 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001819 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001820 * eventually terminate the operation. The following events terminate an
1821 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001822 * - A successful call to psa_cipher_finish().
1823 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001824 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001825 * \param[in,out] operation The operation object to set up. It must have
1826 * been initialized as per the documentation for
1827 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001828 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001829 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001830 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001831 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001832 * \param alg The cipher algorithm to compute
1833 * (\c PSA_ALG_XXX value such that
1834 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001835 *
Gilles Peskine28538492018-07-11 17:34:00 +02001836 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001837 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001838 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001839 * \retval #PSA_ERROR_NOT_PERMITTED
1840 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001841 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001842 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001843 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001844 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1845 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1846 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001847 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001848 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001849 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001850 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001851 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001852 * The library has not been previously initialized by psa_crypto_init().
1853 * It is implementation-dependent whether a failure to initialize
1854 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001855 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001856psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001857 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001858 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001859
Gilles Peskinedcd14942018-07-12 00:30:52 +02001860/** Generate an IV for a symmetric encryption operation.
1861 *
1862 * This function generates a random IV (initialization vector), nonce
1863 * or initial counter value for the encryption operation as appropriate
1864 * for the chosen algorithm, key type and key size.
1865 *
1866 * The application must call psa_cipher_encrypt_setup() before
1867 * calling this function.
1868 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001869 * If this function returns an error status, the operation enters an error
1870 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001871 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001872 * \param[in,out] operation Active cipher operation.
1873 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001874 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001875 * \param[out] iv_length On success, the number of bytes of the
1876 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001877 *
1878 * \retval #PSA_SUCCESS
1879 * Success.
1880 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001881 * The operation state is not valid (it must be active, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001882 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001883 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001884 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1885 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1886 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001887 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001888 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001889 * \retval #PSA_ERROR_BAD_STATE
1890 * The library has not been previously initialized by psa_crypto_init().
1891 * It is implementation-dependent whether a failure to initialize
1892 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001893 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001894psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001895 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001896 size_t iv_size,
1897 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001898
Gilles Peskinedcd14942018-07-12 00:30:52 +02001899/** Set the IV for a symmetric encryption or decryption operation.
1900 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001901 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001902 * or initial counter value for the encryption or decryption operation.
1903 *
1904 * The application must call psa_cipher_encrypt_setup() before
1905 * calling this function.
1906 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001907 * If this function returns an error status, the operation enters an error
1908 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001909 *
1910 * \note When encrypting, applications should use psa_cipher_generate_iv()
1911 * instead of this function, unless implementing a protocol that requires
1912 * a non-random IV.
1913 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001914 * \param[in,out] operation Active cipher operation.
1915 * \param[in] iv Buffer containing the IV to use.
1916 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001917 *
1918 * \retval #PSA_SUCCESS
1919 * Success.
1920 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001921 * The operation state is not valid (it must be an active cipher
1922 * encrypt operation, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001923 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001924 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001925 * or the chosen algorithm does not use an IV.
1926 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1927 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1928 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001929 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001930 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001931 * \retval #PSA_ERROR_BAD_STATE
1932 * The library has not been previously initialized by psa_crypto_init().
1933 * It is implementation-dependent whether a failure to initialize
1934 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001935 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001936psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001937 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001938 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001939
Gilles Peskinedcd14942018-07-12 00:30:52 +02001940/** Encrypt or decrypt a message fragment in an active cipher operation.
1941 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001942 * Before calling this function, you must:
1943 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1944 * The choice of setup function determines whether this function
1945 * encrypts or decrypts its input.
1946 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1947 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001948 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001949 * If this function returns an error status, the operation enters an error
1950 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001951 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001952 * \param[in,out] operation Active cipher operation.
1953 * \param[in] input Buffer containing the message fragment to
1954 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001955 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001956 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001957 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001958 * \param[out] output_length On success, the number of bytes
1959 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001960 *
1961 * \retval #PSA_SUCCESS
1962 * Success.
1963 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001964 * The operation state is not valid (it must be active, with an IV set
1965 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001966 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1967 * The size of the \p output buffer is too small.
1968 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1969 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1970 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001971 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001972 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001973 * \retval #PSA_ERROR_BAD_STATE
1974 * The library has not been previously initialized by psa_crypto_init().
1975 * It is implementation-dependent whether a failure to initialize
1976 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001977 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001978psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1979 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001980 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001981 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001982 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001983 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001984
Gilles Peskinedcd14942018-07-12 00:30:52 +02001985/** Finish encrypting or decrypting a message in a cipher operation.
1986 *
1987 * The application must call psa_cipher_encrypt_setup() or
1988 * psa_cipher_decrypt_setup() before calling this function. The choice
1989 * of setup function determines whether this function encrypts or
1990 * decrypts its input.
1991 *
1992 * This function finishes the encryption or decryption of the message
1993 * formed by concatenating the inputs passed to preceding calls to
1994 * psa_cipher_update().
1995 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001996 * When this function returns successfuly, the operation becomes inactive.
1997 * If this function returns an error status, the operation enters an error
1998 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001999 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002000 * \param[in,out] operation Active cipher operation.
2001 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002002 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002003 * \param[out] output_length On success, the number of bytes
2004 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002005 *
2006 * \retval #PSA_SUCCESS
2007 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02002008 * \retval #PSA_ERROR_INVALID_ARGUMENT
2009 * The total input size passed to this operation is not valid for
2010 * this particular algorithm. For example, the algorithm is a based
2011 * on block cipher and requires a whole number of blocks, but the
2012 * total input size is not a multiple of the block size.
2013 * \retval #PSA_ERROR_INVALID_PADDING
2014 * This is a decryption operation for an algorithm that includes
2015 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002016 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002017 * The operation state is not valid (it must be active, with an IV set
2018 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02002019 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2020 * The size of the \p output buffer is too small.
2021 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2022 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2023 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002024 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002025 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002026 * \retval #PSA_ERROR_BAD_STATE
2027 * The library has not been previously initialized by psa_crypto_init().
2028 * It is implementation-dependent whether a failure to initialize
2029 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002030 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002031psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002032 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002033 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002034 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002035
Gilles Peskinedcd14942018-07-12 00:30:52 +02002036/** Abort a cipher operation.
2037 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002038 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002039 * \p operation structure itself. Once aborted, the operation object
2040 * can be reused for another operation by calling
2041 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002042 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002043 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002044 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002045 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002046 * In particular, calling psa_cipher_abort() after the operation has been
2047 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2048 * is safe and has no effect.
2049 *
2050 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002051 *
2052 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02002053 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2054 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002055 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002056 * \retval #PSA_ERROR_BAD_STATE
2057 * The library has not been previously initialized by psa_crypto_init().
2058 * It is implementation-dependent whether a failure to initialize
2059 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002060 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002061psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2062
2063/**@}*/
2064
Gilles Peskine3b555712018-03-03 21:27:57 +01002065/** \defgroup aead Authenticated encryption with associated data (AEAD)
2066 * @{
2067 */
2068
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002069/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002070 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002071 * \param key Identifier of the key to use for the
2072 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002073 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002074 * \param alg The AEAD algorithm to compute
2075 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002076 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002077 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002078 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002079 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002080 * but not encrypted.
2081 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002082 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002083 * encrypted.
2084 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002085 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002086 * encrypted data. The additional data is not
2087 * part of this output. For algorithms where the
2088 * encrypted data and the authentication tag
2089 * are defined as separate outputs, the
2090 * authentication tag is appended to the
2091 * encrypted data.
2092 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002093 * This must be appropriate for the selected
2094 * algorithm and key:
2095 * - A sufficient output size is
2096 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type,
2097 * \p alg, \p plaintext_length) where
2098 * \c key_type is the type of \p key.
2099 * - #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p
2100 * plaintext_length) evaluates to the maximum
2101 * ciphertext size of any supported AEAD
2102 * encryption.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002103 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002104 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002105 *
Gilles Peskine28538492018-07-11 17:34:00 +02002106 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002107 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002108 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002109 * \retval #PSA_ERROR_NOT_PERMITTED
2110 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002111 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002112 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002113 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002114 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002115 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Bence Szépkútibd98df72021-04-27 04:37:18 +02002116 * \p ciphertext_size is too small.
2117 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2118 * \p plaintext_length) or
2119 * #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length) can be used to
2120 * determine the required buffer size.
Gilles Peskine28538492018-07-11 17:34:00 +02002121 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2122 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002123 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002124 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002125 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002126 * The library has not been previously initialized by psa_crypto_init().
2127 * It is implementation-dependent whether a failure to initialize
2128 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002129 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002130psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002131 psa_algorithm_t alg,
2132 const uint8_t *nonce,
2133 size_t nonce_length,
2134 const uint8_t *additional_data,
2135 size_t additional_data_length,
2136 const uint8_t *plaintext,
2137 size_t plaintext_length,
2138 uint8_t *ciphertext,
2139 size_t ciphertext_size,
2140 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002141
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002142/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002143 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002144 * \param key Identifier of the key to use for the
2145 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002146 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002147 * \param alg The AEAD algorithm to compute
2148 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002149 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002150 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002151 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002152 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002153 * but not encrypted.
2154 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002155 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002156 * encrypted. For algorithms where the
2157 * encrypted data and the authentication tag
2158 * are defined as separate inputs, the buffer
2159 * must contain the encrypted data followed
2160 * by the authentication tag.
2161 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002162 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002163 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002164 * This must be appropriate for the selected
2165 * algorithm and key:
2166 * - A sufficient output size is
2167 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type,
2168 * \p alg, \p ciphertext_length) where
2169 * \c key_type is the type of \p key.
2170 * - #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p
2171 * ciphertext_length) evaluates to the maximum
2172 * plaintext size of any supported AEAD
2173 * decryption.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002174 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002175 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002176 *
Gilles Peskine28538492018-07-11 17:34:00 +02002177 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002178 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002179 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002180 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002181 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002182 * \retval #PSA_ERROR_NOT_PERMITTED
2183 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002184 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002185 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002186 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002187 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002188 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Bence Szépkútibd98df72021-04-27 04:37:18 +02002189 * \p plaintext_size is too small.
2190 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2191 * \p ciphertext_length) or
2192 * #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length) can be used
2193 * to determine the required buffer size.
Gilles Peskine28538492018-07-11 17:34:00 +02002194 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2195 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002196 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002197 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002198 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002199 * The library has not been previously initialized by psa_crypto_init().
2200 * It is implementation-dependent whether a failure to initialize
2201 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002202 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002203psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002204 psa_algorithm_t alg,
2205 const uint8_t *nonce,
2206 size_t nonce_length,
2207 const uint8_t *additional_data,
2208 size_t additional_data_length,
2209 const uint8_t *ciphertext,
2210 size_t ciphertext_length,
2211 uint8_t *plaintext,
2212 size_t plaintext_size,
2213 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002214
Gilles Peskine30a9e412019-01-14 18:36:12 +01002215/** The type of the state data structure for multipart AEAD operations.
2216 *
2217 * Before calling any function on an AEAD operation object, the application
2218 * must initialize it by any of the following means:
2219 * - Set the structure to all-bits-zero, for example:
2220 * \code
2221 * psa_aead_operation_t operation;
2222 * memset(&operation, 0, sizeof(operation));
2223 * \endcode
2224 * - Initialize the structure to logical zero values, for example:
2225 * \code
2226 * psa_aead_operation_t operation = {0};
2227 * \endcode
2228 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2229 * for example:
2230 * \code
2231 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2232 * \endcode
2233 * - Assign the result of the function psa_aead_operation_init()
2234 * to the structure, for example:
2235 * \code
2236 * psa_aead_operation_t operation;
2237 * operation = psa_aead_operation_init();
2238 * \endcode
2239 *
2240 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01002241 * make any assumptions about the content of this structure.
2242 * Implementation details can change in future versions without notice. */
Gilles Peskine30a9e412019-01-14 18:36:12 +01002243typedef struct psa_aead_operation_s psa_aead_operation_t;
2244
2245/** \def PSA_AEAD_OPERATION_INIT
2246 *
2247 * This macro returns a suitable initializer for an AEAD operation object of
2248 * type #psa_aead_operation_t.
2249 */
Gilles Peskine30a9e412019-01-14 18:36:12 +01002250
2251/** Return an initial value for an AEAD operation object.
2252 */
2253static psa_aead_operation_t psa_aead_operation_init(void);
2254
2255/** Set the key for a multipart authenticated encryption operation.
2256 *
2257 * The sequence of operations to encrypt a message with authentication
2258 * is as follows:
2259 * -# Allocate an operation object which will be passed to all the functions
2260 * listed here.
2261 * -# Initialize the operation object with one of the methods described in the
2262 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002263 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002264 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002265 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2266 * inputs to the subsequent calls to psa_aead_update_ad() and
2267 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2268 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002269 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2270 * generate or set the nonce. You should use
2271 * psa_aead_generate_nonce() unless the protocol you are implementing
2272 * requires a specific nonce value.
2273 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2274 * of the non-encrypted additional authenticated data each time.
2275 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002276 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002277 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002278 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002279 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2280 * the operation will need to be reset by a call to psa_aead_abort(). The
2281 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002282 * has been initialized.
2283 *
2284 * After a successful call to psa_aead_encrypt_setup(), the application must
2285 * eventually terminate the operation. The following events terminate an
2286 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002287 * - A successful call to psa_aead_finish().
2288 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002289 *
2290 * \param[in,out] operation The operation object to set up. It must have
2291 * been initialized as per the documentation for
2292 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002293 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002294 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002295 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002296 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002297 * \param alg The AEAD algorithm to compute
2298 * (\c PSA_ALG_XXX value such that
2299 * #PSA_ALG_IS_AEAD(\p alg) is true).
2300 *
2301 * \retval #PSA_SUCCESS
2302 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002303 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002304 * The operation state is not valid (it must be inactive).
Ronald Cron96783552020-10-19 12:06:30 +02002305 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002306 * \retval #PSA_ERROR_NOT_PERMITTED
2307 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002308 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002309 * \retval #PSA_ERROR_NOT_SUPPORTED
2310 * \p alg is not supported or is not an AEAD algorithm.
2311 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2312 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2313 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002314 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002315 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002316 * \retval #PSA_ERROR_BAD_STATE
2317 * The library has not been previously initialized by psa_crypto_init().
2318 * It is implementation-dependent whether a failure to initialize
2319 * results in this error code.
2320 */
2321psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002322 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002323 psa_algorithm_t alg);
2324
2325/** Set the key for a multipart authenticated decryption operation.
2326 *
2327 * The sequence of operations to decrypt a message with authentication
2328 * is as follows:
2329 * -# Allocate an operation object which will be passed to all the functions
2330 * listed here.
2331 * -# Initialize the operation object with one of the methods described in the
2332 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002333 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002334 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002335 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2336 * inputs to the subsequent calls to psa_aead_update_ad() and
2337 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2338 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002339 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2340 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2341 * of the non-encrypted additional authenticated data each time.
2342 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002343 * of the ciphertext to decrypt each time.
2344 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002345 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002346 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2347 * the operation will need to be reset by a call to psa_aead_abort(). The
2348 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002349 * has been initialized.
2350 *
2351 * After a successful call to psa_aead_decrypt_setup(), the application must
2352 * eventually terminate the operation. The following events terminate an
2353 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002354 * - A successful call to psa_aead_verify().
2355 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002356 *
2357 * \param[in,out] operation The operation object to set up. It must have
2358 * been initialized as per the documentation for
2359 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002360 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002361 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002362 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002363 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002364 * \param alg The AEAD algorithm to compute
2365 * (\c PSA_ALG_XXX value such that
2366 * #PSA_ALG_IS_AEAD(\p alg) is true).
2367 *
2368 * \retval #PSA_SUCCESS
2369 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002370 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002371 * The operation state is not valid (it must be inactive).
Ronald Cron96783552020-10-19 12:06:30 +02002372 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002373 * \retval #PSA_ERROR_NOT_PERMITTED
2374 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002375 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002376 * \retval #PSA_ERROR_NOT_SUPPORTED
2377 * \p alg is not supported or is not an AEAD algorithm.
2378 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2379 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2380 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002381 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002382 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002383 * \retval #PSA_ERROR_BAD_STATE
2384 * The library has not been previously initialized by psa_crypto_init().
2385 * It is implementation-dependent whether a failure to initialize
2386 * results in this error code.
2387 */
2388psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002389 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002390 psa_algorithm_t alg);
2391
2392/** Generate a random nonce for an authenticated encryption operation.
2393 *
2394 * This function generates a random nonce for the authenticated encryption
2395 * operation with an appropriate size for the chosen algorithm, key type
2396 * and key size.
2397 *
2398 * The application must call psa_aead_encrypt_setup() before
2399 * calling this function.
2400 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002401 * If this function returns an error status, the operation enters an error
2402 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002403 *
2404 * \param[in,out] operation Active AEAD operation.
2405 * \param[out] nonce Buffer where the generated nonce is to be
2406 * written.
2407 * \param nonce_size Size of the \p nonce buffer in bytes.
2408 * \param[out] nonce_length On success, the number of bytes of the
2409 * generated nonce.
2410 *
2411 * \retval #PSA_SUCCESS
2412 * Success.
2413 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002414 * The operation state is not valid (it must be an active aead encrypt
Ronald Cron96783552020-10-19 12:06:30 +02002415 * operation, with no nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002416 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2417 * The size of the \p nonce buffer is too small.
2418 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2419 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2420 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002421 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002422 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002423 * \retval #PSA_ERROR_BAD_STATE
2424 * The library has not been previously initialized by psa_crypto_init().
2425 * It is implementation-dependent whether a failure to initialize
2426 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002427 */
2428psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002429 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002430 size_t nonce_size,
2431 size_t *nonce_length);
2432
2433/** Set the nonce for an authenticated encryption or decryption operation.
2434 *
2435 * This function sets the nonce for the authenticated
2436 * encryption or decryption operation.
2437 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002438 * The application must call psa_aead_encrypt_setup() or
2439 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002440 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002441 * If this function returns an error status, the operation enters an error
2442 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002443 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002444 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002445 * instead of this function, unless implementing a protocol that requires
2446 * a non-random IV.
2447 *
2448 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002449 * \param[in] nonce Buffer containing the nonce to use.
2450 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002451 *
2452 * \retval #PSA_SUCCESS
2453 * Success.
2454 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002455 * The operation state is not valid (it must be active, with no nonce
2456 * set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002457 * \retval #PSA_ERROR_INVALID_ARGUMENT
2458 * The size of \p nonce is not acceptable for the chosen algorithm.
2459 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2460 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2461 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002462 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002463 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002464 * \retval #PSA_ERROR_BAD_STATE
2465 * The library has not been previously initialized by psa_crypto_init().
2466 * It is implementation-dependent whether a failure to initialize
2467 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002468 */
2469psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002470 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002471 size_t nonce_length);
2472
Gilles Peskinebc59c852019-01-17 15:26:08 +01002473/** Declare the lengths of the message and additional data for AEAD.
2474 *
2475 * The application must call this function before calling
2476 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2477 * the operation requires it. If the algorithm does not require it,
2478 * calling this function is optional, but if this function is called
2479 * then the implementation must enforce the lengths.
2480 *
2481 * You may call this function before or after setting the nonce with
2482 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2483 *
2484 * - For #PSA_ALG_CCM, calling this function is required.
2485 * - For the other AEAD algorithms defined in this specification, calling
2486 * this function is not required.
2487 * - For vendor-defined algorithm, refer to the vendor documentation.
2488 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002489 * If this function returns an error status, the operation enters an error
2490 * state and must be aborted by calling psa_aead_abort().
2491 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002492 * \param[in,out] operation Active AEAD operation.
2493 * \param ad_length Size of the non-encrypted additional
2494 * authenticated data in bytes.
2495 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2496 *
2497 * \retval #PSA_SUCCESS
2498 * Success.
2499 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002500 * The operation state is not valid (it must be active, and
2501 * psa_aead_update_ad() and psa_aead_update() must not have been
2502 * called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002503 * \retval #PSA_ERROR_INVALID_ARGUMENT
2504 * At least one of the lengths is not acceptable for the chosen
2505 * algorithm.
2506 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2507 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2508 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002509 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002510 * \retval #PSA_ERROR_BAD_STATE
2511 * The library has not been previously initialized by psa_crypto_init().
2512 * It is implementation-dependent whether a failure to initialize
2513 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002514 */
2515psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2516 size_t ad_length,
2517 size_t plaintext_length);
2518
Gilles Peskine30a9e412019-01-14 18:36:12 +01002519/** Pass additional data to an active AEAD operation.
2520 *
2521 * Additional data is authenticated, but not encrypted.
2522 *
2523 * You may call this function multiple times to pass successive fragments
2524 * of the additional data. You may not call this function after passing
2525 * data to encrypt or decrypt with psa_aead_update().
2526 *
2527 * Before calling this function, you must:
2528 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2529 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2530 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002531 * If this function returns an error status, the operation enters an error
2532 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002533 *
2534 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2535 * there is no guarantee that the input is valid. Therefore, until
2536 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2537 * treat the input as untrusted and prepare to undo any action that
2538 * depends on the input if psa_aead_verify() returns an error status.
2539 *
2540 * \param[in,out] operation Active AEAD operation.
2541 * \param[in] input Buffer containing the fragment of
2542 * additional data.
2543 * \param input_length Size of the \p input buffer in bytes.
2544 *
2545 * \retval #PSA_SUCCESS
2546 * Success.
2547 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002548 * The operation state is not valid (it must be active, have a nonce
2549 * set, have lengths set if required by the algorithm, and
2550 * psa_aead_update() must not have been called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002551 * \retval #PSA_ERROR_INVALID_ARGUMENT
2552 * The total input length overflows the additional data length that
2553 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002554 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2555 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2556 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002557 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002558 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002559 * \retval #PSA_ERROR_BAD_STATE
2560 * The library has not been previously initialized by psa_crypto_init().
2561 * It is implementation-dependent whether a failure to initialize
2562 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002563 */
2564psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2565 const uint8_t *input,
2566 size_t input_length);
2567
2568/** Encrypt or decrypt a message fragment in an active AEAD operation.
2569 *
2570 * Before calling this function, you must:
2571 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2572 * The choice of setup function determines whether this function
2573 * encrypts or decrypts its input.
2574 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2575 * 3. Call psa_aead_update_ad() to pass all the additional data.
2576 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002577 * If this function returns an error status, the operation enters an error
2578 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002579 *
2580 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2581 * there is no guarantee that the input is valid. Therefore, until
2582 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2583 * - Do not use the output in any way other than storing it in a
2584 * confidential location. If you take any action that depends
2585 * on the tentative decrypted data, this action will need to be
2586 * undone if the input turns out not to be valid. Furthermore,
2587 * if an adversary can observe that this action took place
2588 * (for example through timing), they may be able to use this
2589 * fact as an oracle to decrypt any message encrypted with the
2590 * same key.
2591 * - In particular, do not copy the output anywhere but to a
2592 * memory or storage space that you have exclusive access to.
2593 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002594 * This function does not require the input to be aligned to any
2595 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002596 * a whole block at a time, it must consume all the input provided, but
2597 * it may delay the end of the corresponding output until a subsequent
2598 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2599 * provides sufficient input. The amount of data that can be delayed
2600 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002601 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002602 * \param[in,out] operation Active AEAD operation.
2603 * \param[in] input Buffer containing the message fragment to
2604 * encrypt or decrypt.
2605 * \param input_length Size of the \p input buffer in bytes.
2606 * \param[out] output Buffer where the output is to be written.
2607 * \param output_size Size of the \p output buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002608 * This must be appropriate for the selected
2609 * algorithm and key:
2610 * - A sufficient output size is
2611 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,
2612 * \c alg, \p input_length) where
2613 * \c key_type is the type of key and \c alg is
2614 * the algorithm that were used to set up the
2615 * operation.
2616 * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p
2617 * input_length) evaluates to the maximum
2618 * output size of any supported AEAD
2619 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002620 * \param[out] output_length On success, the number of bytes
2621 * that make up the returned output.
2622 *
2623 * \retval #PSA_SUCCESS
2624 * Success.
2625 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002626 * The operation state is not valid (it must be active, have a nonce
2627 * set, and have lengths set if required by the algorithm).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002628 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2629 * The size of the \p output buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002630 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or
2631 * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to
2632 * determine the required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002633 * \retval #PSA_ERROR_INVALID_ARGUMENT
2634 * The total length of input to psa_aead_update_ad() so far is
2635 * less than the additional data length that was previously
2636 * specified with psa_aead_set_lengths().
2637 * \retval #PSA_ERROR_INVALID_ARGUMENT
2638 * The total input length overflows the plaintext length that
2639 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002640 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2641 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2642 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002643 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002644 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002645 * \retval #PSA_ERROR_BAD_STATE
2646 * The library has not been previously initialized by psa_crypto_init().
2647 * It is implementation-dependent whether a failure to initialize
2648 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002649 */
2650psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2651 const uint8_t *input,
2652 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002653 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002654 size_t output_size,
2655 size_t *output_length);
2656
2657/** Finish encrypting a message in an AEAD operation.
2658 *
2659 * The operation must have been set up with psa_aead_encrypt_setup().
2660 *
2661 * This function finishes the authentication of the additional data
2662 * formed by concatenating the inputs passed to preceding calls to
2663 * psa_aead_update_ad() with the plaintext formed by concatenating the
2664 * inputs passed to preceding calls to psa_aead_update().
2665 *
2666 * This function has two output buffers:
2667 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002668 * preceding calls to psa_aead_update().
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002669 * - \p tag contains the authentication tag.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002670 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002671 * When this function returns successfuly, the operation becomes inactive.
2672 * If this function returns an error status, the operation enters an error
2673 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002674 *
2675 * \param[in,out] operation Active AEAD operation.
2676 * \param[out] ciphertext Buffer where the last part of the ciphertext
2677 * is to be written.
2678 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002679 * This must be appropriate for the selected
2680 * algorithm and key:
2681 * - A sufficient output size is
2682 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type,
2683 * \c alg) where \c key_type is the type of key
2684 * and \c alg is the algorithm that were used to
2685 * set up the operation.
2686 * - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to
2687 * the maximum output size of any supported AEAD
2688 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002689 * \param[out] ciphertext_length On success, the number of bytes of
2690 * returned ciphertext.
2691 * \param[out] tag Buffer where the authentication tag is
2692 * to be written.
2693 * \param tag_size Size of the \p tag buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002694 * This must be appropriate for the selected
2695 * algorithm and key:
2696 * - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c
2697 * key_type, \c key_bits, \c alg) where
2698 * \c key_type and \c key_bits are the type and
2699 * bit-size of the key, and \c alg is the
2700 * algorithm that were used in the call to
2701 * psa_aead_encrypt_setup().
2702 * - #PSA_AEAD_TAG_MAX_SIZE evaluates to the
2703 * maximum tag size of any supported AEAD
2704 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002705 * \param[out] tag_length On success, the number of bytes
2706 * that make up the returned tag.
2707 *
2708 * \retval #PSA_SUCCESS
2709 * Success.
2710 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002711 * The operation state is not valid (it must be an active encryption
2712 * operation with a nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002713 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002714 * The size of the \p ciphertext or \p tag buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002715 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or
2716 * #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the
2717 * required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type,
2718 * \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to
2719 * determine the required \p tag buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002720 * \retval #PSA_ERROR_INVALID_ARGUMENT
2721 * The total length of input to psa_aead_update_ad() so far is
2722 * less than the additional data length that was previously
2723 * specified with psa_aead_set_lengths().
2724 * \retval #PSA_ERROR_INVALID_ARGUMENT
2725 * The total length of input to psa_aead_update() so far is
2726 * less than the plaintext length that was previously
2727 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002728 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2729 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2730 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002731 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002732 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002733 * \retval #PSA_ERROR_BAD_STATE
2734 * The library has not been previously initialized by psa_crypto_init().
2735 * It is implementation-dependent whether a failure to initialize
2736 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002737 */
2738psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002739 uint8_t *ciphertext,
2740 size_t ciphertext_size,
2741 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002742 uint8_t *tag,
2743 size_t tag_size,
2744 size_t *tag_length);
2745
2746/** Finish authenticating and decrypting a message in an AEAD operation.
2747 *
2748 * The operation must have been set up with psa_aead_decrypt_setup().
2749 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002750 * This function finishes the authenticated decryption of the message
2751 * components:
2752 *
2753 * - The additional data consisting of the concatenation of the inputs
2754 * passed to preceding calls to psa_aead_update_ad().
2755 * - The ciphertext consisting of the concatenation of the inputs passed to
2756 * preceding calls to psa_aead_update().
2757 * - The tag passed to this function call.
2758 *
2759 * If the authentication tag is correct, this function outputs any remaining
2760 * plaintext and reports success. If the authentication tag is not correct,
2761 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002762 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002763 * When this function returns successfuly, the operation becomes inactive.
2764 * If this function returns an error status, the operation enters an error
2765 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002766 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002767 * \note Implementations shall make the best effort to ensure that the
2768 * comparison between the actual tag and the expected tag is performed
2769 * in constant time.
2770 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002771 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002772 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002773 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002774 * from previous calls to psa_aead_update()
2775 * that could not be processed until the end
2776 * of the input.
2777 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002778 * This must be appropriate for the selected algorithm and key:
2779 * - A sufficient output size is
2780 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type,
2781 * \c alg) where \c key_type is the type of key
2782 * and \c alg is the algorithm that were used to
2783 * set up the operation.
2784 * - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to
2785 * the maximum output size of any supported AEAD
2786 * algorithm.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002787 * \param[out] plaintext_length On success, the number of bytes of
2788 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002789 * \param[in] tag Buffer containing the authentication tag.
2790 * \param tag_length Size of the \p tag buffer in bytes.
2791 *
2792 * \retval #PSA_SUCCESS
2793 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002794 * \retval #PSA_ERROR_INVALID_SIGNATURE
2795 * The calculations were successful, but the authentication tag is
2796 * not correct.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002797 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002798 * The operation state is not valid (it must be an active decryption
2799 * operation with a nonce set).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002800 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2801 * The size of the \p plaintext buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002802 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or
2803 * #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the
2804 * required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002805 * \retval #PSA_ERROR_INVALID_ARGUMENT
2806 * The total length of input to psa_aead_update_ad() so far is
2807 * less than the additional data length that was previously
2808 * specified with psa_aead_set_lengths().
2809 * \retval #PSA_ERROR_INVALID_ARGUMENT
2810 * The total length of input to psa_aead_update() so far is
2811 * less than the plaintext length that was previously
2812 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002813 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2814 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2815 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002816 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002817 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002818 * \retval #PSA_ERROR_BAD_STATE
2819 * The library has not been previously initialized by psa_crypto_init().
2820 * It is implementation-dependent whether a failure to initialize
2821 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002822 */
2823psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002824 uint8_t *plaintext,
2825 size_t plaintext_size,
2826 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002827 const uint8_t *tag,
2828 size_t tag_length);
2829
2830/** Abort an AEAD operation.
2831 *
2832 * Aborting an operation frees all associated resources except for the
2833 * \p operation structure itself. Once aborted, the operation object
2834 * can be reused for another operation by calling
2835 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2836 *
2837 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002838 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002839 *
2840 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002841 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2842 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002843 *
2844 * \param[in,out] operation Initialized AEAD operation.
2845 *
2846 * \retval #PSA_SUCCESS
Gilles Peskine30a9e412019-01-14 18:36:12 +01002847 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2848 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002849 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002850 * \retval #PSA_ERROR_BAD_STATE
2851 * The library has not been previously initialized by psa_crypto_init().
2852 * It is implementation-dependent whether a failure to initialize
2853 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002854 */
2855psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2856
Gilles Peskine3b555712018-03-03 21:27:57 +01002857/**@}*/
2858
Gilles Peskine20035e32018-02-03 22:44:14 +01002859/** \defgroup asymmetric Asymmetric cryptography
2860 * @{
2861 */
2862
2863/**
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002864 * \brief Sign a message with a private key. For hash-and-sign algorithms,
2865 * this includes the hashing step.
2866 *
2867 * \note To perform a multi-part hash-and-sign signature algorithm, first use
2868 * a multi-part hash operation and then pass the resulting hash to
2869 * psa_sign_hash(). PSA_ALG_GET_HASH(\p alg) can be used to determine the
2870 * hash algorithm to use.
2871 *
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002872 * \param[in] key Identifier of the key to use for the operation.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002873 * It must be an asymmetric key pair. The key must
2874 * allow the usage #PSA_KEY_USAGE_SIGN_MESSAGE.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002875 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002876 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2877 * is true), that is compatible with the type of
2878 * \p key.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002879 * \param[in] input The input message to sign.
2880 * \param[in] input_length Size of the \p input buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002881 * \param[out] signature Buffer where the signature is to be written.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002882 * \param[in] signature_size Size of the \p signature buffer in bytes. This
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002883 * must be appropriate for the selected
2884 * algorithm and key:
2885 * - The required signature size is
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002886 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2887 * where \c key_type and \c key_bits are the type and
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002888 * bit-size respectively of key.
2889 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
2890 * maximum signature size of any supported
2891 * signature algorithm.
2892 * \param[out] signature_length On success, the number of bytes that make up
2893 * the returned signature value.
2894 *
2895 * \retval #PSA_SUCCESS
2896 * \retval #PSA_ERROR_INVALID_HANDLE
2897 * \retval #PSA_ERROR_NOT_PERMITTED
2898 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2899 * or it does not permit the requested algorithm.
2900 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2901 * The size of the \p signature buffer is too small. You can
2902 * determine a sufficient buffer size by calling
2903 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2904 * where \c key_type and \c key_bits are the type and bit-size
2905 * respectively of \p key.
2906 * \retval #PSA_ERROR_NOT_SUPPORTED
2907 * \retval #PSA_ERROR_INVALID_ARGUMENT
2908 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2909 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2910 * \retval #PSA_ERROR_HARDWARE_FAILURE
2911 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2912 * \retval #PSA_ERROR_STORAGE_FAILURE
2913 * \retval #PSA_ERROR_DATA_CORRUPT
2914 * \retval #PSA_ERROR_DATA_INVALID
2915 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2916 * \retval #PSA_ERROR_BAD_STATE
2917 * The library has not been previously initialized by psa_crypto_init().
2918 * It is implementation-dependent whether a failure to initialize
2919 * results in this error code.
2920 */
2921psa_status_t psa_sign_message( mbedtls_svc_key_id_t key,
2922 psa_algorithm_t alg,
2923 const uint8_t * input,
2924 size_t input_length,
2925 uint8_t * signature,
2926 size_t signature_size,
2927 size_t * signature_length );
2928
2929/** \brief Verify the signature of a message with a public key, using
2930 * a hash-and-sign verification algorithm.
2931 *
2932 * \note To perform a multi-part hash-and-sign signature verification
2933 * algorithm, first use a multi-part hash operation to hash the message
2934 * and then pass the resulting hash to psa_verify_hash().
2935 * PSA_ALG_GET_HASH(\p alg) can be used to determine the hash algorithm
2936 * to use.
2937 *
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002938 * \param[in] key Identifier of the key to use for the operation.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002939 * It must be a public key or an asymmetric key
2940 * pair. The key must allow the usage
2941 * #PSA_KEY_USAGE_VERIFY_MESSAGE.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002942 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002943 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2944 * is true), that is compatible with the type of
2945 * \p key.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002946 * \param[in] input The message whose signature is to be verified.
2947 * \param[in] input_length Size of the \p input buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002948 * \param[out] signature Buffer containing the signature to verify.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002949 * \param[in] signature_length Size of the \p signature buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002950 *
2951 * \retval #PSA_SUCCESS
2952 * \retval #PSA_ERROR_INVALID_HANDLE
2953 * \retval #PSA_ERROR_NOT_PERMITTED
2954 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2955 * or it does not permit the requested algorithm.
2956 * \retval #PSA_ERROR_INVALID_SIGNATURE
2957 * The calculation was performed successfully, but the passed signature
2958 * is not a valid signature.
2959 * \retval #PSA_ERROR_NOT_SUPPORTED
2960 * \retval #PSA_ERROR_INVALID_ARGUMENT
2961 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2962 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2963 * \retval #PSA_ERROR_HARDWARE_FAILURE
2964 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2965 * \retval #PSA_ERROR_STORAGE_FAILURE
2966 * \retval #PSA_ERROR_DATA_CORRUPT
2967 * \retval #PSA_ERROR_DATA_INVALID
2968 * \retval #PSA_ERROR_BAD_STATE
2969 * The library has not been previously initialized by psa_crypto_init().
2970 * It is implementation-dependent whether a failure to initialize
2971 * results in this error code.
2972 */
2973psa_status_t psa_verify_message( mbedtls_svc_key_id_t key,
2974 psa_algorithm_t alg,
2975 const uint8_t * input,
2976 size_t input_length,
2977 const uint8_t * signature,
2978 size_t signature_length );
2979
2980/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002981 * \brief Sign a hash or short message with a private key.
2982 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002983 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002984 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01002985 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
2986 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02002987 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2988 * to determine the hash algorithm to use.
2989 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002990 * \param key Identifier of the key to use for the operation.
2991 * It must be an asymmetric key pair. The key must
Ronald Cron96783552020-10-19 12:06:30 +02002992 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002993 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002994 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002995 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002996 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002997 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002998 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002999 * \param[out] signature_length On success, the number of bytes
3000 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003001 *
Gilles Peskine28538492018-07-11 17:34:00 +02003002 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01003003 * \retval #PSA_ERROR_INVALID_HANDLE
3004 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003005 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003006 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01003007 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003008 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01003009 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003010 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003011 * \retval #PSA_ERROR_NOT_SUPPORTED
3012 * \retval #PSA_ERROR_INVALID_ARGUMENT
3013 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3014 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3015 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003016 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01003017 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003018 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03003019 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003020 * The library has not been previously initialized by psa_crypto_init().
3021 * It is implementation-dependent whether a failure to initialize
3022 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01003023 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003024psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003025 psa_algorithm_t alg,
3026 const uint8_t *hash,
3027 size_t hash_length,
3028 uint8_t *signature,
3029 size_t signature_size,
3030 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003031
3032/**
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003033 * \brief Verify the signature of a hash or short message using a public key.
Gilles Peskine20035e32018-02-03 22:44:14 +01003034 *
Gilles Peskine08bac712018-06-26 16:14:46 +02003035 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02003036 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01003037 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
3038 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02003039 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
3040 * to determine the hash algorithm to use.
3041 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003042 * \param key Identifier of the key to use for the operation. It
3043 * must be a public key or an asymmetric key pair. The
Ronald Cron96783552020-10-19 12:06:30 +02003044 * key must allow the usage
3045 * #PSA_KEY_USAGE_VERIFY_HASH.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003046 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02003047 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003048 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02003049 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003050 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003051 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003052 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003053 *
Gilles Peskine28538492018-07-11 17:34:00 +02003054 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01003055 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01003056 * \retval #PSA_ERROR_INVALID_HANDLE
3057 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003058 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01003059 * The calculation was perfomed successfully, but the passed
3060 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02003061 * \retval #PSA_ERROR_NOT_SUPPORTED
3062 * \retval #PSA_ERROR_INVALID_ARGUMENT
3063 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3064 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3065 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003066 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01003067 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003068 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003069 * The library has not been previously initialized by psa_crypto_init().
3070 * It is implementation-dependent whether a failure to initialize
3071 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01003072 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003073psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003074 psa_algorithm_t alg,
3075 const uint8_t *hash,
3076 size_t hash_length,
3077 const uint8_t *signature,
3078 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003079
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003080/**
3081 * \brief Encrypt a short message with a public key.
3082 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003083 * \param key Identifer of the key to use for the operation.
3084 * It must be a public key or an asymmetric key
3085 * pair. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02003086 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003087 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003088 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003089 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003090 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003091 * \param[in] salt A salt or label, if supported by the
3092 * encryption algorithm.
3093 * If the algorithm does not support a
3094 * salt, pass \c NULL.
3095 * If the algorithm supports an optional
3096 * salt and you do not want to pass a salt,
3097 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003098 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003099 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3100 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003101 * \param salt_length Size of the \p salt buffer in bytes.
3102 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003103 * \param[out] output Buffer where the encrypted message is to
3104 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003105 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003106 * \param[out] output_length On success, the number of bytes
3107 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003108 *
Gilles Peskine28538492018-07-11 17:34:00 +02003109 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01003110 * \retval #PSA_ERROR_INVALID_HANDLE
3111 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003112 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003113 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003114 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02003115 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003116 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003117 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003118 * \retval #PSA_ERROR_NOT_SUPPORTED
3119 * \retval #PSA_ERROR_INVALID_ARGUMENT
3120 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3121 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3122 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003123 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01003124 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003125 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03003126 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003127 * The library has not been previously initialized by psa_crypto_init().
3128 * It is implementation-dependent whether a failure to initialize
3129 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003130 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003131psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003132 psa_algorithm_t alg,
3133 const uint8_t *input,
3134 size_t input_length,
3135 const uint8_t *salt,
3136 size_t salt_length,
3137 uint8_t *output,
3138 size_t output_size,
3139 size_t *output_length);
3140
3141/**
3142 * \brief Decrypt a short message with a private key.
3143 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003144 * \param key Identifier of the key to use for the operation.
3145 * It must be an asymmetric key pair. It must
Ronald Cron96783552020-10-19 12:06:30 +02003146 * allow the usage #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003147 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003148 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003149 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003150 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003151 * \param[in] salt A salt or label, if supported by the
3152 * encryption algorithm.
3153 * If the algorithm does not support a
3154 * salt, pass \c NULL.
3155 * If the algorithm supports an optional
3156 * salt and you do not want to pass a salt,
3157 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003158 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003159 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3160 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003161 * \param salt_length Size of the \p salt buffer in bytes.
3162 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003163 * \param[out] output Buffer where the decrypted message is to
3164 * be written.
3165 * \param output_size Size of the \c output buffer in bytes.
3166 * \param[out] output_length On success, the number of bytes
3167 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003168 *
Gilles Peskine28538492018-07-11 17:34:00 +02003169 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003170 * \retval #PSA_ERROR_INVALID_HANDLE
3171 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003172 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003173 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003174 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003175 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003176 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003177 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003178 * \retval #PSA_ERROR_NOT_SUPPORTED
3179 * \retval #PSA_ERROR_INVALID_ARGUMENT
3180 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3181 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3182 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003183 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003184 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003185 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3186 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003187 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003188 * The library has not been previously initialized by psa_crypto_init().
3189 * It is implementation-dependent whether a failure to initialize
3190 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003191 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003192psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003193 psa_algorithm_t alg,
3194 const uint8_t *input,
3195 size_t input_length,
3196 const uint8_t *salt,
3197 size_t salt_length,
3198 uint8_t *output,
3199 size_t output_size,
3200 size_t *output_length);
3201
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003202/**@}*/
3203
Gilles Peskine35675b62019-05-16 17:26:11 +02003204/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003205 * @{
3206 */
3207
Gilles Peskine35675b62019-05-16 17:26:11 +02003208/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003209 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003210 * Before calling any function on a key derivation operation object, the
3211 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003212 * - Set the structure to all-bits-zero, for example:
3213 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003214 * psa_key_derivation_operation_t operation;
3215 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003216 * \endcode
3217 * - Initialize the structure to logical zero values, for example:
3218 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003219 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003220 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003221 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003222 * for example:
3223 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003224 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003225 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003226 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003227 * to the structure, for example:
3228 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003229 * psa_key_derivation_operation_t operation;
3230 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003231 * \endcode
3232 *
3233 * This is an implementation-defined \c struct. Applications should not
Janos Follath2ba60792021-04-28 09:37:34 +01003234 * make any assumptions about the content of this structure.
3235 * Implementation details can change in future versions without notice.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003236 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003237typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003238
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003239/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003240 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003241 * This macro returns a suitable initializer for a key derivation operation
3242 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003243 */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003244
Gilles Peskine35675b62019-05-16 17:26:11 +02003245/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003246 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003247static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003248
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003249/** Set up a key derivation operation.
3250 *
3251 * A key derivation algorithm takes some inputs and uses them to generate
3252 * a byte stream in a deterministic way.
3253 * This byte stream can be used to produce keys and other
3254 * cryptographic material.
3255 *
3256 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003257 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3258 * -# Call psa_key_derivation_setup() to select the algorithm.
3259 * -# Provide the inputs for the key derivation by calling
3260 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3261 * as appropriate. Which inputs are needed, in what order, and whether
3262 * they may be keys and if so of what type depends on the algorithm.
3263 * -# Optionally set the operation's maximum capacity with
3264 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3265 * of or after providing inputs. For some algorithms, this step is mandatory
3266 * because the output depends on the maximum capacity.
3267 * -# To derive a key, call psa_key_derivation_output_key().
3268 * To derive a byte string for a different purpose, call
3269 * psa_key_derivation_output_bytes().
3270 * Successive calls to these functions use successive output bytes
3271 * calculated by the key derivation algorithm.
3272 * -# Clean up the key derivation operation object with
3273 * psa_key_derivation_abort().
3274 *
3275 * If this function returns an error, the key derivation operation object is
3276 * not changed.
3277 *
3278 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3279 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003280 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003281 * Implementations must reject an attempt to derive a key of size 0.
3282 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003283 * \param[in,out] operation The key derivation operation object
3284 * to set up. It must
3285 * have been initialized but not set up yet.
3286 * \param alg The key derivation algorithm to compute
3287 * (\c PSA_ALG_XXX value such that
3288 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3289 *
3290 * \retval #PSA_SUCCESS
3291 * Success.
3292 * \retval #PSA_ERROR_INVALID_ARGUMENT
3293 * \c alg is not a key derivation algorithm.
3294 * \retval #PSA_ERROR_NOT_SUPPORTED
3295 * \c alg is not supported or is not a key derivation algorithm.
3296 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3297 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3298 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003299 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003300 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003301 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003302 * The operation state is not valid (it must be inactive).
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003303 * \retval #PSA_ERROR_BAD_STATE
3304 * The library has not been previously initialized by psa_crypto_init().
3305 * It is implementation-dependent whether a failure to initialize
3306 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003307 */
3308psa_status_t psa_key_derivation_setup(
3309 psa_key_derivation_operation_t *operation,
3310 psa_algorithm_t alg);
3311
Gilles Peskine35675b62019-05-16 17:26:11 +02003312/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003313 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003314 * The capacity of a key derivation is the maximum number of bytes that it can
3315 * return. When you get *N* bytes of output from a key derivation operation,
3316 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003317 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003318 * \param[in] operation The operation to query.
3319 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003320 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003321 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003322 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003323 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003324 * The operation state is not valid (it must be active).
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003325 * \retval #PSA_ERROR_HARDWARE_FAILURE
3326 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003327 * \retval #PSA_ERROR_BAD_STATE
3328 * The library has not been previously initialized by psa_crypto_init().
3329 * It is implementation-dependent whether a failure to initialize
3330 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003331 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003332psa_status_t psa_key_derivation_get_capacity(
3333 const psa_key_derivation_operation_t *operation,
3334 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003335
Gilles Peskine35675b62019-05-16 17:26:11 +02003336/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003337 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003338 * The capacity of a key derivation operation is the maximum number of bytes
3339 * that the key derivation operation can return from this point onwards.
3340 *
3341 * \param[in,out] operation The key derivation operation object to modify.
3342 * \param capacity The new capacity of the operation.
3343 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003344 * current capacity.
3345 *
3346 * \retval #PSA_SUCCESS
3347 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003348 * \p capacity is larger than the operation's current capacity.
3349 * In this case, the operation object remains valid and its capacity
3350 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003351 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003352 * The operation state is not valid (it must be active).
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003353 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003354 * \retval #PSA_ERROR_HARDWARE_FAILURE
3355 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003356 * \retval #PSA_ERROR_BAD_STATE
3357 * The library has not been previously initialized by psa_crypto_init().
3358 * It is implementation-dependent whether a failure to initialize
3359 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003360 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003361psa_status_t psa_key_derivation_set_capacity(
3362 psa_key_derivation_operation_t *operation,
3363 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003364
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003365/** Use the maximum possible capacity for a key derivation operation.
3366 *
3367 * Use this value as the capacity argument when setting up a key derivation
3368 * to indicate that the operation should have the maximum possible capacity.
3369 * The value of the maximum possible capacity depends on the key derivation
3370 * algorithm.
3371 */
3372#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3373
3374/** Provide an input for key derivation or key agreement.
3375 *
3376 * Which inputs are required and in what order depends on the algorithm.
3377 * Refer to the documentation of each key derivation or key agreement
3378 * algorithm for information.
3379 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003380 * This function passes direct inputs, which is usually correct for
3381 * non-secret inputs. To pass a secret input, which should be in a key
3382 * object, call psa_key_derivation_input_key() instead of this function.
3383 * Refer to the documentation of individual step types
3384 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3385 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003386 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003387 * If this function returns an error status, the operation enters an error
3388 * state and must be aborted by calling psa_key_derivation_abort().
3389 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003390 * \param[in,out] operation The key derivation operation object to use.
3391 * It must have been set up with
3392 * psa_key_derivation_setup() and must not
3393 * have produced any output yet.
3394 * \param step Which step the input data is for.
3395 * \param[in] data Input data to use.
3396 * \param data_length Size of the \p data buffer in bytes.
3397 *
3398 * \retval #PSA_SUCCESS
3399 * Success.
3400 * \retval #PSA_ERROR_INVALID_ARGUMENT
3401 * \c step is not compatible with the operation's algorithm.
3402 * \retval #PSA_ERROR_INVALID_ARGUMENT
3403 * \c step does not allow direct inputs.
3404 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3405 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3406 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003407 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003408 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003409 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003410 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003411 * \retval #PSA_ERROR_BAD_STATE
3412 * The library has not been previously initialized by psa_crypto_init().
3413 * It is implementation-dependent whether a failure to initialize
3414 * results in this error code.
3415 */
3416psa_status_t psa_key_derivation_input_bytes(
3417 psa_key_derivation_operation_t *operation,
3418 psa_key_derivation_step_t step,
3419 const uint8_t *data,
3420 size_t data_length);
3421
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003422/** Provide a numeric input for key derivation or key agreement.
3423 *
3424 * Which inputs are required and in what order depends on the algorithm.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003425 * However, when an algorithm requires a particular order, numeric inputs
3426 * usually come first as they tend to be configuration parameters.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003427 * Refer to the documentation of each key derivation or key agreement
3428 * algorithm for information.
3429 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003430 * This function is used for inputs which are fixed-size non-negative
3431 * integers.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003432 *
3433 * If this function returns an error status, the operation enters an error
3434 * state and must be aborted by calling psa_key_derivation_abort().
3435 *
3436 * \param[in,out] operation The key derivation operation object to use.
3437 * It must have been set up with
3438 * psa_key_derivation_setup() and must not
3439 * have produced any output yet.
3440 * \param step Which step the input data is for.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003441 * \param[in] value The value of the numeric input.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003442 *
3443 * \retval #PSA_SUCCESS
3444 * Success.
3445 * \retval #PSA_ERROR_INVALID_ARGUMENT
3446 * \c step is not compatible with the operation's algorithm.
3447 * \retval #PSA_ERROR_INVALID_ARGUMENT
3448 * \c step does not allow numeric inputs.
3449 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3450 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3451 * \retval #PSA_ERROR_HARDWARE_FAILURE
3452 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3453 * \retval #PSA_ERROR_STORAGE_FAILURE
3454 * \retval #PSA_ERROR_BAD_STATE
3455 * The operation state is not valid for this input \p step.
3456 * \retval #PSA_ERROR_BAD_STATE
3457 * The library has not been previously initialized by psa_crypto_init().
3458 * It is implementation-dependent whether a failure to initialize
3459 * results in this error code.
3460 */
3461psa_status_t psa_key_derivation_input_integer(
3462 psa_key_derivation_operation_t *operation,
3463 psa_key_derivation_step_t step,
3464 uint64_t value);
3465
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003466/** Provide an input for key derivation in the form of a key.
3467 *
3468 * Which inputs are required and in what order depends on the algorithm.
3469 * Refer to the documentation of each key derivation or key agreement
3470 * algorithm for information.
3471 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003472 * This function obtains input from a key object, which is usually correct for
3473 * secret inputs or for non-secret personalization strings kept in the key
3474 * store. To pass a non-secret parameter which is not in the key store,
3475 * call psa_key_derivation_input_bytes() instead of this function.
3476 * Refer to the documentation of individual step types
3477 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3478 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003479 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003480 * If this function returns an error status, the operation enters an error
3481 * state and must be aborted by calling psa_key_derivation_abort().
3482 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003483 * \param[in,out] operation The key derivation operation object to use.
3484 * It must have been set up with
3485 * psa_key_derivation_setup() and must not
3486 * have produced any output yet.
3487 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003488 * \param key Identifier of the key. It must have an
3489 * appropriate type for step and must allow the
Manuel Pégourié-Gonnardacfde462021-05-05 09:54:22 +02003490 * usage #PSA_KEY_USAGE_DERIVE or
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003491 * #PSA_KEY_USAGE_VERIFY_DERIVATION (see note)
3492 * and the algorithm used by the operation.
3493 *
3494 * \note Once all inputs steps are completed, the operations will allow:
3495 * - psa_key_derivation_output_bytes() if each input was either a direct input
3496 * or a key with #PSA_KEY_USAGE_DERIVE set;
Manuel Pégourié-Gonnarde88511d2021-05-07 12:19:03 +02003497 * - psa_key_derivation_output_key() if the input for step
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003498 * #PSA_KEY_DERIVATION_INPUT_SECRET or #PSA_KEY_DERIVATION_INPUT_PASSWORD
Manuel Pégourié-Gonnarde88511d2021-05-07 12:19:03 +02003499 * was from a key slot with #PSA_KEY_USAGE_DERIVE and each other input was
3500 * either a direct input or a key with #PSA_KEY_USAGE_DERIVE set;
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003501 * - psa_key_derivation_verify_bytes() if each input was either a direct input
3502 * or a key with #PSA_KEY_USAGE_VERIFY_DERIVATION set;
Manuel Pégourié-Gonnard4feb6112021-05-07 12:22:21 +02003503 * - psa_key_derivation_verify_key() under the same conditions as
3504 * psa_key_derivation_verify_bytes().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003505 *
3506 * \retval #PSA_SUCCESS
3507 * Success.
3508 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003509 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003510 * The key allows neither #PSA_KEY_USAGE_DERIVE nor
3511 * #PSA_KEY_USAGE_VERIFY_DERIVATION, or it doesn't allow this
3512 * algorithm.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003513 * \retval #PSA_ERROR_INVALID_ARGUMENT
3514 * \c step is not compatible with the operation's algorithm.
3515 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine224b0d62019-09-23 18:13:17 +02003516 * \c step does not allow key inputs of the given type
3517 * or does not allow key inputs at all.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003518 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3519 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3520 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003521 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003522 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003523 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003524 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003525 * \retval #PSA_ERROR_BAD_STATE
3526 * The library has not been previously initialized by psa_crypto_init().
3527 * It is implementation-dependent whether a failure to initialize
3528 * results in this error code.
3529 */
3530psa_status_t psa_key_derivation_input_key(
3531 psa_key_derivation_operation_t *operation,
3532 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003533 mbedtls_svc_key_id_t key);
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003534
3535/** Perform a key agreement and use the shared secret as input to a key
3536 * derivation.
3537 *
3538 * A key agreement algorithm takes two inputs: a private key \p private_key
3539 * a public key \p peer_key.
3540 * The result of this function is passed as input to a key derivation.
3541 * The output of this key derivation can be extracted by reading from the
3542 * resulting operation to produce keys and other cryptographic material.
3543 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003544 * If this function returns an error status, the operation enters an error
3545 * state and must be aborted by calling psa_key_derivation_abort().
3546 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003547 * \param[in,out] operation The key derivation operation object to use.
3548 * It must have been set up with
3549 * psa_key_derivation_setup() with a
3550 * key agreement and derivation algorithm
3551 * \c alg (\c PSA_ALG_XXX value such that
3552 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3553 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3554 * is false).
3555 * The operation must be ready for an
3556 * input of the type given by \p step.
3557 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003558 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003559 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003560 * \param[in] peer_key Public key of the peer. The peer key must be in the
3561 * same format that psa_import_key() accepts for the
3562 * public key type corresponding to the type of
3563 * private_key. That is, this function performs the
3564 * equivalent of
3565 * #psa_import_key(...,
3566 * `peer_key`, `peer_key_length`) where
3567 * with key attributes indicating the public key
3568 * type corresponding to the type of `private_key`.
3569 * For example, for EC keys, this means that peer_key
3570 * is interpreted as a point on the curve that the
3571 * private key is on. The standard formats for public
3572 * keys are documented in the documentation of
3573 * psa_export_public_key().
3574 * \param peer_key_length Size of \p peer_key in bytes.
3575 *
3576 * \retval #PSA_SUCCESS
3577 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01003578 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003579 * The operation state is not valid for this key agreement \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003580 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003581 * \retval #PSA_ERROR_NOT_PERMITTED
3582 * \retval #PSA_ERROR_INVALID_ARGUMENT
3583 * \c private_key is not compatible with \c alg,
3584 * or \p peer_key is not valid for \c alg or not compatible with
3585 * \c private_key.
3586 * \retval #PSA_ERROR_NOT_SUPPORTED
3587 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskine224b0d62019-09-23 18:13:17 +02003588 * \retval #PSA_ERROR_INVALID_ARGUMENT
3589 * \c step does not allow an input resulting from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003590 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3591 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3592 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003593 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003594 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003595 * \retval #PSA_ERROR_BAD_STATE
3596 * The library has not been previously initialized by psa_crypto_init().
3597 * It is implementation-dependent whether a failure to initialize
3598 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003599 */
3600psa_status_t psa_key_derivation_key_agreement(
3601 psa_key_derivation_operation_t *operation,
3602 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003603 mbedtls_svc_key_id_t private_key,
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003604 const uint8_t *peer_key,
3605 size_t peer_key_length);
3606
Gilles Peskine35675b62019-05-16 17:26:11 +02003607/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003608 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003609 * This function calculates output bytes from a key derivation algorithm and
3610 * return those bytes.
3611 * If you view the key derivation's output as a stream of bytes, this
3612 * function destructively reads the requested number of bytes from the
3613 * stream.
3614 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003615 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003616 * If this function returns an error status other than
3617 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003618 * state and must be aborted by calling psa_key_derivation_abort().
3619 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003620 * \param[in,out] operation The key derivation operation object to read from.
3621 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003622 * \param output_length Number of bytes to output.
3623 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003624 * \retval #PSA_SUCCESS
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003625 * \retval #PSA_ERROR_NOT_PERMITTED
3626 * One of the inputs was a key whose policy didn't allow
3627 * #PSA_KEY_USAGE_DERIVE.
David Saadab4ecc272019-02-14 13:48:10 +02003628 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003629 * The operation's capacity was less than
3630 * \p output_length bytes. Note that in this case,
3631 * no output is written to the output buffer.
3632 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003633 * subsequent calls to this function will not
3634 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003635 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003636 * The operation state is not valid (it must be active and completed
3637 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003638 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3639 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3640 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003641 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003642 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003643 * \retval #PSA_ERROR_BAD_STATE
3644 * The library has not been previously initialized by psa_crypto_init().
3645 * It is implementation-dependent whether a failure to initialize
3646 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003647 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003648psa_status_t psa_key_derivation_output_bytes(
3649 psa_key_derivation_operation_t *operation,
3650 uint8_t *output,
3651 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003652
Gilles Peskine35675b62019-05-16 17:26:11 +02003653/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003654 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003655 * This function calculates output bytes from a key derivation algorithm
3656 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003657 * The key's location, usage policy, type and size are taken from
3658 * \p attributes.
3659 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003660 * If you view the key derivation's output as a stream of bytes, this
3661 * function destructively reads as many bytes as required from the
3662 * stream.
3663 * The operation's capacity decreases by the number of bytes read.
3664 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003665 * If this function returns an error status other than
3666 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003667 * state and must be aborted by calling psa_key_derivation_abort().
3668 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003669 * How much output is produced and consumed from the operation, and how
Gilles Peskine364d12c2021-03-08 17:23:47 +01003670 * the key is derived, depends on the key type and on the key size
3671 * (denoted \c bits below):
Gilles Peskineeab56e42018-07-12 17:12:33 +02003672 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003673 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003674 * of a given size, this function is functionally equivalent to
3675 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003676 * and passing the resulting output to #psa_import_key.
3677 * However, this function has a security benefit:
3678 * if the implementation provides an isolation boundary then
3679 * the key material is not exposed outside the isolation boundary.
3680 * As a consequence, for these key types, this function always consumes
Gilles Peskine364d12c2021-03-08 17:23:47 +01003681 * exactly (\c bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003682 * The following key types defined in this specification follow this scheme:
3683 *
3684 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003685 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003686 * - #PSA_KEY_TYPE_DERIVE;
Manuel Pégourié-Gonnard2171e422021-05-03 10:49:54 +02003687 * - #PSA_KEY_TYPE_HMAC;
3688 * - #PSA_KEY_TYPE_PASSWORD_HASH.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003689 *
3690 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003691 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003692 * Montgomery curve), this function always draws a byte string whose
3693 * length is determined by the curve, and sets the mandatory bits
3694 * accordingly. That is:
3695 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01003696 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003697 * string and process it as specified in RFC 7748 &sect;5.
Paul Elliott8ff510a2020-06-02 17:19:28 +01003698 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003699 * string and process it as specified in RFC 7748 &sect;5.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003700 *
3701 * - For key types for which the key is represented by a single sequence of
Gilles Peskine364d12c2021-03-08 17:23:47 +01003702 * \c bits bits with constraints as to which bit sequences are acceptable,
3703 * this function draws a byte string of length (\c bits / 8) bytes rounded
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003704 * up to the nearest whole number of bytes. If the resulting byte string
3705 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3706 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003707 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003708 * for the output produced by psa_export_key().
3709 * The following key types defined in this specification follow this scheme:
3710 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003711 * - #PSA_KEY_TYPE_DES.
3712 * Force-set the parity bits, but discard forbidden weak keys.
3713 * For 2-key and 3-key triple-DES, the three keys are generated
3714 * successively (for example, for 3-key triple-DES,
3715 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3716 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003717 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003718 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003719 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003720 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003721 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003722 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003723 * Weierstrass curve).
3724 * For these key types, interpret the byte string as integer
3725 * in big-endian order. Discard it if it is not in the range
3726 * [0, *N* - 2] where *N* is the boundary of the private key domain
3727 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003728 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003729 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003730 * This method allows compliance to NIST standards, specifically
3731 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003732 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3733 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3734 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3735 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003736 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003737 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003738 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003739 * implementation-defined.
3740 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003741 * In all cases, the data that is read is discarded from the operation.
3742 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003743 *
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003744 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3745 * the input to that step must be provided with psa_key_derivation_input_key().
3746 * Future versions of this specification may include additional restrictions
3747 * on the derived key based on the attributes and strength of the secret key.
3748 *
Gilles Peskine20628592019-04-19 19:29:50 +02003749 * \param[in] attributes The attributes for the new key.
Manuel Pégourié-Gonnard2c44daf2021-05-10 12:53:30 +02003750 * If the key type to be created is
3751 * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
3752 * the policy must be the same as in the current
3753 * operation.
Gilles Peskine35675b62019-05-16 17:26:11 +02003754 * \param[in,out] operation The key derivation operation object to read from.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003755 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003756 * key. For persistent keys, this is the key
3757 * identifier defined in \p attributes.
3758 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003759 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003760 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003761 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003762 * If the key is persistent, the key material and the key's metadata
3763 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003764 * \retval #PSA_ERROR_ALREADY_EXISTS
3765 * This is an attempt to create a persistent key, and there is
3766 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003767 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003768 * There was not enough data to create the desired key.
3769 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003770 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003771 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003772 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003773 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003774 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003775 * \retval #PSA_ERROR_INVALID_ARGUMENT
3776 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003777 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003778 * The #PSA_KEY_DERIVATION_INPUT_SECRET or
3779 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
3780 * key; or one of the inputs was a key whose policy didn't allow
3781 * #PSA_KEY_USAGE_DERIVE.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003782 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003783 * The operation state is not valid (it must be active and completed
3784 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003785 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3786 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3787 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3788 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003789 * \retval #PSA_ERROR_CORRUPTION_DETECTED
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01003790 * \retval #PSA_ERROR_DATA_INVALID
3791 * \retval #PSA_ERROR_DATA_CORRUPT
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003792 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003793 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003794 * The library has not been previously initialized by psa_crypto_init().
3795 * It is implementation-dependent whether a failure to initialize
3796 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003797 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003798psa_status_t psa_key_derivation_output_key(
3799 const psa_key_attributes_t *attributes,
3800 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003801 mbedtls_svc_key_id_t *key);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003802
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003803/** Compare output data from a key derivation operation to an expected value.
3804 *
3805 * This function calculates output bytes from a key derivation algorithm and
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003806 * compares those bytes to an expected value in constant time.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003807 * If you view the key derivation's output as a stream of bytes, this
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003808 * function destructively reads the expected number of bytes from the
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003809 * stream before comparing them.
3810 * The operation's capacity decreases by the number of bytes read.
3811 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003812 * This is functionally equivalent to the following code:
3813 * \code
3814 * psa_key_derivation_output_bytes(operation, tmp, output_length);
3815 * if (memcmp(output, tmp, output_length) != 0)
3816 * return PSA_ERROR_INVALID_SIGNATURE;
3817 * \endcode
3818 * except (1) it works even if the key's policy does not allow outputting the
3819 * bytes, and (2) the comparison will be done in constant time.
3820 *
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003821 * If this function returns an error status other than
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003822 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3823 * the operation enters an error state and must be aborted by calling
3824 * psa_key_derivation_abort().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003825 *
3826 * \param[in,out] operation The key derivation operation object to read from.
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003827 * \param[in] expected_output Buffer containing the expected derivation output.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003828 * \param output_length Length ot the expected output; this is also the
3829 * number of bytes that will be read.
3830 *
3831 * \retval #PSA_SUCCESS
3832 * \retval #PSA_ERROR_INVALID_SIGNATURE
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003833 * The output was read successfully, but it differs from the expected
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003834 * output.
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003835 * \retval #PSA_ERROR_NOT_PERMITTED
3836 * One of the inputs was a key whose policy didn't allow
3837 * #PSA_KEY_USAGE_VERIFY_DERIVATION.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003838 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3839 * The operation's capacity was less than
3840 * \p output_length bytes. Note that in this case,
3841 * the operation's capacity is set to 0, thus
3842 * subsequent calls to this function will not
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003843 * succeed, even with a smaller expected output.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003844 * \retval #PSA_ERROR_BAD_STATE
3845 * The operation state is not valid (it must be active and completed
3846 * all required input steps).
3847 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3848 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3849 * \retval #PSA_ERROR_HARDWARE_FAILURE
3850 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3851 * \retval #PSA_ERROR_STORAGE_FAILURE
3852 * \retval #PSA_ERROR_BAD_STATE
3853 * The library has not been previously initialized by psa_crypto_init().
3854 * It is implementation-dependent whether a failure to initialize
3855 * results in this error code.
3856 */
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003857psa_status_t psa_key_derivation_verify_bytes(
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003858 psa_key_derivation_operation_t *operation,
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003859 const uint8_t *expected_output,
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003860 size_t output_length);
3861
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003862/** Compare output data from a key derivation operation to an expected value
3863 * stored in a key object.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003864 *
3865 * This function calculates output bytes from a key derivation algorithm and
3866 * compares those bytes to an expected value, provided as key of type
Manuel Pégourié-Gonnard4e02f012021-05-12 10:05:45 +02003867 * #PSA_KEY_TYPE_PASSWORD_HASH.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003868 * If you view the key derivation's output as a stream of bytes, this
3869 * function destructively reads the number of bytes corresponding the the
3870 * length of the expected value from the stream before comparing them.
3871 * The operation's capacity decreases by the number of bytes read.
3872 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003873 * This is functionally equivalent to exporting the key and calling
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003874 * psa_key_derivation_verify_bytes() on the result, except that it
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003875 * works even if the key cannot be exported.
3876 *
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003877 * If this function returns an error status other than
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003878 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3879 * the operation enters an error state and must be aborted by calling
3880 * psa_key_derivation_abort().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003881 *
3882 * \param[in,out] operation The key derivation operation object to read from.
Manuel Pégourié-Gonnard2171e422021-05-03 10:49:54 +02003883 * \param[in] expected A key of type #PSA_KEY_TYPE_PASSWORD_HASH
3884 * containing the expected output. Its policy must
3885 * include the #PSA_KEY_USAGE_VERIFY_DERIVATION flag
3886 * and the permitted algorithm must match the
3887 * operation. The value of this key was likely
3888 * computed by a previous call to
3889 * psa_key_derivation_output_key().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003890 *
3891 * \retval #PSA_SUCCESS
3892 * \retval #PSA_ERROR_INVALID_SIGNATURE
3893 * The output was read successfully, but if differs from the expected
3894 * output.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003895 * \retval #PSA_ERROR_INVALID_HANDLE
3896 * The key passed as the expected value does not exist.
3897 * \retval #PSA_ERROR_INVALID_ARGUMENT
3898 * The key passed as the expected value has an invalid type.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003899 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnard88658be2021-05-03 10:28:57 +02003900 * The key passed as the expected value does not allow this usage or
Manuel Pégourié-Gonnard4feb6112021-05-07 12:22:21 +02003901 * this algorithm; or one of the inputs was a key whose policy didn't
3902 * allow #PSA_KEY_USAGE_VERIFY_DERIVATION.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003903 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3904 * The operation's capacity was less than
3905 * the length of the expected value. In this case,
3906 * the operation's capacity is set to 0, thus
3907 * subsequent calls to this function will not
Andrew Thoelkea0f4b592021-06-24 16:47:14 +01003908 * succeed, even with a smaller expected output.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003909 * \retval #PSA_ERROR_BAD_STATE
3910 * The operation state is not valid (it must be active and completed
3911 * all required input steps).
3912 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3913 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3914 * \retval #PSA_ERROR_HARDWARE_FAILURE
3915 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3916 * \retval #PSA_ERROR_STORAGE_FAILURE
3917 * \retval #PSA_ERROR_BAD_STATE
3918 * The library has not been previously initialized by psa_crypto_init().
3919 * It is implementation-dependent whether a failure to initialize
3920 * results in this error code.
3921 */
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003922psa_status_t psa_key_derivation_verify_key(
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003923 psa_key_derivation_operation_t *operation,
3924 psa_key_id_t expected);
3925
Gilles Peskine35675b62019-05-16 17:26:11 +02003926/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003927 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003928 * Aborting an operation frees all associated resources except for the \c
3929 * operation structure itself. Once aborted, the operation object can be reused
3930 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003931 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003932 * This function may be called at any time after the operation
3933 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003934 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003935 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3936 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003937 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003938 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003939 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003940 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003941 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3942 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003943 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003944 * \retval #PSA_ERROR_BAD_STATE
3945 * The library has not been previously initialized by psa_crypto_init().
3946 * It is implementation-dependent whether a failure to initialize
3947 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003948 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003949psa_status_t psa_key_derivation_abort(
3950 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003951
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003952/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003953 *
3954 * \warning The raw result of a key agreement algorithm such as finite-field
3955 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3956 * not be used directly as key material. It should instead be passed as
3957 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003958 * a key derivation, use psa_key_derivation_key_agreement() and other
3959 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003960 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003961 * \param alg The key agreement algorithm to compute
3962 * (\c PSA_ALG_XXX value such that
3963 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3964 * is true).
Ronald Croncf56a0a2020-08-04 09:51:30 +02003965 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003966 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003967 * \param[in] peer_key Public key of the peer. It must be
3968 * in the same format that psa_import_key()
3969 * accepts. The standard formats for public
3970 * keys are documented in the documentation
3971 * of psa_export_public_key().
3972 * \param peer_key_length Size of \p peer_key in bytes.
3973 * \param[out] output Buffer where the decrypted message is to
3974 * be written.
3975 * \param output_size Size of the \c output buffer in bytes.
3976 * \param[out] output_length On success, the number of bytes
3977 * that make up the returned output.
3978 *
3979 * \retval #PSA_SUCCESS
3980 * Success.
3981 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003982 * \retval #PSA_ERROR_NOT_PERMITTED
3983 * \retval #PSA_ERROR_INVALID_ARGUMENT
3984 * \p alg is not a key agreement algorithm
3985 * \retval #PSA_ERROR_INVALID_ARGUMENT
3986 * \p private_key is not compatible with \p alg,
3987 * or \p peer_key is not valid for \p alg or not compatible with
3988 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003989 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3990 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003991 * \retval #PSA_ERROR_NOT_SUPPORTED
3992 * \p alg is not a supported key agreement algorithm.
3993 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3994 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3995 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003996 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003997 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003998 * \retval #PSA_ERROR_BAD_STATE
3999 * The library has not been previously initialized by psa_crypto_init().
4000 * It is implementation-dependent whether a failure to initialize
4001 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01004002 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02004003psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004004 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02004005 const uint8_t *peer_key,
4006 size_t peer_key_length,
4007 uint8_t *output,
4008 size_t output_size,
4009 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02004010
Gilles Peskineea0fb492018-07-12 17:17:20 +02004011/**@}*/
4012
Gilles Peskineedd76872018-07-20 17:42:05 +02004013/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004014 * @{
4015 */
4016
4017/**
4018 * \brief Generate random bytes.
4019 *
4020 * \warning This function **can** fail! Callers MUST check the return status
4021 * and MUST NOT use the content of the output buffer if the return
4022 * status is not #PSA_SUCCESS.
4023 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004024 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004025 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02004026 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004027 * \param output_size Number of bytes to generate and output.
4028 *
Gilles Peskine28538492018-07-11 17:34:00 +02004029 * \retval #PSA_SUCCESS
4030 * \retval #PSA_ERROR_NOT_SUPPORTED
4031 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01004032 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02004033 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
4034 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02004035 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03004036 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03004037 * The library has not been previously initialized by psa_crypto_init().
4038 * It is implementation-dependent whether a failure to initialize
4039 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004040 */
4041psa_status_t psa_generate_random(uint8_t *output,
4042 size_t output_size);
4043
4044/**
4045 * \brief Generate a key or key pair.
4046 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02004047 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02004048 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02004049 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02004050 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02004051 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02004052 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004053 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02004054 * the public exponent is 65537.
4055 * The modulus is a product of two probabilistic primes
4056 * between 2^{n-1} and 2^n where n is the bit size specified in the
4057 * attributes.
4058 *
Gilles Peskine20628592019-04-19 19:29:50 +02004059 * \param[in] attributes The attributes for the new key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02004060 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02004061 * key. For persistent keys, this is the key
4062 * identifier defined in \p attributes.
4063 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004064 *
Gilles Peskine28538492018-07-11 17:34:00 +02004065 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01004066 * Success.
4067 * If the key is persistent, the key material and the key's metadata
4068 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02004069 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02004070 * This is an attempt to create a persistent key, and there is
4071 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02004072 * \retval #PSA_ERROR_NOT_SUPPORTED
4073 * \retval #PSA_ERROR_INVALID_ARGUMENT
4074 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
4075 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
4076 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
4077 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02004078 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01004079 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01004080 * \retval #PSA_ERROR_DATA_INVALID
4081 * \retval #PSA_ERROR_DATA_CORRUPT
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01004082 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03004083 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03004084 * The library has not been previously initialized by psa_crypto_init().
4085 * It is implementation-dependent whether a failure to initialize
4086 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004087 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004088psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004089 mbedtls_svc_key_id_t *key);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004090
4091/**@}*/
4092
Gilles Peskinee59236f2018-01-27 23:32:46 +01004093#ifdef __cplusplus
4094}
4095#endif
4096
Gilles Peskine0cad07c2018-06-27 19:49:02 +02004097/* The file "crypto_sizes.h" contains definitions for size calculation
4098 * macros whose definitions are implementation-specific. */
4099#include "crypto_sizes.h"
4100
Gilles Peskine9ef733f2018-02-07 21:05:37 +01004101/* The file "crypto_struct.h" contains definitions for
4102 * implementation-specific structs that are declared above. */
4103#include "crypto_struct.h"
4104
4105/* The file "crypto_extra.h" contains vendor-specific definitions. This
4106 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004107#include "crypto_extra.h"
4108
4109#endif /* PSA_CRYPTO_H */