blob: 3c2324ac9de06deb80ce69c7bb9985a819c5215a [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
Ronald Cron96783552020-10-19 12:06:30 +0200470 * #PSA_KEY_USAGE_COPY. If a private or secret key is
Ronald Croncf56a0a2020-08-04 09:51:30 +0200471 * being copied outside of a secure element it must
Ronald Cron96783552020-10-19 12:06:30 +0200472 * 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
Ronald Cron4067d1c2020-10-19 13:34:38 +0200487 * key. For persistent keys, this is the key
488 * identifier defined in \p attributes.
489 * \c 0 on failure.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100490 *
491 * \retval #PSA_SUCCESS
492 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200493 * \p source_key is invalid.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100494 * \retval #PSA_ERROR_ALREADY_EXISTS
495 * This is an attempt to create a persistent key, and there is
496 * already a persistent key with the given identifier.
497 * \retval #PSA_ERROR_INVALID_ARGUMENT
498 * The lifetime or identifier in \p attributes are invalid.
499 * \retval #PSA_ERROR_INVALID_ARGUMENT
500 * The policy constraints on the source and specified in
501 * \p attributes are incompatible.
502 * \retval #PSA_ERROR_INVALID_ARGUMENT
503 * \p attributes specifies a key type or key size
504 * which does not match the attributes of the source key.
505 * \retval #PSA_ERROR_NOT_PERMITTED
506 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
507 * \retval #PSA_ERROR_NOT_PERMITTED
508 * The source key is not exportable and its lifetime does not
509 * allow copying it to the target's lifetime.
510 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
511 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
512 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
513 * \retval #PSA_ERROR_HARDWARE_FAILURE
514 * \retval #PSA_ERROR_STORAGE_FAILURE
515 * \retval #PSA_ERROR_CORRUPTION_DETECTED
516 * \retval #PSA_ERROR_BAD_STATE
517 * The library has not been previously initialized by psa_crypto_init().
518 * It is implementation-dependent whether a failure to initialize
519 * results in this error code.
520 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200521psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100522 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200523 mbedtls_svc_key_id_t *target_key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100524
525
526/**
527 * \brief Destroy a key.
528 *
529 * This function destroys a key from both volatile
530 * memory and, if applicable, non-volatile storage. Implementations shall
531 * make a best effort to ensure that that the key material cannot be recovered.
532 *
533 * This function also erases any metadata such as policies and frees
Ronald Croncf56a0a2020-08-04 09:51:30 +0200534 * resources associated with the key.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100535 *
536 * If a key is currently in use in a multipart operation, then destroying the
537 * key will cause the multipart operation to fail.
538 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200539 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
Ronald Cron96783552020-10-19 12:06:30 +0200540 * return #PSA_SUCCESS.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100541 *
542 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +0200543 * \p key was a valid identifier and the key material that it
544 * referred to has been erased. Alternatively, \p key is \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100545 * \retval #PSA_ERROR_NOT_PERMITTED
546 * The key cannot be erased because it is
547 * read-only, either due to a policy or due to physical restrictions.
548 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200549 * \p key is not a valid identifier nor \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100550 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
551 * There was an failure in communication with the cryptoprocessor.
552 * The key material may still be present in the cryptoprocessor.
553 * \retval #PSA_ERROR_STORAGE_FAILURE
554 * The storage is corrupted. Implementations shall make a best effort
555 * to erase key material even in this stage, however applications
556 * should be aware that it may be impossible to guarantee that the
557 * key material is not recoverable in such cases.
558 * \retval #PSA_ERROR_CORRUPTION_DETECTED
559 * An unexpected condition which is not a storage corruption or
560 * a communication failure occurred. The cryptoprocessor may have
561 * been compromised.
562 * \retval #PSA_ERROR_BAD_STATE
563 * The library has not been previously initialized by psa_crypto_init().
564 * It is implementation-dependent whether a failure to initialize
565 * results in this error code.
566 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200567psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100568
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100569/**@}*/
570
571/** \defgroup import_export Key import and export
572 * @{
573 */
574
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100575/**
576 * \brief Import a key in binary format.
577 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100578 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100579 * documentation of psa_export_public_key() for the format of public keys
580 * and to the documentation of psa_export_key() for the format for
581 * other key types.
582 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200583 * The key data determines the key size. The attributes may optionally
584 * specify a key size; in this case it must match the size determined
585 * from the key data. A key size of 0 in \p attributes indicates that
586 * the key size is solely determined by the key data.
587 *
588 * Implementations must reject an attempt to import a key of size 0.
589 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100590 * This specification supports a single format for each key type.
591 * Implementations may support other formats as long as the standard
592 * format is supported. Implementations that support other formats
593 * should ensure that the formats are clearly unambiguous so as to
594 * minimize the risk that an invalid input is accidentally interpreted
595 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100596 *
Gilles Peskine20628592019-04-19 19:29:50 +0200597 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200598 * The key size is always determined from the
599 * \p data buffer.
600 * If the key size in \p attributes is nonzero,
601 * it must be equal to the size from \p data.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200602 * \param[out] key On success, an identifier to the newly created key.
Ronald Cron4067d1c2020-10-19 13:34:38 +0200603 * For persistent keys, this is the key identifier
604 * defined in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200605 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100606 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200607 * buffer is interpreted according to the type declared
608 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200609 * All implementations must support at least the format
610 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100611 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200612 * the chosen type. Implementations may allow other
613 * formats, but should be conservative: implementations
614 * should err on the side of rejecting content if it
615 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200616 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100617 *
Gilles Peskine28538492018-07-11 17:34:00 +0200618 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100619 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100620 * If the key is persistent, the key material and the key's metadata
621 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200622 * \retval #PSA_ERROR_ALREADY_EXISTS
623 * This is an attempt to create a persistent key, and there is
624 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200625 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200626 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200627 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200628 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200629 * The key attributes, as a whole, are invalid.
630 * \retval #PSA_ERROR_INVALID_ARGUMENT
631 * The key data is not correctly formatted.
632 * \retval #PSA_ERROR_INVALID_ARGUMENT
633 * The size in \p attributes is nonzero and does not match the size
634 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200635 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
636 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
637 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100638 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200639 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200640 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300641 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300642 * The library has not been previously initialized by psa_crypto_init().
643 * It is implementation-dependent whether a failure to initialize
644 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100645 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200646psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100647 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200648 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200649 mbedtls_svc_key_id_t *key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100650
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100651
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100652
653/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100654 * \brief Export a key in binary format.
655 *
656 * The output of this function can be passed to psa_import_key() to
657 * create an equivalent object.
658 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100659 * If the implementation of psa_import_key() supports other formats
660 * beyond the format specified here, the output from psa_export_key()
661 * must use the representation specified here, not the original
662 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100663 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100664 * For standard key types, the output format is as follows:
665 *
666 * - For symmetric keys (including MAC keys), the format is the
667 * raw bytes of the key.
668 * - For DES, the key data consists of 8 bytes. The parity bits must be
669 * correct.
670 * - For Triple-DES, the format is the concatenation of the
671 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200672 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200673 * is the non-encrypted DER encoding of the representation defined by
674 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
675 * ```
676 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200677 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200678 * modulus INTEGER, -- n
679 * publicExponent INTEGER, -- e
680 * privateExponent INTEGER, -- d
681 * prime1 INTEGER, -- p
682 * prime2 INTEGER, -- q
683 * exponent1 INTEGER, -- d mod (p-1)
684 * exponent2 INTEGER, -- d mod (q-1)
685 * coefficient INTEGER, -- (inverse of q) mod p
686 * }
687 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200688 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200689 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100690 * a representation of the private value as a `ceiling(m/8)`-byte string
691 * where `m` is the bit size associated with the curve, i.e. the bit size
692 * of the order of the curve's coordinate field. This byte string is
693 * in little-endian order for Montgomery curves (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +0100694 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
695 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
696 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
Steven Cooreman6f5cc712020-06-11 16:40:41 +0200697 * For Weierstrass curves, this is the content of the `privateKey` field of
698 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
699 * the format is defined by RFC 7748, and output is masked according to §5.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200700 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200701 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000702 * format is the representation of the private key `x` as a big-endian byte
703 * string. The length of the byte string is the private key size in bytes
704 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200705 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
706 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100707 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200708 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
709 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200710 * \param key Identifier of the key to export. It must allow the
Ronald Cron96783552020-10-19 12:06:30 +0200711 * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
Ronald Croncf56a0a2020-08-04 09:51:30 +0200712 * key.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200713 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200714 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200715 * \param[out] data_length On success, the number of bytes
716 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100717 *
Gilles Peskine28538492018-07-11 17:34:00 +0200718 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100719 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200720 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200721 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100722 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200723 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
724 * The size of the \p data buffer is too small. You can determine a
725 * sufficient buffer size by calling
726 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
727 * where \c type is the key type
728 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200729 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
730 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200731 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100732 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100733 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300734 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300735 * The library has not been previously initialized by psa_crypto_init().
736 * It is implementation-dependent whether a failure to initialize
737 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200739psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 uint8_t *data,
741 size_t data_size,
742 size_t *data_length);
743
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100744/**
745 * \brief Export a public key or the public part of a key pair in binary format.
746 *
747 * The output of this function can be passed to psa_import_key() to
748 * create an object that is equivalent to the public key.
749 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000750 * This specification supports a single format for each key type.
751 * Implementations may support other formats as long as the standard
752 * format is supported. Implementations that support other formats
753 * should ensure that the formats are clearly unambiguous so as to
754 * minimize the risk that an invalid input is accidentally interpreted
755 * according to a different format.
756 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000757 * For standard key types, the output format is as follows:
758 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
759 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
760 * ```
761 * RSAPublicKey ::= SEQUENCE {
762 * modulus INTEGER, -- n
763 * publicExponent INTEGER } -- e
764 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000765 * - For elliptic curve public keys (key types for which
766 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
767 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
768 * Let `m` be the bit size associated with the curve, i.e. the bit size of
769 * `q` for a curve over `F_q`. The representation consists of:
770 * - The byte 0x04;
771 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
772 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200773 * - For Diffie-Hellman key exchange public keys (key types for which
774 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000775 * the format is the representation of the public key `y = g^x mod p` as a
776 * big-endian byte string. The length of the byte string is the length of the
777 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100778 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200779 * Exporting a public key object or the public part of a key pair is
780 * always permitted, regardless of the key's usage flags.
781 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200782 * \param key Identifier of the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200783 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200784 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200785 * \param[out] data_length On success, the number of bytes
786 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100787 *
Gilles Peskine28538492018-07-11 17:34:00 +0200788 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100789 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200790 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200791 * The key is neither a public key nor a key pair.
792 * \retval #PSA_ERROR_NOT_SUPPORTED
793 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
794 * The size of the \p data buffer is too small. You can determine a
795 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200796 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200797 * where \c type is the key type
798 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200799 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
800 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200801 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100802 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100803 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300804 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300805 * The library has not been previously initialized by psa_crypto_init().
806 * It is implementation-dependent whether a failure to initialize
807 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100808 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200809psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100810 uint8_t *data,
811 size_t data_size,
812 size_t *data_length);
813
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100814
Gilles Peskine20035e32018-02-03 22:44:14 +0100815
816/**@}*/
817
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100818/** \defgroup hash Message digests
819 * @{
820 */
821
Gilles Peskine69647a42019-01-14 20:18:12 +0100822/** Calculate the hash (digest) of a message.
823 *
824 * \note To verify the hash of a message against an
825 * expected value, use psa_hash_compare() instead.
826 *
827 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
828 * such that #PSA_ALG_IS_HASH(\p alg) is true).
829 * \param[in] input Buffer containing the message to hash.
830 * \param input_length Size of the \p input buffer in bytes.
831 * \param[out] hash Buffer where the hash is to be written.
832 * \param hash_size Size of the \p hash buffer in bytes.
833 * \param[out] hash_length On success, the number of bytes
834 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100835 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100836 *
837 * \retval #PSA_SUCCESS
838 * Success.
839 * \retval #PSA_ERROR_NOT_SUPPORTED
840 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100841 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100842 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
843 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100844 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
845 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
846 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200847 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100848 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100849 * \retval #PSA_ERROR_BAD_STATE
850 * The library has not been previously initialized by psa_crypto_init().
851 * It is implementation-dependent whether a failure to initialize
852 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100853 */
854psa_status_t psa_hash_compute(psa_algorithm_t alg,
855 const uint8_t *input,
856 size_t input_length,
857 uint8_t *hash,
858 size_t hash_size,
859 size_t *hash_length);
860
861/** Calculate the hash (digest) of a message and compare it with a
862 * reference value.
863 *
864 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
865 * such that #PSA_ALG_IS_HASH(\p alg) is true).
866 * \param[in] input Buffer containing the message to hash.
867 * \param input_length Size of the \p input buffer in bytes.
868 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100869 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100870 *
871 * \retval #PSA_SUCCESS
872 * The expected hash is identical to the actual hash of the input.
873 * \retval #PSA_ERROR_INVALID_SIGNATURE
874 * The hash of the message was calculated successfully, but it
875 * differs from the expected hash.
876 * \retval #PSA_ERROR_NOT_SUPPORTED
877 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100878 * \retval #PSA_ERROR_INVALID_ARGUMENT
879 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100880 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
881 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
882 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200883 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100884 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100885 * \retval #PSA_ERROR_BAD_STATE
886 * The library has not been previously initialized by psa_crypto_init().
887 * It is implementation-dependent whether a failure to initialize
888 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100889 */
890psa_status_t psa_hash_compare(psa_algorithm_t alg,
891 const uint8_t *input,
892 size_t input_length,
893 const uint8_t *hash,
Gilles Peskinefa710f52019-12-02 14:31:48 +0100894 size_t hash_length);
Gilles Peskine69647a42019-01-14 20:18:12 +0100895
Gilles Peskine308b91d2018-02-08 09:47:44 +0100896/** The type of the state data structure for multipart hash operations.
897 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000898 * Before calling any function on a hash operation object, the application must
899 * initialize it by any of the following means:
900 * - Set the structure to all-bits-zero, for example:
901 * \code
902 * psa_hash_operation_t operation;
903 * memset(&operation, 0, sizeof(operation));
904 * \endcode
905 * - Initialize the structure to logical zero values, for example:
906 * \code
907 * psa_hash_operation_t operation = {0};
908 * \endcode
909 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
910 * for example:
911 * \code
912 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
913 * \endcode
914 * - Assign the result of the function psa_hash_operation_init()
915 * to the structure, for example:
916 * \code
917 * psa_hash_operation_t operation;
918 * operation = psa_hash_operation_init();
919 * \endcode
920 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100921 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100922 * make any assumptions about the content of this structure except
923 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100924typedef struct psa_hash_operation_s psa_hash_operation_t;
925
Jaeden Amero6a25b412019-01-04 11:47:44 +0000926/** \def PSA_HASH_OPERATION_INIT
927 *
928 * This macro returns a suitable initializer for a hash operation object
929 * of type #psa_hash_operation_t.
930 */
931#ifdef __DOXYGEN_ONLY__
932/* This is an example definition for documentation purposes.
933 * Implementations should define a suitable value in `crypto_struct.h`.
934 */
935#define PSA_HASH_OPERATION_INIT {0}
936#endif
937
938/** Return an initial value for a hash operation object.
939 */
940static psa_hash_operation_t psa_hash_operation_init(void);
941
Gilles Peskinef45adda2019-01-14 18:29:18 +0100942/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100943 *
944 * The sequence of operations to calculate a hash (message digest)
945 * is as follows:
946 * -# Allocate an operation object which will be passed to all the functions
947 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000948 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100949 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200950 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100951 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100952 * of the message each time. The hash that is calculated is the hash
953 * of the concatenation of these messages in order.
954 * -# To calculate the hash, call psa_hash_finish().
955 * To compare the hash with an expected value, call psa_hash_verify().
956 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100957 * If an error occurs at any step after a call to psa_hash_setup(), the
958 * operation will need to be reset by a call to psa_hash_abort(). The
959 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000960 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100961 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200962 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100963 * eventually terminate the operation. The following events terminate an
964 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100965 * - A successful call to psa_hash_finish() or psa_hash_verify().
966 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100967 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000968 * \param[in,out] operation The operation object to set up. It must have
969 * been initialized as per the documentation for
970 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200971 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
972 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100973 *
Gilles Peskine28538492018-07-11 17:34:00 +0200974 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100975 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200976 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100977 * \p alg is not a supported hash algorithm.
978 * \retval #PSA_ERROR_INVALID_ARGUMENT
979 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100980 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100981 * The operation state is not valid (it must be inactive).
Gilles Peskine28538492018-07-11 17:34:00 +0200982 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
983 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
984 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200985 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100986 * \retval #PSA_ERROR_BAD_STATE
987 * The library has not been previously initialized by psa_crypto_init().
988 * It is implementation-dependent whether a failure to initialize
989 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100990 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200991psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100992 psa_algorithm_t alg);
993
Gilles Peskine308b91d2018-02-08 09:47:44 +0100994/** Add a message fragment to a multipart hash operation.
995 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200996 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100997 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100998 * If this function returns an error status, the operation enters an error
999 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001000 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001001 * \param[in,out] operation Active hash operation.
1002 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001003 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001004 *
Gilles Peskine28538492018-07-11 17:34:00 +02001005 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001006 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001007 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001008 * The operation state is not valid (it muct be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001009 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1010 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1011 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001012 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001013 * \retval #PSA_ERROR_BAD_STATE
1014 * The library has not been previously initialized by psa_crypto_init().
1015 * It is implementation-dependent whether a failure to initialize
1016 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001017 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001018psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1019 const uint8_t *input,
1020 size_t input_length);
1021
Gilles Peskine308b91d2018-02-08 09:47:44 +01001022/** Finish the calculation of the hash of a message.
1023 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001024 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001025 * This function calculates the hash of the message formed by concatenating
1026 * the inputs passed to preceding calls to psa_hash_update().
1027 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001028 * When this function returns successfuly, the operation becomes inactive.
1029 * If this function returns an error status, the operation enters an error
1030 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001031 *
1032 * \warning Applications should not call this function if they expect
1033 * a specific value for the hash. Call psa_hash_verify() instead.
1034 * Beware that comparing integrity or authenticity data such as
1035 * hash values with a function such as \c memcmp is risky
1036 * because the time taken by the comparison may leak information
1037 * about the hashed data which could allow an attacker to guess
1038 * a valid hash and thereby bypass security controls.
1039 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001040 * \param[in,out] operation Active hash operation.
1041 * \param[out] hash Buffer where the hash is to be written.
1042 * \param hash_size Size of the \p hash buffer in bytes.
1043 * \param[out] hash_length On success, the number of bytes
1044 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001045 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001046 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001047 *
Gilles Peskine28538492018-07-11 17:34:00 +02001048 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001049 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001050 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001051 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001052 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001053 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001054 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001055 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001056 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1057 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1058 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001059 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001060 * \retval #PSA_ERROR_BAD_STATE
1061 * The library has not been previously initialized by psa_crypto_init().
1062 * It is implementation-dependent whether a failure to initialize
1063 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001064 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001065psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1066 uint8_t *hash,
1067 size_t hash_size,
1068 size_t *hash_length);
1069
Gilles Peskine308b91d2018-02-08 09:47:44 +01001070/** Finish the calculation of the hash of a message and compare it with
1071 * an expected value.
1072 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001073 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074 * This function calculates the hash of the message formed by concatenating
1075 * the inputs passed to preceding calls to psa_hash_update(). It then
1076 * compares the calculated hash with the expected hash passed as a
1077 * parameter to this function.
1078 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001079 * When this function returns successfuly, the operation becomes inactive.
1080 * If this function returns an error status, the operation enters an error
1081 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001082 *
Gilles Peskine19067982018-03-20 17:54:53 +01001083 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001084 * comparison between the actual hash and the expected hash is performed
1085 * in constant time.
1086 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001087 * \param[in,out] operation Active hash operation.
1088 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001089 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001090 *
Gilles Peskine28538492018-07-11 17:34:00 +02001091 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001092 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001093 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001094 * The hash of the message was calculated successfully, but it
1095 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001096 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001097 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001098 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1099 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1100 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001101 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001102 * \retval #PSA_ERROR_BAD_STATE
1103 * The library has not been previously initialized by psa_crypto_init().
1104 * It is implementation-dependent whether a failure to initialize
1105 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001106 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001107psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1108 const uint8_t *hash,
1109 size_t hash_length);
1110
Gilles Peskine308b91d2018-02-08 09:47:44 +01001111/** Abort a hash operation.
1112 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001113 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001114 * \p operation structure itself. Once aborted, the operation object
1115 * can be reused for another operation by calling
1116 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001117 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001118 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001119 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001120 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001121 * In particular, calling psa_hash_abort() after the operation has been
1122 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1123 * psa_hash_verify() is safe and has no effect.
1124 *
1125 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001126 *
Gilles Peskine28538492018-07-11 17:34:00 +02001127 * \retval #PSA_SUCCESS
Gilles Peskine28538492018-07-11 17:34:00 +02001128 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1129 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001130 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001131 * \retval #PSA_ERROR_BAD_STATE
1132 * The library has not been previously initialized by psa_crypto_init().
1133 * It is implementation-dependent whether a failure to initialize
1134 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001135 */
1136psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001137
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001138/** Clone a hash operation.
1139 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001140 * This function copies the state of an ongoing hash operation to
1141 * a new operation object. In other words, this function is equivalent
1142 * to calling psa_hash_setup() on \p target_operation with the same
1143 * algorithm that \p source_operation was set up for, then
1144 * psa_hash_update() on \p target_operation with the same input that
1145 * that was passed to \p source_operation. After this function returns, the
1146 * two objects are independent, i.e. subsequent calls involving one of
1147 * the objects do not affect the other object.
1148 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001149 * \param[in] source_operation The active hash operation to clone.
1150 * \param[in,out] target_operation The operation object to set up.
1151 * It must be initialized but not active.
1152 *
1153 * \retval #PSA_SUCCESS
1154 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001155 * The \p source_operation state is not valid (it must be active).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001156 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001157 * The \p target_operation state is not valid (it must be inactive).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001158 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1159 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001160 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001161 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001162 * \retval #PSA_ERROR_BAD_STATE
1163 * The library has not been previously initialized by psa_crypto_init().
1164 * It is implementation-dependent whether a failure to initialize
1165 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001166 */
1167psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1168 psa_hash_operation_t *target_operation);
1169
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001170/**@}*/
1171
Gilles Peskine8c9def32018-02-08 10:02:12 +01001172/** \defgroup MAC Message authentication codes
1173 * @{
1174 */
1175
Gilles Peskine69647a42019-01-14 20:18:12 +01001176/** Calculate the MAC (message authentication code) of a message.
1177 *
1178 * \note To verify the MAC of a message against an
1179 * expected value, use psa_mac_verify() instead.
1180 * Beware that comparing integrity or authenticity data such as
1181 * MAC values with a function such as \c memcmp is risky
1182 * because the time taken by the comparison may leak information
1183 * about the MAC value which could allow an attacker to guess
1184 * a valid MAC and thereby bypass security controls.
1185 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001186 * \param key Identifier of the key to use for the operation. It
1187 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001188 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001189 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001190 * \param[in] input Buffer containing the input message.
1191 * \param input_length Size of the \p input buffer in bytes.
1192 * \param[out] mac Buffer where the MAC value is to be written.
1193 * \param mac_size Size of the \p mac buffer in bytes.
1194 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001195 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001196 *
1197 * \retval #PSA_SUCCESS
1198 * Success.
1199 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001200 * \retval #PSA_ERROR_NOT_PERMITTED
1201 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001202 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001203 * \retval #PSA_ERROR_NOT_SUPPORTED
1204 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001205 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1206 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001207 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1208 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1209 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001210 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001211 * \retval #PSA_ERROR_STORAGE_FAILURE
1212 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001213 * \retval #PSA_ERROR_BAD_STATE
1214 * The library has not been previously initialized by psa_crypto_init().
1215 * It is implementation-dependent whether a failure to initialize
1216 * results in this error code.
1217 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001218psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001219 psa_algorithm_t alg,
1220 const uint8_t *input,
1221 size_t input_length,
1222 uint8_t *mac,
1223 size_t mac_size,
1224 size_t *mac_length);
1225
1226/** Calculate the MAC of a message and compare it with a reference value.
1227 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001228 * \param key Identifier of the key to use for the operation. It
1229 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001230 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001231 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001232 * \param[in] input Buffer containing the input message.
1233 * \param input_length Size of the \p input buffer in bytes.
1234 * \param[out] mac Buffer containing the expected MAC value.
1235 * \param mac_length Size of the \p mac buffer in bytes.
1236 *
1237 * \retval #PSA_SUCCESS
1238 * The expected MAC is identical to the actual MAC of the input.
1239 * \retval #PSA_ERROR_INVALID_SIGNATURE
1240 * The MAC of the message was calculated successfully, but it
1241 * differs from the expected value.
1242 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001243 * \retval #PSA_ERROR_NOT_PERMITTED
1244 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001245 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001246 * \retval #PSA_ERROR_NOT_SUPPORTED
1247 * \p alg is not supported or is not a MAC algorithm.
1248 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1249 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1250 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001251 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001252 * \retval #PSA_ERROR_STORAGE_FAILURE
1253 * The key could not be retrieved from storage.
1254 * \retval #PSA_ERROR_BAD_STATE
1255 * The library has not been previously initialized by psa_crypto_init().
1256 * It is implementation-dependent whether a failure to initialize
1257 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001258 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001259psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
Gilles Peskinea05602d2019-01-17 15:25:52 +01001260 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001261 const uint8_t *input,
1262 size_t input_length,
1263 const uint8_t *mac,
Gilles Peskine13faa2d2020-01-30 16:32:21 +01001264 size_t mac_length);
Gilles Peskine69647a42019-01-14 20:18:12 +01001265
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001266/** The type of the state data structure for multipart MAC operations.
1267 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001268 * Before calling any function on a MAC operation object, the application must
1269 * initialize it by any of the following means:
1270 * - Set the structure to all-bits-zero, for example:
1271 * \code
1272 * psa_mac_operation_t operation;
1273 * memset(&operation, 0, sizeof(operation));
1274 * \endcode
1275 * - Initialize the structure to logical zero values, for example:
1276 * \code
1277 * psa_mac_operation_t operation = {0};
1278 * \endcode
1279 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1280 * for example:
1281 * \code
1282 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1283 * \endcode
1284 * - Assign the result of the function psa_mac_operation_init()
1285 * to the structure, for example:
1286 * \code
1287 * psa_mac_operation_t operation;
1288 * operation = psa_mac_operation_init();
1289 * \endcode
1290 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001291 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001292 * make any assumptions about the content of this structure except
1293 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001294typedef struct psa_mac_operation_s psa_mac_operation_t;
1295
Jaeden Amero769ce272019-01-04 11:48:03 +00001296/** \def PSA_MAC_OPERATION_INIT
1297 *
1298 * This macro returns a suitable initializer for a MAC operation object of type
1299 * #psa_mac_operation_t.
1300 */
1301#ifdef __DOXYGEN_ONLY__
1302/* This is an example definition for documentation purposes.
1303 * Implementations should define a suitable value in `crypto_struct.h`.
1304 */
1305#define PSA_MAC_OPERATION_INIT {0}
1306#endif
1307
1308/** Return an initial value for a MAC operation object.
1309 */
1310static psa_mac_operation_t psa_mac_operation_init(void);
1311
Gilles Peskinef45adda2019-01-14 18:29:18 +01001312/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001313 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001314 * This function sets up the calculation of the MAC
1315 * (message authentication code) of a byte string.
1316 * To verify the MAC of a message against an
1317 * expected value, use psa_mac_verify_setup() instead.
1318 *
1319 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001320 * -# Allocate an operation object which will be passed to all the functions
1321 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001322 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001323 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001324 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001325 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1326 * of the message each time. The MAC that is calculated is the MAC
1327 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001328 * -# At the end of the message, call psa_mac_sign_finish() to finish
1329 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001330 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001331 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1332 * operation will need to be reset by a call to psa_mac_abort(). The
1333 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001334 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001335 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001336 * After a successful call to psa_mac_sign_setup(), the application must
1337 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001338 * - A successful call to psa_mac_sign_finish().
1339 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001340 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001341 * \param[in,out] operation The operation object to set up. It must have
1342 * been initialized as per the documentation for
1343 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001344 * \param key Identifier of the key to use for the operation. It
1345 * must remain valid until the operation terminates.
1346 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001347 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001348 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001349 *
Gilles Peskine28538492018-07-11 17:34:00 +02001350 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001351 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001352 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001353 * \retval #PSA_ERROR_NOT_PERMITTED
1354 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001355 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001356 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001357 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001358 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1359 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1360 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001361 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001362 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001363 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001364 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001365 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001366 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001367 * The library has not been previously initialized by psa_crypto_init().
1368 * It is implementation-dependent whether a failure to initialize
1369 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001370 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001371psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001372 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001373 psa_algorithm_t alg);
1374
Gilles Peskinef45adda2019-01-14 18:29:18 +01001375/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001376 *
1377 * This function sets up the verification of the MAC
1378 * (message authentication code) of a byte string against an expected value.
1379 *
1380 * The sequence of operations to verify a MAC is as follows:
1381 * -# Allocate an operation object which will be passed to all the functions
1382 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001383 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001384 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001385 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001386 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1387 * of the message each time. The MAC that is calculated is the MAC
1388 * of the concatenation of these messages in order.
1389 * -# At the end of the message, call psa_mac_verify_finish() to finish
1390 * calculating the actual MAC of the message and verify it against
1391 * the expected value.
1392 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001393 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1394 * operation will need to be reset by a call to psa_mac_abort(). The
1395 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001396 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001397 *
1398 * After a successful call to psa_mac_verify_setup(), the application must
1399 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001400 * - A successful call to psa_mac_verify_finish().
1401 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001402 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001403 * \param[in,out] operation The operation object to set up. It must have
1404 * been initialized as per the documentation for
1405 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001406 * \param key Identifier of the key to use for the operation. It
1407 * must remain valid until the operation terminates.
1408 * It must allow the usage
1409 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001410 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1411 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001412 *
Gilles Peskine28538492018-07-11 17:34:00 +02001413 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001414 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001415 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001416 * \retval #PSA_ERROR_NOT_PERMITTED
1417 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001418 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001419 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001420 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001421 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1422 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1423 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001424 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001425 * \retval #PSA_ERROR_STORAGE_FAILURE
1426 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001427 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001428 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001429 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001430 * The library has not been previously initialized by psa_crypto_init().
1431 * It is implementation-dependent whether a failure to initialize
1432 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001433 */
1434psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001435 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001436 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001437
Gilles Peskinedcd14942018-07-12 00:30:52 +02001438/** Add a message fragment to a multipart MAC operation.
1439 *
1440 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1441 * before calling this function.
1442 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001443 * If this function returns an error status, the operation enters an error
1444 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001445 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001446 * \param[in,out] operation Active MAC operation.
1447 * \param[in] input Buffer containing the message fragment to add to
1448 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001449 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001450 *
1451 * \retval #PSA_SUCCESS
1452 * Success.
1453 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001454 * The operation state is not valid (it must be active).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001455 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1456 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1457 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001458 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001459 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001460 * \retval #PSA_ERROR_BAD_STATE
1461 * The library has not been previously initialized by psa_crypto_init().
1462 * It is implementation-dependent whether a failure to initialize
1463 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001464 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001465psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1466 const uint8_t *input,
1467 size_t input_length);
1468
Gilles Peskinedcd14942018-07-12 00:30:52 +02001469/** Finish the calculation of the MAC of a message.
1470 *
1471 * The application must call psa_mac_sign_setup() before calling this function.
1472 * This function calculates the MAC of the message formed by concatenating
1473 * the inputs passed to preceding calls to psa_mac_update().
1474 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001475 * When this function returns successfuly, the operation becomes inactive.
1476 * If this function returns an error status, the operation enters an error
1477 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001478 *
1479 * \warning Applications should not call this function if they expect
1480 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1481 * Beware that comparing integrity or authenticity data such as
1482 * MAC values with a function such as \c memcmp is risky
1483 * because the time taken by the comparison may leak information
1484 * about the MAC value which could allow an attacker to guess
1485 * a valid MAC and thereby bypass security controls.
1486 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001487 * \param[in,out] operation Active MAC operation.
1488 * \param[out] mac Buffer where the MAC value is to be written.
1489 * \param mac_size Size of the \p mac buffer in bytes.
1490 * \param[out] mac_length On success, the number of bytes
1491 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001492 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001493 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001494 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001495 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001496 *
1497 * \retval #PSA_SUCCESS
1498 * Success.
1499 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001500 * The operation state is not valid (it must be an active mac sign
1501 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001502 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001503 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001504 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1505 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1506 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1507 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001508 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001509 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001510 * \retval #PSA_ERROR_BAD_STATE
1511 * The library has not been previously initialized by psa_crypto_init().
1512 * It is implementation-dependent whether a failure to initialize
1513 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001514 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001515psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1516 uint8_t *mac,
1517 size_t mac_size,
1518 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001519
Gilles Peskinedcd14942018-07-12 00:30:52 +02001520/** Finish the calculation of the MAC of a message and compare it with
1521 * an expected value.
1522 *
1523 * The application must call psa_mac_verify_setup() before calling this function.
1524 * This function calculates the MAC of the message formed by concatenating
1525 * the inputs passed to preceding calls to psa_mac_update(). It then
1526 * compares the calculated MAC with the expected MAC passed as a
1527 * parameter to this function.
1528 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001529 * When this function returns successfuly, the operation becomes inactive.
1530 * If this function returns an error status, the operation enters an error
1531 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001532 *
1533 * \note Implementations shall make the best effort to ensure that the
1534 * comparison between the actual MAC and the expected MAC is performed
1535 * in constant time.
1536 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001537 * \param[in,out] operation Active MAC operation.
1538 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001539 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001540 *
1541 * \retval #PSA_SUCCESS
1542 * The expected MAC is identical to the actual MAC of the message.
1543 * \retval #PSA_ERROR_INVALID_SIGNATURE
1544 * The MAC of the message was calculated successfully, but it
1545 * differs from the expected MAC.
1546 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001547 * The operation state is not valid (it must be an active mac verify
1548 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001549 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1550 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1551 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001552 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001553 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001554 * \retval #PSA_ERROR_BAD_STATE
1555 * The library has not been previously initialized by psa_crypto_init().
1556 * It is implementation-dependent whether a failure to initialize
1557 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001558 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001559psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1560 const uint8_t *mac,
1561 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001562
Gilles Peskinedcd14942018-07-12 00:30:52 +02001563/** Abort a MAC operation.
1564 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001565 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001566 * \p operation structure itself. Once aborted, the operation object
1567 * can be reused for another operation by calling
1568 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001569 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001570 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001571 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001572 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001573 * In particular, calling psa_mac_abort() after the operation has been
1574 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1575 * psa_mac_verify_finish() is safe and has no effect.
1576 *
1577 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001578 *
1579 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02001580 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1581 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001582 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001583 * \retval #PSA_ERROR_BAD_STATE
1584 * The library has not been previously initialized by psa_crypto_init().
1585 * It is implementation-dependent whether a failure to initialize
1586 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001587 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001588psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1589
1590/**@}*/
1591
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001592/** \defgroup cipher Symmetric ciphers
1593 * @{
1594 */
1595
Gilles Peskine69647a42019-01-14 20:18:12 +01001596/** Encrypt a message using a symmetric cipher.
1597 *
1598 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001599 * vector). Use the multipart operation interface with a
1600 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001601 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001602 * \param key Identifier of the key to use for the operation.
Ronald Cron96783552020-10-19 12:06:30 +02001603 * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001604 * \param alg The cipher algorithm to compute
1605 * (\c PSA_ALG_XXX value such that
1606 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1607 * \param[in] input Buffer containing the message to encrypt.
1608 * \param input_length Size of the \p input buffer in bytes.
1609 * \param[out] output Buffer where the output is to be written.
1610 * The output contains the IV followed by
1611 * the ciphertext proper.
1612 * \param output_size Size of the \p output buffer in bytes.
1613 * \param[out] output_length On success, the number of bytes
1614 * that make up the output.
1615 *
1616 * \retval #PSA_SUCCESS
1617 * Success.
1618 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001619 * \retval #PSA_ERROR_NOT_PERMITTED
1620 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001621 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001622 * \retval #PSA_ERROR_NOT_SUPPORTED
1623 * \p alg is not supported or is not a cipher algorithm.
1624 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1625 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1626 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1627 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001628 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001629 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001630 * \retval #PSA_ERROR_BAD_STATE
1631 * The library has not been previously initialized by psa_crypto_init().
1632 * It is implementation-dependent whether a failure to initialize
1633 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001634 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001635psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001636 psa_algorithm_t alg,
1637 const uint8_t *input,
1638 size_t input_length,
1639 uint8_t *output,
1640 size_t output_size,
1641 size_t *output_length);
1642
1643/** Decrypt a message using a symmetric cipher.
1644 *
1645 * This function decrypts a message encrypted with a symmetric cipher.
1646 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001647 * \param key Identifier of the key to use for the operation.
Gilles Peskine69647a42019-01-14 20:18:12 +01001648 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001649 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001650 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001651 * \param alg The cipher algorithm to compute
1652 * (\c PSA_ALG_XXX value such that
1653 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1654 * \param[in] input Buffer containing the message to decrypt.
1655 * This consists of the IV followed by the
1656 * ciphertext proper.
1657 * \param input_length Size of the \p input buffer in bytes.
1658 * \param[out] output Buffer where the plaintext is to be written.
1659 * \param output_size Size of the \p output buffer in bytes.
1660 * \param[out] output_length On success, the number of bytes
1661 * that make up the output.
1662 *
1663 * \retval #PSA_SUCCESS
1664 * Success.
1665 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001666 * \retval #PSA_ERROR_NOT_PERMITTED
1667 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001668 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001669 * \retval #PSA_ERROR_NOT_SUPPORTED
1670 * \p alg is not supported or is not a cipher algorithm.
1671 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1672 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1673 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1674 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001675 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001676 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001677 * \retval #PSA_ERROR_BAD_STATE
1678 * The library has not been previously initialized by psa_crypto_init().
1679 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001680 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001681 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001682psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001683 psa_algorithm_t alg,
1684 const uint8_t *input,
1685 size_t input_length,
1686 uint8_t *output,
1687 size_t output_size,
1688 size_t *output_length);
1689
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001690/** The type of the state data structure for multipart cipher operations.
1691 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001692 * Before calling any function on a cipher operation object, the application
1693 * must initialize it by any of the following means:
1694 * - Set the structure to all-bits-zero, for example:
1695 * \code
1696 * psa_cipher_operation_t operation;
1697 * memset(&operation, 0, sizeof(operation));
1698 * \endcode
1699 * - Initialize the structure to logical zero values, for example:
1700 * \code
1701 * psa_cipher_operation_t operation = {0};
1702 * \endcode
1703 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1704 * for example:
1705 * \code
1706 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1707 * \endcode
1708 * - Assign the result of the function psa_cipher_operation_init()
1709 * to the structure, for example:
1710 * \code
1711 * psa_cipher_operation_t operation;
1712 * operation = psa_cipher_operation_init();
1713 * \endcode
1714 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001715 * This is an implementation-defined \c struct. Applications should not
1716 * make any assumptions about the content of this structure except
1717 * as directed by the documentation of a specific implementation. */
1718typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1719
Jaeden Amero5bae2272019-01-04 11:48:27 +00001720/** \def PSA_CIPHER_OPERATION_INIT
1721 *
1722 * This macro returns a suitable initializer for a cipher operation object of
1723 * type #psa_cipher_operation_t.
1724 */
1725#ifdef __DOXYGEN_ONLY__
1726/* This is an example definition for documentation purposes.
1727 * Implementations should define a suitable value in `crypto_struct.h`.
1728 */
1729#define PSA_CIPHER_OPERATION_INIT {0}
1730#endif
1731
1732/** Return an initial value for a cipher operation object.
1733 */
1734static psa_cipher_operation_t psa_cipher_operation_init(void);
1735
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001736/** Set the key for a multipart symmetric encryption operation.
1737 *
1738 * The sequence of operations to encrypt a message with a symmetric cipher
1739 * is as follows:
1740 * -# Allocate an operation object which will be passed to all the functions
1741 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001742 * -# Initialize the operation object with one of the methods described in the
1743 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001744 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001745 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001746 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001747 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001748 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001749 * requires a specific IV value.
1750 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1751 * of the message each time.
1752 * -# Call psa_cipher_finish().
1753 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001754 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1755 * the operation will need to be reset by a call to psa_cipher_abort(). The
1756 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001757 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001758 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001759 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001760 * eventually terminate the operation. The following events terminate an
1761 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001762 * - A successful call to psa_cipher_finish().
1763 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001764 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001765 * \param[in,out] operation The operation object to set up. It must have
1766 * been initialized as per the documentation for
1767 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001768 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001769 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001770 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001771 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001772 * \param alg The cipher algorithm to compute
1773 * (\c PSA_ALG_XXX value such that
1774 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001775 *
Gilles Peskine28538492018-07-11 17:34:00 +02001776 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001777 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001778 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001779 * \retval #PSA_ERROR_NOT_PERMITTED
1780 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001781 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001782 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001783 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001784 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1785 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1786 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001787 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001788 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001789 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001790 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001791 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001792 * The library has not been previously initialized by psa_crypto_init().
1793 * It is implementation-dependent whether a failure to initialize
1794 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001795 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001796psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001797 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001798 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001799
1800/** Set the key for a multipart symmetric decryption operation.
1801 *
1802 * The sequence of operations to decrypt a message with a symmetric cipher
1803 * is as follows:
1804 * -# Allocate an operation object which will be passed to all the functions
1805 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001806 * -# Initialize the operation object with one of the methods described in the
1807 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001808 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001809 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001810 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001811 * decryption. If the IV is prepended to the ciphertext, you can call
1812 * psa_cipher_update() on a buffer containing the IV followed by the
1813 * beginning of the message.
1814 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1815 * of the message each time.
1816 * -# Call psa_cipher_finish().
1817 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001818 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1819 * the operation will need to be reset by a call to psa_cipher_abort(). The
1820 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001821 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001822 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001823 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001824 * eventually terminate the operation. The following events terminate an
1825 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001826 * - A successful call to psa_cipher_finish().
1827 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001828 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001829 * \param[in,out] operation The operation object to set up. It must have
1830 * been initialized as per the documentation for
1831 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001832 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001833 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001834 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001835 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001836 * \param alg The cipher algorithm to compute
1837 * (\c PSA_ALG_XXX value such that
1838 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001839 *
Gilles Peskine28538492018-07-11 17:34:00 +02001840 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001841 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001842 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001843 * \retval #PSA_ERROR_NOT_PERMITTED
1844 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001845 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001846 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001847 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001848 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1849 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1850 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001851 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001852 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001853 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001854 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001855 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001856 * The library has not been previously initialized by psa_crypto_init().
1857 * It is implementation-dependent whether a failure to initialize
1858 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001859 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001860psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001861 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001862 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001863
Gilles Peskinedcd14942018-07-12 00:30:52 +02001864/** Generate an IV for a symmetric encryption operation.
1865 *
1866 * This function generates a random IV (initialization vector), nonce
1867 * or initial counter value for the encryption operation as appropriate
1868 * for the chosen algorithm, key type and key size.
1869 *
1870 * The application must call psa_cipher_encrypt_setup() before
1871 * calling this function.
1872 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001873 * If this function returns an error status, the operation enters an error
1874 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001875 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001876 * \param[in,out] operation Active cipher operation.
1877 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001878 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001879 * \param[out] iv_length On success, the number of bytes of the
1880 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001881 *
1882 * \retval #PSA_SUCCESS
1883 * Success.
1884 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001885 * The operation state is not valid (it must be active, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001886 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001887 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001888 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1889 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1890 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001891 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001892 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001893 * \retval #PSA_ERROR_BAD_STATE
1894 * The library has not been previously initialized by psa_crypto_init().
1895 * It is implementation-dependent whether a failure to initialize
1896 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001897 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001898psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001899 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001900 size_t iv_size,
1901 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001902
Gilles Peskinedcd14942018-07-12 00:30:52 +02001903/** Set the IV for a symmetric encryption or decryption operation.
1904 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001905 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001906 * or initial counter value for the encryption or decryption operation.
1907 *
1908 * The application must call psa_cipher_encrypt_setup() before
1909 * calling this function.
1910 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001911 * If this function returns an error status, the operation enters an error
1912 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001913 *
1914 * \note When encrypting, applications should use psa_cipher_generate_iv()
1915 * instead of this function, unless implementing a protocol that requires
1916 * a non-random IV.
1917 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001918 * \param[in,out] operation Active cipher operation.
1919 * \param[in] iv Buffer containing the IV to use.
1920 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001921 *
1922 * \retval #PSA_SUCCESS
1923 * Success.
1924 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001925 * The operation state is not valid (it must be an active cipher
1926 * encrypt operation, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001927 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001928 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001929 * or the chosen algorithm does not use an IV.
1930 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1931 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1932 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001933 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001934 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001935 * \retval #PSA_ERROR_BAD_STATE
1936 * The library has not been previously initialized by psa_crypto_init().
1937 * It is implementation-dependent whether a failure to initialize
1938 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001939 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001940psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001941 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001942 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001943
Gilles Peskinedcd14942018-07-12 00:30:52 +02001944/** Encrypt or decrypt a message fragment in an active cipher operation.
1945 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001946 * Before calling this function, you must:
1947 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1948 * The choice of setup function determines whether this function
1949 * encrypts or decrypts its input.
1950 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1951 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001952 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001953 * If this function returns an error status, the operation enters an error
1954 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001955 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001956 * \param[in,out] operation Active cipher operation.
1957 * \param[in] input Buffer containing the message fragment to
1958 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001959 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001960 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001961 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001962 * \param[out] output_length On success, the number of bytes
1963 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001964 *
1965 * \retval #PSA_SUCCESS
1966 * Success.
1967 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001968 * The operation state is not valid (it must be active, with an IV set
1969 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001970 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1971 * The size of the \p output buffer is too small.
1972 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1973 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1974 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001975 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001976 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001977 * \retval #PSA_ERROR_BAD_STATE
1978 * The library has not been previously initialized by psa_crypto_init().
1979 * It is implementation-dependent whether a failure to initialize
1980 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001981 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001982psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1983 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001984 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001985 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001986 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001987 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001988
Gilles Peskinedcd14942018-07-12 00:30:52 +02001989/** Finish encrypting or decrypting a message in a cipher operation.
1990 *
1991 * The application must call psa_cipher_encrypt_setup() or
1992 * psa_cipher_decrypt_setup() before calling this function. The choice
1993 * of setup function determines whether this function encrypts or
1994 * decrypts its input.
1995 *
1996 * This function finishes the encryption or decryption of the message
1997 * formed by concatenating the inputs passed to preceding calls to
1998 * psa_cipher_update().
1999 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002000 * When this function returns successfuly, the operation becomes inactive.
2001 * If this function returns an error status, the operation enters an error
2002 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002003 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002004 * \param[in,out] operation Active cipher operation.
2005 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002006 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002007 * \param[out] output_length On success, the number of bytes
2008 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002009 *
2010 * \retval #PSA_SUCCESS
2011 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02002012 * \retval #PSA_ERROR_INVALID_ARGUMENT
2013 * The total input size passed to this operation is not valid for
2014 * this particular algorithm. For example, the algorithm is a based
2015 * on block cipher and requires a whole number of blocks, but the
2016 * total input size is not a multiple of the block size.
2017 * \retval #PSA_ERROR_INVALID_PADDING
2018 * This is a decryption operation for an algorithm that includes
2019 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002020 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002021 * The operation state is not valid (it must be active, with an IV set
2022 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02002023 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2024 * The size of the \p output buffer is too small.
2025 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2026 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2027 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002028 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002029 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002030 * \retval #PSA_ERROR_BAD_STATE
2031 * The library has not been previously initialized by psa_crypto_init().
2032 * It is implementation-dependent whether a failure to initialize
2033 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002034 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002035psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002036 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002037 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002038 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002039
Gilles Peskinedcd14942018-07-12 00:30:52 +02002040/** Abort a cipher operation.
2041 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002042 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002043 * \p operation structure itself. Once aborted, the operation object
2044 * can be reused for another operation by calling
2045 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002046 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002047 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002048 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002049 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002050 * In particular, calling psa_cipher_abort() after the operation has been
2051 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2052 * is safe and has no effect.
2053 *
2054 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002055 *
2056 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02002057 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2058 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002059 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002060 * \retval #PSA_ERROR_BAD_STATE
2061 * The library has not been previously initialized by psa_crypto_init().
2062 * It is implementation-dependent whether a failure to initialize
2063 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002064 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002065psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2066
2067/**@}*/
2068
Gilles Peskine3b555712018-03-03 21:27:57 +01002069/** \defgroup aead Authenticated encryption with associated data (AEAD)
2070 * @{
2071 */
2072
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002073/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002074 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002075 * \param key Identifier of the key to use for the
2076 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002077 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002078 * \param alg The AEAD algorithm to compute
2079 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002080 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002081 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002082 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002083 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002084 * but not encrypted.
2085 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002086 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002087 * encrypted.
2088 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002089 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002090 * encrypted data. The additional data is not
2091 * part of this output. For algorithms where the
2092 * encrypted data and the authentication tag
2093 * are defined as separate outputs, the
2094 * authentication tag is appended to the
2095 * encrypted data.
2096 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2097 * This must be at least
2098 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2099 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002100 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002101 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002102 *
Gilles Peskine28538492018-07-11 17:34:00 +02002103 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002104 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002105 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002106 * \retval #PSA_ERROR_NOT_PERMITTED
2107 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002108 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002109 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002110 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002111 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002112 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2113 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002114 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2115 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002116 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002117 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002118 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002119 * The library has not been previously initialized by psa_crypto_init().
2120 * It is implementation-dependent whether a failure to initialize
2121 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002122 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002123psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002124 psa_algorithm_t alg,
2125 const uint8_t *nonce,
2126 size_t nonce_length,
2127 const uint8_t *additional_data,
2128 size_t additional_data_length,
2129 const uint8_t *plaintext,
2130 size_t plaintext_length,
2131 uint8_t *ciphertext,
2132 size_t ciphertext_size,
2133 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002134
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002135/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002136 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002137 * \param key Identifier of the key to use for the
2138 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002139 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002140 * \param alg The AEAD algorithm to compute
2141 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002142 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002143 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002144 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002145 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002146 * but not encrypted.
2147 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002148 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002149 * encrypted. For algorithms where the
2150 * encrypted data and the authentication tag
2151 * are defined as separate inputs, the buffer
2152 * must contain the encrypted data followed
2153 * by the authentication tag.
2154 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002155 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002156 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2157 * This must be at least
2158 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2159 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002160 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002161 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002162 *
Gilles Peskine28538492018-07-11 17:34:00 +02002163 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002164 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002165 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002166 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002167 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002168 * \retval #PSA_ERROR_NOT_PERMITTED
2169 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002170 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002171 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002172 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002173 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002174 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2175 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002176 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2177 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002178 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002179 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002180 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002181 * The library has not been previously initialized by psa_crypto_init().
2182 * It is implementation-dependent whether a failure to initialize
2183 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002184 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002185psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002186 psa_algorithm_t alg,
2187 const uint8_t *nonce,
2188 size_t nonce_length,
2189 const uint8_t *additional_data,
2190 size_t additional_data_length,
2191 const uint8_t *ciphertext,
2192 size_t ciphertext_length,
2193 uint8_t *plaintext,
2194 size_t plaintext_size,
2195 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002196
Gilles Peskine30a9e412019-01-14 18:36:12 +01002197/** The type of the state data structure for multipart AEAD operations.
2198 *
2199 * Before calling any function on an AEAD operation object, the application
2200 * must initialize it by any of the following means:
2201 * - Set the structure to all-bits-zero, for example:
2202 * \code
2203 * psa_aead_operation_t operation;
2204 * memset(&operation, 0, sizeof(operation));
2205 * \endcode
2206 * - Initialize the structure to logical zero values, for example:
2207 * \code
2208 * psa_aead_operation_t operation = {0};
2209 * \endcode
2210 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2211 * for example:
2212 * \code
2213 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2214 * \endcode
2215 * - Assign the result of the function psa_aead_operation_init()
2216 * to the structure, for example:
2217 * \code
2218 * psa_aead_operation_t operation;
2219 * operation = psa_aead_operation_init();
2220 * \endcode
2221 *
2222 * This is an implementation-defined \c struct. Applications should not
2223 * make any assumptions about the content of this structure except
2224 * as directed by the documentation of a specific implementation. */
2225typedef struct psa_aead_operation_s psa_aead_operation_t;
2226
2227/** \def PSA_AEAD_OPERATION_INIT
2228 *
2229 * This macro returns a suitable initializer for an AEAD operation object of
2230 * type #psa_aead_operation_t.
2231 */
2232#ifdef __DOXYGEN_ONLY__
2233/* This is an example definition for documentation purposes.
2234 * Implementations should define a suitable value in `crypto_struct.h`.
2235 */
2236#define PSA_AEAD_OPERATION_INIT {0}
2237#endif
2238
2239/** Return an initial value for an AEAD operation object.
2240 */
2241static psa_aead_operation_t psa_aead_operation_init(void);
2242
2243/** Set the key for a multipart authenticated encryption operation.
2244 *
2245 * The sequence of operations to encrypt a message with authentication
2246 * is as follows:
2247 * -# Allocate an operation object which will be passed to all the functions
2248 * listed here.
2249 * -# Initialize the operation object with one of the methods described in the
2250 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002251 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002252 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002253 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2254 * inputs to the subsequent calls to psa_aead_update_ad() and
2255 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2256 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002257 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2258 * generate or set the nonce. You should use
2259 * psa_aead_generate_nonce() unless the protocol you are implementing
2260 * requires a specific nonce value.
2261 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2262 * of the non-encrypted additional authenticated data each time.
2263 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002264 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002265 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002266 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002267 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2268 * the operation will need to be reset by a call to psa_aead_abort(). The
2269 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002270 * has been initialized.
2271 *
2272 * After a successful call to psa_aead_encrypt_setup(), the application must
2273 * eventually terminate the operation. The following events terminate an
2274 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002275 * - A successful call to psa_aead_finish().
2276 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002277 *
2278 * \param[in,out] operation The operation object to set up. It must have
2279 * been initialized as per the documentation for
2280 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002281 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002282 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002283 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002284 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002285 * \param alg The AEAD algorithm to compute
2286 * (\c PSA_ALG_XXX value such that
2287 * #PSA_ALG_IS_AEAD(\p alg) is true).
2288 *
2289 * \retval #PSA_SUCCESS
2290 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002291 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002292 * The operation state is not valid (it must be inactive).
Ronald Cron96783552020-10-19 12:06:30 +02002293 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002294 * \retval #PSA_ERROR_NOT_PERMITTED
2295 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002296 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002297 * \retval #PSA_ERROR_NOT_SUPPORTED
2298 * \p alg is not supported or is not an AEAD algorithm.
2299 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2300 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2301 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002302 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002303 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002304 * \retval #PSA_ERROR_BAD_STATE
2305 * The library has not been previously initialized by psa_crypto_init().
2306 * It is implementation-dependent whether a failure to initialize
2307 * results in this error code.
2308 */
2309psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002310 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002311 psa_algorithm_t alg);
2312
2313/** Set the key for a multipart authenticated decryption operation.
2314 *
2315 * The sequence of operations to decrypt a message with authentication
2316 * is as follows:
2317 * -# Allocate an operation object which will be passed to all the functions
2318 * listed here.
2319 * -# Initialize the operation object with one of the methods described in the
2320 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002321 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002322 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002323 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2324 * inputs to the subsequent calls to psa_aead_update_ad() and
2325 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2326 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002327 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2328 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2329 * of the non-encrypted additional authenticated data each time.
2330 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002331 * of the ciphertext to decrypt each time.
2332 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002333 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002334 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2335 * the operation will need to be reset by a call to psa_aead_abort(). The
2336 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002337 * has been initialized.
2338 *
2339 * After a successful call to psa_aead_decrypt_setup(), the application must
2340 * eventually terminate the operation. The following events terminate an
2341 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002342 * - A successful call to psa_aead_verify().
2343 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002344 *
2345 * \param[in,out] operation The operation object to set up. It must have
2346 * been initialized as per the documentation for
2347 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002348 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002349 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002350 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002351 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002352 * \param alg The AEAD algorithm to compute
2353 * (\c PSA_ALG_XXX value such that
2354 * #PSA_ALG_IS_AEAD(\p alg) is true).
2355 *
2356 * \retval #PSA_SUCCESS
2357 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002358 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002359 * The operation state is not valid (it must be inactive).
Ronald Cron96783552020-10-19 12:06:30 +02002360 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002361 * \retval #PSA_ERROR_NOT_PERMITTED
2362 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002363 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002364 * \retval #PSA_ERROR_NOT_SUPPORTED
2365 * \p alg is not supported or is not an AEAD algorithm.
2366 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2367 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2368 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002369 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002370 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002371 * \retval #PSA_ERROR_BAD_STATE
2372 * The library has not been previously initialized by psa_crypto_init().
2373 * It is implementation-dependent whether a failure to initialize
2374 * results in this error code.
2375 */
2376psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002377 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002378 psa_algorithm_t alg);
2379
2380/** Generate a random nonce for an authenticated encryption operation.
2381 *
2382 * This function generates a random nonce for the authenticated encryption
2383 * operation with an appropriate size for the chosen algorithm, key type
2384 * and key size.
2385 *
2386 * The application must call psa_aead_encrypt_setup() before
2387 * calling this function.
2388 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002389 * If this function returns an error status, the operation enters an error
2390 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002391 *
2392 * \param[in,out] operation Active AEAD operation.
2393 * \param[out] nonce Buffer where the generated nonce is to be
2394 * written.
2395 * \param nonce_size Size of the \p nonce buffer in bytes.
2396 * \param[out] nonce_length On success, the number of bytes of the
2397 * generated nonce.
2398 *
2399 * \retval #PSA_SUCCESS
2400 * Success.
2401 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002402 * The operation state is not valid (it must be an active aead encrypt
Ronald Cron96783552020-10-19 12:06:30 +02002403 * operation, with no nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002404 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2405 * The size of the \p nonce buffer is too small.
2406 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2407 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2408 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002409 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002410 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002411 * \retval #PSA_ERROR_BAD_STATE
2412 * The library has not been previously initialized by psa_crypto_init().
2413 * It is implementation-dependent whether a failure to initialize
2414 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002415 */
2416psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002417 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002418 size_t nonce_size,
2419 size_t *nonce_length);
2420
2421/** Set the nonce for an authenticated encryption or decryption operation.
2422 *
2423 * This function sets the nonce for the authenticated
2424 * encryption or decryption operation.
2425 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002426 * The application must call psa_aead_encrypt_setup() or
2427 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002428 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002429 * If this function returns an error status, the operation enters an error
2430 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002431 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002432 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002433 * instead of this function, unless implementing a protocol that requires
2434 * a non-random IV.
2435 *
2436 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002437 * \param[in] nonce Buffer containing the nonce to use.
2438 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002439 *
2440 * \retval #PSA_SUCCESS
2441 * Success.
2442 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002443 * The operation state is not valid (it must be active, with no nonce
2444 * set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002445 * \retval #PSA_ERROR_INVALID_ARGUMENT
2446 * The size of \p nonce is not acceptable for the chosen algorithm.
2447 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2448 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2449 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002450 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002451 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002452 * \retval #PSA_ERROR_BAD_STATE
2453 * The library has not been previously initialized by psa_crypto_init().
2454 * It is implementation-dependent whether a failure to initialize
2455 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002456 */
2457psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002458 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002459 size_t nonce_length);
2460
Gilles Peskinebc59c852019-01-17 15:26:08 +01002461/** Declare the lengths of the message and additional data for AEAD.
2462 *
2463 * The application must call this function before calling
2464 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2465 * the operation requires it. If the algorithm does not require it,
2466 * calling this function is optional, but if this function is called
2467 * then the implementation must enforce the lengths.
2468 *
2469 * You may call this function before or after setting the nonce with
2470 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2471 *
2472 * - For #PSA_ALG_CCM, calling this function is required.
2473 * - For the other AEAD algorithms defined in this specification, calling
2474 * this function is not required.
2475 * - For vendor-defined algorithm, refer to the vendor documentation.
2476 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002477 * If this function returns an error status, the operation enters an error
2478 * state and must be aborted by calling psa_aead_abort().
2479 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002480 * \param[in,out] operation Active AEAD operation.
2481 * \param ad_length Size of the non-encrypted additional
2482 * authenticated data in bytes.
2483 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2484 *
2485 * \retval #PSA_SUCCESS
2486 * Success.
2487 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002488 * The operation state is not valid (it must be active, and
2489 * psa_aead_update_ad() and psa_aead_update() must not have been
2490 * called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002491 * \retval #PSA_ERROR_INVALID_ARGUMENT
2492 * At least one of the lengths is not acceptable for the chosen
2493 * algorithm.
2494 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2495 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2496 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002497 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002498 * \retval #PSA_ERROR_BAD_STATE
2499 * The library has not been previously initialized by psa_crypto_init().
2500 * It is implementation-dependent whether a failure to initialize
2501 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002502 */
2503psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2504 size_t ad_length,
2505 size_t plaintext_length);
2506
Gilles Peskine30a9e412019-01-14 18:36:12 +01002507/** Pass additional data to an active AEAD operation.
2508 *
2509 * Additional data is authenticated, but not encrypted.
2510 *
2511 * You may call this function multiple times to pass successive fragments
2512 * of the additional data. You may not call this function after passing
2513 * data to encrypt or decrypt with psa_aead_update().
2514 *
2515 * Before calling this function, you must:
2516 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2517 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2518 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002519 * If this function returns an error status, the operation enters an error
2520 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002521 *
2522 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2523 * there is no guarantee that the input is valid. Therefore, until
2524 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2525 * treat the input as untrusted and prepare to undo any action that
2526 * depends on the input if psa_aead_verify() returns an error status.
2527 *
2528 * \param[in,out] operation Active AEAD operation.
2529 * \param[in] input Buffer containing the fragment of
2530 * additional data.
2531 * \param input_length Size of the \p input buffer in bytes.
2532 *
2533 * \retval #PSA_SUCCESS
2534 * Success.
2535 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002536 * The operation state is not valid (it must be active, have a nonce
2537 * set, have lengths set if required by the algorithm, and
2538 * psa_aead_update() must not have been called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002539 * \retval #PSA_ERROR_INVALID_ARGUMENT
2540 * The total input length overflows the additional data length that
2541 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002542 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2543 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2544 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002545 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002546 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002547 * \retval #PSA_ERROR_BAD_STATE
2548 * The library has not been previously initialized by psa_crypto_init().
2549 * It is implementation-dependent whether a failure to initialize
2550 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002551 */
2552psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2553 const uint8_t *input,
2554 size_t input_length);
2555
2556/** Encrypt or decrypt a message fragment in an active AEAD operation.
2557 *
2558 * Before calling this function, you must:
2559 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2560 * The choice of setup function determines whether this function
2561 * encrypts or decrypts its input.
2562 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2563 * 3. Call psa_aead_update_ad() to pass all the additional data.
2564 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002565 * If this function returns an error status, the operation enters an error
2566 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002567 *
2568 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2569 * there is no guarantee that the input is valid. Therefore, until
2570 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2571 * - Do not use the output in any way other than storing it in a
2572 * confidential location. If you take any action that depends
2573 * on the tentative decrypted data, this action will need to be
2574 * undone if the input turns out not to be valid. Furthermore,
2575 * if an adversary can observe that this action took place
2576 * (for example through timing), they may be able to use this
2577 * fact as an oracle to decrypt any message encrypted with the
2578 * same key.
2579 * - In particular, do not copy the output anywhere but to a
2580 * memory or storage space that you have exclusive access to.
2581 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002582 * This function does not require the input to be aligned to any
2583 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002584 * a whole block at a time, it must consume all the input provided, but
2585 * it may delay the end of the corresponding output until a subsequent
2586 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2587 * provides sufficient input. The amount of data that can be delayed
2588 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002589 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002590 * \param[in,out] operation Active AEAD operation.
2591 * \param[in] input Buffer containing the message fragment to
2592 * encrypt or decrypt.
2593 * \param input_length Size of the \p input buffer in bytes.
2594 * \param[out] output Buffer where the output is to be written.
2595 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002596 * This must be at least
2597 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2598 * \p input_length) where \c alg is the
2599 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002600 * \param[out] output_length On success, the number of bytes
2601 * that make up the returned output.
2602 *
2603 * \retval #PSA_SUCCESS
2604 * Success.
2605 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002606 * The operation state is not valid (it must be active, have a nonce
2607 * set, and have lengths set if required by the algorithm).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002608 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2609 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002610 * You can determine a sufficient buffer size by calling
2611 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2612 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002613 * \retval #PSA_ERROR_INVALID_ARGUMENT
2614 * The total length of input to psa_aead_update_ad() so far is
2615 * less than the additional data length that was previously
2616 * specified with psa_aead_set_lengths().
2617 * \retval #PSA_ERROR_INVALID_ARGUMENT
2618 * The total input length overflows the plaintext length that
2619 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002620 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2621 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2622 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002623 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002624 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002625 * \retval #PSA_ERROR_BAD_STATE
2626 * The library has not been previously initialized by psa_crypto_init().
2627 * It is implementation-dependent whether a failure to initialize
2628 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002629 */
2630psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2631 const uint8_t *input,
2632 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002633 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002634 size_t output_size,
2635 size_t *output_length);
2636
2637/** Finish encrypting a message in an AEAD operation.
2638 *
2639 * The operation must have been set up with psa_aead_encrypt_setup().
2640 *
2641 * This function finishes the authentication of the additional data
2642 * formed by concatenating the inputs passed to preceding calls to
2643 * psa_aead_update_ad() with the plaintext formed by concatenating the
2644 * inputs passed to preceding calls to psa_aead_update().
2645 *
2646 * This function has two output buffers:
2647 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002648 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002649 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002650 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002651 * that the operation performs.
2652 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002653 * When this function returns successfuly, the operation becomes inactive.
2654 * If this function returns an error status, the operation enters an error
2655 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002656 *
2657 * \param[in,out] operation Active AEAD operation.
2658 * \param[out] ciphertext Buffer where the last part of the ciphertext
2659 * is to be written.
2660 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002661 * This must be at least
2662 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2663 * \c alg is the algorithm that is being
2664 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002665 * \param[out] ciphertext_length On success, the number of bytes of
2666 * returned ciphertext.
2667 * \param[out] tag Buffer where the authentication tag is
2668 * to be written.
2669 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002670 * This must be at least
2671 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2672 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002673 * \param[out] tag_length On success, the number of bytes
2674 * that make up the returned tag.
2675 *
2676 * \retval #PSA_SUCCESS
2677 * Success.
2678 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002679 * The operation state is not valid (it must be an active encryption
2680 * operation with a nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002681 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002682 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002683 * You can determine a sufficient buffer size for \p ciphertext by
2684 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2685 * where \c alg is the algorithm that is being calculated.
2686 * You can determine a sufficient buffer size for \p tag by
2687 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002688 * \retval #PSA_ERROR_INVALID_ARGUMENT
2689 * The total length of input to psa_aead_update_ad() so far is
2690 * less than the additional data length that was previously
2691 * specified with psa_aead_set_lengths().
2692 * \retval #PSA_ERROR_INVALID_ARGUMENT
2693 * The total length of input to psa_aead_update() so far is
2694 * less than the plaintext length that was previously
2695 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002696 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2697 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2698 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002699 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002700 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002701 * \retval #PSA_ERROR_BAD_STATE
2702 * The library has not been previously initialized by psa_crypto_init().
2703 * It is implementation-dependent whether a failure to initialize
2704 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002705 */
2706psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002707 uint8_t *ciphertext,
2708 size_t ciphertext_size,
2709 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002710 uint8_t *tag,
2711 size_t tag_size,
2712 size_t *tag_length);
2713
2714/** Finish authenticating and decrypting a message in an AEAD operation.
2715 *
2716 * The operation must have been set up with psa_aead_decrypt_setup().
2717 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002718 * This function finishes the authenticated decryption of the message
2719 * components:
2720 *
2721 * - The additional data consisting of the concatenation of the inputs
2722 * passed to preceding calls to psa_aead_update_ad().
2723 * - The ciphertext consisting of the concatenation of the inputs passed to
2724 * preceding calls to psa_aead_update().
2725 * - The tag passed to this function call.
2726 *
2727 * If the authentication tag is correct, this function outputs any remaining
2728 * plaintext and reports success. If the authentication tag is not correct,
2729 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002730 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002731 * When this function returns successfuly, the operation becomes inactive.
2732 * If this function returns an error status, the operation enters an error
2733 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002734 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002735 * \note Implementations shall make the best effort to ensure that the
2736 * comparison between the actual tag and the expected tag is performed
2737 * in constant time.
2738 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002739 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002740 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002741 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002742 * from previous calls to psa_aead_update()
2743 * that could not be processed until the end
2744 * of the input.
2745 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002746 * This must be at least
2747 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2748 * \c alg is the algorithm that is being
2749 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002750 * \param[out] plaintext_length On success, the number of bytes of
2751 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002752 * \param[in] tag Buffer containing the authentication tag.
2753 * \param tag_length Size of the \p tag buffer in bytes.
2754 *
2755 * \retval #PSA_SUCCESS
2756 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002757 * \retval #PSA_ERROR_INVALID_SIGNATURE
2758 * The calculations were successful, but the authentication tag is
2759 * not correct.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002760 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002761 * The operation state is not valid (it must be an active decryption
2762 * operation with a nonce set).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002763 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2764 * The size of the \p plaintext buffer is too small.
2765 * You can determine a sufficient buffer size for \p plaintext by
2766 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2767 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002768 * \retval #PSA_ERROR_INVALID_ARGUMENT
2769 * The total length of input to psa_aead_update_ad() so far is
2770 * less than the additional data length that was previously
2771 * specified with psa_aead_set_lengths().
2772 * \retval #PSA_ERROR_INVALID_ARGUMENT
2773 * The total length of input to psa_aead_update() so far is
2774 * less than the plaintext length that was previously
2775 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002776 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2777 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2778 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002779 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002780 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002781 * \retval #PSA_ERROR_BAD_STATE
2782 * The library has not been previously initialized by psa_crypto_init().
2783 * It is implementation-dependent whether a failure to initialize
2784 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002785 */
2786psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002787 uint8_t *plaintext,
2788 size_t plaintext_size,
2789 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002790 const uint8_t *tag,
2791 size_t tag_length);
2792
2793/** Abort an AEAD operation.
2794 *
2795 * Aborting an operation frees all associated resources except for the
2796 * \p operation structure itself. Once aborted, the operation object
2797 * can be reused for another operation by calling
2798 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2799 *
2800 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002801 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002802 *
2803 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002804 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2805 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002806 *
2807 * \param[in,out] operation Initialized AEAD operation.
2808 *
2809 * \retval #PSA_SUCCESS
Gilles Peskine30a9e412019-01-14 18:36:12 +01002810 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2811 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002812 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002813 * \retval #PSA_ERROR_BAD_STATE
2814 * The library has not been previously initialized by psa_crypto_init().
2815 * It is implementation-dependent whether a failure to initialize
2816 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002817 */
2818psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2819
Gilles Peskine3b555712018-03-03 21:27:57 +01002820/**@}*/
2821
Gilles Peskine20035e32018-02-03 22:44:14 +01002822/** \defgroup asymmetric Asymmetric cryptography
2823 * @{
2824 */
2825
2826/**
2827 * \brief Sign a hash or short message with a private key.
2828 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002829 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002830 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002831 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2832 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2833 * to determine the hash algorithm to use.
2834 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002835 * \param key Identifier of the key to use for the operation.
2836 * It must be an asymmetric key pair. The key must
Ronald Cron96783552020-10-19 12:06:30 +02002837 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002838 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002839 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002840 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002841 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002842 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002843 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002844 * \param[out] signature_length On success, the number of bytes
2845 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002846 *
Gilles Peskine28538492018-07-11 17:34:00 +02002847 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002848 * \retval #PSA_ERROR_INVALID_HANDLE
2849 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002850 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002851 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002852 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002853 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002854 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002855 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002856 * \retval #PSA_ERROR_NOT_SUPPORTED
2857 * \retval #PSA_ERROR_INVALID_ARGUMENT
2858 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2859 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2860 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002861 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002862 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002863 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002864 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002865 * The library has not been previously initialized by psa_crypto_init().
2866 * It is implementation-dependent whether a failure to initialize
2867 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002868 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002869psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002870 psa_algorithm_t alg,
2871 const uint8_t *hash,
2872 size_t hash_length,
2873 uint8_t *signature,
2874 size_t signature_size,
2875 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002876
2877/**
2878 * \brief Verify the signature a hash or short message using a public key.
2879 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002880 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002881 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002882 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2883 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2884 * to determine the hash algorithm to use.
2885 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002886 * \param key Identifier of the key to use for the operation. It
2887 * must be a public key or an asymmetric key pair. The
Ronald Cron96783552020-10-19 12:06:30 +02002888 * key must allow the usage
2889 * #PSA_KEY_USAGE_VERIFY_HASH.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002890 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002891 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002892 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002893 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002894 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002895 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002896 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002897 *
Gilles Peskine28538492018-07-11 17:34:00 +02002898 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002899 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002900 * \retval #PSA_ERROR_INVALID_HANDLE
2901 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002902 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002903 * The calculation was perfomed successfully, but the passed
2904 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002905 * \retval #PSA_ERROR_NOT_SUPPORTED
2906 * \retval #PSA_ERROR_INVALID_ARGUMENT
2907 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2908 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2909 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002910 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002911 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002912 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002913 * The library has not been previously initialized by psa_crypto_init().
2914 * It is implementation-dependent whether a failure to initialize
2915 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002916 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002917psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002918 psa_algorithm_t alg,
2919 const uint8_t *hash,
2920 size_t hash_length,
2921 const uint8_t *signature,
2922 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002923
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002924/**
2925 * \brief Encrypt a short message with a public key.
2926 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002927 * \param key Identifer of the key to use for the operation.
2928 * It must be a public key or an asymmetric key
2929 * pair. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002930 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002931 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02002932 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002933 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002934 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002935 * \param[in] salt A salt or label, if supported by the
2936 * encryption algorithm.
2937 * If the algorithm does not support a
2938 * salt, pass \c NULL.
2939 * If the algorithm supports an optional
2940 * salt and you do not want to pass a salt,
2941 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002942 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002943 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2944 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002945 * \param salt_length Size of the \p salt buffer in bytes.
2946 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002947 * \param[out] output Buffer where the encrypted message is to
2948 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002949 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002950 * \param[out] output_length On success, the number of bytes
2951 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002952 *
Gilles Peskine28538492018-07-11 17:34:00 +02002953 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002954 * \retval #PSA_ERROR_INVALID_HANDLE
2955 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002956 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002957 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002958 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002959 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002960 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002961 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002962 * \retval #PSA_ERROR_NOT_SUPPORTED
2963 * \retval #PSA_ERROR_INVALID_ARGUMENT
2964 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2965 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2966 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002967 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002968 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002969 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002970 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002971 * The library has not been previously initialized by psa_crypto_init().
2972 * It is implementation-dependent whether a failure to initialize
2973 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002974 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002975psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002976 psa_algorithm_t alg,
2977 const uint8_t *input,
2978 size_t input_length,
2979 const uint8_t *salt,
2980 size_t salt_length,
2981 uint8_t *output,
2982 size_t output_size,
2983 size_t *output_length);
2984
2985/**
2986 * \brief Decrypt a short message with a private key.
2987 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002988 * \param key Identifier of the key to use for the operation.
2989 * It must be an asymmetric key pair. It must
Ronald Cron96783552020-10-19 12:06:30 +02002990 * allow the usage #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002991 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02002992 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002993 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002994 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002995 * \param[in] salt A salt or label, if supported by the
2996 * encryption algorithm.
2997 * If the algorithm does not support a
2998 * salt, pass \c NULL.
2999 * If the algorithm supports an optional
3000 * salt and you do not want to pass a salt,
3001 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003002 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003003 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3004 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003005 * \param salt_length Size of the \p salt buffer in bytes.
3006 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003007 * \param[out] output Buffer where the decrypted message is to
3008 * be written.
3009 * \param output_size Size of the \c output buffer in bytes.
3010 * \param[out] output_length On success, the number of bytes
3011 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003012 *
Gilles Peskine28538492018-07-11 17:34:00 +02003013 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003014 * \retval #PSA_ERROR_INVALID_HANDLE
3015 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003016 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003017 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003018 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003019 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003020 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003021 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003022 * \retval #PSA_ERROR_NOT_SUPPORTED
3023 * \retval #PSA_ERROR_INVALID_ARGUMENT
3024 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3025 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3026 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003027 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003028 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003029 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3030 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003031 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003032 * The library has not been previously initialized by psa_crypto_init().
3033 * It is implementation-dependent whether a failure to initialize
3034 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003035 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003036psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003037 psa_algorithm_t alg,
3038 const uint8_t *input,
3039 size_t input_length,
3040 const uint8_t *salt,
3041 size_t salt_length,
3042 uint8_t *output,
3043 size_t output_size,
3044 size_t *output_length);
3045
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003046/**@}*/
3047
Gilles Peskine35675b62019-05-16 17:26:11 +02003048/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003049 * @{
3050 */
3051
Gilles Peskine35675b62019-05-16 17:26:11 +02003052/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003053 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003054 * Before calling any function on a key derivation operation object, the
3055 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003056 * - Set the structure to all-bits-zero, for example:
3057 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003058 * psa_key_derivation_operation_t operation;
3059 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003060 * \endcode
3061 * - Initialize the structure to logical zero values, for example:
3062 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003063 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003064 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003065 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003066 * for example:
3067 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003068 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003069 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003070 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003071 * to the structure, for example:
3072 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003073 * psa_key_derivation_operation_t operation;
3074 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003075 * \endcode
3076 *
3077 * This is an implementation-defined \c struct. Applications should not
3078 * make any assumptions about the content of this structure except
3079 * as directed by the documentation of a specific implementation.
3080 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003081typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003082
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003083/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003084 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003085 * This macro returns a suitable initializer for a key derivation operation
3086 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003087 */
3088#ifdef __DOXYGEN_ONLY__
3089/* This is an example definition for documentation purposes.
3090 * Implementations should define a suitable value in `crypto_struct.h`.
3091 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003092#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003093#endif
3094
Gilles Peskine35675b62019-05-16 17:26:11 +02003095/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003096 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003097static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003098
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003099/** Set up a key derivation operation.
3100 *
3101 * A key derivation algorithm takes some inputs and uses them to generate
3102 * a byte stream in a deterministic way.
3103 * This byte stream can be used to produce keys and other
3104 * cryptographic material.
3105 *
3106 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003107 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3108 * -# Call psa_key_derivation_setup() to select the algorithm.
3109 * -# Provide the inputs for the key derivation by calling
3110 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3111 * as appropriate. Which inputs are needed, in what order, and whether
3112 * they may be keys and if so of what type depends on the algorithm.
3113 * -# Optionally set the operation's maximum capacity with
3114 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3115 * of or after providing inputs. For some algorithms, this step is mandatory
3116 * because the output depends on the maximum capacity.
3117 * -# To derive a key, call psa_key_derivation_output_key().
3118 * To derive a byte string for a different purpose, call
3119 * psa_key_derivation_output_bytes().
3120 * Successive calls to these functions use successive output bytes
3121 * calculated by the key derivation algorithm.
3122 * -# Clean up the key derivation operation object with
3123 * psa_key_derivation_abort().
3124 *
3125 * If this function returns an error, the key derivation operation object is
3126 * not changed.
3127 *
3128 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3129 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003130 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003131 * Implementations must reject an attempt to derive a key of size 0.
3132 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003133 * \param[in,out] operation The key derivation operation object
3134 * to set up. It must
3135 * have been initialized but not set up yet.
3136 * \param alg The key derivation algorithm to compute
3137 * (\c PSA_ALG_XXX value such that
3138 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3139 *
3140 * \retval #PSA_SUCCESS
3141 * Success.
3142 * \retval #PSA_ERROR_INVALID_ARGUMENT
3143 * \c alg is not a key derivation algorithm.
3144 * \retval #PSA_ERROR_NOT_SUPPORTED
3145 * \c alg is not supported or is not a key derivation algorithm.
3146 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3147 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3148 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003149 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003150 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003151 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003152 * The operation state is not valid (it must be inactive).
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003153 * \retval #PSA_ERROR_BAD_STATE
3154 * The library has not been previously initialized by psa_crypto_init().
3155 * It is implementation-dependent whether a failure to initialize
3156 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003157 */
3158psa_status_t psa_key_derivation_setup(
3159 psa_key_derivation_operation_t *operation,
3160 psa_algorithm_t alg);
3161
Gilles Peskine35675b62019-05-16 17:26:11 +02003162/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003163 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003164 * The capacity of a key derivation is the maximum number of bytes that it can
3165 * return. When you get *N* bytes of output from a key derivation operation,
3166 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003167 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003168 * \param[in] operation The operation to query.
3169 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003170 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003171 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003172 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003173 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003174 * The operation state is not valid (it must be active).
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003175 * \retval #PSA_ERROR_HARDWARE_FAILURE
3176 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003177 * \retval #PSA_ERROR_BAD_STATE
3178 * The library has not been previously initialized by psa_crypto_init().
3179 * It is implementation-dependent whether a failure to initialize
3180 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003181 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003182psa_status_t psa_key_derivation_get_capacity(
3183 const psa_key_derivation_operation_t *operation,
3184 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003185
Gilles Peskine35675b62019-05-16 17:26:11 +02003186/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003187 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003188 * The capacity of a key derivation operation is the maximum number of bytes
3189 * that the key derivation operation can return from this point onwards.
3190 *
3191 * \param[in,out] operation The key derivation operation object to modify.
3192 * \param capacity The new capacity of the operation.
3193 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003194 * current capacity.
3195 *
3196 * \retval #PSA_SUCCESS
3197 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003198 * \p capacity is larger than the operation's current capacity.
3199 * In this case, the operation object remains valid and its capacity
3200 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003201 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003202 * The operation state is not valid (it must be active).
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003203 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003204 * \retval #PSA_ERROR_HARDWARE_FAILURE
3205 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003206 * \retval #PSA_ERROR_BAD_STATE
3207 * The library has not been previously initialized by psa_crypto_init().
3208 * It is implementation-dependent whether a failure to initialize
3209 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003210 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003211psa_status_t psa_key_derivation_set_capacity(
3212 psa_key_derivation_operation_t *operation,
3213 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003214
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003215/** Use the maximum possible capacity for a key derivation operation.
3216 *
3217 * Use this value as the capacity argument when setting up a key derivation
3218 * to indicate that the operation should have the maximum possible capacity.
3219 * The value of the maximum possible capacity depends on the key derivation
3220 * algorithm.
3221 */
3222#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3223
3224/** Provide an input for key derivation or key agreement.
3225 *
3226 * Which inputs are required and in what order depends on the algorithm.
3227 * Refer to the documentation of each key derivation or key agreement
3228 * algorithm for information.
3229 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003230 * This function passes direct inputs, which is usually correct for
3231 * non-secret inputs. To pass a secret input, which should be in a key
3232 * object, call psa_key_derivation_input_key() instead of this function.
3233 * Refer to the documentation of individual step types
3234 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3235 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003236 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003237 * If this function returns an error status, the operation enters an error
3238 * state and must be aborted by calling psa_key_derivation_abort().
3239 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003240 * \param[in,out] operation The key derivation operation object to use.
3241 * It must have been set up with
3242 * psa_key_derivation_setup() and must not
3243 * have produced any output yet.
3244 * \param step Which step the input data is for.
3245 * \param[in] data Input data to use.
3246 * \param data_length Size of the \p data buffer in bytes.
3247 *
3248 * \retval #PSA_SUCCESS
3249 * Success.
3250 * \retval #PSA_ERROR_INVALID_ARGUMENT
3251 * \c step is not compatible with the operation's algorithm.
3252 * \retval #PSA_ERROR_INVALID_ARGUMENT
3253 * \c step does not allow direct inputs.
3254 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3255 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3256 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003257 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003258 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003259 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003260 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003261 * \retval #PSA_ERROR_BAD_STATE
3262 * The library has not been previously initialized by psa_crypto_init().
3263 * It is implementation-dependent whether a failure to initialize
3264 * results in this error code.
3265 */
3266psa_status_t psa_key_derivation_input_bytes(
3267 psa_key_derivation_operation_t *operation,
3268 psa_key_derivation_step_t step,
3269 const uint8_t *data,
3270 size_t data_length);
3271
3272/** Provide an input for key derivation in the form of a key.
3273 *
3274 * Which inputs are required and in what order depends on the algorithm.
3275 * Refer to the documentation of each key derivation or key agreement
3276 * algorithm for information.
3277 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003278 * This function obtains input from a key object, which is usually correct for
3279 * secret inputs or for non-secret personalization strings kept in the key
3280 * store. To pass a non-secret parameter which is not in the key store,
3281 * call psa_key_derivation_input_bytes() instead of this function.
3282 * Refer to the documentation of individual step types
3283 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3284 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003285 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003286 * If this function returns an error status, the operation enters an error
3287 * state and must be aborted by calling psa_key_derivation_abort().
3288 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003289 * \param[in,out] operation The key derivation operation object to use.
3290 * It must have been set up with
3291 * psa_key_derivation_setup() and must not
3292 * have produced any output yet.
3293 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003294 * \param key Identifier of the key. It must have an
3295 * appropriate type for step and must allow the
Ronald Cron96783552020-10-19 12:06:30 +02003296 * usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003297 *
3298 * \retval #PSA_SUCCESS
3299 * Success.
3300 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003301 * \retval #PSA_ERROR_NOT_PERMITTED
3302 * \retval #PSA_ERROR_INVALID_ARGUMENT
3303 * \c step is not compatible with the operation's algorithm.
3304 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine224b0d62019-09-23 18:13:17 +02003305 * \c step does not allow key inputs of the given type
3306 * or does not allow key inputs at all.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003307 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3308 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3309 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003310 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003311 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003312 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003313 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003314 * \retval #PSA_ERROR_BAD_STATE
3315 * The library has not been previously initialized by psa_crypto_init().
3316 * It is implementation-dependent whether a failure to initialize
3317 * results in this error code.
3318 */
3319psa_status_t psa_key_derivation_input_key(
3320 psa_key_derivation_operation_t *operation,
3321 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003322 mbedtls_svc_key_id_t key);
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003323
3324/** Perform a key agreement and use the shared secret as input to a key
3325 * derivation.
3326 *
3327 * A key agreement algorithm takes two inputs: a private key \p private_key
3328 * a public key \p peer_key.
3329 * The result of this function is passed as input to a key derivation.
3330 * The output of this key derivation can be extracted by reading from the
3331 * resulting operation to produce keys and other cryptographic material.
3332 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003333 * If this function returns an error status, the operation enters an error
3334 * state and must be aborted by calling psa_key_derivation_abort().
3335 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003336 * \param[in,out] operation The key derivation operation object to use.
3337 * It must have been set up with
3338 * psa_key_derivation_setup() with a
3339 * key agreement and derivation algorithm
3340 * \c alg (\c PSA_ALG_XXX value such that
3341 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3342 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3343 * is false).
3344 * The operation must be ready for an
3345 * input of the type given by \p step.
3346 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003347 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003348 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003349 * \param[in] peer_key Public key of the peer. The peer key must be in the
3350 * same format that psa_import_key() accepts for the
3351 * public key type corresponding to the type of
3352 * private_key. That is, this function performs the
3353 * equivalent of
3354 * #psa_import_key(...,
3355 * `peer_key`, `peer_key_length`) where
3356 * with key attributes indicating the public key
3357 * type corresponding to the type of `private_key`.
3358 * For example, for EC keys, this means that peer_key
3359 * is interpreted as a point on the curve that the
3360 * private key is on. The standard formats for public
3361 * keys are documented in the documentation of
3362 * psa_export_public_key().
3363 * \param peer_key_length Size of \p peer_key in bytes.
3364 *
3365 * \retval #PSA_SUCCESS
3366 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01003367 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003368 * The operation state is not valid for this key agreement \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003369 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003370 * \retval #PSA_ERROR_NOT_PERMITTED
3371 * \retval #PSA_ERROR_INVALID_ARGUMENT
3372 * \c private_key is not compatible with \c alg,
3373 * or \p peer_key is not valid for \c alg or not compatible with
3374 * \c private_key.
3375 * \retval #PSA_ERROR_NOT_SUPPORTED
3376 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskine224b0d62019-09-23 18:13:17 +02003377 * \retval #PSA_ERROR_INVALID_ARGUMENT
3378 * \c step does not allow an input resulting from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003379 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3380 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3381 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003382 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003383 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003384 * \retval #PSA_ERROR_BAD_STATE
3385 * The library has not been previously initialized by psa_crypto_init().
3386 * It is implementation-dependent whether a failure to initialize
3387 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003388 */
3389psa_status_t psa_key_derivation_key_agreement(
3390 psa_key_derivation_operation_t *operation,
3391 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003392 mbedtls_svc_key_id_t private_key,
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003393 const uint8_t *peer_key,
3394 size_t peer_key_length);
3395
Gilles Peskine35675b62019-05-16 17:26:11 +02003396/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003397 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003398 * This function calculates output bytes from a key derivation algorithm and
3399 * return those bytes.
3400 * If you view the key derivation's output as a stream of bytes, this
3401 * function destructively reads the requested number of bytes from the
3402 * stream.
3403 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003404 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003405 * If this function returns an error status other than
3406 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003407 * state and must be aborted by calling psa_key_derivation_abort().
3408 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003409 * \param[in,out] operation The key derivation operation object to read from.
3410 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003411 * \param output_length Number of bytes to output.
3412 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003413 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003414 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003415 * The operation's capacity was less than
3416 * \p output_length bytes. Note that in this case,
3417 * no output is written to the output buffer.
3418 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003419 * subsequent calls to this function will not
3420 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003421 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003422 * The operation state is not valid (it must be active and completed
3423 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003424 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3425 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3426 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003427 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003428 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003429 * \retval #PSA_ERROR_BAD_STATE
3430 * The library has not been previously initialized by psa_crypto_init().
3431 * It is implementation-dependent whether a failure to initialize
3432 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003433 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003434psa_status_t psa_key_derivation_output_bytes(
3435 psa_key_derivation_operation_t *operation,
3436 uint8_t *output,
3437 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003438
Gilles Peskine35675b62019-05-16 17:26:11 +02003439/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003440 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003441 * This function calculates output bytes from a key derivation algorithm
3442 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003443 * The key's location, usage policy, type and size are taken from
3444 * \p attributes.
3445 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003446 * If you view the key derivation's output as a stream of bytes, this
3447 * function destructively reads as many bytes as required from the
3448 * stream.
3449 * The operation's capacity decreases by the number of bytes read.
3450 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003451 * If this function returns an error status other than
3452 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003453 * state and must be aborted by calling psa_key_derivation_abort().
3454 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003455 * How much output is produced and consumed from the operation, and how
3456 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003457 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003458 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003459 * of a given size, this function is functionally equivalent to
3460 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003461 * and passing the resulting output to #psa_import_key.
3462 * However, this function has a security benefit:
3463 * if the implementation provides an isolation boundary then
3464 * the key material is not exposed outside the isolation boundary.
3465 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003466 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003467 * The following key types defined in this specification follow this scheme:
3468 *
3469 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003470 * - #PSA_KEY_TYPE_ARC4;
3471 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003472 * - #PSA_KEY_TYPE_DERIVE;
3473 * - #PSA_KEY_TYPE_HMAC.
3474 *
3475 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003476 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003477 * Montgomery curve), this function always draws a byte string whose
3478 * length is determined by the curve, and sets the mandatory bits
3479 * accordingly. That is:
3480 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01003481 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003482 * string and process it as specified in RFC 7748 &sect;5.
Paul Elliott8ff510a2020-06-02 17:19:28 +01003483 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003484 * string and process it as specified in RFC 7748 &sect;5.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003485 *
3486 * - For key types for which the key is represented by a single sequence of
3487 * \p bits bits with constraints as to which bit sequences are acceptable,
3488 * this function draws a byte string of length (\p bits / 8) bytes rounded
3489 * up to the nearest whole number of bytes. If the resulting byte string
3490 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3491 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003492 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003493 * for the output produced by psa_export_key().
3494 * The following key types defined in this specification follow this scheme:
3495 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003496 * - #PSA_KEY_TYPE_DES.
3497 * Force-set the parity bits, but discard forbidden weak keys.
3498 * For 2-key and 3-key triple-DES, the three keys are generated
3499 * successively (for example, for 3-key triple-DES,
3500 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3501 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003502 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003503 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003504 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003505 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003506 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003507 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003508 * Weierstrass curve).
3509 * For these key types, interpret the byte string as integer
3510 * in big-endian order. Discard it if it is not in the range
3511 * [0, *N* - 2] where *N* is the boundary of the private key domain
3512 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003513 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003514 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003515 * This method allows compliance to NIST standards, specifically
3516 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003517 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3518 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3519 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3520 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003521 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003522 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003523 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003524 * implementation-defined.
3525 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003526 * In all cases, the data that is read is discarded from the operation.
3527 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003528 *
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003529 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3530 * the input to that step must be provided with psa_key_derivation_input_key().
3531 * Future versions of this specification may include additional restrictions
3532 * on the derived key based on the attributes and strength of the secret key.
3533 *
Gilles Peskine20628592019-04-19 19:29:50 +02003534 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003535 * \param[in,out] operation The key derivation operation object to read from.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003536 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003537 * key. For persistent keys, this is the key
3538 * identifier defined in \p attributes.
3539 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003540 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003541 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003542 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003543 * If the key is persistent, the key material and the key's metadata
3544 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003545 * \retval #PSA_ERROR_ALREADY_EXISTS
3546 * This is an attempt to create a persistent key, and there is
3547 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003548 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003549 * There was not enough data to create the desired key.
3550 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003551 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003552 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003553 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003554 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003555 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003556 * \retval #PSA_ERROR_INVALID_ARGUMENT
3557 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003558 * \retval #PSA_ERROR_NOT_PERMITTED
3559 * The #PSA_KEY_DERIVATION_INPUT_SECRET input was not provided through
3560 * a key.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003561 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003562 * The operation state is not valid (it must be active and completed
3563 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003564 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3565 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3566 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3567 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003568 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003569 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003570 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003571 * The library has not been previously initialized by psa_crypto_init().
3572 * It is implementation-dependent whether a failure to initialize
3573 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003574 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003575psa_status_t psa_key_derivation_output_key(
3576 const psa_key_attributes_t *attributes,
3577 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003578 mbedtls_svc_key_id_t *key);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003579
Gilles Peskine35675b62019-05-16 17:26:11 +02003580/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003581 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003582 * Aborting an operation frees all associated resources except for the \c
3583 * operation structure itself. Once aborted, the operation object can be reused
3584 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003585 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003586 * This function may be called at any time after the operation
3587 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003588 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003589 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3590 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003591 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003592 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003593 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003594 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003595 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3596 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003597 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003598 * \retval #PSA_ERROR_BAD_STATE
3599 * The library has not been previously initialized by psa_crypto_init().
3600 * It is implementation-dependent whether a failure to initialize
3601 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003602 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003603psa_status_t psa_key_derivation_abort(
3604 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003605
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003606/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003607 *
3608 * \warning The raw result of a key agreement algorithm such as finite-field
3609 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3610 * not be used directly as key material. It should instead be passed as
3611 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003612 * a key derivation, use psa_key_derivation_key_agreement() and other
3613 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003614 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003615 * \param alg The key agreement algorithm to compute
3616 * (\c PSA_ALG_XXX value such that
3617 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3618 * is true).
Ronald Croncf56a0a2020-08-04 09:51:30 +02003619 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003620 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003621 * \param[in] peer_key Public key of the peer. It must be
3622 * in the same format that psa_import_key()
3623 * accepts. The standard formats for public
3624 * keys are documented in the documentation
3625 * of psa_export_public_key().
3626 * \param peer_key_length Size of \p peer_key in bytes.
3627 * \param[out] output Buffer where the decrypted message is to
3628 * be written.
3629 * \param output_size Size of the \c output buffer in bytes.
3630 * \param[out] output_length On success, the number of bytes
3631 * that make up the returned output.
3632 *
3633 * \retval #PSA_SUCCESS
3634 * Success.
3635 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003636 * \retval #PSA_ERROR_NOT_PERMITTED
3637 * \retval #PSA_ERROR_INVALID_ARGUMENT
3638 * \p alg is not a key agreement algorithm
3639 * \retval #PSA_ERROR_INVALID_ARGUMENT
3640 * \p private_key is not compatible with \p alg,
3641 * or \p peer_key is not valid for \p alg or not compatible with
3642 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003643 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3644 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003645 * \retval #PSA_ERROR_NOT_SUPPORTED
3646 * \p alg is not a supported key agreement algorithm.
3647 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3648 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3649 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003650 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003651 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003652 * \retval #PSA_ERROR_BAD_STATE
3653 * The library has not been previously initialized by psa_crypto_init().
3654 * It is implementation-dependent whether a failure to initialize
3655 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003656 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003657psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003658 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02003659 const uint8_t *peer_key,
3660 size_t peer_key_length,
3661 uint8_t *output,
3662 size_t output_size,
3663 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003664
Gilles Peskineea0fb492018-07-12 17:17:20 +02003665/**@}*/
3666
Gilles Peskineedd76872018-07-20 17:42:05 +02003667/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003668 * @{
3669 */
3670
3671/**
3672 * \brief Generate random bytes.
3673 *
3674 * \warning This function **can** fail! Callers MUST check the return status
3675 * and MUST NOT use the content of the output buffer if the return
3676 * status is not #PSA_SUCCESS.
3677 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003678 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003679 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003680 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003681 * \param output_size Number of bytes to generate and output.
3682 *
Gilles Peskine28538492018-07-11 17:34:00 +02003683 * \retval #PSA_SUCCESS
3684 * \retval #PSA_ERROR_NOT_SUPPORTED
3685 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003686 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003687 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3688 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003689 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003690 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003691 * The library has not been previously initialized by psa_crypto_init().
3692 * It is implementation-dependent whether a failure to initialize
3693 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003694 */
3695psa_status_t psa_generate_random(uint8_t *output,
3696 size_t output_size);
3697
3698/**
3699 * \brief Generate a key or key pair.
3700 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003701 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02003702 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003703 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003704 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003705 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003706 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003707 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003708 * the public exponent is 65537.
3709 * The modulus is a product of two probabilistic primes
3710 * between 2^{n-1} and 2^n where n is the bit size specified in the
3711 * attributes.
3712 *
Gilles Peskine20628592019-04-19 19:29:50 +02003713 * \param[in] attributes The attributes for the new key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003714 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003715 * key. For persistent keys, this is the key
3716 * identifier defined in \p attributes.
3717 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003718 *
Gilles Peskine28538492018-07-11 17:34:00 +02003719 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003720 * Success.
3721 * If the key is persistent, the key material and the key's metadata
3722 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003723 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003724 * This is an attempt to create a persistent key, and there is
3725 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003726 * \retval #PSA_ERROR_NOT_SUPPORTED
3727 * \retval #PSA_ERROR_INVALID_ARGUMENT
3728 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3729 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3730 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3731 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003732 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003733 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3734 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003735 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003736 * The library has not been previously initialized by psa_crypto_init().
3737 * It is implementation-dependent whether a failure to initialize
3738 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003739 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003740psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003741 mbedtls_svc_key_id_t *key);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003742
3743/**@}*/
3744
Gilles Peskinee59236f2018-01-27 23:32:46 +01003745#ifdef __cplusplus
3746}
3747#endif
3748
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003749/* The file "crypto_sizes.h" contains definitions for size calculation
3750 * macros whose definitions are implementation-specific. */
3751#include "crypto_sizes.h"
3752
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003753/* The file "crypto_struct.h" contains definitions for
3754 * implementation-specific structs that are declared above. */
3755#include "crypto_struct.h"
3756
3757/* The file "crypto_extra.h" contains vendor-specific definitions. This
3758 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003759#include "crypto_extra.h"
3760
3761#endif /* PSA_CRYPTO_H */