blob: 6aa7ccc9248184c192a820d298ace6e858ff737d [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 */
115#ifdef __DOXYGEN_ONLY__
116/* This is an example definition for documentation purposes.
117 * Implementations should define a suitable value in `crypto_struct.h`.
118 */
119#define PSA_KEY_ATTRIBUTES_INIT {0}
120#endif
121
122/** Return an initial value for a key attributes structure.
123 */
124static psa_key_attributes_t psa_key_attributes_init(void);
125
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200126/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200127 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200128 * If the attribute structure currently declares the key as volatile (which
129 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200130 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200131 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200132 * This function does not access storage, it merely stores the given
133 * value in the structure.
134 * The persistent key will be written to storage when the attribute
135 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200136 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200137 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200138 *
Gilles Peskine20628592019-04-19 19:29:50 +0200139 * This function may be declared as `static` (i.e. without external
140 * linkage). This function may be provided as a function-like macro,
141 * but in this case it must evaluate each of its arguments exactly once.
142 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200143 * \param[out] attributes The attribute structure to write to.
144 * \param key The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200145 */
Ronald Cron71016a92020-08-28 19:01:50 +0200146static void psa_set_key_id( psa_key_attributes_t *attributes,
147 mbedtls_svc_key_id_t key );
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200148
Ronald Cron6b5ff532020-10-16 14:38:19 +0200149#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
150/** Set the owner identifier of a key.
151 *
152 * When key identifiers encode key owner identifiers, psa_set_key_id() does
153 * not allow to define in key attributes the owner of volatile keys as
154 * psa_set_key_id() enforces the key to be persistent.
155 *
156 * This function allows to set in key attributes the owner identifier of a
157 * key. It is intended to be used for volatile keys. For persistent keys,
158 * it is recommended to use the PSA Cryptography API psa_set_key_id() to define
159 * the owner of a key.
160 *
161 * \param[out] attributes The attribute structure to write to.
162 * \param owner_id The key owner identifier.
163 */
164static void mbedtls_set_key_owner_id( psa_key_attributes_t *attributes,
165 mbedtls_key_owner_id_t owner_id );
166#endif
167
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200168/** Set the location of a persistent key.
169 *
170 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200171 * with psa_set_key_id(). By default, a key that has a persistent identifier
172 * is stored in the default storage area identifier by
173 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
174 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200175 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200176 * This function does not access storage, it merely stores the given
177 * value in the structure.
178 * The persistent key will be written to storage when the attribute
179 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200180 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200181 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200182 *
183 * This function may be declared as `static` (i.e. without external
184 * linkage). This function may be provided as a function-like macro,
185 * but in this case it must evaluate each of its arguments exactly once.
186 *
187 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200188 * \param lifetime The lifetime for the key.
189 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200190 * key will be volatile, and the key identifier
191 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200192 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200193static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
194 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200195
Gilles Peskine20628592019-04-19 19:29:50 +0200196/** Retrieve the key identifier from key attributes.
197 *
198 * This function may be declared as `static` (i.e. without external
199 * linkage). This function may be provided as a function-like macro,
200 * but in this case it must evaluate its argument exactly once.
201 *
202 * \param[in] attributes The key attribute structure to query.
203 *
204 * \return The persistent identifier stored in the attribute structure.
205 * This value is unspecified if the attribute structure declares
206 * the key as volatile.
207 */
Ronald Cron71016a92020-08-28 19:01:50 +0200208static mbedtls_svc_key_id_t psa_get_key_id(
209 const psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200210
Gilles Peskine20628592019-04-19 19:29:50 +0200211/** Retrieve the lifetime from key attributes.
212 *
213 * This function may be declared as `static` (i.e. without external
214 * linkage). This function may be provided as a function-like macro,
215 * but in this case it must evaluate its argument exactly once.
216 *
217 * \param[in] attributes The key attribute structure to query.
218 *
219 * \return The lifetime value stored in the attribute structure.
220 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200221static psa_key_lifetime_t psa_get_key_lifetime(
222 const psa_key_attributes_t *attributes);
223
Gilles Peskine20628592019-04-19 19:29:50 +0200224/** Declare usage flags for a key.
225 *
226 * Usage flags are part of a key's usage policy. They encode what
227 * kind of operations are permitted on the key. For more details,
228 * refer to the documentation of the type #psa_key_usage_t.
229 *
230 * This function overwrites any usage flags
231 * previously set in \p attributes.
232 *
233 * This function may be declared as `static` (i.e. without external
234 * linkage). This function may be provided as a function-like macro,
235 * but in this case it must evaluate each of its arguments exactly once.
236 *
237 * \param[out] attributes The attribute structure to write to.
238 * \param usage_flags The usage flags to write.
239 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200240static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
241 psa_key_usage_t usage_flags);
242
Gilles Peskine20628592019-04-19 19:29:50 +0200243/** Retrieve the usage flags from key attributes.
244 *
245 * This function may be declared as `static` (i.e. without external
246 * linkage). This function may be provided as a function-like macro,
247 * but in this case it must evaluate its argument exactly once.
248 *
249 * \param[in] attributes The key attribute structure to query.
250 *
251 * \return The usage flags stored in the attribute structure.
252 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200253static psa_key_usage_t psa_get_key_usage_flags(
254 const psa_key_attributes_t *attributes);
255
Gilles Peskine20628592019-04-19 19:29:50 +0200256/** Declare the permitted algorithm policy for a key.
257 *
258 * The permitted algorithm policy of a key encodes which algorithm or
Gilles Peskinea170d922019-09-12 16:59:37 +0200259 * algorithms are permitted to be used with this key. The following
260 * algorithm policies are supported:
261 * - 0 does not allow any cryptographic operation with the key. The key
262 * may be used for non-cryptographic actions such as exporting (if
263 * permitted by the usage flags).
264 * - An algorithm value permits this particular algorithm.
265 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
266 * signature scheme with any hash algorithm.
Steven Cooremancaad4932021-02-18 11:28:17 +0100267 * - An algorithm built from #PSA_ALG_AT_LEAST_THIS_LENGTH_MAC allows
Steven Cooremanee18b1f2021-02-08 11:44:21 +0100268 * any MAC algorithm from the same base class (e.g. CMAC) which
269 * generates/verifies a MAC length greater than or equal to the length
270 * encoded in the wildcard algorithm.
Steven Cooreman5d814812021-02-18 12:11:39 +0100271 * - An algorithm built from #PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG
272 * allows any AEAD algorithm from the same base class (e.g. CCM) which
Steven Cooremanee18b1f2021-02-08 11:44:21 +0100273 * generates/verifies a tag length greater than or equal to the length
274 * encoded in the wildcard algorithm.
Gilles Peskine20628592019-04-19 19:29:50 +0200275 *
276 * This function overwrites any algorithm policy
277 * previously set in \p attributes.
278 *
279 * This function may be declared as `static` (i.e. without external
280 * linkage). This function may be provided as a function-like macro,
281 * but in this case it must evaluate each of its arguments exactly once.
282 *
283 * \param[out] attributes The attribute structure to write to.
284 * \param alg The permitted algorithm policy to write.
285 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200286static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
287 psa_algorithm_t alg);
288
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100289
Gilles Peskine20628592019-04-19 19:29:50 +0200290/** Retrieve the algorithm policy from key attributes.
291 *
292 * This function may be declared as `static` (i.e. without external
293 * linkage). This function may be provided as a function-like macro,
294 * but in this case it must evaluate its argument exactly once.
295 *
296 * \param[in] attributes The key attribute structure to query.
297 *
298 * \return The algorithm stored in the attribute structure.
299 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200300static psa_algorithm_t psa_get_key_algorithm(
301 const psa_key_attributes_t *attributes);
302
Gilles Peskine20628592019-04-19 19:29:50 +0200303/** Declare the type of a key.
304 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200305 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200306 * previously set in \p attributes.
307 *
308 * This function may be declared as `static` (i.e. without external
309 * linkage). This function may be provided as a function-like macro,
310 * but in this case it must evaluate each of its arguments exactly once.
311 *
312 * \param[out] attributes The attribute structure to write to.
313 * \param type The key type to write.
Gilles Peskinea170d922019-09-12 16:59:37 +0200314 * If this is 0, the key type in \p attributes
315 * becomes unspecified.
Gilles Peskine20628592019-04-19 19:29:50 +0200316 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200317static void psa_set_key_type(psa_key_attributes_t *attributes,
318 psa_key_type_t type);
319
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100320
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200321/** Declare the size of a key.
322 *
323 * This function overwrites any key size previously set in \p attributes.
324 *
325 * This function may be declared as `static` (i.e. without external
326 * linkage). This function may be provided as a function-like macro,
327 * but in this case it must evaluate each of its arguments exactly once.
328 *
329 * \param[out] attributes The attribute structure to write to.
330 * \param bits The key size in bits.
Gilles Peskinea170d922019-09-12 16:59:37 +0200331 * If this is 0, the key size in \p attributes
Gilles Peskine05c900b2019-09-12 18:29:43 +0200332 * becomes unspecified. Keys of size 0 are
333 * not supported.
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200334 */
335static void psa_set_key_bits(psa_key_attributes_t *attributes,
336 size_t bits);
337
Gilles Peskine20628592019-04-19 19:29:50 +0200338/** Retrieve the key type from key attributes.
339 *
340 * This function may be declared as `static` (i.e. without external
341 * linkage). This function may be provided as a function-like macro,
342 * but in this case it must evaluate its argument exactly once.
343 *
344 * \param[in] attributes The key attribute structure to query.
345 *
346 * \return The key type stored in the attribute structure.
347 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200348static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
349
Gilles Peskine20628592019-04-19 19:29:50 +0200350/** Retrieve the key size from key attributes.
351 *
352 * This function may be declared as `static` (i.e. without external
353 * linkage). This function may be provided as a function-like macro,
354 * but in this case it must evaluate its argument exactly once.
355 *
356 * \param[in] attributes The key attribute structure to query.
357 *
358 * \return The key size stored in the attribute structure, in bits.
359 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200360static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
361
Gilles Peskine20628592019-04-19 19:29:50 +0200362/** Retrieve the attributes of a key.
363 *
364 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200365 * psa_reset_key_attributes(). It then copies the attributes of
366 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200367 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200368 * \note This function may allocate memory or other resources.
369 * Once you have called this function on an attribute structure,
370 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200371 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200372 * \param[in] key Identifier of the key to query.
Gilles Peskine20628592019-04-19 19:29:50 +0200373 * \param[in,out] attributes On success, the attributes of the key.
374 * On failure, equivalent to a
375 * freshly-initialized structure.
376 *
377 * \retval #PSA_SUCCESS
378 * \retval #PSA_ERROR_INVALID_HANDLE
379 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
380 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100381 * \retval #PSA_ERROR_CORRUPTION_DETECTED
382 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100383 * \retval #PSA_ERROR_DATA_CORRUPT
384 * \retval #PSA_ERROR_DATA_INVALID
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100385 * \retval #PSA_ERROR_BAD_STATE
386 * The library has not been previously initialized by psa_crypto_init().
387 * It is implementation-dependent whether a failure to initialize
388 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200389 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200390psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
Gilles Peskine4747d192019-04-17 15:05:45 +0200391 psa_key_attributes_t *attributes);
392
Gilles Peskine20628592019-04-19 19:29:50 +0200393/** Reset a key attribute structure to a freshly initialized state.
394 *
395 * You must initialize the attribute structure as described in the
396 * documentation of the type #psa_key_attributes_t before calling this
397 * function. Once the structure has been initialized, you may call this
398 * function at any time.
399 *
400 * This function frees any auxiliary resources that the structure
401 * may contain.
402 *
403 * \param[in,out] attributes The attribute structure to reset.
404 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200405void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200406
Gilles Peskine87a5e562019-04-17 12:28:25 +0200407/**@}*/
408
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100409/** \defgroup key_management Key management
410 * @{
411 */
412
Ronald Cron277a85f2020-08-04 15:49:48 +0200413/** Remove non-essential copies of key material from memory.
414 *
415 * If the key identifier designates a volatile key, this functions does not do
416 * anything and returns successfully.
417 *
418 * If the key identifier designates a persistent key, then this function will
419 * free all resources associated with the key in volatile memory. The key
420 * data in persistent storage is not affected and the key can still be used.
421 *
422 * \param key Identifier of the key to purge.
423 *
424 * \retval #PSA_SUCCESS
425 * The key material will have been removed from memory if it is not
426 * currently required.
427 * \retval #PSA_ERROR_INVALID_ARGUMENT
428 * \p key is not a valid key identifier.
429 * \retval #PSA_ERROR_BAD_STATE
430 * The library has not been previously initialized by psa_crypto_init().
431 * It is implementation-dependent whether a failure to initialize
432 * results in this error code.
433 */
434psa_status_t psa_purge_key(mbedtls_svc_key_id_t key);
435
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100436/** Make a copy of a key.
437 *
438 * Copy key material from one location to another.
439 *
440 * This function is primarily useful to copy a key from one location
441 * to another, since it populates a key using the material from
442 * another key which may have a different lifetime.
443 *
444 * This function may be used to share a key with a different party,
445 * subject to implementation-defined restrictions on key sharing.
446 *
447 * The policy on the source key must have the usage flag
448 * #PSA_KEY_USAGE_COPY set.
449 * This flag is sufficient to permit the copy if the key has the lifetime
450 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
451 * Some secure elements do not provide a way to copy a key without
452 * making it extractable from the secure element. If a key is located
453 * in such a secure element, then the key must have both usage flags
454 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
455 * a copy of the key outside the secure element.
456 *
457 * The resulting key may only be used in a way that conforms to
458 * both the policy of the original key and the policy specified in
459 * the \p attributes parameter:
460 * - The usage flags on the resulting key are the bitwise-and of the
461 * usage flags on the source policy and the usage flags in \p attributes.
462 * - If both allow the same algorithm or wildcard-based
463 * algorithm policy, the resulting key has the same algorithm policy.
464 * - If either of the policies allows an algorithm and the other policy
465 * allows a wildcard-based algorithm policy that includes this algorithm,
466 * the resulting key allows the same algorithm.
467 * - If the policies do not allow any algorithm in common, this function
468 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
469 *
470 * The effect of this function on implementation-defined attributes is
471 * implementation-defined.
472 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200473 * \param source_key The key to copy. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +0200474 * #PSA_KEY_USAGE_COPY. If a private or secret key is
Ronald Croncf56a0a2020-08-04 09:51:30 +0200475 * being copied outside of a secure element it must
Ronald Cron96783552020-10-19 12:06:30 +0200476 * also allow #PSA_KEY_USAGE_EXPORT.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100477 * \param[in] attributes The attributes for the new key.
478 * They are used as follows:
479 * - The key type and size may be 0. If either is
480 * nonzero, it must match the corresponding
481 * attribute of the source key.
482 * - The key location (the lifetime and, for
483 * persistent keys, the key identifier) is
484 * used directly.
485 * - The policy constraints (usage flags and
486 * algorithm policy) are combined from
487 * the source key and \p attributes so that
488 * both sets of restrictions apply, as
489 * described in the documentation of this function.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200490 * \param[out] target_key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +0200491 * key. For persistent keys, this is the key
492 * identifier defined in \p attributes.
493 * \c 0 on failure.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100494 *
495 * \retval #PSA_SUCCESS
496 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200497 * \p source_key is invalid.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100498 * \retval #PSA_ERROR_ALREADY_EXISTS
499 * This is an attempt to create a persistent key, and there is
500 * already a persistent key with the given identifier.
501 * \retval #PSA_ERROR_INVALID_ARGUMENT
502 * The lifetime or identifier in \p attributes are invalid.
503 * \retval #PSA_ERROR_INVALID_ARGUMENT
504 * The policy constraints on the source and specified in
505 * \p attributes are incompatible.
506 * \retval #PSA_ERROR_INVALID_ARGUMENT
507 * \p attributes specifies a key type or key size
508 * which does not match the attributes of the source key.
509 * \retval #PSA_ERROR_NOT_PERMITTED
510 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
511 * \retval #PSA_ERROR_NOT_PERMITTED
512 * The source key is not exportable and its lifetime does not
513 * allow copying it to the target's lifetime.
514 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
515 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
516 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
517 * \retval #PSA_ERROR_HARDWARE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100518 * \retval #PSA_ERROR_DATA_INVALID
519 * \retval #PSA_ERROR_DATA_CORRUPT
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100520 * \retval #PSA_ERROR_STORAGE_FAILURE
521 * \retval #PSA_ERROR_CORRUPTION_DETECTED
522 * \retval #PSA_ERROR_BAD_STATE
523 * The library has not been previously initialized by psa_crypto_init().
524 * It is implementation-dependent whether a failure to initialize
525 * results in this error code.
526 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200527psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100528 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200529 mbedtls_svc_key_id_t *target_key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100530
531
532/**
533 * \brief Destroy a key.
534 *
535 * This function destroys a key from both volatile
536 * memory and, if applicable, non-volatile storage. Implementations shall
537 * make a best effort to ensure that that the key material cannot be recovered.
538 *
539 * This function also erases any metadata such as policies and frees
Ronald Croncf56a0a2020-08-04 09:51:30 +0200540 * resources associated with the key.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100541 *
542 * If a key is currently in use in a multipart operation, then destroying the
543 * key will cause the multipart operation to fail.
544 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200545 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
Ronald Cron96783552020-10-19 12:06:30 +0200546 * return #PSA_SUCCESS.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100547 *
548 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +0200549 * \p key was a valid identifier and the key material that it
550 * referred to has been erased. Alternatively, \p key is \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100551 * \retval #PSA_ERROR_NOT_PERMITTED
552 * The key cannot be erased because it is
553 * read-only, either due to a policy or due to physical restrictions.
554 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200555 * \p key is not a valid identifier nor \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100556 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
557 * There was an failure in communication with the cryptoprocessor.
558 * The key material may still be present in the cryptoprocessor.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100559 * \retval #PSA_ERROR_DATA_INVALID
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100560 * This error is typically a result of either storage corruption on a
561 * cleartext storage backend, or an attempt to read data that was
562 * written by an incompatible version of the library.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100563 * \retval #PSA_ERROR_STORAGE_FAILURE
564 * The storage is corrupted. Implementations shall make a best effort
565 * to erase key material even in this stage, however applications
566 * should be aware that it may be impossible to guarantee that the
567 * key material is not recoverable in such cases.
568 * \retval #PSA_ERROR_CORRUPTION_DETECTED
569 * An unexpected condition which is not a storage corruption or
570 * a communication failure occurred. The cryptoprocessor may have
571 * been compromised.
572 * \retval #PSA_ERROR_BAD_STATE
573 * The library has not been previously initialized by psa_crypto_init().
574 * It is implementation-dependent whether a failure to initialize
575 * results in this error code.
576 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200577psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100578
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100579/**@}*/
580
581/** \defgroup import_export Key import and export
582 * @{
583 */
584
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100585/**
586 * \brief Import a key in binary format.
587 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100588 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100589 * documentation of psa_export_public_key() for the format of public keys
590 * and to the documentation of psa_export_key() for the format for
591 * other key types.
592 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200593 * The key data determines the key size. The attributes may optionally
594 * specify a key size; in this case it must match the size determined
595 * from the key data. A key size of 0 in \p attributes indicates that
596 * the key size is solely determined by the key data.
597 *
598 * Implementations must reject an attempt to import a key of size 0.
599 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100600 * This specification supports a single format for each key type.
601 * Implementations may support other formats as long as the standard
602 * format is supported. Implementations that support other formats
603 * should ensure that the formats are clearly unambiguous so as to
604 * minimize the risk that an invalid input is accidentally interpreted
605 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100606 *
Gilles Peskine20628592019-04-19 19:29:50 +0200607 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200608 * The key size is always determined from the
609 * \p data buffer.
610 * If the key size in \p attributes is nonzero,
611 * it must be equal to the size from \p data.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200612 * \param[out] key On success, an identifier to the newly created key.
Ronald Cron4067d1c2020-10-19 13:34:38 +0200613 * For persistent keys, this is the key identifier
614 * defined in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200615 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100616 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200617 * buffer is interpreted according to the type declared
618 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200619 * All implementations must support at least the format
620 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100621 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200622 * the chosen type. Implementations may allow other
623 * formats, but should be conservative: implementations
624 * should err on the side of rejecting content if it
625 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200626 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100627 *
Gilles Peskine28538492018-07-11 17:34:00 +0200628 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100629 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100630 * If the key is persistent, the key material and the key's metadata
631 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200632 * \retval #PSA_ERROR_ALREADY_EXISTS
633 * This is an attempt to create a persistent key, and there is
634 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200635 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200636 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200637 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200638 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200639 * The key attributes, as a whole, are invalid.
640 * \retval #PSA_ERROR_INVALID_ARGUMENT
641 * The key data is not correctly formatted.
642 * \retval #PSA_ERROR_INVALID_ARGUMENT
643 * The size in \p attributes is nonzero and does not match the size
644 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200645 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
646 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
647 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100648 * \retval #PSA_ERROR_DATA_CORRUPT
649 * \retval #PSA_ERROR_DATA_INVALID
Darryl Greend49a4992018-06-18 17:27:26 +0100650 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200651 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200652 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300653 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300654 * The library has not been previously initialized by psa_crypto_init().
655 * It is implementation-dependent whether a failure to initialize
656 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100657 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200658psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100659 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200660 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200661 mbedtls_svc_key_id_t *key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100662
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100663
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100664
665/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100666 * \brief Export a key in binary format.
667 *
668 * The output of this function can be passed to psa_import_key() to
669 * create an equivalent object.
670 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100671 * If the implementation of psa_import_key() supports other formats
672 * beyond the format specified here, the output from psa_export_key()
673 * must use the representation specified here, not the original
674 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100676 * For standard key types, the output format is as follows:
677 *
678 * - For symmetric keys (including MAC keys), the format is the
679 * raw bytes of the key.
680 * - For DES, the key data consists of 8 bytes. The parity bits must be
681 * correct.
682 * - For Triple-DES, the format is the concatenation of the
683 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200684 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200685 * is the non-encrypted DER encoding of the representation defined by
686 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
687 * ```
688 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200689 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200690 * modulus INTEGER, -- n
691 * publicExponent INTEGER, -- e
692 * privateExponent INTEGER, -- d
693 * prime1 INTEGER, -- p
694 * prime2 INTEGER, -- q
695 * exponent1 INTEGER, -- d mod (p-1)
696 * exponent2 INTEGER, -- d mod (q-1)
697 * coefficient INTEGER, -- (inverse of q) mod p
698 * }
699 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200700 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200701 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100702 * a representation of the private value as a `ceiling(m/8)`-byte string
703 * where `m` is the bit size associated with the curve, i.e. the bit size
704 * of the order of the curve's coordinate field. This byte string is
705 * in little-endian order for Montgomery curves (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +0100706 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
707 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
708 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
Steven Cooreman6f5cc712020-06-11 16:40:41 +0200709 * For Weierstrass curves, this is the content of the `privateKey` field of
710 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
711 * the format is defined by RFC 7748, and output is masked according to §5.
Gilles Peskine67546802021-02-24 21:49:40 +0100712 * For twisted Edwards curves, the private key is as defined by RFC 8032
713 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200714 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200715 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000716 * format is the representation of the private key `x` as a big-endian byte
717 * string. The length of the byte string is the private key size in bytes
718 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200719 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
720 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100721 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200722 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
723 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200724 * \param key Identifier of the key to export. It must allow the
Ronald Cron96783552020-10-19 12:06:30 +0200725 * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
Ronald Croncf56a0a2020-08-04 09:51:30 +0200726 * key.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200727 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200728 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200729 * \param[out] data_length On success, the number of bytes
730 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100731 *
Gilles Peskine28538492018-07-11 17:34:00 +0200732 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100733 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200734 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200735 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100736 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200737 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
738 * The size of the \p data buffer is too small. You can determine a
739 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100740 * #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200741 * where \c type is the key type
742 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200743 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
744 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200745 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100746 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100747 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300748 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300749 * The library has not been previously initialized by psa_crypto_init().
750 * It is implementation-dependent whether a failure to initialize
751 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100752 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200753psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100754 uint8_t *data,
755 size_t data_size,
756 size_t *data_length);
757
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100758/**
759 * \brief Export a public key or the public part of a key pair in binary format.
760 *
761 * The output of this function can be passed to psa_import_key() to
762 * create an object that is equivalent to the public key.
763 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000764 * This specification supports a single format for each key type.
765 * Implementations may support other formats as long as the standard
766 * format is supported. Implementations that support other formats
767 * should ensure that the formats are clearly unambiguous so as to
768 * minimize the risk that an invalid input is accidentally interpreted
769 * according to a different format.
770 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000771 * For standard key types, the output format is as follows:
772 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
773 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
774 * ```
775 * RSAPublicKey ::= SEQUENCE {
776 * modulus INTEGER, -- n
777 * publicExponent INTEGER } -- e
778 * ```
Gilles Peskine67546802021-02-24 21:49:40 +0100779 * - For elliptic curve keys on a twisted Edwards curve (key types for which
Bence Szépkúti3b1cba82021-04-08 15:49:07 +0200780 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true and #PSA_KEY_TYPE_ECC_GET_FAMILY
Gilles Peskine67546802021-02-24 21:49:40 +0100781 * returns #PSA_ECC_FAMILY_TWISTED_EDWARDS), the public key is as defined
782 * by RFC 8032
783 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
784 * - For other elliptic curve public keys (key types for which
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000785 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
786 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
787 * Let `m` be the bit size associated with the curve, i.e. the bit size of
788 * `q` for a curve over `F_q`. The representation consists of:
789 * - The byte 0x04;
790 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
791 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200792 * - For Diffie-Hellman key exchange public keys (key types for which
793 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000794 * the format is the representation of the public key `y = g^x mod p` as a
795 * big-endian byte string. The length of the byte string is the length of the
796 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100797 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200798 * Exporting a public key object or the public part of a key pair is
799 * always permitted, regardless of the key's usage flags.
800 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200801 * \param key Identifier of the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200802 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200803 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200804 * \param[out] data_length On success, the number of bytes
805 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100806 *
Gilles Peskine28538492018-07-11 17:34:00 +0200807 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100808 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200809 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200810 * The key is neither a public key nor a key pair.
811 * \retval #PSA_ERROR_NOT_SUPPORTED
812 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
813 * The size of the \p data buffer is too small. You can determine a
814 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100815 * #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200816 * where \c type is the key type
817 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200818 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
819 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200820 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100821 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100822 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300823 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300824 * The library has not been previously initialized by psa_crypto_init().
825 * It is implementation-dependent whether a failure to initialize
826 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100827 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200828psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100829 uint8_t *data,
830 size_t data_size,
831 size_t *data_length);
832
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100833
Gilles Peskine20035e32018-02-03 22:44:14 +0100834
835/**@}*/
836
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100837/** \defgroup hash Message digests
838 * @{
839 */
840
Gilles Peskine69647a42019-01-14 20:18:12 +0100841/** Calculate the hash (digest) of a message.
842 *
843 * \note To verify the hash of a message against an
844 * expected value, use psa_hash_compare() instead.
845 *
846 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
847 * such that #PSA_ALG_IS_HASH(\p alg) is true).
848 * \param[in] input Buffer containing the message to hash.
849 * \param input_length Size of the \p input buffer in bytes.
850 * \param[out] hash Buffer where the hash is to be written.
851 * \param hash_size Size of the \p hash buffer in bytes.
852 * \param[out] hash_length On success, the number of bytes
853 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100854 * #PSA_HASH_LENGTH(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100855 *
856 * \retval #PSA_SUCCESS
857 * Success.
858 * \retval #PSA_ERROR_NOT_SUPPORTED
859 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100860 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100861 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
862 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100863 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
864 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
865 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200866 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100867 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100868 * \retval #PSA_ERROR_BAD_STATE
869 * The library has not been previously initialized by psa_crypto_init().
870 * It is implementation-dependent whether a failure to initialize
871 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100872 */
873psa_status_t psa_hash_compute(psa_algorithm_t alg,
874 const uint8_t *input,
875 size_t input_length,
876 uint8_t *hash,
877 size_t hash_size,
878 size_t *hash_length);
879
880/** Calculate the hash (digest) of a message and compare it with a
881 * reference value.
882 *
883 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
884 * such that #PSA_ALG_IS_HASH(\p alg) is true).
885 * \param[in] input Buffer containing the message to hash.
886 * \param input_length Size of the \p input buffer in bytes.
887 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100888 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100889 *
890 * \retval #PSA_SUCCESS
891 * The expected hash is identical to the actual hash of the input.
892 * \retval #PSA_ERROR_INVALID_SIGNATURE
893 * The hash of the message was calculated successfully, but it
894 * differs from the expected hash.
895 * \retval #PSA_ERROR_NOT_SUPPORTED
896 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100897 * \retval #PSA_ERROR_INVALID_ARGUMENT
898 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100899 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
900 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
901 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200902 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100903 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100904 * \retval #PSA_ERROR_BAD_STATE
905 * The library has not been previously initialized by psa_crypto_init().
906 * It is implementation-dependent whether a failure to initialize
907 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100908 */
909psa_status_t psa_hash_compare(psa_algorithm_t alg,
910 const uint8_t *input,
911 size_t input_length,
912 const uint8_t *hash,
Gilles Peskinefa710f52019-12-02 14:31:48 +0100913 size_t hash_length);
Gilles Peskine69647a42019-01-14 20:18:12 +0100914
Gilles Peskine308b91d2018-02-08 09:47:44 +0100915/** The type of the state data structure for multipart hash operations.
916 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000917 * Before calling any function on a hash operation object, the application must
918 * initialize it by any of the following means:
919 * - Set the structure to all-bits-zero, for example:
920 * \code
921 * psa_hash_operation_t operation;
922 * memset(&operation, 0, sizeof(operation));
923 * \endcode
924 * - Initialize the structure to logical zero values, for example:
925 * \code
926 * psa_hash_operation_t operation = {0};
927 * \endcode
928 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
929 * for example:
930 * \code
931 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
932 * \endcode
933 * - Assign the result of the function psa_hash_operation_init()
934 * to the structure, for example:
935 * \code
936 * psa_hash_operation_t operation;
937 * operation = psa_hash_operation_init();
938 * \endcode
939 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100940 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100941 * make any assumptions about the content of this structure except
942 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100943typedef struct psa_hash_operation_s psa_hash_operation_t;
944
Jaeden Amero6a25b412019-01-04 11:47:44 +0000945/** \def PSA_HASH_OPERATION_INIT
946 *
947 * This macro returns a suitable initializer for a hash operation object
948 * of type #psa_hash_operation_t.
949 */
950#ifdef __DOXYGEN_ONLY__
951/* This is an example definition for documentation purposes.
952 * Implementations should define a suitable value in `crypto_struct.h`.
953 */
954#define PSA_HASH_OPERATION_INIT {0}
955#endif
956
957/** Return an initial value for a hash operation object.
958 */
959static psa_hash_operation_t psa_hash_operation_init(void);
960
Gilles Peskinef45adda2019-01-14 18:29:18 +0100961/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100962 *
963 * The sequence of operations to calculate a hash (message digest)
964 * is as follows:
965 * -# Allocate an operation object which will be passed to all the functions
966 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000967 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100968 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200969 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100970 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100971 * of the message each time. The hash that is calculated is the hash
972 * of the concatenation of these messages in order.
973 * -# To calculate the hash, call psa_hash_finish().
974 * To compare the hash with an expected value, call psa_hash_verify().
975 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100976 * If an error occurs at any step after a call to psa_hash_setup(), the
977 * operation will need to be reset by a call to psa_hash_abort(). The
978 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000979 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100980 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200981 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100982 * eventually terminate the operation. The following events terminate an
983 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100984 * - A successful call to psa_hash_finish() or psa_hash_verify().
985 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100986 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000987 * \param[in,out] operation The operation object to set up. It must have
988 * been initialized as per the documentation for
989 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200990 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
991 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100992 *
Gilles Peskine28538492018-07-11 17:34:00 +0200993 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100994 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200995 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100996 * \p alg is not a supported hash algorithm.
997 * \retval #PSA_ERROR_INVALID_ARGUMENT
998 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100999 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001000 * The operation state is not valid (it must be inactive).
Gilles Peskine28538492018-07-11 17:34:00 +02001001 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1002 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1003 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001004 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001005 * \retval #PSA_ERROR_BAD_STATE
1006 * The library has not been previously initialized by psa_crypto_init().
1007 * It is implementation-dependent whether a failure to initialize
1008 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001009 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001010psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001011 psa_algorithm_t alg);
1012
Gilles Peskine308b91d2018-02-08 09:47:44 +01001013/** Add a message fragment to a multipart hash operation.
1014 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001015 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001016 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001017 * If this function returns an error status, the operation enters an error
1018 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001019 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001020 * \param[in,out] operation Active hash operation.
1021 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001022 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001023 *
Gilles Peskine28538492018-07-11 17:34:00 +02001024 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001025 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001026 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001027 * The operation state is not valid (it muct be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001028 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1029 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1030 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001031 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001032 * \retval #PSA_ERROR_BAD_STATE
1033 * The library has not been previously initialized by psa_crypto_init().
1034 * It is implementation-dependent whether a failure to initialize
1035 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001036 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001037psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1038 const uint8_t *input,
1039 size_t input_length);
1040
Gilles Peskine308b91d2018-02-08 09:47:44 +01001041/** Finish the calculation of the hash of a message.
1042 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001043 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001044 * This function calculates the hash of the message formed by concatenating
1045 * the inputs passed to preceding calls to psa_hash_update().
1046 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001047 * When this function returns successfuly, the operation becomes inactive.
1048 * If this function returns an error status, the operation enters an error
1049 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001050 *
1051 * \warning Applications should not call this function if they expect
1052 * a specific value for the hash. Call psa_hash_verify() instead.
1053 * Beware that comparing integrity or authenticity data such as
1054 * hash values with a function such as \c memcmp is risky
1055 * because the time taken by the comparison may leak information
1056 * about the hashed data which could allow an attacker to guess
1057 * a valid hash and thereby bypass security controls.
1058 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001059 * \param[in,out] operation Active hash operation.
1060 * \param[out] hash Buffer where the hash is to be written.
1061 * \param hash_size Size of the \p hash buffer in bytes.
1062 * \param[out] hash_length On success, the number of bytes
1063 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001064 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001065 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001066 *
Gilles Peskine28538492018-07-11 17:34:00 +02001067 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001068 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001069 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001070 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001071 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001072 * The size of the \p hash buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001073 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001075 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1076 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1077 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001078 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001079 * \retval #PSA_ERROR_BAD_STATE
1080 * The library has not been previously initialized by psa_crypto_init().
1081 * It is implementation-dependent whether a failure to initialize
1082 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001083 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001084psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1085 uint8_t *hash,
1086 size_t hash_size,
1087 size_t *hash_length);
1088
Gilles Peskine308b91d2018-02-08 09:47:44 +01001089/** Finish the calculation of the hash of a message and compare it with
1090 * an expected value.
1091 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001092 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001093 * This function calculates the hash of the message formed by concatenating
1094 * the inputs passed to preceding calls to psa_hash_update(). It then
1095 * compares the calculated hash with the expected hash passed as a
1096 * parameter to this function.
1097 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001098 * When this function returns successfuly, the operation becomes inactive.
1099 * If this function returns an error status, the operation enters an error
1100 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001101 *
Gilles Peskine19067982018-03-20 17:54:53 +01001102 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001103 * comparison between the actual hash and the expected hash is performed
1104 * in constant time.
1105 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001106 * \param[in,out] operation Active hash operation.
1107 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001108 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001109 *
Gilles Peskine28538492018-07-11 17:34:00 +02001110 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001111 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001112 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001113 * The hash of the message was calculated successfully, but it
1114 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001115 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001116 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001117 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1118 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1119 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001120 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001121 * \retval #PSA_ERROR_BAD_STATE
1122 * The library has not been previously initialized by psa_crypto_init().
1123 * It is implementation-dependent whether a failure to initialize
1124 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001125 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001126psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1127 const uint8_t *hash,
1128 size_t hash_length);
1129
Gilles Peskine308b91d2018-02-08 09:47:44 +01001130/** Abort a hash operation.
1131 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001132 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001133 * \p operation structure itself. Once aborted, the operation object
1134 * can be reused for another operation by calling
1135 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001136 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001137 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001138 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001139 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001140 * In particular, calling psa_hash_abort() after the operation has been
1141 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1142 * psa_hash_verify() is safe and has no effect.
1143 *
1144 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001145 *
Gilles Peskine28538492018-07-11 17:34:00 +02001146 * \retval #PSA_SUCCESS
Gilles Peskine28538492018-07-11 17:34:00 +02001147 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1148 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001149 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001150 * \retval #PSA_ERROR_BAD_STATE
1151 * The library has not been previously initialized by psa_crypto_init().
1152 * It is implementation-dependent whether a failure to initialize
1153 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001154 */
1155psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001156
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001157/** Clone a hash operation.
1158 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001159 * This function copies the state of an ongoing hash operation to
1160 * a new operation object. In other words, this function is equivalent
1161 * to calling psa_hash_setup() on \p target_operation with the same
1162 * algorithm that \p source_operation was set up for, then
1163 * psa_hash_update() on \p target_operation with the same input that
1164 * that was passed to \p source_operation. After this function returns, the
1165 * two objects are independent, i.e. subsequent calls involving one of
1166 * the objects do not affect the other object.
1167 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001168 * \param[in] source_operation The active hash operation to clone.
1169 * \param[in,out] target_operation The operation object to set up.
1170 * It must be initialized but not active.
1171 *
1172 * \retval #PSA_SUCCESS
1173 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001174 * The \p source_operation state is not valid (it must be active).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001175 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001176 * The \p target_operation state is not valid (it must be inactive).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001177 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1178 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001179 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001180 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001181 * \retval #PSA_ERROR_BAD_STATE
1182 * The library has not been previously initialized by psa_crypto_init().
1183 * It is implementation-dependent whether a failure to initialize
1184 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001185 */
1186psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1187 psa_hash_operation_t *target_operation);
1188
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001189/**@}*/
1190
Gilles Peskine8c9def32018-02-08 10:02:12 +01001191/** \defgroup MAC Message authentication codes
1192 * @{
1193 */
1194
Gilles Peskine69647a42019-01-14 20:18:12 +01001195/** Calculate the MAC (message authentication code) of a message.
1196 *
1197 * \note To verify the MAC of a message against an
1198 * expected value, use psa_mac_verify() instead.
1199 * Beware that comparing integrity or authenticity data such as
1200 * MAC values with a function such as \c memcmp is risky
1201 * because the time taken by the comparison may leak information
1202 * about the MAC value which could allow an attacker to guess
1203 * a valid MAC and thereby bypass security controls.
1204 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001205 * \param key Identifier of the key to use for the operation. It
1206 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001207 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001208 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001209 * \param[in] input Buffer containing the input message.
1210 * \param input_length Size of the \p input buffer in bytes.
1211 * \param[out] mac Buffer where the MAC value is to be written.
1212 * \param mac_size Size of the \p mac buffer in bytes.
1213 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001214 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001215 *
1216 * \retval #PSA_SUCCESS
1217 * Success.
1218 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001219 * \retval #PSA_ERROR_NOT_PERMITTED
1220 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001221 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001222 * \retval #PSA_ERROR_NOT_SUPPORTED
1223 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001224 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1225 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001226 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1227 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1228 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001229 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001230 * \retval #PSA_ERROR_STORAGE_FAILURE
1231 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001232 * \retval #PSA_ERROR_BAD_STATE
1233 * The library has not been previously initialized by psa_crypto_init().
1234 * It is implementation-dependent whether a failure to initialize
1235 * results in this error code.
1236 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001237psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001238 psa_algorithm_t alg,
1239 const uint8_t *input,
1240 size_t input_length,
1241 uint8_t *mac,
1242 size_t mac_size,
1243 size_t *mac_length);
1244
1245/** Calculate the MAC of a message and compare it with a reference value.
1246 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001247 * \param key Identifier of the key to use for the operation. It
1248 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001249 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001250 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001251 * \param[in] input Buffer containing the input message.
1252 * \param input_length Size of the \p input buffer in bytes.
1253 * \param[out] mac Buffer containing the expected MAC value.
1254 * \param mac_length Size of the \p mac buffer in bytes.
1255 *
1256 * \retval #PSA_SUCCESS
1257 * The expected MAC is identical to the actual MAC of the input.
1258 * \retval #PSA_ERROR_INVALID_SIGNATURE
1259 * The MAC of the message was calculated successfully, but it
1260 * differs from the expected value.
1261 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001262 * \retval #PSA_ERROR_NOT_PERMITTED
1263 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001264 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001265 * \retval #PSA_ERROR_NOT_SUPPORTED
1266 * \p alg is not supported or is not a MAC algorithm.
1267 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1268 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1269 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001270 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001271 * \retval #PSA_ERROR_STORAGE_FAILURE
1272 * The key could not be retrieved from storage.
1273 * \retval #PSA_ERROR_BAD_STATE
1274 * The library has not been previously initialized by psa_crypto_init().
1275 * It is implementation-dependent whether a failure to initialize
1276 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001277 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001278psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
Gilles Peskinea05602d2019-01-17 15:25:52 +01001279 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001280 const uint8_t *input,
1281 size_t input_length,
1282 const uint8_t *mac,
Gilles Peskine13faa2d2020-01-30 16:32:21 +01001283 size_t mac_length);
Gilles Peskine69647a42019-01-14 20:18:12 +01001284
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001285/** The type of the state data structure for multipart MAC operations.
1286 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001287 * Before calling any function on a MAC operation object, the application must
1288 * initialize it by any of the following means:
1289 * - Set the structure to all-bits-zero, for example:
1290 * \code
1291 * psa_mac_operation_t operation;
1292 * memset(&operation, 0, sizeof(operation));
1293 * \endcode
1294 * - Initialize the structure to logical zero values, for example:
1295 * \code
1296 * psa_mac_operation_t operation = {0};
1297 * \endcode
1298 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1299 * for example:
1300 * \code
1301 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1302 * \endcode
1303 * - Assign the result of the function psa_mac_operation_init()
1304 * to the structure, for example:
1305 * \code
1306 * psa_mac_operation_t operation;
1307 * operation = psa_mac_operation_init();
1308 * \endcode
1309 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001310 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001311 * make any assumptions about the content of this structure except
1312 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001313typedef struct psa_mac_operation_s psa_mac_operation_t;
1314
Jaeden Amero769ce272019-01-04 11:48:03 +00001315/** \def PSA_MAC_OPERATION_INIT
1316 *
1317 * This macro returns a suitable initializer for a MAC operation object of type
1318 * #psa_mac_operation_t.
1319 */
1320#ifdef __DOXYGEN_ONLY__
1321/* This is an example definition for documentation purposes.
1322 * Implementations should define a suitable value in `crypto_struct.h`.
1323 */
1324#define PSA_MAC_OPERATION_INIT {0}
1325#endif
1326
1327/** Return an initial value for a MAC operation object.
1328 */
1329static psa_mac_operation_t psa_mac_operation_init(void);
1330
Gilles Peskinef45adda2019-01-14 18:29:18 +01001331/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001332 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001333 * This function sets up the calculation of the MAC
1334 * (message authentication code) of a byte string.
1335 * To verify the MAC of a message against an
1336 * expected value, use psa_mac_verify_setup() instead.
1337 *
1338 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001339 * -# Allocate an operation object which will be passed to all the functions
1340 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001341 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001342 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001343 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001344 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1345 * of the message each time. The MAC that is calculated is the MAC
1346 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001347 * -# At the end of the message, call psa_mac_sign_finish() to finish
1348 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001349 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001350 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1351 * operation will need to be reset by a call to psa_mac_abort(). The
1352 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001353 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001354 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001355 * After a successful call to psa_mac_sign_setup(), the application must
1356 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001357 * - A successful call to psa_mac_sign_finish().
1358 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001359 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001360 * \param[in,out] operation The operation object to set up. It must have
1361 * been initialized as per the documentation for
1362 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001363 * \param key Identifier of the key to use for the operation. It
1364 * must remain valid until the operation terminates.
1365 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001366 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001367 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001368 *
Gilles Peskine28538492018-07-11 17:34:00 +02001369 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001370 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001371 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001372 * \retval #PSA_ERROR_NOT_PERMITTED
1373 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001374 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001375 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001376 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001377 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1378 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1379 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001380 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001381 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001382 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001383 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001384 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001385 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001386 * The library has not been previously initialized by psa_crypto_init().
1387 * It is implementation-dependent whether a failure to initialize
1388 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001389 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001390psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001391 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001392 psa_algorithm_t alg);
1393
Gilles Peskinef45adda2019-01-14 18:29:18 +01001394/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001395 *
1396 * This function sets up the verification of the MAC
1397 * (message authentication code) of a byte string against an expected value.
1398 *
1399 * The sequence of operations to verify a MAC is as follows:
1400 * -# Allocate an operation object which will be passed to all the functions
1401 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001402 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001403 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001404 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001405 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1406 * of the message each time. The MAC that is calculated is the MAC
1407 * of the concatenation of these messages in order.
1408 * -# At the end of the message, call psa_mac_verify_finish() to finish
1409 * calculating the actual MAC of the message and verify it against
1410 * the expected value.
1411 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001412 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1413 * operation will need to be reset by a call to psa_mac_abort(). The
1414 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001415 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001416 *
1417 * After a successful call to psa_mac_verify_setup(), the application must
1418 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001419 * - A successful call to psa_mac_verify_finish().
1420 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001421 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001422 * \param[in,out] operation The operation object to set up. It must have
1423 * been initialized as per the documentation for
1424 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001425 * \param key Identifier of the key to use for the operation. It
1426 * must remain valid until the operation terminates.
1427 * It must allow the usage
1428 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001429 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1430 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001431 *
Gilles Peskine28538492018-07-11 17:34:00 +02001432 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001433 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001434 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001435 * \retval #PSA_ERROR_NOT_PERMITTED
1436 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001437 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001438 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001439 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001440 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1441 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1442 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001443 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001444 * \retval #PSA_ERROR_STORAGE_FAILURE
1445 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001446 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001447 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001448 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001449 * The library has not been previously initialized by psa_crypto_init().
1450 * It is implementation-dependent whether a failure to initialize
1451 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001452 */
1453psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001454 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001455 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001456
Gilles Peskinedcd14942018-07-12 00:30:52 +02001457/** Add a message fragment to a multipart MAC operation.
1458 *
1459 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1460 * before calling this function.
1461 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001462 * If this function returns an error status, the operation enters an error
1463 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001464 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001465 * \param[in,out] operation Active MAC operation.
1466 * \param[in] input Buffer containing the message fragment to add to
1467 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001468 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001469 *
1470 * \retval #PSA_SUCCESS
1471 * Success.
1472 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001473 * The operation state is not valid (it must be active).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001474 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1475 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1476 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001477 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001478 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001479 * \retval #PSA_ERROR_BAD_STATE
1480 * The library has not been previously initialized by psa_crypto_init().
1481 * It is implementation-dependent whether a failure to initialize
1482 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001483 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001484psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1485 const uint8_t *input,
1486 size_t input_length);
1487
Gilles Peskinedcd14942018-07-12 00:30:52 +02001488/** Finish the calculation of the MAC of a message.
1489 *
1490 * The application must call psa_mac_sign_setup() before calling this function.
1491 * This function calculates the MAC of the message formed by concatenating
1492 * the inputs passed to preceding calls to psa_mac_update().
1493 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001494 * When this function returns successfuly, the operation becomes inactive.
1495 * If this function returns an error status, the operation enters an error
1496 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001497 *
1498 * \warning Applications should not call this function if they expect
1499 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1500 * Beware that comparing integrity or authenticity data such as
1501 * MAC values with a function such as \c memcmp is risky
1502 * because the time taken by the comparison may leak information
1503 * about the MAC value which could allow an attacker to guess
1504 * a valid MAC and thereby bypass security controls.
1505 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001506 * \param[in,out] operation Active MAC operation.
1507 * \param[out] mac Buffer where the MAC value is to be written.
1508 * \param mac_size Size of the \p mac buffer in bytes.
1509 * \param[out] mac_length On success, the number of bytes
1510 * that make up the MAC value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001511 * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001512 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001513 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001514 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001515 *
1516 * \retval #PSA_SUCCESS
1517 * Success.
1518 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001519 * The operation state is not valid (it must be an active mac sign
1520 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001521 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001522 * The size of the \p mac buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001523 * sufficient buffer size by calling PSA_MAC_LENGTH().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001524 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1525 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1526 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001527 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001528 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001529 * \retval #PSA_ERROR_BAD_STATE
1530 * The library has not been previously initialized by psa_crypto_init().
1531 * It is implementation-dependent whether a failure to initialize
1532 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001533 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001534psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1535 uint8_t *mac,
1536 size_t mac_size,
1537 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001538
Gilles Peskinedcd14942018-07-12 00:30:52 +02001539/** Finish the calculation of the MAC of a message and compare it with
1540 * an expected value.
1541 *
1542 * The application must call psa_mac_verify_setup() before calling this function.
1543 * This function calculates the MAC of the message formed by concatenating
1544 * the inputs passed to preceding calls to psa_mac_update(). It then
1545 * compares the calculated MAC with the expected MAC passed as a
1546 * parameter to this function.
1547 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001548 * When this function returns successfuly, the operation becomes inactive.
1549 * If this function returns an error status, the operation enters an error
1550 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001551 *
1552 * \note Implementations shall make the best effort to ensure that the
1553 * comparison between the actual MAC and the expected MAC is performed
1554 * in constant time.
1555 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001556 * \param[in,out] operation Active MAC operation.
1557 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001558 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001559 *
1560 * \retval #PSA_SUCCESS
1561 * The expected MAC is identical to the actual MAC of the message.
1562 * \retval #PSA_ERROR_INVALID_SIGNATURE
1563 * The MAC of the message was calculated successfully, but it
1564 * differs from the expected MAC.
1565 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001566 * The operation state is not valid (it must be an active mac verify
1567 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001568 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1569 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1570 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001571 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001572 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001573 * \retval #PSA_ERROR_BAD_STATE
1574 * The library has not been previously initialized by psa_crypto_init().
1575 * It is implementation-dependent whether a failure to initialize
1576 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001577 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001578psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1579 const uint8_t *mac,
1580 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001581
Gilles Peskinedcd14942018-07-12 00:30:52 +02001582/** Abort a MAC operation.
1583 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001584 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001585 * \p operation structure itself. Once aborted, the operation object
1586 * can be reused for another operation by calling
1587 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001588 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001589 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001590 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001591 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001592 * In particular, calling psa_mac_abort() after the operation has been
1593 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1594 * psa_mac_verify_finish() is safe and has no effect.
1595 *
1596 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001597 *
1598 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02001599 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1600 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001601 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001602 * \retval #PSA_ERROR_BAD_STATE
1603 * The library has not been previously initialized by psa_crypto_init().
1604 * It is implementation-dependent whether a failure to initialize
1605 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001606 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001607psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1608
1609/**@}*/
1610
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001611/** \defgroup cipher Symmetric ciphers
1612 * @{
1613 */
1614
Gilles Peskine69647a42019-01-14 20:18:12 +01001615/** Encrypt a message using a symmetric cipher.
1616 *
1617 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001618 * vector). Use the multipart operation interface with a
1619 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001620 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001621 * \param key Identifier of the key to use for the operation.
Ronald Cron96783552020-10-19 12:06:30 +02001622 * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001623 * \param alg The cipher algorithm to compute
1624 * (\c PSA_ALG_XXX value such that
1625 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1626 * \param[in] input Buffer containing the message to encrypt.
1627 * \param input_length Size of the \p input buffer in bytes.
1628 * \param[out] output Buffer where the output is to be written.
1629 * The output contains the IV followed by
1630 * the ciphertext proper.
1631 * \param output_size Size of the \p output buffer in bytes.
1632 * \param[out] output_length On success, the number of bytes
1633 * that make up the output.
1634 *
1635 * \retval #PSA_SUCCESS
1636 * Success.
1637 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001638 * \retval #PSA_ERROR_NOT_PERMITTED
1639 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001640 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001641 * \retval #PSA_ERROR_NOT_SUPPORTED
1642 * \p alg is not supported or is not a cipher algorithm.
1643 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1644 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1645 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1646 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001647 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001648 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001649 * \retval #PSA_ERROR_BAD_STATE
1650 * The library has not been previously initialized by psa_crypto_init().
1651 * It is implementation-dependent whether a failure to initialize
1652 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001653 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001654psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001655 psa_algorithm_t alg,
1656 const uint8_t *input,
1657 size_t input_length,
1658 uint8_t *output,
1659 size_t output_size,
1660 size_t *output_length);
1661
1662/** Decrypt a message using a symmetric cipher.
1663 *
1664 * This function decrypts a message encrypted with a symmetric cipher.
1665 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001666 * \param key Identifier of the key to use for the operation.
Gilles Peskine69647a42019-01-14 20:18:12 +01001667 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001668 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001669 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001670 * \param alg The cipher algorithm to compute
1671 * (\c PSA_ALG_XXX value such that
1672 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1673 * \param[in] input Buffer containing the message to decrypt.
1674 * This consists of the IV followed by the
1675 * ciphertext proper.
1676 * \param input_length Size of the \p input buffer in bytes.
1677 * \param[out] output Buffer where the plaintext is to be written.
1678 * \param output_size Size of the \p output buffer in bytes.
1679 * \param[out] output_length On success, the number of bytes
1680 * that make up the output.
1681 *
1682 * \retval #PSA_SUCCESS
1683 * Success.
1684 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001685 * \retval #PSA_ERROR_NOT_PERMITTED
1686 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001687 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001688 * \retval #PSA_ERROR_NOT_SUPPORTED
1689 * \p alg is not supported or is not a cipher algorithm.
1690 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1691 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1692 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1693 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001694 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001695 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001696 * \retval #PSA_ERROR_BAD_STATE
1697 * The library has not been previously initialized by psa_crypto_init().
1698 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001699 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001700 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001701psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001702 psa_algorithm_t alg,
1703 const uint8_t *input,
1704 size_t input_length,
1705 uint8_t *output,
1706 size_t output_size,
1707 size_t *output_length);
1708
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001709/** The type of the state data structure for multipart cipher operations.
1710 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001711 * Before calling any function on a cipher operation object, the application
1712 * must initialize it by any of the following means:
1713 * - Set the structure to all-bits-zero, for example:
1714 * \code
1715 * psa_cipher_operation_t operation;
1716 * memset(&operation, 0, sizeof(operation));
1717 * \endcode
1718 * - Initialize the structure to logical zero values, for example:
1719 * \code
1720 * psa_cipher_operation_t operation = {0};
1721 * \endcode
1722 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1723 * for example:
1724 * \code
1725 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1726 * \endcode
1727 * - Assign the result of the function psa_cipher_operation_init()
1728 * to the structure, for example:
1729 * \code
1730 * psa_cipher_operation_t operation;
1731 * operation = psa_cipher_operation_init();
1732 * \endcode
1733 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001734 * This is an implementation-defined \c struct. Applications should not
1735 * make any assumptions about the content of this structure except
1736 * as directed by the documentation of a specific implementation. */
1737typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1738
Jaeden Amero5bae2272019-01-04 11:48:27 +00001739/** \def PSA_CIPHER_OPERATION_INIT
1740 *
1741 * This macro returns a suitable initializer for a cipher operation object of
1742 * type #psa_cipher_operation_t.
1743 */
1744#ifdef __DOXYGEN_ONLY__
1745/* This is an example definition for documentation purposes.
1746 * Implementations should define a suitable value in `crypto_struct.h`.
1747 */
1748#define PSA_CIPHER_OPERATION_INIT {0}
1749#endif
1750
1751/** Return an initial value for a cipher operation object.
1752 */
1753static psa_cipher_operation_t psa_cipher_operation_init(void);
1754
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001755/** Set the key for a multipart symmetric encryption operation.
1756 *
1757 * The sequence of operations to encrypt a message with a symmetric cipher
1758 * is as follows:
1759 * -# Allocate an operation object which will be passed to all the functions
1760 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001761 * -# Initialize the operation object with one of the methods described in the
1762 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001763 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001764 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001765 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001766 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001767 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001768 * requires a specific IV value.
1769 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1770 * of the message each time.
1771 * -# Call psa_cipher_finish().
1772 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001773 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1774 * the operation will need to be reset by a call to psa_cipher_abort(). The
1775 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001776 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001777 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001778 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001779 * eventually terminate the operation. The following events terminate an
1780 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001781 * - A successful call to psa_cipher_finish().
1782 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001783 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001784 * \param[in,out] operation The operation object to set up. It must have
1785 * been initialized as per the documentation for
1786 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001787 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001788 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001789 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001790 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001791 * \param alg The cipher algorithm to compute
1792 * (\c PSA_ALG_XXX value such that
1793 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001794 *
Gilles Peskine28538492018-07-11 17:34:00 +02001795 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001796 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001797 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001798 * \retval #PSA_ERROR_NOT_PERMITTED
1799 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001800 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001801 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001802 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001803 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1804 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1805 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001806 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001807 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001808 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001809 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001810 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001811 * The library has not been previously initialized by psa_crypto_init().
1812 * It is implementation-dependent whether a failure to initialize
1813 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001814 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001815psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001816 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001817 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001818
1819/** Set the key for a multipart symmetric decryption operation.
1820 *
1821 * The sequence of operations to decrypt a message with a symmetric cipher
1822 * is as follows:
1823 * -# Allocate an operation object which will be passed to all the functions
1824 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001825 * -# Initialize the operation object with one of the methods described in the
1826 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001827 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001828 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001829 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001830 * decryption. If the IV is prepended to the ciphertext, you can call
1831 * psa_cipher_update() on a buffer containing the IV followed by the
1832 * beginning of the message.
1833 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1834 * of the message each time.
1835 * -# Call psa_cipher_finish().
1836 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001837 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1838 * the operation will need to be reset by a call to psa_cipher_abort(). The
1839 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001840 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001841 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001842 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001843 * eventually terminate the operation. The following events terminate an
1844 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001845 * - A successful call to psa_cipher_finish().
1846 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001847 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001848 * \param[in,out] operation The operation object to set up. It must have
1849 * been initialized as per the documentation for
1850 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001851 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001852 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001853 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001854 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001855 * \param alg The cipher algorithm to compute
1856 * (\c PSA_ALG_XXX value such that
1857 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001858 *
Gilles Peskine28538492018-07-11 17:34:00 +02001859 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001860 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001861 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001862 * \retval #PSA_ERROR_NOT_PERMITTED
1863 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001864 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001865 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001866 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001867 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1868 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1869 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001870 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001871 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001872 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001873 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001874 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001875 * The library has not been previously initialized by psa_crypto_init().
1876 * It is implementation-dependent whether a failure to initialize
1877 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001878 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001879psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001880 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001881 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001882
Gilles Peskinedcd14942018-07-12 00:30:52 +02001883/** Generate an IV for a symmetric encryption operation.
1884 *
1885 * This function generates a random IV (initialization vector), nonce
1886 * or initial counter value for the encryption operation as appropriate
1887 * for the chosen algorithm, key type and key size.
1888 *
1889 * The application must call psa_cipher_encrypt_setup() before
1890 * calling this function.
1891 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001892 * If this function returns an error status, the operation enters an error
1893 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001894 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001895 * \param[in,out] operation Active cipher operation.
1896 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001897 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001898 * \param[out] iv_length On success, the number of bytes of the
1899 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001900 *
1901 * \retval #PSA_SUCCESS
1902 * Success.
1903 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001904 * The operation state is not valid (it must be active, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001905 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001906 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001907 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1908 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1909 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001910 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001911 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001912 * \retval #PSA_ERROR_BAD_STATE
1913 * The library has not been previously initialized by psa_crypto_init().
1914 * It is implementation-dependent whether a failure to initialize
1915 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001916 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001917psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001918 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001919 size_t iv_size,
1920 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001921
Gilles Peskinedcd14942018-07-12 00:30:52 +02001922/** Set the IV for a symmetric encryption or decryption operation.
1923 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001924 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001925 * or initial counter value for the encryption or decryption operation.
1926 *
1927 * The application must call psa_cipher_encrypt_setup() before
1928 * calling this function.
1929 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001930 * If this function returns an error status, the operation enters an error
1931 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001932 *
1933 * \note When encrypting, applications should use psa_cipher_generate_iv()
1934 * instead of this function, unless implementing a protocol that requires
1935 * a non-random IV.
1936 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001937 * \param[in,out] operation Active cipher operation.
1938 * \param[in] iv Buffer containing the IV to use.
1939 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001940 *
1941 * \retval #PSA_SUCCESS
1942 * Success.
1943 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001944 * The operation state is not valid (it must be an active cipher
1945 * encrypt operation, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001946 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001947 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001948 * or the chosen algorithm does not use an IV.
1949 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1950 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1951 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001952 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001953 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001954 * \retval #PSA_ERROR_BAD_STATE
1955 * The library has not been previously initialized by psa_crypto_init().
1956 * It is implementation-dependent whether a failure to initialize
1957 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001958 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001959psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001960 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001961 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001962
Gilles Peskinedcd14942018-07-12 00:30:52 +02001963/** Encrypt or decrypt a message fragment in an active cipher operation.
1964 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001965 * Before calling this function, you must:
1966 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1967 * The choice of setup function determines whether this function
1968 * encrypts or decrypts its input.
1969 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1970 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001971 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001972 * If this function returns an error status, the operation enters an error
1973 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001974 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001975 * \param[in,out] operation Active cipher operation.
1976 * \param[in] input Buffer containing the message fragment to
1977 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001978 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001979 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001980 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001981 * \param[out] output_length On success, the number of bytes
1982 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001983 *
1984 * \retval #PSA_SUCCESS
1985 * Success.
1986 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001987 * The operation state is not valid (it must be active, with an IV set
1988 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001989 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1990 * The size of the \p output buffer is too small.
1991 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1992 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1993 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001994 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001995 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001996 * \retval #PSA_ERROR_BAD_STATE
1997 * The library has not been previously initialized by psa_crypto_init().
1998 * It is implementation-dependent whether a failure to initialize
1999 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002000 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002001psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2002 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002003 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00002004 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02002005 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002006 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002007
Gilles Peskinedcd14942018-07-12 00:30:52 +02002008/** Finish encrypting or decrypting a message in a cipher operation.
2009 *
2010 * The application must call psa_cipher_encrypt_setup() or
2011 * psa_cipher_decrypt_setup() before calling this function. The choice
2012 * of setup function determines whether this function encrypts or
2013 * decrypts its input.
2014 *
2015 * This function finishes the encryption or decryption of the message
2016 * formed by concatenating the inputs passed to preceding calls to
2017 * psa_cipher_update().
2018 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002019 * When this function returns successfuly, the operation becomes inactive.
2020 * If this function returns an error status, the operation enters an error
2021 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002022 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002023 * \param[in,out] operation Active cipher operation.
2024 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002025 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002026 * \param[out] output_length On success, the number of bytes
2027 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002028 *
2029 * \retval #PSA_SUCCESS
2030 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02002031 * \retval #PSA_ERROR_INVALID_ARGUMENT
2032 * The total input size passed to this operation is not valid for
2033 * this particular algorithm. For example, the algorithm is a based
2034 * on block cipher and requires a whole number of blocks, but the
2035 * total input size is not a multiple of the block size.
2036 * \retval #PSA_ERROR_INVALID_PADDING
2037 * This is a decryption operation for an algorithm that includes
2038 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002039 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002040 * The operation state is not valid (it must be active, with an IV set
2041 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02002042 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2043 * The size of the \p output buffer is too small.
2044 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2045 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2046 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002047 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002048 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002049 * \retval #PSA_ERROR_BAD_STATE
2050 * The library has not been previously initialized by psa_crypto_init().
2051 * It is implementation-dependent whether a failure to initialize
2052 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002053 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002054psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002055 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002056 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002057 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002058
Gilles Peskinedcd14942018-07-12 00:30:52 +02002059/** Abort a cipher operation.
2060 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002061 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002062 * \p operation structure itself. Once aborted, the operation object
2063 * can be reused for another operation by calling
2064 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002065 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002066 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002067 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002068 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002069 * In particular, calling psa_cipher_abort() after the operation has been
2070 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2071 * is safe and has no effect.
2072 *
2073 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002074 *
2075 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02002076 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2077 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002078 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002079 * \retval #PSA_ERROR_BAD_STATE
2080 * The library has not been previously initialized by psa_crypto_init().
2081 * It is implementation-dependent whether a failure to initialize
2082 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002083 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002084psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2085
2086/**@}*/
2087
Gilles Peskine3b555712018-03-03 21:27:57 +01002088/** \defgroup aead Authenticated encryption with associated data (AEAD)
2089 * @{
2090 */
2091
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002092/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002093 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002094 * \param key Identifier of the key to use for the
2095 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002096 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002097 * \param alg The AEAD algorithm to compute
2098 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002099 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002100 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002101 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002102 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002103 * but not encrypted.
2104 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002105 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002106 * encrypted.
2107 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002108 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002109 * encrypted data. The additional data is not
2110 * part of this output. For algorithms where the
2111 * encrypted data and the authentication tag
2112 * are defined as separate outputs, the
2113 * authentication tag is appended to the
2114 * encrypted data.
2115 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002116 * This must be appropriate for the selected
2117 * algorithm and key:
2118 * - A sufficient output size is
2119 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type,
2120 * \p alg, \p plaintext_length) where
2121 * \c key_type is the type of \p key.
2122 * - #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p
2123 * plaintext_length) evaluates to the maximum
2124 * ciphertext size of any supported AEAD
2125 * encryption.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002126 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002127 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002128 *
Gilles Peskine28538492018-07-11 17:34:00 +02002129 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002130 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002131 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002132 * \retval #PSA_ERROR_NOT_PERMITTED
2133 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002134 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002135 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002136 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002137 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002138 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Bence Szépkútibd98df72021-04-27 04:37:18 +02002139 * \p ciphertext_size is too small.
2140 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2141 * \p plaintext_length) or
2142 * #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length) can be used to
2143 * determine the required buffer size.
Gilles Peskine28538492018-07-11 17:34:00 +02002144 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2145 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002146 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002147 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002148 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002149 * The library has not been previously initialized by psa_crypto_init().
2150 * It is implementation-dependent whether a failure to initialize
2151 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002152 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002153psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002154 psa_algorithm_t alg,
2155 const uint8_t *nonce,
2156 size_t nonce_length,
2157 const uint8_t *additional_data,
2158 size_t additional_data_length,
2159 const uint8_t *plaintext,
2160 size_t plaintext_length,
2161 uint8_t *ciphertext,
2162 size_t ciphertext_size,
2163 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002164
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002165/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002166 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002167 * \param key Identifier of the key to use for the
2168 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002169 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002170 * \param alg The AEAD algorithm to compute
2171 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002172 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002173 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002174 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002175 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002176 * but not encrypted.
2177 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002178 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002179 * encrypted. For algorithms where the
2180 * encrypted data and the authentication tag
2181 * are defined as separate inputs, the buffer
2182 * must contain the encrypted data followed
2183 * by the authentication tag.
2184 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002185 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002186 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002187 * This must be appropriate for the selected
2188 * algorithm and key:
2189 * - A sufficient output size is
2190 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type,
2191 * \p alg, \p ciphertext_length) where
2192 * \c key_type is the type of \p key.
2193 * - #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p
2194 * ciphertext_length) evaluates to the maximum
2195 * plaintext size of any supported AEAD
2196 * decryption.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002197 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002198 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002199 *
Gilles Peskine28538492018-07-11 17:34:00 +02002200 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002201 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002202 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002203 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002204 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002205 * \retval #PSA_ERROR_NOT_PERMITTED
2206 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002207 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002208 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002209 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002210 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002211 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Bence Szépkútibd98df72021-04-27 04:37:18 +02002212 * \p plaintext_size is too small.
2213 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2214 * \p ciphertext_length) or
2215 * #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length) can be used
2216 * to determine the required buffer size.
Gilles Peskine28538492018-07-11 17:34:00 +02002217 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2218 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002219 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002220 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002221 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002222 * The library has not been previously initialized by psa_crypto_init().
2223 * It is implementation-dependent whether a failure to initialize
2224 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002225 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002226psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002227 psa_algorithm_t alg,
2228 const uint8_t *nonce,
2229 size_t nonce_length,
2230 const uint8_t *additional_data,
2231 size_t additional_data_length,
2232 const uint8_t *ciphertext,
2233 size_t ciphertext_length,
2234 uint8_t *plaintext,
2235 size_t plaintext_size,
2236 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002237
Gilles Peskine30a9e412019-01-14 18:36:12 +01002238/** The type of the state data structure for multipart AEAD operations.
2239 *
2240 * Before calling any function on an AEAD operation object, the application
2241 * must initialize it by any of the following means:
2242 * - Set the structure to all-bits-zero, for example:
2243 * \code
2244 * psa_aead_operation_t operation;
2245 * memset(&operation, 0, sizeof(operation));
2246 * \endcode
2247 * - Initialize the structure to logical zero values, for example:
2248 * \code
2249 * psa_aead_operation_t operation = {0};
2250 * \endcode
2251 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2252 * for example:
2253 * \code
2254 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2255 * \endcode
2256 * - Assign the result of the function psa_aead_operation_init()
2257 * to the structure, for example:
2258 * \code
2259 * psa_aead_operation_t operation;
2260 * operation = psa_aead_operation_init();
2261 * \endcode
2262 *
2263 * This is an implementation-defined \c struct. Applications should not
2264 * make any assumptions about the content of this structure except
2265 * as directed by the documentation of a specific implementation. */
2266typedef struct psa_aead_operation_s psa_aead_operation_t;
2267
2268/** \def PSA_AEAD_OPERATION_INIT
2269 *
2270 * This macro returns a suitable initializer for an AEAD operation object of
2271 * type #psa_aead_operation_t.
2272 */
2273#ifdef __DOXYGEN_ONLY__
2274/* This is an example definition for documentation purposes.
2275 * Implementations should define a suitable value in `crypto_struct.h`.
2276 */
2277#define PSA_AEAD_OPERATION_INIT {0}
2278#endif
2279
2280/** Return an initial value for an AEAD operation object.
2281 */
2282static psa_aead_operation_t psa_aead_operation_init(void);
2283
2284/** Set the key for a multipart authenticated encryption operation.
2285 *
2286 * The sequence of operations to encrypt a message with authentication
2287 * is as follows:
2288 * -# Allocate an operation object which will be passed to all the functions
2289 * listed here.
2290 * -# Initialize the operation object with one of the methods described in the
2291 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002292 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002293 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002294 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2295 * inputs to the subsequent calls to psa_aead_update_ad() and
2296 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2297 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002298 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2299 * generate or set the nonce. You should use
2300 * psa_aead_generate_nonce() unless the protocol you are implementing
2301 * requires a specific nonce value.
2302 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2303 * of the non-encrypted additional authenticated data each time.
2304 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002305 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002306 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002307 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002308 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2309 * the operation will need to be reset by a call to psa_aead_abort(). The
2310 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002311 * has been initialized.
2312 *
2313 * After a successful call to psa_aead_encrypt_setup(), the application must
2314 * eventually terminate the operation. The following events terminate an
2315 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002316 * - A successful call to psa_aead_finish().
2317 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002318 *
2319 * \param[in,out] operation The operation object to set up. It must have
2320 * been initialized as per the documentation for
2321 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002322 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002323 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002324 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002325 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002326 * \param alg The AEAD algorithm to compute
2327 * (\c PSA_ALG_XXX value such that
2328 * #PSA_ALG_IS_AEAD(\p alg) is true).
2329 *
2330 * \retval #PSA_SUCCESS
2331 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002332 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002333 * The operation state is not valid (it must be inactive).
Ronald Cron96783552020-10-19 12:06:30 +02002334 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002335 * \retval #PSA_ERROR_NOT_PERMITTED
2336 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002337 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002338 * \retval #PSA_ERROR_NOT_SUPPORTED
2339 * \p alg is not supported or is not an AEAD algorithm.
2340 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2341 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2342 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002343 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002344 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002345 * \retval #PSA_ERROR_BAD_STATE
2346 * The library has not been previously initialized by psa_crypto_init().
2347 * It is implementation-dependent whether a failure to initialize
2348 * results in this error code.
2349 */
2350psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002351 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002352 psa_algorithm_t alg);
2353
2354/** Set the key for a multipart authenticated decryption operation.
2355 *
2356 * The sequence of operations to decrypt a message with authentication
2357 * is as follows:
2358 * -# Allocate an operation object which will be passed to all the functions
2359 * listed here.
2360 * -# Initialize the operation object with one of the methods described in the
2361 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002362 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002363 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002364 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2365 * inputs to the subsequent calls to psa_aead_update_ad() and
2366 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2367 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002368 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2369 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2370 * of the non-encrypted additional authenticated data each time.
2371 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002372 * of the ciphertext to decrypt each time.
2373 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002374 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002375 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2376 * the operation will need to be reset by a call to psa_aead_abort(). The
2377 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002378 * has been initialized.
2379 *
2380 * After a successful call to psa_aead_decrypt_setup(), the application must
2381 * eventually terminate the operation. The following events terminate an
2382 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002383 * - A successful call to psa_aead_verify().
2384 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002385 *
2386 * \param[in,out] operation The operation object to set up. It must have
2387 * been initialized as per the documentation for
2388 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002389 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002390 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002391 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002392 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002393 * \param alg The AEAD algorithm to compute
2394 * (\c PSA_ALG_XXX value such that
2395 * #PSA_ALG_IS_AEAD(\p alg) is true).
2396 *
2397 * \retval #PSA_SUCCESS
2398 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002399 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002400 * The operation state is not valid (it must be inactive).
Ronald Cron96783552020-10-19 12:06:30 +02002401 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002402 * \retval #PSA_ERROR_NOT_PERMITTED
2403 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002404 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002405 * \retval #PSA_ERROR_NOT_SUPPORTED
2406 * \p alg is not supported or is not an AEAD algorithm.
2407 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2408 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2409 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002410 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002411 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002412 * \retval #PSA_ERROR_BAD_STATE
2413 * The library has not been previously initialized by psa_crypto_init().
2414 * It is implementation-dependent whether a failure to initialize
2415 * results in this error code.
2416 */
2417psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002418 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002419 psa_algorithm_t alg);
2420
2421/** Generate a random nonce for an authenticated encryption operation.
2422 *
2423 * This function generates a random nonce for the authenticated encryption
2424 * operation with an appropriate size for the chosen algorithm, key type
2425 * and key size.
2426 *
2427 * The application must call psa_aead_encrypt_setup() before
2428 * calling this function.
2429 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002430 * If this function returns an error status, the operation enters an error
2431 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002432 *
2433 * \param[in,out] operation Active AEAD operation.
2434 * \param[out] nonce Buffer where the generated nonce is to be
2435 * written.
2436 * \param nonce_size Size of the \p nonce buffer in bytes.
2437 * \param[out] nonce_length On success, the number of bytes of the
2438 * generated nonce.
2439 *
2440 * \retval #PSA_SUCCESS
2441 * Success.
2442 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002443 * The operation state is not valid (it must be an active aead encrypt
Ronald Cron96783552020-10-19 12:06:30 +02002444 * operation, with no nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002445 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2446 * The size of the \p nonce buffer is too small.
2447 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2448 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2449 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002450 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002451 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002452 * \retval #PSA_ERROR_BAD_STATE
2453 * The library has not been previously initialized by psa_crypto_init().
2454 * It is implementation-dependent whether a failure to initialize
2455 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002456 */
2457psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002458 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002459 size_t nonce_size,
2460 size_t *nonce_length);
2461
2462/** Set the nonce for an authenticated encryption or decryption operation.
2463 *
2464 * This function sets the nonce for the authenticated
2465 * encryption or decryption operation.
2466 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002467 * The application must call psa_aead_encrypt_setup() or
2468 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002469 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002470 * If this function returns an error status, the operation enters an error
2471 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002472 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002473 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002474 * instead of this function, unless implementing a protocol that requires
2475 * a non-random IV.
2476 *
2477 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002478 * \param[in] nonce Buffer containing the nonce to use.
2479 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002480 *
2481 * \retval #PSA_SUCCESS
2482 * Success.
2483 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002484 * The operation state is not valid (it must be active, with no nonce
2485 * set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002486 * \retval #PSA_ERROR_INVALID_ARGUMENT
2487 * The size of \p nonce is not acceptable for the chosen algorithm.
2488 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2489 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2490 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002491 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002492 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002493 * \retval #PSA_ERROR_BAD_STATE
2494 * The library has not been previously initialized by psa_crypto_init().
2495 * It is implementation-dependent whether a failure to initialize
2496 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002497 */
2498psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002499 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002500 size_t nonce_length);
2501
Gilles Peskinebc59c852019-01-17 15:26:08 +01002502/** Declare the lengths of the message and additional data for AEAD.
2503 *
2504 * The application must call this function before calling
2505 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2506 * the operation requires it. If the algorithm does not require it,
2507 * calling this function is optional, but if this function is called
2508 * then the implementation must enforce the lengths.
2509 *
2510 * You may call this function before or after setting the nonce with
2511 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2512 *
2513 * - For #PSA_ALG_CCM, calling this function is required.
2514 * - For the other AEAD algorithms defined in this specification, calling
2515 * this function is not required.
2516 * - For vendor-defined algorithm, refer to the vendor documentation.
2517 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002518 * If this function returns an error status, the operation enters an error
2519 * state and must be aborted by calling psa_aead_abort().
2520 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002521 * \param[in,out] operation Active AEAD operation.
2522 * \param ad_length Size of the non-encrypted additional
2523 * authenticated data in bytes.
2524 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2525 *
2526 * \retval #PSA_SUCCESS
2527 * Success.
2528 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002529 * The operation state is not valid (it must be active, and
2530 * psa_aead_update_ad() and psa_aead_update() must not have been
2531 * called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002532 * \retval #PSA_ERROR_INVALID_ARGUMENT
2533 * At least one of the lengths is not acceptable for the chosen
2534 * algorithm.
2535 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2536 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2537 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002538 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002539 * \retval #PSA_ERROR_BAD_STATE
2540 * The library has not been previously initialized by psa_crypto_init().
2541 * It is implementation-dependent whether a failure to initialize
2542 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002543 */
2544psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2545 size_t ad_length,
2546 size_t plaintext_length);
2547
Gilles Peskine30a9e412019-01-14 18:36:12 +01002548/** Pass additional data to an active AEAD operation.
2549 *
2550 * Additional data is authenticated, but not encrypted.
2551 *
2552 * You may call this function multiple times to pass successive fragments
2553 * of the additional data. You may not call this function after passing
2554 * data to encrypt or decrypt with psa_aead_update().
2555 *
2556 * Before calling this function, you must:
2557 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2558 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2559 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002560 * If this function returns an error status, the operation enters an error
2561 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002562 *
2563 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2564 * there is no guarantee that the input is valid. Therefore, until
2565 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2566 * treat the input as untrusted and prepare to undo any action that
2567 * depends on the input if psa_aead_verify() returns an error status.
2568 *
2569 * \param[in,out] operation Active AEAD operation.
2570 * \param[in] input Buffer containing the fragment of
2571 * additional data.
2572 * \param input_length Size of the \p input buffer in bytes.
2573 *
2574 * \retval #PSA_SUCCESS
2575 * Success.
2576 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002577 * The operation state is not valid (it must be active, have a nonce
2578 * set, have lengths set if required by the algorithm, and
2579 * psa_aead_update() must not have been called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002580 * \retval #PSA_ERROR_INVALID_ARGUMENT
2581 * The total input length overflows the additional data length that
2582 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002583 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2584 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2585 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002586 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002587 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002588 * \retval #PSA_ERROR_BAD_STATE
2589 * The library has not been previously initialized by psa_crypto_init().
2590 * It is implementation-dependent whether a failure to initialize
2591 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002592 */
2593psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2594 const uint8_t *input,
2595 size_t input_length);
2596
2597/** Encrypt or decrypt a message fragment in an active AEAD operation.
2598 *
2599 * Before calling this function, you must:
2600 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2601 * The choice of setup function determines whether this function
2602 * encrypts or decrypts its input.
2603 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2604 * 3. Call psa_aead_update_ad() to pass all the additional data.
2605 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002606 * If this function returns an error status, the operation enters an error
2607 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002608 *
2609 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2610 * there is no guarantee that the input is valid. Therefore, until
2611 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2612 * - Do not use the output in any way other than storing it in a
2613 * confidential location. If you take any action that depends
2614 * on the tentative decrypted data, this action will need to be
2615 * undone if the input turns out not to be valid. Furthermore,
2616 * if an adversary can observe that this action took place
2617 * (for example through timing), they may be able to use this
2618 * fact as an oracle to decrypt any message encrypted with the
2619 * same key.
2620 * - In particular, do not copy the output anywhere but to a
2621 * memory or storage space that you have exclusive access to.
2622 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002623 * This function does not require the input to be aligned to any
2624 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002625 * a whole block at a time, it must consume all the input provided, but
2626 * it may delay the end of the corresponding output until a subsequent
2627 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2628 * provides sufficient input. The amount of data that can be delayed
2629 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002630 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002631 * \param[in,out] operation Active AEAD operation.
2632 * \param[in] input Buffer containing the message fragment to
2633 * encrypt or decrypt.
2634 * \param input_length Size of the \p input buffer in bytes.
2635 * \param[out] output Buffer where the output is to be written.
2636 * \param output_size Size of the \p output buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002637 * This must be appropriate for the selected
2638 * algorithm and key:
2639 * - A sufficient output size is
2640 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,
2641 * \c alg, \p input_length) where
2642 * \c key_type is the type of key and \c alg is
2643 * the algorithm that were used to set up the
2644 * operation.
2645 * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p
2646 * input_length) evaluates to the maximum
2647 * output size of any supported AEAD
2648 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002649 * \param[out] output_length On success, the number of bytes
2650 * that make up the returned output.
2651 *
2652 * \retval #PSA_SUCCESS
2653 * Success.
2654 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002655 * The operation state is not valid (it must be active, have a nonce
2656 * set, and have lengths set if required by the algorithm).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002657 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2658 * The size of the \p output buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002659 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or
2660 * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to
2661 * determine the required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002662 * \retval #PSA_ERROR_INVALID_ARGUMENT
2663 * The total length of input to psa_aead_update_ad() so far is
2664 * less than the additional data length that was previously
2665 * specified with psa_aead_set_lengths().
2666 * \retval #PSA_ERROR_INVALID_ARGUMENT
2667 * The total input length overflows the plaintext length that
2668 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002669 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2670 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2671 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002672 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002673 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002674 * \retval #PSA_ERROR_BAD_STATE
2675 * The library has not been previously initialized by psa_crypto_init().
2676 * It is implementation-dependent whether a failure to initialize
2677 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002678 */
2679psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2680 const uint8_t *input,
2681 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002682 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002683 size_t output_size,
2684 size_t *output_length);
2685
2686/** Finish encrypting a message in an AEAD operation.
2687 *
2688 * The operation must have been set up with psa_aead_encrypt_setup().
2689 *
2690 * This function finishes the authentication of the additional data
2691 * formed by concatenating the inputs passed to preceding calls to
2692 * psa_aead_update_ad() with the plaintext formed by concatenating the
2693 * inputs passed to preceding calls to psa_aead_update().
2694 *
2695 * This function has two output buffers:
2696 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002697 * preceding calls to psa_aead_update().
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002698 * - \p tag contains the authentication tag.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002699 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002700 * When this function returns successfuly, the operation becomes inactive.
2701 * If this function returns an error status, the operation enters an error
2702 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002703 *
2704 * \param[in,out] operation Active AEAD operation.
2705 * \param[out] ciphertext Buffer where the last part of the ciphertext
2706 * is to be written.
2707 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002708 * This must be appropriate for the selected
2709 * algorithm and key:
2710 * - A sufficient output size is
2711 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type,
2712 * \c alg) where \c key_type is the type of key
2713 * and \c alg is the algorithm that were used to
2714 * set up the operation.
2715 * - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to
2716 * the maximum output size of any supported AEAD
2717 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002718 * \param[out] ciphertext_length On success, the number of bytes of
2719 * returned ciphertext.
2720 * \param[out] tag Buffer where the authentication tag is
2721 * to be written.
2722 * \param tag_size Size of the \p tag buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002723 * This must be appropriate for the selected
2724 * algorithm and key:
2725 * - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c
2726 * key_type, \c key_bits, \c alg) where
2727 * \c key_type and \c key_bits are the type and
2728 * bit-size of the key, and \c alg is the
2729 * algorithm that were used in the call to
2730 * psa_aead_encrypt_setup().
2731 * - #PSA_AEAD_TAG_MAX_SIZE evaluates to the
2732 * maximum tag size of any supported AEAD
2733 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002734 * \param[out] tag_length On success, the number of bytes
2735 * that make up the returned tag.
2736 *
2737 * \retval #PSA_SUCCESS
2738 * Success.
2739 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002740 * The operation state is not valid (it must be an active encryption
2741 * operation with a nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002742 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002743 * The size of the \p ciphertext or \p tag buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002744 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or
2745 * #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the
2746 * required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type,
2747 * \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to
2748 * determine the required \p tag buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002749 * \retval #PSA_ERROR_INVALID_ARGUMENT
2750 * The total length of input to psa_aead_update_ad() so far is
2751 * less than the additional data length that was previously
2752 * specified with psa_aead_set_lengths().
2753 * \retval #PSA_ERROR_INVALID_ARGUMENT
2754 * The total length of input to psa_aead_update() so far is
2755 * less than the plaintext length that was previously
2756 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002757 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2758 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2759 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002760 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002761 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002762 * \retval #PSA_ERROR_BAD_STATE
2763 * The library has not been previously initialized by psa_crypto_init().
2764 * It is implementation-dependent whether a failure to initialize
2765 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002766 */
2767psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002768 uint8_t *ciphertext,
2769 size_t ciphertext_size,
2770 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002771 uint8_t *tag,
2772 size_t tag_size,
2773 size_t *tag_length);
2774
2775/** Finish authenticating and decrypting a message in an AEAD operation.
2776 *
2777 * The operation must have been set up with psa_aead_decrypt_setup().
2778 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002779 * This function finishes the authenticated decryption of the message
2780 * components:
2781 *
2782 * - The additional data consisting of the concatenation of the inputs
2783 * passed to preceding calls to psa_aead_update_ad().
2784 * - The ciphertext consisting of the concatenation of the inputs passed to
2785 * preceding calls to psa_aead_update().
2786 * - The tag passed to this function call.
2787 *
2788 * If the authentication tag is correct, this function outputs any remaining
2789 * plaintext and reports success. If the authentication tag is not correct,
2790 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002791 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002792 * When this function returns successfuly, the operation becomes inactive.
2793 * If this function returns an error status, the operation enters an error
2794 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002795 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002796 * \note Implementations shall make the best effort to ensure that the
2797 * comparison between the actual tag and the expected tag is performed
2798 * in constant time.
2799 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002800 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002801 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002802 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002803 * from previous calls to psa_aead_update()
2804 * that could not be processed until the end
2805 * of the input.
2806 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002807 * This must be appropriate for the selected algorithm and key:
2808 * - A sufficient output size is
2809 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type,
2810 * \c alg) where \c key_type is the type of key
2811 * and \c alg is the algorithm that were used to
2812 * set up the operation.
2813 * - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to
2814 * the maximum output size of any supported AEAD
2815 * algorithm.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002816 * \param[out] plaintext_length On success, the number of bytes of
2817 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002818 * \param[in] tag Buffer containing the authentication tag.
2819 * \param tag_length Size of the \p tag buffer in bytes.
2820 *
2821 * \retval #PSA_SUCCESS
2822 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002823 * \retval #PSA_ERROR_INVALID_SIGNATURE
2824 * The calculations were successful, but the authentication tag is
2825 * not correct.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002826 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002827 * The operation state is not valid (it must be an active decryption
2828 * operation with a nonce set).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002829 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2830 * The size of the \p plaintext buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002831 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or
2832 * #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the
2833 * required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002834 * \retval #PSA_ERROR_INVALID_ARGUMENT
2835 * The total length of input to psa_aead_update_ad() so far is
2836 * less than the additional data length that was previously
2837 * specified with psa_aead_set_lengths().
2838 * \retval #PSA_ERROR_INVALID_ARGUMENT
2839 * The total length of input to psa_aead_update() so far is
2840 * less than the plaintext length that was previously
2841 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002842 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2843 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2844 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002845 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002846 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002847 * \retval #PSA_ERROR_BAD_STATE
2848 * The library has not been previously initialized by psa_crypto_init().
2849 * It is implementation-dependent whether a failure to initialize
2850 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002851 */
2852psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002853 uint8_t *plaintext,
2854 size_t plaintext_size,
2855 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002856 const uint8_t *tag,
2857 size_t tag_length);
2858
2859/** Abort an AEAD operation.
2860 *
2861 * Aborting an operation frees all associated resources except for the
2862 * \p operation structure itself. Once aborted, the operation object
2863 * can be reused for another operation by calling
2864 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2865 *
2866 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002867 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002868 *
2869 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002870 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2871 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002872 *
2873 * \param[in,out] operation Initialized AEAD operation.
2874 *
2875 * \retval #PSA_SUCCESS
Gilles Peskine30a9e412019-01-14 18:36:12 +01002876 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2877 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002878 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002879 * \retval #PSA_ERROR_BAD_STATE
2880 * The library has not been previously initialized by psa_crypto_init().
2881 * It is implementation-dependent whether a failure to initialize
2882 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002883 */
2884psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2885
Gilles Peskine3b555712018-03-03 21:27:57 +01002886/**@}*/
2887
Gilles Peskine20035e32018-02-03 22:44:14 +01002888/** \defgroup asymmetric Asymmetric cryptography
2889 * @{
2890 */
2891
2892/**
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002893 * \brief Sign a message with a private key. For hash-and-sign algorithms,
2894 * this includes the hashing step.
2895 *
2896 * \note To perform a multi-part hash-and-sign signature algorithm, first use
2897 * a multi-part hash operation and then pass the resulting hash to
2898 * psa_sign_hash(). PSA_ALG_GET_HASH(\p alg) can be used to determine the
2899 * hash algorithm to use.
2900 *
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002901 * \param[in] key Identifier of the key to use for the operation.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002902 * It must be an asymmetric key pair. The key must
2903 * allow the usage #PSA_KEY_USAGE_SIGN_MESSAGE.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002904 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002905 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2906 * is true), that is compatible with the type of
2907 * \p key.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002908 * \param[in] input The input message to sign.
2909 * \param[in] input_length Size of the \p input buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002910 * \param[out] signature Buffer where the signature is to be written.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002911 * \param[in] signature_size Size of the \p signature buffer in bytes. This
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002912 * must be appropriate for the selected
2913 * algorithm and key:
2914 * - The required signature size is
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002915 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2916 * where \c key_type and \c key_bits are the type and
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002917 * bit-size respectively of key.
2918 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
2919 * maximum signature size of any supported
2920 * signature algorithm.
2921 * \param[out] signature_length On success, the number of bytes that make up
2922 * the returned signature value.
2923 *
2924 * \retval #PSA_SUCCESS
2925 * \retval #PSA_ERROR_INVALID_HANDLE
2926 * \retval #PSA_ERROR_NOT_PERMITTED
2927 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2928 * or it does not permit the requested algorithm.
2929 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2930 * The size of the \p signature buffer is too small. You can
2931 * determine a sufficient buffer size by calling
2932 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2933 * where \c key_type and \c key_bits are the type and bit-size
2934 * respectively of \p key.
2935 * \retval #PSA_ERROR_NOT_SUPPORTED
2936 * \retval #PSA_ERROR_INVALID_ARGUMENT
2937 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2938 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2939 * \retval #PSA_ERROR_HARDWARE_FAILURE
2940 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2941 * \retval #PSA_ERROR_STORAGE_FAILURE
2942 * \retval #PSA_ERROR_DATA_CORRUPT
2943 * \retval #PSA_ERROR_DATA_INVALID
2944 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2945 * \retval #PSA_ERROR_BAD_STATE
2946 * The library has not been previously initialized by psa_crypto_init().
2947 * It is implementation-dependent whether a failure to initialize
2948 * results in this error code.
2949 */
2950psa_status_t psa_sign_message( mbedtls_svc_key_id_t key,
2951 psa_algorithm_t alg,
2952 const uint8_t * input,
2953 size_t input_length,
2954 uint8_t * signature,
2955 size_t signature_size,
2956 size_t * signature_length );
2957
2958/** \brief Verify the signature of a message with a public key, using
2959 * a hash-and-sign verification algorithm.
2960 *
2961 * \note To perform a multi-part hash-and-sign signature verification
2962 * algorithm, first use a multi-part hash operation to hash the message
2963 * and then pass the resulting hash to psa_verify_hash().
2964 * PSA_ALG_GET_HASH(\p alg) can be used to determine the hash algorithm
2965 * to use.
2966 *
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002967 * \param[in] key Identifier of the key to use for the operation.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002968 * It must be a public key or an asymmetric key
2969 * pair. The key must allow the usage
2970 * #PSA_KEY_USAGE_VERIFY_MESSAGE.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002971 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002972 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2973 * is true), that is compatible with the type of
2974 * \p key.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002975 * \param[in] input The message whose signature is to be verified.
2976 * \param[in] input_length Size of the \p input buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002977 * \param[out] signature Buffer containing the signature to verify.
gabor-mezei-arm38cbaf22021-04-22 11:27:47 +02002978 * \param[in] signature_length Size of the \p signature buffer in bytes.
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002979 *
2980 * \retval #PSA_SUCCESS
2981 * \retval #PSA_ERROR_INVALID_HANDLE
2982 * \retval #PSA_ERROR_NOT_PERMITTED
2983 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2984 * or it does not permit the requested algorithm.
2985 * \retval #PSA_ERROR_INVALID_SIGNATURE
2986 * The calculation was performed successfully, but the passed signature
2987 * is not a valid signature.
2988 * \retval #PSA_ERROR_NOT_SUPPORTED
2989 * \retval #PSA_ERROR_INVALID_ARGUMENT
2990 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2991 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2992 * \retval #PSA_ERROR_HARDWARE_FAILURE
2993 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2994 * \retval #PSA_ERROR_STORAGE_FAILURE
2995 * \retval #PSA_ERROR_DATA_CORRUPT
2996 * \retval #PSA_ERROR_DATA_INVALID
2997 * \retval #PSA_ERROR_BAD_STATE
2998 * The library has not been previously initialized by psa_crypto_init().
2999 * It is implementation-dependent whether a failure to initialize
3000 * results in this error code.
3001 */
3002psa_status_t psa_verify_message( mbedtls_svc_key_id_t key,
3003 psa_algorithm_t alg,
3004 const uint8_t * input,
3005 size_t input_length,
3006 const uint8_t * signature,
3007 size_t signature_length );
3008
3009/**
Gilles Peskine20035e32018-02-03 22:44:14 +01003010 * \brief Sign a hash or short message with a private key.
3011 *
Gilles Peskine08bac712018-06-26 16:14:46 +02003012 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02003013 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01003014 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
3015 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02003016 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
3017 * to determine the hash algorithm to use.
3018 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003019 * \param key Identifier of the key to use for the operation.
3020 * It must be an asymmetric key pair. The key must
Ronald Cron96783552020-10-19 12:06:30 +02003021 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003022 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02003023 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003024 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003025 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003026 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003027 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003028 * \param[out] signature_length On success, the number of bytes
3029 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003030 *
Gilles Peskine28538492018-07-11 17:34:00 +02003031 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01003032 * \retval #PSA_ERROR_INVALID_HANDLE
3033 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003034 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003035 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01003036 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003037 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01003038 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003039 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003040 * \retval #PSA_ERROR_NOT_SUPPORTED
3041 * \retval #PSA_ERROR_INVALID_ARGUMENT
3042 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3043 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3044 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003045 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01003046 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003047 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03003048 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003049 * The library has not been previously initialized by psa_crypto_init().
3050 * It is implementation-dependent whether a failure to initialize
3051 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01003052 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003053psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003054 psa_algorithm_t alg,
3055 const uint8_t *hash,
3056 size_t hash_length,
3057 uint8_t *signature,
3058 size_t signature_size,
3059 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003060
3061/**
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003062 * \brief Verify the signature of a hash or short message using a public key.
Gilles Peskine20035e32018-02-03 22:44:14 +01003063 *
Gilles Peskine08bac712018-06-26 16:14:46 +02003064 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02003065 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01003066 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
3067 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02003068 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
3069 * to determine the hash algorithm to use.
3070 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003071 * \param key Identifier of the key to use for the operation. It
3072 * must be a public key or an asymmetric key pair. The
Ronald Cron96783552020-10-19 12:06:30 +02003073 * key must allow the usage
3074 * #PSA_KEY_USAGE_VERIFY_HASH.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003075 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02003076 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003077 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02003078 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003079 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003080 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003081 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003082 *
Gilles Peskine28538492018-07-11 17:34:00 +02003083 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01003084 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01003085 * \retval #PSA_ERROR_INVALID_HANDLE
3086 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003087 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01003088 * The calculation was perfomed successfully, but the passed
3089 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02003090 * \retval #PSA_ERROR_NOT_SUPPORTED
3091 * \retval #PSA_ERROR_INVALID_ARGUMENT
3092 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3093 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3094 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003095 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01003096 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003097 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003098 * The library has not been previously initialized by psa_crypto_init().
3099 * It is implementation-dependent whether a failure to initialize
3100 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01003101 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003102psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003103 psa_algorithm_t alg,
3104 const uint8_t *hash,
3105 size_t hash_length,
3106 const uint8_t *signature,
3107 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003108
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003109/**
3110 * \brief Encrypt a short message with a public key.
3111 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003112 * \param key Identifer of the key to use for the operation.
3113 * It must be a public key or an asymmetric key
3114 * pair. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02003115 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003116 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003117 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003118 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003119 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003120 * \param[in] salt A salt or label, if supported by the
3121 * encryption algorithm.
3122 * If the algorithm does not support a
3123 * salt, pass \c NULL.
3124 * If the algorithm supports an optional
3125 * salt and you do not want to pass a salt,
3126 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003127 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003128 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3129 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003130 * \param salt_length Size of the \p salt buffer in bytes.
3131 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003132 * \param[out] output Buffer where the encrypted message is to
3133 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003134 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003135 * \param[out] output_length On success, the number of bytes
3136 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003137 *
Gilles Peskine28538492018-07-11 17:34:00 +02003138 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01003139 * \retval #PSA_ERROR_INVALID_HANDLE
3140 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003141 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003142 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003143 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02003144 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003145 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003146 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003147 * \retval #PSA_ERROR_NOT_SUPPORTED
3148 * \retval #PSA_ERROR_INVALID_ARGUMENT
3149 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3150 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3151 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003152 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01003153 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003154 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03003155 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003156 * The library has not been previously initialized by psa_crypto_init().
3157 * It is implementation-dependent whether a failure to initialize
3158 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003159 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003160psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003161 psa_algorithm_t alg,
3162 const uint8_t *input,
3163 size_t input_length,
3164 const uint8_t *salt,
3165 size_t salt_length,
3166 uint8_t *output,
3167 size_t output_size,
3168 size_t *output_length);
3169
3170/**
3171 * \brief Decrypt a short message with a private key.
3172 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003173 * \param key Identifier of the key to use for the operation.
3174 * It must be an asymmetric key pair. It must
Ronald Cron96783552020-10-19 12:06:30 +02003175 * allow the usage #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003176 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003177 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003178 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003179 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003180 * \param[in] salt A salt or label, if supported by the
3181 * encryption algorithm.
3182 * If the algorithm does not support a
3183 * salt, pass \c NULL.
3184 * If the algorithm supports an optional
3185 * salt and you do not want to pass a salt,
3186 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003187 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003188 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3189 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003190 * \param salt_length Size of the \p salt buffer in bytes.
3191 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003192 * \param[out] output Buffer where the decrypted message is to
3193 * be written.
3194 * \param output_size Size of the \c output buffer in bytes.
3195 * \param[out] output_length On success, the number of bytes
3196 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003197 *
Gilles Peskine28538492018-07-11 17:34:00 +02003198 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003199 * \retval #PSA_ERROR_INVALID_HANDLE
3200 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003201 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003202 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003203 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003204 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003205 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003206 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003207 * \retval #PSA_ERROR_NOT_SUPPORTED
3208 * \retval #PSA_ERROR_INVALID_ARGUMENT
3209 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3210 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3211 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003212 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003213 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003214 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3215 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003216 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003217 * The library has not been previously initialized by psa_crypto_init().
3218 * It is implementation-dependent whether a failure to initialize
3219 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003220 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003221psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003222 psa_algorithm_t alg,
3223 const uint8_t *input,
3224 size_t input_length,
3225 const uint8_t *salt,
3226 size_t salt_length,
3227 uint8_t *output,
3228 size_t output_size,
3229 size_t *output_length);
3230
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003231/**@}*/
3232
Gilles Peskine35675b62019-05-16 17:26:11 +02003233/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003234 * @{
3235 */
3236
Gilles Peskine35675b62019-05-16 17:26:11 +02003237/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003238 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003239 * Before calling any function on a key derivation operation object, the
3240 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003241 * - Set the structure to all-bits-zero, for example:
3242 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003243 * psa_key_derivation_operation_t operation;
3244 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003245 * \endcode
3246 * - Initialize the structure to logical zero values, for example:
3247 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003248 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003249 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003250 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003251 * for example:
3252 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003253 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003254 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003255 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003256 * to the structure, for example:
3257 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003258 * psa_key_derivation_operation_t operation;
3259 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003260 * \endcode
3261 *
3262 * This is an implementation-defined \c struct. Applications should not
3263 * make any assumptions about the content of this structure except
3264 * as directed by the documentation of a specific implementation.
3265 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003266typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003267
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003268/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003269 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003270 * This macro returns a suitable initializer for a key derivation operation
3271 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003272 */
3273#ifdef __DOXYGEN_ONLY__
3274/* This is an example definition for documentation purposes.
3275 * Implementations should define a suitable value in `crypto_struct.h`.
3276 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003277#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003278#endif
3279
Gilles Peskine35675b62019-05-16 17:26:11 +02003280/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003281 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003282static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003283
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003284/** Set up a key derivation operation.
3285 *
3286 * A key derivation algorithm takes some inputs and uses them to generate
3287 * a byte stream in a deterministic way.
3288 * This byte stream can be used to produce keys and other
3289 * cryptographic material.
3290 *
3291 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003292 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3293 * -# Call psa_key_derivation_setup() to select the algorithm.
3294 * -# Provide the inputs for the key derivation by calling
3295 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3296 * as appropriate. Which inputs are needed, in what order, and whether
3297 * they may be keys and if so of what type depends on the algorithm.
3298 * -# Optionally set the operation's maximum capacity with
3299 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3300 * of or after providing inputs. For some algorithms, this step is mandatory
3301 * because the output depends on the maximum capacity.
3302 * -# To derive a key, call psa_key_derivation_output_key().
3303 * To derive a byte string for a different purpose, call
3304 * psa_key_derivation_output_bytes().
3305 * Successive calls to these functions use successive output bytes
3306 * calculated by the key derivation algorithm.
3307 * -# Clean up the key derivation operation object with
3308 * psa_key_derivation_abort().
3309 *
3310 * If this function returns an error, the key derivation operation object is
3311 * not changed.
3312 *
3313 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3314 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003315 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003316 * Implementations must reject an attempt to derive a key of size 0.
3317 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003318 * \param[in,out] operation The key derivation operation object
3319 * to set up. It must
3320 * have been initialized but not set up yet.
3321 * \param alg The key derivation algorithm to compute
3322 * (\c PSA_ALG_XXX value such that
3323 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3324 *
3325 * \retval #PSA_SUCCESS
3326 * Success.
3327 * \retval #PSA_ERROR_INVALID_ARGUMENT
3328 * \c alg is not a key derivation algorithm.
3329 * \retval #PSA_ERROR_NOT_SUPPORTED
3330 * \c alg is not supported or is not a key derivation algorithm.
3331 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3332 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3333 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003334 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003335 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003336 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003337 * The operation state is not valid (it must be inactive).
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003338 * \retval #PSA_ERROR_BAD_STATE
3339 * The library has not been previously initialized by psa_crypto_init().
3340 * It is implementation-dependent whether a failure to initialize
3341 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003342 */
3343psa_status_t psa_key_derivation_setup(
3344 psa_key_derivation_operation_t *operation,
3345 psa_algorithm_t alg);
3346
Gilles Peskine35675b62019-05-16 17:26:11 +02003347/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003348 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003349 * The capacity of a key derivation is the maximum number of bytes that it can
3350 * return. When you get *N* bytes of output from a key derivation operation,
3351 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003352 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003353 * \param[in] operation The operation to query.
3354 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003355 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003356 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003357 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003358 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003359 * The operation state is not valid (it must be active).
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003360 * \retval #PSA_ERROR_HARDWARE_FAILURE
3361 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003362 * \retval #PSA_ERROR_BAD_STATE
3363 * The library has not been previously initialized by psa_crypto_init().
3364 * It is implementation-dependent whether a failure to initialize
3365 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003366 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003367psa_status_t psa_key_derivation_get_capacity(
3368 const psa_key_derivation_operation_t *operation,
3369 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003370
Gilles Peskine35675b62019-05-16 17:26:11 +02003371/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003372 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003373 * The capacity of a key derivation operation is the maximum number of bytes
3374 * that the key derivation operation can return from this point onwards.
3375 *
3376 * \param[in,out] operation The key derivation operation object to modify.
3377 * \param capacity The new capacity of the operation.
3378 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003379 * current capacity.
3380 *
3381 * \retval #PSA_SUCCESS
3382 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003383 * \p capacity is larger than the operation's current capacity.
3384 * In this case, the operation object remains valid and its capacity
3385 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003386 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003387 * The operation state is not valid (it must be active).
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003388 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003389 * \retval #PSA_ERROR_HARDWARE_FAILURE
3390 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003391 * \retval #PSA_ERROR_BAD_STATE
3392 * The library has not been previously initialized by psa_crypto_init().
3393 * It is implementation-dependent whether a failure to initialize
3394 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003395 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003396psa_status_t psa_key_derivation_set_capacity(
3397 psa_key_derivation_operation_t *operation,
3398 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003399
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003400/** Use the maximum possible capacity for a key derivation operation.
3401 *
3402 * Use this value as the capacity argument when setting up a key derivation
3403 * to indicate that the operation should have the maximum possible capacity.
3404 * The value of the maximum possible capacity depends on the key derivation
3405 * algorithm.
3406 */
3407#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3408
3409/** Provide an input for key derivation or key agreement.
3410 *
3411 * Which inputs are required and in what order depends on the algorithm.
3412 * Refer to the documentation of each key derivation or key agreement
3413 * algorithm for information.
3414 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003415 * This function passes direct inputs, which is usually correct for
3416 * non-secret inputs. To pass a secret input, which should be in a key
3417 * object, call psa_key_derivation_input_key() instead of this function.
3418 * Refer to the documentation of individual step types
3419 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3420 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003421 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003422 * If this function returns an error status, the operation enters an error
3423 * state and must be aborted by calling psa_key_derivation_abort().
3424 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003425 * \param[in,out] operation The key derivation operation object to use.
3426 * It must have been set up with
3427 * psa_key_derivation_setup() and must not
3428 * have produced any output yet.
3429 * \param step Which step the input data is for.
3430 * \param[in] data Input data to use.
3431 * \param data_length Size of the \p data buffer in bytes.
3432 *
3433 * \retval #PSA_SUCCESS
3434 * Success.
3435 * \retval #PSA_ERROR_INVALID_ARGUMENT
3436 * \c step is not compatible with the operation's algorithm.
3437 * \retval #PSA_ERROR_INVALID_ARGUMENT
3438 * \c step does not allow direct inputs.
3439 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3440 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3441 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003442 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003443 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003444 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003445 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003446 * \retval #PSA_ERROR_BAD_STATE
3447 * The library has not been previously initialized by psa_crypto_init().
3448 * It is implementation-dependent whether a failure to initialize
3449 * results in this error code.
3450 */
3451psa_status_t psa_key_derivation_input_bytes(
3452 psa_key_derivation_operation_t *operation,
3453 psa_key_derivation_step_t step,
3454 const uint8_t *data,
3455 size_t data_length);
3456
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003457/** Provide a numeric input for key derivation or key agreement.
3458 *
3459 * Which inputs are required and in what order depends on the algorithm.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003460 * However, when an algorithm requires a particular order, numeric inputs
3461 * usually come first as they tend to be configuration parameters.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003462 * Refer to the documentation of each key derivation or key agreement
3463 * algorithm for information.
3464 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003465 * This function is used for inputs which are fixed-size non-negative
3466 * integers.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003467 *
3468 * If this function returns an error status, the operation enters an error
3469 * state and must be aborted by calling psa_key_derivation_abort().
3470 *
3471 * \param[in,out] operation The key derivation operation object to use.
3472 * It must have been set up with
3473 * psa_key_derivation_setup() and must not
3474 * have produced any output yet.
3475 * \param step Which step the input data is for.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003476 * \param[in] value The value of the numeric input.
Manuel Pégourié-Gonnard22f08bc2021-04-20 11:57:34 +02003477 *
3478 * \retval #PSA_SUCCESS
3479 * Success.
3480 * \retval #PSA_ERROR_INVALID_ARGUMENT
3481 * \c step is not compatible with the operation's algorithm.
3482 * \retval #PSA_ERROR_INVALID_ARGUMENT
3483 * \c step does not allow numeric inputs.
3484 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3485 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3486 * \retval #PSA_ERROR_HARDWARE_FAILURE
3487 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3488 * \retval #PSA_ERROR_STORAGE_FAILURE
3489 * \retval #PSA_ERROR_BAD_STATE
3490 * The operation state is not valid for this input \p step.
3491 * \retval #PSA_ERROR_BAD_STATE
3492 * The library has not been previously initialized by psa_crypto_init().
3493 * It is implementation-dependent whether a failure to initialize
3494 * results in this error code.
3495 */
3496psa_status_t psa_key_derivation_input_integer(
3497 psa_key_derivation_operation_t *operation,
3498 psa_key_derivation_step_t step,
3499 uint64_t value);
3500
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003501/** Provide an input for key derivation in the form of a key.
3502 *
3503 * Which inputs are required and in what order depends on the algorithm.
3504 * Refer to the documentation of each key derivation or key agreement
3505 * algorithm for information.
3506 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003507 * This function obtains input from a key object, which is usually correct for
3508 * secret inputs or for non-secret personalization strings kept in the key
3509 * store. To pass a non-secret parameter which is not in the key store,
3510 * call psa_key_derivation_input_bytes() instead of this function.
3511 * Refer to the documentation of individual step types
3512 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3513 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003514 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003515 * If this function returns an error status, the operation enters an error
3516 * state and must be aborted by calling psa_key_derivation_abort().
3517 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003518 * \param[in,out] operation The key derivation operation object to use.
3519 * It must have been set up with
3520 * psa_key_derivation_setup() and must not
3521 * have produced any output yet.
3522 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003523 * \param key Identifier of the key. It must have an
3524 * appropriate type for step and must allow the
Manuel Pégourié-Gonnardacfde462021-05-05 09:54:22 +02003525 * usage #PSA_KEY_USAGE_DERIVE or
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003526 * #PSA_KEY_USAGE_VERIFY_DERIVATION (see note)
3527 * and the algorithm used by the operation.
3528 *
3529 * \note Once all inputs steps are completed, the operations will allow:
3530 * - psa_key_derivation_output_bytes() if each input was either a direct input
3531 * or a key with #PSA_KEY_USAGE_DERIVE set;
Manuel Pégourié-Gonnarde88511d2021-05-07 12:19:03 +02003532 * - psa_key_derivation_output_key() if the input for step
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003533 * #PSA_KEY_DERIVATION_INPUT_SECRET or #PSA_KEY_DERIVATION_INPUT_PASSWORD
Manuel Pégourié-Gonnarde88511d2021-05-07 12:19:03 +02003534 * was from a key slot with #PSA_KEY_USAGE_DERIVE and each other input was
3535 * either a direct input or a key with #PSA_KEY_USAGE_DERIVE set;
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003536 * - psa_key_derivation_verify_bytes() if each input was either a direct input
3537 * or a key with #PSA_KEY_USAGE_VERIFY_DERIVATION set;
Manuel Pégourié-Gonnard4feb6112021-05-07 12:22:21 +02003538 * - psa_key_derivation_verify_key() under the same conditions as
3539 * psa_key_derivation_verify_bytes().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003540 *
3541 * \retval #PSA_SUCCESS
3542 * Success.
3543 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003544 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003545 * The key allows neither #PSA_KEY_USAGE_DERIVE nor
3546 * #PSA_KEY_USAGE_VERIFY_DERIVATION, or it doesn't allow this
3547 * algorithm.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003548 * \retval #PSA_ERROR_INVALID_ARGUMENT
3549 * \c step is not compatible with the operation's algorithm.
3550 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine224b0d62019-09-23 18:13:17 +02003551 * \c step does not allow key inputs of the given type
3552 * or does not allow key inputs at all.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003553 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3554 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3555 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003556 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003557 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003558 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003559 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003560 * \retval #PSA_ERROR_BAD_STATE
3561 * The library has not been previously initialized by psa_crypto_init().
3562 * It is implementation-dependent whether a failure to initialize
3563 * results in this error code.
3564 */
3565psa_status_t psa_key_derivation_input_key(
3566 psa_key_derivation_operation_t *operation,
3567 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003568 mbedtls_svc_key_id_t key);
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003569
3570/** Perform a key agreement and use the shared secret as input to a key
3571 * derivation.
3572 *
3573 * A key agreement algorithm takes two inputs: a private key \p private_key
3574 * a public key \p peer_key.
3575 * The result of this function is passed as input to a key derivation.
3576 * The output of this key derivation can be extracted by reading from the
3577 * resulting operation to produce keys and other cryptographic material.
3578 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003579 * If this function returns an error status, the operation enters an error
3580 * state and must be aborted by calling psa_key_derivation_abort().
3581 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003582 * \param[in,out] operation The key derivation operation object to use.
3583 * It must have been set up with
3584 * psa_key_derivation_setup() with a
3585 * key agreement and derivation algorithm
3586 * \c alg (\c PSA_ALG_XXX value such that
3587 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3588 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3589 * is false).
3590 * The operation must be ready for an
3591 * input of the type given by \p step.
3592 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003593 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003594 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003595 * \param[in] peer_key Public key of the peer. The peer key must be in the
3596 * same format that psa_import_key() accepts for the
3597 * public key type corresponding to the type of
3598 * private_key. That is, this function performs the
3599 * equivalent of
3600 * #psa_import_key(...,
3601 * `peer_key`, `peer_key_length`) where
3602 * with key attributes indicating the public key
3603 * type corresponding to the type of `private_key`.
3604 * For example, for EC keys, this means that peer_key
3605 * is interpreted as a point on the curve that the
3606 * private key is on. The standard formats for public
3607 * keys are documented in the documentation of
3608 * psa_export_public_key().
3609 * \param peer_key_length Size of \p peer_key in bytes.
3610 *
3611 * \retval #PSA_SUCCESS
3612 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01003613 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003614 * The operation state is not valid for this key agreement \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003615 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003616 * \retval #PSA_ERROR_NOT_PERMITTED
3617 * \retval #PSA_ERROR_INVALID_ARGUMENT
3618 * \c private_key is not compatible with \c alg,
3619 * or \p peer_key is not valid for \c alg or not compatible with
3620 * \c private_key.
3621 * \retval #PSA_ERROR_NOT_SUPPORTED
3622 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskine224b0d62019-09-23 18:13:17 +02003623 * \retval #PSA_ERROR_INVALID_ARGUMENT
3624 * \c step does not allow an input resulting from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003625 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3626 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3627 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003628 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003629 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003630 * \retval #PSA_ERROR_BAD_STATE
3631 * The library has not been previously initialized by psa_crypto_init().
3632 * It is implementation-dependent whether a failure to initialize
3633 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003634 */
3635psa_status_t psa_key_derivation_key_agreement(
3636 psa_key_derivation_operation_t *operation,
3637 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003638 mbedtls_svc_key_id_t private_key,
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003639 const uint8_t *peer_key,
3640 size_t peer_key_length);
3641
Gilles Peskine35675b62019-05-16 17:26:11 +02003642/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003643 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003644 * This function calculates output bytes from a key derivation algorithm and
3645 * return those bytes.
3646 * If you view the key derivation's output as a stream of bytes, this
3647 * function destructively reads the requested number of bytes from the
3648 * stream.
3649 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003650 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003651 * If this function returns an error status other than
3652 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003653 * state and must be aborted by calling psa_key_derivation_abort().
3654 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003655 * \param[in,out] operation The key derivation operation object to read from.
3656 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003657 * \param output_length Number of bytes to output.
3658 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003659 * \retval #PSA_SUCCESS
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003660 * \retval #PSA_ERROR_NOT_PERMITTED
3661 * One of the inputs was a key whose policy didn't allow
3662 * #PSA_KEY_USAGE_DERIVE.
David Saadab4ecc272019-02-14 13:48:10 +02003663 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003664 * The operation's capacity was less than
3665 * \p output_length bytes. Note that in this case,
3666 * no output is written to the output buffer.
3667 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003668 * subsequent calls to this function will not
3669 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003670 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003671 * The operation state is not valid (it must be active and completed
3672 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003673 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3674 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3675 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003676 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003677 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003678 * \retval #PSA_ERROR_BAD_STATE
3679 * The library has not been previously initialized by psa_crypto_init().
3680 * It is implementation-dependent whether a failure to initialize
3681 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003682 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003683psa_status_t psa_key_derivation_output_bytes(
3684 psa_key_derivation_operation_t *operation,
3685 uint8_t *output,
3686 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003687
Gilles Peskine35675b62019-05-16 17:26:11 +02003688/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003689 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003690 * This function calculates output bytes from a key derivation algorithm
3691 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003692 * The key's location, usage policy, type and size are taken from
3693 * \p attributes.
3694 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003695 * If you view the key derivation's output as a stream of bytes, this
3696 * function destructively reads as many bytes as required from the
3697 * stream.
3698 * The operation's capacity decreases by the number of bytes read.
3699 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003700 * If this function returns an error status other than
3701 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003702 * state and must be aborted by calling psa_key_derivation_abort().
3703 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003704 * How much output is produced and consumed from the operation, and how
Gilles Peskine364d12c2021-03-08 17:23:47 +01003705 * the key is derived, depends on the key type and on the key size
3706 * (denoted \c bits below):
Gilles Peskineeab56e42018-07-12 17:12:33 +02003707 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003708 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003709 * of a given size, this function is functionally equivalent to
3710 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003711 * and passing the resulting output to #psa_import_key.
3712 * However, this function has a security benefit:
3713 * if the implementation provides an isolation boundary then
3714 * the key material is not exposed outside the isolation boundary.
3715 * As a consequence, for these key types, this function always consumes
Gilles Peskine364d12c2021-03-08 17:23:47 +01003716 * exactly (\c bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003717 * The following key types defined in this specification follow this scheme:
3718 *
3719 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003720 * - #PSA_KEY_TYPE_ARC4;
3721 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003722 * - #PSA_KEY_TYPE_DERIVE;
Manuel Pégourié-Gonnard2171e422021-05-03 10:49:54 +02003723 * - #PSA_KEY_TYPE_HMAC;
3724 * - #PSA_KEY_TYPE_PASSWORD_HASH.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003725 *
3726 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003727 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003728 * Montgomery curve), this function always draws a byte string whose
3729 * length is determined by the curve, and sets the mandatory bits
3730 * accordingly. That is:
3731 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01003732 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003733 * string and process it as specified in RFC 7748 &sect;5.
Paul Elliott8ff510a2020-06-02 17:19:28 +01003734 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003735 * string and process it as specified in RFC 7748 &sect;5.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003736 *
3737 * - For key types for which the key is represented by a single sequence of
Gilles Peskine364d12c2021-03-08 17:23:47 +01003738 * \c bits bits with constraints as to which bit sequences are acceptable,
3739 * this function draws a byte string of length (\c bits / 8) bytes rounded
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003740 * up to the nearest whole number of bytes. If the resulting byte string
3741 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3742 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003743 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003744 * for the output produced by psa_export_key().
3745 * The following key types defined in this specification follow this scheme:
3746 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003747 * - #PSA_KEY_TYPE_DES.
3748 * Force-set the parity bits, but discard forbidden weak keys.
3749 * For 2-key and 3-key triple-DES, the three keys are generated
3750 * successively (for example, for 3-key triple-DES,
3751 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3752 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003753 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003754 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003755 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003756 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003757 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003758 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003759 * Weierstrass curve).
3760 * For these key types, interpret the byte string as integer
3761 * in big-endian order. Discard it if it is not in the range
3762 * [0, *N* - 2] where *N* is the boundary of the private key domain
3763 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003764 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003765 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003766 * This method allows compliance to NIST standards, specifically
3767 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003768 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3769 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3770 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3771 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003772 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003773 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003774 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003775 * implementation-defined.
3776 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003777 * In all cases, the data that is read is discarded from the operation.
3778 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003779 *
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003780 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3781 * the input to that step must be provided with psa_key_derivation_input_key().
3782 * Future versions of this specification may include additional restrictions
3783 * on the derived key based on the attributes and strength of the secret key.
3784 *
Gilles Peskine20628592019-04-19 19:29:50 +02003785 * \param[in] attributes The attributes for the new key.
Manuel Pégourié-Gonnard2c44daf2021-05-10 12:53:30 +02003786 * If the key type to be created is
3787 * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
3788 * the policy must be the same as in the current
3789 * operation.
Gilles Peskine35675b62019-05-16 17:26:11 +02003790 * \param[in,out] operation The key derivation operation object to read from.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003791 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003792 * key. For persistent keys, this is the key
3793 * identifier defined in \p attributes.
3794 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003795 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003796 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003797 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003798 * If the key is persistent, the key material and the key's metadata
3799 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003800 * \retval #PSA_ERROR_ALREADY_EXISTS
3801 * This is an attempt to create a persistent key, and there is
3802 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003803 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003804 * There was not enough data to create the desired key.
3805 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003806 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003807 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003808 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003809 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003810 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003811 * \retval #PSA_ERROR_INVALID_ARGUMENT
3812 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003813 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003814 * The #PSA_KEY_DERIVATION_INPUT_SECRET or
3815 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
3816 * key; or one of the inputs was a key whose policy didn't allow
3817 * #PSA_KEY_USAGE_DERIVE.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003818 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003819 * The operation state is not valid (it must be active and completed
3820 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003821 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3822 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3823 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3824 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003825 * \retval #PSA_ERROR_CORRUPTION_DETECTED
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01003826 * \retval #PSA_ERROR_DATA_INVALID
3827 * \retval #PSA_ERROR_DATA_CORRUPT
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003828 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003829 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003830 * The library has not been previously initialized by psa_crypto_init().
3831 * It is implementation-dependent whether a failure to initialize
3832 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003833 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003834psa_status_t psa_key_derivation_output_key(
3835 const psa_key_attributes_t *attributes,
3836 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003837 mbedtls_svc_key_id_t *key);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003838
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003839/** Compare output data from a key derivation operation to an expected value.
3840 *
3841 * This function calculates output bytes from a key derivation algorithm and
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003842 * compares those bytes to an expected value in constant time.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003843 * If you view the key derivation's output as a stream of bytes, this
3844 * function destructively reads the requested number of bytes from the
3845 * stream before comparing them.
3846 * The operation's capacity decreases by the number of bytes read.
3847 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003848 * This is functionally equivalent to the following code:
3849 * \code
3850 * psa_key_derivation_output_bytes(operation, tmp, output_length);
3851 * if (memcmp(output, tmp, output_length) != 0)
3852 * return PSA_ERROR_INVALID_SIGNATURE;
3853 * \endcode
3854 * except (1) it works even if the key's policy does not allow outputting the
3855 * bytes, and (2) the comparison will be done in constant time.
3856 *
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003857 * If this function returns an error status other than
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003858 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3859 * the operation enters an error state and must be aborted by calling
3860 * psa_key_derivation_abort().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003861 *
3862 * \param[in,out] operation The key derivation operation object to read from.
3863 * \param[in] expected_output Buffer where the output will be written.
3864 * \param output_length Length ot the expected output; this is also the
3865 * number of bytes that will be read.
3866 *
3867 * \retval #PSA_SUCCESS
3868 * \retval #PSA_ERROR_INVALID_SIGNATURE
3869 * The output was read successfully, but if differs from the expected
3870 * output.
Manuel Pégourié-Gonnardece90872021-05-05 10:26:29 +02003871 * \retval #PSA_ERROR_NOT_PERMITTED
3872 * One of the inputs was a key whose policy didn't allow
3873 * #PSA_KEY_USAGE_VERIFY_DERIVATION.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003874 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3875 * The operation's capacity was less than
3876 * \p output_length bytes. Note that in this case,
3877 * the operation's capacity is set to 0, thus
3878 * subsequent calls to this function will not
3879 * succeed, even with a smaller output buffer.
3880 * \retval #PSA_ERROR_BAD_STATE
3881 * The operation state is not valid (it must be active and completed
3882 * all required input steps).
3883 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3884 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3885 * \retval #PSA_ERROR_HARDWARE_FAILURE
3886 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3887 * \retval #PSA_ERROR_STORAGE_FAILURE
3888 * \retval #PSA_ERROR_BAD_STATE
3889 * The library has not been previously initialized by psa_crypto_init().
3890 * It is implementation-dependent whether a failure to initialize
3891 * results in this error code.
3892 */
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003893psa_status_t psa_key_derivation_verify_bytes(
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003894 psa_key_derivation_operation_t *operation,
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003895 const uint8_t *expected_output,
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003896 size_t output_length);
3897
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003898/** Compare output data from a key derivation operation to an expected value
3899 * stored in a key object.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003900 *
3901 * This function calculates output bytes from a key derivation algorithm and
3902 * compares those bytes to an expected value, provided as key of type
Manuel Pégourié-Gonnard4e02f012021-05-12 10:05:45 +02003903 * #PSA_KEY_TYPE_PASSWORD_HASH.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003904 * If you view the key derivation's output as a stream of bytes, this
3905 * function destructively reads the number of bytes corresponding the the
3906 * length of the expected value from the stream before comparing them.
3907 * The operation's capacity decreases by the number of bytes read.
3908 *
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003909 * This is functionally equivalent to exporting the key and calling
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003910 * psa_key_derivation_verify_bytes() on the result, except that it
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003911 * works even if the key cannot be exported.
3912 *
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003913 * If this function returns an error status other than
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003914 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3915 * the operation enters an error state and must be aborted by calling
3916 * psa_key_derivation_abort().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003917 *
3918 * \param[in,out] operation The key derivation operation object to read from.
Manuel Pégourié-Gonnard2171e422021-05-03 10:49:54 +02003919 * \param[in] expected A key of type #PSA_KEY_TYPE_PASSWORD_HASH
3920 * containing the expected output. Its policy must
3921 * include the #PSA_KEY_USAGE_VERIFY_DERIVATION flag
3922 * and the permitted algorithm must match the
3923 * operation. The value of this key was likely
3924 * computed by a previous call to
3925 * psa_key_derivation_output_key().
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003926 *
3927 * \retval #PSA_SUCCESS
3928 * \retval #PSA_ERROR_INVALID_SIGNATURE
3929 * The output was read successfully, but if differs from the expected
3930 * output.
Manuel Pégourié-Gonnardffc86ce2021-04-30 11:37:57 +02003931 * \retval #PSA_ERROR_INVALID_HANDLE
3932 * The key passed as the expected value does not exist.
3933 * \retval #PSA_ERROR_INVALID_ARGUMENT
3934 * The key passed as the expected value has an invalid type.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003935 * \retval #PSA_ERROR_NOT_PERMITTED
Manuel Pégourié-Gonnard88658be2021-05-03 10:28:57 +02003936 * The key passed as the expected value does not allow this usage or
Manuel Pégourié-Gonnard4feb6112021-05-07 12:22:21 +02003937 * this algorithm; or one of the inputs was a key whose policy didn't
3938 * allow #PSA_KEY_USAGE_VERIFY_DERIVATION.
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003939 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3940 * The operation's capacity was less than
3941 * the length of the expected value. In this case,
3942 * the operation's capacity is set to 0, thus
3943 * subsequent calls to this function will not
3944 * succeed, even with a smaller output buffer.
3945 * \retval #PSA_ERROR_BAD_STATE
3946 * The operation state is not valid (it must be active and completed
3947 * all required input steps).
3948 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3949 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3950 * \retval #PSA_ERROR_HARDWARE_FAILURE
3951 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3952 * \retval #PSA_ERROR_STORAGE_FAILURE
3953 * \retval #PSA_ERROR_BAD_STATE
3954 * The library has not been previously initialized by psa_crypto_init().
3955 * It is implementation-dependent whether a failure to initialize
3956 * results in this error code.
3957 */
Manuel Pégourié-Gonnardd307f632021-05-03 10:12:06 +02003958psa_status_t psa_key_derivation_verify_key(
Manuel Pégourié-Gonnard49325d32021-04-20 12:16:17 +02003959 psa_key_derivation_operation_t *operation,
3960 psa_key_id_t expected);
3961
Gilles Peskine35675b62019-05-16 17:26:11 +02003962/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003963 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003964 * Aborting an operation frees all associated resources except for the \c
3965 * operation structure itself. Once aborted, the operation object can be reused
3966 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003967 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003968 * This function may be called at any time after the operation
3969 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003970 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003971 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3972 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003973 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003974 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003975 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003976 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003977 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3978 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003979 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003980 * \retval #PSA_ERROR_BAD_STATE
3981 * The library has not been previously initialized by psa_crypto_init().
3982 * It is implementation-dependent whether a failure to initialize
3983 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003984 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003985psa_status_t psa_key_derivation_abort(
3986 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003987
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003988/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003989 *
3990 * \warning The raw result of a key agreement algorithm such as finite-field
3991 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3992 * not be used directly as key material. It should instead be passed as
3993 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003994 * a key derivation, use psa_key_derivation_key_agreement() and other
3995 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003996 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003997 * \param alg The key agreement algorithm to compute
3998 * (\c PSA_ALG_XXX value such that
3999 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
4000 * is true).
Ronald Croncf56a0a2020-08-04 09:51:30 +02004001 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02004002 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine769c7a62019-01-18 16:42:29 +01004003 * \param[in] peer_key Public key of the peer. It must be
4004 * in the same format that psa_import_key()
4005 * accepts. The standard formats for public
4006 * keys are documented in the documentation
4007 * of psa_export_public_key().
4008 * \param peer_key_length Size of \p peer_key in bytes.
4009 * \param[out] output Buffer where the decrypted message is to
4010 * be written.
4011 * \param output_size Size of the \c output buffer in bytes.
4012 * \param[out] output_length On success, the number of bytes
4013 * that make up the returned output.
4014 *
4015 * \retval #PSA_SUCCESS
4016 * Success.
4017 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01004018 * \retval #PSA_ERROR_NOT_PERMITTED
4019 * \retval #PSA_ERROR_INVALID_ARGUMENT
4020 * \p alg is not a key agreement algorithm
4021 * \retval #PSA_ERROR_INVALID_ARGUMENT
4022 * \p private_key is not compatible with \p alg,
4023 * or \p peer_key is not valid for \p alg or not compatible with
4024 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01004025 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
4026 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01004027 * \retval #PSA_ERROR_NOT_SUPPORTED
4028 * \p alg is not a supported key agreement algorithm.
4029 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
4030 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
4031 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02004032 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01004033 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01004034 * \retval #PSA_ERROR_BAD_STATE
4035 * The library has not been previously initialized by psa_crypto_init().
4036 * It is implementation-dependent whether a failure to initialize
4037 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01004038 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02004039psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004040 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02004041 const uint8_t *peer_key,
4042 size_t peer_key_length,
4043 uint8_t *output,
4044 size_t output_size,
4045 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02004046
Gilles Peskineea0fb492018-07-12 17:17:20 +02004047/**@}*/
4048
Gilles Peskineedd76872018-07-20 17:42:05 +02004049/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004050 * @{
4051 */
4052
4053/**
4054 * \brief Generate random bytes.
4055 *
4056 * \warning This function **can** fail! Callers MUST check the return status
4057 * and MUST NOT use the content of the output buffer if the return
4058 * status is not #PSA_SUCCESS.
4059 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004060 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004061 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02004062 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004063 * \param output_size Number of bytes to generate and output.
4064 *
Gilles Peskine28538492018-07-11 17:34:00 +02004065 * \retval #PSA_SUCCESS
4066 * \retval #PSA_ERROR_NOT_SUPPORTED
4067 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01004068 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02004069 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
4070 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02004071 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03004072 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03004073 * The library has not been previously initialized by psa_crypto_init().
4074 * It is implementation-dependent whether a failure to initialize
4075 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004076 */
4077psa_status_t psa_generate_random(uint8_t *output,
4078 size_t output_size);
4079
4080/**
4081 * \brief Generate a key or key pair.
4082 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02004083 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02004084 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02004085 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02004086 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02004087 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02004088 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004089 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02004090 * the public exponent is 65537.
4091 * The modulus is a product of two probabilistic primes
4092 * between 2^{n-1} and 2^n where n is the bit size specified in the
4093 * attributes.
4094 *
Gilles Peskine20628592019-04-19 19:29:50 +02004095 * \param[in] attributes The attributes for the new key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02004096 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02004097 * key. For persistent keys, this is the key
4098 * identifier defined in \p attributes.
4099 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004100 *
Gilles Peskine28538492018-07-11 17:34:00 +02004101 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01004102 * Success.
4103 * If the key is persistent, the key material and the key's metadata
4104 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02004105 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02004106 * This is an attempt to create a persistent key, and there is
4107 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02004108 * \retval #PSA_ERROR_NOT_SUPPORTED
4109 * \retval #PSA_ERROR_INVALID_ARGUMENT
4110 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
4111 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
4112 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
4113 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02004114 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01004115 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01004116 * \retval #PSA_ERROR_DATA_INVALID
4117 * \retval #PSA_ERROR_DATA_CORRUPT
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01004118 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03004119 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03004120 * The library has not been previously initialized by psa_crypto_init().
4121 * It is implementation-dependent whether a failure to initialize
4122 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004123 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004124psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004125 mbedtls_svc_key_id_t *key);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02004126
4127/**@}*/
4128
Gilles Peskinee59236f2018-01-27 23:32:46 +01004129#ifdef __cplusplus
4130}
4131#endif
4132
Gilles Peskine0cad07c2018-06-27 19:49:02 +02004133/* The file "crypto_sizes.h" contains definitions for size calculation
4134 * macros whose definitions are implementation-specific. */
4135#include "crypto_sizes.h"
4136
Gilles Peskine9ef733f2018-02-07 21:05:37 +01004137/* The file "crypto_struct.h" contains definitions for
4138 * implementation-specific structs that are declared above. */
4139#include "crypto_struct.h"
4140
4141/* The file "crypto_extra.h" contains vendor-specific definitions. This
4142 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004143#include "crypto_extra.h"
4144
4145#endif /* PSA_CRYPTO_H */