blob: faa3b9e3fb0e9b13d8641fccdbb56145270628c7 [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.
Antonio de Angelisbf672332021-11-30 12:21:44 +0000162 * \param owner The key owner identifier.
Ronald Cron6b5ff532020-10-16 14:38:19 +0200163 */
164static void mbedtls_set_key_owner_id( psa_key_attributes_t *attributes,
Antonio de Angelisbf672332021-11-30 12:21:44 +0000165 mbedtls_key_owner_id_t owner );
Ronald Cron6b5ff532020-10-16 14:38:19 +0200166#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
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -0500502 * The lifetime or identifier in \p attributes are invalid, or
503 * the policy constraints on the source and specified in
504 * \p attributes are incompatible, or
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100505 * \p attributes specifies a key type or key size
506 * which does not match the attributes of the source key.
507 * \retval #PSA_ERROR_NOT_PERMITTED
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -0500508 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag, or
509 * the source key is not exportable and its lifetime does not
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100510 * allow copying it to the target's lifetime.
511 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
512 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
513 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
514 * \retval #PSA_ERROR_HARDWARE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100515 * \retval #PSA_ERROR_DATA_INVALID
516 * \retval #PSA_ERROR_DATA_CORRUPT
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100517 * \retval #PSA_ERROR_STORAGE_FAILURE
518 * \retval #PSA_ERROR_CORRUPTION_DETECTED
519 * \retval #PSA_ERROR_BAD_STATE
520 * The library has not been previously initialized by psa_crypto_init().
521 * It is implementation-dependent whether a failure to initialize
522 * results in this error code.
523 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200524psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100525 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200526 mbedtls_svc_key_id_t *target_key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100527
528
529/**
530 * \brief Destroy a key.
531 *
532 * This function destroys a key from both volatile
533 * memory and, if applicable, non-volatile storage. Implementations shall
534 * make a best effort to ensure that that the key material cannot be recovered.
535 *
536 * This function also erases any metadata such as policies and frees
Ronald Croncf56a0a2020-08-04 09:51:30 +0200537 * resources associated with the key.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100538 *
539 * If a key is currently in use in a multipart operation, then destroying the
540 * key will cause the multipart operation to fail.
541 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200542 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
Ronald Cron96783552020-10-19 12:06:30 +0200543 * return #PSA_SUCCESS.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100544 *
545 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +0200546 * \p key was a valid identifier and the key material that it
547 * referred to has been erased. Alternatively, \p key is \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100548 * \retval #PSA_ERROR_NOT_PERMITTED
549 * The key cannot be erased because it is
550 * read-only, either due to a policy or due to physical restrictions.
551 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200552 * \p key is not a valid identifier nor \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100553 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Tom Cosgrove5205c972022-07-28 06:12:08 +0100554 * There was a failure in communication with the cryptoprocessor.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100555 * The key material may still be present in the cryptoprocessor.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100556 * \retval #PSA_ERROR_DATA_INVALID
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100557 * This error is typically a result of either storage corruption on a
558 * cleartext storage backend, or an attempt to read data that was
559 * written by an incompatible version of the library.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100560 * \retval #PSA_ERROR_STORAGE_FAILURE
561 * The storage is corrupted. Implementations shall make a best effort
562 * to erase key material even in this stage, however applications
563 * should be aware that it may be impossible to guarantee that the
564 * key material is not recoverable in such cases.
565 * \retval #PSA_ERROR_CORRUPTION_DETECTED
566 * An unexpected condition which is not a storage corruption or
567 * a communication failure occurred. The cryptoprocessor may have
568 * been compromised.
569 * \retval #PSA_ERROR_BAD_STATE
570 * The library has not been previously initialized by psa_crypto_init().
571 * It is implementation-dependent whether a failure to initialize
572 * results in this error code.
573 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200574psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100575
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100576/**@}*/
577
578/** \defgroup import_export Key import and export
579 * @{
580 */
581
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100582/**
583 * \brief Import a key in binary format.
584 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100585 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100586 * documentation of psa_export_public_key() for the format of public keys
587 * and to the documentation of psa_export_key() for the format for
588 * other key types.
589 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200590 * The key data determines the key size. The attributes may optionally
591 * specify a key size; in this case it must match the size determined
592 * from the key data. A key size of 0 in \p attributes indicates that
593 * the key size is solely determined by the key data.
594 *
595 * Implementations must reject an attempt to import a key of size 0.
596 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100597 * This specification supports a single format for each key type.
598 * Implementations may support other formats as long as the standard
599 * format is supported. Implementations that support other formats
600 * should ensure that the formats are clearly unambiguous so as to
601 * minimize the risk that an invalid input is accidentally interpreted
602 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100603 *
Gilles Peskine20628592019-04-19 19:29:50 +0200604 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200605 * The key size is always determined from the
606 * \p data buffer.
607 * If the key size in \p attributes is nonzero,
608 * it must be equal to the size from \p data.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200609 * \param[out] key On success, an identifier to the newly created key.
Ronald Cron4067d1c2020-10-19 13:34:38 +0200610 * For persistent keys, this is the key identifier
611 * defined in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200612 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100613 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200614 * buffer is interpreted according to the type declared
615 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200616 * All implementations must support at least the format
617 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100618 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200619 * the chosen type. Implementations may allow other
620 * formats, but should be conservative: implementations
621 * should err on the side of rejecting content if it
622 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200623 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100624 *
Gilles Peskine28538492018-07-11 17:34:00 +0200625 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100626 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100627 * If the key is persistent, the key material and the key's metadata
628 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200629 * \retval #PSA_ERROR_ALREADY_EXISTS
630 * This is an attempt to create a persistent key, and there is
631 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200632 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200633 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200634 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200635 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -0500636 * The key attributes, as a whole, are invalid, or
637 * the key data is not correctly formatted, or
638 * the size in \p attributes is nonzero and does not match the size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200639 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200640 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
641 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
642 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100643 * \retval #PSA_ERROR_DATA_CORRUPT
644 * \retval #PSA_ERROR_DATA_INVALID
Darryl Greend49a4992018-06-18 17:27:26 +0100645 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200646 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200647 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300648 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300649 * The library has not been previously initialized by psa_crypto_init().
650 * It is implementation-dependent whether a failure to initialize
651 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100652 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200653psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100654 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200655 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200656 mbedtls_svc_key_id_t *key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100657
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100658
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100659
660/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100661 * \brief Export a key in binary format.
662 *
663 * The output of this function can be passed to psa_import_key() to
664 * create an equivalent object.
665 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100666 * If the implementation of psa_import_key() supports other formats
667 * beyond the format specified here, the output from psa_export_key()
668 * must use the representation specified here, not the original
669 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100670 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100671 * For standard key types, the output format is as follows:
672 *
673 * - For symmetric keys (including MAC keys), the format is the
674 * raw bytes of the key.
675 * - For DES, the key data consists of 8 bytes. The parity bits must be
676 * correct.
677 * - For Triple-DES, the format is the concatenation of the
678 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200679 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200680 * is the non-encrypted DER encoding of the representation defined by
681 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
682 * ```
683 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200684 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200685 * modulus INTEGER, -- n
686 * publicExponent INTEGER, -- e
687 * privateExponent INTEGER, -- d
688 * prime1 INTEGER, -- p
689 * prime2 INTEGER, -- q
690 * exponent1 INTEGER, -- d mod (p-1)
691 * exponent2 INTEGER, -- d mod (q-1)
692 * coefficient INTEGER, -- (inverse of q) mod p
693 * }
694 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200695 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200696 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100697 * a representation of the private value as a `ceiling(m/8)`-byte string
698 * where `m` is the bit size associated with the curve, i.e. the bit size
699 * of the order of the curve's coordinate field. This byte string is
700 * in little-endian order for Montgomery curves (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +0100701 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
702 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
703 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
Steven Cooreman6f5cc712020-06-11 16:40:41 +0200704 * For Weierstrass curves, this is the content of the `privateKey` field of
705 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
706 * the format is defined by RFC 7748, and output is masked according to §5.
Gilles Peskine67546802021-02-24 21:49:40 +0100707 * For twisted Edwards curves, the private key is as defined by RFC 8032
708 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200709 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200710 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000711 * format is the representation of the private key `x` as a big-endian byte
712 * string. The length of the byte string is the private key size in bytes
713 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200714 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
715 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100716 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200717 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
718 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200719 * \param key Identifier of the key to export. It must allow the
Ronald Cron96783552020-10-19 12:06:30 +0200720 * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
Ronald Croncf56a0a2020-08-04 09:51:30 +0200721 * key.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200722 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200723 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200724 * \param[out] data_length On success, the number of bytes
725 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100726 *
Gilles Peskine28538492018-07-11 17:34:00 +0200727 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100728 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200729 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200730 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100731 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200732 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
733 * The size of the \p data buffer is too small. You can determine a
734 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100735 * #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200736 * where \c type is the key type
737 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200738 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
739 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200740 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100741 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100742 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300743 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300744 * The library has not been previously initialized by psa_crypto_init().
745 * It is implementation-dependent whether a failure to initialize
746 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100747 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200748psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100749 uint8_t *data,
750 size_t data_size,
751 size_t *data_length);
752
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100753/**
754 * \brief Export a public key or the public part of a key pair in binary format.
755 *
756 * The output of this function can be passed to psa_import_key() to
757 * create an object that is equivalent to the public key.
758 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000759 * This specification supports a single format for each key type.
760 * Implementations may support other formats as long as the standard
761 * format is supported. Implementations that support other formats
762 * should ensure that the formats are clearly unambiguous so as to
763 * minimize the risk that an invalid input is accidentally interpreted
764 * according to a different format.
765 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000766 * For standard key types, the output format is as follows:
767 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
768 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
769 * ```
770 * RSAPublicKey ::= SEQUENCE {
771 * modulus INTEGER, -- n
772 * publicExponent INTEGER } -- e
773 * ```
Gilles Peskine67546802021-02-24 21:49:40 +0100774 * - For elliptic curve keys on a twisted Edwards curve (key types for which
Bence Szépkúti3b1cba82021-04-08 15:49:07 +0200775 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true and #PSA_KEY_TYPE_ECC_GET_FAMILY
Gilles Peskine67546802021-02-24 21:49:40 +0100776 * returns #PSA_ECC_FAMILY_TWISTED_EDWARDS), the public key is as defined
777 * by RFC 8032
778 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
779 * - For other elliptic curve public keys (key types for which
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000780 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
781 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
782 * Let `m` be the bit size associated with the curve, i.e. the bit size of
783 * `q` for a curve over `F_q`. The representation consists of:
784 * - The byte 0x04;
785 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
786 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200787 * - For Diffie-Hellman key exchange public keys (key types for which
788 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000789 * the format is the representation of the public key `y = g^x mod p` as a
790 * big-endian byte string. The length of the byte string is the length of the
791 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100792 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200793 * Exporting a public key object or the public part of a key pair is
794 * always permitted, regardless of the key's usage flags.
795 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200796 * \param key Identifier of the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200797 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200798 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200799 * \param[out] data_length On success, the number of bytes
800 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100801 *
Gilles Peskine28538492018-07-11 17:34:00 +0200802 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100803 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200804 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200805 * The key is neither a public key nor a key pair.
806 * \retval #PSA_ERROR_NOT_SUPPORTED
807 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
808 * The size of the \p data buffer is too small. You can determine a
809 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100810 * #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200811 * where \c type is the key type
812 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200813 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
814 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200815 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100816 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100817 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300818 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300819 * The library has not been previously initialized by psa_crypto_init().
820 * It is implementation-dependent whether a failure to initialize
821 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100822 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200823psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100824 uint8_t *data,
825 size_t data_size,
826 size_t *data_length);
827
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100828
Gilles Peskine20035e32018-02-03 22:44:14 +0100829
830/**@}*/
831
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100832/** \defgroup hash Message digests
833 * @{
834 */
835
Gilles Peskine69647a42019-01-14 20:18:12 +0100836/** Calculate the hash (digest) of a message.
837 *
838 * \note To verify the hash of a message against an
839 * expected value, use psa_hash_compare() instead.
840 *
841 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
842 * such that #PSA_ALG_IS_HASH(\p alg) is true).
843 * \param[in] input Buffer containing the message to hash.
844 * \param input_length Size of the \p input buffer in bytes.
845 * \param[out] hash Buffer where the hash is to be written.
846 * \param hash_size Size of the \p hash buffer in bytes.
847 * \param[out] hash_length On success, the number of bytes
848 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100849 * #PSA_HASH_LENGTH(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100850 *
851 * \retval #PSA_SUCCESS
852 * Success.
853 * \retval #PSA_ERROR_NOT_SUPPORTED
854 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100855 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100856 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
857 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100858 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
859 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
860 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200861 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100862 * \retval #PSA_ERROR_BAD_STATE
863 * The library has not been previously initialized by psa_crypto_init().
864 * It is implementation-dependent whether a failure to initialize
865 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100866 */
867psa_status_t psa_hash_compute(psa_algorithm_t alg,
868 const uint8_t *input,
869 size_t input_length,
870 uint8_t *hash,
871 size_t hash_size,
872 size_t *hash_length);
873
874/** Calculate the hash (digest) of a message and compare it with a
875 * reference value.
876 *
877 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
878 * such that #PSA_ALG_IS_HASH(\p alg) is true).
879 * \param[in] input Buffer containing the message to hash.
880 * \param input_length Size of the \p input buffer in bytes.
881 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100882 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100883 *
884 * \retval #PSA_SUCCESS
885 * The expected hash is identical to the actual hash of the input.
886 * \retval #PSA_ERROR_INVALID_SIGNATURE
887 * The hash of the message was calculated successfully, but it
888 * differs from the expected hash.
889 * \retval #PSA_ERROR_NOT_SUPPORTED
890 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100891 * \retval #PSA_ERROR_INVALID_ARGUMENT
892 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100893 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
894 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
895 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200896 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100897 * \retval #PSA_ERROR_BAD_STATE
898 * The library has not been previously initialized by psa_crypto_init().
899 * It is implementation-dependent whether a failure to initialize
900 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100901 */
902psa_status_t psa_hash_compare(psa_algorithm_t alg,
903 const uint8_t *input,
904 size_t input_length,
905 const uint8_t *hash,
Gilles Peskinefa710f52019-12-02 14:31:48 +0100906 size_t hash_length);
Gilles Peskine69647a42019-01-14 20:18:12 +0100907
Gilles Peskine308b91d2018-02-08 09:47:44 +0100908/** The type of the state data structure for multipart hash operations.
909 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000910 * Before calling any function on a hash operation object, the application must
911 * initialize it by any of the following means:
912 * - Set the structure to all-bits-zero, for example:
913 * \code
914 * psa_hash_operation_t operation;
915 * memset(&operation, 0, sizeof(operation));
916 * \endcode
917 * - Initialize the structure to logical zero values, for example:
918 * \code
919 * psa_hash_operation_t operation = {0};
920 * \endcode
921 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
922 * for example:
923 * \code
924 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
925 * \endcode
926 * - Assign the result of the function psa_hash_operation_init()
927 * to the structure, for example:
928 * \code
929 * psa_hash_operation_t operation;
930 * operation = psa_hash_operation_init();
931 * \endcode
932 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100933 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100934 * make any assumptions about the content of this structure except
935 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100936typedef struct psa_hash_operation_s psa_hash_operation_t;
937
Jaeden Amero6a25b412019-01-04 11:47:44 +0000938/** \def PSA_HASH_OPERATION_INIT
939 *
940 * This macro returns a suitable initializer for a hash operation object
941 * of type #psa_hash_operation_t.
942 */
943#ifdef __DOXYGEN_ONLY__
944/* This is an example definition for documentation purposes.
945 * Implementations should define a suitable value in `crypto_struct.h`.
946 */
947#define PSA_HASH_OPERATION_INIT {0}
948#endif
949
950/** Return an initial value for a hash operation object.
951 */
952static psa_hash_operation_t psa_hash_operation_init(void);
953
Gilles Peskinef45adda2019-01-14 18:29:18 +0100954/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100955 *
956 * The sequence of operations to calculate a hash (message digest)
957 * is as follows:
958 * -# Allocate an operation object which will be passed to all the functions
959 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000960 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100961 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200962 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100963 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100964 * of the message each time. The hash that is calculated is the hash
965 * of the concatenation of these messages in order.
966 * -# To calculate the hash, call psa_hash_finish().
967 * To compare the hash with an expected value, call psa_hash_verify().
968 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100969 * If an error occurs at any step after a call to psa_hash_setup(), the
970 * operation will need to be reset by a call to psa_hash_abort(). The
971 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000972 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100973 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200974 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100975 * eventually terminate the operation. The following events terminate an
976 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100977 * - A successful call to psa_hash_finish() or psa_hash_verify().
978 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100979 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000980 * \param[in,out] operation The operation object to set up. It must have
981 * been initialized as per the documentation for
982 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200983 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
984 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100985 *
Gilles Peskine28538492018-07-11 17:34:00 +0200986 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100987 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200988 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100989 * \p alg is not a supported hash algorithm.
990 * \retval #PSA_ERROR_INVALID_ARGUMENT
991 * \p alg is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200992 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
993 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
994 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200995 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100996 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -0500997 * The operation state is not valid (it must be inactive), or
998 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100999 * It is implementation-dependent whether a failure to initialize
1000 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001001 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001002psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001003 psa_algorithm_t alg);
1004
Gilles Peskine308b91d2018-02-08 09:47:44 +01001005/** Add a message fragment to a multipart hash operation.
1006 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001007 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001008 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001009 * If this function returns an error status, the operation enters an error
1010 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001011 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001012 * \param[in,out] operation Active hash operation.
1013 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001014 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001015 *
Gilles Peskine28538492018-07-11 17:34:00 +02001016 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001017 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001018 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1019 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1020 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001021 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001022 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001023 * The operation state is not valid (it must be active), or
1024 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001025 * It is implementation-dependent whether a failure to initialize
1026 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001027 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001028psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1029 const uint8_t *input,
1030 size_t input_length);
1031
Gilles Peskine308b91d2018-02-08 09:47:44 +01001032/** Finish the calculation of the hash of a message.
1033 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001034 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001035 * This function calculates the hash of the message formed by concatenating
1036 * the inputs passed to preceding calls to psa_hash_update().
1037 *
Shaun Case0e7791f2021-12-20 21:14:10 -08001038 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001039 * If this function returns an error status, the operation enters an error
1040 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001041 *
1042 * \warning Applications should not call this function if they expect
1043 * a specific value for the hash. Call psa_hash_verify() instead.
1044 * Beware that comparing integrity or authenticity data such as
1045 * hash values with a function such as \c memcmp is risky
1046 * because the time taken by the comparison may leak information
1047 * about the hashed data which could allow an attacker to guess
1048 * a valid hash and thereby bypass security controls.
1049 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001050 * \param[in,out] operation Active hash operation.
1051 * \param[out] hash Buffer where the hash is to be written.
1052 * \param hash_size Size of the \p hash buffer in bytes.
1053 * \param[out] hash_length On success, the number of bytes
1054 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001055 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001056 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001057 *
Gilles Peskine28538492018-07-11 17:34:00 +02001058 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001059 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001060 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001061 * The size of the \p hash buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001062 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001063 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001064 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1065 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1066 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001067 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001068 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001069 * The operation state is not valid (it must be active), or
1070 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001071 * It is implementation-dependent whether a failure to initialize
1072 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001073 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001074psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1075 uint8_t *hash,
1076 size_t hash_size,
1077 size_t *hash_length);
1078
Gilles Peskine308b91d2018-02-08 09:47:44 +01001079/** Finish the calculation of the hash of a message and compare it with
1080 * an expected value.
1081 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001082 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001083 * This function calculates the hash of the message formed by concatenating
1084 * the inputs passed to preceding calls to psa_hash_update(). It then
1085 * compares the calculated hash with the expected hash passed as a
1086 * parameter to this function.
1087 *
Shaun Case0e7791f2021-12-20 21:14:10 -08001088 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001089 * If this function returns an error status, the operation enters an error
1090 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001091 *
Gilles Peskine19067982018-03-20 17:54:53 +01001092 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001093 * comparison between the actual hash and the expected hash is performed
1094 * in constant time.
1095 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001096 * \param[in,out] operation Active hash operation.
1097 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001098 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001099 *
Gilles Peskine28538492018-07-11 17:34:00 +02001100 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001101 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001102 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001103 * The hash of the message was calculated successfully, but it
1104 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001105 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1106 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1107 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001108 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001109 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001110 * The operation state is not valid (it must be active), or
1111 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001112 * It is implementation-dependent whether a failure to initialize
1113 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001114 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001115psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1116 const uint8_t *hash,
1117 size_t hash_length);
1118
Gilles Peskine308b91d2018-02-08 09:47:44 +01001119/** Abort a hash operation.
1120 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001121 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001122 * \p operation structure itself. Once aborted, the operation object
1123 * can be reused for another operation by calling
1124 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001125 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001126 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001127 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001128 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001129 * In particular, calling psa_hash_abort() after the operation has been
1130 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1131 * psa_hash_verify() is safe and has no effect.
1132 *
1133 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001134 *
Gilles Peskine28538492018-07-11 17:34:00 +02001135 * \retval #PSA_SUCCESS
Gilles Peskine28538492018-07-11 17:34:00 +02001136 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1137 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001138 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001139 * \retval #PSA_ERROR_BAD_STATE
1140 * The library has not been previously initialized by psa_crypto_init().
1141 * It is implementation-dependent whether a failure to initialize
1142 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001143 */
1144psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001145
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001146/** Clone a hash operation.
1147 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001148 * This function copies the state of an ongoing hash operation to
1149 * a new operation object. In other words, this function is equivalent
1150 * to calling psa_hash_setup() on \p target_operation with the same
1151 * algorithm that \p source_operation was set up for, then
1152 * psa_hash_update() on \p target_operation with the same input that
1153 * that was passed to \p source_operation. After this function returns, the
1154 * two objects are independent, i.e. subsequent calls involving one of
1155 * the objects do not affect the other object.
1156 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001157 * \param[in] source_operation The active hash operation to clone.
1158 * \param[in,out] target_operation The operation object to set up.
1159 * It must be initialized but not active.
1160 *
1161 * \retval #PSA_SUCCESS
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001162 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1163 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001164 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001165 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001166 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001167 * The \p source_operation state is not valid (it must be active), or
1168 * the \p target_operation state is not valid (it must be inactive), or
1169 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001170 * It is implementation-dependent whether a failure to initialize
1171 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001172 */
1173psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1174 psa_hash_operation_t *target_operation);
1175
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001176/**@}*/
1177
Gilles Peskine8c9def32018-02-08 10:02:12 +01001178/** \defgroup MAC Message authentication codes
1179 * @{
1180 */
1181
Gilles Peskine69647a42019-01-14 20:18:12 +01001182/** Calculate the MAC (message authentication code) of a message.
1183 *
1184 * \note To verify the MAC of a message against an
1185 * expected value, use psa_mac_verify() instead.
1186 * Beware that comparing integrity or authenticity data such as
1187 * MAC values with a function such as \c memcmp is risky
1188 * because the time taken by the comparison may leak information
1189 * about the MAC value which could allow an attacker to guess
1190 * a valid MAC and thereby bypass security controls.
1191 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001192 * \param key Identifier of the key to use for the operation. It
1193 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001194 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001195 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001196 * \param[in] input Buffer containing the input message.
1197 * \param input_length Size of the \p input buffer in bytes.
1198 * \param[out] mac Buffer where the MAC value is to be written.
1199 * \param mac_size Size of the \p mac buffer in bytes.
1200 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001201 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001202 *
1203 * \retval #PSA_SUCCESS
1204 * Success.
1205 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001206 * \retval #PSA_ERROR_NOT_PERMITTED
1207 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001208 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001209 * \retval #PSA_ERROR_NOT_SUPPORTED
1210 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001211 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1212 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001213 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1214 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1215 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001216 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001217 * \retval #PSA_ERROR_STORAGE_FAILURE
1218 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001219 * \retval #PSA_ERROR_BAD_STATE
1220 * The library has not been previously initialized by psa_crypto_init().
1221 * It is implementation-dependent whether a failure to initialize
1222 * results in this error code.
1223 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001224psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001225 psa_algorithm_t alg,
1226 const uint8_t *input,
1227 size_t input_length,
1228 uint8_t *mac,
1229 size_t mac_size,
1230 size_t *mac_length);
1231
1232/** Calculate the MAC of a message and compare it with a reference value.
1233 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001234 * \param key Identifier of the key to use for the operation. It
1235 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001236 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001237 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001238 * \param[in] input Buffer containing the input message.
1239 * \param input_length Size of the \p input buffer in bytes.
1240 * \param[out] mac Buffer containing the expected MAC value.
1241 * \param mac_length Size of the \p mac buffer in bytes.
1242 *
1243 * \retval #PSA_SUCCESS
1244 * The expected MAC is identical to the actual MAC of the input.
1245 * \retval #PSA_ERROR_INVALID_SIGNATURE
1246 * The MAC of the message was calculated successfully, but it
1247 * differs from the expected value.
1248 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001249 * \retval #PSA_ERROR_NOT_PERMITTED
1250 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001251 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001252 * \retval #PSA_ERROR_NOT_SUPPORTED
1253 * \p alg is not supported or is not a MAC algorithm.
1254 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1255 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1256 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001257 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001258 * \retval #PSA_ERROR_STORAGE_FAILURE
1259 * The key could not be retrieved from storage.
1260 * \retval #PSA_ERROR_BAD_STATE
1261 * The library has not been previously initialized by psa_crypto_init().
1262 * It is implementation-dependent whether a failure to initialize
1263 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001264 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001265psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
Gilles Peskinea05602d2019-01-17 15:25:52 +01001266 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001267 const uint8_t *input,
1268 size_t input_length,
1269 const uint8_t *mac,
Gilles Peskine13faa2d2020-01-30 16:32:21 +01001270 size_t mac_length);
Gilles Peskine69647a42019-01-14 20:18:12 +01001271
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001272/** The type of the state data structure for multipart MAC operations.
1273 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001274 * Before calling any function on a MAC operation object, the application must
1275 * initialize it by any of the following means:
1276 * - Set the structure to all-bits-zero, for example:
1277 * \code
1278 * psa_mac_operation_t operation;
1279 * memset(&operation, 0, sizeof(operation));
1280 * \endcode
1281 * - Initialize the structure to logical zero values, for example:
1282 * \code
1283 * psa_mac_operation_t operation = {0};
1284 * \endcode
1285 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1286 * for example:
1287 * \code
1288 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1289 * \endcode
1290 * - Assign the result of the function psa_mac_operation_init()
1291 * to the structure, for example:
1292 * \code
1293 * psa_mac_operation_t operation;
1294 * operation = psa_mac_operation_init();
1295 * \endcode
1296 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001297 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001298 * make any assumptions about the content of this structure except
1299 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001300typedef struct psa_mac_operation_s psa_mac_operation_t;
1301
Jaeden Amero769ce272019-01-04 11:48:03 +00001302/** \def PSA_MAC_OPERATION_INIT
1303 *
1304 * This macro returns a suitable initializer for a MAC operation object of type
1305 * #psa_mac_operation_t.
1306 */
1307#ifdef __DOXYGEN_ONLY__
1308/* This is an example definition for documentation purposes.
1309 * Implementations should define a suitable value in `crypto_struct.h`.
1310 */
1311#define PSA_MAC_OPERATION_INIT {0}
1312#endif
1313
1314/** Return an initial value for a MAC operation object.
1315 */
1316static psa_mac_operation_t psa_mac_operation_init(void);
1317
Gilles Peskinef45adda2019-01-14 18:29:18 +01001318/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001319 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001320 * This function sets up the calculation of the MAC
1321 * (message authentication code) of a byte string.
1322 * To verify the MAC of a message against an
1323 * expected value, use psa_mac_verify_setup() instead.
1324 *
1325 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001326 * -# Allocate an operation object which will be passed to all the functions
1327 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001328 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001329 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001330 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001331 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1332 * of the message each time. The MAC that is calculated is the MAC
1333 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001334 * -# At the end of the message, call psa_mac_sign_finish() to finish
1335 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001336 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001337 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1338 * operation will need to be reset by a call to psa_mac_abort(). The
1339 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001340 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001341 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001342 * After a successful call to psa_mac_sign_setup(), the application must
1343 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001344 * - A successful call to psa_mac_sign_finish().
1345 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001346 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001347 * \param[in,out] operation The operation object to set up. It must have
1348 * been initialized as per the documentation for
1349 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001350 * \param key Identifier of the key to use for the operation. It
1351 * must remain valid until the operation terminates.
1352 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001353 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001354 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001355 *
Gilles Peskine28538492018-07-11 17:34:00 +02001356 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001357 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001358 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001359 * \retval #PSA_ERROR_NOT_PERMITTED
1360 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001361 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001362 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001363 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001364 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1365 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1366 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001367 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001368 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001369 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001370 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001371 * The operation state is not valid (it must be inactive), or
1372 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001373 * It is implementation-dependent whether a failure to initialize
1374 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001375 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001376psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001377 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001378 psa_algorithm_t alg);
1379
Gilles Peskinef45adda2019-01-14 18:29:18 +01001380/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001381 *
1382 * This function sets up the verification of the MAC
1383 * (message authentication code) of a byte string against an expected value.
1384 *
1385 * The sequence of operations to verify a MAC is as follows:
1386 * -# Allocate an operation object which will be passed to all the functions
1387 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001388 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001389 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001390 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001391 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1392 * of the message each time. The MAC that is calculated is the MAC
1393 * of the concatenation of these messages in order.
1394 * -# At the end of the message, call psa_mac_verify_finish() to finish
1395 * calculating the actual MAC of the message and verify it against
1396 * the expected value.
1397 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001398 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1399 * operation will need to be reset by a call to psa_mac_abort(). The
1400 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001401 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001402 *
1403 * After a successful call to psa_mac_verify_setup(), the application must
1404 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001405 * - A successful call to psa_mac_verify_finish().
1406 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001407 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001408 * \param[in,out] operation The operation object to set up. It must have
1409 * been initialized as per the documentation for
1410 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001411 * \param key Identifier of the key to use for the operation. It
1412 * must remain valid until the operation terminates.
1413 * It must allow the usage
1414 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001415 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1416 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001417 *
Gilles Peskine28538492018-07-11 17:34:00 +02001418 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001419 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001420 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001421 * \retval #PSA_ERROR_NOT_PERMITTED
1422 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001423 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001424 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001425 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001426 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1427 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1428 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001429 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001430 * \retval #PSA_ERROR_STORAGE_FAILURE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001431 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001432 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001433 * The operation state is not valid (it must be inactive), or
1434 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001435 * It is implementation-dependent whether a failure to initialize
1436 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001437 */
1438psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001439 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001440 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001441
Gilles Peskinedcd14942018-07-12 00:30:52 +02001442/** Add a message fragment to a multipart MAC operation.
1443 *
1444 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1445 * before calling this function.
1446 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001447 * If this function returns an error status, the operation enters an error
1448 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001449 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001450 * \param[in,out] operation Active MAC operation.
1451 * \param[in] input Buffer containing the message fragment to add to
1452 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001453 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001454 *
1455 * \retval #PSA_SUCCESS
1456 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001457 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1458 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1459 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001460 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001461 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001462 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001463 * The operation state is not valid (it must be active), or
1464 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001465 * It is implementation-dependent whether a failure to initialize
1466 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001467 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001468psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1469 const uint8_t *input,
1470 size_t input_length);
1471
Gilles Peskinedcd14942018-07-12 00:30:52 +02001472/** Finish the calculation of the MAC of a message.
1473 *
1474 * The application must call psa_mac_sign_setup() before calling this function.
1475 * This function calculates the MAC of the message formed by concatenating
1476 * the inputs passed to preceding calls to psa_mac_update().
1477 *
Shaun Case0e7791f2021-12-20 21:14:10 -08001478 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001479 * If this function returns an error status, the operation enters an error
1480 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001481 *
1482 * \warning Applications should not call this function if they expect
1483 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1484 * Beware that comparing integrity or authenticity data such as
1485 * MAC values with a function such as \c memcmp is risky
1486 * because the time taken by the comparison may leak information
1487 * about the MAC value which could allow an attacker to guess
1488 * a valid MAC and thereby bypass security controls.
1489 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001490 * \param[in,out] operation Active MAC operation.
1491 * \param[out] mac Buffer where the MAC value is to be written.
1492 * \param mac_size Size of the \p mac buffer in bytes.
1493 * \param[out] mac_length On success, the number of bytes
1494 * that make up the MAC value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001495 * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001496 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001497 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001498 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001499 *
1500 * \retval #PSA_SUCCESS
1501 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001502 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001503 * The size of the \p mac buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001504 * sufficient buffer size by calling PSA_MAC_LENGTH().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001505 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1506 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1507 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001508 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001509 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001510 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001511 * The operation state is not valid (it must be an active mac sign
1512 * operation), or the library has not been previously initialized
1513 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001514 * It is implementation-dependent whether a failure to initialize
1515 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001516 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001517psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1518 uint8_t *mac,
1519 size_t mac_size,
1520 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001521
Gilles Peskinedcd14942018-07-12 00:30:52 +02001522/** Finish the calculation of the MAC of a message and compare it with
1523 * an expected value.
1524 *
1525 * The application must call psa_mac_verify_setup() before calling this function.
1526 * This function calculates the MAC of the message formed by concatenating
1527 * the inputs passed to preceding calls to psa_mac_update(). It then
1528 * compares the calculated MAC with the expected MAC passed as a
1529 * parameter to this function.
1530 *
Shaun Case0e7791f2021-12-20 21:14:10 -08001531 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001532 * If this function returns an error status, the operation enters an error
1533 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001534 *
1535 * \note Implementations shall make the best effort to ensure that the
1536 * comparison between the actual MAC and the expected MAC is performed
1537 * in constant time.
1538 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001539 * \param[in,out] operation Active MAC operation.
1540 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001541 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001542 *
1543 * \retval #PSA_SUCCESS
1544 * The expected MAC is identical to the actual MAC of the message.
1545 * \retval #PSA_ERROR_INVALID_SIGNATURE
1546 * The MAC of the message was calculated successfully, but it
1547 * differs from the expected MAC.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001548 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1549 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1550 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001551 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001552 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001553 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001554 * The operation state is not valid (it must be an active mac verify
1555 * operation), or the library has not been previously initialized
1556 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001557 * It is implementation-dependent whether a failure to initialize
1558 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001559 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001560psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1561 const uint8_t *mac,
1562 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001563
Gilles Peskinedcd14942018-07-12 00:30:52 +02001564/** Abort a MAC operation.
1565 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001566 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001567 * \p operation structure itself. Once aborted, the operation object
1568 * can be reused for another operation by calling
1569 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001570 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001571 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001572 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001573 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001574 * In particular, calling psa_mac_abort() after the operation has been
1575 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1576 * psa_mac_verify_finish() is safe and has no effect.
1577 *
1578 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001579 *
1580 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02001581 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1582 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001583 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001584 * \retval #PSA_ERROR_BAD_STATE
1585 * The library has not been previously initialized by psa_crypto_init().
1586 * It is implementation-dependent whether a failure to initialize
1587 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001588 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001589psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1590
1591/**@}*/
1592
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001593/** \defgroup cipher Symmetric ciphers
1594 * @{
1595 */
1596
Gilles Peskine69647a42019-01-14 20:18:12 +01001597/** Encrypt a message using a symmetric cipher.
1598 *
1599 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001600 * vector). Use the multipart operation interface with a
1601 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001602 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001603 * \param key Identifier of the key to use for the operation.
Ronald Cron96783552020-10-19 12:06:30 +02001604 * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001605 * \param alg The cipher algorithm to compute
1606 * (\c PSA_ALG_XXX value such that
1607 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1608 * \param[in] input Buffer containing the message to encrypt.
1609 * \param input_length Size of the \p input buffer in bytes.
1610 * \param[out] output Buffer where the output is to be written.
1611 * The output contains the IV followed by
1612 * the ciphertext proper.
1613 * \param output_size Size of the \p output buffer in bytes.
1614 * \param[out] output_length On success, the number of bytes
1615 * that make up the output.
1616 *
1617 * \retval #PSA_SUCCESS
1618 * Success.
1619 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001620 * \retval #PSA_ERROR_NOT_PERMITTED
1621 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001622 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001623 * \retval #PSA_ERROR_NOT_SUPPORTED
1624 * \p alg is not supported or is not a cipher algorithm.
1625 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1626 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1627 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1628 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001629 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001630 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001631 * \retval #PSA_ERROR_BAD_STATE
1632 * The library has not been previously initialized by psa_crypto_init().
1633 * It is implementation-dependent whether a failure to initialize
1634 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001635 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001636psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001637 psa_algorithm_t alg,
1638 const uint8_t *input,
1639 size_t input_length,
1640 uint8_t *output,
1641 size_t output_size,
1642 size_t *output_length);
1643
1644/** Decrypt a message using a symmetric cipher.
1645 *
1646 * This function decrypts a message encrypted with a symmetric cipher.
1647 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001648 * \param key Identifier of the key to use for the operation.
Gilles Peskine69647a42019-01-14 20:18:12 +01001649 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001650 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001651 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001652 * \param alg The cipher algorithm to compute
1653 * (\c PSA_ALG_XXX value such that
1654 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1655 * \param[in] input Buffer containing the message to decrypt.
1656 * This consists of the IV followed by the
1657 * ciphertext proper.
1658 * \param input_length Size of the \p input buffer in bytes.
1659 * \param[out] output Buffer where the plaintext is to be written.
1660 * \param output_size Size of the \p output buffer in bytes.
1661 * \param[out] output_length On success, the number of bytes
1662 * that make up the output.
1663 *
1664 * \retval #PSA_SUCCESS
1665 * Success.
1666 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001667 * \retval #PSA_ERROR_NOT_PERMITTED
1668 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001669 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001670 * \retval #PSA_ERROR_NOT_SUPPORTED
1671 * \p alg is not supported or is not a cipher algorithm.
1672 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1673 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1674 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1675 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001676 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001677 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001678 * \retval #PSA_ERROR_BAD_STATE
1679 * The library has not been previously initialized by psa_crypto_init().
1680 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001681 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001682 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001683psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001684 psa_algorithm_t alg,
1685 const uint8_t *input,
1686 size_t input_length,
1687 uint8_t *output,
1688 size_t output_size,
1689 size_t *output_length);
1690
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001691/** The type of the state data structure for multipart cipher operations.
1692 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001693 * Before calling any function on a cipher operation object, the application
1694 * must initialize it by any of the following means:
1695 * - Set the structure to all-bits-zero, for example:
1696 * \code
1697 * psa_cipher_operation_t operation;
1698 * memset(&operation, 0, sizeof(operation));
1699 * \endcode
1700 * - Initialize the structure to logical zero values, for example:
1701 * \code
1702 * psa_cipher_operation_t operation = {0};
1703 * \endcode
1704 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1705 * for example:
1706 * \code
1707 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1708 * \endcode
1709 * - Assign the result of the function psa_cipher_operation_init()
1710 * to the structure, for example:
1711 * \code
1712 * psa_cipher_operation_t operation;
1713 * operation = psa_cipher_operation_init();
1714 * \endcode
1715 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001716 * This is an implementation-defined \c struct. Applications should not
1717 * make any assumptions about the content of this structure except
1718 * as directed by the documentation of a specific implementation. */
1719typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1720
Jaeden Amero5bae2272019-01-04 11:48:27 +00001721/** \def PSA_CIPHER_OPERATION_INIT
1722 *
1723 * This macro returns a suitable initializer for a cipher operation object of
1724 * type #psa_cipher_operation_t.
1725 */
1726#ifdef __DOXYGEN_ONLY__
1727/* This is an example definition for documentation purposes.
1728 * Implementations should define a suitable value in `crypto_struct.h`.
1729 */
1730#define PSA_CIPHER_OPERATION_INIT {0}
1731#endif
1732
1733/** Return an initial value for a cipher operation object.
1734 */
1735static psa_cipher_operation_t psa_cipher_operation_init(void);
1736
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001737/** Set the key for a multipart symmetric encryption operation.
1738 *
1739 * The sequence of operations to encrypt a message with a symmetric cipher
1740 * is as follows:
1741 * -# Allocate an operation object which will be passed to all the functions
1742 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001743 * -# Initialize the operation object with one of the methods described in the
1744 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001745 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001746 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001747 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001748 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001749 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001750 * requires a specific IV value.
1751 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1752 * of the message each time.
1753 * -# Call psa_cipher_finish().
1754 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001755 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1756 * the operation will need to be reset by a call to psa_cipher_abort(). The
1757 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001758 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001759 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001760 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001761 * eventually terminate the operation. The following events terminate an
1762 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001763 * - A successful call to psa_cipher_finish().
1764 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001765 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001766 * \param[in,out] operation The operation object to set up. It must have
1767 * been initialized as per the documentation for
1768 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001769 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001770 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001771 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001772 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001773 * \param alg The cipher algorithm to compute
1774 * (\c PSA_ALG_XXX value such that
1775 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001776 *
Gilles Peskine28538492018-07-11 17:34:00 +02001777 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001778 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001779 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001780 * \retval #PSA_ERROR_NOT_PERMITTED
1781 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001782 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001783 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001784 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001785 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1786 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1787 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001788 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001789 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001790 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001791 * The operation state is not valid (it must be inactive), or
1792 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001793 * It is implementation-dependent whether a failure to initialize
1794 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001795 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001796psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001797 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001798 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001799
1800/** Set the key for a multipart symmetric decryption operation.
1801 *
1802 * The sequence of operations to decrypt a message with a symmetric cipher
1803 * is as follows:
1804 * -# Allocate an operation object which will be passed to all the functions
1805 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001806 * -# Initialize the operation object with one of the methods described in the
1807 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001808 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001809 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001810 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001811 * decryption. If the IV is prepended to the ciphertext, you can call
1812 * psa_cipher_update() on a buffer containing the IV followed by the
1813 * beginning of the message.
1814 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1815 * of the message each time.
1816 * -# Call psa_cipher_finish().
1817 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001818 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1819 * the operation will need to be reset by a call to psa_cipher_abort(). The
1820 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001821 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001822 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001823 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001824 * eventually terminate the operation. The following events terminate an
1825 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001826 * - A successful call to psa_cipher_finish().
1827 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001828 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001829 * \param[in,out] operation The operation object to set up. It must have
1830 * been initialized as per the documentation for
1831 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001832 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001833 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001834 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001835 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001836 * \param alg The cipher algorithm to compute
1837 * (\c PSA_ALG_XXX value such that
1838 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001839 *
Gilles Peskine28538492018-07-11 17:34:00 +02001840 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001841 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001842 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001843 * \retval #PSA_ERROR_NOT_PERMITTED
1844 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001845 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001846 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001847 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001848 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1849 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1850 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001851 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001852 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001853 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001854 * The operation state is not valid (it must be inactive), or
1855 * the library has not been previously initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03001856 * It is implementation-dependent whether a failure to initialize
1857 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001858 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001859psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001860 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001861 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001862
Gilles Peskinedcd14942018-07-12 00:30:52 +02001863/** Generate an IV for a symmetric encryption operation.
1864 *
1865 * This function generates a random IV (initialization vector), nonce
1866 * or initial counter value for the encryption operation as appropriate
1867 * for the chosen algorithm, key type and key size.
1868 *
1869 * The application must call psa_cipher_encrypt_setup() before
1870 * calling this function.
1871 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001872 * If this function returns an error status, the operation enters an error
1873 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001874 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001875 * \param[in,out] operation Active cipher operation.
1876 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001877 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001878 * \param[out] iv_length On success, the number of bytes of the
1879 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001880 *
1881 * \retval #PSA_SUCCESS
1882 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001883 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001884 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001885 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1886 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1887 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001888 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001889 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001890 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001891 * The operation state is not valid (it must be active, with no IV set),
1892 * or the library has not been previously initialized
1893 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001894 * It is implementation-dependent whether a failure to initialize
1895 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001896 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001897psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001898 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001899 size_t iv_size,
1900 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001901
Gilles Peskinedcd14942018-07-12 00:30:52 +02001902/** Set the IV for a symmetric encryption or decryption operation.
1903 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001904 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001905 * or initial counter value for the encryption or decryption operation.
1906 *
1907 * The application must call psa_cipher_encrypt_setup() before
1908 * calling this function.
1909 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001910 * If this function returns an error status, the operation enters an error
1911 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001912 *
1913 * \note When encrypting, applications should use psa_cipher_generate_iv()
1914 * instead of this function, unless implementing a protocol that requires
1915 * a non-random IV.
1916 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001917 * \param[in,out] operation Active cipher operation.
1918 * \param[in] iv Buffer containing the IV to use.
1919 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001920 *
1921 * \retval #PSA_SUCCESS
1922 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001923 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001924 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001925 * or the chosen algorithm does not use an IV.
1926 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1927 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1928 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001929 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001930 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001931 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001932 * The operation state is not valid (it must be an active cipher
1933 * encrypt operation, with no IV set), or the library has not been
1934 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001935 * It is implementation-dependent whether a failure to initialize
1936 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001937 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001938psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001939 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001940 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001941
Gilles Peskinedcd14942018-07-12 00:30:52 +02001942/** Encrypt or decrypt a message fragment in an active cipher operation.
1943 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001944 * Before calling this function, you must:
1945 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1946 * The choice of setup function determines whether this function
1947 * encrypts or decrypts its input.
1948 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1949 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001950 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001951 * If this function returns an error status, the operation enters an error
1952 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001953 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001954 * \param[in,out] operation Active cipher operation.
1955 * \param[in] input Buffer containing the message fragment to
1956 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001957 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001958 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001959 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001960 * \param[out] output_length On success, the number of bytes
1961 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001962 *
1963 * \retval #PSA_SUCCESS
1964 * Success.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001965 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1966 * The size of the \p output buffer is too small.
1967 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1968 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1969 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001970 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001971 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001972 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05001973 * The operation state is not valid (it must be active, with an IV set
1974 * if required for the algorithm), or the library has not been
1975 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001976 * It is implementation-dependent whether a failure to initialize
1977 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001978 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001979psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1980 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001981 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001982 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001983 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001984 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001985
Gilles Peskinedcd14942018-07-12 00:30:52 +02001986/** Finish encrypting or decrypting a message in a cipher operation.
1987 *
1988 * The application must call psa_cipher_encrypt_setup() or
1989 * psa_cipher_decrypt_setup() before calling this function. The choice
1990 * of setup function determines whether this function encrypts or
1991 * decrypts its input.
1992 *
1993 * This function finishes the encryption or decryption of the message
1994 * formed by concatenating the inputs passed to preceding calls to
1995 * psa_cipher_update().
1996 *
Shaun Case0e7791f2021-12-20 21:14:10 -08001997 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001998 * If this function returns an error status, the operation enters an error
1999 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002000 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002001 * \param[in,out] operation Active cipher operation.
2002 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002003 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002004 * \param[out] output_length On success, the number of bytes
2005 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002006 *
2007 * \retval #PSA_SUCCESS
2008 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02002009 * \retval #PSA_ERROR_INVALID_ARGUMENT
2010 * The total input size passed to this operation is not valid for
2011 * this particular algorithm. For example, the algorithm is a based
2012 * on block cipher and requires a whole number of blocks, but the
2013 * total input size is not a multiple of the block size.
2014 * \retval #PSA_ERROR_INVALID_PADDING
2015 * This is a decryption operation for an algorithm that includes
2016 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002017 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2018 * The size of the \p output buffer is too small.
2019 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2020 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2021 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002022 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002023 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002024 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002025 * The operation state is not valid (it must be active, with an IV set
2026 * if required for the algorithm), or the library has not been
2027 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002028 * It is implementation-dependent whether a failure to initialize
2029 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002030 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002031psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002032 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002033 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002034 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002035
Gilles Peskinedcd14942018-07-12 00:30:52 +02002036/** Abort a cipher operation.
2037 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002038 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002039 * \p operation structure itself. Once aborted, the operation object
2040 * can be reused for another operation by calling
2041 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002042 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002043 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002044 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002045 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002046 * In particular, calling psa_cipher_abort() after the operation has been
2047 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2048 * is safe and has no effect.
2049 *
2050 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002051 *
2052 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02002053 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2054 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002055 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002056 * \retval #PSA_ERROR_BAD_STATE
2057 * The library has not been previously initialized by psa_crypto_init().
2058 * It is implementation-dependent whether a failure to initialize
2059 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002060 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002061psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2062
2063/**@}*/
2064
Gilles Peskine3b555712018-03-03 21:27:57 +01002065/** \defgroup aead Authenticated encryption with associated data (AEAD)
2066 * @{
2067 */
2068
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002069/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002070 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002071 * \param key Identifier of the key to use for the
2072 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002073 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002074 * \param alg The AEAD algorithm to compute
2075 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002076 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002077 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002078 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002079 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002080 * but not encrypted.
2081 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002082 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002083 * encrypted.
2084 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002085 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002086 * encrypted data. The additional data is not
2087 * part of this output. For algorithms where the
2088 * encrypted data and the authentication tag
2089 * are defined as separate outputs, the
2090 * authentication tag is appended to the
2091 * encrypted data.
2092 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002093 * This must be appropriate for the selected
2094 * algorithm and key:
2095 * - A sufficient output size is
2096 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type,
2097 * \p alg, \p plaintext_length) where
2098 * \c key_type is the type of \p key.
2099 * - #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p
2100 * plaintext_length) evaluates to the maximum
2101 * ciphertext size of any supported AEAD
2102 * encryption.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002103 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002104 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002105 *
Gilles Peskine28538492018-07-11 17:34:00 +02002106 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002107 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002108 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002109 * \retval #PSA_ERROR_NOT_PERMITTED
2110 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002111 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002112 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002113 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002114 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002115 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Bence Szépkútibd98df72021-04-27 04:37:18 +02002116 * \p ciphertext_size is too small.
2117 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2118 * \p plaintext_length) or
2119 * #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length) can be used to
2120 * determine the required buffer size.
Gilles Peskine28538492018-07-11 17:34:00 +02002121 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2122 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002123 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002124 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002125 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002126 * The library has not been previously initialized by psa_crypto_init().
2127 * It is implementation-dependent whether a failure to initialize
2128 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002129 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002130psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002131 psa_algorithm_t alg,
2132 const uint8_t *nonce,
2133 size_t nonce_length,
2134 const uint8_t *additional_data,
2135 size_t additional_data_length,
2136 const uint8_t *plaintext,
2137 size_t plaintext_length,
2138 uint8_t *ciphertext,
2139 size_t ciphertext_size,
2140 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002141
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002142/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002143 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002144 * \param key Identifier of the key to use for the
2145 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002146 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002147 * \param alg The AEAD algorithm to compute
2148 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002149 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002150 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002151 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002152 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002153 * but not encrypted.
2154 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002155 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002156 * encrypted. For algorithms where the
2157 * encrypted data and the authentication tag
2158 * are defined as separate inputs, the buffer
2159 * must contain the encrypted data followed
2160 * by the authentication tag.
2161 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002162 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002163 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002164 * This must be appropriate for the selected
2165 * algorithm and key:
2166 * - A sufficient output size is
2167 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type,
2168 * \p alg, \p ciphertext_length) where
2169 * \c key_type is the type of \p key.
2170 * - #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p
2171 * ciphertext_length) evaluates to the maximum
2172 * plaintext size of any supported AEAD
2173 * decryption.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002174 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002175 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002176 *
Gilles Peskine28538492018-07-11 17:34:00 +02002177 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002178 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002179 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002180 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002181 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002182 * \retval #PSA_ERROR_NOT_PERMITTED
2183 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002184 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002185 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002186 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002187 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002188 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Bence Szépkútibd98df72021-04-27 04:37:18 +02002189 * \p plaintext_size is too small.
2190 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2191 * \p ciphertext_length) or
2192 * #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length) can be used
2193 * to determine the required buffer size.
Gilles Peskine28538492018-07-11 17:34:00 +02002194 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2195 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002196 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002197 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002198 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002199 * The library has not been previously initialized by psa_crypto_init().
2200 * It is implementation-dependent whether a failure to initialize
2201 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002202 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002203psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002204 psa_algorithm_t alg,
2205 const uint8_t *nonce,
2206 size_t nonce_length,
2207 const uint8_t *additional_data,
2208 size_t additional_data_length,
2209 const uint8_t *ciphertext,
2210 size_t ciphertext_length,
2211 uint8_t *plaintext,
2212 size_t plaintext_size,
2213 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002214
Gilles Peskine30a9e412019-01-14 18:36:12 +01002215/** The type of the state data structure for multipart AEAD operations.
2216 *
2217 * Before calling any function on an AEAD operation object, the application
2218 * must initialize it by any of the following means:
2219 * - Set the structure to all-bits-zero, for example:
2220 * \code
2221 * psa_aead_operation_t operation;
2222 * memset(&operation, 0, sizeof(operation));
2223 * \endcode
2224 * - Initialize the structure to logical zero values, for example:
2225 * \code
2226 * psa_aead_operation_t operation = {0};
2227 * \endcode
2228 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2229 * for example:
2230 * \code
2231 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2232 * \endcode
2233 * - Assign the result of the function psa_aead_operation_init()
2234 * to the structure, for example:
2235 * \code
2236 * psa_aead_operation_t operation;
2237 * operation = psa_aead_operation_init();
2238 * \endcode
2239 *
2240 * This is an implementation-defined \c struct. Applications should not
2241 * make any assumptions about the content of this structure except
2242 * as directed by the documentation of a specific implementation. */
2243typedef struct psa_aead_operation_s psa_aead_operation_t;
2244
2245/** \def PSA_AEAD_OPERATION_INIT
2246 *
2247 * This macro returns a suitable initializer for an AEAD operation object of
2248 * type #psa_aead_operation_t.
2249 */
2250#ifdef __DOXYGEN_ONLY__
2251/* This is an example definition for documentation purposes.
2252 * Implementations should define a suitable value in `crypto_struct.h`.
2253 */
2254#define PSA_AEAD_OPERATION_INIT {0}
2255#endif
2256
2257/** Return an initial value for an AEAD operation object.
2258 */
2259static psa_aead_operation_t psa_aead_operation_init(void);
2260
2261/** Set the key for a multipart authenticated encryption operation.
2262 *
2263 * The sequence of operations to encrypt a message with authentication
2264 * is as follows:
2265 * -# Allocate an operation object which will be passed to all the functions
2266 * listed here.
2267 * -# Initialize the operation object with one of the methods described in the
2268 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002269 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002270 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002271 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2272 * inputs to the subsequent calls to psa_aead_update_ad() and
2273 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2274 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002275 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2276 * generate or set the nonce. You should use
2277 * psa_aead_generate_nonce() unless the protocol you are implementing
2278 * requires a specific nonce value.
2279 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2280 * of the non-encrypted additional authenticated data each time.
2281 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002282 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002283 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002284 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002285 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2286 * the operation will need to be reset by a call to psa_aead_abort(). The
2287 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002288 * has been initialized.
2289 *
2290 * After a successful call to psa_aead_encrypt_setup(), the application must
2291 * eventually terminate the operation. The following events terminate an
2292 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002293 * - A successful call to psa_aead_finish().
2294 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002295 *
2296 * \param[in,out] operation The operation object to set up. It must have
2297 * been initialized as per the documentation for
2298 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002299 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002300 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002301 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002302 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002303 * \param alg The AEAD algorithm to compute
2304 * (\c PSA_ALG_XXX value such that
2305 * #PSA_ALG_IS_AEAD(\p alg) is true).
2306 *
2307 * \retval #PSA_SUCCESS
2308 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002309 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002310 * The operation state is not valid (it must be inactive), or
2311 * the library has not been previously initialized by psa_crypto_init().
Ronald Cron96783552020-10-19 12:06:30 +02002312 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002313 * \retval #PSA_ERROR_NOT_PERMITTED
2314 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002315 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002316 * \retval #PSA_ERROR_NOT_SUPPORTED
2317 * \p alg is not supported or is not an AEAD algorithm.
2318 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2319 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2320 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002321 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002322 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002323 * The library has not been previously initialized by psa_crypto_init().
2324 * It is implementation-dependent whether a failure to initialize
2325 * results in this error code.
2326 */
2327psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002328 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002329 psa_algorithm_t alg);
2330
2331/** Set the key for a multipart authenticated decryption operation.
2332 *
2333 * The sequence of operations to decrypt a message with authentication
2334 * is as follows:
2335 * -# Allocate an operation object which will be passed to all the functions
2336 * listed here.
2337 * -# Initialize the operation object with one of the methods described in the
2338 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002339 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002340 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002341 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2342 * inputs to the subsequent calls to psa_aead_update_ad() and
2343 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2344 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002345 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2346 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2347 * of the non-encrypted additional authenticated data each time.
2348 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002349 * of the ciphertext to decrypt each time.
2350 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002351 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002352 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2353 * the operation will need to be reset by a call to psa_aead_abort(). The
2354 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002355 * has been initialized.
2356 *
2357 * After a successful call to psa_aead_decrypt_setup(), the application must
2358 * eventually terminate the operation. The following events terminate an
2359 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002360 * - A successful call to psa_aead_verify().
2361 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002362 *
2363 * \param[in,out] operation The operation object to set up. It must have
2364 * been initialized as per the documentation for
2365 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002366 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002367 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002368 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002369 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002370 * \param alg The AEAD algorithm to compute
2371 * (\c PSA_ALG_XXX value such that
2372 * #PSA_ALG_IS_AEAD(\p alg) is true).
2373 *
2374 * \retval #PSA_SUCCESS
2375 * Success.
Ronald Cron96783552020-10-19 12:06:30 +02002376 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002377 * \retval #PSA_ERROR_NOT_PERMITTED
2378 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002379 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002380 * \retval #PSA_ERROR_NOT_SUPPORTED
2381 * \p alg is not supported or is not an AEAD algorithm.
2382 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2383 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2384 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002385 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002386 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002387 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002388 * The operation state is not valid (it must be inactive), or the
2389 * library has not been previously initialized by psa_crypto_init().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002390 * It is implementation-dependent whether a failure to initialize
2391 * results in this error code.
2392 */
2393psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002394 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002395 psa_algorithm_t alg);
2396
2397/** Generate a random nonce for an authenticated encryption operation.
2398 *
2399 * This function generates a random nonce for the authenticated encryption
2400 * operation with an appropriate size for the chosen algorithm, key type
2401 * and key size.
2402 *
2403 * The application must call psa_aead_encrypt_setup() before
2404 * calling this function.
2405 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002406 * If this function returns an error status, the operation enters an error
2407 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002408 *
2409 * \param[in,out] operation Active AEAD operation.
2410 * \param[out] nonce Buffer where the generated nonce is to be
2411 * written.
2412 * \param nonce_size Size of the \p nonce buffer in bytes.
2413 * \param[out] nonce_length On success, the number of bytes of the
2414 * generated nonce.
2415 *
2416 * \retval #PSA_SUCCESS
2417 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002418 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2419 * The size of the \p nonce buffer is too small.
2420 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2421 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2422 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002423 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002424 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002425 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002426 * The operation state is not valid (it must be an active aead encrypt
2427 * operation, with no nonce set), or the library has not been
2428 * previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002429 * It is implementation-dependent whether a failure to initialize
2430 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002431 */
2432psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002433 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002434 size_t nonce_size,
2435 size_t *nonce_length);
2436
2437/** Set the nonce for an authenticated encryption or decryption operation.
2438 *
2439 * This function sets the nonce for the authenticated
2440 * encryption or decryption operation.
2441 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002442 * The application must call psa_aead_encrypt_setup() or
2443 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002444 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002445 * If this function returns an error status, the operation enters an error
2446 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002447 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002448 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002449 * instead of this function, unless implementing a protocol that requires
2450 * a non-random IV.
2451 *
2452 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002453 * \param[in] nonce Buffer containing the nonce to use.
2454 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002455 *
2456 * \retval #PSA_SUCCESS
2457 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002458 * \retval #PSA_ERROR_INVALID_ARGUMENT
2459 * The size of \p nonce is not acceptable for the chosen algorithm.
2460 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2461 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2462 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002463 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002464 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002465 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002466 * The operation state is not valid (it must be active, with no nonce
2467 * set), or the library has not been previously initialized
2468 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002469 * It is implementation-dependent whether a failure to initialize
2470 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002471 */
2472psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002473 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002474 size_t nonce_length);
2475
Gilles Peskinebc59c852019-01-17 15:26:08 +01002476/** Declare the lengths of the message and additional data for AEAD.
2477 *
2478 * The application must call this function before calling
2479 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2480 * the operation requires it. If the algorithm does not require it,
2481 * calling this function is optional, but if this function is called
2482 * then the implementation must enforce the lengths.
2483 *
2484 * You may call this function before or after setting the nonce with
2485 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2486 *
2487 * - For #PSA_ALG_CCM, calling this function is required.
2488 * - For the other AEAD algorithms defined in this specification, calling
2489 * this function is not required.
2490 * - For vendor-defined algorithm, refer to the vendor documentation.
2491 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002492 * If this function returns an error status, the operation enters an error
2493 * state and must be aborted by calling psa_aead_abort().
2494 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002495 * \param[in,out] operation Active AEAD operation.
2496 * \param ad_length Size of the non-encrypted additional
2497 * authenticated data in bytes.
2498 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2499 *
2500 * \retval #PSA_SUCCESS
2501 * Success.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002502 * \retval #PSA_ERROR_INVALID_ARGUMENT
2503 * At least one of the lengths is not acceptable for the chosen
2504 * algorithm.
2505 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2506 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2507 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002508 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002509 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002510 * The operation state is not valid (it must be active, and
2511 * psa_aead_update_ad() and psa_aead_update() must not have been
2512 * called yet), or the library has not been previously initialized
2513 * by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002514 * It is implementation-dependent whether a failure to initialize
2515 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002516 */
2517psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2518 size_t ad_length,
2519 size_t plaintext_length);
2520
Gilles Peskine30a9e412019-01-14 18:36:12 +01002521/** Pass additional data to an active AEAD operation.
2522 *
2523 * Additional data is authenticated, but not encrypted.
2524 *
2525 * You may call this function multiple times to pass successive fragments
2526 * of the additional data. You may not call this function after passing
2527 * data to encrypt or decrypt with psa_aead_update().
2528 *
2529 * Before calling this function, you must:
2530 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2531 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2532 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002533 * If this function returns an error status, the operation enters an error
2534 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002535 *
2536 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2537 * there is no guarantee that the input is valid. Therefore, until
2538 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2539 * treat the input as untrusted and prepare to undo any action that
2540 * depends on the input if psa_aead_verify() returns an error status.
2541 *
2542 * \param[in,out] operation Active AEAD operation.
2543 * \param[in] input Buffer containing the fragment of
2544 * additional data.
2545 * \param input_length Size of the \p input buffer in bytes.
2546 *
2547 * \retval #PSA_SUCCESS
2548 * Success.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002549 * \retval #PSA_ERROR_INVALID_ARGUMENT
2550 * The total input length overflows the additional data length that
2551 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002552 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2553 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2554 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002555 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002556 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002557 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002558 * The operation state is not valid (it must be active, have a nonce
2559 * set, have lengths set if required by the algorithm, and
2560 * psa_aead_update() must not have been called yet), or the library
2561 * has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002562 * It is implementation-dependent whether a failure to initialize
2563 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002564 */
2565psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2566 const uint8_t *input,
2567 size_t input_length);
2568
2569/** Encrypt or decrypt a message fragment in an active AEAD operation.
2570 *
2571 * Before calling this function, you must:
2572 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2573 * The choice of setup function determines whether this function
2574 * encrypts or decrypts its input.
2575 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2576 * 3. Call psa_aead_update_ad() to pass all the additional data.
2577 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002578 * If this function returns an error status, the operation enters an error
2579 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002580 *
2581 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2582 * there is no guarantee that the input is valid. Therefore, until
2583 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2584 * - Do not use the output in any way other than storing it in a
2585 * confidential location. If you take any action that depends
2586 * on the tentative decrypted data, this action will need to be
2587 * undone if the input turns out not to be valid. Furthermore,
2588 * if an adversary can observe that this action took place
2589 * (for example through timing), they may be able to use this
2590 * fact as an oracle to decrypt any message encrypted with the
2591 * same key.
2592 * - In particular, do not copy the output anywhere but to a
2593 * memory or storage space that you have exclusive access to.
2594 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002595 * This function does not require the input to be aligned to any
2596 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002597 * a whole block at a time, it must consume all the input provided, but
2598 * it may delay the end of the corresponding output until a subsequent
2599 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2600 * provides sufficient input. The amount of data that can be delayed
2601 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002602 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002603 * \param[in,out] operation Active AEAD operation.
2604 * \param[in] input Buffer containing the message fragment to
2605 * encrypt or decrypt.
2606 * \param input_length Size of the \p input buffer in bytes.
2607 * \param[out] output Buffer where the output is to be written.
2608 * \param output_size Size of the \p output buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002609 * This must be appropriate for the selected
2610 * algorithm and key:
2611 * - A sufficient output size is
2612 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,
2613 * \c alg, \p input_length) where
2614 * \c key_type is the type of key and \c alg is
2615 * the algorithm that were used to set up the
2616 * operation.
2617 * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p
2618 * input_length) evaluates to the maximum
2619 * output size of any supported AEAD
2620 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002621 * \param[out] output_length On success, the number of bytes
2622 * that make up the returned output.
2623 *
2624 * \retval #PSA_SUCCESS
2625 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002626 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2627 * The size of the \p output buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002628 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or
2629 * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to
2630 * determine the required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002631 * \retval #PSA_ERROR_INVALID_ARGUMENT
2632 * The total length of input to psa_aead_update_ad() so far is
2633 * less than the additional data length that was previously
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002634 * specified with psa_aead_set_lengths(), or
2635 * the total input length overflows the plaintext length that
Gilles Peskinebc59c852019-01-17 15:26:08 +01002636 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002637 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2638 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2639 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002640 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002641 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002642 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002643 * The operation state is not valid (it must be active, have a nonce
2644 * set, and have lengths set if required by the algorithm), or the
2645 * library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002646 * It is implementation-dependent whether a failure to initialize
2647 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002648 */
2649psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2650 const uint8_t *input,
2651 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002652 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002653 size_t output_size,
2654 size_t *output_length);
2655
2656/** Finish encrypting a message in an AEAD operation.
2657 *
2658 * The operation must have been set up with psa_aead_encrypt_setup().
2659 *
2660 * This function finishes the authentication of the additional data
2661 * formed by concatenating the inputs passed to preceding calls to
2662 * psa_aead_update_ad() with the plaintext formed by concatenating the
2663 * inputs passed to preceding calls to psa_aead_update().
2664 *
2665 * This function has two output buffers:
2666 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002667 * preceding calls to psa_aead_update().
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002668 * - \p tag contains the authentication tag.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002669 *
Shaun Case0e7791f2021-12-20 21:14:10 -08002670 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002671 * If this function returns an error status, the operation enters an error
2672 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002673 *
2674 * \param[in,out] operation Active AEAD operation.
2675 * \param[out] ciphertext Buffer where the last part of the ciphertext
2676 * is to be written.
2677 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002678 * This must be appropriate for the selected
2679 * algorithm and key:
2680 * - A sufficient output size is
2681 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type,
2682 * \c alg) where \c key_type is the type of key
2683 * and \c alg is the algorithm that were used to
2684 * set up the operation.
2685 * - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to
2686 * the maximum output size of any supported AEAD
2687 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002688 * \param[out] ciphertext_length On success, the number of bytes of
2689 * returned ciphertext.
2690 * \param[out] tag Buffer where the authentication tag is
2691 * to be written.
2692 * \param tag_size Size of the \p tag buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002693 * This must be appropriate for the selected
2694 * algorithm and key:
2695 * - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c
2696 * key_type, \c key_bits, \c alg) where
2697 * \c key_type and \c key_bits are the type and
2698 * bit-size of the key, and \c alg is the
2699 * algorithm that were used in the call to
2700 * psa_aead_encrypt_setup().
2701 * - #PSA_AEAD_TAG_MAX_SIZE evaluates to the
2702 * maximum tag size of any supported AEAD
2703 * algorithm.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002704 * \param[out] tag_length On success, the number of bytes
2705 * that make up the returned tag.
2706 *
2707 * \retval #PSA_SUCCESS
2708 * Success.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002709 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002710 * The size of the \p ciphertext or \p tag buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002711 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or
2712 * #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the
2713 * required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type,
2714 * \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to
2715 * determine the required \p tag buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002716 * \retval #PSA_ERROR_INVALID_ARGUMENT
2717 * The total length of input to psa_aead_update_ad() so far is
2718 * less than the additional data length that was previously
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002719 * specified with psa_aead_set_lengths(), or
2720 * the total length of input to psa_aead_update() so far is
Gilles Peskinebc59c852019-01-17 15:26:08 +01002721 * less than the plaintext length that was previously
2722 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002723 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2724 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2725 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002726 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002727 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002728 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002729 * The operation state is not valid (it must be an active encryption
2730 * operation with a nonce set), or the library has not been previously
2731 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002732 * It is implementation-dependent whether a failure to initialize
2733 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002734 */
2735psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002736 uint8_t *ciphertext,
2737 size_t ciphertext_size,
2738 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002739 uint8_t *tag,
2740 size_t tag_size,
2741 size_t *tag_length);
2742
2743/** Finish authenticating and decrypting a message in an AEAD operation.
2744 *
2745 * The operation must have been set up with psa_aead_decrypt_setup().
2746 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002747 * This function finishes the authenticated decryption of the message
2748 * components:
2749 *
2750 * - The additional data consisting of the concatenation of the inputs
2751 * passed to preceding calls to psa_aead_update_ad().
2752 * - The ciphertext consisting of the concatenation of the inputs passed to
2753 * preceding calls to psa_aead_update().
2754 * - The tag passed to this function call.
2755 *
2756 * If the authentication tag is correct, this function outputs any remaining
2757 * plaintext and reports success. If the authentication tag is not correct,
2758 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002759 *
Shaun Case0e7791f2021-12-20 21:14:10 -08002760 * When this function returns successfully, the operation becomes inactive.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002761 * If this function returns an error status, the operation enters an error
2762 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002763 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002764 * \note Implementations shall make the best effort to ensure that the
2765 * comparison between the actual tag and the expected tag is performed
2766 * in constant time.
2767 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002768 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002769 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002770 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002771 * from previous calls to psa_aead_update()
2772 * that could not be processed until the end
2773 * of the input.
2774 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002775 * This must be appropriate for the selected algorithm and key:
2776 * - A sufficient output size is
2777 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type,
2778 * \c alg) where \c key_type is the type of key
2779 * and \c alg is the algorithm that were used to
2780 * set up the operation.
2781 * - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to
2782 * the maximum output size of any supported AEAD
2783 * algorithm.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002784 * \param[out] plaintext_length On success, the number of bytes of
2785 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002786 * \param[in] tag Buffer containing the authentication tag.
2787 * \param tag_length Size of the \p tag buffer in bytes.
2788 *
2789 * \retval #PSA_SUCCESS
2790 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002791 * \retval #PSA_ERROR_INVALID_SIGNATURE
2792 * The calculations were successful, but the authentication tag is
2793 * not correct.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002794 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2795 * The size of the \p plaintext buffer is too small.
Bence Szépkútieb1a3012021-03-18 10:33:33 +01002796 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or
2797 * #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the
2798 * required buffer size.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002799 * \retval #PSA_ERROR_INVALID_ARGUMENT
2800 * The total length of input to psa_aead_update_ad() so far is
2801 * less than the additional data length that was previously
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002802 * specified with psa_aead_set_lengths(), or
2803 * the total length of input to psa_aead_update() so far is
Gilles Peskinebc59c852019-01-17 15:26:08 +01002804 * less than the plaintext length that was previously
2805 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002806 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2807 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2808 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002809 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002810 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002811 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05002812 * The operation state is not valid (it must be an active decryption
2813 * operation with a nonce set), or the library has not been previously
2814 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002815 * It is implementation-dependent whether a failure to initialize
2816 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002817 */
2818psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002819 uint8_t *plaintext,
2820 size_t plaintext_size,
2821 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002822 const uint8_t *tag,
2823 size_t tag_length);
2824
2825/** Abort an AEAD operation.
2826 *
2827 * Aborting an operation frees all associated resources except for the
2828 * \p operation structure itself. Once aborted, the operation object
2829 * can be reused for another operation by calling
2830 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2831 *
2832 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002833 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002834 *
2835 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002836 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2837 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002838 *
2839 * \param[in,out] operation Initialized AEAD operation.
2840 *
2841 * \retval #PSA_SUCCESS
Gilles Peskine30a9e412019-01-14 18:36:12 +01002842 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2843 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002844 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002845 * \retval #PSA_ERROR_BAD_STATE
2846 * The library has not been previously initialized by psa_crypto_init().
2847 * It is implementation-dependent whether a failure to initialize
2848 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002849 */
2850psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2851
Gilles Peskine3b555712018-03-03 21:27:57 +01002852/**@}*/
2853
Gilles Peskine20035e32018-02-03 22:44:14 +01002854/** \defgroup asymmetric Asymmetric cryptography
2855 * @{
2856 */
2857
2858/**
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002859 * \brief Sign a message with a private key. For hash-and-sign algorithms,
2860 * this includes the hashing step.
2861 *
2862 * \note To perform a multi-part hash-and-sign signature algorithm, first use
2863 * a multi-part hash operation and then pass the resulting hash to
2864 * psa_sign_hash(). PSA_ALG_GET_HASH(\p alg) can be used to determine the
2865 * hash algorithm to use.
2866 *
gabor-mezei-arm9d26fa32021-04-22 11:27:47 +02002867 * \param[in] key Identifier of the key to use for the operation.
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002868 * It must be an asymmetric key pair. The key must
2869 * allow the usage #PSA_KEY_USAGE_SIGN_MESSAGE.
gabor-mezei-arm9d26fa32021-04-22 11:27:47 +02002870 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002871 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2872 * is true), that is compatible with the type of
2873 * \p key.
gabor-mezei-arm9d26fa32021-04-22 11:27:47 +02002874 * \param[in] input The input message to sign.
2875 * \param[in] input_length Size of the \p input buffer in bytes.
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002876 * \param[out] signature Buffer where the signature is to be written.
gabor-mezei-arm9d26fa32021-04-22 11:27:47 +02002877 * \param[in] signature_size Size of the \p signature buffer in bytes. This
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002878 * must be appropriate for the selected
2879 * algorithm and key:
2880 * - The required signature size is
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002881 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2882 * where \c key_type and \c key_bits are the type and
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002883 * bit-size respectively of key.
2884 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
2885 * maximum signature size of any supported
2886 * signature algorithm.
2887 * \param[out] signature_length On success, the number of bytes that make up
2888 * the returned signature value.
2889 *
2890 * \retval #PSA_SUCCESS
2891 * \retval #PSA_ERROR_INVALID_HANDLE
2892 * \retval #PSA_ERROR_NOT_PERMITTED
2893 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2894 * or it does not permit the requested algorithm.
2895 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2896 * The size of the \p signature buffer is too small. You can
2897 * determine a sufficient buffer size by calling
2898 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2899 * where \c key_type and \c key_bits are the type and bit-size
2900 * respectively of \p key.
2901 * \retval #PSA_ERROR_NOT_SUPPORTED
2902 * \retval #PSA_ERROR_INVALID_ARGUMENT
2903 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2904 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2905 * \retval #PSA_ERROR_HARDWARE_FAILURE
2906 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2907 * \retval #PSA_ERROR_STORAGE_FAILURE
2908 * \retval #PSA_ERROR_DATA_CORRUPT
2909 * \retval #PSA_ERROR_DATA_INVALID
2910 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2911 * \retval #PSA_ERROR_BAD_STATE
2912 * The library has not been previously initialized by psa_crypto_init().
2913 * It is implementation-dependent whether a failure to initialize
2914 * results in this error code.
2915 */
2916psa_status_t psa_sign_message( mbedtls_svc_key_id_t key,
2917 psa_algorithm_t alg,
2918 const uint8_t * input,
2919 size_t input_length,
2920 uint8_t * signature,
2921 size_t signature_size,
2922 size_t * signature_length );
2923
2924/** \brief Verify the signature of a message with a public key, using
2925 * a hash-and-sign verification algorithm.
2926 *
2927 * \note To perform a multi-part hash-and-sign signature verification
2928 * algorithm, first use a multi-part hash operation to hash the message
2929 * and then pass the resulting hash to psa_verify_hash().
2930 * PSA_ALG_GET_HASH(\p alg) can be used to determine the hash algorithm
2931 * to use.
2932 *
gabor-mezei-arm9d26fa32021-04-22 11:27:47 +02002933 * \param[in] key Identifier of the key to use for the operation.
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002934 * It must be a public key or an asymmetric key
2935 * pair. The key must allow the usage
2936 * #PSA_KEY_USAGE_VERIFY_MESSAGE.
gabor-mezei-arm9d26fa32021-04-22 11:27:47 +02002937 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002938 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2939 * is true), that is compatible with the type of
2940 * \p key.
gabor-mezei-arm9d26fa32021-04-22 11:27:47 +02002941 * \param[in] input The message whose signature is to be verified.
2942 * \param[in] input_length Size of the \p input buffer in bytes.
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002943 * \param[out] signature Buffer containing the signature to verify.
gabor-mezei-arm9d26fa32021-04-22 11:27:47 +02002944 * \param[in] signature_length Size of the \p signature buffer in bytes.
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002945 *
2946 * \retval #PSA_SUCCESS
2947 * \retval #PSA_ERROR_INVALID_HANDLE
2948 * \retval #PSA_ERROR_NOT_PERMITTED
2949 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2950 * or it does not permit the requested algorithm.
2951 * \retval #PSA_ERROR_INVALID_SIGNATURE
2952 * The calculation was performed successfully, but the passed signature
2953 * is not a valid signature.
2954 * \retval #PSA_ERROR_NOT_SUPPORTED
2955 * \retval #PSA_ERROR_INVALID_ARGUMENT
2956 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2957 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2958 * \retval #PSA_ERROR_HARDWARE_FAILURE
2959 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2960 * \retval #PSA_ERROR_STORAGE_FAILURE
2961 * \retval #PSA_ERROR_DATA_CORRUPT
2962 * \retval #PSA_ERROR_DATA_INVALID
2963 * \retval #PSA_ERROR_BAD_STATE
2964 * The library has not been previously initialized by psa_crypto_init().
2965 * It is implementation-dependent whether a failure to initialize
2966 * results in this error code.
2967 */
2968psa_status_t psa_verify_message( mbedtls_svc_key_id_t key,
2969 psa_algorithm_t alg,
2970 const uint8_t * input,
2971 size_t input_length,
2972 const uint8_t * signature,
2973 size_t signature_length );
2974
2975/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002976 * \brief Sign a hash or short message with a private key.
2977 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002978 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002979 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01002980 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
2981 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02002982 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2983 * to determine the hash algorithm to use.
2984 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002985 * \param key Identifier of the key to use for the operation.
2986 * It must be an asymmetric key pair. The key must
Ronald Cron96783552020-10-19 12:06:30 +02002987 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
Mateusz Starzykd22362c2021-08-26 11:46:14 +02002988 * \param alg A signature algorithm (PSA_ALG_XXX
2989 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
2990 * is true), that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002991 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002992 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002993 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002994 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002995 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002996 * \param[out] signature_length On success, the number of bytes
2997 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002998 *
Gilles Peskine28538492018-07-11 17:34:00 +02002999 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01003000 * \retval #PSA_ERROR_INVALID_HANDLE
3001 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003002 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003003 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01003004 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003005 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01003006 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003007 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003008 * \retval #PSA_ERROR_NOT_SUPPORTED
3009 * \retval #PSA_ERROR_INVALID_ARGUMENT
3010 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3011 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3012 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003013 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01003014 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003015 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03003016 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003017 * The library has not been previously initialized by psa_crypto_init().
3018 * It is implementation-dependent whether a failure to initialize
3019 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01003020 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003021psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003022 psa_algorithm_t alg,
3023 const uint8_t *hash,
3024 size_t hash_length,
3025 uint8_t *signature,
3026 size_t signature_size,
3027 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003028
3029/**
gabor-mezei-arme8efa392021-04-14 21:14:28 +02003030 * \brief Verify the signature of a hash or short message using a public key.
Gilles Peskine20035e32018-02-03 22:44:14 +01003031 *
Gilles Peskine08bac712018-06-26 16:14:46 +02003032 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02003033 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine79622842021-02-24 21:56:22 +01003034 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
3035 * Then pass the resulting hash as the \p hash
Gilles Peskine08bac712018-06-26 16:14:46 +02003036 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
3037 * to determine the hash algorithm to use.
3038 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003039 * \param key Identifier of the key to use for the operation. It
3040 * must be a public key or an asymmetric key pair. The
Ronald Cron96783552020-10-19 12:06:30 +02003041 * key must allow the usage
3042 * #PSA_KEY_USAGE_VERIFY_HASH.
Mateusz Starzykd22362c2021-08-26 11:46:14 +02003043 * \param alg A signature algorithm (PSA_ALG_XXX
3044 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
3045 * is true), that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02003046 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003047 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02003048 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003049 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003050 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003051 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003052 *
Gilles Peskine28538492018-07-11 17:34:00 +02003053 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01003054 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01003055 * \retval #PSA_ERROR_INVALID_HANDLE
3056 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003057 * \retval #PSA_ERROR_INVALID_SIGNATURE
Shaun Case0e7791f2021-12-20 21:14:10 -08003058 * The calculation was performed successfully, but the passed
Gilles Peskine308b91d2018-02-08 09:47:44 +01003059 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02003060 * \retval #PSA_ERROR_NOT_SUPPORTED
3061 * \retval #PSA_ERROR_INVALID_ARGUMENT
3062 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3063 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3064 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003065 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01003066 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003067 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003068 * The library has not been previously initialized by psa_crypto_init().
3069 * It is implementation-dependent whether a failure to initialize
3070 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01003071 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003072psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003073 psa_algorithm_t alg,
3074 const uint8_t *hash,
3075 size_t hash_length,
3076 const uint8_t *signature,
3077 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003078
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003079/**
3080 * \brief Encrypt a short message with a public key.
3081 *
Shaun Case0e7791f2021-12-20 21:14:10 -08003082 * \param key Identifier of the key to use for the operation.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003083 * It must be a public key or an asymmetric key
3084 * pair. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02003085 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003086 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003087 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003088 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003089 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003090 * \param[in] salt A salt or label, if supported by the
3091 * encryption algorithm.
3092 * If the algorithm does not support a
3093 * salt, pass \c NULL.
3094 * If the algorithm supports an optional
3095 * salt and you do not want to pass a salt,
3096 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003097 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003098 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3099 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003100 * \param salt_length Size of the \p salt buffer in bytes.
3101 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003102 * \param[out] output Buffer where the encrypted message is to
3103 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003104 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003105 * \param[out] output_length On success, the number of bytes
3106 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003107 *
Gilles Peskine28538492018-07-11 17:34:00 +02003108 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01003109 * \retval #PSA_ERROR_INVALID_HANDLE
3110 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003111 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003112 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003113 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02003114 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003115 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003116 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003117 * \retval #PSA_ERROR_NOT_SUPPORTED
3118 * \retval #PSA_ERROR_INVALID_ARGUMENT
3119 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3120 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3121 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003122 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01003123 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003124 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03003125 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003126 * The library has not been previously initialized by psa_crypto_init().
3127 * It is implementation-dependent whether a failure to initialize
3128 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003129 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003130psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003131 psa_algorithm_t alg,
3132 const uint8_t *input,
3133 size_t input_length,
3134 const uint8_t *salt,
3135 size_t salt_length,
3136 uint8_t *output,
3137 size_t output_size,
3138 size_t *output_length);
3139
3140/**
3141 * \brief Decrypt a short message with a private key.
3142 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003143 * \param key Identifier of the key to use for the operation.
3144 * It must be an asymmetric key pair. It must
Ronald Cron96783552020-10-19 12:06:30 +02003145 * allow the usage #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003146 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003147 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003148 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003149 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003150 * \param[in] salt A salt or label, if supported by the
3151 * encryption algorithm.
3152 * If the algorithm does not support a
3153 * salt, pass \c NULL.
3154 * If the algorithm supports an optional
3155 * salt and you do not want to pass a salt,
3156 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003157 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003158 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3159 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003160 * \param salt_length Size of the \p salt buffer in bytes.
3161 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003162 * \param[out] output Buffer where the decrypted message is to
3163 * be written.
3164 * \param output_size Size of the \c output buffer in bytes.
3165 * \param[out] output_length On success, the number of bytes
3166 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003167 *
Gilles Peskine28538492018-07-11 17:34:00 +02003168 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003169 * \retval #PSA_ERROR_INVALID_HANDLE
3170 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003171 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003172 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003173 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003174 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003175 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003176 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003177 * \retval #PSA_ERROR_NOT_SUPPORTED
3178 * \retval #PSA_ERROR_INVALID_ARGUMENT
3179 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3180 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3181 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003182 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003183 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003184 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3185 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003186 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003187 * The library has not been previously initialized by psa_crypto_init().
3188 * It is implementation-dependent whether a failure to initialize
3189 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003190 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003191psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003192 psa_algorithm_t alg,
3193 const uint8_t *input,
3194 size_t input_length,
3195 const uint8_t *salt,
3196 size_t salt_length,
3197 uint8_t *output,
3198 size_t output_size,
3199 size_t *output_length);
3200
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003201/**@}*/
3202
Gilles Peskine35675b62019-05-16 17:26:11 +02003203/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003204 * @{
3205 */
3206
Gilles Peskine35675b62019-05-16 17:26:11 +02003207/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003208 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003209 * Before calling any function on a key derivation operation object, the
3210 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003211 * - Set the structure to all-bits-zero, for example:
3212 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003213 * psa_key_derivation_operation_t operation;
3214 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003215 * \endcode
3216 * - Initialize the structure to logical zero values, for example:
3217 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003218 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003219 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003220 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003221 * for example:
3222 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003223 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003224 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003225 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003226 * to the structure, for example:
3227 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003228 * psa_key_derivation_operation_t operation;
3229 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003230 * \endcode
3231 *
3232 * This is an implementation-defined \c struct. Applications should not
3233 * make any assumptions about the content of this structure except
3234 * as directed by the documentation of a specific implementation.
3235 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003236typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003237
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003238/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003239 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003240 * This macro returns a suitable initializer for a key derivation operation
3241 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003242 */
3243#ifdef __DOXYGEN_ONLY__
3244/* This is an example definition for documentation purposes.
3245 * Implementations should define a suitable value in `crypto_struct.h`.
3246 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003247#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003248#endif
3249
Gilles Peskine35675b62019-05-16 17:26:11 +02003250/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003251 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003252static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003253
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003254/** Set up a key derivation operation.
3255 *
3256 * A key derivation algorithm takes some inputs and uses them to generate
3257 * a byte stream in a deterministic way.
3258 * This byte stream can be used to produce keys and other
3259 * cryptographic material.
3260 *
3261 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003262 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3263 * -# Call psa_key_derivation_setup() to select the algorithm.
3264 * -# Provide the inputs for the key derivation by calling
3265 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3266 * as appropriate. Which inputs are needed, in what order, and whether
3267 * they may be keys and if so of what type depends on the algorithm.
3268 * -# Optionally set the operation's maximum capacity with
3269 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3270 * of or after providing inputs. For some algorithms, this step is mandatory
3271 * because the output depends on the maximum capacity.
3272 * -# To derive a key, call psa_key_derivation_output_key().
3273 * To derive a byte string for a different purpose, call
3274 * psa_key_derivation_output_bytes().
3275 * Successive calls to these functions use successive output bytes
3276 * calculated by the key derivation algorithm.
3277 * -# Clean up the key derivation operation object with
3278 * psa_key_derivation_abort().
3279 *
3280 * If this function returns an error, the key derivation operation object is
3281 * not changed.
3282 *
3283 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3284 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003285 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003286 * Implementations must reject an attempt to derive a key of size 0.
3287 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003288 * \param[in,out] operation The key derivation operation object
3289 * to set up. It must
3290 * have been initialized but not set up yet.
3291 * \param alg The key derivation algorithm to compute
3292 * (\c PSA_ALG_XXX value such that
3293 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3294 *
3295 * \retval #PSA_SUCCESS
3296 * Success.
3297 * \retval #PSA_ERROR_INVALID_ARGUMENT
3298 * \c alg is not a key derivation algorithm.
3299 * \retval #PSA_ERROR_NOT_SUPPORTED
3300 * \c alg is not supported or is not a key derivation algorithm.
3301 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3302 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3303 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003304 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003305 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003306 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003307 * The operation state is not valid (it must be inactive), or
3308 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003309 * It is implementation-dependent whether a failure to initialize
3310 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003311 */
3312psa_status_t psa_key_derivation_setup(
3313 psa_key_derivation_operation_t *operation,
3314 psa_algorithm_t alg);
3315
Gilles Peskine35675b62019-05-16 17:26:11 +02003316/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003317 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003318 * The capacity of a key derivation is the maximum number of bytes that it can
3319 * return. When you get *N* bytes of output from a key derivation operation,
3320 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003321 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003322 * \param[in] operation The operation to query.
3323 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003324 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003325 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003326 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003327 * \retval #PSA_ERROR_HARDWARE_FAILURE
3328 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003329 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003330 * The operation state is not valid (it must be active), or
3331 * the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003332 * It is implementation-dependent whether a failure to initialize
3333 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003334 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003335psa_status_t psa_key_derivation_get_capacity(
3336 const psa_key_derivation_operation_t *operation,
3337 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003338
Gilles Peskine35675b62019-05-16 17:26:11 +02003339/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003340 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003341 * The capacity of a key derivation operation is the maximum number of bytes
3342 * that the key derivation operation can return from this point onwards.
3343 *
3344 * \param[in,out] operation The key derivation operation object to modify.
3345 * \param capacity The new capacity of the operation.
3346 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003347 * current capacity.
3348 *
3349 * \retval #PSA_SUCCESS
3350 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003351 * \p capacity is larger than the operation's current capacity.
3352 * In this case, the operation object remains valid and its capacity
3353 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003354 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003355 * \retval #PSA_ERROR_HARDWARE_FAILURE
3356 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003357 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003358 * The operation state is not valid (it must be active), or the
3359 * library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003360 * It is implementation-dependent whether a failure to initialize
3361 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003362 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003363psa_status_t psa_key_derivation_set_capacity(
3364 psa_key_derivation_operation_t *operation,
3365 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003366
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003367/** Use the maximum possible capacity for a key derivation operation.
3368 *
3369 * Use this value as the capacity argument when setting up a key derivation
3370 * to indicate that the operation should have the maximum possible capacity.
3371 * The value of the maximum possible capacity depends on the key derivation
3372 * algorithm.
3373 */
3374#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3375
3376/** Provide an input for key derivation or key agreement.
3377 *
3378 * Which inputs are required and in what order depends on the algorithm.
3379 * Refer to the documentation of each key derivation or key agreement
3380 * algorithm for information.
3381 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003382 * This function passes direct inputs, which is usually correct for
3383 * non-secret inputs. To pass a secret input, which should be in a key
3384 * object, call psa_key_derivation_input_key() instead of this function.
3385 * Refer to the documentation of individual step types
3386 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3387 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003388 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003389 * If this function returns an error status, the operation enters an error
3390 * state and must be aborted by calling psa_key_derivation_abort().
3391 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003392 * \param[in,out] operation The key derivation operation object to use.
3393 * It must have been set up with
3394 * psa_key_derivation_setup() and must not
3395 * have produced any output yet.
3396 * \param step Which step the input data is for.
3397 * \param[in] data Input data to use.
3398 * \param data_length Size of the \p data buffer in bytes.
3399 *
3400 * \retval #PSA_SUCCESS
3401 * Success.
3402 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003403 * \c step is not compatible with the operation's algorithm, or
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003404 * \c step does not allow direct inputs.
3405 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3406 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3407 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003408 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003409 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003410 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003411 * The operation state is not valid for this input \p step, or
3412 * the library has not been previously initialized by psa_crypto_init().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003413 * It is implementation-dependent whether a failure to initialize
3414 * results in this error code.
3415 */
3416psa_status_t psa_key_derivation_input_bytes(
3417 psa_key_derivation_operation_t *operation,
3418 psa_key_derivation_step_t step,
3419 const uint8_t *data,
3420 size_t data_length);
3421
3422/** Provide an input for key derivation in the form of a key.
3423 *
3424 * Which inputs are required and in what order depends on the algorithm.
3425 * Refer to the documentation of each key derivation or key agreement
3426 * algorithm for information.
3427 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003428 * This function obtains input from a key object, which is usually correct for
3429 * secret inputs or for non-secret personalization strings kept in the key
3430 * store. To pass a non-secret parameter which is not in the key store,
3431 * call psa_key_derivation_input_bytes() instead of this function.
3432 * Refer to the documentation of individual step types
3433 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3434 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003435 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003436 * If this function returns an error status, the operation enters an error
3437 * state and must be aborted by calling psa_key_derivation_abort().
3438 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003439 * \param[in,out] operation The key derivation operation object to use.
3440 * It must have been set up with
3441 * psa_key_derivation_setup() and must not
3442 * have produced any output yet.
3443 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003444 * \param key Identifier of the key. It must have an
3445 * appropriate type for step and must allow the
Ronald Cron96783552020-10-19 12:06:30 +02003446 * usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003447 *
3448 * \retval #PSA_SUCCESS
3449 * Success.
3450 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003451 * \retval #PSA_ERROR_NOT_PERMITTED
3452 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003453 * \c step is not compatible with the operation's algorithm, or
Gilles Peskine224b0d62019-09-23 18:13:17 +02003454 * \c step does not allow key inputs of the given type
3455 * or does not allow key inputs at all.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003456 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3457 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3458 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003459 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003460 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003461 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003462 * The operation state is not valid for this input \p step, or
3463 * the library has not been previously initialized by psa_crypto_init().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003464 * It is implementation-dependent whether a failure to initialize
3465 * results in this error code.
3466 */
3467psa_status_t psa_key_derivation_input_key(
3468 psa_key_derivation_operation_t *operation,
3469 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003470 mbedtls_svc_key_id_t key);
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003471
3472/** Perform a key agreement and use the shared secret as input to a key
3473 * derivation.
3474 *
3475 * A key agreement algorithm takes two inputs: a private key \p private_key
3476 * a public key \p peer_key.
3477 * The result of this function is passed as input to a key derivation.
3478 * The output of this key derivation can be extracted by reading from the
3479 * resulting operation to produce keys and other cryptographic material.
3480 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003481 * If this function returns an error status, the operation enters an error
3482 * state and must be aborted by calling psa_key_derivation_abort().
3483 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003484 * \param[in,out] operation The key derivation operation object to use.
3485 * It must have been set up with
3486 * psa_key_derivation_setup() with a
3487 * key agreement and derivation algorithm
3488 * \c alg (\c PSA_ALG_XXX value such that
3489 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3490 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3491 * is false).
3492 * The operation must be ready for an
3493 * input of the type given by \p step.
3494 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003495 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003496 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003497 * \param[in] peer_key Public key of the peer. The peer key must be in the
3498 * same format that psa_import_key() accepts for the
3499 * public key type corresponding to the type of
3500 * private_key. That is, this function performs the
3501 * equivalent of
3502 * #psa_import_key(...,
3503 * `peer_key`, `peer_key_length`) where
3504 * with key attributes indicating the public key
3505 * type corresponding to the type of `private_key`.
3506 * For example, for EC keys, this means that peer_key
3507 * is interpreted as a point on the curve that the
3508 * private key is on. The standard formats for public
3509 * keys are documented in the documentation of
3510 * psa_export_public_key().
3511 * \param peer_key_length Size of \p peer_key in bytes.
3512 *
3513 * \retval #PSA_SUCCESS
3514 * Success.
3515 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003516 * \retval #PSA_ERROR_NOT_PERMITTED
3517 * \retval #PSA_ERROR_INVALID_ARGUMENT
3518 * \c private_key is not compatible with \c alg,
3519 * or \p peer_key is not valid for \c alg or not compatible with
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003520 * \c private_key, or \c step does not allow an input resulting
3521 * from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003522 * \retval #PSA_ERROR_NOT_SUPPORTED
3523 * \c alg is not supported or is not a key derivation algorithm.
3524 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3525 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3526 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003527 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003528 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003529 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003530 * The operation state is not valid for this key agreement \p step,
3531 * or the library has not been previously initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003532 * It is implementation-dependent whether a failure to initialize
3533 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003534 */
3535psa_status_t psa_key_derivation_key_agreement(
3536 psa_key_derivation_operation_t *operation,
3537 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003538 mbedtls_svc_key_id_t private_key,
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003539 const uint8_t *peer_key,
3540 size_t peer_key_length);
3541
Gilles Peskine35675b62019-05-16 17:26:11 +02003542/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003543 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003544 * This function calculates output bytes from a key derivation algorithm and
3545 * return those bytes.
3546 * If you view the key derivation's output as a stream of bytes, this
3547 * function destructively reads the requested number of bytes from the
3548 * stream.
3549 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003550 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003551 * If this function returns an error status other than
3552 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003553 * state and must be aborted by calling psa_key_derivation_abort().
3554 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003555 * \param[in,out] operation The key derivation operation object to read from.
3556 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003557 * \param output_length Number of bytes to output.
3558 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003559 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003560 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003561 * The operation's capacity was less than
3562 * \p output_length bytes. Note that in this case,
3563 * no output is written to the output buffer.
3564 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003565 * subsequent calls to this function will not
3566 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003567 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3568 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3569 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003570 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003571 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003572 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003573 * The operation state is not valid (it must be active and completed
3574 * all required input steps), or the library has not been previously
3575 * initialized by psa_crypto_init().
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003576 * It is implementation-dependent whether a failure to initialize
3577 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003578 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003579psa_status_t psa_key_derivation_output_bytes(
3580 psa_key_derivation_operation_t *operation,
3581 uint8_t *output,
3582 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003583
Gilles Peskine35675b62019-05-16 17:26:11 +02003584/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003585 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003586 * This function calculates output bytes from a key derivation algorithm
3587 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003588 * The key's location, usage policy, type and size are taken from
3589 * \p attributes.
3590 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003591 * If you view the key derivation's output as a stream of bytes, this
3592 * function destructively reads as many bytes as required from the
3593 * stream.
3594 * The operation's capacity decreases by the number of bytes read.
3595 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003596 * If this function returns an error status other than
3597 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003598 * state and must be aborted by calling psa_key_derivation_abort().
3599 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003600 * How much output is produced and consumed from the operation, and how
Gilles Peskine364d12c2021-03-08 17:23:47 +01003601 * the key is derived, depends on the key type and on the key size
3602 * (denoted \c bits below):
Gilles Peskineeab56e42018-07-12 17:12:33 +02003603 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003604 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003605 * of a given size, this function is functionally equivalent to
3606 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003607 * and passing the resulting output to #psa_import_key.
3608 * However, this function has a security benefit:
3609 * if the implementation provides an isolation boundary then
3610 * the key material is not exposed outside the isolation boundary.
3611 * As a consequence, for these key types, this function always consumes
Gilles Peskine364d12c2021-03-08 17:23:47 +01003612 * exactly (\c bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003613 * The following key types defined in this specification follow this scheme:
3614 *
3615 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003616 * - #PSA_KEY_TYPE_ARC4;
Gilles Peskine8890f642021-09-21 11:59:39 +02003617 * - #PSA_KEY_TYPE_ARIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003618 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003619 * - #PSA_KEY_TYPE_DERIVE;
3620 * - #PSA_KEY_TYPE_HMAC.
3621 *
3622 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003623 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003624 * Montgomery curve), this function always draws a byte string whose
3625 * length is determined by the curve, and sets the mandatory bits
3626 * accordingly. That is:
3627 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01003628 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003629 * string and process it as specified in RFC 7748 &sect;5.
Paul Elliott8ff510a2020-06-02 17:19:28 +01003630 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003631 * string and process it as specified in RFC 7748 &sect;5.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003632 *
3633 * - For key types for which the key is represented by a single sequence of
Gilles Peskine364d12c2021-03-08 17:23:47 +01003634 * \c bits bits with constraints as to which bit sequences are acceptable,
3635 * this function draws a byte string of length (\c bits / 8) bytes rounded
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003636 * up to the nearest whole number of bytes. If the resulting byte string
3637 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3638 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003639 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003640 * for the output produced by psa_export_key().
3641 * The following key types defined in this specification follow this scheme:
3642 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003643 * - #PSA_KEY_TYPE_DES.
3644 * Force-set the parity bits, but discard forbidden weak keys.
3645 * For 2-key and 3-key triple-DES, the three keys are generated
3646 * successively (for example, for 3-key triple-DES,
3647 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3648 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003649 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003650 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003651 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003652 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003653 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003654 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003655 * Weierstrass curve).
3656 * For these key types, interpret the byte string as integer
3657 * in big-endian order. Discard it if it is not in the range
3658 * [0, *N* - 2] where *N* is the boundary of the private key domain
3659 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003660 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003661 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003662 * This method allows compliance to NIST standards, specifically
3663 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003664 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3665 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3666 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3667 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003668 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003669 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003670 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003671 * implementation-defined.
3672 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003673 * In all cases, the data that is read is discarded from the operation.
3674 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003675 *
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003676 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3677 * the input to that step must be provided with psa_key_derivation_input_key().
3678 * Future versions of this specification may include additional restrictions
3679 * on the derived key based on the attributes and strength of the secret key.
3680 *
Gilles Peskine20628592019-04-19 19:29:50 +02003681 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003682 * \param[in,out] operation The key derivation operation object to read from.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003683 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003684 * key. For persistent keys, this is the key
3685 * identifier defined in \p attributes.
3686 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003687 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003688 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003689 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003690 * If the key is persistent, the key material and the key's metadata
3691 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003692 * \retval #PSA_ERROR_ALREADY_EXISTS
3693 * This is an attempt to create a persistent key, and there is
3694 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003695 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003696 * There was not enough data to create the desired key.
3697 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003698 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003699 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003700 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003701 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003702 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003703 * \retval #PSA_ERROR_INVALID_ARGUMENT
3704 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003705 * \retval #PSA_ERROR_NOT_PERMITTED
3706 * The #PSA_KEY_DERIVATION_INPUT_SECRET input was not provided through
3707 * a key.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003708 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3709 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3710 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3711 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003712 * \retval #PSA_ERROR_CORRUPTION_DETECTED
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01003713 * \retval #PSA_ERROR_DATA_INVALID
3714 * \retval #PSA_ERROR_DATA_CORRUPT
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003715 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003716 * \retval #PSA_ERROR_BAD_STATE
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003717 * The operation state is not valid (it must be active and completed
3718 * all required input steps), or the library has not been previously
3719 * initialized by psa_crypto_init().
itayzafrir18617092018-09-16 12:22:41 +03003720 * It is implementation-dependent whether a failure to initialize
3721 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003722 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003723psa_status_t psa_key_derivation_output_key(
3724 const psa_key_attributes_t *attributes,
3725 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003726 mbedtls_svc_key_id_t *key);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003727
Gilles Peskine35675b62019-05-16 17:26:11 +02003728/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003729 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003730 * Aborting an operation frees all associated resources except for the \c
3731 * operation structure itself. Once aborted, the operation object can be reused
3732 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003733 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003734 * This function may be called at any time after the operation
3735 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003736 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003737 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3738 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003739 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003740 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003741 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003742 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003743 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3744 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003745 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003746 * \retval #PSA_ERROR_BAD_STATE
3747 * The library has not been previously initialized by psa_crypto_init().
3748 * It is implementation-dependent whether a failure to initialize
3749 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003750 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003751psa_status_t psa_key_derivation_abort(
3752 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003753
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003754/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003755 *
3756 * \warning The raw result of a key agreement algorithm such as finite-field
3757 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3758 * not be used directly as key material. It should instead be passed as
3759 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003760 * a key derivation, use psa_key_derivation_key_agreement() and other
3761 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003762 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003763 * \param alg The key agreement algorithm to compute
3764 * (\c PSA_ALG_XXX value such that
3765 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3766 * is true).
Ronald Croncf56a0a2020-08-04 09:51:30 +02003767 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003768 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003769 * \param[in] peer_key Public key of the peer. It must be
3770 * in the same format that psa_import_key()
3771 * accepts. The standard formats for public
3772 * keys are documented in the documentation
3773 * of psa_export_public_key().
3774 * \param peer_key_length Size of \p peer_key in bytes.
3775 * \param[out] output Buffer where the decrypted message is to
3776 * be written.
3777 * \param output_size Size of the \c output buffer in bytes.
3778 * \param[out] output_length On success, the number of bytes
3779 * that make up the returned output.
3780 *
3781 * \retval #PSA_SUCCESS
3782 * Success.
3783 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003784 * \retval #PSA_ERROR_NOT_PERMITTED
3785 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrzej Kurekfcaef2e2022-02-04 07:54:59 -05003786 * \p alg is not a key agreement algorithm, or
Gilles Peskine769c7a62019-01-18 16:42:29 +01003787 * \p private_key is not compatible with \p alg,
3788 * or \p peer_key is not valid for \p alg or not compatible with
3789 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003790 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3791 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003792 * \retval #PSA_ERROR_NOT_SUPPORTED
3793 * \p alg is not a supported key agreement algorithm.
3794 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3795 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3796 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003797 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003798 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003799 * \retval #PSA_ERROR_BAD_STATE
3800 * The library has not been previously initialized by psa_crypto_init().
3801 * It is implementation-dependent whether a failure to initialize
3802 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003803 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003804psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003805 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02003806 const uint8_t *peer_key,
3807 size_t peer_key_length,
3808 uint8_t *output,
3809 size_t output_size,
3810 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003811
Gilles Peskineea0fb492018-07-12 17:17:20 +02003812/**@}*/
3813
Gilles Peskineedd76872018-07-20 17:42:05 +02003814/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003815 * @{
3816 */
3817
3818/**
3819 * \brief Generate random bytes.
3820 *
3821 * \warning This function **can** fail! Callers MUST check the return status
3822 * and MUST NOT use the content of the output buffer if the return
3823 * status is not #PSA_SUCCESS.
3824 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003825 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003826 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003827 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003828 * \param output_size Number of bytes to generate and output.
3829 *
Gilles Peskine28538492018-07-11 17:34:00 +02003830 * \retval #PSA_SUCCESS
3831 * \retval #PSA_ERROR_NOT_SUPPORTED
3832 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003833 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003834 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3835 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003836 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003837 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003838 * The library has not been previously initialized by psa_crypto_init().
3839 * It is implementation-dependent whether a failure to initialize
3840 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003841 */
3842psa_status_t psa_generate_random(uint8_t *output,
3843 size_t output_size);
3844
3845/**
3846 * \brief Generate a key or key pair.
3847 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003848 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02003849 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003850 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003851 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003852 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003853 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003854 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003855 * the public exponent is 65537.
3856 * The modulus is a product of two probabilistic primes
3857 * between 2^{n-1} and 2^n where n is the bit size specified in the
3858 * attributes.
3859 *
Gilles Peskine20628592019-04-19 19:29:50 +02003860 * \param[in] attributes The attributes for the new key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003861 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003862 * key. For persistent keys, this is the key
3863 * identifier defined in \p attributes.
3864 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003865 *
Gilles Peskine28538492018-07-11 17:34:00 +02003866 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003867 * Success.
3868 * If the key is persistent, the key material and the key's metadata
3869 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003870 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003871 * This is an attempt to create a persistent key, and there is
3872 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003873 * \retval #PSA_ERROR_NOT_SUPPORTED
3874 * \retval #PSA_ERROR_INVALID_ARGUMENT
3875 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3876 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3877 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3878 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003879 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003880 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01003881 * \retval #PSA_ERROR_DATA_INVALID
3882 * \retval #PSA_ERROR_DATA_CORRUPT
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003883 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003884 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003885 * The library has not been previously initialized by psa_crypto_init().
3886 * It is implementation-dependent whether a failure to initialize
3887 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003888 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003889psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003890 mbedtls_svc_key_id_t *key);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003891
3892/**@}*/
3893
Gilles Peskinee59236f2018-01-27 23:32:46 +01003894#ifdef __cplusplus
3895}
3896#endif
3897
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003898/* The file "crypto_sizes.h" contains definitions for size calculation
3899 * macros whose definitions are implementation-specific. */
3900#include "crypto_sizes.h"
3901
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003902/* The file "crypto_struct.h" contains definitions for
3903 * implementation-specific structs that are declared above. */
3904#include "crypto_struct.h"
3905
3906/* The file "crypto_extra.h" contains vendor-specific definitions. This
3907 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003908#include "crypto_extra.h"
3909
3910#endif /* PSA_CRYPTO_H */