blob: 66222c5c9dd69f44a6b0aa8f3de310993567adae [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02006 * Copyright The Mbed TLS Contributors
Jaeden Amerocab54942018-07-25 13:26:13 +01007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
39/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010040#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010041
Gilles Peskinee59236f2018-01-27 23:32:46 +010042#ifdef __cplusplus
43extern "C" {
44#endif
45
Gilles Peskinef3b731e2018-12-12 13:38:31 +010046/* The file "crypto_types.h" declares types that encode errors,
47 * algorithms, key types, policies, etc. */
48#include "crypto_types.h"
49
Andrew Thoelke02b372b2019-10-02 09:32:21 +010050/** \defgroup version API version
Adrian L. Shawd89338a2019-09-19 13:32:57 +010051 * @{
52 */
53
54/**
55 * The major version of this implementation of the PSA Crypto API
56 */
57#define PSA_CRYPTO_API_VERSION_MAJOR 1
58
59/**
60 * The minor version of this implementation of the PSA Crypto API
61 */
62#define PSA_CRYPTO_API_VERSION_MINOR 0
63
64/**@}*/
65
Gilles Peskinef3b731e2018-12-12 13:38:31 +010066/* The file "crypto_values.h" declares macros to build and analyze values
67 * of integral types defined in "crypto_types.h". */
68#include "crypto_values.h"
69
70/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010071 * @{
72 */
73
74/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010075 * \brief Library initialization.
76 *
77 * Applications must call this function before calling any other
78 * function in this module.
79 *
80 * Applications may call this function more than once. Once a call
81 * succeeds, subsequent calls are guaranteed to succeed.
82 *
itayzafrir18617092018-09-16 12:22:41 +030083 * If the application calls other functions before calling psa_crypto_init(),
84 * the behavior is undefined. Implementations are encouraged to either perform
85 * the operation as if the library had been initialized or to return
86 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
87 * implementations should not return a success status if the lack of
88 * initialization may have security implications, for example due to improper
89 * seeding of the random number generator.
90 *
Gilles Peskine28538492018-07-11 17:34:00 +020091 * \retval #PSA_SUCCESS
92 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
gabor-mezei-arm452b0a32020-11-09 17:42:55 +010093 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine28538492018-07-11 17:34:00 +020094 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
95 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +020096 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +020097 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
gabor-mezei-arm452b0a32020-11-09 17:42:55 +010098 * \retval #PSA_ERROR_STORAGE_FAILURE
99 * \retval #PSA_ERROR_DATA_INVALID
100 * \retval #PSA_ERROR_DATA_CORRUPT
101 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinee59236f2018-01-27 23:32:46 +0100102 */
103psa_status_t psa_crypto_init(void);
104
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100105/**@}*/
106
Gilles Peskine105f67f2019-07-23 18:16:05 +0200107/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +0200108 * @{
109 */
110
Gilles Peskinea0c06552019-05-21 15:54:54 +0200111/** \def PSA_KEY_ATTRIBUTES_INIT
112 *
113 * This macro returns a suitable initializer for a key attribute structure
114 * of type #psa_key_attributes_t.
115 */
116#ifdef __DOXYGEN_ONLY__
117/* This is an example definition for documentation purposes.
118 * Implementations should define a suitable value in `crypto_struct.h`.
119 */
120#define PSA_KEY_ATTRIBUTES_INIT {0}
121#endif
122
123/** Return an initial value for a key attributes structure.
124 */
125static psa_key_attributes_t psa_key_attributes_init(void);
126
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200127/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200128 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200129 * If the attribute structure currently declares the key as volatile (which
130 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200131 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200132 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200133 * This function does not access storage, it merely stores the given
134 * value in the structure.
135 * The persistent key will be written to storage when the attribute
136 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200137 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200138 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200139 *
Gilles Peskine20628592019-04-19 19:29:50 +0200140 * This function may be declared as `static` (i.e. without external
141 * linkage). This function may be provided as a function-like macro,
142 * but in this case it must evaluate each of its arguments exactly once.
143 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200144 * \param[out] attributes The attribute structure to write to.
145 * \param key The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200146 */
Ronald Cron71016a92020-08-28 19:01:50 +0200147static void psa_set_key_id( psa_key_attributes_t *attributes,
148 mbedtls_svc_key_id_t key );
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200149
Ronald Cron6b5ff532020-10-16 14:38:19 +0200150#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
151/** Set the owner identifier of a key.
152 *
153 * When key identifiers encode key owner identifiers, psa_set_key_id() does
154 * not allow to define in key attributes the owner of volatile keys as
155 * psa_set_key_id() enforces the key to be persistent.
156 *
157 * This function allows to set in key attributes the owner identifier of a
158 * key. It is intended to be used for volatile keys. For persistent keys,
159 * it is recommended to use the PSA Cryptography API psa_set_key_id() to define
160 * the owner of a key.
161 *
162 * \param[out] attributes The attribute structure to write to.
163 * \param owner_id The key owner identifier.
164 */
165static void mbedtls_set_key_owner_id( psa_key_attributes_t *attributes,
166 mbedtls_key_owner_id_t owner_id );
167#endif
168
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200169/** Set the location of a persistent key.
170 *
171 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200172 * with psa_set_key_id(). By default, a key that has a persistent identifier
173 * is stored in the default storage area identifier by
174 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
175 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200176 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200177 * This function does not access storage, it merely stores the given
178 * value in the structure.
179 * The persistent key will be written to storage when the attribute
180 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200181 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200182 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200183 *
184 * This function may be declared as `static` (i.e. without external
185 * linkage). This function may be provided as a function-like macro,
186 * but in this case it must evaluate each of its arguments exactly once.
187 *
188 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200189 * \param lifetime The lifetime for the key.
190 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200191 * key will be volatile, and the key identifier
192 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200193 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200194static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
195 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200196
Gilles Peskine20628592019-04-19 19:29:50 +0200197/** Retrieve the key identifier from key attributes.
198 *
199 * This function may be declared as `static` (i.e. without external
200 * linkage). This function may be provided as a function-like macro,
201 * but in this case it must evaluate its argument exactly once.
202 *
203 * \param[in] attributes The key attribute structure to query.
204 *
205 * \return The persistent identifier stored in the attribute structure.
206 * This value is unspecified if the attribute structure declares
207 * the key as volatile.
208 */
Ronald Cron71016a92020-08-28 19:01:50 +0200209static mbedtls_svc_key_id_t psa_get_key_id(
210 const psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200211
Gilles Peskine20628592019-04-19 19:29:50 +0200212/** Retrieve the lifetime from key attributes.
213 *
214 * This function may be declared as `static` (i.e. without external
215 * linkage). This function may be provided as a function-like macro,
216 * but in this case it must evaluate its argument exactly once.
217 *
218 * \param[in] attributes The key attribute structure to query.
219 *
220 * \return The lifetime value stored in the attribute structure.
221 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200222static psa_key_lifetime_t psa_get_key_lifetime(
223 const psa_key_attributes_t *attributes);
224
Gilles Peskine20628592019-04-19 19:29:50 +0200225/** Declare usage flags for a key.
226 *
227 * Usage flags are part of a key's usage policy. They encode what
228 * kind of operations are permitted on the key. For more details,
229 * refer to the documentation of the type #psa_key_usage_t.
230 *
231 * This function overwrites any usage flags
232 * previously set in \p attributes.
233 *
234 * This function may be declared as `static` (i.e. without external
235 * linkage). This function may be provided as a function-like macro,
236 * but in this case it must evaluate each of its arguments exactly once.
237 *
238 * \param[out] attributes The attribute structure to write to.
239 * \param usage_flags The usage flags to write.
240 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200241static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
242 psa_key_usage_t usage_flags);
243
Gilles Peskine20628592019-04-19 19:29:50 +0200244/** Retrieve the usage flags from key attributes.
245 *
246 * This function may be declared as `static` (i.e. without external
247 * linkage). This function may be provided as a function-like macro,
248 * but in this case it must evaluate its argument exactly once.
249 *
250 * \param[in] attributes The key attribute structure to query.
251 *
252 * \return The usage flags stored in the attribute structure.
253 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200254static psa_key_usage_t psa_get_key_usage_flags(
255 const psa_key_attributes_t *attributes);
256
Gilles Peskine20628592019-04-19 19:29:50 +0200257/** Declare the permitted algorithm policy for a key.
258 *
259 * The permitted algorithm policy of a key encodes which algorithm or
Gilles Peskinea170d922019-09-12 16:59:37 +0200260 * algorithms are permitted to be used with this key. The following
261 * algorithm policies are supported:
262 * - 0 does not allow any cryptographic operation with the key. The key
263 * may be used for non-cryptographic actions such as exporting (if
264 * permitted by the usage flags).
265 * - An algorithm value permits this particular algorithm.
266 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
267 * signature scheme with any hash algorithm.
Gilles Peskine20628592019-04-19 19:29:50 +0200268 *
269 * This function overwrites any algorithm policy
270 * previously set in \p attributes.
271 *
272 * This function may be declared as `static` (i.e. without external
273 * linkage). This function may be provided as a function-like macro,
274 * but in this case it must evaluate each of its arguments exactly once.
275 *
276 * \param[out] attributes The attribute structure to write to.
277 * \param alg The permitted algorithm policy to write.
278 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200279static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
280 psa_algorithm_t alg);
281
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100282
Gilles Peskine20628592019-04-19 19:29:50 +0200283/** Retrieve the algorithm policy from key attributes.
284 *
285 * This function may be declared as `static` (i.e. without external
286 * linkage). This function may be provided as a function-like macro,
287 * but in this case it must evaluate its argument exactly once.
288 *
289 * \param[in] attributes The key attribute structure to query.
290 *
291 * \return The algorithm stored in the attribute structure.
292 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200293static psa_algorithm_t psa_get_key_algorithm(
294 const psa_key_attributes_t *attributes);
295
Gilles Peskine20628592019-04-19 19:29:50 +0200296/** Declare the type of a key.
297 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200298 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200299 * previously set in \p attributes.
300 *
301 * This function may be declared as `static` (i.e. without external
302 * linkage). This function may be provided as a function-like macro,
303 * but in this case it must evaluate each of its arguments exactly once.
304 *
305 * \param[out] attributes The attribute structure to write to.
306 * \param type The key type to write.
Gilles Peskinea170d922019-09-12 16:59:37 +0200307 * If this is 0, the key type in \p attributes
308 * becomes unspecified.
Gilles Peskine20628592019-04-19 19:29:50 +0200309 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200310static void psa_set_key_type(psa_key_attributes_t *attributes,
311 psa_key_type_t type);
312
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100313
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200314/** Declare the size of a key.
315 *
316 * This function overwrites any key size previously set in \p attributes.
317 *
318 * This function may be declared as `static` (i.e. without external
319 * linkage). This function may be provided as a function-like macro,
320 * but in this case it must evaluate each of its arguments exactly once.
321 *
322 * \param[out] attributes The attribute structure to write to.
323 * \param bits The key size in bits.
Gilles Peskinea170d922019-09-12 16:59:37 +0200324 * If this is 0, the key size in \p attributes
Gilles Peskine05c900b2019-09-12 18:29:43 +0200325 * becomes unspecified. Keys of size 0 are
326 * not supported.
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200327 */
328static void psa_set_key_bits(psa_key_attributes_t *attributes,
329 size_t bits);
330
Gilles Peskine20628592019-04-19 19:29:50 +0200331/** Retrieve the key type from key attributes.
332 *
333 * This function may be declared as `static` (i.e. without external
334 * linkage). This function may be provided as a function-like macro,
335 * but in this case it must evaluate its argument exactly once.
336 *
337 * \param[in] attributes The key attribute structure to query.
338 *
339 * \return The key type stored in the attribute structure.
340 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200341static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
342
Gilles Peskine20628592019-04-19 19:29:50 +0200343/** Retrieve the key size from key attributes.
344 *
345 * This function may be declared as `static` (i.e. without external
346 * linkage). This function may be provided as a function-like macro,
347 * but in this case it must evaluate its argument exactly once.
348 *
349 * \param[in] attributes The key attribute structure to query.
350 *
351 * \return The key size stored in the attribute structure, in bits.
352 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200353static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
354
Gilles Peskine20628592019-04-19 19:29:50 +0200355/** Retrieve the attributes of a key.
356 *
357 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200358 * psa_reset_key_attributes(). It then copies the attributes of
359 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200360 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200361 * \note This function may allocate memory or other resources.
362 * Once you have called this function on an attribute structure,
363 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200364 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200365 * \param[in] key Identifier of the key to query.
Gilles Peskine20628592019-04-19 19:29:50 +0200366 * \param[in,out] attributes On success, the attributes of the key.
367 * On failure, equivalent to a
368 * freshly-initialized structure.
369 *
370 * \retval #PSA_SUCCESS
371 * \retval #PSA_ERROR_INVALID_HANDLE
372 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
373 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100374 * \retval #PSA_ERROR_CORRUPTION_DETECTED
375 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100376 * \retval #PSA_ERROR_BAD_STATE
377 * The library has not been previously initialized by psa_crypto_init().
378 * It is implementation-dependent whether a failure to initialize
379 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200380 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200381psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
Gilles Peskine4747d192019-04-17 15:05:45 +0200382 psa_key_attributes_t *attributes);
383
Gilles Peskine20628592019-04-19 19:29:50 +0200384/** Reset a key attribute structure to a freshly initialized state.
385 *
386 * You must initialize the attribute structure as described in the
387 * documentation of the type #psa_key_attributes_t before calling this
388 * function. Once the structure has been initialized, you may call this
389 * function at any time.
390 *
391 * This function frees any auxiliary resources that the structure
392 * may contain.
393 *
394 * \param[in,out] attributes The attribute structure to reset.
395 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200396void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200397
Gilles Peskine87a5e562019-04-17 12:28:25 +0200398/**@}*/
399
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100400/** \defgroup key_management Key management
401 * @{
402 */
403
Ronald Cron277a85f2020-08-04 15:49:48 +0200404/** Remove non-essential copies of key material from memory.
405 *
406 * If the key identifier designates a volatile key, this functions does not do
407 * anything and returns successfully.
408 *
409 * If the key identifier designates a persistent key, then this function will
410 * free all resources associated with the key in volatile memory. The key
411 * data in persistent storage is not affected and the key can still be used.
412 *
413 * \param key Identifier of the key to purge.
414 *
415 * \retval #PSA_SUCCESS
416 * The key material will have been removed from memory if it is not
417 * currently required.
418 * \retval #PSA_ERROR_INVALID_ARGUMENT
419 * \p key is not a valid key identifier.
420 * \retval #PSA_ERROR_BAD_STATE
421 * The library has not been previously initialized by psa_crypto_init().
422 * It is implementation-dependent whether a failure to initialize
423 * results in this error code.
424 */
425psa_status_t psa_purge_key(mbedtls_svc_key_id_t key);
426
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100427/** Make a copy of a key.
428 *
429 * Copy key material from one location to another.
430 *
431 * This function is primarily useful to copy a key from one location
432 * to another, since it populates a key using the material from
433 * another key which may have a different lifetime.
434 *
435 * This function may be used to share a key with a different party,
436 * subject to implementation-defined restrictions on key sharing.
437 *
438 * The policy on the source key must have the usage flag
439 * #PSA_KEY_USAGE_COPY set.
440 * This flag is sufficient to permit the copy if the key has the lifetime
441 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
442 * Some secure elements do not provide a way to copy a key without
443 * making it extractable from the secure element. If a key is located
444 * in such a secure element, then the key must have both usage flags
445 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
446 * a copy of the key outside the secure element.
447 *
448 * The resulting key may only be used in a way that conforms to
449 * both the policy of the original key and the policy specified in
450 * the \p attributes parameter:
451 * - The usage flags on the resulting key are the bitwise-and of the
452 * usage flags on the source policy and the usage flags in \p attributes.
453 * - If both allow the same algorithm or wildcard-based
454 * algorithm policy, the resulting key has the same algorithm policy.
455 * - If either of the policies allows an algorithm and the other policy
456 * allows a wildcard-based algorithm policy that includes this algorithm,
457 * the resulting key allows the same algorithm.
458 * - If the policies do not allow any algorithm in common, this function
459 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
460 *
461 * The effect of this function on implementation-defined attributes is
462 * implementation-defined.
463 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200464 * \param source_key The key to copy. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +0200465 * #PSA_KEY_USAGE_COPY. If a private or secret key is
Ronald Croncf56a0a2020-08-04 09:51:30 +0200466 * being copied outside of a secure element it must
Ronald Cron96783552020-10-19 12:06:30 +0200467 * also allow #PSA_KEY_USAGE_EXPORT.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100468 * \param[in] attributes The attributes for the new key.
469 * They are used as follows:
470 * - The key type and size may be 0. If either is
471 * nonzero, it must match the corresponding
472 * attribute of the source key.
473 * - The key location (the lifetime and, for
474 * persistent keys, the key identifier) is
475 * used directly.
476 * - The policy constraints (usage flags and
477 * algorithm policy) are combined from
478 * the source key and \p attributes so that
479 * both sets of restrictions apply, as
480 * described in the documentation of this function.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200481 * \param[out] target_key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +0200482 * key. For persistent keys, this is the key
483 * identifier defined in \p attributes.
484 * \c 0 on failure.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100485 *
486 * \retval #PSA_SUCCESS
487 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200488 * \p source_key is invalid.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100489 * \retval #PSA_ERROR_ALREADY_EXISTS
490 * This is an attempt to create a persistent key, and there is
491 * already a persistent key with the given identifier.
492 * \retval #PSA_ERROR_INVALID_ARGUMENT
493 * The lifetime or identifier in \p attributes are invalid.
494 * \retval #PSA_ERROR_INVALID_ARGUMENT
495 * The policy constraints on the source and specified in
496 * \p attributes are incompatible.
497 * \retval #PSA_ERROR_INVALID_ARGUMENT
498 * \p attributes specifies a key type or key size
499 * which does not match the attributes of the source key.
500 * \retval #PSA_ERROR_NOT_PERMITTED
501 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
502 * \retval #PSA_ERROR_NOT_PERMITTED
503 * The source key is not exportable and its lifetime does not
504 * allow copying it to the target's lifetime.
505 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
506 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
507 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
508 * \retval #PSA_ERROR_HARDWARE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100509 * \retval #PSA_ERROR_DATA_INVALID
510 * \retval #PSA_ERROR_DATA_CORRUPT
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100511 * \retval #PSA_ERROR_CORRUPTION_DETECTED
512 * \retval #PSA_ERROR_BAD_STATE
513 * The library has not been previously initialized by psa_crypto_init().
514 * It is implementation-dependent whether a failure to initialize
515 * results in this error code.
516 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200517psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100518 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200519 mbedtls_svc_key_id_t *target_key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100520
521
522/**
523 * \brief Destroy a key.
524 *
525 * This function destroys a key from both volatile
526 * memory and, if applicable, non-volatile storage. Implementations shall
527 * make a best effort to ensure that that the key material cannot be recovered.
528 *
529 * This function also erases any metadata such as policies and frees
Ronald Croncf56a0a2020-08-04 09:51:30 +0200530 * resources associated with the key.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100531 *
532 * If a key is currently in use in a multipart operation, then destroying the
533 * key will cause the multipart operation to fail.
534 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200535 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
Ronald Cron96783552020-10-19 12:06:30 +0200536 * return #PSA_SUCCESS.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100537 *
538 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +0200539 * \p key was a valid identifier and the key material that it
540 * referred to has been erased. Alternatively, \p key is \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100541 * \retval #PSA_ERROR_NOT_PERMITTED
542 * The key cannot be erased because it is
543 * read-only, either due to a policy or due to physical restrictions.
544 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200545 * \p key is not a valid identifier nor \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100546 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
547 * There was an failure in communication with the cryptoprocessor.
548 * The key material may still be present in the cryptoprocessor.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100549 * \retval #PSA_ERROR_DATA_INVALID
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100550 * The storage is corrupted. Implementations shall make a best effort
551 * to erase key material even in this stage, however applications
552 * should be aware that it may be impossible to guarantee that the
553 * key material is not recoverable in such cases.
554 * \retval #PSA_ERROR_CORRUPTION_DETECTED
555 * An unexpected condition which is not a storage corruption or
556 * a communication failure occurred. The cryptoprocessor may have
557 * been compromised.
558 * \retval #PSA_ERROR_BAD_STATE
559 * The library has not been previously initialized by psa_crypto_init().
560 * It is implementation-dependent whether a failure to initialize
561 * results in this error code.
562 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200563psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100564
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100565/**@}*/
566
567/** \defgroup import_export Key import and export
568 * @{
569 */
570
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100571/**
572 * \brief Import a key in binary format.
573 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100574 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100575 * documentation of psa_export_public_key() for the format of public keys
576 * and to the documentation of psa_export_key() for the format for
577 * other key types.
578 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200579 * The key data determines the key size. The attributes may optionally
580 * specify a key size; in this case it must match the size determined
581 * from the key data. A key size of 0 in \p attributes indicates that
582 * the key size is solely determined by the key data.
583 *
584 * Implementations must reject an attempt to import a key of size 0.
585 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100586 * This specification supports a single format for each key type.
587 * Implementations may support other formats as long as the standard
588 * format is supported. Implementations that support other formats
589 * should ensure that the formats are clearly unambiguous so as to
590 * minimize the risk that an invalid input is accidentally interpreted
591 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100592 *
Gilles Peskine20628592019-04-19 19:29:50 +0200593 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200594 * The key size is always determined from the
595 * \p data buffer.
596 * If the key size in \p attributes is nonzero,
597 * it must be equal to the size from \p data.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200598 * \param[out] key On success, an identifier to the newly created key.
Ronald Cron4067d1c2020-10-19 13:34:38 +0200599 * For persistent keys, this is the key identifier
600 * defined in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200601 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100602 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200603 * buffer is interpreted according to the type declared
604 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200605 * All implementations must support at least the format
606 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100607 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200608 * the chosen type. Implementations may allow other
609 * formats, but should be conservative: implementations
610 * should err on the side of rejecting content if it
611 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200612 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100613 *
Gilles Peskine28538492018-07-11 17:34:00 +0200614 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100615 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100616 * If the key is persistent, the key material and the key's metadata
617 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200618 * \retval #PSA_ERROR_ALREADY_EXISTS
619 * This is an attempt to create a persistent key, and there is
620 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200621 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200622 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200623 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200624 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200625 * The key attributes, as a whole, are invalid.
626 * \retval #PSA_ERROR_INVALID_ARGUMENT
627 * The key data is not correctly formatted.
628 * \retval #PSA_ERROR_INVALID_ARGUMENT
629 * The size in \p attributes is nonzero and does not match the size
630 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200631 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
632 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
633 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100634 * \retval #PSA_ERROR_DATA_CORRUPT
635 * \retval #PSA_ERROR_DATA_INVALID
Darryl Greend49a4992018-06-18 17:27:26 +0100636 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200637 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200638 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300639 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300640 * The library has not been previously initialized by psa_crypto_init().
641 * It is implementation-dependent whether a failure to initialize
642 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100643 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200644psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100645 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200646 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200647 mbedtls_svc_key_id_t *key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100648
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100649
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100650
651/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100652 * \brief Export a key in binary format.
653 *
654 * The output of this function can be passed to psa_import_key() to
655 * create an equivalent object.
656 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100657 * If the implementation of psa_import_key() supports other formats
658 * beyond the format specified here, the output from psa_export_key()
659 * must use the representation specified here, not the original
660 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100661 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100662 * For standard key types, the output format is as follows:
663 *
664 * - For symmetric keys (including MAC keys), the format is the
665 * raw bytes of the key.
666 * - For DES, the key data consists of 8 bytes. The parity bits must be
667 * correct.
668 * - For Triple-DES, the format is the concatenation of the
669 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200670 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200671 * is the non-encrypted DER encoding of the representation defined by
672 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
673 * ```
674 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200675 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200676 * modulus INTEGER, -- n
677 * publicExponent INTEGER, -- e
678 * privateExponent INTEGER, -- d
679 * prime1 INTEGER, -- p
680 * prime2 INTEGER, -- q
681 * exponent1 INTEGER, -- d mod (p-1)
682 * exponent2 INTEGER, -- d mod (q-1)
683 * coefficient INTEGER, -- (inverse of q) mod p
684 * }
685 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200686 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200687 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100688 * a representation of the private value as a `ceiling(m/8)`-byte string
689 * where `m` is the bit size associated with the curve, i.e. the bit size
690 * of the order of the curve's coordinate field. This byte string is
691 * in little-endian order for Montgomery curves (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +0100692 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
693 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
694 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
Steven Cooreman6f5cc712020-06-11 16:40:41 +0200695 * For Weierstrass curves, this is the content of the `privateKey` field of
696 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
697 * the format is defined by RFC 7748, and output is masked according to §5.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200698 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200699 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000700 * format is the representation of the private key `x` as a big-endian byte
701 * string. The length of the byte string is the private key size in bytes
702 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200703 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
704 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100705 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200706 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
707 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200708 * \param key Identifier of the key to export. It must allow the
Ronald Cron96783552020-10-19 12:06:30 +0200709 * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
Ronald Croncf56a0a2020-08-04 09:51:30 +0200710 * key.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200711 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200712 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200713 * \param[out] data_length On success, the number of bytes
714 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100715 *
Gilles Peskine28538492018-07-11 17:34:00 +0200716 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100717 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200718 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200719 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100720 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200721 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
722 * The size of the \p data buffer is too small. You can determine a
723 * sufficient buffer size by calling
724 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
725 * where \c type is the key type
726 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200727 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
728 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200729 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100730 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100731 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300732 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300733 * The library has not been previously initialized by psa_crypto_init().
734 * It is implementation-dependent whether a failure to initialize
735 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100736 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200737psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738 uint8_t *data,
739 size_t data_size,
740 size_t *data_length);
741
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100742/**
743 * \brief Export a public key or the public part of a key pair in binary format.
744 *
745 * The output of this function can be passed to psa_import_key() to
746 * create an object that is equivalent to the public key.
747 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000748 * This specification supports a single format for each key type.
749 * Implementations may support other formats as long as the standard
750 * format is supported. Implementations that support other formats
751 * should ensure that the formats are clearly unambiguous so as to
752 * minimize the risk that an invalid input is accidentally interpreted
753 * according to a different format.
754 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000755 * For standard key types, the output format is as follows:
756 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
757 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
758 * ```
759 * RSAPublicKey ::= SEQUENCE {
760 * modulus INTEGER, -- n
761 * publicExponent INTEGER } -- e
762 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000763 * - For elliptic curve public keys (key types for which
764 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
765 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
766 * Let `m` be the bit size associated with the curve, i.e. the bit size of
767 * `q` for a curve over `F_q`. The representation consists of:
768 * - The byte 0x04;
769 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
770 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200771 * - For Diffie-Hellman key exchange public keys (key types for which
772 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000773 * the format is the representation of the public key `y = g^x mod p` as a
774 * big-endian byte string. The length of the byte string is the length of the
775 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100776 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200777 * Exporting a public key object or the public part of a key pair is
778 * always permitted, regardless of the key's usage flags.
779 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200780 * \param key Identifier of the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200781 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200782 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200783 * \param[out] data_length On success, the number of bytes
784 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100785 *
Gilles Peskine28538492018-07-11 17:34:00 +0200786 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100787 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200788 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200789 * The key is neither a public key nor a key pair.
790 * \retval #PSA_ERROR_NOT_SUPPORTED
791 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
792 * The size of the \p data buffer is too small. You can determine a
793 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200794 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200795 * where \c type is the key type
796 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200797 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
798 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200799 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100800 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100801 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300802 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300803 * The library has not been previously initialized by psa_crypto_init().
804 * It is implementation-dependent whether a failure to initialize
805 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100806 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200807psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100808 uint8_t *data,
809 size_t data_size,
810 size_t *data_length);
811
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100812
Gilles Peskine20035e32018-02-03 22:44:14 +0100813
814/**@}*/
815
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100816/** \defgroup hash Message digests
817 * @{
818 */
819
Gilles Peskine69647a42019-01-14 20:18:12 +0100820/** Calculate the hash (digest) of a message.
821 *
822 * \note To verify the hash of a message against an
823 * expected value, use psa_hash_compare() instead.
824 *
825 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
826 * such that #PSA_ALG_IS_HASH(\p alg) is true).
827 * \param[in] input Buffer containing the message to hash.
828 * \param input_length Size of the \p input buffer in bytes.
829 * \param[out] hash Buffer where the hash is to be written.
830 * \param hash_size Size of the \p hash buffer in bytes.
831 * \param[out] hash_length On success, the number of bytes
832 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100833 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100834 *
835 * \retval #PSA_SUCCESS
836 * Success.
837 * \retval #PSA_ERROR_NOT_SUPPORTED
838 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100839 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100840 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
841 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100842 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
843 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
844 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200845 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100846 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100847 * \retval #PSA_ERROR_BAD_STATE
848 * The library has not been previously initialized by psa_crypto_init().
849 * It is implementation-dependent whether a failure to initialize
850 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100851 */
852psa_status_t psa_hash_compute(psa_algorithm_t alg,
853 const uint8_t *input,
854 size_t input_length,
855 uint8_t *hash,
856 size_t hash_size,
857 size_t *hash_length);
858
859/** Calculate the hash (digest) of a message and compare it with a
860 * reference value.
861 *
862 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
863 * such that #PSA_ALG_IS_HASH(\p alg) is true).
864 * \param[in] input Buffer containing the message to hash.
865 * \param input_length Size of the \p input buffer in bytes.
866 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100867 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100868 *
869 * \retval #PSA_SUCCESS
870 * The expected hash is identical to the actual hash of the input.
871 * \retval #PSA_ERROR_INVALID_SIGNATURE
872 * The hash of the message was calculated successfully, but it
873 * differs from the expected hash.
874 * \retval #PSA_ERROR_NOT_SUPPORTED
875 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100876 * \retval #PSA_ERROR_INVALID_ARGUMENT
877 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100878 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
879 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
880 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200881 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100882 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100883 * \retval #PSA_ERROR_BAD_STATE
884 * The library has not been previously initialized by psa_crypto_init().
885 * It is implementation-dependent whether a failure to initialize
886 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100887 */
888psa_status_t psa_hash_compare(psa_algorithm_t alg,
889 const uint8_t *input,
890 size_t input_length,
891 const uint8_t *hash,
Gilles Peskinefa710f52019-12-02 14:31:48 +0100892 size_t hash_length);
Gilles Peskine69647a42019-01-14 20:18:12 +0100893
Gilles Peskine308b91d2018-02-08 09:47:44 +0100894/** The type of the state data structure for multipart hash operations.
895 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000896 * Before calling any function on a hash operation object, the application must
897 * initialize it by any of the following means:
898 * - Set the structure to all-bits-zero, for example:
899 * \code
900 * psa_hash_operation_t operation;
901 * memset(&operation, 0, sizeof(operation));
902 * \endcode
903 * - Initialize the structure to logical zero values, for example:
904 * \code
905 * psa_hash_operation_t operation = {0};
906 * \endcode
907 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
908 * for example:
909 * \code
910 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
911 * \endcode
912 * - Assign the result of the function psa_hash_operation_init()
913 * to the structure, for example:
914 * \code
915 * psa_hash_operation_t operation;
916 * operation = psa_hash_operation_init();
917 * \endcode
918 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100919 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100920 * make any assumptions about the content of this structure except
921 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100922typedef struct psa_hash_operation_s psa_hash_operation_t;
923
Jaeden Amero6a25b412019-01-04 11:47:44 +0000924/** \def PSA_HASH_OPERATION_INIT
925 *
926 * This macro returns a suitable initializer for a hash operation object
927 * of type #psa_hash_operation_t.
928 */
929#ifdef __DOXYGEN_ONLY__
930/* This is an example definition for documentation purposes.
931 * Implementations should define a suitable value in `crypto_struct.h`.
932 */
933#define PSA_HASH_OPERATION_INIT {0}
934#endif
935
936/** Return an initial value for a hash operation object.
937 */
938static psa_hash_operation_t psa_hash_operation_init(void);
939
Gilles Peskinef45adda2019-01-14 18:29:18 +0100940/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100941 *
942 * The sequence of operations to calculate a hash (message digest)
943 * is as follows:
944 * -# Allocate an operation object which will be passed to all the functions
945 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000946 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100947 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200948 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100949 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100950 * of the message each time. The hash that is calculated is the hash
951 * of the concatenation of these messages in order.
952 * -# To calculate the hash, call psa_hash_finish().
953 * To compare the hash with an expected value, call psa_hash_verify().
954 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100955 * If an error occurs at any step after a call to psa_hash_setup(), the
956 * operation will need to be reset by a call to psa_hash_abort(). The
957 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000958 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100959 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200960 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100961 * eventually terminate the operation. The following events terminate an
962 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100963 * - A successful call to psa_hash_finish() or psa_hash_verify().
964 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100965 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000966 * \param[in,out] operation The operation object to set up. It must have
967 * been initialized as per the documentation for
968 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200969 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
970 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100971 *
Gilles Peskine28538492018-07-11 17:34:00 +0200972 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100973 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200974 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100975 * \p alg is not a supported hash algorithm.
976 * \retval #PSA_ERROR_INVALID_ARGUMENT
977 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100978 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100979 * The operation state is not valid (it must be inactive).
Gilles Peskine28538492018-07-11 17:34:00 +0200980 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
981 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
982 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200983 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100984 * \retval #PSA_ERROR_BAD_STATE
985 * The library has not been previously initialized by psa_crypto_init().
986 * It is implementation-dependent whether a failure to initialize
987 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100988 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200989psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100990 psa_algorithm_t alg);
991
Gilles Peskine308b91d2018-02-08 09:47:44 +0100992/** Add a message fragment to a multipart hash operation.
993 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200994 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100995 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100996 * If this function returns an error status, the operation enters an error
997 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100998 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200999 * \param[in,out] operation Active hash operation.
1000 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001001 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001002 *
Gilles Peskine28538492018-07-11 17:34:00 +02001003 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001004 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001005 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001006 * The operation state is not valid (it muct be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001007 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1008 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1009 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001010 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001011 * \retval #PSA_ERROR_BAD_STATE
1012 * The library has not been previously initialized by psa_crypto_init().
1013 * It is implementation-dependent whether a failure to initialize
1014 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001015 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001016psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1017 const uint8_t *input,
1018 size_t input_length);
1019
Gilles Peskine308b91d2018-02-08 09:47:44 +01001020/** Finish the calculation of the hash of a message.
1021 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001022 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001023 * This function calculates the hash of the message formed by concatenating
1024 * the inputs passed to preceding calls to psa_hash_update().
1025 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001026 * When this function returns successfuly, the operation becomes inactive.
1027 * If this function returns an error status, the operation enters an error
1028 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001029 *
1030 * \warning Applications should not call this function if they expect
1031 * a specific value for the hash. Call psa_hash_verify() instead.
1032 * Beware that comparing integrity or authenticity data such as
1033 * hash values with a function such as \c memcmp is risky
1034 * because the time taken by the comparison may leak information
1035 * about the hashed data which could allow an attacker to guess
1036 * a valid hash and thereby bypass security controls.
1037 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001038 * \param[in,out] operation Active hash operation.
1039 * \param[out] hash Buffer where the hash is to be written.
1040 * \param hash_size Size of the \p hash buffer in bytes.
1041 * \param[out] hash_length On success, the number of bytes
1042 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001043 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001044 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001045 *
Gilles Peskine28538492018-07-11 17:34:00 +02001046 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001047 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001048 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001049 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001050 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001051 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001052 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001053 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001054 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1055 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1056 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001057 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001058 * \retval #PSA_ERROR_BAD_STATE
1059 * The library has not been previously initialized by psa_crypto_init().
1060 * It is implementation-dependent whether a failure to initialize
1061 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001062 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001063psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1064 uint8_t *hash,
1065 size_t hash_size,
1066 size_t *hash_length);
1067
Gilles Peskine308b91d2018-02-08 09:47:44 +01001068/** Finish the calculation of the hash of a message and compare it with
1069 * an expected value.
1070 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001071 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001072 * This function calculates the hash of the message formed by concatenating
1073 * the inputs passed to preceding calls to psa_hash_update(). It then
1074 * compares the calculated hash with the expected hash passed as a
1075 * parameter to this function.
1076 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001077 * When this function returns successfuly, the operation becomes inactive.
1078 * If this function returns an error status, the operation enters an error
1079 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001080 *
Gilles Peskine19067982018-03-20 17:54:53 +01001081 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001082 * comparison between the actual hash and the expected hash is performed
1083 * in constant time.
1084 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001085 * \param[in,out] operation Active hash operation.
1086 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001087 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001088 *
Gilles Peskine28538492018-07-11 17:34:00 +02001089 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001090 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001091 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001092 * The hash of the message was calculated successfully, but it
1093 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001094 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001095 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001096 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1097 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1098 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001099 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001100 * \retval #PSA_ERROR_BAD_STATE
1101 * The library has not been previously initialized by psa_crypto_init().
1102 * It is implementation-dependent whether a failure to initialize
1103 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001104 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001105psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1106 const uint8_t *hash,
1107 size_t hash_length);
1108
Gilles Peskine308b91d2018-02-08 09:47:44 +01001109/** Abort a hash operation.
1110 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001111 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001112 * \p operation structure itself. Once aborted, the operation object
1113 * can be reused for another operation by calling
1114 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001115 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001116 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001117 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001118 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001119 * In particular, calling psa_hash_abort() after the operation has been
1120 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1121 * psa_hash_verify() is safe and has no effect.
1122 *
1123 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001124 *
Gilles Peskine28538492018-07-11 17:34:00 +02001125 * \retval #PSA_SUCCESS
Gilles Peskine28538492018-07-11 17:34:00 +02001126 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1127 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001128 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001129 * \retval #PSA_ERROR_BAD_STATE
1130 * The library has not been previously initialized by psa_crypto_init().
1131 * It is implementation-dependent whether a failure to initialize
1132 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001133 */
1134psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001135
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001136/** Clone a hash operation.
1137 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001138 * This function copies the state of an ongoing hash operation to
1139 * a new operation object. In other words, this function is equivalent
1140 * to calling psa_hash_setup() on \p target_operation with the same
1141 * algorithm that \p source_operation was set up for, then
1142 * psa_hash_update() on \p target_operation with the same input that
1143 * that was passed to \p source_operation. After this function returns, the
1144 * two objects are independent, i.e. subsequent calls involving one of
1145 * the objects do not affect the other object.
1146 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001147 * \param[in] source_operation The active hash operation to clone.
1148 * \param[in,out] target_operation The operation object to set up.
1149 * It must be initialized but not active.
1150 *
1151 * \retval #PSA_SUCCESS
1152 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001153 * The \p source_operation state is not valid (it must be active).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001154 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001155 * The \p target_operation state is not valid (it must be inactive).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001156 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1157 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001158 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001159 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001160 * \retval #PSA_ERROR_BAD_STATE
1161 * The library has not been previously initialized by psa_crypto_init().
1162 * It is implementation-dependent whether a failure to initialize
1163 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001164 */
1165psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1166 psa_hash_operation_t *target_operation);
1167
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001168/**@}*/
1169
Gilles Peskine8c9def32018-02-08 10:02:12 +01001170/** \defgroup MAC Message authentication codes
1171 * @{
1172 */
1173
Gilles Peskine69647a42019-01-14 20:18:12 +01001174/** Calculate the MAC (message authentication code) of a message.
1175 *
1176 * \note To verify the MAC of a message against an
1177 * expected value, use psa_mac_verify() instead.
1178 * Beware that comparing integrity or authenticity data such as
1179 * MAC values with a function such as \c memcmp is risky
1180 * because the time taken by the comparison may leak information
1181 * about the MAC value which could allow an attacker to guess
1182 * a valid MAC and thereby bypass security controls.
1183 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001184 * \param key Identifier of the key to use for the operation. It
1185 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001186 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001187 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001188 * \param[in] input Buffer containing the input message.
1189 * \param input_length Size of the \p input buffer in bytes.
1190 * \param[out] mac Buffer where the MAC value is to be written.
1191 * \param mac_size Size of the \p mac buffer in bytes.
1192 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001193 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001194 *
1195 * \retval #PSA_SUCCESS
1196 * Success.
1197 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001198 * \retval #PSA_ERROR_NOT_PERMITTED
1199 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001200 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001201 * \retval #PSA_ERROR_NOT_SUPPORTED
1202 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001203 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1204 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001205 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1206 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1207 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001208 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001209 * \retval #PSA_ERROR_STORAGE_FAILURE
1210 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001211 * \retval #PSA_ERROR_BAD_STATE
1212 * The library has not been previously initialized by psa_crypto_init().
1213 * It is implementation-dependent whether a failure to initialize
1214 * results in this error code.
1215 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001216psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001217 psa_algorithm_t alg,
1218 const uint8_t *input,
1219 size_t input_length,
1220 uint8_t *mac,
1221 size_t mac_size,
1222 size_t *mac_length);
1223
1224/** Calculate the MAC of a message and compare it with a reference value.
1225 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001226 * \param key Identifier of the key to use for the operation. It
1227 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001228 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001229 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001230 * \param[in] input Buffer containing the input message.
1231 * \param input_length Size of the \p input buffer in bytes.
1232 * \param[out] mac Buffer containing the expected MAC value.
1233 * \param mac_length Size of the \p mac buffer in bytes.
1234 *
1235 * \retval #PSA_SUCCESS
1236 * The expected MAC is identical to the actual MAC of the input.
1237 * \retval #PSA_ERROR_INVALID_SIGNATURE
1238 * The MAC of the message was calculated successfully, but it
1239 * differs from the expected value.
1240 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001241 * \retval #PSA_ERROR_NOT_PERMITTED
1242 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001243 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001244 * \retval #PSA_ERROR_NOT_SUPPORTED
1245 * \p alg is not supported or is not a MAC algorithm.
1246 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1247 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1248 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001249 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001250 * \retval #PSA_ERROR_STORAGE_FAILURE
1251 * The key could not be retrieved from storage.
1252 * \retval #PSA_ERROR_BAD_STATE
1253 * The library has not been previously initialized by psa_crypto_init().
1254 * It is implementation-dependent whether a failure to initialize
1255 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001256 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001257psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
Gilles Peskinea05602d2019-01-17 15:25:52 +01001258 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001259 const uint8_t *input,
1260 size_t input_length,
1261 const uint8_t *mac,
Gilles Peskine13faa2d2020-01-30 16:32:21 +01001262 size_t mac_length);
Gilles Peskine69647a42019-01-14 20:18:12 +01001263
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001264/** The type of the state data structure for multipart MAC operations.
1265 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001266 * Before calling any function on a MAC operation object, the application must
1267 * initialize it by any of the following means:
1268 * - Set the structure to all-bits-zero, for example:
1269 * \code
1270 * psa_mac_operation_t operation;
1271 * memset(&operation, 0, sizeof(operation));
1272 * \endcode
1273 * - Initialize the structure to logical zero values, for example:
1274 * \code
1275 * psa_mac_operation_t operation = {0};
1276 * \endcode
1277 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1278 * for example:
1279 * \code
1280 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1281 * \endcode
1282 * - Assign the result of the function psa_mac_operation_init()
1283 * to the structure, for example:
1284 * \code
1285 * psa_mac_operation_t operation;
1286 * operation = psa_mac_operation_init();
1287 * \endcode
1288 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001289 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001290 * make any assumptions about the content of this structure except
1291 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001292typedef struct psa_mac_operation_s psa_mac_operation_t;
1293
Jaeden Amero769ce272019-01-04 11:48:03 +00001294/** \def PSA_MAC_OPERATION_INIT
1295 *
1296 * This macro returns a suitable initializer for a MAC operation object of type
1297 * #psa_mac_operation_t.
1298 */
1299#ifdef __DOXYGEN_ONLY__
1300/* This is an example definition for documentation purposes.
1301 * Implementations should define a suitable value in `crypto_struct.h`.
1302 */
1303#define PSA_MAC_OPERATION_INIT {0}
1304#endif
1305
1306/** Return an initial value for a MAC operation object.
1307 */
1308static psa_mac_operation_t psa_mac_operation_init(void);
1309
Gilles Peskinef45adda2019-01-14 18:29:18 +01001310/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001311 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001312 * This function sets up the calculation of the MAC
1313 * (message authentication code) of a byte string.
1314 * To verify the MAC of a message against an
1315 * expected value, use psa_mac_verify_setup() instead.
1316 *
1317 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001318 * -# Allocate an operation object which will be passed to all the functions
1319 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001320 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001321 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001322 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001323 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1324 * of the message each time. The MAC that is calculated is the MAC
1325 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001326 * -# At the end of the message, call psa_mac_sign_finish() to finish
1327 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001328 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001329 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1330 * operation will need to be reset by a call to psa_mac_abort(). The
1331 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001332 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001333 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001334 * After a successful call to psa_mac_sign_setup(), the application must
1335 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001336 * - A successful call to psa_mac_sign_finish().
1337 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001338 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001339 * \param[in,out] operation The operation object to set up. It must have
1340 * been initialized as per the documentation for
1341 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001342 * \param key Identifier of the key to use for the operation. It
1343 * must remain valid until the operation terminates.
1344 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001345 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001346 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001347 *
Gilles Peskine28538492018-07-11 17:34:00 +02001348 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001349 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001350 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001351 * \retval #PSA_ERROR_NOT_PERMITTED
1352 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001353 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001354 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001355 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001356 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1357 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1358 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001359 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001360 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001361 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001362 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001363 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001364 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001365 * The library has not been previously initialized by psa_crypto_init().
1366 * It is implementation-dependent whether a failure to initialize
1367 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001368 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001369psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001370 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001371 psa_algorithm_t alg);
1372
Gilles Peskinef45adda2019-01-14 18:29:18 +01001373/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001374 *
1375 * This function sets up the verification of the MAC
1376 * (message authentication code) of a byte string against an expected value.
1377 *
1378 * The sequence of operations to verify a MAC is as follows:
1379 * -# Allocate an operation object which will be passed to all the functions
1380 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001381 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001382 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001383 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001384 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1385 * of the message each time. The MAC that is calculated is the MAC
1386 * of the concatenation of these messages in order.
1387 * -# At the end of the message, call psa_mac_verify_finish() to finish
1388 * calculating the actual MAC of the message and verify it against
1389 * the expected value.
1390 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001391 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1392 * operation will need to be reset by a call to psa_mac_abort(). The
1393 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001394 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001395 *
1396 * After a successful call to psa_mac_verify_setup(), the application must
1397 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001398 * - A successful call to psa_mac_verify_finish().
1399 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001400 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001401 * \param[in,out] operation The operation object to set up. It must have
1402 * been initialized as per the documentation for
1403 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001404 * \param key Identifier of the key to use for the operation. It
1405 * must remain valid until the operation terminates.
1406 * It must allow the usage
1407 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001408 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1409 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001410 *
Gilles Peskine28538492018-07-11 17:34:00 +02001411 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001412 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001413 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001414 * \retval #PSA_ERROR_NOT_PERMITTED
1415 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001416 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001417 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001418 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001419 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1420 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1421 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001422 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001423 * \retval #PSA_ERROR_STORAGE_FAILURE
1424 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001425 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001426 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001427 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001428 * The library has not been previously initialized by psa_crypto_init().
1429 * It is implementation-dependent whether a failure to initialize
1430 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001431 */
1432psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001433 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001434 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001435
Gilles Peskinedcd14942018-07-12 00:30:52 +02001436/** Add a message fragment to a multipart MAC operation.
1437 *
1438 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1439 * before calling this function.
1440 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001441 * If this function returns an error status, the operation enters an error
1442 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001443 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001444 * \param[in,out] operation Active MAC operation.
1445 * \param[in] input Buffer containing the message fragment to add to
1446 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001447 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001448 *
1449 * \retval #PSA_SUCCESS
1450 * Success.
1451 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001452 * The operation state is not valid (it must be active).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001453 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1454 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1455 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001456 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001457 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001458 * \retval #PSA_ERROR_BAD_STATE
1459 * The library has not been previously initialized by psa_crypto_init().
1460 * It is implementation-dependent whether a failure to initialize
1461 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001462 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001463psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1464 const uint8_t *input,
1465 size_t input_length);
1466
Gilles Peskinedcd14942018-07-12 00:30:52 +02001467/** Finish the calculation of the MAC of a message.
1468 *
1469 * The application must call psa_mac_sign_setup() before calling this function.
1470 * This function calculates the MAC of the message formed by concatenating
1471 * the inputs passed to preceding calls to psa_mac_update().
1472 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001473 * When this function returns successfuly, the operation becomes inactive.
1474 * If this function returns an error status, the operation enters an error
1475 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001476 *
1477 * \warning Applications should not call this function if they expect
1478 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1479 * Beware that comparing integrity or authenticity data such as
1480 * MAC values with a function such as \c memcmp is risky
1481 * because the time taken by the comparison may leak information
1482 * about the MAC value which could allow an attacker to guess
1483 * a valid MAC and thereby bypass security controls.
1484 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001485 * \param[in,out] operation Active MAC operation.
1486 * \param[out] mac Buffer where the MAC value is to be written.
1487 * \param mac_size Size of the \p mac buffer in bytes.
1488 * \param[out] mac_length On success, the number of bytes
1489 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001490 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001491 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001492 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001493 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001494 *
1495 * \retval #PSA_SUCCESS
1496 * Success.
1497 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001498 * The operation state is not valid (it must be an active mac sign
1499 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001500 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001501 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001502 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1503 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1504 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1505 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001506 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001507 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001508 * \retval #PSA_ERROR_BAD_STATE
1509 * The library has not been previously initialized by psa_crypto_init().
1510 * It is implementation-dependent whether a failure to initialize
1511 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001512 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001513psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1514 uint8_t *mac,
1515 size_t mac_size,
1516 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001517
Gilles Peskinedcd14942018-07-12 00:30:52 +02001518/** Finish the calculation of the MAC of a message and compare it with
1519 * an expected value.
1520 *
1521 * The application must call psa_mac_verify_setup() before calling this function.
1522 * This function calculates the MAC of the message formed by concatenating
1523 * the inputs passed to preceding calls to psa_mac_update(). It then
1524 * compares the calculated MAC with the expected MAC passed as a
1525 * parameter to this function.
1526 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001527 * When this function returns successfuly, the operation becomes inactive.
1528 * If this function returns an error status, the operation enters an error
1529 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001530 *
1531 * \note Implementations shall make the best effort to ensure that the
1532 * comparison between the actual MAC and the expected MAC is performed
1533 * in constant time.
1534 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001535 * \param[in,out] operation Active MAC operation.
1536 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001537 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001538 *
1539 * \retval #PSA_SUCCESS
1540 * The expected MAC is identical to the actual MAC of the message.
1541 * \retval #PSA_ERROR_INVALID_SIGNATURE
1542 * The MAC of the message was calculated successfully, but it
1543 * differs from the expected MAC.
1544 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001545 * The operation state is not valid (it must be an active mac verify
1546 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001547 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1548 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1549 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001550 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001551 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001552 * \retval #PSA_ERROR_BAD_STATE
1553 * The library has not been previously initialized by psa_crypto_init().
1554 * It is implementation-dependent whether a failure to initialize
1555 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001556 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001557psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1558 const uint8_t *mac,
1559 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001560
Gilles Peskinedcd14942018-07-12 00:30:52 +02001561/** Abort a MAC operation.
1562 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001563 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001564 * \p operation structure itself. Once aborted, the operation object
1565 * can be reused for another operation by calling
1566 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001567 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001568 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001569 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001570 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001571 * In particular, calling psa_mac_abort() after the operation has been
1572 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1573 * psa_mac_verify_finish() is safe and has no effect.
1574 *
1575 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001576 *
1577 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02001578 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1579 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001580 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001581 * \retval #PSA_ERROR_BAD_STATE
1582 * The library has not been previously initialized by psa_crypto_init().
1583 * It is implementation-dependent whether a failure to initialize
1584 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001585 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001586psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1587
1588/**@}*/
1589
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001590/** \defgroup cipher Symmetric ciphers
1591 * @{
1592 */
1593
Gilles Peskine69647a42019-01-14 20:18:12 +01001594/** Encrypt a message using a symmetric cipher.
1595 *
1596 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001597 * vector). Use the multipart operation interface with a
1598 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001599 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001600 * \param key Identifier of the key to use for the operation.
Ronald Cron96783552020-10-19 12:06:30 +02001601 * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001602 * \param alg The cipher algorithm to compute
1603 * (\c PSA_ALG_XXX value such that
1604 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1605 * \param[in] input Buffer containing the message to encrypt.
1606 * \param input_length Size of the \p input buffer in bytes.
1607 * \param[out] output Buffer where the output is to be written.
1608 * The output contains the IV followed by
1609 * the ciphertext proper.
1610 * \param output_size Size of the \p output buffer in bytes.
1611 * \param[out] output_length On success, the number of bytes
1612 * that make up the output.
1613 *
1614 * \retval #PSA_SUCCESS
1615 * Success.
1616 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001617 * \retval #PSA_ERROR_NOT_PERMITTED
1618 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001619 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001620 * \retval #PSA_ERROR_NOT_SUPPORTED
1621 * \p alg is not supported or is not a cipher algorithm.
1622 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1623 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1624 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1625 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001626 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001627 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001628 * \retval #PSA_ERROR_BAD_STATE
1629 * The library has not been previously initialized by psa_crypto_init().
1630 * It is implementation-dependent whether a failure to initialize
1631 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001632 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001633psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001634 psa_algorithm_t alg,
1635 const uint8_t *input,
1636 size_t input_length,
1637 uint8_t *output,
1638 size_t output_size,
1639 size_t *output_length);
1640
1641/** Decrypt a message using a symmetric cipher.
1642 *
1643 * This function decrypts a message encrypted with a symmetric cipher.
1644 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001645 * \param key Identifier of the key to use for the operation.
Gilles Peskine69647a42019-01-14 20:18:12 +01001646 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001647 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001648 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001649 * \param alg The cipher algorithm to compute
1650 * (\c PSA_ALG_XXX value such that
1651 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1652 * \param[in] input Buffer containing the message to decrypt.
1653 * This consists of the IV followed by the
1654 * ciphertext proper.
1655 * \param input_length Size of the \p input buffer in bytes.
1656 * \param[out] output Buffer where the plaintext is to be written.
1657 * \param output_size Size of the \p output buffer in bytes.
1658 * \param[out] output_length On success, the number of bytes
1659 * that make up the output.
1660 *
1661 * \retval #PSA_SUCCESS
1662 * Success.
1663 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001664 * \retval #PSA_ERROR_NOT_PERMITTED
1665 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001666 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001667 * \retval #PSA_ERROR_NOT_SUPPORTED
1668 * \p alg is not supported or is not a cipher algorithm.
1669 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1670 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1671 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1672 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001673 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001674 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001675 * \retval #PSA_ERROR_BAD_STATE
1676 * The library has not been previously initialized by psa_crypto_init().
1677 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001678 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001679 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001680psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001681 psa_algorithm_t alg,
1682 const uint8_t *input,
1683 size_t input_length,
1684 uint8_t *output,
1685 size_t output_size,
1686 size_t *output_length);
1687
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001688/** The type of the state data structure for multipart cipher operations.
1689 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001690 * Before calling any function on a cipher operation object, the application
1691 * must initialize it by any of the following means:
1692 * - Set the structure to all-bits-zero, for example:
1693 * \code
1694 * psa_cipher_operation_t operation;
1695 * memset(&operation, 0, sizeof(operation));
1696 * \endcode
1697 * - Initialize the structure to logical zero values, for example:
1698 * \code
1699 * psa_cipher_operation_t operation = {0};
1700 * \endcode
1701 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1702 * for example:
1703 * \code
1704 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1705 * \endcode
1706 * - Assign the result of the function psa_cipher_operation_init()
1707 * to the structure, for example:
1708 * \code
1709 * psa_cipher_operation_t operation;
1710 * operation = psa_cipher_operation_init();
1711 * \endcode
1712 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001713 * This is an implementation-defined \c struct. Applications should not
1714 * make any assumptions about the content of this structure except
1715 * as directed by the documentation of a specific implementation. */
1716typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1717
Jaeden Amero5bae2272019-01-04 11:48:27 +00001718/** \def PSA_CIPHER_OPERATION_INIT
1719 *
1720 * This macro returns a suitable initializer for a cipher operation object of
1721 * type #psa_cipher_operation_t.
1722 */
1723#ifdef __DOXYGEN_ONLY__
1724/* This is an example definition for documentation purposes.
1725 * Implementations should define a suitable value in `crypto_struct.h`.
1726 */
1727#define PSA_CIPHER_OPERATION_INIT {0}
1728#endif
1729
1730/** Return an initial value for a cipher operation object.
1731 */
1732static psa_cipher_operation_t psa_cipher_operation_init(void);
1733
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001734/** Set the key for a multipart symmetric encryption operation.
1735 *
1736 * The sequence of operations to encrypt a message with a symmetric cipher
1737 * is as follows:
1738 * -# Allocate an operation object which will be passed to all the functions
1739 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001740 * -# Initialize the operation object with one of the methods described in the
1741 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001742 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001743 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001744 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001745 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001746 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001747 * requires a specific IV value.
1748 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1749 * of the message each time.
1750 * -# Call psa_cipher_finish().
1751 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001752 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1753 * the operation will need to be reset by a call to psa_cipher_abort(). The
1754 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001755 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001756 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001757 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001758 * eventually terminate the operation. The following events terminate an
1759 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001760 * - A successful call to psa_cipher_finish().
1761 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001762 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001763 * \param[in,out] operation The operation object to set up. It must have
1764 * been initialized as per the documentation for
1765 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001766 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001767 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001768 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001769 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001770 * \param alg The cipher algorithm to compute
1771 * (\c PSA_ALG_XXX value such that
1772 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001773 *
Gilles Peskine28538492018-07-11 17:34:00 +02001774 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001775 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001776 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001777 * \retval #PSA_ERROR_NOT_PERMITTED
1778 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001779 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001780 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001781 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001782 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1783 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1784 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001785 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001786 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001787 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001788 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001789 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001790 * The library has not been previously initialized by psa_crypto_init().
1791 * It is implementation-dependent whether a failure to initialize
1792 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001793 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001794psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001795 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001796 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001797
1798/** Set the key for a multipart symmetric decryption operation.
1799 *
1800 * The sequence of operations to decrypt a message with a symmetric cipher
1801 * is as follows:
1802 * -# Allocate an operation object which will be passed to all the functions
1803 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001804 * -# Initialize the operation object with one of the methods described in the
1805 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001806 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001807 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001808 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001809 * decryption. If the IV is prepended to the ciphertext, you can call
1810 * psa_cipher_update() on a buffer containing the IV followed by the
1811 * beginning of the message.
1812 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1813 * of the message each time.
1814 * -# Call psa_cipher_finish().
1815 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001816 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1817 * the operation will need to be reset by a call to psa_cipher_abort(). The
1818 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001819 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001820 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001821 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001822 * eventually terminate the operation. The following events terminate an
1823 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001824 * - A successful call to psa_cipher_finish().
1825 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001826 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001827 * \param[in,out] operation The operation object to set up. It must have
1828 * been initialized as per the documentation for
1829 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001830 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001831 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001832 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001833 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001834 * \param alg The cipher algorithm to compute
1835 * (\c PSA_ALG_XXX value such that
1836 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001837 *
Gilles Peskine28538492018-07-11 17:34:00 +02001838 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001839 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001840 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001841 * \retval #PSA_ERROR_NOT_PERMITTED
1842 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001843 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001844 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001845 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001846 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1847 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1848 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001849 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001850 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001851 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001852 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001853 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001854 * The library has not been previously initialized by psa_crypto_init().
1855 * It is implementation-dependent whether a failure to initialize
1856 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001857 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001858psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001859 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001860 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001861
Gilles Peskinedcd14942018-07-12 00:30:52 +02001862/** Generate an IV for a symmetric encryption operation.
1863 *
1864 * This function generates a random IV (initialization vector), nonce
1865 * or initial counter value for the encryption operation as appropriate
1866 * for the chosen algorithm, key type and key size.
1867 *
1868 * The application must call psa_cipher_encrypt_setup() before
1869 * calling this function.
1870 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001871 * If this function returns an error status, the operation enters an error
1872 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001873 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001874 * \param[in,out] operation Active cipher operation.
1875 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001876 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001877 * \param[out] iv_length On success, the number of bytes of the
1878 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001879 *
1880 * \retval #PSA_SUCCESS
1881 * Success.
1882 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001883 * The operation state is not valid (it must be active, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001884 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001885 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001886 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1887 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1888 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001889 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001890 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001891 * \retval #PSA_ERROR_BAD_STATE
1892 * The library has not been previously initialized by psa_crypto_init().
1893 * It is implementation-dependent whether a failure to initialize
1894 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001895 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001896psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001897 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001898 size_t iv_size,
1899 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001900
Gilles Peskinedcd14942018-07-12 00:30:52 +02001901/** Set the IV for a symmetric encryption or decryption operation.
1902 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001903 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001904 * or initial counter value for the encryption or decryption operation.
1905 *
1906 * The application must call psa_cipher_encrypt_setup() before
1907 * calling this function.
1908 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001909 * If this function returns an error status, the operation enters an error
1910 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001911 *
1912 * \note When encrypting, applications should use psa_cipher_generate_iv()
1913 * instead of this function, unless implementing a protocol that requires
1914 * a non-random IV.
1915 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001916 * \param[in,out] operation Active cipher operation.
1917 * \param[in] iv Buffer containing the IV to use.
1918 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001919 *
1920 * \retval #PSA_SUCCESS
1921 * Success.
1922 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001923 * The operation state is not valid (it must be an active cipher
1924 * encrypt operation, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001925 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001926 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001927 * or the chosen algorithm does not use an IV.
1928 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1929 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1930 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001931 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001932 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001933 * \retval #PSA_ERROR_BAD_STATE
1934 * The library has not been previously initialized by psa_crypto_init().
1935 * It is implementation-dependent whether a failure to initialize
1936 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001937 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001938psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001939 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001940 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001941
Gilles Peskinedcd14942018-07-12 00:30:52 +02001942/** Encrypt or decrypt a message fragment in an active cipher operation.
1943 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001944 * Before calling this function, you must:
1945 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1946 * The choice of setup function determines whether this function
1947 * encrypts or decrypts its input.
1948 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1949 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001950 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001951 * If this function returns an error status, the operation enters an error
1952 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001953 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001954 * \param[in,out] operation Active cipher operation.
1955 * \param[in] input Buffer containing the message fragment to
1956 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001957 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001958 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001959 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001960 * \param[out] output_length On success, the number of bytes
1961 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001962 *
1963 * \retval #PSA_SUCCESS
1964 * Success.
1965 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001966 * The operation state is not valid (it must be active, with an IV set
1967 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001968 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1969 * The size of the \p output buffer is too small.
1970 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1971 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1972 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001973 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001974 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001975 * \retval #PSA_ERROR_BAD_STATE
1976 * The library has not been previously initialized by psa_crypto_init().
1977 * It is implementation-dependent whether a failure to initialize
1978 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001979 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001980psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1981 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001982 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001983 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001984 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001985 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001986
Gilles Peskinedcd14942018-07-12 00:30:52 +02001987/** Finish encrypting or decrypting a message in a cipher operation.
1988 *
1989 * The application must call psa_cipher_encrypt_setup() or
1990 * psa_cipher_decrypt_setup() before calling this function. The choice
1991 * of setup function determines whether this function encrypts or
1992 * decrypts its input.
1993 *
1994 * This function finishes the encryption or decryption of the message
1995 * formed by concatenating the inputs passed to preceding calls to
1996 * psa_cipher_update().
1997 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001998 * When this function returns successfuly, the operation becomes inactive.
1999 * If this function returns an error status, the operation enters an error
2000 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002001 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002002 * \param[in,out] operation Active cipher operation.
2003 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002004 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002005 * \param[out] output_length On success, the number of bytes
2006 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002007 *
2008 * \retval #PSA_SUCCESS
2009 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02002010 * \retval #PSA_ERROR_INVALID_ARGUMENT
2011 * The total input size passed to this operation is not valid for
2012 * this particular algorithm. For example, the algorithm is a based
2013 * on block cipher and requires a whole number of blocks, but the
2014 * total input size is not a multiple of the block size.
2015 * \retval #PSA_ERROR_INVALID_PADDING
2016 * This is a decryption operation for an algorithm that includes
2017 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002018 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002019 * The operation state is not valid (it must be active, with an IV set
2020 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02002021 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2022 * The size of the \p output buffer is too small.
2023 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2024 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2025 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002026 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002027 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002028 * \retval #PSA_ERROR_BAD_STATE
2029 * The library has not been previously initialized by psa_crypto_init().
2030 * It is implementation-dependent whether a failure to initialize
2031 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002032 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002033psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002034 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002035 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002036 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002037
Gilles Peskinedcd14942018-07-12 00:30:52 +02002038/** Abort a cipher operation.
2039 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002040 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002041 * \p operation structure itself. Once aborted, the operation object
2042 * can be reused for another operation by calling
2043 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002044 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002045 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002046 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002047 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002048 * In particular, calling psa_cipher_abort() after the operation has been
2049 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2050 * is safe and has no effect.
2051 *
2052 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002053 *
2054 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02002055 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2056 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002057 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002058 * \retval #PSA_ERROR_BAD_STATE
2059 * The library has not been previously initialized by psa_crypto_init().
2060 * It is implementation-dependent whether a failure to initialize
2061 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002062 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002063psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2064
2065/**@}*/
2066
Gilles Peskine3b555712018-03-03 21:27:57 +01002067/** \defgroup aead Authenticated encryption with associated data (AEAD)
2068 * @{
2069 */
2070
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002071/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002072 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002073 * \param key Identifier of the key to use for the
2074 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002075 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002076 * \param alg The AEAD algorithm to compute
2077 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002078 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002079 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002080 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002081 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002082 * but not encrypted.
2083 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002084 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002085 * encrypted.
2086 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002087 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002088 * encrypted data. The additional data is not
2089 * part of this output. For algorithms where the
2090 * encrypted data and the authentication tag
2091 * are defined as separate outputs, the
2092 * authentication tag is appended to the
2093 * encrypted data.
2094 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2095 * This must be at least
2096 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2097 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002098 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002099 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002100 *
Gilles Peskine28538492018-07-11 17:34:00 +02002101 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002102 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002103 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002104 * \retval #PSA_ERROR_NOT_PERMITTED
2105 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002106 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002107 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002108 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002109 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002110 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2111 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002112 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2113 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002114 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002115 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002116 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002117 * The library has not been previously initialized by psa_crypto_init().
2118 * It is implementation-dependent whether a failure to initialize
2119 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002120 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002121psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002122 psa_algorithm_t alg,
2123 const uint8_t *nonce,
2124 size_t nonce_length,
2125 const uint8_t *additional_data,
2126 size_t additional_data_length,
2127 const uint8_t *plaintext,
2128 size_t plaintext_length,
2129 uint8_t *ciphertext,
2130 size_t ciphertext_size,
2131 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002132
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002133/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002134 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002135 * \param key Identifier of the key to use for the
2136 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002137 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002138 * \param alg The AEAD algorithm to compute
2139 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002140 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002141 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002142 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002143 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002144 * but not encrypted.
2145 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002146 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002147 * encrypted. For algorithms where the
2148 * encrypted data and the authentication tag
2149 * are defined as separate inputs, the buffer
2150 * must contain the encrypted data followed
2151 * by the authentication tag.
2152 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002153 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002154 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2155 * This must be at least
2156 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2157 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002158 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002159 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002160 *
Gilles Peskine28538492018-07-11 17:34:00 +02002161 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002162 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002163 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002164 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002165 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002166 * \retval #PSA_ERROR_NOT_PERMITTED
2167 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002168 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002169 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002170 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002171 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002172 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2173 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002174 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2175 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002176 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002177 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002178 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002179 * The library has not been previously initialized by psa_crypto_init().
2180 * It is implementation-dependent whether a failure to initialize
2181 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002182 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002183psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002184 psa_algorithm_t alg,
2185 const uint8_t *nonce,
2186 size_t nonce_length,
2187 const uint8_t *additional_data,
2188 size_t additional_data_length,
2189 const uint8_t *ciphertext,
2190 size_t ciphertext_length,
2191 uint8_t *plaintext,
2192 size_t plaintext_size,
2193 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002194
Gilles Peskine30a9e412019-01-14 18:36:12 +01002195/** The type of the state data structure for multipart AEAD operations.
2196 *
2197 * Before calling any function on an AEAD operation object, the application
2198 * must initialize it by any of the following means:
2199 * - Set the structure to all-bits-zero, for example:
2200 * \code
2201 * psa_aead_operation_t operation;
2202 * memset(&operation, 0, sizeof(operation));
2203 * \endcode
2204 * - Initialize the structure to logical zero values, for example:
2205 * \code
2206 * psa_aead_operation_t operation = {0};
2207 * \endcode
2208 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2209 * for example:
2210 * \code
2211 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2212 * \endcode
2213 * - Assign the result of the function psa_aead_operation_init()
2214 * to the structure, for example:
2215 * \code
2216 * psa_aead_operation_t operation;
2217 * operation = psa_aead_operation_init();
2218 * \endcode
2219 *
2220 * This is an implementation-defined \c struct. Applications should not
2221 * make any assumptions about the content of this structure except
2222 * as directed by the documentation of a specific implementation. */
2223typedef struct psa_aead_operation_s psa_aead_operation_t;
2224
2225/** \def PSA_AEAD_OPERATION_INIT
2226 *
2227 * This macro returns a suitable initializer for an AEAD operation object of
2228 * type #psa_aead_operation_t.
2229 */
2230#ifdef __DOXYGEN_ONLY__
2231/* This is an example definition for documentation purposes.
2232 * Implementations should define a suitable value in `crypto_struct.h`.
2233 */
2234#define PSA_AEAD_OPERATION_INIT {0}
2235#endif
2236
2237/** Return an initial value for an AEAD operation object.
2238 */
2239static psa_aead_operation_t psa_aead_operation_init(void);
2240
2241/** Set the key for a multipart authenticated encryption operation.
2242 *
2243 * The sequence of operations to encrypt a message with authentication
2244 * is as follows:
2245 * -# Allocate an operation object which will be passed to all the functions
2246 * listed here.
2247 * -# Initialize the operation object with one of the methods described in the
2248 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002249 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002250 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002251 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2252 * inputs to the subsequent calls to psa_aead_update_ad() and
2253 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2254 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002255 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2256 * generate or set the nonce. You should use
2257 * psa_aead_generate_nonce() unless the protocol you are implementing
2258 * requires a specific nonce value.
2259 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2260 * of the non-encrypted additional authenticated data each time.
2261 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002262 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002263 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002264 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002265 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2266 * the operation will need to be reset by a call to psa_aead_abort(). The
2267 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002268 * has been initialized.
2269 *
2270 * After a successful call to psa_aead_encrypt_setup(), the application must
2271 * eventually terminate the operation. The following events terminate an
2272 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002273 * - A successful call to psa_aead_finish().
2274 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002275 *
2276 * \param[in,out] operation The operation object to set up. It must have
2277 * been initialized as per the documentation for
2278 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002279 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002280 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002281 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002282 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002283 * \param alg The AEAD algorithm to compute
2284 * (\c PSA_ALG_XXX value such that
2285 * #PSA_ALG_IS_AEAD(\p alg) is true).
2286 *
2287 * \retval #PSA_SUCCESS
2288 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002289 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002290 * The operation state is not valid (it must be inactive).
Ronald Cron96783552020-10-19 12:06:30 +02002291 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002292 * \retval #PSA_ERROR_NOT_PERMITTED
2293 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002294 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002295 * \retval #PSA_ERROR_NOT_SUPPORTED
2296 * \p alg is not supported or is not an AEAD algorithm.
2297 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2298 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2299 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002300 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002301 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002302 * \retval #PSA_ERROR_BAD_STATE
2303 * The library has not been previously initialized by psa_crypto_init().
2304 * It is implementation-dependent whether a failure to initialize
2305 * results in this error code.
2306 */
2307psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002308 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002309 psa_algorithm_t alg);
2310
2311/** Set the key for a multipart authenticated decryption operation.
2312 *
2313 * The sequence of operations to decrypt a message with authentication
2314 * is as follows:
2315 * -# Allocate an operation object which will be passed to all the functions
2316 * listed here.
2317 * -# Initialize the operation object with one of the methods described in the
2318 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002319 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002320 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002321 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2322 * inputs to the subsequent calls to psa_aead_update_ad() and
2323 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2324 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002325 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2326 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2327 * of the non-encrypted additional authenticated data each time.
2328 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002329 * of the ciphertext to decrypt each time.
2330 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002331 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002332 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2333 * the operation will need to be reset by a call to psa_aead_abort(). The
2334 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002335 * has been initialized.
2336 *
2337 * After a successful call to psa_aead_decrypt_setup(), the application must
2338 * eventually terminate the operation. The following events terminate an
2339 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002340 * - A successful call to psa_aead_verify().
2341 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002342 *
2343 * \param[in,out] operation The operation object to set up. It must have
2344 * been initialized as per the documentation for
2345 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002346 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002347 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002348 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002349 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002350 * \param alg The AEAD algorithm to compute
2351 * (\c PSA_ALG_XXX value such that
2352 * #PSA_ALG_IS_AEAD(\p alg) is true).
2353 *
2354 * \retval #PSA_SUCCESS
2355 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002356 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002357 * The operation state is not valid (it must be inactive).
Ronald Cron96783552020-10-19 12:06:30 +02002358 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002359 * \retval #PSA_ERROR_NOT_PERMITTED
2360 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002361 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002362 * \retval #PSA_ERROR_NOT_SUPPORTED
2363 * \p alg is not supported or is not an AEAD algorithm.
2364 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2365 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2366 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002367 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002368 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002369 * \retval #PSA_ERROR_BAD_STATE
2370 * The library has not been previously initialized by psa_crypto_init().
2371 * It is implementation-dependent whether a failure to initialize
2372 * results in this error code.
2373 */
2374psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002375 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002376 psa_algorithm_t alg);
2377
2378/** Generate a random nonce for an authenticated encryption operation.
2379 *
2380 * This function generates a random nonce for the authenticated encryption
2381 * operation with an appropriate size for the chosen algorithm, key type
2382 * and key size.
2383 *
2384 * The application must call psa_aead_encrypt_setup() before
2385 * calling this function.
2386 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002387 * If this function returns an error status, the operation enters an error
2388 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002389 *
2390 * \param[in,out] operation Active AEAD operation.
2391 * \param[out] nonce Buffer where the generated nonce is to be
2392 * written.
2393 * \param nonce_size Size of the \p nonce buffer in bytes.
2394 * \param[out] nonce_length On success, the number of bytes of the
2395 * generated nonce.
2396 *
2397 * \retval #PSA_SUCCESS
2398 * Success.
2399 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002400 * The operation state is not valid (it must be an active aead encrypt
Ronald Cron96783552020-10-19 12:06:30 +02002401 * operation, with no nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002402 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2403 * The size of the \p nonce buffer is too small.
2404 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2405 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2406 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002407 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002408 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002409 * \retval #PSA_ERROR_BAD_STATE
2410 * The library has not been previously initialized by psa_crypto_init().
2411 * It is implementation-dependent whether a failure to initialize
2412 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002413 */
2414psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002415 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002416 size_t nonce_size,
2417 size_t *nonce_length);
2418
2419/** Set the nonce for an authenticated encryption or decryption operation.
2420 *
2421 * This function sets the nonce for the authenticated
2422 * encryption or decryption operation.
2423 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002424 * The application must call psa_aead_encrypt_setup() or
2425 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002426 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002427 * If this function returns an error status, the operation enters an error
2428 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002429 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002430 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002431 * instead of this function, unless implementing a protocol that requires
2432 * a non-random IV.
2433 *
2434 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002435 * \param[in] nonce Buffer containing the nonce to use.
2436 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002437 *
2438 * \retval #PSA_SUCCESS
2439 * Success.
2440 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002441 * The operation state is not valid (it must be active, with no nonce
2442 * set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002443 * \retval #PSA_ERROR_INVALID_ARGUMENT
2444 * The size of \p nonce is not acceptable for the chosen algorithm.
2445 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2446 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2447 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002448 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002449 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002450 * \retval #PSA_ERROR_BAD_STATE
2451 * The library has not been previously initialized by psa_crypto_init().
2452 * It is implementation-dependent whether a failure to initialize
2453 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002454 */
2455psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002456 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002457 size_t nonce_length);
2458
Gilles Peskinebc59c852019-01-17 15:26:08 +01002459/** Declare the lengths of the message and additional data for AEAD.
2460 *
2461 * The application must call this function before calling
2462 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2463 * the operation requires it. If the algorithm does not require it,
2464 * calling this function is optional, but if this function is called
2465 * then the implementation must enforce the lengths.
2466 *
2467 * You may call this function before or after setting the nonce with
2468 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2469 *
2470 * - For #PSA_ALG_CCM, calling this function is required.
2471 * - For the other AEAD algorithms defined in this specification, calling
2472 * this function is not required.
2473 * - For vendor-defined algorithm, refer to the vendor documentation.
2474 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002475 * If this function returns an error status, the operation enters an error
2476 * state and must be aborted by calling psa_aead_abort().
2477 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002478 * \param[in,out] operation Active AEAD operation.
2479 * \param ad_length Size of the non-encrypted additional
2480 * authenticated data in bytes.
2481 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2482 *
2483 * \retval #PSA_SUCCESS
2484 * Success.
2485 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002486 * The operation state is not valid (it must be active, and
2487 * psa_aead_update_ad() and psa_aead_update() must not have been
2488 * called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002489 * \retval #PSA_ERROR_INVALID_ARGUMENT
2490 * At least one of the lengths is not acceptable for the chosen
2491 * algorithm.
2492 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2493 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2494 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002495 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002496 * \retval #PSA_ERROR_BAD_STATE
2497 * The library has not been previously initialized by psa_crypto_init().
2498 * It is implementation-dependent whether a failure to initialize
2499 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002500 */
2501psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2502 size_t ad_length,
2503 size_t plaintext_length);
2504
Gilles Peskine30a9e412019-01-14 18:36:12 +01002505/** Pass additional data to an active AEAD operation.
2506 *
2507 * Additional data is authenticated, but not encrypted.
2508 *
2509 * You may call this function multiple times to pass successive fragments
2510 * of the additional data. You may not call this function after passing
2511 * data to encrypt or decrypt with psa_aead_update().
2512 *
2513 * Before calling this function, you must:
2514 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2515 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2516 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002517 * If this function returns an error status, the operation enters an error
2518 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002519 *
2520 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2521 * there is no guarantee that the input is valid. Therefore, until
2522 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2523 * treat the input as untrusted and prepare to undo any action that
2524 * depends on the input if psa_aead_verify() returns an error status.
2525 *
2526 * \param[in,out] operation Active AEAD operation.
2527 * \param[in] input Buffer containing the fragment of
2528 * additional data.
2529 * \param input_length Size of the \p input buffer in bytes.
2530 *
2531 * \retval #PSA_SUCCESS
2532 * Success.
2533 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002534 * The operation state is not valid (it must be active, have a nonce
2535 * set, have lengths set if required by the algorithm, and
2536 * psa_aead_update() must not have been called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002537 * \retval #PSA_ERROR_INVALID_ARGUMENT
2538 * The total input length overflows the additional data length that
2539 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002540 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2541 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2542 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002543 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002544 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002545 * \retval #PSA_ERROR_BAD_STATE
2546 * The library has not been previously initialized by psa_crypto_init().
2547 * It is implementation-dependent whether a failure to initialize
2548 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002549 */
2550psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2551 const uint8_t *input,
2552 size_t input_length);
2553
2554/** Encrypt or decrypt a message fragment in an active AEAD operation.
2555 *
2556 * Before calling this function, you must:
2557 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2558 * The choice of setup function determines whether this function
2559 * encrypts or decrypts its input.
2560 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2561 * 3. Call psa_aead_update_ad() to pass all the additional data.
2562 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002563 * If this function returns an error status, the operation enters an error
2564 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002565 *
2566 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2567 * there is no guarantee that the input is valid. Therefore, until
2568 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2569 * - Do not use the output in any way other than storing it in a
2570 * confidential location. If you take any action that depends
2571 * on the tentative decrypted data, this action will need to be
2572 * undone if the input turns out not to be valid. Furthermore,
2573 * if an adversary can observe that this action took place
2574 * (for example through timing), they may be able to use this
2575 * fact as an oracle to decrypt any message encrypted with the
2576 * same key.
2577 * - In particular, do not copy the output anywhere but to a
2578 * memory or storage space that you have exclusive access to.
2579 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002580 * This function does not require the input to be aligned to any
2581 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002582 * a whole block at a time, it must consume all the input provided, but
2583 * it may delay the end of the corresponding output until a subsequent
2584 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2585 * provides sufficient input. The amount of data that can be delayed
2586 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002587 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002588 * \param[in,out] operation Active AEAD operation.
2589 * \param[in] input Buffer containing the message fragment to
2590 * encrypt or decrypt.
2591 * \param input_length Size of the \p input buffer in bytes.
2592 * \param[out] output Buffer where the output is to be written.
2593 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002594 * This must be at least
2595 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2596 * \p input_length) where \c alg is the
2597 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002598 * \param[out] output_length On success, the number of bytes
2599 * that make up the returned output.
2600 *
2601 * \retval #PSA_SUCCESS
2602 * Success.
2603 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002604 * The operation state is not valid (it must be active, have a nonce
2605 * set, and have lengths set if required by the algorithm).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002606 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2607 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002608 * You can determine a sufficient buffer size by calling
2609 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2610 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002611 * \retval #PSA_ERROR_INVALID_ARGUMENT
2612 * The total length of input to psa_aead_update_ad() so far is
2613 * less than the additional data length that was previously
2614 * specified with psa_aead_set_lengths().
2615 * \retval #PSA_ERROR_INVALID_ARGUMENT
2616 * The total input length overflows the plaintext length that
2617 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002618 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2619 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2620 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002621 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002622 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002623 * \retval #PSA_ERROR_BAD_STATE
2624 * The library has not been previously initialized by psa_crypto_init().
2625 * It is implementation-dependent whether a failure to initialize
2626 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002627 */
2628psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2629 const uint8_t *input,
2630 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002631 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002632 size_t output_size,
2633 size_t *output_length);
2634
2635/** Finish encrypting a message in an AEAD operation.
2636 *
2637 * The operation must have been set up with psa_aead_encrypt_setup().
2638 *
2639 * This function finishes the authentication of the additional data
2640 * formed by concatenating the inputs passed to preceding calls to
2641 * psa_aead_update_ad() with the plaintext formed by concatenating the
2642 * inputs passed to preceding calls to psa_aead_update().
2643 *
2644 * This function has two output buffers:
2645 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002646 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002647 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002648 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002649 * that the operation performs.
2650 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002651 * When this function returns successfuly, the operation becomes inactive.
2652 * If this function returns an error status, the operation enters an error
2653 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002654 *
2655 * \param[in,out] operation Active AEAD operation.
2656 * \param[out] ciphertext Buffer where the last part of the ciphertext
2657 * is to be written.
2658 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002659 * This must be at least
2660 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2661 * \c alg is the algorithm that is being
2662 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002663 * \param[out] ciphertext_length On success, the number of bytes of
2664 * returned ciphertext.
2665 * \param[out] tag Buffer where the authentication tag is
2666 * to be written.
2667 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002668 * This must be at least
2669 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2670 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002671 * \param[out] tag_length On success, the number of bytes
2672 * that make up the returned tag.
2673 *
2674 * \retval #PSA_SUCCESS
2675 * Success.
2676 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002677 * The operation state is not valid (it must be an active encryption
2678 * operation with a nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002679 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002680 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002681 * You can determine a sufficient buffer size for \p ciphertext by
2682 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2683 * where \c alg is the algorithm that is being calculated.
2684 * You can determine a sufficient buffer size for \p tag by
2685 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002686 * \retval #PSA_ERROR_INVALID_ARGUMENT
2687 * The total length of input to psa_aead_update_ad() so far is
2688 * less than the additional data length that was previously
2689 * specified with psa_aead_set_lengths().
2690 * \retval #PSA_ERROR_INVALID_ARGUMENT
2691 * The total length of input to psa_aead_update() so far is
2692 * less than the plaintext length that was previously
2693 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002694 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2695 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2696 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002697 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002698 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002699 * \retval #PSA_ERROR_BAD_STATE
2700 * The library has not been previously initialized by psa_crypto_init().
2701 * It is implementation-dependent whether a failure to initialize
2702 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002703 */
2704psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002705 uint8_t *ciphertext,
2706 size_t ciphertext_size,
2707 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002708 uint8_t *tag,
2709 size_t tag_size,
2710 size_t *tag_length);
2711
2712/** Finish authenticating and decrypting a message in an AEAD operation.
2713 *
2714 * The operation must have been set up with psa_aead_decrypt_setup().
2715 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002716 * This function finishes the authenticated decryption of the message
2717 * components:
2718 *
2719 * - The additional data consisting of the concatenation of the inputs
2720 * passed to preceding calls to psa_aead_update_ad().
2721 * - The ciphertext consisting of the concatenation of the inputs passed to
2722 * preceding calls to psa_aead_update().
2723 * - The tag passed to this function call.
2724 *
2725 * If the authentication tag is correct, this function outputs any remaining
2726 * plaintext and reports success. If the authentication tag is not correct,
2727 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002728 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002729 * When this function returns successfuly, the operation becomes inactive.
2730 * If this function returns an error status, the operation enters an error
2731 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002732 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002733 * \note Implementations shall make the best effort to ensure that the
2734 * comparison between the actual tag and the expected tag is performed
2735 * in constant time.
2736 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002737 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002738 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002739 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002740 * from previous calls to psa_aead_update()
2741 * that could not be processed until the end
2742 * of the input.
2743 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002744 * This must be at least
2745 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2746 * \c alg is the algorithm that is being
2747 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002748 * \param[out] plaintext_length On success, the number of bytes of
2749 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002750 * \param[in] tag Buffer containing the authentication tag.
2751 * \param tag_length Size of the \p tag buffer in bytes.
2752 *
2753 * \retval #PSA_SUCCESS
2754 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002755 * \retval #PSA_ERROR_INVALID_SIGNATURE
2756 * The calculations were successful, but the authentication tag is
2757 * not correct.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002758 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002759 * The operation state is not valid (it must be an active decryption
2760 * operation with a nonce set).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002761 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2762 * The size of the \p plaintext buffer is too small.
2763 * You can determine a sufficient buffer size for \p plaintext by
2764 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2765 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002766 * \retval #PSA_ERROR_INVALID_ARGUMENT
2767 * The total length of input to psa_aead_update_ad() so far is
2768 * less than the additional data length that was previously
2769 * specified with psa_aead_set_lengths().
2770 * \retval #PSA_ERROR_INVALID_ARGUMENT
2771 * The total length of input to psa_aead_update() so far is
2772 * less than the plaintext length that was previously
2773 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002774 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2775 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2776 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002777 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002778 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002779 * \retval #PSA_ERROR_BAD_STATE
2780 * The library has not been previously initialized by psa_crypto_init().
2781 * It is implementation-dependent whether a failure to initialize
2782 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002783 */
2784psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002785 uint8_t *plaintext,
2786 size_t plaintext_size,
2787 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002788 const uint8_t *tag,
2789 size_t tag_length);
2790
2791/** Abort an AEAD operation.
2792 *
2793 * Aborting an operation frees all associated resources except for the
2794 * \p operation structure itself. Once aborted, the operation object
2795 * can be reused for another operation by calling
2796 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2797 *
2798 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002799 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002800 *
2801 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002802 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2803 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002804 *
2805 * \param[in,out] operation Initialized AEAD operation.
2806 *
2807 * \retval #PSA_SUCCESS
Gilles Peskine30a9e412019-01-14 18:36:12 +01002808 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2809 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002810 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002811 * \retval #PSA_ERROR_BAD_STATE
2812 * The library has not been previously initialized by psa_crypto_init().
2813 * It is implementation-dependent whether a failure to initialize
2814 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002815 */
2816psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2817
Gilles Peskine3b555712018-03-03 21:27:57 +01002818/**@}*/
2819
Gilles Peskine20035e32018-02-03 22:44:14 +01002820/** \defgroup asymmetric Asymmetric cryptography
2821 * @{
2822 */
2823
2824/**
2825 * \brief Sign a hash or short message with a private key.
2826 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002827 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002828 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002829 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2830 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2831 * to determine the hash algorithm to use.
2832 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002833 * \param key Identifier of the key to use for the operation.
2834 * It must be an asymmetric key pair. The key must
Ronald Cron96783552020-10-19 12:06:30 +02002835 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002836 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002837 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002838 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002839 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002840 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002841 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002842 * \param[out] signature_length On success, the number of bytes
2843 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002844 *
Gilles Peskine28538492018-07-11 17:34:00 +02002845 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002846 * \retval #PSA_ERROR_INVALID_HANDLE
2847 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002848 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002849 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002850 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002851 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002852 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002853 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002854 * \retval #PSA_ERROR_NOT_SUPPORTED
2855 * \retval #PSA_ERROR_INVALID_ARGUMENT
2856 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2857 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2858 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002859 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002860 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002861 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002862 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002863 * The library has not been previously initialized by psa_crypto_init().
2864 * It is implementation-dependent whether a failure to initialize
2865 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002866 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002867psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002868 psa_algorithm_t alg,
2869 const uint8_t *hash,
2870 size_t hash_length,
2871 uint8_t *signature,
2872 size_t signature_size,
2873 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002874
2875/**
2876 * \brief Verify the signature a hash or short message using a public key.
2877 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002878 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002879 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002880 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2881 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2882 * to determine the hash algorithm to use.
2883 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002884 * \param key Identifier of the key to use for the operation. It
2885 * must be a public key or an asymmetric key pair. The
Ronald Cron96783552020-10-19 12:06:30 +02002886 * key must allow the usage
2887 * #PSA_KEY_USAGE_VERIFY_HASH.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002888 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002889 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002890 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002891 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002892 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002893 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002894 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002895 *
Gilles Peskine28538492018-07-11 17:34:00 +02002896 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002897 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002898 * \retval #PSA_ERROR_INVALID_HANDLE
2899 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002900 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002901 * The calculation was perfomed successfully, but the passed
2902 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002903 * \retval #PSA_ERROR_NOT_SUPPORTED
2904 * \retval #PSA_ERROR_INVALID_ARGUMENT
2905 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2906 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2907 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002908 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002909 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002910 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002911 * The library has not been previously initialized by psa_crypto_init().
2912 * It is implementation-dependent whether a failure to initialize
2913 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002914 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002915psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002916 psa_algorithm_t alg,
2917 const uint8_t *hash,
2918 size_t hash_length,
2919 const uint8_t *signature,
2920 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002921
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002922/**
2923 * \brief Encrypt a short message with a public key.
2924 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002925 * \param key Identifer of the key to use for the operation.
2926 * It must be a public key or an asymmetric key
2927 * pair. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002928 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002929 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02002930 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002931 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002932 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002933 * \param[in] salt A salt or label, if supported by the
2934 * encryption algorithm.
2935 * If the algorithm does not support a
2936 * salt, pass \c NULL.
2937 * If the algorithm supports an optional
2938 * salt and you do not want to pass a salt,
2939 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002940 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002941 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2942 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002943 * \param salt_length Size of the \p salt buffer in bytes.
2944 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002945 * \param[out] output Buffer where the encrypted message is to
2946 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002947 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002948 * \param[out] output_length On success, the number of bytes
2949 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002950 *
Gilles Peskine28538492018-07-11 17:34:00 +02002951 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002952 * \retval #PSA_ERROR_INVALID_HANDLE
2953 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002954 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002955 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002956 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002957 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002958 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002959 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002960 * \retval #PSA_ERROR_NOT_SUPPORTED
2961 * \retval #PSA_ERROR_INVALID_ARGUMENT
2962 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2963 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2964 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002965 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002966 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002967 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002968 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002969 * The library has not been previously initialized by psa_crypto_init().
2970 * It is implementation-dependent whether a failure to initialize
2971 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002972 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002973psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002974 psa_algorithm_t alg,
2975 const uint8_t *input,
2976 size_t input_length,
2977 const uint8_t *salt,
2978 size_t salt_length,
2979 uint8_t *output,
2980 size_t output_size,
2981 size_t *output_length);
2982
2983/**
2984 * \brief Decrypt a short message with a private key.
2985 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002986 * \param key Identifier of the key to use for the operation.
2987 * It must be an asymmetric key pair. It must
Ronald Cron96783552020-10-19 12:06:30 +02002988 * allow the usage #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002989 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02002990 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002991 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002992 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002993 * \param[in] salt A salt or label, if supported by the
2994 * encryption algorithm.
2995 * If the algorithm does not support a
2996 * salt, pass \c NULL.
2997 * If the algorithm supports an optional
2998 * salt and you do not want to pass a salt,
2999 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003000 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003001 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3002 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003003 * \param salt_length Size of the \p salt buffer in bytes.
3004 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003005 * \param[out] output Buffer where the decrypted message is to
3006 * be written.
3007 * \param output_size Size of the \c output buffer in bytes.
3008 * \param[out] output_length On success, the number of bytes
3009 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003010 *
Gilles Peskine28538492018-07-11 17:34:00 +02003011 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003012 * \retval #PSA_ERROR_INVALID_HANDLE
3013 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003014 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003015 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003016 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003017 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003018 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003019 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003020 * \retval #PSA_ERROR_NOT_SUPPORTED
3021 * \retval #PSA_ERROR_INVALID_ARGUMENT
3022 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3023 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3024 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003025 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003026 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003027 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3028 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003029 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003030 * The library has not been previously initialized by psa_crypto_init().
3031 * It is implementation-dependent whether a failure to initialize
3032 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003033 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003034psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003035 psa_algorithm_t alg,
3036 const uint8_t *input,
3037 size_t input_length,
3038 const uint8_t *salt,
3039 size_t salt_length,
3040 uint8_t *output,
3041 size_t output_size,
3042 size_t *output_length);
3043
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003044/**@}*/
3045
Gilles Peskine35675b62019-05-16 17:26:11 +02003046/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003047 * @{
3048 */
3049
Gilles Peskine35675b62019-05-16 17:26:11 +02003050/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003051 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003052 * Before calling any function on a key derivation operation object, the
3053 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003054 * - Set the structure to all-bits-zero, for example:
3055 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003056 * psa_key_derivation_operation_t operation;
3057 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003058 * \endcode
3059 * - Initialize the structure to logical zero values, for example:
3060 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003061 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003062 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003063 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003064 * for example:
3065 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003066 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003067 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003068 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003069 * to the structure, for example:
3070 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003071 * psa_key_derivation_operation_t operation;
3072 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003073 * \endcode
3074 *
3075 * This is an implementation-defined \c struct. Applications should not
3076 * make any assumptions about the content of this structure except
3077 * as directed by the documentation of a specific implementation.
3078 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003079typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003080
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003081/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003082 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003083 * This macro returns a suitable initializer for a key derivation operation
3084 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003085 */
3086#ifdef __DOXYGEN_ONLY__
3087/* This is an example definition for documentation purposes.
3088 * Implementations should define a suitable value in `crypto_struct.h`.
3089 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003090#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003091#endif
3092
Gilles Peskine35675b62019-05-16 17:26:11 +02003093/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003094 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003095static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003096
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003097/** Set up a key derivation operation.
3098 *
3099 * A key derivation algorithm takes some inputs and uses them to generate
3100 * a byte stream in a deterministic way.
3101 * This byte stream can be used to produce keys and other
3102 * cryptographic material.
3103 *
3104 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003105 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3106 * -# Call psa_key_derivation_setup() to select the algorithm.
3107 * -# Provide the inputs for the key derivation by calling
3108 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3109 * as appropriate. Which inputs are needed, in what order, and whether
3110 * they may be keys and if so of what type depends on the algorithm.
3111 * -# Optionally set the operation's maximum capacity with
3112 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3113 * of or after providing inputs. For some algorithms, this step is mandatory
3114 * because the output depends on the maximum capacity.
3115 * -# To derive a key, call psa_key_derivation_output_key().
3116 * To derive a byte string for a different purpose, call
3117 * psa_key_derivation_output_bytes().
3118 * Successive calls to these functions use successive output bytes
3119 * calculated by the key derivation algorithm.
3120 * -# Clean up the key derivation operation object with
3121 * psa_key_derivation_abort().
3122 *
3123 * If this function returns an error, the key derivation operation object is
3124 * not changed.
3125 *
3126 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3127 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003128 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003129 * Implementations must reject an attempt to derive a key of size 0.
3130 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003131 * \param[in,out] operation The key derivation operation object
3132 * to set up. It must
3133 * have been initialized but not set up yet.
3134 * \param alg The key derivation algorithm to compute
3135 * (\c PSA_ALG_XXX value such that
3136 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3137 *
3138 * \retval #PSA_SUCCESS
3139 * Success.
3140 * \retval #PSA_ERROR_INVALID_ARGUMENT
3141 * \c alg is not a key derivation algorithm.
3142 * \retval #PSA_ERROR_NOT_SUPPORTED
3143 * \c alg is not supported or is not a key derivation algorithm.
3144 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3145 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3146 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003147 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003148 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003149 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003150 * The operation state is not valid (it must be inactive).
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003151 * \retval #PSA_ERROR_BAD_STATE
3152 * The library has not been previously initialized by psa_crypto_init().
3153 * It is implementation-dependent whether a failure to initialize
3154 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003155 */
3156psa_status_t psa_key_derivation_setup(
3157 psa_key_derivation_operation_t *operation,
3158 psa_algorithm_t alg);
3159
Gilles Peskine35675b62019-05-16 17:26:11 +02003160/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003161 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003162 * The capacity of a key derivation is the maximum number of bytes that it can
3163 * return. When you get *N* bytes of output from a key derivation operation,
3164 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003165 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003166 * \param[in] operation The operation to query.
3167 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003168 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003169 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003170 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003171 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003172 * The operation state is not valid (it must be active).
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003173 * \retval #PSA_ERROR_HARDWARE_FAILURE
3174 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003175 * \retval #PSA_ERROR_BAD_STATE
3176 * The library has not been previously initialized by psa_crypto_init().
3177 * It is implementation-dependent whether a failure to initialize
3178 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003179 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003180psa_status_t psa_key_derivation_get_capacity(
3181 const psa_key_derivation_operation_t *operation,
3182 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003183
Gilles Peskine35675b62019-05-16 17:26:11 +02003184/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003185 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003186 * The capacity of a key derivation operation is the maximum number of bytes
3187 * that the key derivation operation can return from this point onwards.
3188 *
3189 * \param[in,out] operation The key derivation operation object to modify.
3190 * \param capacity The new capacity of the operation.
3191 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003192 * current capacity.
3193 *
3194 * \retval #PSA_SUCCESS
3195 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003196 * \p capacity is larger than the operation's current capacity.
3197 * In this case, the operation object remains valid and its capacity
3198 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003199 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003200 * The operation state is not valid (it must be active).
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003201 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003202 * \retval #PSA_ERROR_HARDWARE_FAILURE
3203 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003204 * \retval #PSA_ERROR_BAD_STATE
3205 * The library has not been previously initialized by psa_crypto_init().
3206 * It is implementation-dependent whether a failure to initialize
3207 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003208 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003209psa_status_t psa_key_derivation_set_capacity(
3210 psa_key_derivation_operation_t *operation,
3211 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003212
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003213/** Use the maximum possible capacity for a key derivation operation.
3214 *
3215 * Use this value as the capacity argument when setting up a key derivation
3216 * to indicate that the operation should have the maximum possible capacity.
3217 * The value of the maximum possible capacity depends on the key derivation
3218 * algorithm.
3219 */
3220#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3221
3222/** Provide an input for key derivation or key agreement.
3223 *
3224 * Which inputs are required and in what order depends on the algorithm.
3225 * Refer to the documentation of each key derivation or key agreement
3226 * algorithm for information.
3227 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003228 * This function passes direct inputs, which is usually correct for
3229 * non-secret inputs. To pass a secret input, which should be in a key
3230 * object, call psa_key_derivation_input_key() instead of this function.
3231 * Refer to the documentation of individual step types
3232 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3233 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003234 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003235 * If this function returns an error status, the operation enters an error
3236 * state and must be aborted by calling psa_key_derivation_abort().
3237 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003238 * \param[in,out] operation The key derivation operation object to use.
3239 * It must have been set up with
3240 * psa_key_derivation_setup() and must not
3241 * have produced any output yet.
3242 * \param step Which step the input data is for.
3243 * \param[in] data Input data to use.
3244 * \param data_length Size of the \p data buffer in bytes.
3245 *
3246 * \retval #PSA_SUCCESS
3247 * Success.
3248 * \retval #PSA_ERROR_INVALID_ARGUMENT
3249 * \c step is not compatible with the operation's algorithm.
3250 * \retval #PSA_ERROR_INVALID_ARGUMENT
3251 * \c step does not allow direct inputs.
3252 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3253 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3254 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003255 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003256 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003257 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003258 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003259 * \retval #PSA_ERROR_BAD_STATE
3260 * The library has not been previously initialized by psa_crypto_init().
3261 * It is implementation-dependent whether a failure to initialize
3262 * results in this error code.
3263 */
3264psa_status_t psa_key_derivation_input_bytes(
3265 psa_key_derivation_operation_t *operation,
3266 psa_key_derivation_step_t step,
3267 const uint8_t *data,
3268 size_t data_length);
3269
3270/** Provide an input for key derivation in the form of a key.
3271 *
3272 * Which inputs are required and in what order depends on the algorithm.
3273 * Refer to the documentation of each key derivation or key agreement
3274 * algorithm for information.
3275 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003276 * This function obtains input from a key object, which is usually correct for
3277 * secret inputs or for non-secret personalization strings kept in the key
3278 * store. To pass a non-secret parameter which is not in the key store,
3279 * call psa_key_derivation_input_bytes() instead of this function.
3280 * Refer to the documentation of individual step types
3281 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3282 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003283 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003284 * If this function returns an error status, the operation enters an error
3285 * state and must be aborted by calling psa_key_derivation_abort().
3286 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003287 * \param[in,out] operation The key derivation operation object to use.
3288 * It must have been set up with
3289 * psa_key_derivation_setup() and must not
3290 * have produced any output yet.
3291 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003292 * \param key Identifier of the key. It must have an
3293 * appropriate type for step and must allow the
Ronald Cron96783552020-10-19 12:06:30 +02003294 * usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003295 *
3296 * \retval #PSA_SUCCESS
3297 * Success.
3298 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003299 * \retval #PSA_ERROR_NOT_PERMITTED
3300 * \retval #PSA_ERROR_INVALID_ARGUMENT
3301 * \c step is not compatible with the operation's algorithm.
3302 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine224b0d62019-09-23 18:13:17 +02003303 * \c step does not allow key inputs of the given type
3304 * or does not allow key inputs at all.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003305 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3306 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3307 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003308 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003309 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003310 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003311 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003312 * \retval #PSA_ERROR_BAD_STATE
3313 * The library has not been previously initialized by psa_crypto_init().
3314 * It is implementation-dependent whether a failure to initialize
3315 * results in this error code.
3316 */
3317psa_status_t psa_key_derivation_input_key(
3318 psa_key_derivation_operation_t *operation,
3319 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003320 mbedtls_svc_key_id_t key);
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003321
3322/** Perform a key agreement and use the shared secret as input to a key
3323 * derivation.
3324 *
3325 * A key agreement algorithm takes two inputs: a private key \p private_key
3326 * a public key \p peer_key.
3327 * The result of this function is passed as input to a key derivation.
3328 * The output of this key derivation can be extracted by reading from the
3329 * resulting operation to produce keys and other cryptographic material.
3330 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003331 * If this function returns an error status, the operation enters an error
3332 * state and must be aborted by calling psa_key_derivation_abort().
3333 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003334 * \param[in,out] operation The key derivation operation object to use.
3335 * It must have been set up with
3336 * psa_key_derivation_setup() with a
3337 * key agreement and derivation algorithm
3338 * \c alg (\c PSA_ALG_XXX value such that
3339 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3340 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3341 * is false).
3342 * The operation must be ready for an
3343 * input of the type given by \p step.
3344 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003345 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003346 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003347 * \param[in] peer_key Public key of the peer. The peer key must be in the
3348 * same format that psa_import_key() accepts for the
3349 * public key type corresponding to the type of
3350 * private_key. That is, this function performs the
3351 * equivalent of
3352 * #psa_import_key(...,
3353 * `peer_key`, `peer_key_length`) where
3354 * with key attributes indicating the public key
3355 * type corresponding to the type of `private_key`.
3356 * For example, for EC keys, this means that peer_key
3357 * is interpreted as a point on the curve that the
3358 * private key is on. The standard formats for public
3359 * keys are documented in the documentation of
3360 * psa_export_public_key().
3361 * \param peer_key_length Size of \p peer_key in bytes.
3362 *
3363 * \retval #PSA_SUCCESS
3364 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01003365 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003366 * The operation state is not valid for this key agreement \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003367 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003368 * \retval #PSA_ERROR_NOT_PERMITTED
3369 * \retval #PSA_ERROR_INVALID_ARGUMENT
3370 * \c private_key is not compatible with \c alg,
3371 * or \p peer_key is not valid for \c alg or not compatible with
3372 * \c private_key.
3373 * \retval #PSA_ERROR_NOT_SUPPORTED
3374 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskine224b0d62019-09-23 18:13:17 +02003375 * \retval #PSA_ERROR_INVALID_ARGUMENT
3376 * \c step does not allow an input resulting from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003377 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3378 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3379 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003380 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003381 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003382 * \retval #PSA_ERROR_BAD_STATE
3383 * The library has not been previously initialized by psa_crypto_init().
3384 * It is implementation-dependent whether a failure to initialize
3385 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003386 */
3387psa_status_t psa_key_derivation_key_agreement(
3388 psa_key_derivation_operation_t *operation,
3389 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003390 mbedtls_svc_key_id_t private_key,
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003391 const uint8_t *peer_key,
3392 size_t peer_key_length);
3393
Gilles Peskine35675b62019-05-16 17:26:11 +02003394/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003395 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003396 * This function calculates output bytes from a key derivation algorithm and
3397 * return those bytes.
3398 * If you view the key derivation's output as a stream of bytes, this
3399 * function destructively reads the requested number of bytes from the
3400 * stream.
3401 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003402 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003403 * If this function returns an error status other than
3404 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003405 * state and must be aborted by calling psa_key_derivation_abort().
3406 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003407 * \param[in,out] operation The key derivation operation object to read from.
3408 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003409 * \param output_length Number of bytes to output.
3410 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003411 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003412 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003413 * The operation's capacity was less than
3414 * \p output_length bytes. Note that in this case,
3415 * no output is written to the output buffer.
3416 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003417 * subsequent calls to this function will not
3418 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003419 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003420 * The operation state is not valid (it must be active and completed
3421 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003422 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3423 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3424 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003425 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003426 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003427 * \retval #PSA_ERROR_BAD_STATE
3428 * The library has not been previously initialized by psa_crypto_init().
3429 * It is implementation-dependent whether a failure to initialize
3430 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003431 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003432psa_status_t psa_key_derivation_output_bytes(
3433 psa_key_derivation_operation_t *operation,
3434 uint8_t *output,
3435 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003436
Gilles Peskine35675b62019-05-16 17:26:11 +02003437/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003438 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003439 * This function calculates output bytes from a key derivation algorithm
3440 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003441 * The key's location, usage policy, type and size are taken from
3442 * \p attributes.
3443 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003444 * If you view the key derivation's output as a stream of bytes, this
3445 * function destructively reads as many bytes as required from the
3446 * stream.
3447 * The operation's capacity decreases by the number of bytes read.
3448 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003449 * If this function returns an error status other than
3450 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003451 * state and must be aborted by calling psa_key_derivation_abort().
3452 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003453 * How much output is produced and consumed from the operation, and how
3454 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003455 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003456 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003457 * of a given size, this function is functionally equivalent to
3458 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003459 * and passing the resulting output to #psa_import_key.
3460 * However, this function has a security benefit:
3461 * if the implementation provides an isolation boundary then
3462 * the key material is not exposed outside the isolation boundary.
3463 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003464 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003465 * The following key types defined in this specification follow this scheme:
3466 *
3467 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003468 * - #PSA_KEY_TYPE_ARC4;
3469 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003470 * - #PSA_KEY_TYPE_DERIVE;
3471 * - #PSA_KEY_TYPE_HMAC.
3472 *
3473 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003474 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003475 * Montgomery curve), this function always draws a byte string whose
3476 * length is determined by the curve, and sets the mandatory bits
3477 * accordingly. That is:
3478 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01003479 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003480 * string and process it as specified in RFC 7748 &sect;5.
Paul Elliott8ff510a2020-06-02 17:19:28 +01003481 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003482 * string and process it as specified in RFC 7748 &sect;5.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003483 *
3484 * - For key types for which the key is represented by a single sequence of
3485 * \p bits bits with constraints as to which bit sequences are acceptable,
3486 * this function draws a byte string of length (\p bits / 8) bytes rounded
3487 * up to the nearest whole number of bytes. If the resulting byte string
3488 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3489 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003490 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003491 * for the output produced by psa_export_key().
3492 * The following key types defined in this specification follow this scheme:
3493 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003494 * - #PSA_KEY_TYPE_DES.
3495 * Force-set the parity bits, but discard forbidden weak keys.
3496 * For 2-key and 3-key triple-DES, the three keys are generated
3497 * successively (for example, for 3-key triple-DES,
3498 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3499 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003500 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003501 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003502 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003503 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003504 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003505 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003506 * Weierstrass curve).
3507 * For these key types, interpret the byte string as integer
3508 * in big-endian order. Discard it if it is not in the range
3509 * [0, *N* - 2] where *N* is the boundary of the private key domain
3510 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003511 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003512 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003513 * This method allows compliance to NIST standards, specifically
3514 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003515 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3516 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3517 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3518 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003519 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003520 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003521 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003522 * implementation-defined.
3523 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003524 * In all cases, the data that is read is discarded from the operation.
3525 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003526 *
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003527 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3528 * the input to that step must be provided with psa_key_derivation_input_key().
3529 * Future versions of this specification may include additional restrictions
3530 * on the derived key based on the attributes and strength of the secret key.
3531 *
Gilles Peskine20628592019-04-19 19:29:50 +02003532 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003533 * \param[in,out] operation The key derivation operation object to read from.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003534 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003535 * key. For persistent keys, this is the key
3536 * identifier defined in \p attributes.
3537 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003538 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003539 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003540 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003541 * If the key is persistent, the key material and the key's metadata
3542 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003543 * \retval #PSA_ERROR_ALREADY_EXISTS
3544 * This is an attempt to create a persistent key, and there is
3545 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003546 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003547 * There was not enough data to create the desired key.
3548 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003549 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003550 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003551 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003552 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003553 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003554 * \retval #PSA_ERROR_INVALID_ARGUMENT
3555 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003556 * \retval #PSA_ERROR_NOT_PERMITTED
3557 * The #PSA_KEY_DERIVATION_INPUT_SECRET input was not provided through
3558 * a key.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003559 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003560 * The operation state is not valid (it must be active and completed
3561 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003562 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3563 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3564 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3565 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003566 * \retval #PSA_ERROR_CORRUPTION_DETECTED
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01003567 * \retval #PSA_ERROR_DATA_INVALID
3568 * \retval #PSA_ERROR_DATA_CORRUPT
itayzafrir90d8c7a2018-09-12 11:44:52 +03003569 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003570 * The library has not been previously initialized by psa_crypto_init().
3571 * It is implementation-dependent whether a failure to initialize
3572 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003573 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003574psa_status_t psa_key_derivation_output_key(
3575 const psa_key_attributes_t *attributes,
3576 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003577 mbedtls_svc_key_id_t *key);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003578
Gilles Peskine35675b62019-05-16 17:26:11 +02003579/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003580 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003581 * Aborting an operation frees all associated resources except for the \c
3582 * operation structure itself. Once aborted, the operation object can be reused
3583 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003584 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003585 * This function may be called at any time after the operation
3586 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003587 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003588 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3589 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003590 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003591 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003592 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003593 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003594 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3595 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003596 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003597 * \retval #PSA_ERROR_BAD_STATE
3598 * The library has not been previously initialized by psa_crypto_init().
3599 * It is implementation-dependent whether a failure to initialize
3600 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003601 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003602psa_status_t psa_key_derivation_abort(
3603 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003604
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003605/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003606 *
3607 * \warning The raw result of a key agreement algorithm such as finite-field
3608 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3609 * not be used directly as key material. It should instead be passed as
3610 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003611 * a key derivation, use psa_key_derivation_key_agreement() and other
3612 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003613 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003614 * \param alg The key agreement algorithm to compute
3615 * (\c PSA_ALG_XXX value such that
3616 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3617 * is true).
Ronald Croncf56a0a2020-08-04 09:51:30 +02003618 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003619 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003620 * \param[in] peer_key Public key of the peer. It must be
3621 * in the same format that psa_import_key()
3622 * accepts. The standard formats for public
3623 * keys are documented in the documentation
3624 * of psa_export_public_key().
3625 * \param peer_key_length Size of \p peer_key in bytes.
3626 * \param[out] output Buffer where the decrypted message is to
3627 * be written.
3628 * \param output_size Size of the \c output buffer in bytes.
3629 * \param[out] output_length On success, the number of bytes
3630 * that make up the returned output.
3631 *
3632 * \retval #PSA_SUCCESS
3633 * Success.
3634 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003635 * \retval #PSA_ERROR_NOT_PERMITTED
3636 * \retval #PSA_ERROR_INVALID_ARGUMENT
3637 * \p alg is not a key agreement algorithm
3638 * \retval #PSA_ERROR_INVALID_ARGUMENT
3639 * \p private_key is not compatible with \p alg,
3640 * or \p peer_key is not valid for \p alg or not compatible with
3641 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003642 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3643 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003644 * \retval #PSA_ERROR_NOT_SUPPORTED
3645 * \p alg is not a supported key agreement algorithm.
3646 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3647 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3648 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003649 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003650 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003651 * \retval #PSA_ERROR_BAD_STATE
3652 * The library has not been previously initialized by psa_crypto_init().
3653 * It is implementation-dependent whether a failure to initialize
3654 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003655 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003656psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003657 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02003658 const uint8_t *peer_key,
3659 size_t peer_key_length,
3660 uint8_t *output,
3661 size_t output_size,
3662 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003663
Gilles Peskineea0fb492018-07-12 17:17:20 +02003664/**@}*/
3665
Gilles Peskineedd76872018-07-20 17:42:05 +02003666/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003667 * @{
3668 */
3669
3670/**
3671 * \brief Generate random bytes.
3672 *
3673 * \warning This function **can** fail! Callers MUST check the return status
3674 * and MUST NOT use the content of the output buffer if the return
3675 * status is not #PSA_SUCCESS.
3676 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003677 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003678 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003679 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003680 * \param output_size Number of bytes to generate and output.
3681 *
Gilles Peskine28538492018-07-11 17:34:00 +02003682 * \retval #PSA_SUCCESS
3683 * \retval #PSA_ERROR_NOT_SUPPORTED
3684 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003685 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003686 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3687 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003688 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003689 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003690 * The library has not been previously initialized by psa_crypto_init().
3691 * It is implementation-dependent whether a failure to initialize
3692 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003693 */
3694psa_status_t psa_generate_random(uint8_t *output,
3695 size_t output_size);
3696
3697/**
3698 * \brief Generate a key or key pair.
3699 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003700 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02003701 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003702 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003703 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003704 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003705 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003706 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003707 * the public exponent is 65537.
3708 * The modulus is a product of two probabilistic primes
3709 * between 2^{n-1} and 2^n where n is the bit size specified in the
3710 * attributes.
3711 *
Gilles Peskine20628592019-04-19 19:29:50 +02003712 * \param[in] attributes The attributes for the new key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003713 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003714 * key. For persistent keys, this is the key
3715 * identifier defined in \p attributes.
3716 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003717 *
Gilles Peskine28538492018-07-11 17:34:00 +02003718 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003719 * Success.
3720 * If the key is persistent, the key material and the key's metadata
3721 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003722 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003723 * This is an attempt to create a persistent key, and there is
3724 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003725 * \retval #PSA_ERROR_NOT_SUPPORTED
3726 * \retval #PSA_ERROR_INVALID_ARGUMENT
3727 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3728 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3729 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3730 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003731 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003732 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01003733 * \retval #PSA_ERROR_DATA_INVALID
3734 * \retval #PSA_ERROR_DATA_CORRUPT
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 */