blob: 4aad09452012e95a4c59dc382c3e14a9b8b94f44 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02006 * Copyright The Mbed TLS Contributors
Jaeden Amerocab54942018-07-25 13:26:13 +01007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
39/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010040#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010041
Gilles Peskinee59236f2018-01-27 23:32:46 +010042#ifdef __cplusplus
43extern "C" {
44#endif
45
Gilles Peskinef3b731e2018-12-12 13:38:31 +010046/* The file "crypto_types.h" declares types that encode errors,
47 * algorithms, key types, policies, etc. */
48#include "crypto_types.h"
49
Andrew Thoelke02b372b2019-10-02 09:32:21 +010050/** \defgroup version API version
Adrian L. Shawd89338a2019-09-19 13:32:57 +010051 * @{
52 */
53
54/**
55 * The major version of this implementation of the PSA Crypto API
56 */
57#define PSA_CRYPTO_API_VERSION_MAJOR 1
58
59/**
60 * The minor version of this implementation of the PSA Crypto API
61 */
62#define PSA_CRYPTO_API_VERSION_MINOR 0
63
64/**@}*/
65
Gilles Peskinef3b731e2018-12-12 13:38:31 +010066/* The file "crypto_values.h" declares macros to build and analyze values
67 * of integral types defined in "crypto_types.h". */
68#include "crypto_values.h"
69
70/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010071 * @{
72 */
73
74/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010075 * \brief Library initialization.
76 *
77 * Applications must call this function before calling any other
78 * function in this module.
79 *
80 * Applications may call this function more than once. Once a call
81 * succeeds, subsequent calls are guaranteed to succeed.
82 *
itayzafrir18617092018-09-16 12:22:41 +030083 * If the application calls other functions before calling psa_crypto_init(),
84 * the behavior is undefined. Implementations are encouraged to either perform
85 * the operation as if the library had been initialized or to return
86 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
87 * implementations should not return a success status if the lack of
88 * initialization may have security implications, for example due to improper
89 * seeding of the random number generator.
90 *
Gilles Peskine28538492018-07-11 17:34:00 +020091 * \retval #PSA_SUCCESS
92 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
gabor-mezei-arm452b0a32020-11-09 17:42:55 +010093 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine28538492018-07-11 17:34:00 +020094 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
95 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +020096 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +020097 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
gabor-mezei-arm452b0a32020-11-09 17:42:55 +010098 * \retval #PSA_ERROR_STORAGE_FAILURE
99 * \retval #PSA_ERROR_DATA_INVALID
100 * \retval #PSA_ERROR_DATA_CORRUPT
Gilles Peskinee59236f2018-01-27 23:32:46 +0100101 */
102psa_status_t psa_crypto_init(void);
103
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100104/**@}*/
105
Gilles Peskine105f67f2019-07-23 18:16:05 +0200106/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +0200107 * @{
108 */
109
Gilles Peskinea0c06552019-05-21 15:54:54 +0200110/** \def PSA_KEY_ATTRIBUTES_INIT
111 *
112 * This macro returns a suitable initializer for a key attribute structure
113 * of type #psa_key_attributes_t.
114 */
115#ifdef __DOXYGEN_ONLY__
116/* This is an example definition for documentation purposes.
117 * Implementations should define a suitable value in `crypto_struct.h`.
118 */
119#define PSA_KEY_ATTRIBUTES_INIT {0}
120#endif
121
122/** Return an initial value for a key attributes structure.
123 */
124static psa_key_attributes_t psa_key_attributes_init(void);
125
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200126/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200127 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200128 * If the attribute structure currently declares the key as volatile (which
129 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200130 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200131 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200132 * This function does not access storage, it merely stores the given
133 * value in the structure.
134 * The persistent key will be written to storage when the attribute
135 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200136 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200137 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200138 *
Gilles Peskine20628592019-04-19 19:29:50 +0200139 * This function may be declared as `static` (i.e. without external
140 * linkage). This function may be provided as a function-like macro,
141 * but in this case it must evaluate each of its arguments exactly once.
142 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200143 * \param[out] attributes The attribute structure to write to.
144 * \param key The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200145 */
Ronald Cron71016a92020-08-28 19:01:50 +0200146static void psa_set_key_id( psa_key_attributes_t *attributes,
147 mbedtls_svc_key_id_t key );
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200148
Ronald Cron6b5ff532020-10-16 14:38:19 +0200149#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
150/** Set the owner identifier of a key.
151 *
152 * When key identifiers encode key owner identifiers, psa_set_key_id() does
153 * not allow to define in key attributes the owner of volatile keys as
154 * psa_set_key_id() enforces the key to be persistent.
155 *
156 * This function allows to set in key attributes the owner identifier of a
157 * key. It is intended to be used for volatile keys. For persistent keys,
158 * it is recommended to use the PSA Cryptography API psa_set_key_id() to define
159 * the owner of a key.
160 *
161 * \param[out] attributes The attribute structure to write to.
162 * \param owner_id The key owner identifier.
163 */
164static void mbedtls_set_key_owner_id( psa_key_attributes_t *attributes,
165 mbedtls_key_owner_id_t owner_id );
166#endif
167
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200168/** Set the location of a persistent key.
169 *
170 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200171 * with psa_set_key_id(). By default, a key that has a persistent identifier
172 * is stored in the default storage area identifier by
173 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
174 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200175 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200176 * This function does not access storage, it merely stores the given
177 * value in the structure.
178 * The persistent key will be written to storage when the attribute
179 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200180 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200181 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200182 *
183 * This function may be declared as `static` (i.e. without external
184 * linkage). This function may be provided as a function-like macro,
185 * but in this case it must evaluate each of its arguments exactly once.
186 *
187 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200188 * \param lifetime The lifetime for the key.
189 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200190 * key will be volatile, and the key identifier
191 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200192 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200193static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
194 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200195
Gilles Peskine20628592019-04-19 19:29:50 +0200196/** Retrieve the key identifier from key attributes.
197 *
198 * This function may be declared as `static` (i.e. without external
199 * linkage). This function may be provided as a function-like macro,
200 * but in this case it must evaluate its argument exactly once.
201 *
202 * \param[in] attributes The key attribute structure to query.
203 *
204 * \return The persistent identifier stored in the attribute structure.
205 * This value is unspecified if the attribute structure declares
206 * the key as volatile.
207 */
Ronald Cron71016a92020-08-28 19:01:50 +0200208static mbedtls_svc_key_id_t psa_get_key_id(
209 const psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200210
Gilles Peskine20628592019-04-19 19:29:50 +0200211/** Retrieve the lifetime from key attributes.
212 *
213 * This function may be declared as `static` (i.e. without external
214 * linkage). This function may be provided as a function-like macro,
215 * but in this case it must evaluate its argument exactly once.
216 *
217 * \param[in] attributes The key attribute structure to query.
218 *
219 * \return The lifetime value stored in the attribute structure.
220 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200221static psa_key_lifetime_t psa_get_key_lifetime(
222 const psa_key_attributes_t *attributes);
223
Gilles Peskine20628592019-04-19 19:29:50 +0200224/** Declare usage flags for a key.
225 *
226 * Usage flags are part of a key's usage policy. They encode what
227 * kind of operations are permitted on the key. For more details,
228 * refer to the documentation of the type #psa_key_usage_t.
229 *
230 * This function overwrites any usage flags
231 * previously set in \p attributes.
232 *
233 * This function may be declared as `static` (i.e. without external
234 * linkage). This function may be provided as a function-like macro,
235 * but in this case it must evaluate each of its arguments exactly once.
236 *
237 * \param[out] attributes The attribute structure to write to.
238 * \param usage_flags The usage flags to write.
239 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200240static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
241 psa_key_usage_t usage_flags);
242
Gilles Peskine20628592019-04-19 19:29:50 +0200243/** Retrieve the usage flags from key attributes.
244 *
245 * This function may be declared as `static` (i.e. without external
246 * linkage). This function may be provided as a function-like macro,
247 * but in this case it must evaluate its argument exactly once.
248 *
249 * \param[in] attributes The key attribute structure to query.
250 *
251 * \return The usage flags stored in the attribute structure.
252 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200253static psa_key_usage_t psa_get_key_usage_flags(
254 const psa_key_attributes_t *attributes);
255
Gilles Peskine20628592019-04-19 19:29:50 +0200256/** Declare the permitted algorithm policy for a key.
257 *
258 * The permitted algorithm policy of a key encodes which algorithm or
Gilles Peskinea170d922019-09-12 16:59:37 +0200259 * algorithms are permitted to be used with this key. The following
260 * algorithm policies are supported:
261 * - 0 does not allow any cryptographic operation with the key. The key
262 * may be used for non-cryptographic actions such as exporting (if
263 * permitted by the usage flags).
264 * - An algorithm value permits this particular algorithm.
265 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
266 * signature scheme with any hash algorithm.
Steven Cooremanee18b1f2021-02-08 11:44:21 +0100267 * - An algorithm built from #PSA_ALG_MAC_WITH_MINIMUM_LENGTH_TAG allows
268 * any MAC algorithm from the same base class (e.g. CMAC) which
269 * generates/verifies a MAC length greater than or equal to the length
270 * encoded in the wildcard algorithm.
271 * - An algorithm built from #PSA_ALG_AEAD_WITH_MINIMUM_LENGTH_TAG allows
272 * any AEAD algorithm from the same base class (e.g. CCM) which
273 * generates/verifies a tag length greater than or equal to the length
274 * encoded in the wildcard algorithm.
Gilles Peskine20628592019-04-19 19:29:50 +0200275 *
276 * This function overwrites any algorithm policy
277 * previously set in \p attributes.
278 *
279 * This function may be declared as `static` (i.e. without external
280 * linkage). This function may be provided as a function-like macro,
281 * but in this case it must evaluate each of its arguments exactly once.
282 *
283 * \param[out] attributes The attribute structure to write to.
284 * \param alg The permitted algorithm policy to write.
285 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200286static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
287 psa_algorithm_t alg);
288
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100289
Gilles Peskine20628592019-04-19 19:29:50 +0200290/** Retrieve the algorithm policy from key attributes.
291 *
292 * This function may be declared as `static` (i.e. without external
293 * linkage). This function may be provided as a function-like macro,
294 * but in this case it must evaluate its argument exactly once.
295 *
296 * \param[in] attributes The key attribute structure to query.
297 *
298 * \return The algorithm stored in the attribute structure.
299 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200300static psa_algorithm_t psa_get_key_algorithm(
301 const psa_key_attributes_t *attributes);
302
Gilles Peskine20628592019-04-19 19:29:50 +0200303/** Declare the type of a key.
304 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200305 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200306 * previously set in \p attributes.
307 *
308 * This function may be declared as `static` (i.e. without external
309 * linkage). This function may be provided as a function-like macro,
310 * but in this case it must evaluate each of its arguments exactly once.
311 *
312 * \param[out] attributes The attribute structure to write to.
313 * \param type The key type to write.
Gilles Peskinea170d922019-09-12 16:59:37 +0200314 * If this is 0, the key type in \p attributes
315 * becomes unspecified.
Gilles Peskine20628592019-04-19 19:29:50 +0200316 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200317static void psa_set_key_type(psa_key_attributes_t *attributes,
318 psa_key_type_t type);
319
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100320
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200321/** Declare the size of a key.
322 *
323 * This function overwrites any key size previously set in \p attributes.
324 *
325 * This function may be declared as `static` (i.e. without external
326 * linkage). This function may be provided as a function-like macro,
327 * but in this case it must evaluate each of its arguments exactly once.
328 *
329 * \param[out] attributes The attribute structure to write to.
330 * \param bits The key size in bits.
Gilles Peskinea170d922019-09-12 16:59:37 +0200331 * If this is 0, the key size in \p attributes
Gilles Peskine05c900b2019-09-12 18:29:43 +0200332 * becomes unspecified. Keys of size 0 are
333 * not supported.
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200334 */
335static void psa_set_key_bits(psa_key_attributes_t *attributes,
336 size_t bits);
337
Gilles Peskine20628592019-04-19 19:29:50 +0200338/** Retrieve the key type from key attributes.
339 *
340 * This function may be declared as `static` (i.e. without external
341 * linkage). This function may be provided as a function-like macro,
342 * but in this case it must evaluate its argument exactly once.
343 *
344 * \param[in] attributes The key attribute structure to query.
345 *
346 * \return The key type stored in the attribute structure.
347 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200348static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
349
Gilles Peskine20628592019-04-19 19:29:50 +0200350/** Retrieve the key size from key attributes.
351 *
352 * This function may be declared as `static` (i.e. without external
353 * linkage). This function may be provided as a function-like macro,
354 * but in this case it must evaluate its argument exactly once.
355 *
356 * \param[in] attributes The key attribute structure to query.
357 *
358 * \return The key size stored in the attribute structure, in bits.
359 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200360static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
361
Gilles Peskine20628592019-04-19 19:29:50 +0200362/** Retrieve the attributes of a key.
363 *
364 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200365 * psa_reset_key_attributes(). It then copies the attributes of
366 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200367 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200368 * \note This function may allocate memory or other resources.
369 * Once you have called this function on an attribute structure,
370 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200371 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200372 * \param[in] key Identifier of the key to query.
Gilles Peskine20628592019-04-19 19:29:50 +0200373 * \param[in,out] attributes On success, the attributes of the key.
374 * On failure, equivalent to a
375 * freshly-initialized structure.
376 *
377 * \retval #PSA_SUCCESS
378 * \retval #PSA_ERROR_INVALID_HANDLE
379 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
380 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100381 * \retval #PSA_ERROR_CORRUPTION_DETECTED
382 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100383 * \retval #PSA_ERROR_DATA_CORRUPT
384 * \retval #PSA_ERROR_DATA_INVALID
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100385 * \retval #PSA_ERROR_BAD_STATE
386 * The library has not been previously initialized by psa_crypto_init().
387 * It is implementation-dependent whether a failure to initialize
388 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200389 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200390psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
Gilles Peskine4747d192019-04-17 15:05:45 +0200391 psa_key_attributes_t *attributes);
392
Gilles Peskine20628592019-04-19 19:29:50 +0200393/** Reset a key attribute structure to a freshly initialized state.
394 *
395 * You must initialize the attribute structure as described in the
396 * documentation of the type #psa_key_attributes_t before calling this
397 * function. Once the structure has been initialized, you may call this
398 * function at any time.
399 *
400 * This function frees any auxiliary resources that the structure
401 * may contain.
402 *
403 * \param[in,out] attributes The attribute structure to reset.
404 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200405void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200406
Gilles Peskine87a5e562019-04-17 12:28:25 +0200407/**@}*/
408
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100409/** \defgroup key_management Key management
410 * @{
411 */
412
Ronald Cron277a85f2020-08-04 15:49:48 +0200413/** Remove non-essential copies of key material from memory.
414 *
415 * If the key identifier designates a volatile key, this functions does not do
416 * anything and returns successfully.
417 *
418 * If the key identifier designates a persistent key, then this function will
419 * free all resources associated with the key in volatile memory. The key
420 * data in persistent storage is not affected and the key can still be used.
421 *
422 * \param key Identifier of the key to purge.
423 *
424 * \retval #PSA_SUCCESS
425 * The key material will have been removed from memory if it is not
426 * currently required.
427 * \retval #PSA_ERROR_INVALID_ARGUMENT
428 * \p key is not a valid key identifier.
429 * \retval #PSA_ERROR_BAD_STATE
430 * The library has not been previously initialized by psa_crypto_init().
431 * It is implementation-dependent whether a failure to initialize
432 * results in this error code.
433 */
434psa_status_t psa_purge_key(mbedtls_svc_key_id_t key);
435
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100436/** Make a copy of a key.
437 *
438 * Copy key material from one location to another.
439 *
440 * This function is primarily useful to copy a key from one location
441 * to another, since it populates a key using the material from
442 * another key which may have a different lifetime.
443 *
444 * This function may be used to share a key with a different party,
445 * subject to implementation-defined restrictions on key sharing.
446 *
447 * The policy on the source key must have the usage flag
448 * #PSA_KEY_USAGE_COPY set.
449 * This flag is sufficient to permit the copy if the key has the lifetime
450 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
451 * Some secure elements do not provide a way to copy a key without
452 * making it extractable from the secure element. If a key is located
453 * in such a secure element, then the key must have both usage flags
454 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
455 * a copy of the key outside the secure element.
456 *
457 * The resulting key may only be used in a way that conforms to
458 * both the policy of the original key and the policy specified in
459 * the \p attributes parameter:
460 * - The usage flags on the resulting key are the bitwise-and of the
461 * usage flags on the source policy and the usage flags in \p attributes.
462 * - If both allow the same algorithm or wildcard-based
463 * algorithm policy, the resulting key has the same algorithm policy.
464 * - If either of the policies allows an algorithm and the other policy
465 * allows a wildcard-based algorithm policy that includes this algorithm,
466 * the resulting key allows the same algorithm.
467 * - If the policies do not allow any algorithm in common, this function
468 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
469 *
470 * The effect of this function on implementation-defined attributes is
471 * implementation-defined.
472 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200473 * \param source_key The key to copy. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +0200474 * #PSA_KEY_USAGE_COPY. If a private or secret key is
Ronald Croncf56a0a2020-08-04 09:51:30 +0200475 * being copied outside of a secure element it must
Ronald Cron96783552020-10-19 12:06:30 +0200476 * also allow #PSA_KEY_USAGE_EXPORT.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100477 * \param[in] attributes The attributes for the new key.
478 * They are used as follows:
479 * - The key type and size may be 0. If either is
480 * nonzero, it must match the corresponding
481 * attribute of the source key.
482 * - The key location (the lifetime and, for
483 * persistent keys, the key identifier) is
484 * used directly.
485 * - The policy constraints (usage flags and
486 * algorithm policy) are combined from
487 * the source key and \p attributes so that
488 * both sets of restrictions apply, as
489 * described in the documentation of this function.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200490 * \param[out] target_key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +0200491 * key. For persistent keys, this is the key
492 * identifier defined in \p attributes.
493 * \c 0 on failure.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100494 *
495 * \retval #PSA_SUCCESS
496 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200497 * \p source_key is invalid.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100498 * \retval #PSA_ERROR_ALREADY_EXISTS
499 * This is an attempt to create a persistent key, and there is
500 * already a persistent key with the given identifier.
501 * \retval #PSA_ERROR_INVALID_ARGUMENT
502 * The lifetime or identifier in \p attributes are invalid.
503 * \retval #PSA_ERROR_INVALID_ARGUMENT
504 * The policy constraints on the source and specified in
505 * \p attributes are incompatible.
506 * \retval #PSA_ERROR_INVALID_ARGUMENT
507 * \p attributes specifies a key type or key size
508 * which does not match the attributes of the source key.
509 * \retval #PSA_ERROR_NOT_PERMITTED
510 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
511 * \retval #PSA_ERROR_NOT_PERMITTED
512 * The source key is not exportable and its lifetime does not
513 * allow copying it to the target's lifetime.
514 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
515 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
516 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
517 * \retval #PSA_ERROR_HARDWARE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100518 * \retval #PSA_ERROR_DATA_INVALID
519 * \retval #PSA_ERROR_DATA_CORRUPT
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100520 * \retval #PSA_ERROR_STORAGE_FAILURE
521 * \retval #PSA_ERROR_CORRUPTION_DETECTED
522 * \retval #PSA_ERROR_BAD_STATE
523 * The library has not been previously initialized by psa_crypto_init().
524 * It is implementation-dependent whether a failure to initialize
525 * results in this error code.
526 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200527psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100528 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200529 mbedtls_svc_key_id_t *target_key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100530
531
532/**
533 * \brief Destroy a key.
534 *
535 * This function destroys a key from both volatile
536 * memory and, if applicable, non-volatile storage. Implementations shall
537 * make a best effort to ensure that that the key material cannot be recovered.
538 *
539 * This function also erases any metadata such as policies and frees
Ronald Croncf56a0a2020-08-04 09:51:30 +0200540 * resources associated with the key.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100541 *
542 * If a key is currently in use in a multipart operation, then destroying the
543 * key will cause the multipart operation to fail.
544 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200545 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
Ronald Cron96783552020-10-19 12:06:30 +0200546 * return #PSA_SUCCESS.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100547 *
548 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +0200549 * \p key was a valid identifier and the key material that it
550 * referred to has been erased. Alternatively, \p key is \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100551 * \retval #PSA_ERROR_NOT_PERMITTED
552 * The key cannot be erased because it is
553 * read-only, either due to a policy or due to physical restrictions.
554 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200555 * \p key is not a valid identifier nor \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100556 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
557 * There was an failure in communication with the cryptoprocessor.
558 * The key material may still be present in the cryptoprocessor.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100559 * \retval #PSA_ERROR_DATA_INVALID
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100560 * This error is typically a result of either storage corruption on a
561 * cleartext storage backend, or an attempt to read data that was
562 * written by an incompatible version of the library.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100563 * \retval #PSA_ERROR_STORAGE_FAILURE
564 * The storage is corrupted. Implementations shall make a best effort
565 * to erase key material even in this stage, however applications
566 * should be aware that it may be impossible to guarantee that the
567 * key material is not recoverable in such cases.
568 * \retval #PSA_ERROR_CORRUPTION_DETECTED
569 * An unexpected condition which is not a storage corruption or
570 * a communication failure occurred. The cryptoprocessor may have
571 * been compromised.
572 * \retval #PSA_ERROR_BAD_STATE
573 * The library has not been previously initialized by psa_crypto_init().
574 * It is implementation-dependent whether a failure to initialize
575 * results in this error code.
576 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200577psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100578
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100579/**@}*/
580
581/** \defgroup import_export Key import and export
582 * @{
583 */
584
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100585/**
586 * \brief Import a key in binary format.
587 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100588 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100589 * documentation of psa_export_public_key() for the format of public keys
590 * and to the documentation of psa_export_key() for the format for
591 * other key types.
592 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200593 * The key data determines the key size. The attributes may optionally
594 * specify a key size; in this case it must match the size determined
595 * from the key data. A key size of 0 in \p attributes indicates that
596 * the key size is solely determined by the key data.
597 *
598 * Implementations must reject an attempt to import a key of size 0.
599 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100600 * This specification supports a single format for each key type.
601 * Implementations may support other formats as long as the standard
602 * format is supported. Implementations that support other formats
603 * should ensure that the formats are clearly unambiguous so as to
604 * minimize the risk that an invalid input is accidentally interpreted
605 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100606 *
Gilles Peskine20628592019-04-19 19:29:50 +0200607 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200608 * The key size is always determined from the
609 * \p data buffer.
610 * If the key size in \p attributes is nonzero,
611 * it must be equal to the size from \p data.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200612 * \param[out] key On success, an identifier to the newly created key.
Ronald Cron4067d1c2020-10-19 13:34:38 +0200613 * For persistent keys, this is the key identifier
614 * defined in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200615 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100616 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200617 * buffer is interpreted according to the type declared
618 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200619 * All implementations must support at least the format
620 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100621 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200622 * the chosen type. Implementations may allow other
623 * formats, but should be conservative: implementations
624 * should err on the side of rejecting content if it
625 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200626 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100627 *
Gilles Peskine28538492018-07-11 17:34:00 +0200628 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100629 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100630 * If the key is persistent, the key material and the key's metadata
631 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200632 * \retval #PSA_ERROR_ALREADY_EXISTS
633 * This is an attempt to create a persistent key, and there is
634 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200635 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200636 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200637 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200638 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200639 * The key attributes, as a whole, are invalid.
640 * \retval #PSA_ERROR_INVALID_ARGUMENT
641 * The key data is not correctly formatted.
642 * \retval #PSA_ERROR_INVALID_ARGUMENT
643 * The size in \p attributes is nonzero and does not match the size
644 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200645 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
646 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
647 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100648 * \retval #PSA_ERROR_DATA_CORRUPT
649 * \retval #PSA_ERROR_DATA_INVALID
Darryl Greend49a4992018-06-18 17:27:26 +0100650 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200651 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200652 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300653 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300654 * The library has not been previously initialized by psa_crypto_init().
655 * It is implementation-dependent whether a failure to initialize
656 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100657 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200658psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100659 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200660 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200661 mbedtls_svc_key_id_t *key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100662
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100663
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100664
665/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100666 * \brief Export a key in binary format.
667 *
668 * The output of this function can be passed to psa_import_key() to
669 * create an equivalent object.
670 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100671 * If the implementation of psa_import_key() supports other formats
672 * beyond the format specified here, the output from psa_export_key()
673 * must use the representation specified here, not the original
674 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100676 * For standard key types, the output format is as follows:
677 *
678 * - For symmetric keys (including MAC keys), the format is the
679 * raw bytes of the key.
680 * - For DES, the key data consists of 8 bytes. The parity bits must be
681 * correct.
682 * - For Triple-DES, the format is the concatenation of the
683 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200684 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200685 * is the non-encrypted DER encoding of the representation defined by
686 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
687 * ```
688 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200689 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200690 * modulus INTEGER, -- n
691 * publicExponent INTEGER, -- e
692 * privateExponent INTEGER, -- d
693 * prime1 INTEGER, -- p
694 * prime2 INTEGER, -- q
695 * exponent1 INTEGER, -- d mod (p-1)
696 * exponent2 INTEGER, -- d mod (q-1)
697 * coefficient INTEGER, -- (inverse of q) mod p
698 * }
699 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200700 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200701 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100702 * a representation of the private value as a `ceiling(m/8)`-byte string
703 * where `m` is the bit size associated with the curve, i.e. the bit size
704 * of the order of the curve's coordinate field. This byte string is
705 * in little-endian order for Montgomery curves (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +0100706 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
707 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
708 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
Steven Cooreman6f5cc712020-06-11 16:40:41 +0200709 * For Weierstrass curves, this is the content of the `privateKey` field of
710 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
711 * the format is defined by RFC 7748, and output is masked according to §5.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200712 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200713 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000714 * format is the representation of the private key `x` as a big-endian byte
715 * string. The length of the byte string is the private key size in bytes
716 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200717 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
718 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100719 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200720 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
721 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200722 * \param key Identifier of the key to export. It must allow the
Ronald Cron96783552020-10-19 12:06:30 +0200723 * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
Ronald Croncf56a0a2020-08-04 09:51:30 +0200724 * key.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200725 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200726 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200727 * \param[out] data_length On success, the number of bytes
728 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100729 *
Gilles Peskine28538492018-07-11 17:34:00 +0200730 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100731 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200732 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200733 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100734 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200735 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
736 * The size of the \p data buffer is too small. You can determine a
737 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100738 * #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200739 * where \c type is the key type
740 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200741 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
742 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200743 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100744 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100745 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300746 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300747 * The library has not been previously initialized by psa_crypto_init().
748 * It is implementation-dependent whether a failure to initialize
749 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100750 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200751psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100752 uint8_t *data,
753 size_t data_size,
754 size_t *data_length);
755
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100756/**
757 * \brief Export a public key or the public part of a key pair in binary format.
758 *
759 * The output of this function can be passed to psa_import_key() to
760 * create an object that is equivalent to the public key.
761 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000762 * This specification supports a single format for each key type.
763 * Implementations may support other formats as long as the standard
764 * format is supported. Implementations that support other formats
765 * should ensure that the formats are clearly unambiguous so as to
766 * minimize the risk that an invalid input is accidentally interpreted
767 * according to a different format.
768 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000769 * For standard key types, the output format is as follows:
770 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
771 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
772 * ```
773 * RSAPublicKey ::= SEQUENCE {
774 * modulus INTEGER, -- n
775 * publicExponent INTEGER } -- e
776 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000777 * - For elliptic curve public keys (key types for which
778 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
779 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
780 * Let `m` be the bit size associated with the curve, i.e. the bit size of
781 * `q` for a curve over `F_q`. The representation consists of:
782 * - The byte 0x04;
783 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
784 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200785 * - For Diffie-Hellman key exchange public keys (key types for which
786 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000787 * the format is the representation of the public key `y = g^x mod p` as a
788 * big-endian byte string. The length of the byte string is the length of the
789 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100790 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200791 * Exporting a public key object or the public part of a key pair is
792 * always permitted, regardless of the key's usage flags.
793 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200794 * \param key Identifier of the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200795 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200796 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200797 * \param[out] data_length On success, the number of bytes
798 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100799 *
Gilles Peskine28538492018-07-11 17:34:00 +0200800 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100801 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200802 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200803 * The key is neither a public key nor a key pair.
804 * \retval #PSA_ERROR_NOT_SUPPORTED
805 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
806 * The size of the \p data buffer is too small. You can determine a
807 * sufficient buffer size by calling
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100808 * #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200809 * where \c type is the key type
810 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200811 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
812 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200813 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100814 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100815 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300816 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300817 * The library has not been previously initialized by psa_crypto_init().
818 * It is implementation-dependent whether a failure to initialize
819 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100820 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200821psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100822 uint8_t *data,
823 size_t data_size,
824 size_t *data_length);
825
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100826
Gilles Peskine20035e32018-02-03 22:44:14 +0100827
828/**@}*/
829
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100830/** \defgroup hash Message digests
831 * @{
832 */
833
Gilles Peskine69647a42019-01-14 20:18:12 +0100834/** Calculate the hash (digest) of a message.
835 *
836 * \note To verify the hash of a message against an
837 * expected value, use psa_hash_compare() instead.
838 *
839 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
840 * such that #PSA_ALG_IS_HASH(\p alg) is true).
841 * \param[in] input Buffer containing the message to hash.
842 * \param input_length Size of the \p input buffer in bytes.
843 * \param[out] hash Buffer where the hash is to be written.
844 * \param hash_size Size of the \p hash buffer in bytes.
845 * \param[out] hash_length On success, the number of bytes
846 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100847 * #PSA_HASH_LENGTH(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100848 *
849 * \retval #PSA_SUCCESS
850 * Success.
851 * \retval #PSA_ERROR_NOT_SUPPORTED
852 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100853 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100854 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
855 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100856 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
857 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
858 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200859 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100860 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100861 * \retval #PSA_ERROR_BAD_STATE
862 * The library has not been previously initialized by psa_crypto_init().
863 * It is implementation-dependent whether a failure to initialize
864 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100865 */
866psa_status_t psa_hash_compute(psa_algorithm_t alg,
867 const uint8_t *input,
868 size_t input_length,
869 uint8_t *hash,
870 size_t hash_size,
871 size_t *hash_length);
872
873/** Calculate the hash (digest) of a message and compare it with a
874 * reference value.
875 *
876 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
877 * such that #PSA_ALG_IS_HASH(\p alg) is true).
878 * \param[in] input Buffer containing the message to hash.
879 * \param input_length Size of the \p input buffer in bytes.
880 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100881 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100882 *
883 * \retval #PSA_SUCCESS
884 * The expected hash is identical to the actual hash of the input.
885 * \retval #PSA_ERROR_INVALID_SIGNATURE
886 * The hash of the message was calculated successfully, but it
887 * differs from the expected hash.
888 * \retval #PSA_ERROR_NOT_SUPPORTED
889 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100890 * \retval #PSA_ERROR_INVALID_ARGUMENT
891 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100892 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
893 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
894 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200895 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100896 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100897 * \retval #PSA_ERROR_BAD_STATE
898 * The library has not been previously initialized by psa_crypto_init().
899 * It is implementation-dependent whether a failure to initialize
900 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100901 */
902psa_status_t psa_hash_compare(psa_algorithm_t alg,
903 const uint8_t *input,
904 size_t input_length,
905 const uint8_t *hash,
Gilles Peskinefa710f52019-12-02 14:31:48 +0100906 size_t hash_length);
Gilles Peskine69647a42019-01-14 20:18:12 +0100907
Gilles Peskine308b91d2018-02-08 09:47:44 +0100908/** The type of the state data structure for multipart hash operations.
909 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000910 * Before calling any function on a hash operation object, the application must
911 * initialize it by any of the following means:
912 * - Set the structure to all-bits-zero, for example:
913 * \code
914 * psa_hash_operation_t operation;
915 * memset(&operation, 0, sizeof(operation));
916 * \endcode
917 * - Initialize the structure to logical zero values, for example:
918 * \code
919 * psa_hash_operation_t operation = {0};
920 * \endcode
921 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
922 * for example:
923 * \code
924 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
925 * \endcode
926 * - Assign the result of the function psa_hash_operation_init()
927 * to the structure, for example:
928 * \code
929 * psa_hash_operation_t operation;
930 * operation = psa_hash_operation_init();
931 * \endcode
932 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100933 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100934 * make any assumptions about the content of this structure except
935 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100936typedef struct psa_hash_operation_s psa_hash_operation_t;
937
Jaeden Amero6a25b412019-01-04 11:47:44 +0000938/** \def PSA_HASH_OPERATION_INIT
939 *
940 * This macro returns a suitable initializer for a hash operation object
941 * of type #psa_hash_operation_t.
942 */
943#ifdef __DOXYGEN_ONLY__
944/* This is an example definition for documentation purposes.
945 * Implementations should define a suitable value in `crypto_struct.h`.
946 */
947#define PSA_HASH_OPERATION_INIT {0}
948#endif
949
950/** Return an initial value for a hash operation object.
951 */
952static psa_hash_operation_t psa_hash_operation_init(void);
953
Gilles Peskinef45adda2019-01-14 18:29:18 +0100954/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100955 *
956 * The sequence of operations to calculate a hash (message digest)
957 * is as follows:
958 * -# Allocate an operation object which will be passed to all the functions
959 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000960 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100961 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200962 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100963 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100964 * of the message each time. The hash that is calculated is the hash
965 * of the concatenation of these messages in order.
966 * -# To calculate the hash, call psa_hash_finish().
967 * To compare the hash with an expected value, call psa_hash_verify().
968 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100969 * If an error occurs at any step after a call to psa_hash_setup(), the
970 * operation will need to be reset by a call to psa_hash_abort(). The
971 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000972 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100973 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200974 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100975 * eventually terminate the operation. The following events terminate an
976 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100977 * - A successful call to psa_hash_finish() or psa_hash_verify().
978 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100979 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000980 * \param[in,out] operation The operation object to set up. It must have
981 * been initialized as per the documentation for
982 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200983 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
984 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100985 *
Gilles Peskine28538492018-07-11 17:34:00 +0200986 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100987 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200988 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100989 * \p alg is not a supported hash algorithm.
990 * \retval #PSA_ERROR_INVALID_ARGUMENT
991 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100992 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100993 * The operation state is not valid (it must be inactive).
Gilles Peskine28538492018-07-11 17:34:00 +0200994 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
995 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
996 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200997 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100998 * \retval #PSA_ERROR_BAD_STATE
999 * The library has not been previously initialized by psa_crypto_init().
1000 * It is implementation-dependent whether a failure to initialize
1001 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001002 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001003psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001004 psa_algorithm_t alg);
1005
Gilles Peskine308b91d2018-02-08 09:47:44 +01001006/** Add a message fragment to a multipart hash operation.
1007 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001008 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001009 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001010 * If this function returns an error status, the operation enters an error
1011 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001012 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001013 * \param[in,out] operation Active hash operation.
1014 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001015 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001016 *
Gilles Peskine28538492018-07-11 17:34:00 +02001017 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001018 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001019 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001020 * The operation state is not valid (it muct be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001021 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1022 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1023 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001024 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001025 * \retval #PSA_ERROR_BAD_STATE
1026 * The library has not been previously initialized by psa_crypto_init().
1027 * It is implementation-dependent whether a failure to initialize
1028 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001029 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001030psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1031 const uint8_t *input,
1032 size_t input_length);
1033
Gilles Peskine308b91d2018-02-08 09:47:44 +01001034/** Finish the calculation of the hash of a message.
1035 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001036 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001037 * This function calculates the hash of the message formed by concatenating
1038 * the inputs passed to preceding calls to psa_hash_update().
1039 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001040 * When this function returns successfuly, the operation becomes inactive.
1041 * If this function returns an error status, the operation enters an error
1042 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001043 *
1044 * \warning Applications should not call this function if they expect
1045 * a specific value for the hash. Call psa_hash_verify() instead.
1046 * Beware that comparing integrity or authenticity data such as
1047 * hash values with a function such as \c memcmp is risky
1048 * because the time taken by the comparison may leak information
1049 * about the hashed data which could allow an attacker to guess
1050 * a valid hash and thereby bypass security controls.
1051 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001052 * \param[in,out] operation Active hash operation.
1053 * \param[out] hash Buffer where the hash is to be written.
1054 * \param hash_size Size of the \p hash buffer in bytes.
1055 * \param[out] hash_length On success, the number of bytes
1056 * that make up the hash value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001057 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001058 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001059 *
Gilles Peskine28538492018-07-11 17:34:00 +02001060 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001061 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001062 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001063 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001064 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001065 * The size of the \p hash buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001066 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001067 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001068 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1069 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1070 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001071 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001072 * \retval #PSA_ERROR_BAD_STATE
1073 * The library has not been previously initialized by psa_crypto_init().
1074 * It is implementation-dependent whether a failure to initialize
1075 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001076 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001077psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1078 uint8_t *hash,
1079 size_t hash_size,
1080 size_t *hash_length);
1081
Gilles Peskine308b91d2018-02-08 09:47:44 +01001082/** Finish the calculation of the hash of a message and compare it with
1083 * an expected value.
1084 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001085 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001086 * This function calculates the hash of the message formed by concatenating
1087 * the inputs passed to preceding calls to psa_hash_update(). It then
1088 * compares the calculated hash with the expected hash passed as a
1089 * parameter to this function.
1090 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001091 * When this function returns successfuly, the operation becomes inactive.
1092 * If this function returns an error status, the operation enters an error
1093 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001094 *
Gilles Peskine19067982018-03-20 17:54:53 +01001095 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001096 * comparison between the actual hash and the expected hash is performed
1097 * in constant time.
1098 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001099 * \param[in,out] operation Active hash operation.
1100 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001101 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001102 *
Gilles Peskine28538492018-07-11 17:34:00 +02001103 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001104 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001105 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001106 * The hash of the message was calculated successfully, but it
1107 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001108 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001109 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001110 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1111 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1112 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001113 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001114 * \retval #PSA_ERROR_BAD_STATE
1115 * The library has not been previously initialized by psa_crypto_init().
1116 * It is implementation-dependent whether a failure to initialize
1117 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001118 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001119psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1120 const uint8_t *hash,
1121 size_t hash_length);
1122
Gilles Peskine308b91d2018-02-08 09:47:44 +01001123/** Abort a hash operation.
1124 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001125 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001126 * \p operation structure itself. Once aborted, the operation object
1127 * can be reused for another operation by calling
1128 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001129 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001130 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001131 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001132 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001133 * In particular, calling psa_hash_abort() after the operation has been
1134 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1135 * psa_hash_verify() is safe and has no effect.
1136 *
1137 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001138 *
Gilles Peskine28538492018-07-11 17:34:00 +02001139 * \retval #PSA_SUCCESS
Gilles Peskine28538492018-07-11 17:34:00 +02001140 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1141 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001142 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001143 * \retval #PSA_ERROR_BAD_STATE
1144 * The library has not been previously initialized by psa_crypto_init().
1145 * It is implementation-dependent whether a failure to initialize
1146 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001147 */
1148psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001149
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001150/** Clone a hash operation.
1151 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001152 * This function copies the state of an ongoing hash operation to
1153 * a new operation object. In other words, this function is equivalent
1154 * to calling psa_hash_setup() on \p target_operation with the same
1155 * algorithm that \p source_operation was set up for, then
1156 * psa_hash_update() on \p target_operation with the same input that
1157 * that was passed to \p source_operation. After this function returns, the
1158 * two objects are independent, i.e. subsequent calls involving one of
1159 * the objects do not affect the other object.
1160 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001161 * \param[in] source_operation The active hash operation to clone.
1162 * \param[in,out] target_operation The operation object to set up.
1163 * It must be initialized but not active.
1164 *
1165 * \retval #PSA_SUCCESS
1166 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001167 * The \p source_operation state is not valid (it must be active).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001168 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001169 * The \p target_operation state is not valid (it must be inactive).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001170 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1171 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001172 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001173 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001174 * \retval #PSA_ERROR_BAD_STATE
1175 * The library has not been previously initialized by psa_crypto_init().
1176 * It is implementation-dependent whether a failure to initialize
1177 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001178 */
1179psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1180 psa_hash_operation_t *target_operation);
1181
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001182/**@}*/
1183
Gilles Peskine8c9def32018-02-08 10:02:12 +01001184/** \defgroup MAC Message authentication codes
1185 * @{
1186 */
1187
Gilles Peskine69647a42019-01-14 20:18:12 +01001188/** Calculate the MAC (message authentication code) of a message.
1189 *
1190 * \note To verify the MAC of a message against an
1191 * expected value, use psa_mac_verify() instead.
1192 * Beware that comparing integrity or authenticity data such as
1193 * MAC values with a function such as \c memcmp is risky
1194 * because the time taken by the comparison may leak information
1195 * about the MAC value which could allow an attacker to guess
1196 * a valid MAC and thereby bypass security controls.
1197 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001198 * \param key Identifier of the key to use for the operation. It
1199 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001200 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001201 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001202 * \param[in] input Buffer containing the input message.
1203 * \param input_length Size of the \p input buffer in bytes.
1204 * \param[out] mac Buffer where the MAC value is to be written.
1205 * \param mac_size Size of the \p mac buffer in bytes.
1206 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001207 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001208 *
1209 * \retval #PSA_SUCCESS
1210 * Success.
1211 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001212 * \retval #PSA_ERROR_NOT_PERMITTED
1213 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001214 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001215 * \retval #PSA_ERROR_NOT_SUPPORTED
1216 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001217 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1218 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001219 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1220 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1221 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001222 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001223 * \retval #PSA_ERROR_STORAGE_FAILURE
1224 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001225 * \retval #PSA_ERROR_BAD_STATE
1226 * The library has not been previously initialized by psa_crypto_init().
1227 * It is implementation-dependent whether a failure to initialize
1228 * results in this error code.
1229 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001230psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001231 psa_algorithm_t alg,
1232 const uint8_t *input,
1233 size_t input_length,
1234 uint8_t *mac,
1235 size_t mac_size,
1236 size_t *mac_length);
1237
1238/** Calculate the MAC of a message and compare it with a reference value.
1239 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001240 * \param key Identifier of the key to use for the operation. It
1241 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001242 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001243 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001244 * \param[in] input Buffer containing the input message.
1245 * \param input_length Size of the \p input buffer in bytes.
1246 * \param[out] mac Buffer containing the expected MAC value.
1247 * \param mac_length Size of the \p mac buffer in bytes.
1248 *
1249 * \retval #PSA_SUCCESS
1250 * The expected MAC is identical to the actual MAC of the input.
1251 * \retval #PSA_ERROR_INVALID_SIGNATURE
1252 * The MAC of the message was calculated successfully, but it
1253 * differs from the expected value.
1254 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001255 * \retval #PSA_ERROR_NOT_PERMITTED
1256 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001257 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001258 * \retval #PSA_ERROR_NOT_SUPPORTED
1259 * \p alg is not supported or is not a MAC algorithm.
1260 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1261 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1262 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001263 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001264 * \retval #PSA_ERROR_STORAGE_FAILURE
1265 * The key could not be retrieved from storage.
1266 * \retval #PSA_ERROR_BAD_STATE
1267 * The library has not been previously initialized by psa_crypto_init().
1268 * It is implementation-dependent whether a failure to initialize
1269 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001270 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001271psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
Gilles Peskinea05602d2019-01-17 15:25:52 +01001272 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001273 const uint8_t *input,
1274 size_t input_length,
1275 const uint8_t *mac,
Gilles Peskine13faa2d2020-01-30 16:32:21 +01001276 size_t mac_length);
Gilles Peskine69647a42019-01-14 20:18:12 +01001277
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001278/** The type of the state data structure for multipart MAC operations.
1279 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001280 * Before calling any function on a MAC operation object, the application must
1281 * initialize it by any of the following means:
1282 * - Set the structure to all-bits-zero, for example:
1283 * \code
1284 * psa_mac_operation_t operation;
1285 * memset(&operation, 0, sizeof(operation));
1286 * \endcode
1287 * - Initialize the structure to logical zero values, for example:
1288 * \code
1289 * psa_mac_operation_t operation = {0};
1290 * \endcode
1291 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1292 * for example:
1293 * \code
1294 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1295 * \endcode
1296 * - Assign the result of the function psa_mac_operation_init()
1297 * to the structure, for example:
1298 * \code
1299 * psa_mac_operation_t operation;
1300 * operation = psa_mac_operation_init();
1301 * \endcode
1302 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001303 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001304 * make any assumptions about the content of this structure except
1305 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001306typedef struct psa_mac_operation_s psa_mac_operation_t;
1307
Jaeden Amero769ce272019-01-04 11:48:03 +00001308/** \def PSA_MAC_OPERATION_INIT
1309 *
1310 * This macro returns a suitable initializer for a MAC operation object of type
1311 * #psa_mac_operation_t.
1312 */
1313#ifdef __DOXYGEN_ONLY__
1314/* This is an example definition for documentation purposes.
1315 * Implementations should define a suitable value in `crypto_struct.h`.
1316 */
1317#define PSA_MAC_OPERATION_INIT {0}
1318#endif
1319
1320/** Return an initial value for a MAC operation object.
1321 */
1322static psa_mac_operation_t psa_mac_operation_init(void);
1323
Gilles Peskinef45adda2019-01-14 18:29:18 +01001324/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001325 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001326 * This function sets up the calculation of the MAC
1327 * (message authentication code) of a byte string.
1328 * To verify the MAC of a message against an
1329 * expected value, use psa_mac_verify_setup() instead.
1330 *
1331 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001332 * -# Allocate an operation object which will be passed to all the functions
1333 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001334 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001335 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001336 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001337 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1338 * of the message each time. The MAC that is calculated is the MAC
1339 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001340 * -# At the end of the message, call psa_mac_sign_finish() to finish
1341 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001342 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001343 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1344 * operation will need to be reset by a call to psa_mac_abort(). The
1345 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001346 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001347 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001348 * After a successful call to psa_mac_sign_setup(), the application must
1349 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001350 * - A successful call to psa_mac_sign_finish().
1351 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001352 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001353 * \param[in,out] operation The operation object to set up. It must have
1354 * been initialized as per the documentation for
1355 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001356 * \param key Identifier of the key to use for the operation. It
1357 * must remain valid until the operation terminates.
1358 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001359 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001360 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001361 *
Gilles Peskine28538492018-07-11 17:34:00 +02001362 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001363 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001364 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001365 * \retval #PSA_ERROR_NOT_PERMITTED
1366 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001367 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001368 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001369 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001370 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1371 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1372 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001373 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001374 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001375 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001376 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001377 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001378 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001379 * The library has not been previously initialized by psa_crypto_init().
1380 * It is implementation-dependent whether a failure to initialize
1381 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001382 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001383psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001384 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001385 psa_algorithm_t alg);
1386
Gilles Peskinef45adda2019-01-14 18:29:18 +01001387/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001388 *
1389 * This function sets up the verification of the MAC
1390 * (message authentication code) of a byte string against an expected value.
1391 *
1392 * The sequence of operations to verify a MAC is as follows:
1393 * -# Allocate an operation object which will be passed to all the functions
1394 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001395 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001396 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001397 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001398 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1399 * of the message each time. The MAC that is calculated is the MAC
1400 * of the concatenation of these messages in order.
1401 * -# At the end of the message, call psa_mac_verify_finish() to finish
1402 * calculating the actual MAC of the message and verify it against
1403 * the expected value.
1404 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001405 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1406 * operation will need to be reset by a call to psa_mac_abort(). The
1407 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001408 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001409 *
1410 * After a successful call to psa_mac_verify_setup(), the application must
1411 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001412 * - A successful call to psa_mac_verify_finish().
1413 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001414 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001415 * \param[in,out] operation The operation object to set up. It must have
1416 * been initialized as per the documentation for
1417 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001418 * \param key Identifier of the key to use for the operation. It
1419 * must remain valid until the operation terminates.
1420 * It must allow the usage
1421 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001422 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1423 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001424 *
Gilles Peskine28538492018-07-11 17:34:00 +02001425 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001426 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001427 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001428 * \retval #PSA_ERROR_NOT_PERMITTED
1429 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001430 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001431 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001432 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001433 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1434 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1435 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001436 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001437 * \retval #PSA_ERROR_STORAGE_FAILURE
1438 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001439 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001440 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001441 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001442 * The library has not been previously initialized by psa_crypto_init().
1443 * It is implementation-dependent whether a failure to initialize
1444 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001445 */
1446psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001447 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001448 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001449
Gilles Peskinedcd14942018-07-12 00:30:52 +02001450/** Add a message fragment to a multipart MAC operation.
1451 *
1452 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1453 * before calling this function.
1454 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001455 * If this function returns an error status, the operation enters an error
1456 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001457 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001458 * \param[in,out] operation Active MAC operation.
1459 * \param[in] input Buffer containing the message fragment to add to
1460 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001461 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001462 *
1463 * \retval #PSA_SUCCESS
1464 * Success.
1465 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001466 * The operation state is not valid (it must be active).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001467 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1468 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1469 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001470 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001471 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001472 * \retval #PSA_ERROR_BAD_STATE
1473 * The library has not been previously initialized by psa_crypto_init().
1474 * It is implementation-dependent whether a failure to initialize
1475 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001476 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001477psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1478 const uint8_t *input,
1479 size_t input_length);
1480
Gilles Peskinedcd14942018-07-12 00:30:52 +02001481/** Finish the calculation of the MAC of a message.
1482 *
1483 * The application must call psa_mac_sign_setup() before calling this function.
1484 * This function calculates the MAC of the message formed by concatenating
1485 * the inputs passed to preceding calls to psa_mac_update().
1486 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001487 * When this function returns successfuly, the operation becomes inactive.
1488 * If this function returns an error status, the operation enters an error
1489 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001490 *
1491 * \warning Applications should not call this function if they expect
1492 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1493 * Beware that comparing integrity or authenticity data such as
1494 * MAC values with a function such as \c memcmp is risky
1495 * because the time taken by the comparison may leak information
1496 * about the MAC value which could allow an attacker to guess
1497 * a valid MAC and thereby bypass security controls.
1498 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001499 * \param[in,out] operation Active MAC operation.
1500 * \param[out] mac Buffer where the MAC value is to be written.
1501 * \param mac_size Size of the \p mac buffer in bytes.
1502 * \param[out] mac_length On success, the number of bytes
1503 * that make up the MAC value. This is always
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001504 * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001505 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001506 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001507 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001508 *
1509 * \retval #PSA_SUCCESS
1510 * Success.
1511 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001512 * The operation state is not valid (it must be an active mac sign
1513 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001514 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001515 * The size of the \p mac buffer is too small. You can determine a
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001516 * sufficient buffer size by calling PSA_MAC_LENGTH().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001517 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1518 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1519 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001520 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001521 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001522 * \retval #PSA_ERROR_BAD_STATE
1523 * The library has not been previously initialized by psa_crypto_init().
1524 * It is implementation-dependent whether a failure to initialize
1525 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001526 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001527psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1528 uint8_t *mac,
1529 size_t mac_size,
1530 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001531
Gilles Peskinedcd14942018-07-12 00:30:52 +02001532/** Finish the calculation of the MAC of a message and compare it with
1533 * an expected value.
1534 *
1535 * The application must call psa_mac_verify_setup() before calling this function.
1536 * This function calculates the MAC of the message formed by concatenating
1537 * the inputs passed to preceding calls to psa_mac_update(). It then
1538 * compares the calculated MAC with the expected MAC passed as a
1539 * parameter to this function.
1540 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001541 * When this function returns successfuly, the operation becomes inactive.
1542 * If this function returns an error status, the operation enters an error
1543 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001544 *
1545 * \note Implementations shall make the best effort to ensure that the
1546 * comparison between the actual MAC and the expected MAC is performed
1547 * in constant time.
1548 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001549 * \param[in,out] operation Active MAC operation.
1550 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001551 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001552 *
1553 * \retval #PSA_SUCCESS
1554 * The expected MAC is identical to the actual MAC of the message.
1555 * \retval #PSA_ERROR_INVALID_SIGNATURE
1556 * The MAC of the message was calculated successfully, but it
1557 * differs from the expected MAC.
1558 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001559 * The operation state is not valid (it must be an active mac verify
1560 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001561 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1562 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1563 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001564 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001565 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001566 * \retval #PSA_ERROR_BAD_STATE
1567 * The library has not been previously initialized by psa_crypto_init().
1568 * It is implementation-dependent whether a failure to initialize
1569 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001570 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001571psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1572 const uint8_t *mac,
1573 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001574
Gilles Peskinedcd14942018-07-12 00:30:52 +02001575/** Abort a MAC operation.
1576 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001577 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001578 * \p operation structure itself. Once aborted, the operation object
1579 * can be reused for another operation by calling
1580 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001581 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001582 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001583 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001584 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001585 * In particular, calling psa_mac_abort() after the operation has been
1586 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1587 * psa_mac_verify_finish() is safe and has no effect.
1588 *
1589 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001590 *
1591 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02001592 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1593 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001594 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001595 * \retval #PSA_ERROR_BAD_STATE
1596 * The library has not been previously initialized by psa_crypto_init().
1597 * It is implementation-dependent whether a failure to initialize
1598 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001599 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001600psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1601
1602/**@}*/
1603
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001604/** \defgroup cipher Symmetric ciphers
1605 * @{
1606 */
1607
Gilles Peskine69647a42019-01-14 20:18:12 +01001608/** Encrypt a message using a symmetric cipher.
1609 *
1610 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001611 * vector). Use the multipart operation interface with a
1612 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001613 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001614 * \param key Identifier of the key to use for the operation.
Ronald Cron96783552020-10-19 12:06:30 +02001615 * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001616 * \param alg The cipher algorithm to compute
1617 * (\c PSA_ALG_XXX value such that
1618 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1619 * \param[in] input Buffer containing the message to encrypt.
1620 * \param input_length Size of the \p input buffer in bytes.
1621 * \param[out] output Buffer where the output is to be written.
1622 * The output contains the IV followed by
1623 * the ciphertext proper.
1624 * \param output_size Size of the \p output buffer in bytes.
1625 * \param[out] output_length On success, the number of bytes
1626 * that make up the output.
1627 *
1628 * \retval #PSA_SUCCESS
1629 * Success.
1630 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001631 * \retval #PSA_ERROR_NOT_PERMITTED
1632 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001633 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001634 * \retval #PSA_ERROR_NOT_SUPPORTED
1635 * \p alg is not supported or is not a cipher algorithm.
1636 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1637 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1638 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1639 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001640 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001641 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001642 * \retval #PSA_ERROR_BAD_STATE
1643 * The library has not been previously initialized by psa_crypto_init().
1644 * It is implementation-dependent whether a failure to initialize
1645 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001646 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001647psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001648 psa_algorithm_t alg,
1649 const uint8_t *input,
1650 size_t input_length,
1651 uint8_t *output,
1652 size_t output_size,
1653 size_t *output_length);
1654
1655/** Decrypt a message using a symmetric cipher.
1656 *
1657 * This function decrypts a message encrypted with a symmetric cipher.
1658 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001659 * \param key Identifier of the key to use for the operation.
Gilles Peskine69647a42019-01-14 20:18:12 +01001660 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001661 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001662 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001663 * \param alg The cipher algorithm to compute
1664 * (\c PSA_ALG_XXX value such that
1665 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1666 * \param[in] input Buffer containing the message to decrypt.
1667 * This consists of the IV followed by the
1668 * ciphertext proper.
1669 * \param input_length Size of the \p input buffer in bytes.
1670 * \param[out] output Buffer where the plaintext is to be written.
1671 * \param output_size Size of the \p output buffer in bytes.
1672 * \param[out] output_length On success, the number of bytes
1673 * that make up the output.
1674 *
1675 * \retval #PSA_SUCCESS
1676 * Success.
1677 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001678 * \retval #PSA_ERROR_NOT_PERMITTED
1679 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001680 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001681 * \retval #PSA_ERROR_NOT_SUPPORTED
1682 * \p alg is not supported or is not a cipher algorithm.
1683 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1684 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1685 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1686 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001687 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001688 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001689 * \retval #PSA_ERROR_BAD_STATE
1690 * The library has not been previously initialized by psa_crypto_init().
1691 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001692 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001693 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001694psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001695 psa_algorithm_t alg,
1696 const uint8_t *input,
1697 size_t input_length,
1698 uint8_t *output,
1699 size_t output_size,
1700 size_t *output_length);
1701
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001702/** The type of the state data structure for multipart cipher operations.
1703 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001704 * Before calling any function on a cipher operation object, the application
1705 * must initialize it by any of the following means:
1706 * - Set the structure to all-bits-zero, for example:
1707 * \code
1708 * psa_cipher_operation_t operation;
1709 * memset(&operation, 0, sizeof(operation));
1710 * \endcode
1711 * - Initialize the structure to logical zero values, for example:
1712 * \code
1713 * psa_cipher_operation_t operation = {0};
1714 * \endcode
1715 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1716 * for example:
1717 * \code
1718 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1719 * \endcode
1720 * - Assign the result of the function psa_cipher_operation_init()
1721 * to the structure, for example:
1722 * \code
1723 * psa_cipher_operation_t operation;
1724 * operation = psa_cipher_operation_init();
1725 * \endcode
1726 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001727 * This is an implementation-defined \c struct. Applications should not
1728 * make any assumptions about the content of this structure except
1729 * as directed by the documentation of a specific implementation. */
1730typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1731
Jaeden Amero5bae2272019-01-04 11:48:27 +00001732/** \def PSA_CIPHER_OPERATION_INIT
1733 *
1734 * This macro returns a suitable initializer for a cipher operation object of
1735 * type #psa_cipher_operation_t.
1736 */
1737#ifdef __DOXYGEN_ONLY__
1738/* This is an example definition for documentation purposes.
1739 * Implementations should define a suitable value in `crypto_struct.h`.
1740 */
1741#define PSA_CIPHER_OPERATION_INIT {0}
1742#endif
1743
1744/** Return an initial value for a cipher operation object.
1745 */
1746static psa_cipher_operation_t psa_cipher_operation_init(void);
1747
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001748/** Set the key for a multipart symmetric encryption operation.
1749 *
1750 * The sequence of operations to encrypt a message with a symmetric cipher
1751 * is as follows:
1752 * -# Allocate an operation object which will be passed to all the functions
1753 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001754 * -# Initialize the operation object with one of the methods described in the
1755 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001756 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001757 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001758 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001759 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001760 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001761 * requires a specific IV value.
1762 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1763 * of the message each time.
1764 * -# Call psa_cipher_finish().
1765 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001766 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1767 * the operation will need to be reset by a call to psa_cipher_abort(). The
1768 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001769 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001770 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001771 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001772 * eventually terminate the operation. The following events terminate an
1773 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001774 * - A successful call to psa_cipher_finish().
1775 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001776 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001777 * \param[in,out] operation The operation object to set up. It must have
1778 * been initialized as per the documentation for
1779 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001780 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001781 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001782 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001783 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001784 * \param alg The cipher algorithm to compute
1785 * (\c PSA_ALG_XXX value such that
1786 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001787 *
Gilles Peskine28538492018-07-11 17:34:00 +02001788 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001789 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001790 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001791 * \retval #PSA_ERROR_NOT_PERMITTED
1792 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001793 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001794 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001795 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001796 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1797 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1798 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001799 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001800 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001801 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001802 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001803 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001804 * The library has not been previously initialized by psa_crypto_init().
1805 * It is implementation-dependent whether a failure to initialize
1806 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001807 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001808psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001809 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001810 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001811
1812/** Set the key for a multipart symmetric decryption operation.
1813 *
1814 * The sequence of operations to decrypt a message with a symmetric cipher
1815 * is as follows:
1816 * -# Allocate an operation object which will be passed to all the functions
1817 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001818 * -# Initialize the operation object with one of the methods described in the
1819 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001820 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001821 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001822 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001823 * decryption. If the IV is prepended to the ciphertext, you can call
1824 * psa_cipher_update() on a buffer containing the IV followed by the
1825 * beginning of the message.
1826 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1827 * of the message each time.
1828 * -# Call psa_cipher_finish().
1829 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001830 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1831 * the operation will need to be reset by a call to psa_cipher_abort(). The
1832 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001833 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001834 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001835 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001836 * eventually terminate the operation. The following events terminate an
1837 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001838 * - A successful call to psa_cipher_finish().
1839 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001840 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001841 * \param[in,out] operation The operation object to set up. It must have
1842 * been initialized as per the documentation for
1843 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001844 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001845 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001846 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001847 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001848 * \param alg The cipher algorithm to compute
1849 * (\c PSA_ALG_XXX value such that
1850 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001851 *
Gilles Peskine28538492018-07-11 17:34:00 +02001852 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001853 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001854 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001855 * \retval #PSA_ERROR_NOT_PERMITTED
1856 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001857 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001858 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001859 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001860 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1861 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1862 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001863 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001864 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001865 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001866 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001867 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001868 * The library has not been previously initialized by psa_crypto_init().
1869 * It is implementation-dependent whether a failure to initialize
1870 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001871 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001872psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001873 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001874 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001875
Gilles Peskinedcd14942018-07-12 00:30:52 +02001876/** Generate an IV for a symmetric encryption operation.
1877 *
1878 * This function generates a random IV (initialization vector), nonce
1879 * or initial counter value for the encryption operation as appropriate
1880 * for the chosen algorithm, key type and key size.
1881 *
1882 * The application must call psa_cipher_encrypt_setup() before
1883 * calling this function.
1884 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001885 * If this function returns an error status, the operation enters an error
1886 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001887 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001888 * \param[in,out] operation Active cipher operation.
1889 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001890 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001891 * \param[out] iv_length On success, the number of bytes of the
1892 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001893 *
1894 * \retval #PSA_SUCCESS
1895 * Success.
1896 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001897 * The operation state is not valid (it must be active, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001898 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001899 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001900 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1901 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1902 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001903 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001904 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001905 * \retval #PSA_ERROR_BAD_STATE
1906 * The library has not been previously initialized by psa_crypto_init().
1907 * It is implementation-dependent whether a failure to initialize
1908 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001909 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001910psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001911 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001912 size_t iv_size,
1913 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001914
Gilles Peskinedcd14942018-07-12 00:30:52 +02001915/** Set the IV for a symmetric encryption or decryption operation.
1916 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001917 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001918 * or initial counter value for the encryption or decryption operation.
1919 *
1920 * The application must call psa_cipher_encrypt_setup() before
1921 * calling this function.
1922 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001923 * If this function returns an error status, the operation enters an error
1924 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001925 *
1926 * \note When encrypting, applications should use psa_cipher_generate_iv()
1927 * instead of this function, unless implementing a protocol that requires
1928 * a non-random IV.
1929 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001930 * \param[in,out] operation Active cipher operation.
1931 * \param[in] iv Buffer containing the IV to use.
1932 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001933 *
1934 * \retval #PSA_SUCCESS
1935 * Success.
1936 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001937 * The operation state is not valid (it must be an active cipher
1938 * encrypt operation, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001939 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001940 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001941 * or the chosen algorithm does not use an IV.
1942 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1943 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1944 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001945 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001946 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001947 * \retval #PSA_ERROR_BAD_STATE
1948 * The library has not been previously initialized by psa_crypto_init().
1949 * It is implementation-dependent whether a failure to initialize
1950 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001951 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001952psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001953 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001954 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001955
Gilles Peskinedcd14942018-07-12 00:30:52 +02001956/** Encrypt or decrypt a message fragment in an active cipher operation.
1957 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001958 * Before calling this function, you must:
1959 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1960 * The choice of setup function determines whether this function
1961 * encrypts or decrypts its input.
1962 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1963 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001964 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001965 * If this function returns an error status, the operation enters an error
1966 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001967 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001968 * \param[in,out] operation Active cipher operation.
1969 * \param[in] input Buffer containing the message fragment to
1970 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001971 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001972 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001973 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001974 * \param[out] output_length On success, the number of bytes
1975 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001976 *
1977 * \retval #PSA_SUCCESS
1978 * Success.
1979 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001980 * The operation state is not valid (it must be active, with an IV set
1981 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001982 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1983 * The size of the \p output buffer is too small.
1984 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1985 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1986 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001987 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001988 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001989 * \retval #PSA_ERROR_BAD_STATE
1990 * The library has not been previously initialized by psa_crypto_init().
1991 * It is implementation-dependent whether a failure to initialize
1992 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001993 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001994psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1995 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001996 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001997 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001998 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001999 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002000
Gilles Peskinedcd14942018-07-12 00:30:52 +02002001/** Finish encrypting or decrypting a message in a cipher operation.
2002 *
2003 * The application must call psa_cipher_encrypt_setup() or
2004 * psa_cipher_decrypt_setup() before calling this function. The choice
2005 * of setup function determines whether this function encrypts or
2006 * decrypts its input.
2007 *
2008 * This function finishes the encryption or decryption of the message
2009 * formed by concatenating the inputs passed to preceding calls to
2010 * psa_cipher_update().
2011 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002012 * When this function returns successfuly, the operation becomes inactive.
2013 * If this function returns an error status, the operation enters an error
2014 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002015 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002016 * \param[in,out] operation Active cipher operation.
2017 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002018 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002019 * \param[out] output_length On success, the number of bytes
2020 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002021 *
2022 * \retval #PSA_SUCCESS
2023 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02002024 * \retval #PSA_ERROR_INVALID_ARGUMENT
2025 * The total input size passed to this operation is not valid for
2026 * this particular algorithm. For example, the algorithm is a based
2027 * on block cipher and requires a whole number of blocks, but the
2028 * total input size is not a multiple of the block size.
2029 * \retval #PSA_ERROR_INVALID_PADDING
2030 * This is a decryption operation for an algorithm that includes
2031 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002032 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002033 * The operation state is not valid (it must be active, with an IV set
2034 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02002035 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2036 * The size of the \p output buffer is too small.
2037 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2038 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2039 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002040 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002041 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002042 * \retval #PSA_ERROR_BAD_STATE
2043 * The library has not been previously initialized by psa_crypto_init().
2044 * It is implementation-dependent whether a failure to initialize
2045 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002046 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002047psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002048 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002049 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002050 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002051
Gilles Peskinedcd14942018-07-12 00:30:52 +02002052/** Abort a cipher operation.
2053 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002054 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002055 * \p operation structure itself. Once aborted, the operation object
2056 * can be reused for another operation by calling
2057 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002058 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002059 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002060 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002061 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002062 * In particular, calling psa_cipher_abort() after the operation has been
2063 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2064 * is safe and has no effect.
2065 *
2066 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002067 *
2068 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02002069 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2070 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002071 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002072 * \retval #PSA_ERROR_BAD_STATE
2073 * The library has not been previously initialized by psa_crypto_init().
2074 * It is implementation-dependent whether a failure to initialize
2075 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002076 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002077psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2078
2079/**@}*/
2080
Gilles Peskine3b555712018-03-03 21:27:57 +01002081/** \defgroup aead Authenticated encryption with associated data (AEAD)
2082 * @{
2083 */
2084
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002085/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002086 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002087 * \param key Identifier of the key to use for the
2088 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002089 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002090 * \param alg The AEAD algorithm to compute
2091 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002092 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002093 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002094 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002095 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002096 * but not encrypted.
2097 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002098 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002099 * encrypted.
2100 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002101 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002102 * encrypted data. The additional data is not
2103 * part of this output. For algorithms where the
2104 * encrypted data and the authentication tag
2105 * are defined as separate outputs, the
2106 * authentication tag is appended to the
2107 * encrypted data.
2108 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2109 * This must be at least
2110 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2111 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002112 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002113 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002114 *
Gilles Peskine28538492018-07-11 17:34:00 +02002115 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002116 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002117 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002118 * \retval #PSA_ERROR_NOT_PERMITTED
2119 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002120 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002121 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002122 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002123 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002124 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2125 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002126 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2127 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002128 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002129 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002130 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002131 * The library has not been previously initialized by psa_crypto_init().
2132 * It is implementation-dependent whether a failure to initialize
2133 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002134 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002135psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002136 psa_algorithm_t alg,
2137 const uint8_t *nonce,
2138 size_t nonce_length,
2139 const uint8_t *additional_data,
2140 size_t additional_data_length,
2141 const uint8_t *plaintext,
2142 size_t plaintext_length,
2143 uint8_t *ciphertext,
2144 size_t ciphertext_size,
2145 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002146
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002147/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002148 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002149 * \param key Identifier of the key to use for the
2150 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002151 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002152 * \param alg The AEAD algorithm to compute
2153 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002154 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002155 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002156 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002157 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002158 * but not encrypted.
2159 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002160 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002161 * encrypted. For algorithms where the
2162 * encrypted data and the authentication tag
2163 * are defined as separate inputs, the buffer
2164 * must contain the encrypted data followed
2165 * by the authentication tag.
2166 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002167 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002168 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2169 * This must be at least
2170 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2171 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002172 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002173 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002174 *
Gilles Peskine28538492018-07-11 17:34:00 +02002175 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002176 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002177 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002178 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002179 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002180 * \retval #PSA_ERROR_NOT_PERMITTED
2181 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002182 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002183 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002184 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002185 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002186 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2187 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002188 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2189 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002190 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002191 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002192 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002193 * The library has not been previously initialized by psa_crypto_init().
2194 * It is implementation-dependent whether a failure to initialize
2195 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002196 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002197psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002198 psa_algorithm_t alg,
2199 const uint8_t *nonce,
2200 size_t nonce_length,
2201 const uint8_t *additional_data,
2202 size_t additional_data_length,
2203 const uint8_t *ciphertext,
2204 size_t ciphertext_length,
2205 uint8_t *plaintext,
2206 size_t plaintext_size,
2207 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002208
Gilles Peskine30a9e412019-01-14 18:36:12 +01002209/** The type of the state data structure for multipart AEAD operations.
2210 *
2211 * Before calling any function on an AEAD operation object, the application
2212 * must initialize it by any of the following means:
2213 * - Set the structure to all-bits-zero, for example:
2214 * \code
2215 * psa_aead_operation_t operation;
2216 * memset(&operation, 0, sizeof(operation));
2217 * \endcode
2218 * - Initialize the structure to logical zero values, for example:
2219 * \code
2220 * psa_aead_operation_t operation = {0};
2221 * \endcode
2222 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2223 * for example:
2224 * \code
2225 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2226 * \endcode
2227 * - Assign the result of the function psa_aead_operation_init()
2228 * to the structure, for example:
2229 * \code
2230 * psa_aead_operation_t operation;
2231 * operation = psa_aead_operation_init();
2232 * \endcode
2233 *
2234 * This is an implementation-defined \c struct. Applications should not
2235 * make any assumptions about the content of this structure except
2236 * as directed by the documentation of a specific implementation. */
2237typedef struct psa_aead_operation_s psa_aead_operation_t;
2238
2239/** \def PSA_AEAD_OPERATION_INIT
2240 *
2241 * This macro returns a suitable initializer for an AEAD operation object of
2242 * type #psa_aead_operation_t.
2243 */
2244#ifdef __DOXYGEN_ONLY__
2245/* This is an example definition for documentation purposes.
2246 * Implementations should define a suitable value in `crypto_struct.h`.
2247 */
2248#define PSA_AEAD_OPERATION_INIT {0}
2249#endif
2250
2251/** Return an initial value for an AEAD operation object.
2252 */
2253static psa_aead_operation_t psa_aead_operation_init(void);
2254
2255/** Set the key for a multipart authenticated encryption operation.
2256 *
2257 * The sequence of operations to encrypt a message with authentication
2258 * is as follows:
2259 * -# Allocate an operation object which will be passed to all the functions
2260 * listed here.
2261 * -# Initialize the operation object with one of the methods described in the
2262 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002263 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002264 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002265 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2266 * inputs to the subsequent calls to psa_aead_update_ad() and
2267 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2268 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002269 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2270 * generate or set the nonce. You should use
2271 * psa_aead_generate_nonce() unless the protocol you are implementing
2272 * requires a specific nonce value.
2273 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2274 * of the non-encrypted additional authenticated data each time.
2275 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002276 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002277 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002278 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002279 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2280 * the operation will need to be reset by a call to psa_aead_abort(). The
2281 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002282 * has been initialized.
2283 *
2284 * After a successful call to psa_aead_encrypt_setup(), the application must
2285 * eventually terminate the operation. The following events terminate an
2286 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002287 * - A successful call to psa_aead_finish().
2288 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002289 *
2290 * \param[in,out] operation The operation object to set up. It must have
2291 * been initialized as per the documentation for
2292 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002293 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002294 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002295 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002296 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002297 * \param alg The AEAD algorithm to compute
2298 * (\c PSA_ALG_XXX value such that
2299 * #PSA_ALG_IS_AEAD(\p alg) is true).
2300 *
2301 * \retval #PSA_SUCCESS
2302 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002303 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002304 * The operation state is not valid (it must be inactive).
Ronald Cron96783552020-10-19 12:06:30 +02002305 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002306 * \retval #PSA_ERROR_NOT_PERMITTED
2307 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002308 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002309 * \retval #PSA_ERROR_NOT_SUPPORTED
2310 * \p alg is not supported or is not an AEAD algorithm.
2311 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2312 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2313 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002314 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002315 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002316 * \retval #PSA_ERROR_BAD_STATE
2317 * The library has not been previously initialized by psa_crypto_init().
2318 * It is implementation-dependent whether a failure to initialize
2319 * results in this error code.
2320 */
2321psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002322 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002323 psa_algorithm_t alg);
2324
2325/** Set the key for a multipart authenticated decryption operation.
2326 *
2327 * The sequence of operations to decrypt a message with authentication
2328 * is as follows:
2329 * -# Allocate an operation object which will be passed to all the functions
2330 * listed here.
2331 * -# Initialize the operation object with one of the methods described in the
2332 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002333 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002334 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002335 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2336 * inputs to the subsequent calls to psa_aead_update_ad() and
2337 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2338 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002339 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2340 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2341 * of the non-encrypted additional authenticated data each time.
2342 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002343 * of the ciphertext to decrypt each time.
2344 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002345 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002346 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2347 * the operation will need to be reset by a call to psa_aead_abort(). The
2348 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002349 * has been initialized.
2350 *
2351 * After a successful call to psa_aead_decrypt_setup(), the application must
2352 * eventually terminate the operation. The following events terminate an
2353 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002354 * - A successful call to psa_aead_verify().
2355 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002356 *
2357 * \param[in,out] operation The operation object to set up. It must have
2358 * been initialized as per the documentation for
2359 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002360 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002361 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002362 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002363 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002364 * \param alg The AEAD algorithm to compute
2365 * (\c PSA_ALG_XXX value such that
2366 * #PSA_ALG_IS_AEAD(\p alg) is true).
2367 *
2368 * \retval #PSA_SUCCESS
2369 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002370 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002371 * The operation state is not valid (it must be inactive).
Ronald Cron96783552020-10-19 12:06:30 +02002372 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002373 * \retval #PSA_ERROR_NOT_PERMITTED
2374 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002375 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002376 * \retval #PSA_ERROR_NOT_SUPPORTED
2377 * \p alg is not supported or is not an AEAD algorithm.
2378 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2379 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2380 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002381 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002382 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002383 * \retval #PSA_ERROR_BAD_STATE
2384 * The library has not been previously initialized by psa_crypto_init().
2385 * It is implementation-dependent whether a failure to initialize
2386 * results in this error code.
2387 */
2388psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002389 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002390 psa_algorithm_t alg);
2391
2392/** Generate a random nonce for an authenticated encryption operation.
2393 *
2394 * This function generates a random nonce for the authenticated encryption
2395 * operation with an appropriate size for the chosen algorithm, key type
2396 * and key size.
2397 *
2398 * The application must call psa_aead_encrypt_setup() before
2399 * calling this function.
2400 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002401 * If this function returns an error status, the operation enters an error
2402 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002403 *
2404 * \param[in,out] operation Active AEAD operation.
2405 * \param[out] nonce Buffer where the generated nonce is to be
2406 * written.
2407 * \param nonce_size Size of the \p nonce buffer in bytes.
2408 * \param[out] nonce_length On success, the number of bytes of the
2409 * generated nonce.
2410 *
2411 * \retval #PSA_SUCCESS
2412 * Success.
2413 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002414 * The operation state is not valid (it must be an active aead encrypt
Ronald Cron96783552020-10-19 12:06:30 +02002415 * operation, with no nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002416 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2417 * The size of the \p nonce buffer is too small.
2418 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2419 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2420 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002421 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002422 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002423 * \retval #PSA_ERROR_BAD_STATE
2424 * The library has not been previously initialized by psa_crypto_init().
2425 * It is implementation-dependent whether a failure to initialize
2426 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002427 */
2428psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002429 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002430 size_t nonce_size,
2431 size_t *nonce_length);
2432
2433/** Set the nonce for an authenticated encryption or decryption operation.
2434 *
2435 * This function sets the nonce for the authenticated
2436 * encryption or decryption operation.
2437 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002438 * The application must call psa_aead_encrypt_setup() or
2439 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002440 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002441 * If this function returns an error status, the operation enters an error
2442 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002443 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002444 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002445 * instead of this function, unless implementing a protocol that requires
2446 * a non-random IV.
2447 *
2448 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002449 * \param[in] nonce Buffer containing the nonce to use.
2450 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002451 *
2452 * \retval #PSA_SUCCESS
2453 * Success.
2454 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002455 * The operation state is not valid (it must be active, with no nonce
2456 * set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002457 * \retval #PSA_ERROR_INVALID_ARGUMENT
2458 * The size of \p nonce is not acceptable for the chosen algorithm.
2459 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2460 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2461 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002462 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002463 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002464 * \retval #PSA_ERROR_BAD_STATE
2465 * The library has not been previously initialized by psa_crypto_init().
2466 * It is implementation-dependent whether a failure to initialize
2467 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002468 */
2469psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002470 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002471 size_t nonce_length);
2472
Gilles Peskinebc59c852019-01-17 15:26:08 +01002473/** Declare the lengths of the message and additional data for AEAD.
2474 *
2475 * The application must call this function before calling
2476 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2477 * the operation requires it. If the algorithm does not require it,
2478 * calling this function is optional, but if this function is called
2479 * then the implementation must enforce the lengths.
2480 *
2481 * You may call this function before or after setting the nonce with
2482 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2483 *
2484 * - For #PSA_ALG_CCM, calling this function is required.
2485 * - For the other AEAD algorithms defined in this specification, calling
2486 * this function is not required.
2487 * - For vendor-defined algorithm, refer to the vendor documentation.
2488 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002489 * If this function returns an error status, the operation enters an error
2490 * state and must be aborted by calling psa_aead_abort().
2491 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002492 * \param[in,out] operation Active AEAD operation.
2493 * \param ad_length Size of the non-encrypted additional
2494 * authenticated data in bytes.
2495 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2496 *
2497 * \retval #PSA_SUCCESS
2498 * Success.
2499 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002500 * The operation state is not valid (it must be active, and
2501 * psa_aead_update_ad() and psa_aead_update() must not have been
2502 * called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002503 * \retval #PSA_ERROR_INVALID_ARGUMENT
2504 * At least one of the lengths is not acceptable for the chosen
2505 * algorithm.
2506 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2507 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2508 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002509 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002510 * \retval #PSA_ERROR_BAD_STATE
2511 * The library has not been previously initialized by psa_crypto_init().
2512 * It is implementation-dependent whether a failure to initialize
2513 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002514 */
2515psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2516 size_t ad_length,
2517 size_t plaintext_length);
2518
Gilles Peskine30a9e412019-01-14 18:36:12 +01002519/** Pass additional data to an active AEAD operation.
2520 *
2521 * Additional data is authenticated, but not encrypted.
2522 *
2523 * You may call this function multiple times to pass successive fragments
2524 * of the additional data. You may not call this function after passing
2525 * data to encrypt or decrypt with psa_aead_update().
2526 *
2527 * Before calling this function, you must:
2528 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2529 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2530 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002531 * If this function returns an error status, the operation enters an error
2532 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002533 *
2534 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2535 * there is no guarantee that the input is valid. Therefore, until
2536 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2537 * treat the input as untrusted and prepare to undo any action that
2538 * depends on the input if psa_aead_verify() returns an error status.
2539 *
2540 * \param[in,out] operation Active AEAD operation.
2541 * \param[in] input Buffer containing the fragment of
2542 * additional data.
2543 * \param input_length Size of the \p input buffer in bytes.
2544 *
2545 * \retval #PSA_SUCCESS
2546 * Success.
2547 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002548 * The operation state is not valid (it must be active, have a nonce
2549 * set, have lengths set if required by the algorithm, and
2550 * psa_aead_update() must not have been called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002551 * \retval #PSA_ERROR_INVALID_ARGUMENT
2552 * The total input length overflows the additional data length that
2553 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002554 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2555 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2556 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002557 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002558 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002559 * \retval #PSA_ERROR_BAD_STATE
2560 * The library has not been previously initialized by psa_crypto_init().
2561 * It is implementation-dependent whether a failure to initialize
2562 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002563 */
2564psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2565 const uint8_t *input,
2566 size_t input_length);
2567
2568/** Encrypt or decrypt a message fragment in an active AEAD operation.
2569 *
2570 * Before calling this function, you must:
2571 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2572 * The choice of setup function determines whether this function
2573 * encrypts or decrypts its input.
2574 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2575 * 3. Call psa_aead_update_ad() to pass all the additional data.
2576 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002577 * If this function returns an error status, the operation enters an error
2578 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002579 *
2580 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2581 * there is no guarantee that the input is valid. Therefore, until
2582 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2583 * - Do not use the output in any way other than storing it in a
2584 * confidential location. If you take any action that depends
2585 * on the tentative decrypted data, this action will need to be
2586 * undone if the input turns out not to be valid. Furthermore,
2587 * if an adversary can observe that this action took place
2588 * (for example through timing), they may be able to use this
2589 * fact as an oracle to decrypt any message encrypted with the
2590 * same key.
2591 * - In particular, do not copy the output anywhere but to a
2592 * memory or storage space that you have exclusive access to.
2593 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002594 * This function does not require the input to be aligned to any
2595 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002596 * a whole block at a time, it must consume all the input provided, but
2597 * it may delay the end of the corresponding output until a subsequent
2598 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2599 * provides sufficient input. The amount of data that can be delayed
2600 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002601 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002602 * \param[in,out] operation Active AEAD operation.
2603 * \param[in] input Buffer containing the message fragment to
2604 * encrypt or decrypt.
2605 * \param input_length Size of the \p input buffer in bytes.
2606 * \param[out] output Buffer where the output is to be written.
2607 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002608 * This must be at least
2609 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2610 * \p input_length) where \c alg is the
2611 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002612 * \param[out] output_length On success, the number of bytes
2613 * that make up the returned output.
2614 *
2615 * \retval #PSA_SUCCESS
2616 * Success.
2617 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002618 * The operation state is not valid (it must be active, have a nonce
2619 * set, and have lengths set if required by the algorithm).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002620 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2621 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002622 * You can determine a sufficient buffer size by calling
2623 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2624 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002625 * \retval #PSA_ERROR_INVALID_ARGUMENT
2626 * The total length of input to psa_aead_update_ad() so far is
2627 * less than the additional data length that was previously
2628 * specified with psa_aead_set_lengths().
2629 * \retval #PSA_ERROR_INVALID_ARGUMENT
2630 * The total input length overflows the plaintext length that
2631 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002632 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2633 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2634 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002635 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002636 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002637 * \retval #PSA_ERROR_BAD_STATE
2638 * The library has not been previously initialized by psa_crypto_init().
2639 * It is implementation-dependent whether a failure to initialize
2640 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002641 */
2642psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2643 const uint8_t *input,
2644 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002645 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002646 size_t output_size,
2647 size_t *output_length);
2648
2649/** Finish encrypting a message in an AEAD operation.
2650 *
2651 * The operation must have been set up with psa_aead_encrypt_setup().
2652 *
2653 * This function finishes the authentication of the additional data
2654 * formed by concatenating the inputs passed to preceding calls to
2655 * psa_aead_update_ad() with the plaintext formed by concatenating the
2656 * inputs passed to preceding calls to psa_aead_update().
2657 *
2658 * This function has two output buffers:
2659 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002660 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002661 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002662 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002663 * that the operation performs.
2664 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002665 * When this function returns successfuly, the operation becomes inactive.
2666 * If this function returns an error status, the operation enters an error
2667 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002668 *
2669 * \param[in,out] operation Active AEAD operation.
2670 * \param[out] ciphertext Buffer where the last part of the ciphertext
2671 * is to be written.
2672 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002673 * This must be at least
2674 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2675 * \c alg is the algorithm that is being
2676 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002677 * \param[out] ciphertext_length On success, the number of bytes of
2678 * returned ciphertext.
2679 * \param[out] tag Buffer where the authentication tag is
2680 * to be written.
2681 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002682 * This must be at least
2683 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2684 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002685 * \param[out] tag_length On success, the number of bytes
2686 * that make up the returned tag.
2687 *
2688 * \retval #PSA_SUCCESS
2689 * Success.
2690 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002691 * The operation state is not valid (it must be an active encryption
2692 * operation with a nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002693 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002694 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002695 * You can determine a sufficient buffer size for \p ciphertext by
2696 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2697 * where \c alg is the algorithm that is being calculated.
2698 * You can determine a sufficient buffer size for \p tag by
2699 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002700 * \retval #PSA_ERROR_INVALID_ARGUMENT
2701 * The total length of input to psa_aead_update_ad() so far is
2702 * less than the additional data length that was previously
2703 * specified with psa_aead_set_lengths().
2704 * \retval #PSA_ERROR_INVALID_ARGUMENT
2705 * The total length of input to psa_aead_update() so far is
2706 * less than the plaintext length that was previously
2707 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002708 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2709 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2710 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002711 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002712 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002713 * \retval #PSA_ERROR_BAD_STATE
2714 * The library has not been previously initialized by psa_crypto_init().
2715 * It is implementation-dependent whether a failure to initialize
2716 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002717 */
2718psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002719 uint8_t *ciphertext,
2720 size_t ciphertext_size,
2721 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002722 uint8_t *tag,
2723 size_t tag_size,
2724 size_t *tag_length);
2725
2726/** Finish authenticating and decrypting a message in an AEAD operation.
2727 *
2728 * The operation must have been set up with psa_aead_decrypt_setup().
2729 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002730 * This function finishes the authenticated decryption of the message
2731 * components:
2732 *
2733 * - The additional data consisting of the concatenation of the inputs
2734 * passed to preceding calls to psa_aead_update_ad().
2735 * - The ciphertext consisting of the concatenation of the inputs passed to
2736 * preceding calls to psa_aead_update().
2737 * - The tag passed to this function call.
2738 *
2739 * If the authentication tag is correct, this function outputs any remaining
2740 * plaintext and reports success. If the authentication tag is not correct,
2741 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002742 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002743 * When this function returns successfuly, the operation becomes inactive.
2744 * If this function returns an error status, the operation enters an error
2745 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002746 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002747 * \note Implementations shall make the best effort to ensure that the
2748 * comparison between the actual tag and the expected tag is performed
2749 * in constant time.
2750 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002751 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002752 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002753 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002754 * from previous calls to psa_aead_update()
2755 * that could not be processed until the end
2756 * of the input.
2757 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002758 * This must be at least
2759 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2760 * \c alg is the algorithm that is being
2761 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002762 * \param[out] plaintext_length On success, the number of bytes of
2763 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002764 * \param[in] tag Buffer containing the authentication tag.
2765 * \param tag_length Size of the \p tag buffer in bytes.
2766 *
2767 * \retval #PSA_SUCCESS
2768 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002769 * \retval #PSA_ERROR_INVALID_SIGNATURE
2770 * The calculations were successful, but the authentication tag is
2771 * not correct.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002772 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002773 * The operation state is not valid (it must be an active decryption
2774 * operation with a nonce set).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002775 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2776 * The size of the \p plaintext buffer is too small.
2777 * You can determine a sufficient buffer size for \p plaintext by
2778 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2779 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002780 * \retval #PSA_ERROR_INVALID_ARGUMENT
2781 * The total length of input to psa_aead_update_ad() so far is
2782 * less than the additional data length that was previously
2783 * specified with psa_aead_set_lengths().
2784 * \retval #PSA_ERROR_INVALID_ARGUMENT
2785 * The total length of input to psa_aead_update() so far is
2786 * less than the plaintext length that was previously
2787 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002788 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2789 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2790 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002791 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002792 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002793 * \retval #PSA_ERROR_BAD_STATE
2794 * The library has not been previously initialized by psa_crypto_init().
2795 * It is implementation-dependent whether a failure to initialize
2796 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002797 */
2798psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002799 uint8_t *plaintext,
2800 size_t plaintext_size,
2801 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002802 const uint8_t *tag,
2803 size_t tag_length);
2804
2805/** Abort an AEAD operation.
2806 *
2807 * Aborting an operation frees all associated resources except for the
2808 * \p operation structure itself. Once aborted, the operation object
2809 * can be reused for another operation by calling
2810 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2811 *
2812 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002813 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002814 *
2815 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002816 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2817 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002818 *
2819 * \param[in,out] operation Initialized AEAD operation.
2820 *
2821 * \retval #PSA_SUCCESS
Gilles Peskine30a9e412019-01-14 18:36:12 +01002822 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2823 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002824 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002825 * \retval #PSA_ERROR_BAD_STATE
2826 * The library has not been previously initialized by psa_crypto_init().
2827 * It is implementation-dependent whether a failure to initialize
2828 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002829 */
2830psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2831
Gilles Peskine3b555712018-03-03 21:27:57 +01002832/**@}*/
2833
Gilles Peskine20035e32018-02-03 22:44:14 +01002834/** \defgroup asymmetric Asymmetric cryptography
2835 * @{
2836 */
2837
2838/**
2839 * \brief Sign a hash or short message with a private key.
2840 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002841 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002842 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002843 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2844 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2845 * to determine the hash algorithm to use.
2846 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002847 * \param key Identifier of the key to use for the operation.
2848 * It must be an asymmetric key pair. The key must
Ronald Cron96783552020-10-19 12:06:30 +02002849 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002850 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002851 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002852 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002853 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002854 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002855 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002856 * \param[out] signature_length On success, the number of bytes
2857 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002858 *
Gilles Peskine28538492018-07-11 17:34:00 +02002859 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002860 * \retval #PSA_ERROR_INVALID_HANDLE
2861 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002862 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002863 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002864 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002865 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002866 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002867 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002868 * \retval #PSA_ERROR_NOT_SUPPORTED
2869 * \retval #PSA_ERROR_INVALID_ARGUMENT
2870 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2871 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2872 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002873 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002874 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002875 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002876 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002877 * The library has not been previously initialized by psa_crypto_init().
2878 * It is implementation-dependent whether a failure to initialize
2879 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002880 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002881psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002882 psa_algorithm_t alg,
2883 const uint8_t *hash,
2884 size_t hash_length,
2885 uint8_t *signature,
2886 size_t signature_size,
2887 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002888
2889/**
2890 * \brief Verify the signature a hash or short message using a public key.
2891 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002892 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002893 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002894 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2895 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2896 * to determine the hash algorithm to use.
2897 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002898 * \param key Identifier of the key to use for the operation. It
2899 * must be a public key or an asymmetric key pair. The
Ronald Cron96783552020-10-19 12:06:30 +02002900 * key must allow the usage
2901 * #PSA_KEY_USAGE_VERIFY_HASH.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002902 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002903 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002904 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002905 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002906 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002907 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002908 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002909 *
Gilles Peskine28538492018-07-11 17:34:00 +02002910 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002911 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002912 * \retval #PSA_ERROR_INVALID_HANDLE
2913 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002914 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002915 * The calculation was perfomed successfully, but the passed
2916 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002917 * \retval #PSA_ERROR_NOT_SUPPORTED
2918 * \retval #PSA_ERROR_INVALID_ARGUMENT
2919 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2920 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2921 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002922 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002923 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002924 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002925 * The library has not been previously initialized by psa_crypto_init().
2926 * It is implementation-dependent whether a failure to initialize
2927 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002928 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002929psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002930 psa_algorithm_t alg,
2931 const uint8_t *hash,
2932 size_t hash_length,
2933 const uint8_t *signature,
2934 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002935
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002936/**
2937 * \brief Encrypt a short message with a public key.
2938 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002939 * \param key Identifer of the key to use for the operation.
2940 * It must be a public key or an asymmetric key
2941 * pair. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002942 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002943 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02002944 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002945 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002946 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002947 * \param[in] salt A salt or label, if supported by the
2948 * encryption algorithm.
2949 * If the algorithm does not support a
2950 * salt, pass \c NULL.
2951 * If the algorithm supports an optional
2952 * salt and you do not want to pass a salt,
2953 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002954 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002955 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2956 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002957 * \param salt_length Size of the \p salt buffer in bytes.
2958 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002959 * \param[out] output Buffer where the encrypted message is to
2960 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002961 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002962 * \param[out] output_length On success, the number of bytes
2963 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002964 *
Gilles Peskine28538492018-07-11 17:34:00 +02002965 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002966 * \retval #PSA_ERROR_INVALID_HANDLE
2967 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002968 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002969 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002970 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002971 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002972 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002973 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002974 * \retval #PSA_ERROR_NOT_SUPPORTED
2975 * \retval #PSA_ERROR_INVALID_ARGUMENT
2976 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2977 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2978 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002979 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002980 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002981 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002982 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002983 * The library has not been previously initialized by psa_crypto_init().
2984 * It is implementation-dependent whether a failure to initialize
2985 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002986 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002987psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002988 psa_algorithm_t alg,
2989 const uint8_t *input,
2990 size_t input_length,
2991 const uint8_t *salt,
2992 size_t salt_length,
2993 uint8_t *output,
2994 size_t output_size,
2995 size_t *output_length);
2996
2997/**
2998 * \brief Decrypt a short message with a private key.
2999 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02003000 * \param key Identifier of the key to use for the operation.
3001 * It must be an asymmetric key pair. It must
Ronald Cron96783552020-10-19 12:06:30 +02003002 * allow the usage #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003003 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02003004 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003005 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003006 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003007 * \param[in] salt A salt or label, if supported by the
3008 * encryption algorithm.
3009 * If the algorithm does not support a
3010 * salt, pass \c NULL.
3011 * If the algorithm supports an optional
3012 * salt and you do not want to pass a salt,
3013 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003014 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003015 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3016 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003017 * \param salt_length Size of the \p salt buffer in bytes.
3018 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003019 * \param[out] output Buffer where the decrypted message is to
3020 * be written.
3021 * \param output_size Size of the \c output buffer in bytes.
3022 * \param[out] output_length On success, the number of bytes
3023 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003024 *
Gilles Peskine28538492018-07-11 17:34:00 +02003025 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003026 * \retval #PSA_ERROR_INVALID_HANDLE
3027 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003028 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003029 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003030 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003031 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003032 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003033 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003034 * \retval #PSA_ERROR_NOT_SUPPORTED
3035 * \retval #PSA_ERROR_INVALID_ARGUMENT
3036 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3037 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3038 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003039 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003040 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003041 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3042 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003043 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003044 * The library has not been previously initialized by psa_crypto_init().
3045 * It is implementation-dependent whether a failure to initialize
3046 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003047 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003048psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003049 psa_algorithm_t alg,
3050 const uint8_t *input,
3051 size_t input_length,
3052 const uint8_t *salt,
3053 size_t salt_length,
3054 uint8_t *output,
3055 size_t output_size,
3056 size_t *output_length);
3057
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003058/**@}*/
3059
Gilles Peskine35675b62019-05-16 17:26:11 +02003060/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003061 * @{
3062 */
3063
Gilles Peskine35675b62019-05-16 17:26:11 +02003064/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003065 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003066 * Before calling any function on a key derivation operation object, the
3067 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003068 * - Set the structure to all-bits-zero, for example:
3069 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003070 * psa_key_derivation_operation_t operation;
3071 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003072 * \endcode
3073 * - Initialize the structure to logical zero values, for example:
3074 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003075 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003076 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003077 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003078 * for example:
3079 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003080 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003081 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003082 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003083 * to the structure, for example:
3084 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003085 * psa_key_derivation_operation_t operation;
3086 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003087 * \endcode
3088 *
3089 * This is an implementation-defined \c struct. Applications should not
3090 * make any assumptions about the content of this structure except
3091 * as directed by the documentation of a specific implementation.
3092 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003093typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003094
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003095/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003096 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003097 * This macro returns a suitable initializer for a key derivation operation
3098 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003099 */
3100#ifdef __DOXYGEN_ONLY__
3101/* This is an example definition for documentation purposes.
3102 * Implementations should define a suitable value in `crypto_struct.h`.
3103 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003104#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003105#endif
3106
Gilles Peskine35675b62019-05-16 17:26:11 +02003107/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003108 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003109static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003110
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003111/** Set up a key derivation operation.
3112 *
3113 * A key derivation algorithm takes some inputs and uses them to generate
3114 * a byte stream in a deterministic way.
3115 * This byte stream can be used to produce keys and other
3116 * cryptographic material.
3117 *
3118 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003119 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3120 * -# Call psa_key_derivation_setup() to select the algorithm.
3121 * -# Provide the inputs for the key derivation by calling
3122 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3123 * as appropriate. Which inputs are needed, in what order, and whether
3124 * they may be keys and if so of what type depends on the algorithm.
3125 * -# Optionally set the operation's maximum capacity with
3126 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3127 * of or after providing inputs. For some algorithms, this step is mandatory
3128 * because the output depends on the maximum capacity.
3129 * -# To derive a key, call psa_key_derivation_output_key().
3130 * To derive a byte string for a different purpose, call
3131 * psa_key_derivation_output_bytes().
3132 * Successive calls to these functions use successive output bytes
3133 * calculated by the key derivation algorithm.
3134 * -# Clean up the key derivation operation object with
3135 * psa_key_derivation_abort().
3136 *
3137 * If this function returns an error, the key derivation operation object is
3138 * not changed.
3139 *
3140 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3141 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003142 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003143 * Implementations must reject an attempt to derive a key of size 0.
3144 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003145 * \param[in,out] operation The key derivation operation object
3146 * to set up. It must
3147 * have been initialized but not set up yet.
3148 * \param alg The key derivation algorithm to compute
3149 * (\c PSA_ALG_XXX value such that
3150 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3151 *
3152 * \retval #PSA_SUCCESS
3153 * Success.
3154 * \retval #PSA_ERROR_INVALID_ARGUMENT
3155 * \c alg is not a key derivation algorithm.
3156 * \retval #PSA_ERROR_NOT_SUPPORTED
3157 * \c alg is not supported or is not a key derivation algorithm.
3158 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3159 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3160 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003161 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003162 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003163 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003164 * The operation state is not valid (it must be inactive).
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003165 * \retval #PSA_ERROR_BAD_STATE
3166 * The library has not been previously initialized by psa_crypto_init().
3167 * It is implementation-dependent whether a failure to initialize
3168 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003169 */
3170psa_status_t psa_key_derivation_setup(
3171 psa_key_derivation_operation_t *operation,
3172 psa_algorithm_t alg);
3173
Gilles Peskine35675b62019-05-16 17:26:11 +02003174/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003175 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003176 * The capacity of a key derivation is the maximum number of bytes that it can
3177 * return. When you get *N* bytes of output from a key derivation operation,
3178 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003179 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003180 * \param[in] operation The operation to query.
3181 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003182 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003183 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003184 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003185 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003186 * The operation state is not valid (it must be active).
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003187 * \retval #PSA_ERROR_HARDWARE_FAILURE
3188 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003189 * \retval #PSA_ERROR_BAD_STATE
3190 * The library has not been previously initialized by psa_crypto_init().
3191 * It is implementation-dependent whether a failure to initialize
3192 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003193 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003194psa_status_t psa_key_derivation_get_capacity(
3195 const psa_key_derivation_operation_t *operation,
3196 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003197
Gilles Peskine35675b62019-05-16 17:26:11 +02003198/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003199 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003200 * The capacity of a key derivation operation is the maximum number of bytes
3201 * that the key derivation operation can return from this point onwards.
3202 *
3203 * \param[in,out] operation The key derivation operation object to modify.
3204 * \param capacity The new capacity of the operation.
3205 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003206 * current capacity.
3207 *
3208 * \retval #PSA_SUCCESS
3209 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003210 * \p capacity is larger than the operation's current capacity.
3211 * In this case, the operation object remains valid and its capacity
3212 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003213 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003214 * The operation state is not valid (it must be active).
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003215 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003216 * \retval #PSA_ERROR_HARDWARE_FAILURE
3217 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003218 * \retval #PSA_ERROR_BAD_STATE
3219 * The library has not been previously initialized by psa_crypto_init().
3220 * It is implementation-dependent whether a failure to initialize
3221 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003222 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003223psa_status_t psa_key_derivation_set_capacity(
3224 psa_key_derivation_operation_t *operation,
3225 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003226
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003227/** Use the maximum possible capacity for a key derivation operation.
3228 *
3229 * Use this value as the capacity argument when setting up a key derivation
3230 * to indicate that the operation should have the maximum possible capacity.
3231 * The value of the maximum possible capacity depends on the key derivation
3232 * algorithm.
3233 */
3234#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3235
3236/** Provide an input for key derivation or key agreement.
3237 *
3238 * Which inputs are required and in what order depends on the algorithm.
3239 * Refer to the documentation of each key derivation or key agreement
3240 * algorithm for information.
3241 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003242 * This function passes direct inputs, which is usually correct for
3243 * non-secret inputs. To pass a secret input, which should be in a key
3244 * object, call psa_key_derivation_input_key() instead of this function.
3245 * Refer to the documentation of individual step types
3246 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3247 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003248 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003249 * If this function returns an error status, the operation enters an error
3250 * state and must be aborted by calling psa_key_derivation_abort().
3251 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003252 * \param[in,out] operation The key derivation operation object to use.
3253 * It must have been set up with
3254 * psa_key_derivation_setup() and must not
3255 * have produced any output yet.
3256 * \param step Which step the input data is for.
3257 * \param[in] data Input data to use.
3258 * \param data_length Size of the \p data buffer in bytes.
3259 *
3260 * \retval #PSA_SUCCESS
3261 * Success.
3262 * \retval #PSA_ERROR_INVALID_ARGUMENT
3263 * \c step is not compatible with the operation's algorithm.
3264 * \retval #PSA_ERROR_INVALID_ARGUMENT
3265 * \c step does not allow direct inputs.
3266 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3267 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3268 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003269 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003270 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003271 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003272 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003273 * \retval #PSA_ERROR_BAD_STATE
3274 * The library has not been previously initialized by psa_crypto_init().
3275 * It is implementation-dependent whether a failure to initialize
3276 * results in this error code.
3277 */
3278psa_status_t psa_key_derivation_input_bytes(
3279 psa_key_derivation_operation_t *operation,
3280 psa_key_derivation_step_t step,
3281 const uint8_t *data,
3282 size_t data_length);
3283
3284/** Provide an input for key derivation in the form of a key.
3285 *
3286 * Which inputs are required and in what order depends on the algorithm.
3287 * Refer to the documentation of each key derivation or key agreement
3288 * algorithm for information.
3289 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003290 * This function obtains input from a key object, which is usually correct for
3291 * secret inputs or for non-secret personalization strings kept in the key
3292 * store. To pass a non-secret parameter which is not in the key store,
3293 * call psa_key_derivation_input_bytes() instead of this function.
3294 * Refer to the documentation of individual step types
3295 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3296 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003297 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003298 * If this function returns an error status, the operation enters an error
3299 * state and must be aborted by calling psa_key_derivation_abort().
3300 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003301 * \param[in,out] operation The key derivation operation object to use.
3302 * It must have been set up with
3303 * psa_key_derivation_setup() and must not
3304 * have produced any output yet.
3305 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003306 * \param key Identifier of the key. It must have an
3307 * appropriate type for step and must allow the
Ronald Cron96783552020-10-19 12:06:30 +02003308 * usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003309 *
3310 * \retval #PSA_SUCCESS
3311 * Success.
3312 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003313 * \retval #PSA_ERROR_NOT_PERMITTED
3314 * \retval #PSA_ERROR_INVALID_ARGUMENT
3315 * \c step is not compatible with the operation's algorithm.
3316 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine224b0d62019-09-23 18:13:17 +02003317 * \c step does not allow key inputs of the given type
3318 * or does not allow key inputs at all.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003319 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3320 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3321 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003322 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003323 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003324 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003325 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003326 * \retval #PSA_ERROR_BAD_STATE
3327 * The library has not been previously initialized by psa_crypto_init().
3328 * It is implementation-dependent whether a failure to initialize
3329 * results in this error code.
3330 */
3331psa_status_t psa_key_derivation_input_key(
3332 psa_key_derivation_operation_t *operation,
3333 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003334 mbedtls_svc_key_id_t key);
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003335
3336/** Perform a key agreement and use the shared secret as input to a key
3337 * derivation.
3338 *
3339 * A key agreement algorithm takes two inputs: a private key \p private_key
3340 * a public key \p peer_key.
3341 * The result of this function is passed as input to a key derivation.
3342 * The output of this key derivation can be extracted by reading from the
3343 * resulting operation to produce keys and other cryptographic material.
3344 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003345 * If this function returns an error status, the operation enters an error
3346 * state and must be aborted by calling psa_key_derivation_abort().
3347 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003348 * \param[in,out] operation The key derivation operation object to use.
3349 * It must have been set up with
3350 * psa_key_derivation_setup() with a
3351 * key agreement and derivation algorithm
3352 * \c alg (\c PSA_ALG_XXX value such that
3353 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3354 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3355 * is false).
3356 * The operation must be ready for an
3357 * input of the type given by \p step.
3358 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003359 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003360 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003361 * \param[in] peer_key Public key of the peer. The peer key must be in the
3362 * same format that psa_import_key() accepts for the
3363 * public key type corresponding to the type of
3364 * private_key. That is, this function performs the
3365 * equivalent of
3366 * #psa_import_key(...,
3367 * `peer_key`, `peer_key_length`) where
3368 * with key attributes indicating the public key
3369 * type corresponding to the type of `private_key`.
3370 * For example, for EC keys, this means that peer_key
3371 * is interpreted as a point on the curve that the
3372 * private key is on. The standard formats for public
3373 * keys are documented in the documentation of
3374 * psa_export_public_key().
3375 * \param peer_key_length Size of \p peer_key in bytes.
3376 *
3377 * \retval #PSA_SUCCESS
3378 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01003379 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003380 * The operation state is not valid for this key agreement \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003381 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003382 * \retval #PSA_ERROR_NOT_PERMITTED
3383 * \retval #PSA_ERROR_INVALID_ARGUMENT
3384 * \c private_key is not compatible with \c alg,
3385 * or \p peer_key is not valid for \c alg or not compatible with
3386 * \c private_key.
3387 * \retval #PSA_ERROR_NOT_SUPPORTED
3388 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskine224b0d62019-09-23 18:13:17 +02003389 * \retval #PSA_ERROR_INVALID_ARGUMENT
3390 * \c step does not allow an input resulting from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003391 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3392 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3393 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003394 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003395 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003396 * \retval #PSA_ERROR_BAD_STATE
3397 * The library has not been previously initialized by psa_crypto_init().
3398 * It is implementation-dependent whether a failure to initialize
3399 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003400 */
3401psa_status_t psa_key_derivation_key_agreement(
3402 psa_key_derivation_operation_t *operation,
3403 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003404 mbedtls_svc_key_id_t private_key,
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003405 const uint8_t *peer_key,
3406 size_t peer_key_length);
3407
Gilles Peskine35675b62019-05-16 17:26:11 +02003408/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003409 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003410 * This function calculates output bytes from a key derivation algorithm and
3411 * return those bytes.
3412 * If you view the key derivation's output as a stream of bytes, this
3413 * function destructively reads the requested number of bytes from the
3414 * stream.
3415 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003416 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003417 * If this function returns an error status other than
3418 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003419 * state and must be aborted by calling psa_key_derivation_abort().
3420 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003421 * \param[in,out] operation The key derivation operation object to read from.
3422 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003423 * \param output_length Number of bytes to output.
3424 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003425 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003426 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003427 * The operation's capacity was less than
3428 * \p output_length bytes. Note that in this case,
3429 * no output is written to the output buffer.
3430 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003431 * subsequent calls to this function will not
3432 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003433 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003434 * The operation state is not valid (it must be active and completed
3435 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003436 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3437 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3438 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003439 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003440 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003441 * \retval #PSA_ERROR_BAD_STATE
3442 * The library has not been previously initialized by psa_crypto_init().
3443 * It is implementation-dependent whether a failure to initialize
3444 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003445 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003446psa_status_t psa_key_derivation_output_bytes(
3447 psa_key_derivation_operation_t *operation,
3448 uint8_t *output,
3449 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003450
Gilles Peskine35675b62019-05-16 17:26:11 +02003451/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003452 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003453 * This function calculates output bytes from a key derivation algorithm
3454 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003455 * The key's location, usage policy, type and size are taken from
3456 * \p attributes.
3457 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003458 * If you view the key derivation's output as a stream of bytes, this
3459 * function destructively reads as many bytes as required from the
3460 * stream.
3461 * The operation's capacity decreases by the number of bytes read.
3462 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003463 * If this function returns an error status other than
3464 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003465 * state and must be aborted by calling psa_key_derivation_abort().
3466 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003467 * How much output is produced and consumed from the operation, and how
3468 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003469 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003470 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003471 * of a given size, this function is functionally equivalent to
3472 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003473 * and passing the resulting output to #psa_import_key.
3474 * However, this function has a security benefit:
3475 * if the implementation provides an isolation boundary then
3476 * the key material is not exposed outside the isolation boundary.
3477 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003478 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003479 * The following key types defined in this specification follow this scheme:
3480 *
3481 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003482 * - #PSA_KEY_TYPE_ARC4;
3483 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003484 * - #PSA_KEY_TYPE_DERIVE;
3485 * - #PSA_KEY_TYPE_HMAC.
3486 *
3487 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003488 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003489 * Montgomery curve), this function always draws a byte string whose
3490 * length is determined by the curve, and sets the mandatory bits
3491 * accordingly. That is:
3492 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01003493 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003494 * string and process it as specified in RFC 7748 &sect;5.
Paul Elliott8ff510a2020-06-02 17:19:28 +01003495 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003496 * string and process it as specified in RFC 7748 &sect;5.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003497 *
3498 * - For key types for which the key is represented by a single sequence of
3499 * \p bits bits with constraints as to which bit sequences are acceptable,
3500 * this function draws a byte string of length (\p bits / 8) bytes rounded
3501 * up to the nearest whole number of bytes. If the resulting byte string
3502 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3503 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003504 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003505 * for the output produced by psa_export_key().
3506 * The following key types defined in this specification follow this scheme:
3507 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003508 * - #PSA_KEY_TYPE_DES.
3509 * Force-set the parity bits, but discard forbidden weak keys.
3510 * For 2-key and 3-key triple-DES, the three keys are generated
3511 * successively (for example, for 3-key triple-DES,
3512 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3513 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003514 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003515 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003516 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003517 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003518 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003519 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003520 * Weierstrass curve).
3521 * For these key types, interpret the byte string as integer
3522 * in big-endian order. Discard it if it is not in the range
3523 * [0, *N* - 2] where *N* is the boundary of the private key domain
3524 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003525 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003526 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003527 * This method allows compliance to NIST standards, specifically
3528 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003529 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3530 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3531 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3532 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003533 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003534 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003535 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003536 * implementation-defined.
3537 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003538 * In all cases, the data that is read is discarded from the operation.
3539 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003540 *
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003541 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3542 * the input to that step must be provided with psa_key_derivation_input_key().
3543 * Future versions of this specification may include additional restrictions
3544 * on the derived key based on the attributes and strength of the secret key.
3545 *
Gilles Peskine20628592019-04-19 19:29:50 +02003546 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003547 * \param[in,out] operation The key derivation operation object to read from.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003548 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003549 * key. For persistent keys, this is the key
3550 * identifier defined in \p attributes.
3551 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003552 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003553 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003554 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003555 * If the key is persistent, the key material and the key's metadata
3556 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003557 * \retval #PSA_ERROR_ALREADY_EXISTS
3558 * This is an attempt to create a persistent key, and there is
3559 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003560 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003561 * There was not enough data to create the desired key.
3562 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003563 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003564 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003565 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003566 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003567 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003568 * \retval #PSA_ERROR_INVALID_ARGUMENT
3569 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003570 * \retval #PSA_ERROR_NOT_PERMITTED
3571 * The #PSA_KEY_DERIVATION_INPUT_SECRET input was not provided through
3572 * a key.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003573 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003574 * The operation state is not valid (it must be active and completed
3575 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003576 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3577 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3578 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3579 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003580 * \retval #PSA_ERROR_CORRUPTION_DETECTED
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01003581 * \retval #PSA_ERROR_DATA_INVALID
3582 * \retval #PSA_ERROR_DATA_CORRUPT
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003583 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003584 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003585 * The library has not been previously initialized by psa_crypto_init().
3586 * It is implementation-dependent whether a failure to initialize
3587 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003588 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003589psa_status_t psa_key_derivation_output_key(
3590 const psa_key_attributes_t *attributes,
3591 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003592 mbedtls_svc_key_id_t *key);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003593
Gilles Peskine35675b62019-05-16 17:26:11 +02003594/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003595 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003596 * Aborting an operation frees all associated resources except for the \c
3597 * operation structure itself. Once aborted, the operation object can be reused
3598 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003599 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003600 * This function may be called at any time after the operation
3601 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003602 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003603 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3604 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003605 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003606 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003607 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003608 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003609 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3610 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003611 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003612 * \retval #PSA_ERROR_BAD_STATE
3613 * The library has not been previously initialized by psa_crypto_init().
3614 * It is implementation-dependent whether a failure to initialize
3615 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003616 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003617psa_status_t psa_key_derivation_abort(
3618 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003619
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003620/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003621 *
3622 * \warning The raw result of a key agreement algorithm such as finite-field
3623 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3624 * not be used directly as key material. It should instead be passed as
3625 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003626 * a key derivation, use psa_key_derivation_key_agreement() and other
3627 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003628 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003629 * \param alg The key agreement algorithm to compute
3630 * (\c PSA_ALG_XXX value such that
3631 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3632 * is true).
Ronald Croncf56a0a2020-08-04 09:51:30 +02003633 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003634 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003635 * \param[in] peer_key Public key of the peer. It must be
3636 * in the same format that psa_import_key()
3637 * accepts. The standard formats for public
3638 * keys are documented in the documentation
3639 * of psa_export_public_key().
3640 * \param peer_key_length Size of \p peer_key in bytes.
3641 * \param[out] output Buffer where the decrypted message is to
3642 * be written.
3643 * \param output_size Size of the \c output buffer in bytes.
3644 * \param[out] output_length On success, the number of bytes
3645 * that make up the returned output.
3646 *
3647 * \retval #PSA_SUCCESS
3648 * Success.
3649 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003650 * \retval #PSA_ERROR_NOT_PERMITTED
3651 * \retval #PSA_ERROR_INVALID_ARGUMENT
3652 * \p alg is not a key agreement algorithm
3653 * \retval #PSA_ERROR_INVALID_ARGUMENT
3654 * \p private_key is not compatible with \p alg,
3655 * or \p peer_key is not valid for \p alg or not compatible with
3656 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003657 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3658 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003659 * \retval #PSA_ERROR_NOT_SUPPORTED
3660 * \p alg is not a supported key agreement algorithm.
3661 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3662 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3663 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003664 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003665 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003666 * \retval #PSA_ERROR_BAD_STATE
3667 * The library has not been previously initialized by psa_crypto_init().
3668 * It is implementation-dependent whether a failure to initialize
3669 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003670 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003671psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003672 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02003673 const uint8_t *peer_key,
3674 size_t peer_key_length,
3675 uint8_t *output,
3676 size_t output_size,
3677 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003678
Gilles Peskineea0fb492018-07-12 17:17:20 +02003679/**@}*/
3680
Gilles Peskineedd76872018-07-20 17:42:05 +02003681/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003682 * @{
3683 */
3684
3685/**
3686 * \brief Generate random bytes.
3687 *
3688 * \warning This function **can** fail! Callers MUST check the return status
3689 * and MUST NOT use the content of the output buffer if the return
3690 * status is not #PSA_SUCCESS.
3691 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003692 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003693 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003694 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003695 * \param output_size Number of bytes to generate and output.
3696 *
Gilles Peskine28538492018-07-11 17:34:00 +02003697 * \retval #PSA_SUCCESS
3698 * \retval #PSA_ERROR_NOT_SUPPORTED
3699 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003700 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003701 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3702 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003703 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003704 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003705 * The library has not been previously initialized by psa_crypto_init().
3706 * It is implementation-dependent whether a failure to initialize
3707 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003708 */
3709psa_status_t psa_generate_random(uint8_t *output,
3710 size_t output_size);
3711
3712/**
3713 * \brief Generate a key or key pair.
3714 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003715 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02003716 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003717 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003718 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003719 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003720 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003721 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003722 * the public exponent is 65537.
3723 * The modulus is a product of two probabilistic primes
3724 * between 2^{n-1} and 2^n where n is the bit size specified in the
3725 * attributes.
3726 *
Gilles Peskine20628592019-04-19 19:29:50 +02003727 * \param[in] attributes The attributes for the new key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003728 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003729 * key. For persistent keys, this is the key
3730 * identifier defined in \p attributes.
3731 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003732 *
Gilles Peskine28538492018-07-11 17:34:00 +02003733 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003734 * Success.
3735 * If the key is persistent, the key material and the key's metadata
3736 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003737 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003738 * This is an attempt to create a persistent key, and there is
3739 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003740 * \retval #PSA_ERROR_NOT_SUPPORTED
3741 * \retval #PSA_ERROR_INVALID_ARGUMENT
3742 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3743 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3744 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3745 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003746 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003747 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01003748 * \retval #PSA_ERROR_DATA_INVALID
3749 * \retval #PSA_ERROR_DATA_CORRUPT
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003750 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003751 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003752 * The library has not been previously initialized by psa_crypto_init().
3753 * It is implementation-dependent whether a failure to initialize
3754 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003755 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003756psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003757 mbedtls_svc_key_id_t *key);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003758
3759/**@}*/
3760
Gilles Peskinee59236f2018-01-27 23:32:46 +01003761#ifdef __cplusplus
3762}
3763#endif
3764
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003765/* The file "crypto_sizes.h" contains definitions for size calculation
3766 * macros whose definitions are implementation-specific. */
3767#include "crypto_sizes.h"
3768
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003769/* The file "crypto_struct.h" contains definitions for
3770 * implementation-specific structs that are declared above. */
3771#include "crypto_struct.h"
3772
3773/* The file "crypto_extra.h" contains vendor-specific definitions. This
3774 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003775#include "crypto_extra.h"
3776
3777#endif /* PSA_CRYPTO_H */