blob: f1f5bd896a85a8b161c7e747ad0c33577b748524 [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
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
Andrew Thoelke02b372b2019-10-02 09:32:21 +010060/** \defgroup version API version
Adrian L. Shawd89338a2019-09-19 13:32:57 +010061 * @{
62 */
63
64/**
65 * The major version of this implementation of the PSA Crypto API
66 */
67#define PSA_CRYPTO_API_VERSION_MAJOR 1
68
69/**
70 * The minor version of this implementation of the PSA Crypto API
71 */
72#define PSA_CRYPTO_API_VERSION_MINOR 0
73
74/**@}*/
75
Gilles Peskinef3b731e2018-12-12 13:38:31 +010076/* The file "crypto_values.h" declares macros to build and analyze values
77 * of integral types defined in "crypto_types.h". */
78#include "crypto_values.h"
79
80/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010081 * @{
82 */
83
84/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010085 * \brief Library initialization.
86 *
87 * Applications must call this function before calling any other
88 * function in this module.
89 *
90 * Applications may call this function more than once. Once a call
91 * succeeds, subsequent calls are guaranteed to succeed.
92 *
itayzafrir18617092018-09-16 12:22:41 +030093 * If the application calls other functions before calling psa_crypto_init(),
94 * the behavior is undefined. Implementations are encouraged to either perform
95 * the operation as if the library had been initialized or to return
96 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
97 * implementations should not return a success status if the lack of
98 * initialization may have security implications, for example due to improper
99 * seeding of the random number generator.
100 *
Gilles Peskine28538492018-07-11 17:34:00 +0200101 * \retval #PSA_SUCCESS
102 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
103 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
104 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200105 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +0200106 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +0100107 */
108psa_status_t psa_crypto_init(void);
109
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100110/**@}*/
111
Gilles Peskine105f67f2019-07-23 18:16:05 +0200112/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +0200113 * @{
114 */
115
Gilles Peskinea0c06552019-05-21 15:54:54 +0200116/** \def PSA_KEY_ATTRIBUTES_INIT
117 *
118 * This macro returns a suitable initializer for a key attribute structure
119 * of type #psa_key_attributes_t.
120 */
121#ifdef __DOXYGEN_ONLY__
122/* This is an example definition for documentation purposes.
123 * Implementations should define a suitable value in `crypto_struct.h`.
124 */
125#define PSA_KEY_ATTRIBUTES_INIT {0}
126#endif
127
128/** Return an initial value for a key attributes structure.
129 */
130static psa_key_attributes_t psa_key_attributes_init(void);
131
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200132/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200133 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200134 * If the attribute structure currently declares the key as volatile (which
135 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200136 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200137 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200138 * This function does not access storage, it merely stores the given
139 * value in the structure.
140 * The persistent key will be written to storage when the attribute
141 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200142 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200143 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200144 *
Gilles Peskine20628592019-04-19 19:29:50 +0200145 * This function may be declared as `static` (i.e. without external
146 * linkage). This function may be provided as a function-like macro,
147 * but in this case it must evaluate each of its arguments exactly once.
148 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200149 * \param[out] attributes The attribute structure to write to.
150 * \param key The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200151 */
Ronald Cron71016a92020-08-28 19:01:50 +0200152static void psa_set_key_id( psa_key_attributes_t *attributes,
153 mbedtls_svc_key_id_t key );
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200154
Ronald Cron6b5ff532020-10-16 14:38:19 +0200155#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
156/** Set the owner identifier of a key.
157 *
158 * When key identifiers encode key owner identifiers, psa_set_key_id() does
159 * not allow to define in key attributes the owner of volatile keys as
160 * psa_set_key_id() enforces the key to be persistent.
161 *
162 * This function allows to set in key attributes the owner identifier of a
163 * key. It is intended to be used for volatile keys. For persistent keys,
164 * it is recommended to use the PSA Cryptography API psa_set_key_id() to define
165 * the owner of a key.
166 *
167 * \param[out] attributes The attribute structure to write to.
168 * \param owner_id The key owner identifier.
169 */
170static void mbedtls_set_key_owner_id( psa_key_attributes_t *attributes,
171 mbedtls_key_owner_id_t owner_id );
172#endif
173
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200174/** Set the location of a persistent key.
175 *
176 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200177 * with psa_set_key_id(). By default, a key that has a persistent identifier
178 * is stored in the default storage area identifier by
179 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
180 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200181 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200182 * This function does not access storage, it merely stores the given
183 * value in the structure.
184 * The persistent key will be written to storage when the attribute
185 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200186 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200187 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200188 *
189 * This function may be declared as `static` (i.e. without external
190 * linkage). This function may be provided as a function-like macro,
191 * but in this case it must evaluate each of its arguments exactly once.
192 *
193 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200194 * \param lifetime The lifetime for the key.
195 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200196 * key will be volatile, and the key identifier
197 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200198 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200199static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
200 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200201
Gilles Peskine20628592019-04-19 19:29:50 +0200202/** Retrieve the key identifier from key attributes.
203 *
204 * This function may be declared as `static` (i.e. without external
205 * linkage). This function may be provided as a function-like macro,
206 * but in this case it must evaluate its argument exactly once.
207 *
208 * \param[in] attributes The key attribute structure to query.
209 *
210 * \return The persistent identifier stored in the attribute structure.
211 * This value is unspecified if the attribute structure declares
212 * the key as volatile.
213 */
Ronald Cron71016a92020-08-28 19:01:50 +0200214static mbedtls_svc_key_id_t psa_get_key_id(
215 const psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200216
Gilles Peskine20628592019-04-19 19:29:50 +0200217/** Retrieve the lifetime from key attributes.
218 *
219 * This function may be declared as `static` (i.e. without external
220 * linkage). This function may be provided as a function-like macro,
221 * but in this case it must evaluate its argument exactly once.
222 *
223 * \param[in] attributes The key attribute structure to query.
224 *
225 * \return The lifetime value stored in the attribute structure.
226 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200227static psa_key_lifetime_t psa_get_key_lifetime(
228 const psa_key_attributes_t *attributes);
229
Gilles Peskine20628592019-04-19 19:29:50 +0200230/** Declare usage flags for a key.
231 *
232 * Usage flags are part of a key's usage policy. They encode what
233 * kind of operations are permitted on the key. For more details,
234 * refer to the documentation of the type #psa_key_usage_t.
235 *
236 * This function overwrites any usage flags
237 * previously set in \p attributes.
238 *
239 * This function may be declared as `static` (i.e. without external
240 * linkage). This function may be provided as a function-like macro,
241 * but in this case it must evaluate each of its arguments exactly once.
242 *
243 * \param[out] attributes The attribute structure to write to.
244 * \param usage_flags The usage flags to write.
245 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200246static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
247 psa_key_usage_t usage_flags);
248
Gilles Peskine20628592019-04-19 19:29:50 +0200249/** Retrieve the usage flags from key attributes.
250 *
251 * This function may be declared as `static` (i.e. without external
252 * linkage). This function may be provided as a function-like macro,
253 * but in this case it must evaluate its argument exactly once.
254 *
255 * \param[in] attributes The key attribute structure to query.
256 *
257 * \return The usage flags stored in the attribute structure.
258 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200259static psa_key_usage_t psa_get_key_usage_flags(
260 const psa_key_attributes_t *attributes);
261
Gilles Peskine20628592019-04-19 19:29:50 +0200262/** Declare the permitted algorithm policy for a key.
263 *
264 * The permitted algorithm policy of a key encodes which algorithm or
Gilles Peskinea170d922019-09-12 16:59:37 +0200265 * algorithms are permitted to be used with this key. The following
266 * algorithm policies are supported:
267 * - 0 does not allow any cryptographic operation with the key. The key
268 * may be used for non-cryptographic actions such as exporting (if
269 * permitted by the usage flags).
270 * - An algorithm value permits this particular algorithm.
271 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
272 * signature scheme with any hash algorithm.
Gilles Peskine20628592019-04-19 19:29:50 +0200273 *
274 * This function overwrites any algorithm policy
275 * previously set in \p attributes.
276 *
277 * This function may be declared as `static` (i.e. without external
278 * linkage). This function may be provided as a function-like macro,
279 * but in this case it must evaluate each of its arguments exactly once.
280 *
281 * \param[out] attributes The attribute structure to write to.
282 * \param alg The permitted algorithm policy to write.
283 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200284static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
285 psa_algorithm_t alg);
286
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100287
Gilles Peskine20628592019-04-19 19:29:50 +0200288/** Retrieve the algorithm policy from key attributes.
289 *
290 * This function may be declared as `static` (i.e. without external
291 * linkage). This function may be provided as a function-like macro,
292 * but in this case it must evaluate its argument exactly once.
293 *
294 * \param[in] attributes The key attribute structure to query.
295 *
296 * \return The algorithm stored in the attribute structure.
297 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200298static psa_algorithm_t psa_get_key_algorithm(
299 const psa_key_attributes_t *attributes);
300
Gilles Peskine20628592019-04-19 19:29:50 +0200301/** Declare the type of a key.
302 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200303 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200304 * previously set in \p attributes.
305 *
306 * This function may be declared as `static` (i.e. without external
307 * linkage). This function may be provided as a function-like macro,
308 * but in this case it must evaluate each of its arguments exactly once.
309 *
310 * \param[out] attributes The attribute structure to write to.
311 * \param type The key type to write.
Gilles Peskinea170d922019-09-12 16:59:37 +0200312 * If this is 0, the key type in \p attributes
313 * becomes unspecified.
Gilles Peskine20628592019-04-19 19:29:50 +0200314 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200315static void psa_set_key_type(psa_key_attributes_t *attributes,
316 psa_key_type_t type);
317
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100318
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200319/** Declare the size of a key.
320 *
321 * This function overwrites any key size previously set in \p attributes.
322 *
323 * This function may be declared as `static` (i.e. without external
324 * linkage). This function may be provided as a function-like macro,
325 * but in this case it must evaluate each of its arguments exactly once.
326 *
327 * \param[out] attributes The attribute structure to write to.
328 * \param bits The key size in bits.
Gilles Peskinea170d922019-09-12 16:59:37 +0200329 * If this is 0, the key size in \p attributes
Gilles Peskine05c900b2019-09-12 18:29:43 +0200330 * becomes unspecified. Keys of size 0 are
331 * not supported.
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200332 */
333static void psa_set_key_bits(psa_key_attributes_t *attributes,
334 size_t bits);
335
Gilles Peskine20628592019-04-19 19:29:50 +0200336/** Retrieve the key type from key attributes.
337 *
338 * This function may be declared as `static` (i.e. without external
339 * linkage). This function may be provided as a function-like macro,
340 * but in this case it must evaluate its argument exactly once.
341 *
342 * \param[in] attributes The key attribute structure to query.
343 *
344 * \return The key type stored in the attribute structure.
345 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200346static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
347
Gilles Peskine20628592019-04-19 19:29:50 +0200348/** Retrieve the key size from key attributes.
349 *
350 * This function may be declared as `static` (i.e. without external
351 * linkage). This function may be provided as a function-like macro,
352 * but in this case it must evaluate its argument exactly once.
353 *
354 * \param[in] attributes The key attribute structure to query.
355 *
356 * \return The key size stored in the attribute structure, in bits.
357 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200358static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
359
Gilles Peskine20628592019-04-19 19:29:50 +0200360/** Retrieve the attributes of a key.
361 *
362 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200363 * psa_reset_key_attributes(). It then copies the attributes of
364 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200365 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200366 * \note This function may allocate memory or other resources.
367 * Once you have called this function on an attribute structure,
368 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200369 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200370 * \param[in] key Identifier of the key to query.
Gilles Peskine20628592019-04-19 19:29:50 +0200371 * \param[in,out] attributes On success, the attributes of the key.
372 * On failure, equivalent to a
373 * freshly-initialized structure.
374 *
375 * \retval #PSA_SUCCESS
376 * \retval #PSA_ERROR_INVALID_HANDLE
377 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
378 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100379 * \retval #PSA_ERROR_CORRUPTION_DETECTED
380 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100381 * \retval #PSA_ERROR_BAD_STATE
382 * The library has not been previously initialized by psa_crypto_init().
383 * It is implementation-dependent whether a failure to initialize
384 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200385 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200386psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
Gilles Peskine4747d192019-04-17 15:05:45 +0200387 psa_key_attributes_t *attributes);
388
Gilles Peskine20628592019-04-19 19:29:50 +0200389/** Reset a key attribute structure to a freshly initialized state.
390 *
391 * You must initialize the attribute structure as described in the
392 * documentation of the type #psa_key_attributes_t before calling this
393 * function. Once the structure has been initialized, you may call this
394 * function at any time.
395 *
396 * This function frees any auxiliary resources that the structure
397 * may contain.
398 *
399 * \param[in,out] attributes The attribute structure to reset.
400 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200401void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200402
Gilles Peskine87a5e562019-04-17 12:28:25 +0200403/**@}*/
404
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100405/** \defgroup key_management Key management
406 * @{
407 */
408
Ronald Cron277a85f2020-08-04 15:49:48 +0200409/** Remove non-essential copies of key material from memory.
410 *
411 * If the key identifier designates a volatile key, this functions does not do
412 * anything and returns successfully.
413 *
414 * If the key identifier designates a persistent key, then this function will
415 * free all resources associated with the key in volatile memory. The key
416 * data in persistent storage is not affected and the key can still be used.
417 *
418 * \param key Identifier of the key to purge.
419 *
420 * \retval #PSA_SUCCESS
421 * The key material will have been removed from memory if it is not
422 * currently required.
423 * \retval #PSA_ERROR_INVALID_ARGUMENT
424 * \p key is not a valid key identifier.
425 * \retval #PSA_ERROR_BAD_STATE
426 * The library has not been previously initialized by psa_crypto_init().
427 * It is implementation-dependent whether a failure to initialize
428 * results in this error code.
429 */
430psa_status_t psa_purge_key(mbedtls_svc_key_id_t key);
431
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100432/** Make a copy of a key.
433 *
434 * Copy key material from one location to another.
435 *
436 * This function is primarily useful to copy a key from one location
437 * to another, since it populates a key using the material from
438 * another key which may have a different lifetime.
439 *
440 * This function may be used to share a key with a different party,
441 * subject to implementation-defined restrictions on key sharing.
442 *
443 * The policy on the source key must have the usage flag
444 * #PSA_KEY_USAGE_COPY set.
445 * This flag is sufficient to permit the copy if the key has the lifetime
446 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
447 * Some secure elements do not provide a way to copy a key without
448 * making it extractable from the secure element. If a key is located
449 * in such a secure element, then the key must have both usage flags
450 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
451 * a copy of the key outside the secure element.
452 *
453 * The resulting key may only be used in a way that conforms to
454 * both the policy of the original key and the policy specified in
455 * the \p attributes parameter:
456 * - The usage flags on the resulting key are the bitwise-and of the
457 * usage flags on the source policy and the usage flags in \p attributes.
458 * - If both allow the same algorithm or wildcard-based
459 * algorithm policy, the resulting key has the same algorithm policy.
460 * - If either of the policies allows an algorithm and the other policy
461 * allows a wildcard-based algorithm policy that includes this algorithm,
462 * the resulting key allows the same algorithm.
463 * - If the policies do not allow any algorithm in common, this function
464 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
465 *
466 * The effect of this function on implementation-defined attributes is
467 * implementation-defined.
468 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200469 * \param source_key The key to copy. It must allow the usage
470 * PSA_KEY_USAGE_COPY. If a private or secret key is
471 * being copied outside of a secure element it must
472 * also allow PSA_KEY_USAGE_EXPORT.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100473 * \param[in] attributes The attributes for the new key.
474 * They are used as follows:
475 * - The key type and size may be 0. If either is
476 * nonzero, it must match the corresponding
477 * attribute of the source key.
478 * - The key location (the lifetime and, for
479 * persistent keys, the key identifier) is
480 * used directly.
481 * - The policy constraints (usage flags and
482 * algorithm policy) are combined from
483 * the source key and \p attributes so that
484 * both sets of restrictions apply, as
485 * described in the documentation of this function.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200486 * \param[out] target_key On success, an identifier for the newly created
487 * key. \c 0 on failure.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100488 *
489 * \retval #PSA_SUCCESS
490 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200491 * \p source_key is invalid.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100492 * \retval #PSA_ERROR_ALREADY_EXISTS
493 * This is an attempt to create a persistent key, and there is
494 * already a persistent key with the given identifier.
495 * \retval #PSA_ERROR_INVALID_ARGUMENT
496 * The lifetime or identifier in \p attributes are invalid.
497 * \retval #PSA_ERROR_INVALID_ARGUMENT
498 * The policy constraints on the source and specified in
499 * \p attributes are incompatible.
500 * \retval #PSA_ERROR_INVALID_ARGUMENT
501 * \p attributes specifies a key type or key size
502 * which does not match the attributes of the source key.
503 * \retval #PSA_ERROR_NOT_PERMITTED
504 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
505 * \retval #PSA_ERROR_NOT_PERMITTED
506 * The source key is not exportable and its lifetime does not
507 * allow copying it to the target's lifetime.
508 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
509 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
510 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
511 * \retval #PSA_ERROR_HARDWARE_FAILURE
512 * \retval #PSA_ERROR_STORAGE_FAILURE
513 * \retval #PSA_ERROR_CORRUPTION_DETECTED
514 * \retval #PSA_ERROR_BAD_STATE
515 * The library has not been previously initialized by psa_crypto_init().
516 * It is implementation-dependent whether a failure to initialize
517 * results in this error code.
518 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200519psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100520 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200521 mbedtls_svc_key_id_t *target_key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100522
523
524/**
525 * \brief Destroy a key.
526 *
527 * This function destroys a key from both volatile
528 * memory and, if applicable, non-volatile storage. Implementations shall
529 * make a best effort to ensure that that the key material cannot be recovered.
530 *
531 * This function also erases any metadata such as policies and frees
Ronald Croncf56a0a2020-08-04 09:51:30 +0200532 * resources associated with the key.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100533 *
534 * If a key is currently in use in a multipart operation, then destroying the
535 * key will cause the multipart operation to fail.
536 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200537 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
538 * return PSA_SUCCESS.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100539 *
540 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +0200541 * \p key was a valid identifier and the key material that it
542 * referred to has been erased. Alternatively, \p key is \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100543 * \retval #PSA_ERROR_NOT_PERMITTED
544 * The key cannot be erased because it is
545 * read-only, either due to a policy or due to physical restrictions.
546 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200547 * \p key is not a valid identifier nor \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100548 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
549 * There was an failure in communication with the cryptoprocessor.
550 * The key material may still be present in the cryptoprocessor.
551 * \retval #PSA_ERROR_STORAGE_FAILURE
552 * The storage is corrupted. Implementations shall make a best effort
553 * to erase key material even in this stage, however applications
554 * should be aware that it may be impossible to guarantee that the
555 * key material is not recoverable in such cases.
556 * \retval #PSA_ERROR_CORRUPTION_DETECTED
557 * An unexpected condition which is not a storage corruption or
558 * a communication failure occurred. The cryptoprocessor may have
559 * been compromised.
560 * \retval #PSA_ERROR_BAD_STATE
561 * The library has not been previously initialized by psa_crypto_init().
562 * It is implementation-dependent whether a failure to initialize
563 * results in this error code.
564 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200565psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100566
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100567/**@}*/
568
569/** \defgroup import_export Key import and export
570 * @{
571 */
572
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573/**
574 * \brief Import a key in binary format.
575 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100576 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100577 * documentation of psa_export_public_key() for the format of public keys
578 * and to the documentation of psa_export_key() for the format for
579 * other key types.
580 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200581 * The key data determines the key size. The attributes may optionally
582 * specify a key size; in this case it must match the size determined
583 * from the key data. A key size of 0 in \p attributes indicates that
584 * the key size is solely determined by the key data.
585 *
586 * Implementations must reject an attempt to import a key of size 0.
587 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100588 * This specification supports a single format for each key type.
589 * Implementations may support other formats as long as the standard
590 * format is supported. Implementations that support other formats
591 * should ensure that the formats are clearly unambiguous so as to
592 * minimize the risk that an invalid input is accidentally interpreted
593 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100594 *
Gilles Peskine20628592019-04-19 19:29:50 +0200595 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200596 * The key size is always determined from the
597 * \p data buffer.
598 * If the key size in \p attributes is nonzero,
599 * it must be equal to the size from \p data.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200600 * \param[out] key On success, an identifier to the newly created key.
Gilles Peskine20628592019-04-19 19:29:50 +0200601 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100602 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200603 * buffer is interpreted according to the type declared
604 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200605 * All implementations must support at least the format
606 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100607 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200608 * the chosen type. Implementations may allow other
609 * formats, but should be conservative: implementations
610 * should err on the side of rejecting content if it
611 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200612 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100613 *
Gilles Peskine28538492018-07-11 17:34:00 +0200614 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100615 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100616 * If the key is persistent, the key material and the key's metadata
617 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200618 * \retval #PSA_ERROR_ALREADY_EXISTS
619 * This is an attempt to create a persistent key, and there is
620 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200621 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200622 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200623 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200624 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200625 * The key attributes, as a whole, are invalid.
626 * \retval #PSA_ERROR_INVALID_ARGUMENT
627 * The key data is not correctly formatted.
628 * \retval #PSA_ERROR_INVALID_ARGUMENT
629 * The size in \p attributes is nonzero and does not match the size
630 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200631 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
632 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
633 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100634 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200635 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200636 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300637 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300638 * The library has not been previously initialized by psa_crypto_init().
639 * It is implementation-dependent whether a failure to initialize
640 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100641 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200642psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100643 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200644 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200645 mbedtls_svc_key_id_t *key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100646
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100647
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100648
649/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100650 * \brief Export a key in binary format.
651 *
652 * The output of this function can be passed to psa_import_key() to
653 * create an equivalent object.
654 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100655 * If the implementation of psa_import_key() supports other formats
656 * beyond the format specified here, the output from psa_export_key()
657 * must use the representation specified here, not the original
658 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100659 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100660 * For standard key types, the output format is as follows:
661 *
662 * - For symmetric keys (including MAC keys), the format is the
663 * raw bytes of the key.
664 * - For DES, the key data consists of 8 bytes. The parity bits must be
665 * correct.
666 * - For Triple-DES, the format is the concatenation of the
667 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200668 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200669 * is the non-encrypted DER encoding of the representation defined by
670 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
671 * ```
672 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200673 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200674 * modulus INTEGER, -- n
675 * publicExponent INTEGER, -- e
676 * privateExponent INTEGER, -- d
677 * prime1 INTEGER, -- p
678 * prime2 INTEGER, -- q
679 * exponent1 INTEGER, -- d mod (p-1)
680 * exponent2 INTEGER, -- d mod (q-1)
681 * coefficient INTEGER, -- (inverse of q) mod p
682 * }
683 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200684 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200685 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100686 * a representation of the private value as a `ceiling(m/8)`-byte string
687 * where `m` is the bit size associated with the curve, i.e. the bit size
688 * of the order of the curve's coordinate field. This byte string is
689 * in little-endian order for Montgomery curves (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +0100690 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
691 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
692 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
Steven Cooreman6f5cc712020-06-11 16:40:41 +0200693 * For Weierstrass curves, this is the content of the `privateKey` field of
694 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
695 * the format is defined by RFC 7748, and output is masked according to §5.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200696 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200697 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000698 * format is the representation of the private key `x` as a big-endian byte
699 * string. The length of the byte string is the private key size in bytes
700 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200701 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
702 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100703 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200704 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
705 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200706 * \param key Identifier of the key to export. It must allow the
707 * usage PSA_KEY_USAGE_EXPORT, unless it is a public
708 * key.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200709 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200710 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200711 * \param[out] data_length On success, the number of bytes
712 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100713 *
Gilles Peskine28538492018-07-11 17:34:00 +0200714 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100715 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200716 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200717 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100718 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200719 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
720 * The size of the \p data buffer is too small. You can determine a
721 * sufficient buffer size by calling
722 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
723 * where \c type is the key type
724 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200725 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
726 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200727 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100728 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100729 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300730 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300731 * The library has not been previously initialized by psa_crypto_init().
732 * It is implementation-dependent whether a failure to initialize
733 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100734 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200735psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100736 uint8_t *data,
737 size_t data_size,
738 size_t *data_length);
739
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100740/**
741 * \brief Export a public key or the public part of a key pair in binary format.
742 *
743 * The output of this function can be passed to psa_import_key() to
744 * create an object that is equivalent to the public key.
745 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000746 * This specification supports a single format for each key type.
747 * Implementations may support other formats as long as the standard
748 * format is supported. Implementations that support other formats
749 * should ensure that the formats are clearly unambiguous so as to
750 * minimize the risk that an invalid input is accidentally interpreted
751 * according to a different format.
752 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000753 * For standard key types, the output format is as follows:
754 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
755 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
756 * ```
757 * RSAPublicKey ::= SEQUENCE {
758 * modulus INTEGER, -- n
759 * publicExponent INTEGER } -- e
760 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000761 * - For elliptic curve public keys (key types for which
762 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
763 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
764 * Let `m` be the bit size associated with the curve, i.e. the bit size of
765 * `q` for a curve over `F_q`. The representation consists of:
766 * - The byte 0x04;
767 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
768 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200769 * - For Diffie-Hellman key exchange public keys (key types for which
770 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000771 * the format is the representation of the public key `y = g^x mod p` as a
772 * big-endian byte string. The length of the byte string is the length of the
773 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100774 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200775 * Exporting a public key object or the public part of a key pair is
776 * always permitted, regardless of the key's usage flags.
777 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200778 * \param key Identifier of the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200779 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200780 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200781 * \param[out] data_length On success, the number of bytes
782 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100783 *
Gilles Peskine28538492018-07-11 17:34:00 +0200784 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100785 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200786 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200787 * The key is neither a public key nor a key pair.
788 * \retval #PSA_ERROR_NOT_SUPPORTED
789 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
790 * The size of the \p data buffer is too small. You can determine a
791 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200792 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200793 * where \c type is the key type
794 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200795 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
796 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200797 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100798 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100799 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300800 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300801 * The library has not been previously initialized by psa_crypto_init().
802 * It is implementation-dependent whether a failure to initialize
803 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100804 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200805psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100806 uint8_t *data,
807 size_t data_size,
808 size_t *data_length);
809
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100810
Gilles Peskine20035e32018-02-03 22:44:14 +0100811
812/**@}*/
813
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100814/** \defgroup hash Message digests
815 * @{
816 */
817
Gilles Peskine69647a42019-01-14 20:18:12 +0100818/** Calculate the hash (digest) of a message.
819 *
820 * \note To verify the hash of a message against an
821 * expected value, use psa_hash_compare() instead.
822 *
823 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
824 * such that #PSA_ALG_IS_HASH(\p alg) is true).
825 * \param[in] input Buffer containing the message to hash.
826 * \param input_length Size of the \p input buffer in bytes.
827 * \param[out] hash Buffer where the hash is to be written.
828 * \param hash_size Size of the \p hash buffer in bytes.
829 * \param[out] hash_length On success, the number of bytes
830 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100831 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100832 *
833 * \retval #PSA_SUCCESS
834 * Success.
835 * \retval #PSA_ERROR_NOT_SUPPORTED
836 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100837 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100838 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
839 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100840 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
841 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
842 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200843 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100844 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100845 * \retval #PSA_ERROR_BAD_STATE
846 * The library has not been previously initialized by psa_crypto_init().
847 * It is implementation-dependent whether a failure to initialize
848 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100849 */
850psa_status_t psa_hash_compute(psa_algorithm_t alg,
851 const uint8_t *input,
852 size_t input_length,
853 uint8_t *hash,
854 size_t hash_size,
855 size_t *hash_length);
856
857/** Calculate the hash (digest) of a message and compare it with a
858 * reference value.
859 *
860 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
861 * such that #PSA_ALG_IS_HASH(\p alg) is true).
862 * \param[in] input Buffer containing the message to hash.
863 * \param input_length Size of the \p input buffer in bytes.
864 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100865 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100866 *
867 * \retval #PSA_SUCCESS
868 * The expected hash is identical to the actual hash of the input.
869 * \retval #PSA_ERROR_INVALID_SIGNATURE
870 * The hash of the message was calculated successfully, but it
871 * differs from the expected hash.
872 * \retval #PSA_ERROR_NOT_SUPPORTED
873 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100874 * \retval #PSA_ERROR_INVALID_ARGUMENT
875 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100876 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
877 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
878 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200879 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100880 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100881 * \retval #PSA_ERROR_BAD_STATE
882 * The library has not been previously initialized by psa_crypto_init().
883 * It is implementation-dependent whether a failure to initialize
884 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100885 */
886psa_status_t psa_hash_compare(psa_algorithm_t alg,
887 const uint8_t *input,
888 size_t input_length,
889 const uint8_t *hash,
Gilles Peskinefa710f52019-12-02 14:31:48 +0100890 size_t hash_length);
Gilles Peskine69647a42019-01-14 20:18:12 +0100891
Gilles Peskine308b91d2018-02-08 09:47:44 +0100892/** The type of the state data structure for multipart hash operations.
893 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000894 * Before calling any function on a hash operation object, the application must
895 * initialize it by any of the following means:
896 * - Set the structure to all-bits-zero, for example:
897 * \code
898 * psa_hash_operation_t operation;
899 * memset(&operation, 0, sizeof(operation));
900 * \endcode
901 * - Initialize the structure to logical zero values, for example:
902 * \code
903 * psa_hash_operation_t operation = {0};
904 * \endcode
905 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
906 * for example:
907 * \code
908 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
909 * \endcode
910 * - Assign the result of the function psa_hash_operation_init()
911 * to the structure, for example:
912 * \code
913 * psa_hash_operation_t operation;
914 * operation = psa_hash_operation_init();
915 * \endcode
916 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100917 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100918 * make any assumptions about the content of this structure except
919 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100920typedef struct psa_hash_operation_s psa_hash_operation_t;
921
Jaeden Amero6a25b412019-01-04 11:47:44 +0000922/** \def PSA_HASH_OPERATION_INIT
923 *
924 * This macro returns a suitable initializer for a hash operation object
925 * of type #psa_hash_operation_t.
926 */
927#ifdef __DOXYGEN_ONLY__
928/* This is an example definition for documentation purposes.
929 * Implementations should define a suitable value in `crypto_struct.h`.
930 */
931#define PSA_HASH_OPERATION_INIT {0}
932#endif
933
934/** Return an initial value for a hash operation object.
935 */
936static psa_hash_operation_t psa_hash_operation_init(void);
937
Gilles Peskinef45adda2019-01-14 18:29:18 +0100938/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100939 *
940 * The sequence of operations to calculate a hash (message digest)
941 * is as follows:
942 * -# Allocate an operation object which will be passed to all the functions
943 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000944 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100945 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200946 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100947 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100948 * of the message each time. The hash that is calculated is the hash
949 * of the concatenation of these messages in order.
950 * -# To calculate the hash, call psa_hash_finish().
951 * To compare the hash with an expected value, call psa_hash_verify().
952 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100953 * If an error occurs at any step after a call to psa_hash_setup(), the
954 * operation will need to be reset by a call to psa_hash_abort(). The
955 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000956 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100957 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200958 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100959 * eventually terminate the operation. The following events terminate an
960 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100961 * - A successful call to psa_hash_finish() or psa_hash_verify().
962 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100963 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000964 * \param[in,out] operation The operation object to set up. It must have
965 * been initialized as per the documentation for
966 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200967 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
968 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100969 *
Gilles Peskine28538492018-07-11 17:34:00 +0200970 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100971 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200972 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100973 * \p alg is not a supported hash algorithm.
974 * \retval #PSA_ERROR_INVALID_ARGUMENT
975 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100976 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100977 * The operation state is not valid (it must be inactive).
Gilles Peskine28538492018-07-11 17:34:00 +0200978 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
979 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
980 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200981 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100982 * \retval #PSA_ERROR_BAD_STATE
983 * The library has not been previously initialized by psa_crypto_init().
984 * It is implementation-dependent whether a failure to initialize
985 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100986 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200987psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100988 psa_algorithm_t alg);
989
Gilles Peskine308b91d2018-02-08 09:47:44 +0100990/** Add a message fragment to a multipart hash operation.
991 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200992 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100993 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100994 * If this function returns an error status, the operation enters an error
995 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100996 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200997 * \param[in,out] operation Active hash operation.
998 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200999 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001000 *
Gilles Peskine28538492018-07-11 17:34:00 +02001001 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001002 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001003 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001004 * The operation state is not valid (it muct be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001005 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1006 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1007 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001008 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001009 * \retval #PSA_ERROR_BAD_STATE
1010 * The library has not been previously initialized by psa_crypto_init().
1011 * It is implementation-dependent whether a failure to initialize
1012 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001013 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001014psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1015 const uint8_t *input,
1016 size_t input_length);
1017
Gilles Peskine308b91d2018-02-08 09:47:44 +01001018/** Finish the calculation of the hash of a message.
1019 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001020 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001021 * This function calculates the hash of the message formed by concatenating
1022 * the inputs passed to preceding calls to psa_hash_update().
1023 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001024 * When this function returns successfuly, the operation becomes inactive.
1025 * If this function returns an error status, the operation enters an error
1026 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001027 *
1028 * \warning Applications should not call this function if they expect
1029 * a specific value for the hash. Call psa_hash_verify() instead.
1030 * Beware that comparing integrity or authenticity data such as
1031 * hash values with a function such as \c memcmp is risky
1032 * because the time taken by the comparison may leak information
1033 * about the hashed data which could allow an attacker to guess
1034 * a valid hash and thereby bypass security controls.
1035 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001036 * \param[in,out] operation Active hash operation.
1037 * \param[out] hash Buffer where the hash is to be written.
1038 * \param hash_size Size of the \p hash buffer in bytes.
1039 * \param[out] hash_length On success, the number of bytes
1040 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001041 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001042 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001043 *
Gilles Peskine28538492018-07-11 17:34:00 +02001044 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001045 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001046 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001047 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001048 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001049 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001050 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001051 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001052 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1053 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1054 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001055 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001056 * \retval #PSA_ERROR_BAD_STATE
1057 * The library has not been previously initialized by psa_crypto_init().
1058 * It is implementation-dependent whether a failure to initialize
1059 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001060 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001061psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1062 uint8_t *hash,
1063 size_t hash_size,
1064 size_t *hash_length);
1065
Gilles Peskine308b91d2018-02-08 09:47:44 +01001066/** Finish the calculation of the hash of a message and compare it with
1067 * an expected value.
1068 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001069 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001070 * This function calculates the hash of the message formed by concatenating
1071 * the inputs passed to preceding calls to psa_hash_update(). It then
1072 * compares the calculated hash with the expected hash passed as a
1073 * parameter to this function.
1074 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001075 * When this function returns successfuly, the operation becomes inactive.
1076 * If this function returns an error status, the operation enters an error
1077 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001078 *
Gilles Peskine19067982018-03-20 17:54:53 +01001079 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001080 * comparison between the actual hash and the expected hash is performed
1081 * in constant time.
1082 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001083 * \param[in,out] operation Active hash operation.
1084 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001085 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001086 *
Gilles Peskine28538492018-07-11 17:34:00 +02001087 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001088 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001089 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001090 * The hash of the message was calculated successfully, but it
1091 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001092 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001093 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001094 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1095 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1096 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001097 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001098 * \retval #PSA_ERROR_BAD_STATE
1099 * The library has not been previously initialized by psa_crypto_init().
1100 * It is implementation-dependent whether a failure to initialize
1101 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001102 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001103psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1104 const uint8_t *hash,
1105 size_t hash_length);
1106
Gilles Peskine308b91d2018-02-08 09:47:44 +01001107/** Abort a hash operation.
1108 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001109 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001110 * \p operation structure itself. Once aborted, the operation object
1111 * can be reused for another operation by calling
1112 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001113 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001114 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001115 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001116 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001117 * In particular, calling psa_hash_abort() after the operation has been
1118 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1119 * psa_hash_verify() is safe and has no effect.
1120 *
1121 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001122 *
Gilles Peskine28538492018-07-11 17:34:00 +02001123 * \retval #PSA_SUCCESS
Gilles Peskine28538492018-07-11 17:34:00 +02001124 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1125 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001126 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001127 * \retval #PSA_ERROR_BAD_STATE
1128 * The library has not been previously initialized by psa_crypto_init().
1129 * It is implementation-dependent whether a failure to initialize
1130 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001131 */
1132psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001133
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001134/** Clone a hash operation.
1135 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001136 * This function copies the state of an ongoing hash operation to
1137 * a new operation object. In other words, this function is equivalent
1138 * to calling psa_hash_setup() on \p target_operation with the same
1139 * algorithm that \p source_operation was set up for, then
1140 * psa_hash_update() on \p target_operation with the same input that
1141 * that was passed to \p source_operation. After this function returns, the
1142 * two objects are independent, i.e. subsequent calls involving one of
1143 * the objects do not affect the other object.
1144 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001145 * \param[in] source_operation The active hash operation to clone.
1146 * \param[in,out] target_operation The operation object to set up.
1147 * It must be initialized but not active.
1148 *
1149 * \retval #PSA_SUCCESS
1150 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001151 * The \p source_operation state is not valid (it must be active).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001152 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001153 * The \p target_operation state is not valid (it must be inactive).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001154 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1155 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001156 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001157 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001158 * \retval #PSA_ERROR_BAD_STATE
1159 * The library has not been previously initialized by psa_crypto_init().
1160 * It is implementation-dependent whether a failure to initialize
1161 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001162 */
1163psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1164 psa_hash_operation_t *target_operation);
1165
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001166/**@}*/
1167
Gilles Peskine8c9def32018-02-08 10:02:12 +01001168/** \defgroup MAC Message authentication codes
1169 * @{
1170 */
1171
Gilles Peskine69647a42019-01-14 20:18:12 +01001172/** Calculate the MAC (message authentication code) of a message.
1173 *
1174 * \note To verify the MAC of a message against an
1175 * expected value, use psa_mac_verify() instead.
1176 * Beware that comparing integrity or authenticity data such as
1177 * MAC values with a function such as \c memcmp is risky
1178 * because the time taken by the comparison may leak information
1179 * about the MAC value which could allow an attacker to guess
1180 * a valid MAC and thereby bypass security controls.
1181 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001182 * \param key Identifier of the key to use for the operation. It
1183 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001184 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001185 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001186 * \param[in] input Buffer containing the input message.
1187 * \param input_length Size of the \p input buffer in bytes.
1188 * \param[out] mac Buffer where the MAC value is to be written.
1189 * \param mac_size Size of the \p mac buffer in bytes.
1190 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001191 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001192 *
1193 * \retval #PSA_SUCCESS
1194 * Success.
1195 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001196 * \retval #PSA_ERROR_NOT_PERMITTED
1197 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001198 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001199 * \retval #PSA_ERROR_NOT_SUPPORTED
1200 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001201 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1202 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001203 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1204 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1205 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001206 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001207 * \retval #PSA_ERROR_STORAGE_FAILURE
1208 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001209 * \retval #PSA_ERROR_BAD_STATE
1210 * The library has not been previously initialized by psa_crypto_init().
1211 * It is implementation-dependent whether a failure to initialize
1212 * results in this error code.
1213 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001214psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001215 psa_algorithm_t alg,
1216 const uint8_t *input,
1217 size_t input_length,
1218 uint8_t *mac,
1219 size_t mac_size,
1220 size_t *mac_length);
1221
1222/** Calculate the MAC of a message and compare it with a reference value.
1223 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001224 * \param key Identifier of the key to use for the operation. It
1225 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001226 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001227 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001228 * \param[in] input Buffer containing the input message.
1229 * \param input_length Size of the \p input buffer in bytes.
1230 * \param[out] mac Buffer containing the expected MAC value.
1231 * \param mac_length Size of the \p mac buffer in bytes.
1232 *
1233 * \retval #PSA_SUCCESS
1234 * The expected MAC is identical to the actual MAC of the input.
1235 * \retval #PSA_ERROR_INVALID_SIGNATURE
1236 * The MAC of the message was calculated successfully, but it
1237 * differs from the expected value.
1238 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001239 * \retval #PSA_ERROR_NOT_PERMITTED
1240 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001241 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001242 * \retval #PSA_ERROR_NOT_SUPPORTED
1243 * \p alg is not supported or is not a MAC algorithm.
1244 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1245 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1246 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001247 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001248 * \retval #PSA_ERROR_STORAGE_FAILURE
1249 * The key could not be retrieved from storage.
1250 * \retval #PSA_ERROR_BAD_STATE
1251 * The library has not been previously initialized by psa_crypto_init().
1252 * It is implementation-dependent whether a failure to initialize
1253 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001254 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001255psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
Gilles Peskinea05602d2019-01-17 15:25:52 +01001256 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001257 const uint8_t *input,
1258 size_t input_length,
1259 const uint8_t *mac,
Gilles Peskine13faa2d2020-01-30 16:32:21 +01001260 size_t mac_length);
Gilles Peskine69647a42019-01-14 20:18:12 +01001261
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001262/** The type of the state data structure for multipart MAC operations.
1263 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001264 * Before calling any function on a MAC operation object, the application must
1265 * initialize it by any of the following means:
1266 * - Set the structure to all-bits-zero, for example:
1267 * \code
1268 * psa_mac_operation_t operation;
1269 * memset(&operation, 0, sizeof(operation));
1270 * \endcode
1271 * - Initialize the structure to logical zero values, for example:
1272 * \code
1273 * psa_mac_operation_t operation = {0};
1274 * \endcode
1275 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1276 * for example:
1277 * \code
1278 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1279 * \endcode
1280 * - Assign the result of the function psa_mac_operation_init()
1281 * to the structure, for example:
1282 * \code
1283 * psa_mac_operation_t operation;
1284 * operation = psa_mac_operation_init();
1285 * \endcode
1286 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001287 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001288 * make any assumptions about the content of this structure except
1289 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001290typedef struct psa_mac_operation_s psa_mac_operation_t;
1291
Jaeden Amero769ce272019-01-04 11:48:03 +00001292/** \def PSA_MAC_OPERATION_INIT
1293 *
1294 * This macro returns a suitable initializer for a MAC operation object of type
1295 * #psa_mac_operation_t.
1296 */
1297#ifdef __DOXYGEN_ONLY__
1298/* This is an example definition for documentation purposes.
1299 * Implementations should define a suitable value in `crypto_struct.h`.
1300 */
1301#define PSA_MAC_OPERATION_INIT {0}
1302#endif
1303
1304/** Return an initial value for a MAC operation object.
1305 */
1306static psa_mac_operation_t psa_mac_operation_init(void);
1307
Gilles Peskinef45adda2019-01-14 18:29:18 +01001308/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001309 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001310 * This function sets up the calculation of the MAC
1311 * (message authentication code) of a byte string.
1312 * To verify the MAC of a message against an
1313 * expected value, use psa_mac_verify_setup() instead.
1314 *
1315 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001316 * -# Allocate an operation object which will be passed to all the functions
1317 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001318 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001319 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001320 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001321 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1322 * of the message each time. The MAC that is calculated is the MAC
1323 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001324 * -# At the end of the message, call psa_mac_sign_finish() to finish
1325 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001326 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001327 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1328 * operation will need to be reset by a call to psa_mac_abort(). The
1329 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001330 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001331 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001332 * After a successful call to psa_mac_sign_setup(), the application must
1333 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001334 * - A successful call to psa_mac_sign_finish().
1335 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001336 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001337 * \param[in,out] operation The operation object to set up. It must have
1338 * been initialized as per the documentation for
1339 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001340 * \param key Identifier of the key to use for the operation. It
1341 * must remain valid until the operation terminates.
1342 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001343 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001344 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001345 *
Gilles Peskine28538492018-07-11 17:34:00 +02001346 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001347 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001348 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001349 * \retval #PSA_ERROR_NOT_PERMITTED
1350 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001351 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001352 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001353 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001354 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1355 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1356 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001357 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001358 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001359 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001360 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001361 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001362 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001363 * The library has not been previously initialized by psa_crypto_init().
1364 * It is implementation-dependent whether a failure to initialize
1365 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001366 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001367psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001368 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001369 psa_algorithm_t alg);
1370
Gilles Peskinef45adda2019-01-14 18:29:18 +01001371/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001372 *
1373 * This function sets up the verification of the MAC
1374 * (message authentication code) of a byte string against an expected value.
1375 *
1376 * The sequence of operations to verify a MAC is as follows:
1377 * -# Allocate an operation object which will be passed to all the functions
1378 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001379 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001380 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001381 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001382 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1383 * of the message each time. The MAC that is calculated is the MAC
1384 * of the concatenation of these messages in order.
1385 * -# At the end of the message, call psa_mac_verify_finish() to finish
1386 * calculating the actual MAC of the message and verify it against
1387 * the expected value.
1388 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001389 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1390 * operation will need to be reset by a call to psa_mac_abort(). The
1391 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001392 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001393 *
1394 * After a successful call to psa_mac_verify_setup(), the application must
1395 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001396 * - A successful call to psa_mac_verify_finish().
1397 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001398 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001399 * \param[in,out] operation The operation object to set up. It must have
1400 * been initialized as per the documentation for
1401 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001402 * \param key Identifier of the key to use for the operation. It
1403 * must remain valid until the operation terminates.
1404 * It must allow the usage
1405 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001406 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1407 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001408 *
Gilles Peskine28538492018-07-11 17:34:00 +02001409 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001410 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001411 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001412 * \retval #PSA_ERROR_NOT_PERMITTED
1413 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001414 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001415 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001416 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001417 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1418 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1419 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001420 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001421 * \retval #PSA_ERROR_STORAGE_FAILURE
1422 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001423 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001424 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001425 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001426 * The library has not been previously initialized by psa_crypto_init().
1427 * It is implementation-dependent whether a failure to initialize
1428 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001429 */
1430psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001431 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001432 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001433
Gilles Peskinedcd14942018-07-12 00:30:52 +02001434/** Add a message fragment to a multipart MAC operation.
1435 *
1436 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1437 * before calling this function.
1438 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001439 * If this function returns an error status, the operation enters an error
1440 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001441 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001442 * \param[in,out] operation Active MAC operation.
1443 * \param[in] input Buffer containing the message fragment to add to
1444 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001445 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001446 *
1447 * \retval #PSA_SUCCESS
1448 * Success.
1449 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001450 * The operation state is not valid (it must be active).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001451 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1452 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1453 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001454 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001455 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001456 * \retval #PSA_ERROR_BAD_STATE
1457 * The library has not been previously initialized by psa_crypto_init().
1458 * It is implementation-dependent whether a failure to initialize
1459 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001460 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001461psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1462 const uint8_t *input,
1463 size_t input_length);
1464
Gilles Peskinedcd14942018-07-12 00:30:52 +02001465/** Finish the calculation of the MAC of a message.
1466 *
1467 * The application must call psa_mac_sign_setup() before calling this function.
1468 * This function calculates the MAC of the message formed by concatenating
1469 * the inputs passed to preceding calls to psa_mac_update().
1470 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001471 * When this function returns successfuly, the operation becomes inactive.
1472 * If this function returns an error status, the operation enters an error
1473 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001474 *
1475 * \warning Applications should not call this function if they expect
1476 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1477 * Beware that comparing integrity or authenticity data such as
1478 * MAC values with a function such as \c memcmp is risky
1479 * because the time taken by the comparison may leak information
1480 * about the MAC value which could allow an attacker to guess
1481 * a valid MAC and thereby bypass security controls.
1482 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001483 * \param[in,out] operation Active MAC operation.
1484 * \param[out] mac Buffer where the MAC value is to be written.
1485 * \param mac_size Size of the \p mac buffer in bytes.
1486 * \param[out] mac_length On success, the number of bytes
1487 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001488 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001489 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001490 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001491 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001492 *
1493 * \retval #PSA_SUCCESS
1494 * Success.
1495 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001496 * The operation state is not valid (it must be an active mac sign
1497 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001498 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001499 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001500 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1501 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1502 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1503 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001504 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001505 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001506 * \retval #PSA_ERROR_BAD_STATE
1507 * The library has not been previously initialized by psa_crypto_init().
1508 * It is implementation-dependent whether a failure to initialize
1509 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001510 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001511psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1512 uint8_t *mac,
1513 size_t mac_size,
1514 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001515
Gilles Peskinedcd14942018-07-12 00:30:52 +02001516/** Finish the calculation of the MAC of a message and compare it with
1517 * an expected value.
1518 *
1519 * The application must call psa_mac_verify_setup() before calling this function.
1520 * This function calculates the MAC of the message formed by concatenating
1521 * the inputs passed to preceding calls to psa_mac_update(). It then
1522 * compares the calculated MAC with the expected MAC passed as a
1523 * parameter to this function.
1524 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001525 * When this function returns successfuly, the operation becomes inactive.
1526 * If this function returns an error status, the operation enters an error
1527 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001528 *
1529 * \note Implementations shall make the best effort to ensure that the
1530 * comparison between the actual MAC and the expected MAC is performed
1531 * in constant time.
1532 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001533 * \param[in,out] operation Active MAC operation.
1534 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001535 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001536 *
1537 * \retval #PSA_SUCCESS
1538 * The expected MAC is identical to the actual MAC of the message.
1539 * \retval #PSA_ERROR_INVALID_SIGNATURE
1540 * The MAC of the message was calculated successfully, but it
1541 * differs from the expected MAC.
1542 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001543 * The operation state is not valid (it must be an active mac verify
1544 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001545 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1546 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1547 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001548 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001549 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001550 * \retval #PSA_ERROR_BAD_STATE
1551 * The library has not been previously initialized by psa_crypto_init().
1552 * It is implementation-dependent whether a failure to initialize
1553 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001554 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001555psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1556 const uint8_t *mac,
1557 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001558
Gilles Peskinedcd14942018-07-12 00:30:52 +02001559/** Abort a MAC operation.
1560 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001561 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001562 * \p operation structure itself. Once aborted, the operation object
1563 * can be reused for another operation by calling
1564 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001565 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001566 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001567 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001568 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001569 * In particular, calling psa_mac_abort() after the operation has been
1570 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1571 * psa_mac_verify_finish() is safe and has no effect.
1572 *
1573 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001574 *
1575 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02001576 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1577 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001578 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001579 * \retval #PSA_ERROR_BAD_STATE
1580 * The library has not been previously initialized by psa_crypto_init().
1581 * It is implementation-dependent whether a failure to initialize
1582 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001583 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001584psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1585
1586/**@}*/
1587
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001588/** \defgroup cipher Symmetric ciphers
1589 * @{
1590 */
1591
Gilles Peskine69647a42019-01-14 20:18:12 +01001592/** Encrypt a message using a symmetric cipher.
1593 *
1594 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001595 * vector). Use the multipart operation interface with a
1596 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001597 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001598 * \param key Identifier of the key to use for the operation.
1599 * It must allow the usage PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001600 * \param alg The cipher algorithm to compute
1601 * (\c PSA_ALG_XXX value such that
1602 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1603 * \param[in] input Buffer containing the message to encrypt.
1604 * \param input_length Size of the \p input buffer in bytes.
1605 * \param[out] output Buffer where the output is to be written.
1606 * The output contains the IV followed by
1607 * the ciphertext proper.
1608 * \param output_size Size of the \p output buffer in bytes.
1609 * \param[out] output_length On success, the number of bytes
1610 * that make up the output.
1611 *
1612 * \retval #PSA_SUCCESS
1613 * Success.
1614 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001615 * \retval #PSA_ERROR_NOT_PERMITTED
1616 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001617 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001618 * \retval #PSA_ERROR_NOT_SUPPORTED
1619 * \p alg is not supported or is not a cipher algorithm.
1620 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1621 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1622 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1623 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001624 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001625 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001626 * \retval #PSA_ERROR_BAD_STATE
1627 * The library has not been previously initialized by psa_crypto_init().
1628 * It is implementation-dependent whether a failure to initialize
1629 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001630 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001631psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001632 psa_algorithm_t alg,
1633 const uint8_t *input,
1634 size_t input_length,
1635 uint8_t *output,
1636 size_t output_size,
1637 size_t *output_length);
1638
1639/** Decrypt a message using a symmetric cipher.
1640 *
1641 * This function decrypts a message encrypted with a symmetric cipher.
1642 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001643 * \param key Identifier of the key to use for the operation.
Gilles Peskine69647a42019-01-14 20:18:12 +01001644 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001645 * terminates. It must allow the usage
1646 * PSA_KEY_USAGE_DECRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001647 * \param alg The cipher algorithm to compute
1648 * (\c PSA_ALG_XXX value such that
1649 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1650 * \param[in] input Buffer containing the message to decrypt.
1651 * This consists of the IV followed by the
1652 * ciphertext proper.
1653 * \param input_length Size of the \p input buffer in bytes.
1654 * \param[out] output Buffer where the plaintext is to be written.
1655 * \param output_size Size of the \p output buffer in bytes.
1656 * \param[out] output_length On success, the number of bytes
1657 * that make up the output.
1658 *
1659 * \retval #PSA_SUCCESS
1660 * Success.
1661 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001662 * \retval #PSA_ERROR_NOT_PERMITTED
1663 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001664 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001665 * \retval #PSA_ERROR_NOT_SUPPORTED
1666 * \p alg is not supported or is not a cipher algorithm.
1667 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1668 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1669 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1670 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001671 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001672 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001673 * \retval #PSA_ERROR_BAD_STATE
1674 * The library has not been previously initialized by psa_crypto_init().
1675 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001676 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001677 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001678psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001679 psa_algorithm_t alg,
1680 const uint8_t *input,
1681 size_t input_length,
1682 uint8_t *output,
1683 size_t output_size,
1684 size_t *output_length);
1685
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001686/** The type of the state data structure for multipart cipher operations.
1687 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001688 * Before calling any function on a cipher operation object, the application
1689 * must initialize it by any of the following means:
1690 * - Set the structure to all-bits-zero, for example:
1691 * \code
1692 * psa_cipher_operation_t operation;
1693 * memset(&operation, 0, sizeof(operation));
1694 * \endcode
1695 * - Initialize the structure to logical zero values, for example:
1696 * \code
1697 * psa_cipher_operation_t operation = {0};
1698 * \endcode
1699 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1700 * for example:
1701 * \code
1702 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1703 * \endcode
1704 * - Assign the result of the function psa_cipher_operation_init()
1705 * to the structure, for example:
1706 * \code
1707 * psa_cipher_operation_t operation;
1708 * operation = psa_cipher_operation_init();
1709 * \endcode
1710 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001711 * This is an implementation-defined \c struct. Applications should not
1712 * make any assumptions about the content of this structure except
1713 * as directed by the documentation of a specific implementation. */
1714typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1715
Jaeden Amero5bae2272019-01-04 11:48:27 +00001716/** \def PSA_CIPHER_OPERATION_INIT
1717 *
1718 * This macro returns a suitable initializer for a cipher operation object of
1719 * type #psa_cipher_operation_t.
1720 */
1721#ifdef __DOXYGEN_ONLY__
1722/* This is an example definition for documentation purposes.
1723 * Implementations should define a suitable value in `crypto_struct.h`.
1724 */
1725#define PSA_CIPHER_OPERATION_INIT {0}
1726#endif
1727
1728/** Return an initial value for a cipher operation object.
1729 */
1730static psa_cipher_operation_t psa_cipher_operation_init(void);
1731
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001732/** Set the key for a multipart symmetric encryption operation.
1733 *
1734 * The sequence of operations to encrypt a message with a symmetric cipher
1735 * is as follows:
1736 * -# Allocate an operation object which will be passed to all the functions
1737 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001738 * -# Initialize the operation object with one of the methods described in the
1739 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001740 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001741 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001742 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001743 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001744 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001745 * requires a specific IV value.
1746 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1747 * of the message each time.
1748 * -# Call psa_cipher_finish().
1749 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001750 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1751 * the operation will need to be reset by a call to psa_cipher_abort(). The
1752 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001753 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001754 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001755 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001756 * eventually terminate the operation. The following events terminate an
1757 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001758 * - A successful call to psa_cipher_finish().
1759 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001760 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001761 * \param[in,out] operation The operation object to set up. It must have
1762 * been initialized as per the documentation for
1763 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001764 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001765 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001766 * terminates. It must allow the usage
1767 * PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001768 * \param alg The cipher algorithm to compute
1769 * (\c PSA_ALG_XXX value such that
1770 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001771 *
Gilles Peskine28538492018-07-11 17:34:00 +02001772 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001773 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001774 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001775 * \retval #PSA_ERROR_NOT_PERMITTED
1776 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001777 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001778 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001779 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001780 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1781 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1782 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001783 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001784 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001785 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001786 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001787 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001788 * The library has not been previously initialized by psa_crypto_init().
1789 * It is implementation-dependent whether a failure to initialize
1790 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001791 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001792psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001793 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001794 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001795
1796/** Set the key for a multipart symmetric decryption operation.
1797 *
1798 * The sequence of operations to decrypt a message with a symmetric cipher
1799 * is as follows:
1800 * -# Allocate an operation object which will be passed to all the functions
1801 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001802 * -# Initialize the operation object with one of the methods described in the
1803 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001804 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001805 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001806 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001807 * decryption. If the IV is prepended to the ciphertext, you can call
1808 * psa_cipher_update() on a buffer containing the IV followed by the
1809 * beginning of the message.
1810 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1811 * of the message each time.
1812 * -# Call psa_cipher_finish().
1813 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001814 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1815 * the operation will need to be reset by a call to psa_cipher_abort(). The
1816 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001817 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001818 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001819 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001820 * eventually terminate the operation. The following events terminate an
1821 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001822 * - A successful call to psa_cipher_finish().
1823 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001824 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001825 * \param[in,out] operation The operation object to set up. It must have
1826 * been initialized as per the documentation for
1827 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001828 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001829 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001830 * terminates. It must allow the usage
1831 * PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001832 * \param alg The cipher algorithm to compute
1833 * (\c PSA_ALG_XXX value such that
1834 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001835 *
Gilles Peskine28538492018-07-11 17:34:00 +02001836 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001837 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001838 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001839 * \retval #PSA_ERROR_NOT_PERMITTED
1840 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001841 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001842 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001843 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001844 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1845 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1846 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001847 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001848 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001849 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001850 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001851 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001852 * The library has not been previously initialized by psa_crypto_init().
1853 * It is implementation-dependent whether a failure to initialize
1854 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001855 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001856psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001857 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001858 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001859
Gilles Peskinedcd14942018-07-12 00:30:52 +02001860/** Generate an IV for a symmetric encryption operation.
1861 *
1862 * This function generates a random IV (initialization vector), nonce
1863 * or initial counter value for the encryption operation as appropriate
1864 * for the chosen algorithm, key type and key size.
1865 *
1866 * The application must call psa_cipher_encrypt_setup() before
1867 * calling this function.
1868 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001869 * If this function returns an error status, the operation enters an error
1870 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001871 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001872 * \param[in,out] operation Active cipher operation.
1873 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001874 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001875 * \param[out] iv_length On success, the number of bytes of the
1876 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001877 *
1878 * \retval #PSA_SUCCESS
1879 * Success.
1880 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001881 * The operation state is not valid (it must be active, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001882 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001883 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001884 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1885 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1886 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001887 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001888 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001889 * \retval #PSA_ERROR_BAD_STATE
1890 * The library has not been previously initialized by psa_crypto_init().
1891 * It is implementation-dependent whether a failure to initialize
1892 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001893 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001894psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001895 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001896 size_t iv_size,
1897 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001898
Gilles Peskinedcd14942018-07-12 00:30:52 +02001899/** Set the IV for a symmetric encryption or decryption operation.
1900 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001901 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001902 * or initial counter value for the encryption or decryption operation.
1903 *
1904 * The application must call psa_cipher_encrypt_setup() before
1905 * calling this function.
1906 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001907 * If this function returns an error status, the operation enters an error
1908 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001909 *
1910 * \note When encrypting, applications should use psa_cipher_generate_iv()
1911 * instead of this function, unless implementing a protocol that requires
1912 * a non-random IV.
1913 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001914 * \param[in,out] operation Active cipher operation.
1915 * \param[in] iv Buffer containing the IV to use.
1916 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001917 *
1918 * \retval #PSA_SUCCESS
1919 * Success.
1920 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001921 * The operation state is not valid (it must be an active cipher
1922 * encrypt operation, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001923 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001924 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001925 * or the chosen algorithm does not use an IV.
1926 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1927 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1928 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001929 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001930 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001931 * \retval #PSA_ERROR_BAD_STATE
1932 * The library has not been previously initialized by psa_crypto_init().
1933 * It is implementation-dependent whether a failure to initialize
1934 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001935 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001936psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001937 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001938 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001939
Gilles Peskinedcd14942018-07-12 00:30:52 +02001940/** Encrypt or decrypt a message fragment in an active cipher operation.
1941 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001942 * Before calling this function, you must:
1943 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1944 * The choice of setup function determines whether this function
1945 * encrypts or decrypts its input.
1946 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1947 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001948 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001949 * If this function returns an error status, the operation enters an error
1950 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001951 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001952 * \param[in,out] operation Active cipher operation.
1953 * \param[in] input Buffer containing the message fragment to
1954 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001955 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001956 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001957 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001958 * \param[out] output_length On success, the number of bytes
1959 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001960 *
1961 * \retval #PSA_SUCCESS
1962 * Success.
1963 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001964 * The operation state is not valid (it must be active, with an IV set
1965 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001966 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1967 * The size of the \p output buffer is too small.
1968 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1969 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1970 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001971 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001972 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001973 * \retval #PSA_ERROR_BAD_STATE
1974 * The library has not been previously initialized by psa_crypto_init().
1975 * It is implementation-dependent whether a failure to initialize
1976 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001977 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001978psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1979 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001980 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001981 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001982 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001983 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001984
Gilles Peskinedcd14942018-07-12 00:30:52 +02001985/** Finish encrypting or decrypting a message in a cipher operation.
1986 *
1987 * The application must call psa_cipher_encrypt_setup() or
1988 * psa_cipher_decrypt_setup() before calling this function. The choice
1989 * of setup function determines whether this function encrypts or
1990 * decrypts its input.
1991 *
1992 * This function finishes the encryption or decryption of the message
1993 * formed by concatenating the inputs passed to preceding calls to
1994 * psa_cipher_update().
1995 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001996 * When this function returns successfuly, the operation becomes inactive.
1997 * If this function returns an error status, the operation enters an error
1998 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001999 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002000 * \param[in,out] operation Active cipher operation.
2001 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002002 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002003 * \param[out] output_length On success, the number of bytes
2004 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002005 *
2006 * \retval #PSA_SUCCESS
2007 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02002008 * \retval #PSA_ERROR_INVALID_ARGUMENT
2009 * The total input size passed to this operation is not valid for
2010 * this particular algorithm. For example, the algorithm is a based
2011 * on block cipher and requires a whole number of blocks, but the
2012 * total input size is not a multiple of the block size.
2013 * \retval #PSA_ERROR_INVALID_PADDING
2014 * This is a decryption operation for an algorithm that includes
2015 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002016 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002017 * The operation state is not valid (it must be active, with an IV set
2018 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02002019 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2020 * The size of the \p output buffer is too small.
2021 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2022 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2023 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002024 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002025 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002026 * \retval #PSA_ERROR_BAD_STATE
2027 * The library has not been previously initialized by psa_crypto_init().
2028 * It is implementation-dependent whether a failure to initialize
2029 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002030 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002031psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002032 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002033 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002034 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002035
Gilles Peskinedcd14942018-07-12 00:30:52 +02002036/** Abort a cipher operation.
2037 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002038 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002039 * \p operation structure itself. Once aborted, the operation object
2040 * can be reused for another operation by calling
2041 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002042 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002043 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002044 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002045 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002046 * In particular, calling psa_cipher_abort() after the operation has been
2047 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2048 * is safe and has no effect.
2049 *
2050 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002051 *
2052 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02002053 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2054 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002055 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002056 * \retval #PSA_ERROR_BAD_STATE
2057 * The library has not been previously initialized by psa_crypto_init().
2058 * It is implementation-dependent whether a failure to initialize
2059 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002060 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002061psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2062
2063/**@}*/
2064
Gilles Peskine3b555712018-03-03 21:27:57 +01002065/** \defgroup aead Authenticated encryption with associated data (AEAD)
2066 * @{
2067 */
2068
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002069/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002070 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002071 * \param key Identifier of the key to use for the
2072 * operation. It must allow the usage
2073 * 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.
2093 * This must be at least
2094 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2095 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002096 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002097 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002098 *
Gilles Peskine28538492018-07-11 17:34:00 +02002099 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002100 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002101 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002102 * \retval #PSA_ERROR_NOT_PERMITTED
2103 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002104 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002105 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002106 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002107 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002108 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2109 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002110 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2111 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002112 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002113 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002114 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002115 * The library has not been previously initialized by psa_crypto_init().
2116 * It is implementation-dependent whether a failure to initialize
2117 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002118 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002119psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002120 psa_algorithm_t alg,
2121 const uint8_t *nonce,
2122 size_t nonce_length,
2123 const uint8_t *additional_data,
2124 size_t additional_data_length,
2125 const uint8_t *plaintext,
2126 size_t plaintext_length,
2127 uint8_t *ciphertext,
2128 size_t ciphertext_size,
2129 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002130
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002131/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002132 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002133 * \param key Identifier of the key to use for the
2134 * operation. It must allow the usage
2135 * PSA_KEY_USAGE_DECRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002136 * \param alg The AEAD algorithm to compute
2137 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002138 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002139 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002140 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002141 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002142 * but not encrypted.
2143 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002144 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002145 * encrypted. For algorithms where the
2146 * encrypted data and the authentication tag
2147 * are defined as separate inputs, the buffer
2148 * must contain the encrypted data followed
2149 * by the authentication tag.
2150 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002151 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002152 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2153 * This must be at least
2154 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2155 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002156 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002157 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002158 *
Gilles Peskine28538492018-07-11 17:34:00 +02002159 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002160 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002161 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002162 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002163 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002164 * \retval #PSA_ERROR_NOT_PERMITTED
2165 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002166 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002167 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002168 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002169 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002170 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2171 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002172 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2173 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002174 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002175 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002176 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002177 * The library has not been previously initialized by psa_crypto_init().
2178 * It is implementation-dependent whether a failure to initialize
2179 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002180 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002181psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002182 psa_algorithm_t alg,
2183 const uint8_t *nonce,
2184 size_t nonce_length,
2185 const uint8_t *additional_data,
2186 size_t additional_data_length,
2187 const uint8_t *ciphertext,
2188 size_t ciphertext_length,
2189 uint8_t *plaintext,
2190 size_t plaintext_size,
2191 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002192
Gilles Peskine30a9e412019-01-14 18:36:12 +01002193/** The type of the state data structure for multipart AEAD operations.
2194 *
2195 * Before calling any function on an AEAD operation object, the application
2196 * must initialize it by any of the following means:
2197 * - Set the structure to all-bits-zero, for example:
2198 * \code
2199 * psa_aead_operation_t operation;
2200 * memset(&operation, 0, sizeof(operation));
2201 * \endcode
2202 * - Initialize the structure to logical zero values, for example:
2203 * \code
2204 * psa_aead_operation_t operation = {0};
2205 * \endcode
2206 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2207 * for example:
2208 * \code
2209 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2210 * \endcode
2211 * - Assign the result of the function psa_aead_operation_init()
2212 * to the structure, for example:
2213 * \code
2214 * psa_aead_operation_t operation;
2215 * operation = psa_aead_operation_init();
2216 * \endcode
2217 *
2218 * This is an implementation-defined \c struct. Applications should not
2219 * make any assumptions about the content of this structure except
2220 * as directed by the documentation of a specific implementation. */
2221typedef struct psa_aead_operation_s psa_aead_operation_t;
2222
2223/** \def PSA_AEAD_OPERATION_INIT
2224 *
2225 * This macro returns a suitable initializer for an AEAD operation object of
2226 * type #psa_aead_operation_t.
2227 */
2228#ifdef __DOXYGEN_ONLY__
2229/* This is an example definition for documentation purposes.
2230 * Implementations should define a suitable value in `crypto_struct.h`.
2231 */
2232#define PSA_AEAD_OPERATION_INIT {0}
2233#endif
2234
2235/** Return an initial value for an AEAD operation object.
2236 */
2237static psa_aead_operation_t psa_aead_operation_init(void);
2238
2239/** Set the key for a multipart authenticated encryption operation.
2240 *
2241 * The sequence of operations to encrypt a message with authentication
2242 * is as follows:
2243 * -# Allocate an operation object which will be passed to all the functions
2244 * listed here.
2245 * -# Initialize the operation object with one of the methods described in the
2246 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002247 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002248 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002249 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2250 * inputs to the subsequent calls to psa_aead_update_ad() and
2251 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2252 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002253 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2254 * generate or set the nonce. You should use
2255 * psa_aead_generate_nonce() unless the protocol you are implementing
2256 * requires a specific nonce value.
2257 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2258 * of the non-encrypted additional authenticated data each time.
2259 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002260 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002261 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002262 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002263 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2264 * the operation will need to be reset by a call to psa_aead_abort(). The
2265 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002266 * has been initialized.
2267 *
2268 * After a successful call to psa_aead_encrypt_setup(), the application must
2269 * eventually terminate the operation. The following events terminate an
2270 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002271 * - A successful call to psa_aead_finish().
2272 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002273 *
2274 * \param[in,out] operation The operation object to set up. It must have
2275 * been initialized as per the documentation for
2276 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002277 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002278 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002279 * terminates. It must allow the usage
2280 * PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002281 * \param alg The AEAD algorithm to compute
2282 * (\c PSA_ALG_XXX value such that
2283 * #PSA_ALG_IS_AEAD(\p alg) is true).
2284 *
2285 * \retval #PSA_SUCCESS
2286 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002287 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002288 * The operation state is not valid (it must be inactive).
Andrew Thoelke340984b2019-09-11 21:33:41 +01002289 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002290 * \retval #PSA_ERROR_NOT_PERMITTED
2291 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002292 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002293 * \retval #PSA_ERROR_NOT_SUPPORTED
2294 * \p alg is not supported or is not an AEAD algorithm.
2295 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2296 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2297 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002298 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002299 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002300 * \retval #PSA_ERROR_BAD_STATE
2301 * The library has not been previously initialized by psa_crypto_init().
2302 * It is implementation-dependent whether a failure to initialize
2303 * results in this error code.
2304 */
2305psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002306 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002307 psa_algorithm_t alg);
2308
2309/** Set the key for a multipart authenticated decryption operation.
2310 *
2311 * The sequence of operations to decrypt a message with authentication
2312 * is as follows:
2313 * -# Allocate an operation object which will be passed to all the functions
2314 * listed here.
2315 * -# Initialize the operation object with one of the methods described in the
2316 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002317 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002318 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002319 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2320 * inputs to the subsequent calls to psa_aead_update_ad() and
2321 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2322 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002323 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2324 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2325 * of the non-encrypted additional authenticated data each time.
2326 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002327 * of the ciphertext to decrypt each time.
2328 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002329 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002330 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2331 * the operation will need to be reset by a call to psa_aead_abort(). The
2332 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002333 * has been initialized.
2334 *
2335 * After a successful call to psa_aead_decrypt_setup(), the application must
2336 * eventually terminate the operation. The following events terminate an
2337 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002338 * - A successful call to psa_aead_verify().
2339 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002340 *
2341 * \param[in,out] operation The operation object to set up. It must have
2342 * been initialized as per the documentation for
2343 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002344 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002345 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002346 * terminates. It must allow the usage
2347 * PSA_KEY_USAGE_DECRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002348 * \param alg The AEAD algorithm to compute
2349 * (\c PSA_ALG_XXX value such that
2350 * #PSA_ALG_IS_AEAD(\p alg) is true).
2351 *
2352 * \retval #PSA_SUCCESS
2353 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002354 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002355 * The operation state is not valid (it must be inactive).
Andrew Thoelke340984b2019-09-11 21:33:41 +01002356 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002357 * \retval #PSA_ERROR_NOT_PERMITTED
2358 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002359 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002360 * \retval #PSA_ERROR_NOT_SUPPORTED
2361 * \p alg is not supported or is not an AEAD algorithm.
2362 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2363 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2364 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002365 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002366 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002367 * \retval #PSA_ERROR_BAD_STATE
2368 * The library has not been previously initialized by psa_crypto_init().
2369 * It is implementation-dependent whether a failure to initialize
2370 * results in this error code.
2371 */
2372psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002373 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002374 psa_algorithm_t alg);
2375
2376/** Generate a random nonce for an authenticated encryption operation.
2377 *
2378 * This function generates a random nonce for the authenticated encryption
2379 * operation with an appropriate size for the chosen algorithm, key type
2380 * and key size.
2381 *
2382 * The application must call psa_aead_encrypt_setup() before
2383 * calling this function.
2384 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002385 * If this function returns an error status, the operation enters an error
2386 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002387 *
2388 * \param[in,out] operation Active AEAD operation.
2389 * \param[out] nonce Buffer where the generated nonce is to be
2390 * written.
2391 * \param nonce_size Size of the \p nonce buffer in bytes.
2392 * \param[out] nonce_length On success, the number of bytes of the
2393 * generated nonce.
2394 *
2395 * \retval #PSA_SUCCESS
2396 * Success.
2397 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002398 * The operation state is not valid (it must be an active aead encrypt
2399 operation, with no nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002400 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2401 * The size of the \p nonce buffer is too small.
2402 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2403 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2404 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002405 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002406 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002407 * \retval #PSA_ERROR_BAD_STATE
2408 * The library has not been previously initialized by psa_crypto_init().
2409 * It is implementation-dependent whether a failure to initialize
2410 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002411 */
2412psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002413 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002414 size_t nonce_size,
2415 size_t *nonce_length);
2416
2417/** Set the nonce for an authenticated encryption or decryption operation.
2418 *
2419 * This function sets the nonce for the authenticated
2420 * encryption or decryption operation.
2421 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002422 * The application must call psa_aead_encrypt_setup() or
2423 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002424 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002425 * If this function returns an error status, the operation enters an error
2426 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002427 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002428 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002429 * instead of this function, unless implementing a protocol that requires
2430 * a non-random IV.
2431 *
2432 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002433 * \param[in] nonce Buffer containing the nonce to use.
2434 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002435 *
2436 * \retval #PSA_SUCCESS
2437 * Success.
2438 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002439 * The operation state is not valid (it must be active, with no nonce
2440 * set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002441 * \retval #PSA_ERROR_INVALID_ARGUMENT
2442 * The size of \p nonce is not acceptable for the chosen algorithm.
2443 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2444 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2445 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002446 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002447 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002448 * \retval #PSA_ERROR_BAD_STATE
2449 * The library has not been previously initialized by psa_crypto_init().
2450 * It is implementation-dependent whether a failure to initialize
2451 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002452 */
2453psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002454 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002455 size_t nonce_length);
2456
Gilles Peskinebc59c852019-01-17 15:26:08 +01002457/** Declare the lengths of the message and additional data for AEAD.
2458 *
2459 * The application must call this function before calling
2460 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2461 * the operation requires it. If the algorithm does not require it,
2462 * calling this function is optional, but if this function is called
2463 * then the implementation must enforce the lengths.
2464 *
2465 * You may call this function before or after setting the nonce with
2466 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2467 *
2468 * - For #PSA_ALG_CCM, calling this function is required.
2469 * - For the other AEAD algorithms defined in this specification, calling
2470 * this function is not required.
2471 * - For vendor-defined algorithm, refer to the vendor documentation.
2472 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002473 * If this function returns an error status, the operation enters an error
2474 * state and must be aborted by calling psa_aead_abort().
2475 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002476 * \param[in,out] operation Active AEAD operation.
2477 * \param ad_length Size of the non-encrypted additional
2478 * authenticated data in bytes.
2479 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2480 *
2481 * \retval #PSA_SUCCESS
2482 * Success.
2483 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002484 * The operation state is not valid (it must be active, and
2485 * psa_aead_update_ad() and psa_aead_update() must not have been
2486 * called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002487 * \retval #PSA_ERROR_INVALID_ARGUMENT
2488 * At least one of the lengths is not acceptable for the chosen
2489 * algorithm.
2490 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2491 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2492 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002493 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002494 * \retval #PSA_ERROR_BAD_STATE
2495 * The library has not been previously initialized by psa_crypto_init().
2496 * It is implementation-dependent whether a failure to initialize
2497 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002498 */
2499psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2500 size_t ad_length,
2501 size_t plaintext_length);
2502
Gilles Peskine30a9e412019-01-14 18:36:12 +01002503/** Pass additional data to an active AEAD operation.
2504 *
2505 * Additional data is authenticated, but not encrypted.
2506 *
2507 * You may call this function multiple times to pass successive fragments
2508 * of the additional data. You may not call this function after passing
2509 * data to encrypt or decrypt with psa_aead_update().
2510 *
2511 * Before calling this function, you must:
2512 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2513 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2514 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002515 * If this function returns an error status, the operation enters an error
2516 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002517 *
2518 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2519 * there is no guarantee that the input is valid. Therefore, until
2520 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2521 * treat the input as untrusted and prepare to undo any action that
2522 * depends on the input if psa_aead_verify() returns an error status.
2523 *
2524 * \param[in,out] operation Active AEAD operation.
2525 * \param[in] input Buffer containing the fragment of
2526 * additional data.
2527 * \param input_length Size of the \p input buffer in bytes.
2528 *
2529 * \retval #PSA_SUCCESS
2530 * Success.
2531 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002532 * The operation state is not valid (it must be active, have a nonce
2533 * set, have lengths set if required by the algorithm, and
2534 * psa_aead_update() must not have been called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002535 * \retval #PSA_ERROR_INVALID_ARGUMENT
2536 * The total input length overflows the additional data length that
2537 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002538 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2539 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2540 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002541 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002542 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002543 * \retval #PSA_ERROR_BAD_STATE
2544 * The library has not been previously initialized by psa_crypto_init().
2545 * It is implementation-dependent whether a failure to initialize
2546 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002547 */
2548psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2549 const uint8_t *input,
2550 size_t input_length);
2551
2552/** Encrypt or decrypt a message fragment in an active AEAD operation.
2553 *
2554 * Before calling this function, you must:
2555 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2556 * The choice of setup function determines whether this function
2557 * encrypts or decrypts its input.
2558 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2559 * 3. Call psa_aead_update_ad() to pass all the additional data.
2560 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002561 * If this function returns an error status, the operation enters an error
2562 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002563 *
2564 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2565 * there is no guarantee that the input is valid. Therefore, until
2566 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2567 * - Do not use the output in any way other than storing it in a
2568 * confidential location. If you take any action that depends
2569 * on the tentative decrypted data, this action will need to be
2570 * undone if the input turns out not to be valid. Furthermore,
2571 * if an adversary can observe that this action took place
2572 * (for example through timing), they may be able to use this
2573 * fact as an oracle to decrypt any message encrypted with the
2574 * same key.
2575 * - In particular, do not copy the output anywhere but to a
2576 * memory or storage space that you have exclusive access to.
2577 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002578 * This function does not require the input to be aligned to any
2579 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002580 * a whole block at a time, it must consume all the input provided, but
2581 * it may delay the end of the corresponding output until a subsequent
2582 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2583 * provides sufficient input. The amount of data that can be delayed
2584 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002585 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002586 * \param[in,out] operation Active AEAD operation.
2587 * \param[in] input Buffer containing the message fragment to
2588 * encrypt or decrypt.
2589 * \param input_length Size of the \p input buffer in bytes.
2590 * \param[out] output Buffer where the output is to be written.
2591 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002592 * This must be at least
2593 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2594 * \p input_length) where \c alg is the
2595 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002596 * \param[out] output_length On success, the number of bytes
2597 * that make up the returned output.
2598 *
2599 * \retval #PSA_SUCCESS
2600 * Success.
2601 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002602 * The operation state is not valid (it must be active, have a nonce
2603 * set, and have lengths set if required by the algorithm).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002604 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2605 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002606 * You can determine a sufficient buffer size by calling
2607 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2608 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002609 * \retval #PSA_ERROR_INVALID_ARGUMENT
2610 * The total length of input to psa_aead_update_ad() so far is
2611 * less than the additional data length that was previously
2612 * specified with psa_aead_set_lengths().
2613 * \retval #PSA_ERROR_INVALID_ARGUMENT
2614 * The total input length overflows the plaintext length that
2615 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002616 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2617 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2618 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002619 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002620 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002621 * \retval #PSA_ERROR_BAD_STATE
2622 * The library has not been previously initialized by psa_crypto_init().
2623 * It is implementation-dependent whether a failure to initialize
2624 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002625 */
2626psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2627 const uint8_t *input,
2628 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002629 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002630 size_t output_size,
2631 size_t *output_length);
2632
2633/** Finish encrypting a message in an AEAD operation.
2634 *
2635 * The operation must have been set up with psa_aead_encrypt_setup().
2636 *
2637 * This function finishes the authentication of the additional data
2638 * formed by concatenating the inputs passed to preceding calls to
2639 * psa_aead_update_ad() with the plaintext formed by concatenating the
2640 * inputs passed to preceding calls to psa_aead_update().
2641 *
2642 * This function has two output buffers:
2643 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002644 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002645 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002646 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002647 * that the operation performs.
2648 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002649 * When this function returns successfuly, the operation becomes inactive.
2650 * If this function returns an error status, the operation enters an error
2651 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002652 *
2653 * \param[in,out] operation Active AEAD operation.
2654 * \param[out] ciphertext Buffer where the last part of the ciphertext
2655 * is to be written.
2656 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002657 * This must be at least
2658 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2659 * \c alg is the algorithm that is being
2660 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002661 * \param[out] ciphertext_length On success, the number of bytes of
2662 * returned ciphertext.
2663 * \param[out] tag Buffer where the authentication tag is
2664 * to be written.
2665 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002666 * This must be at least
2667 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2668 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002669 * \param[out] tag_length On success, the number of bytes
2670 * that make up the returned tag.
2671 *
2672 * \retval #PSA_SUCCESS
2673 * Success.
2674 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002675 * The operation state is not valid (it must be an active encryption
2676 * operation with a nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002677 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002678 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002679 * You can determine a sufficient buffer size for \p ciphertext by
2680 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2681 * where \c alg is the algorithm that is being calculated.
2682 * You can determine a sufficient buffer size for \p tag by
2683 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002684 * \retval #PSA_ERROR_INVALID_ARGUMENT
2685 * The total length of input to psa_aead_update_ad() so far is
2686 * less than the additional data length that was previously
2687 * specified with psa_aead_set_lengths().
2688 * \retval #PSA_ERROR_INVALID_ARGUMENT
2689 * The total length of input to psa_aead_update() so far is
2690 * less than the plaintext length that was previously
2691 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002692 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2693 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2694 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002695 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002696 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002697 * \retval #PSA_ERROR_BAD_STATE
2698 * The library has not been previously initialized by psa_crypto_init().
2699 * It is implementation-dependent whether a failure to initialize
2700 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002701 */
2702psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002703 uint8_t *ciphertext,
2704 size_t ciphertext_size,
2705 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002706 uint8_t *tag,
2707 size_t tag_size,
2708 size_t *tag_length);
2709
2710/** Finish authenticating and decrypting a message in an AEAD operation.
2711 *
2712 * The operation must have been set up with psa_aead_decrypt_setup().
2713 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002714 * This function finishes the authenticated decryption of the message
2715 * components:
2716 *
2717 * - The additional data consisting of the concatenation of the inputs
2718 * passed to preceding calls to psa_aead_update_ad().
2719 * - The ciphertext consisting of the concatenation of the inputs passed to
2720 * preceding calls to psa_aead_update().
2721 * - The tag passed to this function call.
2722 *
2723 * If the authentication tag is correct, this function outputs any remaining
2724 * plaintext and reports success. If the authentication tag is not correct,
2725 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002726 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002727 * When this function returns successfuly, the operation becomes inactive.
2728 * If this function returns an error status, the operation enters an error
2729 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002730 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002731 * \note Implementations shall make the best effort to ensure that the
2732 * comparison between the actual tag and the expected tag is performed
2733 * in constant time.
2734 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002735 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002736 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002737 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002738 * from previous calls to psa_aead_update()
2739 * that could not be processed until the end
2740 * of the input.
2741 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002742 * This must be at least
2743 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2744 * \c alg is the algorithm that is being
2745 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002746 * \param[out] plaintext_length On success, the number of bytes of
2747 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002748 * \param[in] tag Buffer containing the authentication tag.
2749 * \param tag_length Size of the \p tag buffer in bytes.
2750 *
2751 * \retval #PSA_SUCCESS
2752 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002753 * \retval #PSA_ERROR_INVALID_SIGNATURE
2754 * The calculations were successful, but the authentication tag is
2755 * not correct.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002756 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002757 * The operation state is not valid (it must be an active decryption
2758 * operation with a nonce set).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002759 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2760 * The size of the \p plaintext buffer is too small.
2761 * You can determine a sufficient buffer size for \p plaintext by
2762 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2763 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002764 * \retval #PSA_ERROR_INVALID_ARGUMENT
2765 * The total length of input to psa_aead_update_ad() so far is
2766 * less than the additional data length that was previously
2767 * specified with psa_aead_set_lengths().
2768 * \retval #PSA_ERROR_INVALID_ARGUMENT
2769 * The total length of input to psa_aead_update() so far is
2770 * less than the plaintext length that was previously
2771 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002772 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2773 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2774 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002775 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002776 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002777 * \retval #PSA_ERROR_BAD_STATE
2778 * The library has not been previously initialized by psa_crypto_init().
2779 * It is implementation-dependent whether a failure to initialize
2780 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002781 */
2782psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002783 uint8_t *plaintext,
2784 size_t plaintext_size,
2785 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002786 const uint8_t *tag,
2787 size_t tag_length);
2788
2789/** Abort an AEAD operation.
2790 *
2791 * Aborting an operation frees all associated resources except for the
2792 * \p operation structure itself. Once aborted, the operation object
2793 * can be reused for another operation by calling
2794 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2795 *
2796 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002797 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002798 *
2799 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002800 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2801 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002802 *
2803 * \param[in,out] operation Initialized AEAD operation.
2804 *
2805 * \retval #PSA_SUCCESS
Gilles Peskine30a9e412019-01-14 18:36:12 +01002806 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2807 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002808 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002809 * \retval #PSA_ERROR_BAD_STATE
2810 * The library has not been previously initialized by psa_crypto_init().
2811 * It is implementation-dependent whether a failure to initialize
2812 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002813 */
2814psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2815
Gilles Peskine3b555712018-03-03 21:27:57 +01002816/**@}*/
2817
Gilles Peskine20035e32018-02-03 22:44:14 +01002818/** \defgroup asymmetric Asymmetric cryptography
2819 * @{
2820 */
2821
2822/**
2823 * \brief Sign a hash or short message with a private key.
2824 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002825 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002826 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002827 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2828 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2829 * to determine the hash algorithm to use.
2830 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002831 * \param key Identifier of the key to use for the operation.
2832 * It must be an asymmetric key pair. The key must
2833 * allow the usage PSA_KEY_USAGE_SIGN_HASH.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002834 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002835 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002836 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002837 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002838 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002839 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002840 * \param[out] signature_length On success, the number of bytes
2841 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002842 *
Gilles Peskine28538492018-07-11 17:34:00 +02002843 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002844 * \retval #PSA_ERROR_INVALID_HANDLE
2845 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002846 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002847 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002848 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002849 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002850 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002851 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002852 * \retval #PSA_ERROR_NOT_SUPPORTED
2853 * \retval #PSA_ERROR_INVALID_ARGUMENT
2854 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2855 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2856 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002857 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002858 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002859 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002860 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002861 * The library has not been previously initialized by psa_crypto_init().
2862 * It is implementation-dependent whether a failure to initialize
2863 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002864 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002865psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002866 psa_algorithm_t alg,
2867 const uint8_t *hash,
2868 size_t hash_length,
2869 uint8_t *signature,
2870 size_t signature_size,
2871 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002872
2873/**
2874 * \brief Verify the signature a hash or short message using a public key.
2875 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002876 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002877 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002878 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2879 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2880 * to determine the hash algorithm to use.
2881 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002882 * \param key Identifier of the key to use for the operation. It
2883 * must be a public key or an asymmetric key pair. The
2884 * key must allow the usage PSA_KEY_USAGE_VERIFY_HASH.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002885 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002886 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002887 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002888 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002889 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002890 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002891 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002892 *
Gilles Peskine28538492018-07-11 17:34:00 +02002893 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002894 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002895 * \retval #PSA_ERROR_INVALID_HANDLE
2896 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002897 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002898 * The calculation was perfomed successfully, but the passed
2899 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002900 * \retval #PSA_ERROR_NOT_SUPPORTED
2901 * \retval #PSA_ERROR_INVALID_ARGUMENT
2902 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2903 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2904 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002905 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002906 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002907 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002908 * The library has not been previously initialized by psa_crypto_init().
2909 * It is implementation-dependent whether a failure to initialize
2910 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002911 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002912psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002913 psa_algorithm_t alg,
2914 const uint8_t *hash,
2915 size_t hash_length,
2916 const uint8_t *signature,
2917 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002918
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002919/**
2920 * \brief Encrypt a short message with a public key.
2921 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002922 * \param key Identifer of the key to use for the operation.
2923 * It must be a public key or an asymmetric key
2924 * pair. It must allow the usage
2925 * PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002926 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02002927 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002928 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002929 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002930 * \param[in] salt A salt or label, if supported by the
2931 * encryption algorithm.
2932 * If the algorithm does not support a
2933 * salt, pass \c NULL.
2934 * If the algorithm supports an optional
2935 * salt and you do not want to pass a salt,
2936 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002937 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002938 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2939 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002940 * \param salt_length Size of the \p salt buffer in bytes.
2941 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002942 * \param[out] output Buffer where the encrypted message is to
2943 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002944 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002945 * \param[out] output_length On success, the number of bytes
2946 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002947 *
Gilles Peskine28538492018-07-11 17:34:00 +02002948 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002949 * \retval #PSA_ERROR_INVALID_HANDLE
2950 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002951 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002952 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002953 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002954 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002955 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002956 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002957 * \retval #PSA_ERROR_NOT_SUPPORTED
2958 * \retval #PSA_ERROR_INVALID_ARGUMENT
2959 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2960 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2961 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002962 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002963 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002964 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002965 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002966 * The library has not been previously initialized by psa_crypto_init().
2967 * It is implementation-dependent whether a failure to initialize
2968 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002969 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002970psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002971 psa_algorithm_t alg,
2972 const uint8_t *input,
2973 size_t input_length,
2974 const uint8_t *salt,
2975 size_t salt_length,
2976 uint8_t *output,
2977 size_t output_size,
2978 size_t *output_length);
2979
2980/**
2981 * \brief Decrypt a short message with a private key.
2982 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002983 * \param key Identifier of the key to use for the operation.
2984 * It must be an asymmetric key pair. It must
2985 * allow the usage PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002986 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02002987 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002988 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002989 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002990 * \param[in] salt A salt or label, if supported by the
2991 * encryption algorithm.
2992 * If the algorithm does not support a
2993 * salt, pass \c NULL.
2994 * If the algorithm supports an optional
2995 * salt and you do not want to pass a salt,
2996 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002997 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002998 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2999 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003000 * \param salt_length Size of the \p salt buffer in bytes.
3001 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003002 * \param[out] output Buffer where the decrypted message is to
3003 * be written.
3004 * \param output_size Size of the \c output buffer in bytes.
3005 * \param[out] output_length On success, the number of bytes
3006 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003007 *
Gilles Peskine28538492018-07-11 17:34:00 +02003008 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003009 * \retval #PSA_ERROR_INVALID_HANDLE
3010 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003011 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003012 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003013 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003014 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003015 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003016 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003017 * \retval #PSA_ERROR_NOT_SUPPORTED
3018 * \retval #PSA_ERROR_INVALID_ARGUMENT
3019 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3020 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3021 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003022 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003023 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003024 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3025 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003026 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003027 * The library has not been previously initialized by psa_crypto_init().
3028 * It is implementation-dependent whether a failure to initialize
3029 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003030 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003031psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003032 psa_algorithm_t alg,
3033 const uint8_t *input,
3034 size_t input_length,
3035 const uint8_t *salt,
3036 size_t salt_length,
3037 uint8_t *output,
3038 size_t output_size,
3039 size_t *output_length);
3040
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003041/**@}*/
3042
Gilles Peskine35675b62019-05-16 17:26:11 +02003043/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003044 * @{
3045 */
3046
Gilles Peskine35675b62019-05-16 17:26:11 +02003047/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003048 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003049 * Before calling any function on a key derivation operation object, the
3050 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003051 * - Set the structure to all-bits-zero, for example:
3052 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003053 * psa_key_derivation_operation_t operation;
3054 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003055 * \endcode
3056 * - Initialize the structure to logical zero values, for example:
3057 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003058 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003059 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003060 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003061 * for example:
3062 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003063 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003064 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003065 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003066 * to the structure, for example:
3067 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003068 * psa_key_derivation_operation_t operation;
3069 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003070 * \endcode
3071 *
3072 * This is an implementation-defined \c struct. Applications should not
3073 * make any assumptions about the content of this structure except
3074 * as directed by the documentation of a specific implementation.
3075 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003076typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003077
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003078/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003079 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003080 * This macro returns a suitable initializer for a key derivation operation
3081 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003082 */
3083#ifdef __DOXYGEN_ONLY__
3084/* This is an example definition for documentation purposes.
3085 * Implementations should define a suitable value in `crypto_struct.h`.
3086 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003087#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003088#endif
3089
Gilles Peskine35675b62019-05-16 17:26:11 +02003090/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003091 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003092static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003093
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003094/** Set up a key derivation operation.
3095 *
3096 * A key derivation algorithm takes some inputs and uses them to generate
3097 * a byte stream in a deterministic way.
3098 * This byte stream can be used to produce keys and other
3099 * cryptographic material.
3100 *
3101 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003102 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3103 * -# Call psa_key_derivation_setup() to select the algorithm.
3104 * -# Provide the inputs for the key derivation by calling
3105 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3106 * as appropriate. Which inputs are needed, in what order, and whether
3107 * they may be keys and if so of what type depends on the algorithm.
3108 * -# Optionally set the operation's maximum capacity with
3109 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3110 * of or after providing inputs. For some algorithms, this step is mandatory
3111 * because the output depends on the maximum capacity.
3112 * -# To derive a key, call psa_key_derivation_output_key().
3113 * To derive a byte string for a different purpose, call
3114 * psa_key_derivation_output_bytes().
3115 * Successive calls to these functions use successive output bytes
3116 * calculated by the key derivation algorithm.
3117 * -# Clean up the key derivation operation object with
3118 * psa_key_derivation_abort().
3119 *
3120 * If this function returns an error, the key derivation operation object is
3121 * not changed.
3122 *
3123 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3124 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003125 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003126 * Implementations must reject an attempt to derive a key of size 0.
3127 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003128 * \param[in,out] operation The key derivation operation object
3129 * to set up. It must
3130 * have been initialized but not set up yet.
3131 * \param alg The key derivation algorithm to compute
3132 * (\c PSA_ALG_XXX value such that
3133 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3134 *
3135 * \retval #PSA_SUCCESS
3136 * Success.
3137 * \retval #PSA_ERROR_INVALID_ARGUMENT
3138 * \c alg is not a key derivation algorithm.
3139 * \retval #PSA_ERROR_NOT_SUPPORTED
3140 * \c alg is not supported or is not a key derivation algorithm.
3141 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3142 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3143 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003144 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003145 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003146 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003147 * The operation state is not valid (it must be inactive).
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003148 * \retval #PSA_ERROR_BAD_STATE
3149 * The library has not been previously initialized by psa_crypto_init().
3150 * It is implementation-dependent whether a failure to initialize
3151 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003152 */
3153psa_status_t psa_key_derivation_setup(
3154 psa_key_derivation_operation_t *operation,
3155 psa_algorithm_t alg);
3156
Gilles Peskine35675b62019-05-16 17:26:11 +02003157/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003158 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003159 * The capacity of a key derivation is the maximum number of bytes that it can
3160 * return. When you get *N* bytes of output from a key derivation operation,
3161 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003162 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003163 * \param[in] operation The operation to query.
3164 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003165 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003166 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003167 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003168 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003169 * The operation state is not valid (it must be active).
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003170 * \retval #PSA_ERROR_HARDWARE_FAILURE
3171 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003172 * \retval #PSA_ERROR_BAD_STATE
3173 * The library has not been previously initialized by psa_crypto_init().
3174 * It is implementation-dependent whether a failure to initialize
3175 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003176 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003177psa_status_t psa_key_derivation_get_capacity(
3178 const psa_key_derivation_operation_t *operation,
3179 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003180
Gilles Peskine35675b62019-05-16 17:26:11 +02003181/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003182 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003183 * The capacity of a key derivation operation is the maximum number of bytes
3184 * that the key derivation operation can return from this point onwards.
3185 *
3186 * \param[in,out] operation The key derivation operation object to modify.
3187 * \param capacity The new capacity of the operation.
3188 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003189 * current capacity.
3190 *
3191 * \retval #PSA_SUCCESS
3192 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003193 * \p capacity is larger than the operation's current capacity.
3194 * In this case, the operation object remains valid and its capacity
3195 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003196 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003197 * The operation state is not valid (it must be active).
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003198 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003199 * \retval #PSA_ERROR_HARDWARE_FAILURE
3200 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003201 * \retval #PSA_ERROR_BAD_STATE
3202 * The library has not been previously initialized by psa_crypto_init().
3203 * It is implementation-dependent whether a failure to initialize
3204 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003205 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003206psa_status_t psa_key_derivation_set_capacity(
3207 psa_key_derivation_operation_t *operation,
3208 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003209
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003210/** Use the maximum possible capacity for a key derivation operation.
3211 *
3212 * Use this value as the capacity argument when setting up a key derivation
3213 * to indicate that the operation should have the maximum possible capacity.
3214 * The value of the maximum possible capacity depends on the key derivation
3215 * algorithm.
3216 */
3217#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3218
3219/** Provide an input for key derivation or key agreement.
3220 *
3221 * Which inputs are required and in what order depends on the algorithm.
3222 * Refer to the documentation of each key derivation or key agreement
3223 * algorithm for information.
3224 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003225 * This function passes direct inputs, which is usually correct for
3226 * non-secret inputs. To pass a secret input, which should be in a key
3227 * object, call psa_key_derivation_input_key() instead of this function.
3228 * Refer to the documentation of individual step types
3229 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3230 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003231 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003232 * If this function returns an error status, the operation enters an error
3233 * state and must be aborted by calling psa_key_derivation_abort().
3234 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003235 * \param[in,out] operation The key derivation operation object to use.
3236 * It must have been set up with
3237 * psa_key_derivation_setup() and must not
3238 * have produced any output yet.
3239 * \param step Which step the input data is for.
3240 * \param[in] data Input data to use.
3241 * \param data_length Size of the \p data buffer in bytes.
3242 *
3243 * \retval #PSA_SUCCESS
3244 * Success.
3245 * \retval #PSA_ERROR_INVALID_ARGUMENT
3246 * \c step is not compatible with the operation's algorithm.
3247 * \retval #PSA_ERROR_INVALID_ARGUMENT
3248 * \c step does not allow direct inputs.
3249 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3250 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3251 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003252 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003253 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003254 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003255 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003256 * \retval #PSA_ERROR_BAD_STATE
3257 * The library has not been previously initialized by psa_crypto_init().
3258 * It is implementation-dependent whether a failure to initialize
3259 * results in this error code.
3260 */
3261psa_status_t psa_key_derivation_input_bytes(
3262 psa_key_derivation_operation_t *operation,
3263 psa_key_derivation_step_t step,
3264 const uint8_t *data,
3265 size_t data_length);
3266
3267/** Provide an input for key derivation in the form of a key.
3268 *
3269 * Which inputs are required and in what order depends on the algorithm.
3270 * Refer to the documentation of each key derivation or key agreement
3271 * algorithm for information.
3272 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003273 * This function obtains input from a key object, which is usually correct for
3274 * secret inputs or for non-secret personalization strings kept in the key
3275 * store. To pass a non-secret parameter which is not in the key store,
3276 * call psa_key_derivation_input_bytes() instead of this function.
3277 * Refer to the documentation of individual step types
3278 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3279 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003280 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003281 * If this function returns an error status, the operation enters an error
3282 * state and must be aborted by calling psa_key_derivation_abort().
3283 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003284 * \param[in,out] operation The key derivation operation object to use.
3285 * It must have been set up with
3286 * psa_key_derivation_setup() and must not
3287 * have produced any output yet.
3288 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003289 * \param key Identifier of the key. It must have an
3290 * appropriate type for step and must allow the
3291 * usage PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003292 *
3293 * \retval #PSA_SUCCESS
3294 * Success.
3295 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003296 * \retval #PSA_ERROR_NOT_PERMITTED
3297 * \retval #PSA_ERROR_INVALID_ARGUMENT
3298 * \c step is not compatible with the operation's algorithm.
3299 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine224b0d62019-09-23 18:13:17 +02003300 * \c step does not allow key inputs of the given type
3301 * or does not allow key inputs at all.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003302 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3303 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3304 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003305 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003306 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003307 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003308 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003309 * \retval #PSA_ERROR_BAD_STATE
3310 * The library has not been previously initialized by psa_crypto_init().
3311 * It is implementation-dependent whether a failure to initialize
3312 * results in this error code.
3313 */
3314psa_status_t psa_key_derivation_input_key(
3315 psa_key_derivation_operation_t *operation,
3316 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003317 mbedtls_svc_key_id_t key);
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003318
3319/** Perform a key agreement and use the shared secret as input to a key
3320 * derivation.
3321 *
3322 * A key agreement algorithm takes two inputs: a private key \p private_key
3323 * a public key \p peer_key.
3324 * The result of this function is passed as input to a key derivation.
3325 * The output of this key derivation can be extracted by reading from the
3326 * resulting operation to produce keys and other cryptographic material.
3327 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003328 * If this function returns an error status, the operation enters an error
3329 * state and must be aborted by calling psa_key_derivation_abort().
3330 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003331 * \param[in,out] operation The key derivation operation object to use.
3332 * It must have been set up with
3333 * psa_key_derivation_setup() with a
3334 * key agreement and derivation algorithm
3335 * \c alg (\c PSA_ALG_XXX value such that
3336 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3337 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3338 * is false).
3339 * The operation must be ready for an
3340 * input of the type given by \p step.
3341 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003342 * \param private_key Identifier of the private key to use. It must
3343 * allow the usage PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003344 * \param[in] peer_key Public key of the peer. The peer key must be in the
3345 * same format that psa_import_key() accepts for the
3346 * public key type corresponding to the type of
3347 * private_key. That is, this function performs the
3348 * equivalent of
3349 * #psa_import_key(...,
3350 * `peer_key`, `peer_key_length`) where
3351 * with key attributes indicating the public key
3352 * type corresponding to the type of `private_key`.
3353 * For example, for EC keys, this means that peer_key
3354 * is interpreted as a point on the curve that the
3355 * private key is on. The standard formats for public
3356 * keys are documented in the documentation of
3357 * psa_export_public_key().
3358 * \param peer_key_length Size of \p peer_key in bytes.
3359 *
3360 * \retval #PSA_SUCCESS
3361 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01003362 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003363 * The operation state is not valid for this key agreement \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003364 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003365 * \retval #PSA_ERROR_NOT_PERMITTED
3366 * \retval #PSA_ERROR_INVALID_ARGUMENT
3367 * \c private_key is not compatible with \c alg,
3368 * or \p peer_key is not valid for \c alg or not compatible with
3369 * \c private_key.
3370 * \retval #PSA_ERROR_NOT_SUPPORTED
3371 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskine224b0d62019-09-23 18:13:17 +02003372 * \retval #PSA_ERROR_INVALID_ARGUMENT
3373 * \c step does not allow an input resulting from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003374 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3375 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3376 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003377 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003378 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003379 * \retval #PSA_ERROR_BAD_STATE
3380 * The library has not been previously initialized by psa_crypto_init().
3381 * It is implementation-dependent whether a failure to initialize
3382 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003383 */
3384psa_status_t psa_key_derivation_key_agreement(
3385 psa_key_derivation_operation_t *operation,
3386 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003387 mbedtls_svc_key_id_t private_key,
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003388 const uint8_t *peer_key,
3389 size_t peer_key_length);
3390
Gilles Peskine35675b62019-05-16 17:26:11 +02003391/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003392 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003393 * This function calculates output bytes from a key derivation algorithm and
3394 * return those bytes.
3395 * If you view the key derivation's output as a stream of bytes, this
3396 * function destructively reads the requested number of bytes from the
3397 * stream.
3398 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003399 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003400 * If this function returns an error status other than
3401 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003402 * state and must be aborted by calling psa_key_derivation_abort().
3403 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003404 * \param[in,out] operation The key derivation operation object to read from.
3405 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003406 * \param output_length Number of bytes to output.
3407 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003408 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003409 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003410 * The operation's capacity was less than
3411 * \p output_length bytes. Note that in this case,
3412 * no output is written to the output buffer.
3413 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003414 * subsequent calls to this function will not
3415 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003416 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003417 * The operation state is not valid (it must be active and completed
3418 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003419 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3420 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3421 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003422 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003423 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003424 * \retval #PSA_ERROR_BAD_STATE
3425 * The library has not been previously initialized by psa_crypto_init().
3426 * It is implementation-dependent whether a failure to initialize
3427 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003428 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003429psa_status_t psa_key_derivation_output_bytes(
3430 psa_key_derivation_operation_t *operation,
3431 uint8_t *output,
3432 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003433
Gilles Peskine35675b62019-05-16 17:26:11 +02003434/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003435 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003436 * This function calculates output bytes from a key derivation algorithm
3437 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003438 * The key's location, usage policy, type and size are taken from
3439 * \p attributes.
3440 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003441 * If you view the key derivation's output as a stream of bytes, this
3442 * function destructively reads as many bytes as required from the
3443 * stream.
3444 * The operation's capacity decreases by the number of bytes read.
3445 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003446 * If this function returns an error status other than
3447 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003448 * state and must be aborted by calling psa_key_derivation_abort().
3449 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003450 * How much output is produced and consumed from the operation, and how
3451 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003452 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003453 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003454 * of a given size, this function is functionally equivalent to
3455 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003456 * and passing the resulting output to #psa_import_key.
3457 * However, this function has a security benefit:
3458 * if the implementation provides an isolation boundary then
3459 * the key material is not exposed outside the isolation boundary.
3460 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003461 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003462 * The following key types defined in this specification follow this scheme:
3463 *
3464 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003465 * - #PSA_KEY_TYPE_ARC4;
3466 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003467 * - #PSA_KEY_TYPE_DERIVE;
3468 * - #PSA_KEY_TYPE_HMAC.
3469 *
3470 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003471 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003472 * Montgomery curve), this function always draws a byte string whose
3473 * length is determined by the curve, and sets the mandatory bits
3474 * accordingly. That is:
3475 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01003476 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003477 * string and process it as specified in RFC 7748 &sect;5.
Paul Elliott8ff510a2020-06-02 17:19:28 +01003478 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003479 * string and process it as specified in RFC 7748 &sect;5.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003480 *
3481 * - For key types for which the key is represented by a single sequence of
3482 * \p bits bits with constraints as to which bit sequences are acceptable,
3483 * this function draws a byte string of length (\p bits / 8) bytes rounded
3484 * up to the nearest whole number of bytes. If the resulting byte string
3485 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3486 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003487 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003488 * for the output produced by psa_export_key().
3489 * The following key types defined in this specification follow this scheme:
3490 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003491 * - #PSA_KEY_TYPE_DES.
3492 * Force-set the parity bits, but discard forbidden weak keys.
3493 * For 2-key and 3-key triple-DES, the three keys are generated
3494 * successively (for example, for 3-key triple-DES,
3495 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3496 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003497 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003498 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003499 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003500 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003501 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003502 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003503 * Weierstrass curve).
3504 * For these key types, interpret the byte string as integer
3505 * in big-endian order. Discard it if it is not in the range
3506 * [0, *N* - 2] where *N* is the boundary of the private key domain
3507 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003508 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003509 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003510 * This method allows compliance to NIST standards, specifically
3511 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003512 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3513 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3514 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3515 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003516 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003517 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003518 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003519 * implementation-defined.
3520 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003521 * In all cases, the data that is read is discarded from the operation.
3522 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003523 *
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003524 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3525 * the input to that step must be provided with psa_key_derivation_input_key().
3526 * Future versions of this specification may include additional restrictions
3527 * on the derived key based on the attributes and strength of the secret key.
3528 *
Gilles Peskine20628592019-04-19 19:29:50 +02003529 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003530 * \param[in,out] operation The key derivation operation object to read from.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003531 * \param[out] key On success, an identifier for the newly created
3532 * key. \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003533 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003534 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003535 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003536 * If the key is persistent, the key material and the key's metadata
3537 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003538 * \retval #PSA_ERROR_ALREADY_EXISTS
3539 * This is an attempt to create a persistent key, and there is
3540 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003541 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003542 * There was not enough data to create the desired key.
3543 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003544 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003545 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003546 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003547 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003548 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003549 * \retval #PSA_ERROR_INVALID_ARGUMENT
3550 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003551 * \retval #PSA_ERROR_NOT_PERMITTED
3552 * The #PSA_KEY_DERIVATION_INPUT_SECRET input was not provided through
3553 * a key.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003554 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003555 * The operation state is not valid (it must be active and completed
3556 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003557 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3558 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3559 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3560 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003561 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003562 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003563 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003564 * The library has not been previously initialized by psa_crypto_init().
3565 * It is implementation-dependent whether a failure to initialize
3566 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003567 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003568psa_status_t psa_key_derivation_output_key(
3569 const psa_key_attributes_t *attributes,
3570 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003571 mbedtls_svc_key_id_t *key);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003572
Gilles Peskine35675b62019-05-16 17:26:11 +02003573/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003574 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003575 * Aborting an operation frees all associated resources except for the \c
3576 * operation structure itself. Once aborted, the operation object can be reused
3577 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003578 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003579 * This function may be called at any time after the operation
3580 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003581 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003582 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3583 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003584 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003585 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003586 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003587 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003588 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3589 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003590 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003591 * \retval #PSA_ERROR_BAD_STATE
3592 * The library has not been previously initialized by psa_crypto_init().
3593 * It is implementation-dependent whether a failure to initialize
3594 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003595 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003596psa_status_t psa_key_derivation_abort(
3597 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003598
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003599/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003600 *
3601 * \warning The raw result of a key agreement algorithm such as finite-field
3602 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3603 * not be used directly as key material. It should instead be passed as
3604 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003605 * a key derivation, use psa_key_derivation_key_agreement() and other
3606 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003607 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003608 * \param alg The key agreement algorithm to compute
3609 * (\c PSA_ALG_XXX value such that
3610 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3611 * is true).
Ronald Croncf56a0a2020-08-04 09:51:30 +02003612 * \param private_key Identifier of the private key to use. It must
3613 * allow the usage PSA_KEY_USAGE_DERIVE.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003614 * \param[in] peer_key Public key of the peer. It must be
3615 * in the same format that psa_import_key()
3616 * accepts. The standard formats for public
3617 * keys are documented in the documentation
3618 * of psa_export_public_key().
3619 * \param peer_key_length Size of \p peer_key in bytes.
3620 * \param[out] output Buffer where the decrypted message is to
3621 * be written.
3622 * \param output_size Size of the \c output buffer in bytes.
3623 * \param[out] output_length On success, the number of bytes
3624 * that make up the returned output.
3625 *
3626 * \retval #PSA_SUCCESS
3627 * Success.
3628 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003629 * \retval #PSA_ERROR_NOT_PERMITTED
3630 * \retval #PSA_ERROR_INVALID_ARGUMENT
3631 * \p alg is not a key agreement algorithm
3632 * \retval #PSA_ERROR_INVALID_ARGUMENT
3633 * \p private_key is not compatible with \p alg,
3634 * or \p peer_key is not valid for \p alg or not compatible with
3635 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003636 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3637 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003638 * \retval #PSA_ERROR_NOT_SUPPORTED
3639 * \p alg is not a supported key agreement algorithm.
3640 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3641 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3642 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003643 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003644 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003645 * \retval #PSA_ERROR_BAD_STATE
3646 * The library has not been previously initialized by psa_crypto_init().
3647 * It is implementation-dependent whether a failure to initialize
3648 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003649 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003650psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003651 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02003652 const uint8_t *peer_key,
3653 size_t peer_key_length,
3654 uint8_t *output,
3655 size_t output_size,
3656 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003657
Gilles Peskineea0fb492018-07-12 17:17:20 +02003658/**@}*/
3659
Gilles Peskineedd76872018-07-20 17:42:05 +02003660/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003661 * @{
3662 */
3663
3664/**
3665 * \brief Generate random bytes.
3666 *
3667 * \warning This function **can** fail! Callers MUST check the return status
3668 * and MUST NOT use the content of the output buffer if the return
3669 * status is not #PSA_SUCCESS.
3670 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003671 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003672 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003673 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003674 * \param output_size Number of bytes to generate and output.
3675 *
Gilles Peskine28538492018-07-11 17:34:00 +02003676 * \retval #PSA_SUCCESS
3677 * \retval #PSA_ERROR_NOT_SUPPORTED
3678 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003679 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003680 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3681 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003682 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003683 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003684 * The library has not been previously initialized by psa_crypto_init().
3685 * It is implementation-dependent whether a failure to initialize
3686 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003687 */
3688psa_status_t psa_generate_random(uint8_t *output,
3689 size_t output_size);
3690
3691/**
3692 * \brief Generate a key or key pair.
3693 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003694 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02003695 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003696 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003697 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003698 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003699 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003700 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003701 * the public exponent is 65537.
3702 * The modulus is a product of two probabilistic primes
3703 * between 2^{n-1} and 2^n where n is the bit size specified in the
3704 * attributes.
3705 *
Gilles Peskine20628592019-04-19 19:29:50 +02003706 * \param[in] attributes The attributes for the new key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003707 * \param[out] key On success, an identifier for the newly created
3708 * key. \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003709 *
Gilles Peskine28538492018-07-11 17:34:00 +02003710 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003711 * Success.
3712 * If the key is persistent, the key material and the key's metadata
3713 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003714 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003715 * This is an attempt to create a persistent key, and there is
3716 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003717 * \retval #PSA_ERROR_NOT_SUPPORTED
3718 * \retval #PSA_ERROR_INVALID_ARGUMENT
3719 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3720 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3721 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3722 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003723 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003724 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3725 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003726 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003727 * The library has not been previously initialized by psa_crypto_init().
3728 * It is implementation-dependent whether a failure to initialize
3729 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003730 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003731psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003732 mbedtls_svc_key_id_t *key);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003733
3734/**@}*/
3735
Gilles Peskinee59236f2018-01-27 23:32:46 +01003736#ifdef __cplusplus
3737}
3738#endif
3739
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003740/* The file "crypto_sizes.h" contains definitions for size calculation
3741 * macros whose definitions are implementation-specific. */
3742#include "crypto_sizes.h"
3743
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003744/* The file "crypto_struct.h" contains definitions for
3745 * implementation-specific structs that are declared above. */
3746#include "crypto_struct.h"
3747
3748/* The file "crypto_extra.h" contains vendor-specific definitions. This
3749 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003750#include "crypto_extra.h"
3751
3752#endif /* PSA_CRYPTO_H */