blob: 98de3359e3391166751de7080cf54afd73ff8316 [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
2139 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002140 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2141 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002142 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002143 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002144 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002145 * The library has not been previously initialized by psa_crypto_init().
2146 * It is implementation-dependent whether a failure to initialize
2147 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002148 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002149psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002150 psa_algorithm_t alg,
2151 const uint8_t *nonce,
2152 size_t nonce_length,
2153 const uint8_t *additional_data,
2154 size_t additional_data_length,
2155 const uint8_t *plaintext,
2156 size_t plaintext_length,
2157 uint8_t *ciphertext,
2158 size_t ciphertext_size,
2159 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002160
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002161/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002162 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002163 * \param key Identifier of the key to use for the
2164 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002165 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002166 * \param alg The AEAD algorithm to compute
2167 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002168 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002169 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002170 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002171 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002172 * but not encrypted.
2173 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002174 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002175 * encrypted. For algorithms where the
2176 * encrypted data and the authentication tag
2177 * are defined as separate inputs, the buffer
2178 * must contain the encrypted data followed
2179 * by the authentication tag.
2180 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002181 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002182 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002183 * This must be appropriate for the selected
2184 * algorithm and key:
2185 * - A sufficient output size is
2186 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type,
2187 * \p alg, \p ciphertext_length) where
2188 * \c key_type is the type of \p key.
2189 * - #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p
2190 * ciphertext_length) evaluates to the maximum
2191 * plaintext size of any supported AEAD
2192 * decryption.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002193 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002194 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002195 *
Gilles Peskine28538492018-07-11 17:34:00 +02002196 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002197 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002198 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002199 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002200 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002201 * \retval #PSA_ERROR_NOT_PERMITTED
2202 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002203 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002204 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002205 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002206 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002207 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2208 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002209 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2210 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002211 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002212 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002213 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002214 * The library has not been previously initialized by psa_crypto_init().
2215 * It is implementation-dependent whether a failure to initialize
2216 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002217 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002218psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002219 psa_algorithm_t alg,
2220 const uint8_t *nonce,
2221 size_t nonce_length,
2222 const uint8_t *additional_data,
2223 size_t additional_data_length,
2224 const uint8_t *ciphertext,
2225 size_t ciphertext_length,
2226 uint8_t *plaintext,
2227 size_t plaintext_size,
2228 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002229
Gilles Peskine30a9e412019-01-14 18:36:12 +01002230/** The type of the state data structure for multipart AEAD operations.
2231 *
2232 * Before calling any function on an AEAD operation object, the application
2233 * must initialize it by any of the following means:
2234 * - Set the structure to all-bits-zero, for example:
2235 * \code
2236 * psa_aead_operation_t operation;
2237 * memset(&operation, 0, sizeof(operation));
2238 * \endcode
2239 * - Initialize the structure to logical zero values, for example:
2240 * \code
2241 * psa_aead_operation_t operation = {0};
2242 * \endcode
2243 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2244 * for example:
2245 * \code
2246 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2247 * \endcode
2248 * - Assign the result of the function psa_aead_operation_init()
2249 * to the structure, for example:
2250 * \code
2251 * psa_aead_operation_t operation;
2252 * operation = psa_aead_operation_init();
2253 * \endcode
2254 *
2255 * This is an implementation-defined \c struct. Applications should not
2256 * make any assumptions about the content of this structure except
2257 * as directed by the documentation of a specific implementation. */
2258typedef struct psa_aead_operation_s psa_aead_operation_t;
2259
2260/** \def PSA_AEAD_OPERATION_INIT
2261 *
2262 * This macro returns a suitable initializer for an AEAD operation object of
2263 * type #psa_aead_operation_t.
2264 */
2265#ifdef __DOXYGEN_ONLY__
2266/* This is an example definition for documentation purposes.
2267 * Implementations should define a suitable value in `crypto_struct.h`.
2268 */
2269#define PSA_AEAD_OPERATION_INIT {0}
2270#endif
2271
2272/** Return an initial value for an AEAD operation object.
2273 */
2274static psa_aead_operation_t psa_aead_operation_init(void);
2275
2276/** Set the key for a multipart authenticated encryption operation.
2277 *
2278 * The sequence of operations to encrypt a message with authentication
2279 * is as follows:
2280 * -# Allocate an operation object which will be passed to all the functions
2281 * listed here.
2282 * -# Initialize the operation object with one of the methods described in the
2283 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002284 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002285 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002286 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2287 * inputs to the subsequent calls to psa_aead_update_ad() and
2288 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2289 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002290 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2291 * generate or set the nonce. You should use
2292 * psa_aead_generate_nonce() unless the protocol you are implementing
2293 * requires a specific nonce value.
2294 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2295 * of the non-encrypted additional authenticated data each time.
2296 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002297 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002298 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002299 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002300 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2301 * the operation will need to be reset by a call to psa_aead_abort(). The
2302 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002303 * has been initialized.
2304 *
2305 * After a successful call to psa_aead_encrypt_setup(), the application must
2306 * eventually terminate the operation. The following events terminate an
2307 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002308 * - A successful call to psa_aead_finish().
2309 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002310 *
2311 * \param[in,out] operation The operation object to set up. It must have
2312 * been initialized as per the documentation for
2313 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002314 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002315 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002316 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002317 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002318 * \param alg The AEAD algorithm to compute
2319 * (\c PSA_ALG_XXX value such that
2320 * #PSA_ALG_IS_AEAD(\p alg) is true).
2321 *
2322 * \retval #PSA_SUCCESS
2323 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002324 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002325 * The operation state is not valid (it must be inactive).
Ronald Cron96783552020-10-19 12:06:30 +02002326 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002327 * \retval #PSA_ERROR_NOT_PERMITTED
2328 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002329 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002330 * \retval #PSA_ERROR_NOT_SUPPORTED
2331 * \p alg is not supported or is not an AEAD algorithm.
2332 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2333 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2334 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002335 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002336 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002337 * \retval #PSA_ERROR_BAD_STATE
2338 * The library has not been previously initialized by psa_crypto_init().
2339 * It is implementation-dependent whether a failure to initialize
2340 * results in this error code.
2341 */
2342psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002343 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002344 psa_algorithm_t alg);
2345
2346/** Set the key for a multipart authenticated decryption operation.
2347 *
2348 * The sequence of operations to decrypt a message with authentication
2349 * is as follows:
2350 * -# Allocate an operation object which will be passed to all the functions
2351 * listed here.
2352 * -# Initialize the operation object with one of the methods described in the
2353 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002354 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002355 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002356 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2357 * inputs to the subsequent calls to psa_aead_update_ad() and
2358 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2359 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002360 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2361 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2362 * of the non-encrypted additional authenticated data each time.
2363 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002364 * of the ciphertext to decrypt each time.
2365 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002366 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002367 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2368 * the operation will need to be reset by a call to psa_aead_abort(). The
2369 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002370 * has been initialized.
2371 *
2372 * After a successful call to psa_aead_decrypt_setup(), the application must
2373 * eventually terminate the operation. The following events terminate an
2374 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002375 * - A successful call to psa_aead_verify().
2376 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002377 *
2378 * \param[in,out] operation The operation object to set up. It must have
2379 * been initialized as per the documentation for
2380 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002381 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002382 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002383 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002384 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002385 * \param alg The AEAD algorithm to compute
2386 * (\c PSA_ALG_XXX value such that
2387 * #PSA_ALG_IS_AEAD(\p alg) is true).
2388 *
2389 * \retval #PSA_SUCCESS
2390 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002391 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002392 * The operation state is not valid (it must be inactive).
Ronald Cron96783552020-10-19 12:06:30 +02002393 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002394 * \retval #PSA_ERROR_NOT_PERMITTED
2395 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002396 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002397 * \retval #PSA_ERROR_NOT_SUPPORTED
2398 * \p alg is not supported or is not an AEAD algorithm.
2399 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2400 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2401 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002402 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002403 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002404 * \retval #PSA_ERROR_BAD_STATE
2405 * The library has not been previously initialized by psa_crypto_init().
2406 * It is implementation-dependent whether a failure to initialize
2407 * results in this error code.
2408 */
2409psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002410 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002411 psa_algorithm_t alg);
2412
2413/** Generate a random nonce for an authenticated encryption operation.
2414 *
2415 * This function generates a random nonce for the authenticated encryption
2416 * operation with an appropriate size for the chosen algorithm, key type
2417 * and key size.
2418 *
2419 * The application must call psa_aead_encrypt_setup() before
2420 * calling this function.
2421 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002422 * If this function returns an error status, the operation enters an error
2423 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002424 *
2425 * \param[in,out] operation Active AEAD operation.
2426 * \param[out] nonce Buffer where the generated nonce is to be
2427 * written.
2428 * \param nonce_size Size of the \p nonce buffer in bytes.
2429 * \param[out] nonce_length On success, the number of bytes of the
2430 * generated nonce.
2431 *
2432 * \retval #PSA_SUCCESS
2433 * Success.
2434 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002435 * The operation state is not valid (it must be an active aead encrypt
Ronald Cron96783552020-10-19 12:06:30 +02002436 * operation, with no nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002437 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2438 * The size of the \p nonce buffer is too small.
2439 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2440 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2441 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002442 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002443 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002444 * \retval #PSA_ERROR_BAD_STATE
2445 * The library has not been previously initialized by psa_crypto_init().
2446 * It is implementation-dependent whether a failure to initialize
2447 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002448 */
2449psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002450 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002451 size_t nonce_size,
2452 size_t *nonce_length);
2453
2454/** Set the nonce for an authenticated encryption or decryption operation.
2455 *
2456 * This function sets the nonce for the authenticated
2457 * encryption or decryption operation.
2458 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002459 * The application must call psa_aead_encrypt_setup() or
2460 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002461 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002462 * If this function returns an error status, the operation enters an error
2463 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002464 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002465 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002466 * instead of this function, unless implementing a protocol that requires
2467 * a non-random IV.
2468 *
2469 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002470 * \param[in] nonce Buffer containing the nonce to use.
2471 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002472 *
2473 * \retval #PSA_SUCCESS
2474 * Success.
2475 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002476 * The operation state is not valid (it must be active, with no nonce
2477 * set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002478 * \retval #PSA_ERROR_INVALID_ARGUMENT
2479 * The size of \p nonce is not acceptable for the chosen algorithm.
2480 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2481 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2482 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002483 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002484 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002485 * \retval #PSA_ERROR_BAD_STATE
2486 * The library has not been previously initialized by psa_crypto_init().
2487 * It is implementation-dependent whether a failure to initialize
2488 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002489 */
2490psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002491 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002492 size_t nonce_length);
2493
Gilles Peskinebc59c852019-01-17 15:26:08 +01002494/** Declare the lengths of the message and additional data for AEAD.
2495 *
2496 * The application must call this function before calling
2497 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2498 * the operation requires it. If the algorithm does not require it,
2499 * calling this function is optional, but if this function is called
2500 * then the implementation must enforce the lengths.
2501 *
2502 * You may call this function before or after setting the nonce with
2503 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2504 *
2505 * - For #PSA_ALG_CCM, calling this function is required.
2506 * - For the other AEAD algorithms defined in this specification, calling
2507 * this function is not required.
2508 * - For vendor-defined algorithm, refer to the vendor documentation.
2509 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002510 * If this function returns an error status, the operation enters an error
2511 * state and must be aborted by calling psa_aead_abort().
2512 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002513 * \param[in,out] operation Active AEAD operation.
2514 * \param ad_length Size of the non-encrypted additional
2515 * authenticated data in bytes.
2516 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2517 *
2518 * \retval #PSA_SUCCESS
2519 * Success.
2520 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002521 * The operation state is not valid (it must be active, and
2522 * psa_aead_update_ad() and psa_aead_update() must not have been
2523 * called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002524 * \retval #PSA_ERROR_INVALID_ARGUMENT
2525 * At least one of the lengths is not acceptable for the chosen
2526 * algorithm.
2527 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2528 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2529 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002530 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002531 * \retval #PSA_ERROR_BAD_STATE
2532 * The library has not been previously initialized by psa_crypto_init().
2533 * It is implementation-dependent whether a failure to initialize
2534 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002535 */
2536psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2537 size_t ad_length,
2538 size_t plaintext_length);
2539
Gilles Peskine30a9e412019-01-14 18:36:12 +01002540/** Pass additional data to an active AEAD operation.
2541 *
2542 * Additional data is authenticated, but not encrypted.
2543 *
2544 * You may call this function multiple times to pass successive fragments
2545 * of the additional data. You may not call this function after passing
2546 * data to encrypt or decrypt with psa_aead_update().
2547 *
2548 * Before calling this function, you must:
2549 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2550 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2551 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002552 * If this function returns an error status, the operation enters an error
2553 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002554 *
2555 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2556 * there is no guarantee that the input is valid. Therefore, until
2557 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2558 * treat the input as untrusted and prepare to undo any action that
2559 * depends on the input if psa_aead_verify() returns an error status.
2560 *
2561 * \param[in,out] operation Active AEAD operation.
2562 * \param[in] input Buffer containing the fragment of
2563 * additional data.
2564 * \param input_length Size of the \p input buffer in bytes.
2565 *
2566 * \retval #PSA_SUCCESS
2567 * Success.
2568 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002569 * The operation state is not valid (it must be active, have a nonce
2570 * set, have lengths set if required by the algorithm, and
2571 * psa_aead_update() must not have been called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002572 * \retval #PSA_ERROR_INVALID_ARGUMENT
2573 * The total input length overflows the additional data length that
2574 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002575 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2576 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2577 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002578 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002579 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002580 * \retval #PSA_ERROR_BAD_STATE
2581 * The library has not been previously initialized by psa_crypto_init().
2582 * It is implementation-dependent whether a failure to initialize
2583 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002584 */
2585psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2586 const uint8_t *input,
2587 size_t input_length);
2588
2589/** Encrypt or decrypt a message fragment in an active AEAD operation.
2590 *
2591 * Before calling this function, you must:
2592 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2593 * The choice of setup function determines whether this function
2594 * encrypts or decrypts its input.
2595 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2596 * 3. Call psa_aead_update_ad() to pass all the additional data.
2597 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002598 * If this function returns an error status, the operation enters an error
2599 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002600 *
2601 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2602 * there is no guarantee that the input is valid. Therefore, until
2603 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2604 * - Do not use the output in any way other than storing it in a
2605 * confidential location. If you take any action that depends
2606 * on the tentative decrypted data, this action will need to be
2607 * undone if the input turns out not to be valid. Furthermore,
2608 * if an adversary can observe that this action took place
2609 * (for example through timing), they may be able to use this
2610 * fact as an oracle to decrypt any message encrypted with the
2611 * same key.
2612 * - In particular, do not copy the output anywhere but to a
2613 * memory or storage space that you have exclusive access to.
2614 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002615 * This function does not require the input to be aligned to any
2616 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002617 * a whole block at a time, it must consume all the input provided, but
2618 * it may delay the end of the corresponding output until a subsequent
2619 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2620 * provides sufficient input. The amount of data that can be delayed
2621 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002622 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002623 * \param[in,out] operation Active AEAD operation.
2624 * \param[in] input Buffer containing the message fragment to
2625 * encrypt or decrypt.
2626 * \param input_length Size of the \p input buffer in bytes.
2627 * \param[out] output Buffer where the output is to be written.
2628 * \param output_size Size of the \p output buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002629 * This must be appropriate for the selected
2630 * algorithm and key:
2631 * - A sufficient output size is
2632 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,
2633 * \c alg, \p input_length) where
2634 * \c key_type is the type of key and \c alg is
2635 * the algorithm that were used to set up the
2636 * operation.
2637 * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p
2638 * input_length) evaluates to the maximum
2639 * output size of any supported AEAD
2640 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002641 * \param[out] output_length On success, the number of bytes
2642 * that make up the returned output.
2643 *
2644 * \retval #PSA_SUCCESS
2645 * Success.
2646 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002647 * The operation state is not valid (it must be active, have a nonce
2648 * set, and have lengths set if required by the algorithm).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002649 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2650 * The size of the \p output buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002651 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or
2652 * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to
2653 * determine the required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002654 * \retval #PSA_ERROR_INVALID_ARGUMENT
2655 * The total length of input to psa_aead_update_ad() so far is
2656 * less than the additional data length that was previously
2657 * specified with psa_aead_set_lengths().
2658 * \retval #PSA_ERROR_INVALID_ARGUMENT
2659 * The total input length overflows the plaintext length that
2660 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002661 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2662 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2663 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002664 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002665 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002666 * \retval #PSA_ERROR_BAD_STATE
2667 * The library has not been previously initialized by psa_crypto_init().
2668 * It is implementation-dependent whether a failure to initialize
2669 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002670 */
2671psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2672 const uint8_t *input,
2673 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002674 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002675 size_t output_size,
2676 size_t *output_length);
2677
2678/** Finish encrypting a message in an AEAD operation.
2679 *
2680 * The operation must have been set up with psa_aead_encrypt_setup().
2681 *
2682 * This function finishes the authentication of the additional data
2683 * formed by concatenating the inputs passed to preceding calls to
2684 * psa_aead_update_ad() with the plaintext formed by concatenating the
2685 * inputs passed to preceding calls to psa_aead_update().
2686 *
2687 * This function has two output buffers:
2688 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002689 * preceding calls to psa_aead_update().
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002690 * - \p tag contains the authentication tag.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002691 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002692 * When this function returns successfuly, the operation becomes inactive.
2693 * If this function returns an error status, the operation enters an error
2694 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002695 *
2696 * \param[in,out] operation Active AEAD operation.
2697 * \param[out] ciphertext Buffer where the last part of the ciphertext
2698 * is to be written.
2699 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002700 * This must be appropriate for the selected
2701 * algorithm and key:
2702 * - A sufficient output size is
2703 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type,
2704 * \c alg) where \c key_type is the type of key
2705 * and \c alg is the algorithm that were used to
2706 * set up the operation.
2707 * - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to
2708 * the maximum output size of any supported AEAD
2709 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002710 * \param[out] ciphertext_length On success, the number of bytes of
2711 * returned ciphertext.
2712 * \param[out] tag Buffer where the authentication tag is
2713 * to be written.
2714 * \param tag_size Size of the \p tag buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002715 * This must be appropriate for the selected
2716 * algorithm and key:
2717 * - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c
2718 * key_type, \c key_bits, \c alg) where
2719 * \c key_type and \c key_bits are the type and
2720 * bit-size of the key, and \c alg is the
2721 * algorithm that were used in the call to
2722 * psa_aead_encrypt_setup().
2723 * - #PSA_AEAD_TAG_MAX_SIZE evaluates to the
2724 * maximum tag size of any supported AEAD
2725 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002726 * \param[out] tag_length On success, the number of bytes
2727 * that make up the returned tag.
2728 *
2729 * \retval #PSA_SUCCESS
2730 * Success.
2731 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002732 * The operation state is not valid (it must be an active encryption
2733 * operation with a nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002734 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002735 * The size of the \p ciphertext or \p tag buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002736 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or
2737 * #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the
2738 * required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type,
2739 * \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to
2740 * determine the required \p tag buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002741 * \retval #PSA_ERROR_INVALID_ARGUMENT
2742 * The total length of input to psa_aead_update_ad() so far is
2743 * less than the additional data length that was previously
2744 * specified with psa_aead_set_lengths().
2745 * \retval #PSA_ERROR_INVALID_ARGUMENT
2746 * The total length of input to psa_aead_update() so far is
2747 * less than the plaintext length that was previously
2748 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002749 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2750 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2751 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002752 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002753 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002754 * \retval #PSA_ERROR_BAD_STATE
2755 * The library has not been previously initialized by psa_crypto_init().
2756 * It is implementation-dependent whether a failure to initialize
2757 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002758 */
2759psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002760 uint8_t *ciphertext,
2761 size_t ciphertext_size,
2762 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002763 uint8_t *tag,
2764 size_t tag_size,
2765 size_t *tag_length);
2766
2767/** Finish authenticating and decrypting a message in an AEAD operation.
2768 *
2769 * The operation must have been set up with psa_aead_decrypt_setup().
2770 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002771 * This function finishes the authenticated decryption of the message
2772 * components:
2773 *
2774 * - The additional data consisting of the concatenation of the inputs
2775 * passed to preceding calls to psa_aead_update_ad().
2776 * - The ciphertext consisting of the concatenation of the inputs passed to
2777 * preceding calls to psa_aead_update().
2778 * - The tag passed to this function call.
2779 *
2780 * If the authentication tag is correct, this function outputs any remaining
2781 * plaintext and reports success. If the authentication tag is not correct,
2782 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002783 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002784 * When this function returns successfuly, the operation becomes inactive.
2785 * If this function returns an error status, the operation enters an error
2786 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002787 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002788 * \note Implementations shall make the best effort to ensure that the
2789 * comparison between the actual tag and the expected tag is performed
2790 * in constant time.
2791 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002792 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002793 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002794 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002795 * from previous calls to psa_aead_update()
2796 * that could not be processed until the end
2797 * of the input.
2798 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002799 * This must be appropriate for the selected algorithm and key:
2800 * - A sufficient output size is
2801 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type,
2802 * \c alg) where \c key_type is the type of key
2803 * and \c alg is the algorithm that were used to
2804 * set up the operation.
2805 * - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to
2806 * the maximum output size of any supported AEAD
2807 * algorithm.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002808 * \param[out] plaintext_length On success, the number of bytes of
2809 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002810 * \param[in] tag Buffer containing the authentication tag.
2811 * \param tag_length Size of the \p tag buffer in bytes.
2812 *
2813 * \retval #PSA_SUCCESS
2814 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002815 * \retval #PSA_ERROR_INVALID_SIGNATURE
2816 * The calculations were successful, but the authentication tag is
2817 * not correct.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002818 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002819 * The operation state is not valid (it must be an active decryption
2820 * operation with a nonce set).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002821 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2822 * The size of the \p plaintext buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002823 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or
2824 * #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the
2825 * required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002826 * \retval #PSA_ERROR_INVALID_ARGUMENT
2827 * The total length of input to psa_aead_update_ad() so far is
2828 * less than the additional data length that was previously
2829 * specified with psa_aead_set_lengths().
2830 * \retval #PSA_ERROR_INVALID_ARGUMENT
2831 * The total length of input to psa_aead_update() so far is
2832 * less than the plaintext length that was previously
2833 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002834 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2835 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2836 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002837 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002838 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002839 * \retval #PSA_ERROR_BAD_STATE
2840 * The library has not been previously initialized by psa_crypto_init().
2841 * It is implementation-dependent whether a failure to initialize
2842 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002843 */
2844psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002845 uint8_t *plaintext,
2846 size_t plaintext_size,
2847 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002848 const uint8_t *tag,
2849 size_t tag_length);
2850
2851/** Abort an AEAD operation.
2852 *
2853 * Aborting an operation frees all associated resources except for the
2854 * \p operation structure itself. Once aborted, the operation object
2855 * can be reused for another operation by calling
2856 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2857 *
2858 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002859 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002860 *
2861 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002862 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2863 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002864 *
2865 * \param[in,out] operation Initialized AEAD operation.
2866 *
2867 * \retval #PSA_SUCCESS
Gilles Peskine30a9e412019-01-14 18:36:12 +01002868 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2869 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002870 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002871 * \retval #PSA_ERROR_BAD_STATE
2872 * The library has not been previously initialized by psa_crypto_init().
2873 * It is implementation-dependent whether a failure to initialize
2874 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002875 */
2876psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2877
Gilles Peskine3b555712018-03-03 21:27:57 +01002878/**@}*/
2879
Gilles Peskine20035e32018-02-03 22:44:14 +01002880/** \defgroup asymmetric Asymmetric cryptography
2881 * @{
2882 */
2883
2884/**
2885 * \brief Sign a hash or short message with a private key.
2886 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002887 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002888 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01002889 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
2890 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02002891 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2892 * to determine the hash algorithm to use.
2893 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002894 * \param key Identifier of the key to use for the operation.
2895 * It must be an asymmetric key pair. The key must
Ronald Cron96783552020-10-19 12:06:30 +02002896 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002897 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002898 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002899 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002900 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002901 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002902 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002903 * \param[out] signature_length On success, the number of bytes
2904 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002905 *
Gilles Peskine28538492018-07-11 17:34:00 +02002906 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002907 * \retval #PSA_ERROR_INVALID_HANDLE
2908 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002909 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002910 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002911 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002912 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002913 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002914 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002915 * \retval #PSA_ERROR_NOT_SUPPORTED
2916 * \retval #PSA_ERROR_INVALID_ARGUMENT
2917 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2918 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2919 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002920 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002921 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002922 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002923 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002924 * The library has not been previously initialized by psa_crypto_init().
2925 * It is implementation-dependent whether a failure to initialize
2926 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002927 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002928psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002929 psa_algorithm_t alg,
2930 const uint8_t *hash,
2931 size_t hash_length,
2932 uint8_t *signature,
2933 size_t signature_size,
2934 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002935
2936/**
2937 * \brief Verify the signature a hash or short message using a public key.
2938 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002939 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002940 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01002941 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
2942 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02002943 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2944 * to determine the hash algorithm to use.
2945 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002946 * \param key Identifier of the key to use for the operation. It
2947 * must be a public key or an asymmetric key pair. The
Ronald Cron96783552020-10-19 12:06:30 +02002948 * key must allow the usage
2949 * #PSA_KEY_USAGE_VERIFY_HASH.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002950 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002951 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002952 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002953 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002954 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002955 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002956 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002957 *
Gilles Peskine28538492018-07-11 17:34:00 +02002958 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002959 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002960 * \retval #PSA_ERROR_INVALID_HANDLE
2961 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002962 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002963 * The calculation was perfomed successfully, but the passed
2964 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002965 * \retval #PSA_ERROR_NOT_SUPPORTED
2966 * \retval #PSA_ERROR_INVALID_ARGUMENT
2967 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2968 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2969 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002970 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002971 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002972 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002973 * The library has not been previously initialized by psa_crypto_init().
2974 * It is implementation-dependent whether a failure to initialize
2975 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002976 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002977psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002978 psa_algorithm_t alg,
2979 const uint8_t *hash,
2980 size_t hash_length,
2981 const uint8_t *signature,
2982 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002983
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002984/**
2985 * \brief Encrypt a short message with a public key.
2986 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002987 * \param key Identifer of the key to use for the operation.
2988 * It must be a public key or an asymmetric key
2989 * pair. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002990 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002991 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02002992 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002993 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002994 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002995 * \param[in] salt A salt or label, if supported by the
2996 * encryption algorithm.
2997 * If the algorithm does not support a
2998 * salt, pass \c NULL.
2999 * If the algorithm supports an optional
3000 * salt and you do not want to pass a salt,
3001 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003002 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003003 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3004 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003005 * \param salt_length Size of the \p salt buffer in bytes.
3006 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003007 * \param[out] output Buffer where the encrypted message is to
3008 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003009 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003010 * \param[out] output_length On success, the number of bytes
3011 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003012 *
Gilles Peskine28538492018-07-11 17:34:00 +02003013 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01003014 * \retval #PSA_ERROR_INVALID_HANDLE
3015 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003016 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003017 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003018 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02003019 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003020 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003021 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003022 * \retval #PSA_ERROR_NOT_SUPPORTED
3023 * \retval #PSA_ERROR_INVALID_ARGUMENT
3024 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3025 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3026 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003027 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01003028 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003029 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03003030 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003031 * The library has not been previously initialized by psa_crypto_init().
3032 * It is implementation-dependent whether a failure to initialize
3033 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003034 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003035psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003036 psa_algorithm_t alg,
3037 const uint8_t *input,
3038 size_t input_length,
3039 const uint8_t *salt,
3040 size_t salt_length,
3041 uint8_t *output,
3042 size_t output_size,
3043 size_t *output_length);
3044
3045/**
3046 * \brief Decrypt a short message with a private key.
3047 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003048 * \param key Identifier of the key to use for the operation.
3049 * It must be an asymmetric key pair. It must
Ronald Cron96783552020-10-19 12:06:30 +02003050 * allow the usage #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003051 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003052 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003053 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003054 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003055 * \param[in] salt A salt or label, if supported by the
3056 * encryption algorithm.
3057 * If the algorithm does not support a
3058 * salt, pass \c NULL.
3059 * If the algorithm supports an optional
3060 * salt and you do not want to pass a salt,
3061 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003062 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003063 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3064 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003065 * \param salt_length Size of the \p salt buffer in bytes.
3066 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003067 * \param[out] output Buffer where the decrypted message is to
3068 * be written.
3069 * \param output_size Size of the \c output buffer in bytes.
3070 * \param[out] output_length On success, the number of bytes
3071 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003072 *
Gilles Peskine28538492018-07-11 17:34:00 +02003073 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003074 * \retval #PSA_ERROR_INVALID_HANDLE
3075 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003076 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003077 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003078 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003079 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003080 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003081 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003082 * \retval #PSA_ERROR_NOT_SUPPORTED
3083 * \retval #PSA_ERROR_INVALID_ARGUMENT
3084 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3085 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3086 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003087 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003088 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003089 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3090 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003091 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003092 * The library has not been previously initialized by psa_crypto_init().
3093 * It is implementation-dependent whether a failure to initialize
3094 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003095 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003096psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003097 psa_algorithm_t alg,
3098 const uint8_t *input,
3099 size_t input_length,
3100 const uint8_t *salt,
3101 size_t salt_length,
3102 uint8_t *output,
3103 size_t output_size,
3104 size_t *output_length);
3105
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003106/**@}*/
3107
Gilles Peskine35675b62019-05-16 17:26:11 +02003108/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003109 * @{
3110 */
3111
Gilles Peskine35675b62019-05-16 17:26:11 +02003112/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003113 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003114 * Before calling any function on a key derivation operation object, the
3115 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003116 * - Set the structure to all-bits-zero, for example:
3117 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003118 * psa_key_derivation_operation_t operation;
3119 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003120 * \endcode
3121 * - Initialize the structure to logical zero values, for example:
3122 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003123 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003124 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003125 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003126 * for example:
3127 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003128 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003129 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003130 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003131 * to the structure, for example:
3132 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003133 * psa_key_derivation_operation_t operation;
3134 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003135 * \endcode
3136 *
3137 * This is an implementation-defined \c struct. Applications should not
3138 * make any assumptions about the content of this structure except
3139 * as directed by the documentation of a specific implementation.
3140 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003141typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003142
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003143/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003144 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003145 * This macro returns a suitable initializer for a key derivation operation
3146 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003147 */
3148#ifdef __DOXYGEN_ONLY__
3149/* This is an example definition for documentation purposes.
3150 * Implementations should define a suitable value in `crypto_struct.h`.
3151 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003152#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003153#endif
3154
Gilles Peskine35675b62019-05-16 17:26:11 +02003155/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003156 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003157static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003158
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003159/** Set up a key derivation operation.
3160 *
3161 * A key derivation algorithm takes some inputs and uses them to generate
3162 * a byte stream in a deterministic way.
3163 * This byte stream can be used to produce keys and other
3164 * cryptographic material.
3165 *
3166 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003167 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3168 * -# Call psa_key_derivation_setup() to select the algorithm.
3169 * -# Provide the inputs for the key derivation by calling
3170 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3171 * as appropriate. Which inputs are needed, in what order, and whether
3172 * they may be keys and if so of what type depends on the algorithm.
3173 * -# Optionally set the operation's maximum capacity with
3174 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3175 * of or after providing inputs. For some algorithms, this step is mandatory
3176 * because the output depends on the maximum capacity.
3177 * -# To derive a key, call psa_key_derivation_output_key().
3178 * To derive a byte string for a different purpose, call
3179 * psa_key_derivation_output_bytes().
3180 * Successive calls to these functions use successive output bytes
3181 * calculated by the key derivation algorithm.
3182 * -# Clean up the key derivation operation object with
3183 * psa_key_derivation_abort().
3184 *
3185 * If this function returns an error, the key derivation operation object is
3186 * not changed.
3187 *
3188 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3189 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003190 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003191 * Implementations must reject an attempt to derive a key of size 0.
3192 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003193 * \param[in,out] operation The key derivation operation object
3194 * to set up. It must
3195 * have been initialized but not set up yet.
3196 * \param alg The key derivation algorithm to compute
3197 * (\c PSA_ALG_XXX value such that
3198 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3199 *
3200 * \retval #PSA_SUCCESS
3201 * Success.
3202 * \retval #PSA_ERROR_INVALID_ARGUMENT
3203 * \c alg is not a key derivation algorithm.
3204 * \retval #PSA_ERROR_NOT_SUPPORTED
3205 * \c alg is not supported or is not a key derivation algorithm.
3206 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3207 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3208 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003209 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003210 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003211 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003212 * The operation state is not valid (it must be inactive).
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003213 * \retval #PSA_ERROR_BAD_STATE
3214 * The library has not been previously initialized by psa_crypto_init().
3215 * It is implementation-dependent whether a failure to initialize
3216 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003217 */
3218psa_status_t psa_key_derivation_setup(
3219 psa_key_derivation_operation_t *operation,
3220 psa_algorithm_t alg);
3221
Gilles Peskine35675b62019-05-16 17:26:11 +02003222/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003223 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003224 * The capacity of a key derivation is the maximum number of bytes that it can
3225 * return. When you get *N* bytes of output from a key derivation operation,
3226 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003227 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003228 * \param[in] operation The operation to query.
3229 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003230 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003231 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003232 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003233 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003234 * The operation state is not valid (it must be active).
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003235 * \retval #PSA_ERROR_HARDWARE_FAILURE
3236 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003237 * \retval #PSA_ERROR_BAD_STATE
3238 * The library has not been previously initialized by psa_crypto_init().
3239 * It is implementation-dependent whether a failure to initialize
3240 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003241 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003242psa_status_t psa_key_derivation_get_capacity(
3243 const psa_key_derivation_operation_t *operation,
3244 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003245
Gilles Peskine35675b62019-05-16 17:26:11 +02003246/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003247 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003248 * The capacity of a key derivation operation is the maximum number of bytes
3249 * that the key derivation operation can return from this point onwards.
3250 *
3251 * \param[in,out] operation The key derivation operation object to modify.
3252 * \param capacity The new capacity of the operation.
3253 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003254 * current capacity.
3255 *
3256 * \retval #PSA_SUCCESS
3257 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003258 * \p capacity is larger than the operation's current capacity.
3259 * In this case, the operation object remains valid and its capacity
3260 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003261 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003262 * The operation state is not valid (it must be active).
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003263 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003264 * \retval #PSA_ERROR_HARDWARE_FAILURE
3265 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003266 * \retval #PSA_ERROR_BAD_STATE
3267 * The library has not been previously initialized by psa_crypto_init().
3268 * It is implementation-dependent whether a failure to initialize
3269 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003270 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003271psa_status_t psa_key_derivation_set_capacity(
3272 psa_key_derivation_operation_t *operation,
3273 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003274
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003275/** Use the maximum possible capacity for a key derivation operation.
3276 *
3277 * Use this value as the capacity argument when setting up a key derivation
3278 * to indicate that the operation should have the maximum possible capacity.
3279 * The value of the maximum possible capacity depends on the key derivation
3280 * algorithm.
3281 */
3282#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3283
3284/** Provide an input for key derivation or key agreement.
3285 *
3286 * Which inputs are required and in what order depends on the algorithm.
3287 * Refer to the documentation of each key derivation or key agreement
3288 * algorithm for information.
3289 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003290 * This function passes direct inputs, which is usually correct for
3291 * non-secret inputs. To pass a secret input, which should be in a key
3292 * object, call psa_key_derivation_input_key() instead of this function.
3293 * Refer to the documentation of individual step types
3294 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3295 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003296 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003297 * If this function returns an error status, the operation enters an error
3298 * state and must be aborted by calling psa_key_derivation_abort().
3299 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003300 * \param[in,out] operation The key derivation operation object to use.
3301 * It must have been set up with
3302 * psa_key_derivation_setup() and must not
3303 * have produced any output yet.
3304 * \param step Which step the input data is for.
3305 * \param[in] data Input data to use.
3306 * \param data_length Size of the \p data buffer in bytes.
3307 *
3308 * \retval #PSA_SUCCESS
3309 * Success.
3310 * \retval #PSA_ERROR_INVALID_ARGUMENT
3311 * \c step is not compatible with the operation's algorithm.
3312 * \retval #PSA_ERROR_INVALID_ARGUMENT
3313 * \c step does not allow direct inputs.
3314 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3315 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3316 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003317 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003318 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003319 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003320 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003321 * \retval #PSA_ERROR_BAD_STATE
3322 * The library has not been previously initialized by psa_crypto_init().
3323 * It is implementation-dependent whether a failure to initialize
3324 * results in this error code.
3325 */
3326psa_status_t psa_key_derivation_input_bytes(
3327 psa_key_derivation_operation_t *operation,
3328 psa_key_derivation_step_t step,
3329 const uint8_t *data,
3330 size_t data_length);
3331
3332/** Provide an input for key derivation in the form of a key.
3333 *
3334 * Which inputs are required and in what order depends on the algorithm.
3335 * Refer to the documentation of each key derivation or key agreement
3336 * algorithm for information.
3337 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003338 * This function obtains input from a key object, which is usually correct for
3339 * secret inputs or for non-secret personalization strings kept in the key
3340 * store. To pass a non-secret parameter which is not in the key store,
3341 * call psa_key_derivation_input_bytes() instead of this function.
3342 * Refer to the documentation of individual step types
3343 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3344 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003345 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003346 * If this function returns an error status, the operation enters an error
3347 * state and must be aborted by calling psa_key_derivation_abort().
3348 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003349 * \param[in,out] operation The key derivation operation object to use.
3350 * It must have been set up with
3351 * psa_key_derivation_setup() and must not
3352 * have produced any output yet.
3353 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003354 * \param key Identifier of the key. It must have an
3355 * appropriate type for step and must allow the
Ronald Cron96783552020-10-19 12:06:30 +02003356 * usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003357 *
3358 * \retval #PSA_SUCCESS
3359 * Success.
3360 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003361 * \retval #PSA_ERROR_NOT_PERMITTED
3362 * \retval #PSA_ERROR_INVALID_ARGUMENT
3363 * \c step is not compatible with the operation's algorithm.
3364 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine224b0d62019-09-23 18:13:17 +02003365 * \c step does not allow key inputs of the given type
3366 * or does not allow key inputs at all.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003367 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3368 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3369 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003370 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003371 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003372 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003373 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003374 * \retval #PSA_ERROR_BAD_STATE
3375 * The library has not been previously initialized by psa_crypto_init().
3376 * It is implementation-dependent whether a failure to initialize
3377 * results in this error code.
3378 */
3379psa_status_t psa_key_derivation_input_key(
3380 psa_key_derivation_operation_t *operation,
3381 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003382 mbedtls_svc_key_id_t key);
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003383
3384/** Perform a key agreement and use the shared secret as input to a key
3385 * derivation.
3386 *
3387 * A key agreement algorithm takes two inputs: a private key \p private_key
3388 * a public key \p peer_key.
3389 * The result of this function is passed as input to a key derivation.
3390 * The output of this key derivation can be extracted by reading from the
3391 * resulting operation to produce keys and other cryptographic material.
3392 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003393 * If this function returns an error status, the operation enters an error
3394 * state and must be aborted by calling psa_key_derivation_abort().
3395 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003396 * \param[in,out] operation The key derivation operation object to use.
3397 * It must have been set up with
3398 * psa_key_derivation_setup() with a
3399 * key agreement and derivation algorithm
3400 * \c alg (\c PSA_ALG_XXX value such that
3401 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3402 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3403 * is false).
3404 * The operation must be ready for an
3405 * input of the type given by \p step.
3406 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003407 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003408 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003409 * \param[in] peer_key Public key of the peer. The peer key must be in the
3410 * same format that psa_import_key() accepts for the
3411 * public key type corresponding to the type of
3412 * private_key. That is, this function performs the
3413 * equivalent of
3414 * #psa_import_key(...,
3415 * `peer_key`, `peer_key_length`) where
3416 * with key attributes indicating the public key
3417 * type corresponding to the type of `private_key`.
3418 * For example, for EC keys, this means that peer_key
3419 * is interpreted as a point on the curve that the
3420 * private key is on. The standard formats for public
3421 * keys are documented in the documentation of
3422 * psa_export_public_key().
3423 * \param peer_key_length Size of \p peer_key in bytes.
3424 *
3425 * \retval #PSA_SUCCESS
3426 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01003427 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003428 * The operation state is not valid for this key agreement \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003429 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003430 * \retval #PSA_ERROR_NOT_PERMITTED
3431 * \retval #PSA_ERROR_INVALID_ARGUMENT
3432 * \c private_key is not compatible with \c alg,
3433 * or \p peer_key is not valid for \c alg or not compatible with
3434 * \c private_key.
3435 * \retval #PSA_ERROR_NOT_SUPPORTED
3436 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskine224b0d62019-09-23 18:13:17 +02003437 * \retval #PSA_ERROR_INVALID_ARGUMENT
3438 * \c step does not allow an input resulting from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003439 * \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
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003444 * \retval #PSA_ERROR_BAD_STATE
3445 * The library has not been previously initialized by psa_crypto_init().
3446 * It is implementation-dependent whether a failure to initialize
3447 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003448 */
3449psa_status_t psa_key_derivation_key_agreement(
3450 psa_key_derivation_operation_t *operation,
3451 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003452 mbedtls_svc_key_id_t private_key,
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003453 const uint8_t *peer_key,
3454 size_t peer_key_length);
3455
Gilles Peskine35675b62019-05-16 17:26:11 +02003456/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003457 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003458 * This function calculates output bytes from a key derivation algorithm and
3459 * return those bytes.
3460 * If you view the key derivation's output as a stream of bytes, this
3461 * function destructively reads the requested number of bytes from the
3462 * stream.
3463 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003464 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003465 * If this function returns an error status other than
3466 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003467 * state and must be aborted by calling psa_key_derivation_abort().
3468 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003469 * \param[in,out] operation The key derivation operation object to read from.
3470 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003471 * \param output_length Number of bytes to output.
3472 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003473 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003474 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003475 * The operation's capacity was less than
3476 * \p output_length bytes. Note that in this case,
3477 * no output is written to the output buffer.
3478 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003479 * subsequent calls to this function will not
3480 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003481 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003482 * The operation state is not valid (it must be active and completed
3483 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003484 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3485 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3486 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003487 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003488 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003489 * \retval #PSA_ERROR_BAD_STATE
3490 * The library has not been previously initialized by psa_crypto_init().
3491 * It is implementation-dependent whether a failure to initialize
3492 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003493 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003494psa_status_t psa_key_derivation_output_bytes(
3495 psa_key_derivation_operation_t *operation,
3496 uint8_t *output,
3497 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003498
Gilles Peskine35675b62019-05-16 17:26:11 +02003499/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003500 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003501 * This function calculates output bytes from a key derivation algorithm
3502 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003503 * The key's location, usage policy, type and size are taken from
3504 * \p attributes.
3505 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003506 * If you view the key derivation's output as a stream of bytes, this
3507 * function destructively reads as many bytes as required from the
3508 * stream.
3509 * The operation's capacity decreases by the number of bytes read.
3510 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003511 * If this function returns an error status other than
3512 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003513 * state and must be aborted by calling psa_key_derivation_abort().
3514 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003515 * How much output is produced and consumed from the operation, and how
Gilles Peskine364d12c2021-03-08 17:23:47 +01003516 * the key is derived, depends on the key type and on the key size
3517 * (denoted \c bits below):
Gilles Peskineeab56e42018-07-12 17:12:33 +02003518 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003519 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003520 * of a given size, this function is functionally equivalent to
3521 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003522 * and passing the resulting output to #psa_import_key.
3523 * However, this function has a security benefit:
3524 * if the implementation provides an isolation boundary then
3525 * the key material is not exposed outside the isolation boundary.
3526 * As a consequence, for these key types, this function always consumes
Gilles Peskine364d12c2021-03-08 17:23:47 +01003527 * exactly (\c bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003528 * The following key types defined in this specification follow this scheme:
3529 *
3530 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003531 * - #PSA_KEY_TYPE_ARC4;
3532 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003533 * - #PSA_KEY_TYPE_DERIVE;
3534 * - #PSA_KEY_TYPE_HMAC.
3535 *
3536 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003537 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003538 * Montgomery curve), this function always draws a byte string whose
3539 * length is determined by the curve, and sets the mandatory bits
3540 * accordingly. That is:
3541 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01003542 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003543 * string and process it as specified in RFC 7748 &sect;5.
Paul Elliott8ff510a2020-06-02 17:19:28 +01003544 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003545 * string and process it as specified in RFC 7748 &sect;5.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003546 *
3547 * - For key types for which the key is represented by a single sequence of
Gilles Peskine364d12c2021-03-08 17:23:47 +01003548 * \c bits bits with constraints as to which bit sequences are acceptable,
3549 * this function draws a byte string of length (\c bits / 8) bytes rounded
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003550 * up to the nearest whole number of bytes. If the resulting byte string
3551 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3552 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003553 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003554 * for the output produced by psa_export_key().
3555 * The following key types defined in this specification follow this scheme:
3556 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003557 * - #PSA_KEY_TYPE_DES.
3558 * Force-set the parity bits, but discard forbidden weak keys.
3559 * For 2-key and 3-key triple-DES, the three keys are generated
3560 * successively (for example, for 3-key triple-DES,
3561 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3562 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003563 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003564 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003565 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003566 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003567 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003568 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003569 * Weierstrass curve).
3570 * For these key types, interpret the byte string as integer
3571 * in big-endian order. Discard it if it is not in the range
3572 * [0, *N* - 2] where *N* is the boundary of the private key domain
3573 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003574 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003575 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003576 * This method allows compliance to NIST standards, specifically
3577 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003578 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3579 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3580 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3581 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003582 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003583 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003584 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003585 * implementation-defined.
3586 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003587 * In all cases, the data that is read is discarded from the operation.
3588 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003589 *
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003590 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3591 * the input to that step must be provided with psa_key_derivation_input_key().
3592 * Future versions of this specification may include additional restrictions
3593 * on the derived key based on the attributes and strength of the secret key.
3594 *
Gilles Peskine20628592019-04-19 19:29:50 +02003595 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003596 * \param[in,out] operation The key derivation operation object to read from.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003597 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003598 * key. For persistent keys, this is the key
3599 * identifier defined in \p attributes.
3600 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003601 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003602 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003603 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003604 * If the key is persistent, the key material and the key's metadata
3605 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003606 * \retval #PSA_ERROR_ALREADY_EXISTS
3607 * This is an attempt to create a persistent key, and there is
3608 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003609 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003610 * There was not enough data to create the desired key.
3611 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003612 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003613 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003614 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003615 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003616 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003617 * \retval #PSA_ERROR_INVALID_ARGUMENT
3618 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003619 * \retval #PSA_ERROR_NOT_PERMITTED
3620 * The #PSA_KEY_DERIVATION_INPUT_SECRET input was not provided through
3621 * a key.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003622 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003623 * The operation state is not valid (it must be active and completed
3624 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003625 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3626 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3627 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3628 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003629 * \retval #PSA_ERROR_CORRUPTION_DETECTED
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01003630 * \retval #PSA_ERROR_DATA_INVALID
3631 * \retval #PSA_ERROR_DATA_CORRUPT
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003632 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003633 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003634 * The library has not been previously initialized by psa_crypto_init().
3635 * It is implementation-dependent whether a failure to initialize
3636 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003637 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003638psa_status_t psa_key_derivation_output_key(
3639 const psa_key_attributes_t *attributes,
3640 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003641 mbedtls_svc_key_id_t *key);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003642
Gilles Peskine35675b62019-05-16 17:26:11 +02003643/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003644 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003645 * Aborting an operation frees all associated resources except for the \c
3646 * operation structure itself. Once aborted, the operation object can be reused
3647 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003648 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003649 * This function may be called at any time after the operation
3650 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003651 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003652 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3653 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003654 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003655 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003656 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003657 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003658 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3659 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003660 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003661 * \retval #PSA_ERROR_BAD_STATE
3662 * The library has not been previously initialized by psa_crypto_init().
3663 * It is implementation-dependent whether a failure to initialize
3664 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003665 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003666psa_status_t psa_key_derivation_abort(
3667 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003668
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003669/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003670 *
3671 * \warning The raw result of a key agreement algorithm such as finite-field
3672 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3673 * not be used directly as key material. It should instead be passed as
3674 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003675 * a key derivation, use psa_key_derivation_key_agreement() and other
3676 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003677 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003678 * \param alg The key agreement algorithm to compute
3679 * (\c PSA_ALG_XXX value such that
3680 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3681 * is true).
Ronald Croncf56a0a2020-08-04 09:51:30 +02003682 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003683 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003684 * \param[in] peer_key Public key of the peer. It must be
3685 * in the same format that psa_import_key()
3686 * accepts. The standard formats for public
3687 * keys are documented in the documentation
3688 * of psa_export_public_key().
3689 * \param peer_key_length Size of \p peer_key in bytes.
3690 * \param[out] output Buffer where the decrypted message is to
3691 * be written.
3692 * \param output_size Size of the \c output buffer in bytes.
3693 * \param[out] output_length On success, the number of bytes
3694 * that make up the returned output.
3695 *
3696 * \retval #PSA_SUCCESS
3697 * Success.
3698 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003699 * \retval #PSA_ERROR_NOT_PERMITTED
3700 * \retval #PSA_ERROR_INVALID_ARGUMENT
3701 * \p alg is not a key agreement algorithm
3702 * \retval #PSA_ERROR_INVALID_ARGUMENT
3703 * \p private_key is not compatible with \p alg,
3704 * or \p peer_key is not valid for \p alg or not compatible with
3705 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003706 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3707 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003708 * \retval #PSA_ERROR_NOT_SUPPORTED
3709 * \p alg is not a supported key agreement algorithm.
3710 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3711 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3712 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003713 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003714 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003715 * \retval #PSA_ERROR_BAD_STATE
3716 * The library has not been previously initialized by psa_crypto_init().
3717 * It is implementation-dependent whether a failure to initialize
3718 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003719 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003720psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003721 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02003722 const uint8_t *peer_key,
3723 size_t peer_key_length,
3724 uint8_t *output,
3725 size_t output_size,
3726 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003727
Gilles Peskineea0fb492018-07-12 17:17:20 +02003728/**@}*/
3729
Gilles Peskineedd76872018-07-20 17:42:05 +02003730/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003731 * @{
3732 */
3733
3734/**
3735 * \brief Generate random bytes.
3736 *
3737 * \warning This function **can** fail! Callers MUST check the return status
3738 * and MUST NOT use the content of the output buffer if the return
3739 * status is not #PSA_SUCCESS.
3740 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003741 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003742 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003743 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003744 * \param output_size Number of bytes to generate and output.
3745 *
Gilles Peskine28538492018-07-11 17:34:00 +02003746 * \retval #PSA_SUCCESS
3747 * \retval #PSA_ERROR_NOT_SUPPORTED
3748 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003749 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003750 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3751 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003752 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003753 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003754 * The library has not been previously initialized by psa_crypto_init().
3755 * It is implementation-dependent whether a failure to initialize
3756 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003757 */
3758psa_status_t psa_generate_random(uint8_t *output,
3759 size_t output_size);
3760
3761/**
3762 * \brief Generate a key or key pair.
3763 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003764 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02003765 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003766 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003767 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003768 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003769 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003770 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003771 * the public exponent is 65537.
3772 * The modulus is a product of two probabilistic primes
3773 * between 2^{n-1} and 2^n where n is the bit size specified in the
3774 * attributes.
3775 *
Gilles Peskine20628592019-04-19 19:29:50 +02003776 * \param[in] attributes The attributes for the new key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003777 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003778 * key. For persistent keys, this is the key
3779 * identifier defined in \p attributes.
3780 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003781 *
Gilles Peskine28538492018-07-11 17:34:00 +02003782 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003783 * Success.
3784 * If the key is persistent, the key material and the key's metadata
3785 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003786 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003787 * This is an attempt to create a persistent key, and there is
3788 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003789 * \retval #PSA_ERROR_NOT_SUPPORTED
3790 * \retval #PSA_ERROR_INVALID_ARGUMENT
3791 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3792 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3793 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3794 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003795 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003796 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01003797 * \retval #PSA_ERROR_DATA_INVALID
3798 * \retval #PSA_ERROR_DATA_CORRUPT
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003799 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003800 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003801 * The library has not been previously initialized by psa_crypto_init().
3802 * It is implementation-dependent whether a failure to initialize
3803 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003804 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003805psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003806 mbedtls_svc_key_id_t *key);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003807
3808/**@}*/
3809
Gilles Peskinee59236f2018-01-27 23:32:46 +01003810#ifdef __cplusplus
3811}
3812#endif
3813
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003814/* The file "crypto_sizes.h" contains definitions for size calculation
3815 * macros whose definitions are implementation-specific. */
3816#include "crypto_sizes.h"
3817
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003818/* The file "crypto_struct.h" contains definitions for
3819 * implementation-specific structs that are declared above. */
3820#include "crypto_struct.h"
3821
3822/* The file "crypto_extra.h" contains vendor-specific definitions. This
3823 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003824#include "crypto_extra.h"
3825
3826#endif /* PSA_CRYPTO_H */