blob: b41a20bfc4533e1cdb16cdd1d7a3cd1e9c1bb142 [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
93 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
94 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +020095 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +020096 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010097 */
98psa_status_t psa_crypto_init(void);
99
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100100/**@}*/
101
Gilles Peskine105f67f2019-07-23 18:16:05 +0200102/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +0200103 * @{
104 */
105
Gilles Peskinea0c06552019-05-21 15:54:54 +0200106/** \def PSA_KEY_ATTRIBUTES_INIT
107 *
108 * This macro returns a suitable initializer for a key attribute structure
109 * of type #psa_key_attributes_t.
110 */
111#ifdef __DOXYGEN_ONLY__
112/* This is an example definition for documentation purposes.
113 * Implementations should define a suitable value in `crypto_struct.h`.
114 */
115#define PSA_KEY_ATTRIBUTES_INIT {0}
116#endif
117
118/** Return an initial value for a key attributes structure.
119 */
120static psa_key_attributes_t psa_key_attributes_init(void);
121
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200122/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200123 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200124 * If the attribute structure currently declares the key as volatile (which
125 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200126 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200127 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200128 * This function does not access storage, it merely stores the given
129 * value in the structure.
130 * The persistent key will be written to storage when the attribute
131 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200132 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200133 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200134 *
Gilles Peskine20628592019-04-19 19:29:50 +0200135 * This function may be declared as `static` (i.e. without external
136 * linkage). This function may be provided as a function-like macro,
137 * but in this case it must evaluate each of its arguments exactly once.
138 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200139 * \param[out] attributes The attribute structure to write to.
140 * \param key The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200141 */
Ronald Cron71016a92020-08-28 19:01:50 +0200142static void psa_set_key_id( psa_key_attributes_t *attributes,
143 mbedtls_svc_key_id_t key );
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200144
Ronald Cron6b5ff532020-10-16 14:38:19 +0200145#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
146/** Set the owner identifier of a key.
147 *
148 * When key identifiers encode key owner identifiers, psa_set_key_id() does
149 * not allow to define in key attributes the owner of volatile keys as
150 * psa_set_key_id() enforces the key to be persistent.
151 *
152 * This function allows to set in key attributes the owner identifier of a
153 * key. It is intended to be used for volatile keys. For persistent keys,
154 * it is recommended to use the PSA Cryptography API psa_set_key_id() to define
155 * the owner of a key.
156 *
157 * \param[out] attributes The attribute structure to write to.
158 * \param owner_id The key owner identifier.
159 */
160static void mbedtls_set_key_owner_id( psa_key_attributes_t *attributes,
161 mbedtls_key_owner_id_t owner_id );
162#endif
163
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200164/** Set the location of a persistent key.
165 *
166 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200167 * with psa_set_key_id(). By default, a key that has a persistent identifier
168 * is stored in the default storage area identifier by
169 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
170 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200171 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200172 * This function does not access storage, it merely stores the given
173 * value in the structure.
174 * The persistent key will be written to storage when the attribute
175 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200176 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200177 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200178 *
179 * This function may be declared as `static` (i.e. without external
180 * linkage). This function may be provided as a function-like macro,
181 * but in this case it must evaluate each of its arguments exactly once.
182 *
183 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200184 * \param lifetime The lifetime for the key.
185 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200186 * key will be volatile, and the key identifier
187 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200188 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200189static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
190 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200191
Gilles Peskine20628592019-04-19 19:29:50 +0200192/** Retrieve the key identifier from key attributes.
193 *
194 * This function may be declared as `static` (i.e. without external
195 * linkage). This function may be provided as a function-like macro,
196 * but in this case it must evaluate its argument exactly once.
197 *
198 * \param[in] attributes The key attribute structure to query.
199 *
200 * \return The persistent identifier stored in the attribute structure.
201 * This value is unspecified if the attribute structure declares
202 * the key as volatile.
203 */
Ronald Cron71016a92020-08-28 19:01:50 +0200204static mbedtls_svc_key_id_t psa_get_key_id(
205 const psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200206
Gilles Peskine20628592019-04-19 19:29:50 +0200207/** Retrieve the lifetime from key attributes.
208 *
209 * This function may be declared as `static` (i.e. without external
210 * linkage). This function may be provided as a function-like macro,
211 * but in this case it must evaluate its argument exactly once.
212 *
213 * \param[in] attributes The key attribute structure to query.
214 *
215 * \return The lifetime value stored in the attribute structure.
216 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200217static psa_key_lifetime_t psa_get_key_lifetime(
218 const psa_key_attributes_t *attributes);
219
Gilles Peskine20628592019-04-19 19:29:50 +0200220/** Declare usage flags for a key.
221 *
222 * Usage flags are part of a key's usage policy. They encode what
223 * kind of operations are permitted on the key. For more details,
224 * refer to the documentation of the type #psa_key_usage_t.
225 *
226 * This function overwrites any usage flags
227 * previously set in \p attributes.
228 *
229 * This function may be declared as `static` (i.e. without external
230 * linkage). This function may be provided as a function-like macro,
231 * but in this case it must evaluate each of its arguments exactly once.
232 *
233 * \param[out] attributes The attribute structure to write to.
234 * \param usage_flags The usage flags to write.
235 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200236static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
237 psa_key_usage_t usage_flags);
238
Gilles Peskine20628592019-04-19 19:29:50 +0200239/** Retrieve the usage flags from key attributes.
240 *
241 * This function may be declared as `static` (i.e. without external
242 * linkage). This function may be provided as a function-like macro,
243 * but in this case it must evaluate its argument exactly once.
244 *
245 * \param[in] attributes The key attribute structure to query.
246 *
247 * \return The usage flags stored in the attribute structure.
248 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200249static psa_key_usage_t psa_get_key_usage_flags(
250 const psa_key_attributes_t *attributes);
251
Gilles Peskine20628592019-04-19 19:29:50 +0200252/** Declare the permitted algorithm policy for a key.
253 *
254 * The permitted algorithm policy of a key encodes which algorithm or
Gilles Peskinea170d922019-09-12 16:59:37 +0200255 * algorithms are permitted to be used with this key. The following
256 * algorithm policies are supported:
257 * - 0 does not allow any cryptographic operation with the key. The key
258 * may be used for non-cryptographic actions such as exporting (if
259 * permitted by the usage flags).
260 * - An algorithm value permits this particular algorithm.
261 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
262 * signature scheme with any hash algorithm.
Gilles Peskine20628592019-04-19 19:29:50 +0200263 *
264 * This function overwrites any algorithm policy
265 * previously set in \p attributes.
266 *
267 * This function may be declared as `static` (i.e. without external
268 * linkage). This function may be provided as a function-like macro,
269 * but in this case it must evaluate each of its arguments exactly once.
270 *
271 * \param[out] attributes The attribute structure to write to.
272 * \param alg The permitted algorithm policy to write.
273 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200274static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
275 psa_algorithm_t alg);
276
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100277
Gilles Peskine20628592019-04-19 19:29:50 +0200278/** Retrieve the algorithm policy from key attributes.
279 *
280 * This function may be declared as `static` (i.e. without external
281 * linkage). This function may be provided as a function-like macro,
282 * but in this case it must evaluate its argument exactly once.
283 *
284 * \param[in] attributes The key attribute structure to query.
285 *
286 * \return The algorithm stored in the attribute structure.
287 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200288static psa_algorithm_t psa_get_key_algorithm(
289 const psa_key_attributes_t *attributes);
290
Gilles Peskine20628592019-04-19 19:29:50 +0200291/** Declare the type of a key.
292 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200293 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200294 * previously set in \p attributes.
295 *
296 * This function may be declared as `static` (i.e. without external
297 * linkage). This function may be provided as a function-like macro,
298 * but in this case it must evaluate each of its arguments exactly once.
299 *
300 * \param[out] attributes The attribute structure to write to.
301 * \param type The key type to write.
Gilles Peskinea170d922019-09-12 16:59:37 +0200302 * If this is 0, the key type in \p attributes
303 * becomes unspecified.
Gilles Peskine20628592019-04-19 19:29:50 +0200304 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200305static void psa_set_key_type(psa_key_attributes_t *attributes,
306 psa_key_type_t type);
307
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100308
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200309/** Declare the size of a key.
310 *
311 * This function overwrites any key size previously set in \p attributes.
312 *
313 * This function may be declared as `static` (i.e. without external
314 * linkage). This function may be provided as a function-like macro,
315 * but in this case it must evaluate each of its arguments exactly once.
316 *
317 * \param[out] attributes The attribute structure to write to.
318 * \param bits The key size in bits.
Gilles Peskinea170d922019-09-12 16:59:37 +0200319 * If this is 0, the key size in \p attributes
Gilles Peskine05c900b2019-09-12 18:29:43 +0200320 * becomes unspecified. Keys of size 0 are
321 * not supported.
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200322 */
323static void psa_set_key_bits(psa_key_attributes_t *attributes,
324 size_t bits);
325
Gilles Peskine20628592019-04-19 19:29:50 +0200326/** Retrieve the key type from key attributes.
327 *
328 * This function may be declared as `static` (i.e. without external
329 * linkage). This function may be provided as a function-like macro,
330 * but in this case it must evaluate its argument exactly once.
331 *
332 * \param[in] attributes The key attribute structure to query.
333 *
334 * \return The key type stored in the attribute structure.
335 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200336static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
337
Gilles Peskine20628592019-04-19 19:29:50 +0200338/** Retrieve the key size 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 size stored in the attribute structure, in bits.
347 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200348static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
349
Gilles Peskine20628592019-04-19 19:29:50 +0200350/** Retrieve the attributes of a key.
351 *
352 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200353 * psa_reset_key_attributes(). It then copies the attributes of
354 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200355 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200356 * \note This function may allocate memory or other resources.
357 * Once you have called this function on an attribute structure,
358 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200359 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200360 * \param[in] key Identifier of the key to query.
Gilles Peskine20628592019-04-19 19:29:50 +0200361 * \param[in,out] attributes On success, the attributes of the key.
362 * On failure, equivalent to a
363 * freshly-initialized structure.
364 *
365 * \retval #PSA_SUCCESS
366 * \retval #PSA_ERROR_INVALID_HANDLE
367 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
368 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100369 * \retval #PSA_ERROR_CORRUPTION_DETECTED
370 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100371 * \retval #PSA_ERROR_BAD_STATE
372 * The library has not been previously initialized by psa_crypto_init().
373 * It is implementation-dependent whether a failure to initialize
374 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200375 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200376psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
Gilles Peskine4747d192019-04-17 15:05:45 +0200377 psa_key_attributes_t *attributes);
378
Gilles Peskine20628592019-04-19 19:29:50 +0200379/** Reset a key attribute structure to a freshly initialized state.
380 *
381 * You must initialize the attribute structure as described in the
382 * documentation of the type #psa_key_attributes_t before calling this
383 * function. Once the structure has been initialized, you may call this
384 * function at any time.
385 *
386 * This function frees any auxiliary resources that the structure
387 * may contain.
388 *
389 * \param[in,out] attributes The attribute structure to reset.
390 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200391void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200392
Gilles Peskine87a5e562019-04-17 12:28:25 +0200393/**@}*/
394
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100395/** \defgroup key_management Key management
396 * @{
397 */
398
Ronald Cron277a85f2020-08-04 15:49:48 +0200399/** Remove non-essential copies of key material from memory.
400 *
401 * If the key identifier designates a volatile key, this functions does not do
402 * anything and returns successfully.
403 *
404 * If the key identifier designates a persistent key, then this function will
405 * free all resources associated with the key in volatile memory. The key
406 * data in persistent storage is not affected and the key can still be used.
407 *
408 * \param key Identifier of the key to purge.
409 *
410 * \retval #PSA_SUCCESS
411 * The key material will have been removed from memory if it is not
412 * currently required.
413 * \retval #PSA_ERROR_INVALID_ARGUMENT
414 * \p key is not a valid key identifier.
415 * \retval #PSA_ERROR_BAD_STATE
416 * The library has not been previously initialized by psa_crypto_init().
417 * It is implementation-dependent whether a failure to initialize
418 * results in this error code.
419 */
420psa_status_t psa_purge_key(mbedtls_svc_key_id_t key);
421
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100422/** Make a copy of a key.
423 *
424 * Copy key material from one location to another.
425 *
426 * This function is primarily useful to copy a key from one location
427 * to another, since it populates a key using the material from
428 * another key which may have a different lifetime.
429 *
430 * This function may be used to share a key with a different party,
431 * subject to implementation-defined restrictions on key sharing.
432 *
433 * The policy on the source key must have the usage flag
434 * #PSA_KEY_USAGE_COPY set.
435 * This flag is sufficient to permit the copy if the key has the lifetime
436 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
437 * Some secure elements do not provide a way to copy a key without
438 * making it extractable from the secure element. If a key is located
439 * in such a secure element, then the key must have both usage flags
440 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
441 * a copy of the key outside the secure element.
442 *
443 * The resulting key may only be used in a way that conforms to
444 * both the policy of the original key and the policy specified in
445 * the \p attributes parameter:
446 * - The usage flags on the resulting key are the bitwise-and of the
447 * usage flags on the source policy and the usage flags in \p attributes.
448 * - If both allow the same algorithm or wildcard-based
449 * algorithm policy, the resulting key has the same algorithm policy.
450 * - If either of the policies allows an algorithm and the other policy
451 * allows a wildcard-based algorithm policy that includes this algorithm,
452 * the resulting key allows the same algorithm.
453 * - If the policies do not allow any algorithm in common, this function
454 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
455 *
456 * The effect of this function on implementation-defined attributes is
457 * implementation-defined.
458 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200459 * \param source_key The key to copy. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +0200460 * #PSA_KEY_USAGE_COPY. If a private or secret key is
Ronald Croncf56a0a2020-08-04 09:51:30 +0200461 * being copied outside of a secure element it must
Ronald Cron96783552020-10-19 12:06:30 +0200462 * also allow #PSA_KEY_USAGE_EXPORT.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100463 * \param[in] attributes The attributes for the new key.
464 * They are used as follows:
465 * - The key type and size may be 0. If either is
466 * nonzero, it must match the corresponding
467 * attribute of the source key.
468 * - The key location (the lifetime and, for
469 * persistent keys, the key identifier) is
470 * used directly.
471 * - The policy constraints (usage flags and
472 * algorithm policy) are combined from
473 * the source key and \p attributes so that
474 * both sets of restrictions apply, as
475 * described in the documentation of this function.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200476 * \param[out] target_key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +0200477 * key. For persistent keys, this is the key
478 * identifier defined in \p attributes.
479 * \c 0 on failure.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100480 *
481 * \retval #PSA_SUCCESS
482 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200483 * \p source_key is invalid.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100484 * \retval #PSA_ERROR_ALREADY_EXISTS
485 * This is an attempt to create a persistent key, and there is
486 * already a persistent key with the given identifier.
487 * \retval #PSA_ERROR_INVALID_ARGUMENT
488 * The lifetime or identifier in \p attributes are invalid.
489 * \retval #PSA_ERROR_INVALID_ARGUMENT
490 * The policy constraints on the source and specified in
491 * \p attributes are incompatible.
492 * \retval #PSA_ERROR_INVALID_ARGUMENT
493 * \p attributes specifies a key type or key size
494 * which does not match the attributes of the source key.
495 * \retval #PSA_ERROR_NOT_PERMITTED
496 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
497 * \retval #PSA_ERROR_NOT_PERMITTED
498 * The source key is not exportable and its lifetime does not
499 * allow copying it to the target's lifetime.
500 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
501 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
502 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
503 * \retval #PSA_ERROR_HARDWARE_FAILURE
504 * \retval #PSA_ERROR_STORAGE_FAILURE
505 * \retval #PSA_ERROR_CORRUPTION_DETECTED
506 * \retval #PSA_ERROR_BAD_STATE
507 * The library has not been previously initialized by psa_crypto_init().
508 * It is implementation-dependent whether a failure to initialize
509 * results in this error code.
510 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200511psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100512 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200513 mbedtls_svc_key_id_t *target_key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100514
515
516/**
517 * \brief Destroy a key.
518 *
519 * This function destroys a key from both volatile
520 * memory and, if applicable, non-volatile storage. Implementations shall
521 * make a best effort to ensure that that the key material cannot be recovered.
522 *
523 * This function also erases any metadata such as policies and frees
Ronald Croncf56a0a2020-08-04 09:51:30 +0200524 * resources associated with the key.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100525 *
526 * If a key is currently in use in a multipart operation, then destroying the
527 * key will cause the multipart operation to fail.
528 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200529 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
Ronald Cron96783552020-10-19 12:06:30 +0200530 * return #PSA_SUCCESS.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100531 *
532 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +0200533 * \p key was a valid identifier and the key material that it
534 * referred to has been erased. Alternatively, \p key is \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100535 * \retval #PSA_ERROR_NOT_PERMITTED
536 * The key cannot be erased because it is
537 * read-only, either due to a policy or due to physical restrictions.
538 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200539 * \p key is not a valid identifier nor \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100540 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
541 * There was an failure in communication with the cryptoprocessor.
542 * The key material may still be present in the cryptoprocessor.
543 * \retval #PSA_ERROR_STORAGE_FAILURE
544 * The storage is corrupted. Implementations shall make a best effort
545 * to erase key material even in this stage, however applications
546 * should be aware that it may be impossible to guarantee that the
547 * key material is not recoverable in such cases.
548 * \retval #PSA_ERROR_CORRUPTION_DETECTED
549 * An unexpected condition which is not a storage corruption or
550 * a communication failure occurred. The cryptoprocessor may have
551 * been compromised.
552 * \retval #PSA_ERROR_BAD_STATE
553 * The library has not been previously initialized by psa_crypto_init().
554 * It is implementation-dependent whether a failure to initialize
555 * results in this error code.
556 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200557psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100558
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100559/**@}*/
560
561/** \defgroup import_export Key import and export
562 * @{
563 */
564
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100565/**
566 * \brief Import a key in binary format.
567 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100568 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100569 * documentation of psa_export_public_key() for the format of public keys
570 * and to the documentation of psa_export_key() for the format for
571 * other key types.
572 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200573 * The key data determines the key size. The attributes may optionally
574 * specify a key size; in this case it must match the size determined
575 * from the key data. A key size of 0 in \p attributes indicates that
576 * the key size is solely determined by the key data.
577 *
578 * Implementations must reject an attempt to import a key of size 0.
579 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100580 * This specification supports a single format for each key type.
581 * Implementations may support other formats as long as the standard
582 * format is supported. Implementations that support other formats
583 * should ensure that the formats are clearly unambiguous so as to
584 * minimize the risk that an invalid input is accidentally interpreted
585 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100586 *
Gilles Peskine20628592019-04-19 19:29:50 +0200587 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200588 * The key size is always determined from the
589 * \p data buffer.
590 * If the key size in \p attributes is nonzero,
591 * it must be equal to the size from \p data.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200592 * \param[out] key On success, an identifier to the newly created key.
Ronald Cron4067d1c2020-10-19 13:34:38 +0200593 * For persistent keys, this is the key identifier
594 * defined in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200595 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100596 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200597 * buffer is interpreted according to the type declared
598 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200599 * All implementations must support at least the format
600 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100601 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200602 * the chosen type. Implementations may allow other
603 * formats, but should be conservative: implementations
604 * should err on the side of rejecting content if it
605 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200606 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100607 *
Gilles Peskine28538492018-07-11 17:34:00 +0200608 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100609 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100610 * If the key is persistent, the key material and the key's metadata
611 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200612 * \retval #PSA_ERROR_ALREADY_EXISTS
613 * This is an attempt to create a persistent key, and there is
614 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200615 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200616 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200617 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200618 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200619 * The key attributes, as a whole, are invalid.
620 * \retval #PSA_ERROR_INVALID_ARGUMENT
621 * The key data is not correctly formatted.
622 * \retval #PSA_ERROR_INVALID_ARGUMENT
623 * The size in \p attributes is nonzero and does not match the size
624 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200625 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
626 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
627 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100628 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200629 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200630 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300631 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300632 * The library has not been previously initialized by psa_crypto_init().
633 * It is implementation-dependent whether a failure to initialize
634 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100635 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200636psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100637 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200638 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200639 mbedtls_svc_key_id_t *key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100640
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100641
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100642
643/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100644 * \brief Export a key in binary format.
645 *
646 * The output of this function can be passed to psa_import_key() to
647 * create an equivalent object.
648 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100649 * If the implementation of psa_import_key() supports other formats
650 * beyond the format specified here, the output from psa_export_key()
651 * must use the representation specified here, not the original
652 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100653 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100654 * For standard key types, the output format is as follows:
655 *
656 * - For symmetric keys (including MAC keys), the format is the
657 * raw bytes of the key.
658 * - For DES, the key data consists of 8 bytes. The parity bits must be
659 * correct.
660 * - For Triple-DES, the format is the concatenation of the
661 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200662 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200663 * is the non-encrypted DER encoding of the representation defined by
664 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
665 * ```
666 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200667 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200668 * modulus INTEGER, -- n
669 * publicExponent INTEGER, -- e
670 * privateExponent INTEGER, -- d
671 * prime1 INTEGER, -- p
672 * prime2 INTEGER, -- q
673 * exponent1 INTEGER, -- d mod (p-1)
674 * exponent2 INTEGER, -- d mod (q-1)
675 * coefficient INTEGER, -- (inverse of q) mod p
676 * }
677 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200678 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200679 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100680 * a representation of the private value as a `ceiling(m/8)`-byte string
681 * where `m` is the bit size associated with the curve, i.e. the bit size
682 * of the order of the curve's coordinate field. This byte string is
683 * in little-endian order for Montgomery curves (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +0100684 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
685 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
686 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
Steven Cooreman6f5cc712020-06-11 16:40:41 +0200687 * For Weierstrass curves, this is the content of the `privateKey` field of
688 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
689 * the format is defined by RFC 7748, and output is masked according to §5.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200690 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200691 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000692 * format is the representation of the private key `x` as a big-endian byte
693 * string. The length of the byte string is the private key size in bytes
694 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200695 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
696 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100697 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200698 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
699 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200700 * \param key Identifier of the key to export. It must allow the
Ronald Cron96783552020-10-19 12:06:30 +0200701 * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
Ronald Croncf56a0a2020-08-04 09:51:30 +0200702 * key.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200703 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200704 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200705 * \param[out] data_length On success, the number of bytes
706 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100707 *
Gilles Peskine28538492018-07-11 17:34:00 +0200708 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100709 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200710 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200711 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100712 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200713 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
714 * The size of the \p data buffer is too small. You can determine a
715 * sufficient buffer size by calling
716 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
717 * where \c type is the key type
718 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200719 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
720 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200721 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100722 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100723 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300724 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300725 * The library has not been previously initialized by psa_crypto_init().
726 * It is implementation-dependent whether a failure to initialize
727 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100728 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200729psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100730 uint8_t *data,
731 size_t data_size,
732 size_t *data_length);
733
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100734/**
735 * \brief Export a public key or the public part of a key pair in binary format.
736 *
737 * The output of this function can be passed to psa_import_key() to
738 * create an object that is equivalent to the public key.
739 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000740 * This specification supports a single format for each key type.
741 * Implementations may support other formats as long as the standard
742 * format is supported. Implementations that support other formats
743 * should ensure that the formats are clearly unambiguous so as to
744 * minimize the risk that an invalid input is accidentally interpreted
745 * according to a different format.
746 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000747 * For standard key types, the output format is as follows:
748 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
749 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
750 * ```
751 * RSAPublicKey ::= SEQUENCE {
752 * modulus INTEGER, -- n
753 * publicExponent INTEGER } -- e
754 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000755 * - For elliptic curve public keys (key types for which
756 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
757 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
758 * Let `m` be the bit size associated with the curve, i.e. the bit size of
759 * `q` for a curve over `F_q`. The representation consists of:
760 * - The byte 0x04;
761 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
762 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200763 * - For Diffie-Hellman key exchange public keys (key types for which
764 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000765 * the format is the representation of the public key `y = g^x mod p` as a
766 * big-endian byte string. The length of the byte string is the length of the
767 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100768 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200769 * Exporting a public key object or the public part of a key pair is
770 * always permitted, regardless of the key's usage flags.
771 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200772 * \param key Identifier of the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200773 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200774 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200775 * \param[out] data_length On success, the number of bytes
776 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100777 *
Gilles Peskine28538492018-07-11 17:34:00 +0200778 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100779 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200780 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200781 * The key is neither a public key nor a key pair.
782 * \retval #PSA_ERROR_NOT_SUPPORTED
783 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
784 * The size of the \p data buffer is too small. You can determine a
785 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200786 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200787 * where \c type is the key type
788 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200789 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
790 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200791 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100792 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100793 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300794 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300795 * The library has not been previously initialized by psa_crypto_init().
796 * It is implementation-dependent whether a failure to initialize
797 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100798 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200799psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100800 uint8_t *data,
801 size_t data_size,
802 size_t *data_length);
803
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100804
Gilles Peskine20035e32018-02-03 22:44:14 +0100805
806/**@}*/
807
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100808/** \defgroup hash Message digests
809 * @{
810 */
811
Gilles Peskine69647a42019-01-14 20:18:12 +0100812/** Calculate the hash (digest) of a message.
813 *
814 * \note To verify the hash of a message against an
815 * expected value, use psa_hash_compare() instead.
816 *
817 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
818 * such that #PSA_ALG_IS_HASH(\p alg) is true).
819 * \param[in] input Buffer containing the message to hash.
820 * \param input_length Size of the \p input buffer in bytes.
821 * \param[out] hash Buffer where the hash is to be written.
822 * \param hash_size Size of the \p hash buffer in bytes.
823 * \param[out] hash_length On success, the number of bytes
824 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100825 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100826 *
827 * \retval #PSA_SUCCESS
828 * Success.
829 * \retval #PSA_ERROR_NOT_SUPPORTED
830 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100831 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100832 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
833 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100834 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
835 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
836 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200837 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100838 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100839 * \retval #PSA_ERROR_BAD_STATE
840 * The library has not been previously initialized by psa_crypto_init().
841 * It is implementation-dependent whether a failure to initialize
842 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100843 */
844psa_status_t psa_hash_compute(psa_algorithm_t alg,
845 const uint8_t *input,
846 size_t input_length,
847 uint8_t *hash,
848 size_t hash_size,
849 size_t *hash_length);
850
851/** Calculate the hash (digest) of a message and compare it with a
852 * reference value.
853 *
854 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
855 * such that #PSA_ALG_IS_HASH(\p alg) is true).
856 * \param[in] input Buffer containing the message to hash.
857 * \param input_length Size of the \p input buffer in bytes.
858 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100859 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100860 *
861 * \retval #PSA_SUCCESS
862 * The expected hash is identical to the actual hash of the input.
863 * \retval #PSA_ERROR_INVALID_SIGNATURE
864 * The hash of the message was calculated successfully, but it
865 * differs from the expected hash.
866 * \retval #PSA_ERROR_NOT_SUPPORTED
867 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100868 * \retval #PSA_ERROR_INVALID_ARGUMENT
869 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100870 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
871 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
872 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200873 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100874 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100875 * \retval #PSA_ERROR_BAD_STATE
876 * The library has not been previously initialized by psa_crypto_init().
877 * It is implementation-dependent whether a failure to initialize
878 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100879 */
880psa_status_t psa_hash_compare(psa_algorithm_t alg,
881 const uint8_t *input,
882 size_t input_length,
883 const uint8_t *hash,
Gilles Peskinefa710f52019-12-02 14:31:48 +0100884 size_t hash_length);
Gilles Peskine69647a42019-01-14 20:18:12 +0100885
Gilles Peskine308b91d2018-02-08 09:47:44 +0100886/** The type of the state data structure for multipart hash operations.
887 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000888 * Before calling any function on a hash operation object, the application must
889 * initialize it by any of the following means:
890 * - Set the structure to all-bits-zero, for example:
891 * \code
892 * psa_hash_operation_t operation;
893 * memset(&operation, 0, sizeof(operation));
894 * \endcode
895 * - Initialize the structure to logical zero values, for example:
896 * \code
897 * psa_hash_operation_t operation = {0};
898 * \endcode
899 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
900 * for example:
901 * \code
902 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
903 * \endcode
904 * - Assign the result of the function psa_hash_operation_init()
905 * to the structure, for example:
906 * \code
907 * psa_hash_operation_t operation;
908 * operation = psa_hash_operation_init();
909 * \endcode
910 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100911 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100912 * make any assumptions about the content of this structure except
913 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100914typedef struct psa_hash_operation_s psa_hash_operation_t;
915
Jaeden Amero6a25b412019-01-04 11:47:44 +0000916/** \def PSA_HASH_OPERATION_INIT
917 *
918 * This macro returns a suitable initializer for a hash operation object
919 * of type #psa_hash_operation_t.
920 */
921#ifdef __DOXYGEN_ONLY__
922/* This is an example definition for documentation purposes.
923 * Implementations should define a suitable value in `crypto_struct.h`.
924 */
925#define PSA_HASH_OPERATION_INIT {0}
926#endif
927
928/** Return an initial value for a hash operation object.
929 */
930static psa_hash_operation_t psa_hash_operation_init(void);
931
Gilles Peskinef45adda2019-01-14 18:29:18 +0100932/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100933 *
934 * The sequence of operations to calculate a hash (message digest)
935 * is as follows:
936 * -# Allocate an operation object which will be passed to all the functions
937 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000938 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100939 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200940 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100941 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100942 * of the message each time. The hash that is calculated is the hash
943 * of the concatenation of these messages in order.
944 * -# To calculate the hash, call psa_hash_finish().
945 * To compare the hash with an expected value, call psa_hash_verify().
946 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100947 * If an error occurs at any step after a call to psa_hash_setup(), the
948 * operation will need to be reset by a call to psa_hash_abort(). The
949 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000950 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100951 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200952 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100953 * eventually terminate the operation. The following events terminate an
954 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100955 * - A successful call to psa_hash_finish() or psa_hash_verify().
956 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100957 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000958 * \param[in,out] operation The operation object to set up. It must have
959 * been initialized as per the documentation for
960 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200961 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
962 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100963 *
Gilles Peskine28538492018-07-11 17:34:00 +0200964 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100965 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200966 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100967 * \p alg is not a supported hash algorithm.
968 * \retval #PSA_ERROR_INVALID_ARGUMENT
969 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100970 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100971 * The operation state is not valid (it must be inactive).
Gilles Peskine28538492018-07-11 17:34:00 +0200972 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
973 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
974 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200975 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100976 * \retval #PSA_ERROR_BAD_STATE
977 * The library has not been previously initialized by psa_crypto_init().
978 * It is implementation-dependent whether a failure to initialize
979 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100980 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200981psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100982 psa_algorithm_t alg);
983
Gilles Peskine308b91d2018-02-08 09:47:44 +0100984/** Add a message fragment to a multipart hash operation.
985 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200986 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100987 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100988 * If this function returns an error status, the operation enters an error
989 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100990 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200991 * \param[in,out] operation Active hash operation.
992 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200993 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100994 *
Gilles Peskine28538492018-07-11 17:34:00 +0200995 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100996 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200997 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100998 * The operation state is not valid (it muct be active).
Gilles Peskine28538492018-07-11 17:34:00 +0200999 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1000 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1001 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001002 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001003 * \retval #PSA_ERROR_BAD_STATE
1004 * The library has not been previously initialized by psa_crypto_init().
1005 * It is implementation-dependent whether a failure to initialize
1006 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001007 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001008psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1009 const uint8_t *input,
1010 size_t input_length);
1011
Gilles Peskine308b91d2018-02-08 09:47:44 +01001012/** Finish the calculation of the hash of a message.
1013 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001014 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001015 * This function calculates the hash of the message formed by concatenating
1016 * the inputs passed to preceding calls to psa_hash_update().
1017 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001018 * When this function returns successfuly, the operation becomes inactive.
1019 * If this function returns an error status, the operation enters an error
1020 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001021 *
1022 * \warning Applications should not call this function if they expect
1023 * a specific value for the hash. Call psa_hash_verify() instead.
1024 * Beware that comparing integrity or authenticity data such as
1025 * hash values with a function such as \c memcmp is risky
1026 * because the time taken by the comparison may leak information
1027 * about the hashed data which could allow an attacker to guess
1028 * a valid hash and thereby bypass security controls.
1029 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001030 * \param[in,out] operation Active hash operation.
1031 * \param[out] hash Buffer where the hash is to be written.
1032 * \param hash_size Size of the \p hash buffer in bytes.
1033 * \param[out] hash_length On success, the number of bytes
1034 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001035 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001036 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001037 *
Gilles Peskine28538492018-07-11 17:34:00 +02001038 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001039 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001040 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001041 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001042 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001043 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001044 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001045 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001046 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1047 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1048 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001049 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001050 * \retval #PSA_ERROR_BAD_STATE
1051 * The library has not been previously initialized by psa_crypto_init().
1052 * It is implementation-dependent whether a failure to initialize
1053 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001054 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001055psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1056 uint8_t *hash,
1057 size_t hash_size,
1058 size_t *hash_length);
1059
Gilles Peskine308b91d2018-02-08 09:47:44 +01001060/** Finish the calculation of the hash of a message and compare it with
1061 * an expected value.
1062 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001063 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001064 * This function calculates the hash of the message formed by concatenating
1065 * the inputs passed to preceding calls to psa_hash_update(). It then
1066 * compares the calculated hash with the expected hash passed as a
1067 * parameter to this function.
1068 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001069 * When this function returns successfuly, the operation becomes inactive.
1070 * If this function returns an error status, the operation enters an error
1071 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001072 *
Gilles Peskine19067982018-03-20 17:54:53 +01001073 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074 * comparison between the actual hash and the expected hash is performed
1075 * in constant time.
1076 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001077 * \param[in,out] operation Active hash operation.
1078 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001079 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001080 *
Gilles Peskine28538492018-07-11 17:34:00 +02001081 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001082 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001083 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001084 * The hash of the message was calculated successfully, but it
1085 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001086 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001087 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001088 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1089 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1090 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001091 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001092 * \retval #PSA_ERROR_BAD_STATE
1093 * The library has not been previously initialized by psa_crypto_init().
1094 * It is implementation-dependent whether a failure to initialize
1095 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001096 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001097psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1098 const uint8_t *hash,
1099 size_t hash_length);
1100
Gilles Peskine308b91d2018-02-08 09:47:44 +01001101/** Abort a hash operation.
1102 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001103 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001104 * \p operation structure itself. Once aborted, the operation object
1105 * can be reused for another operation by calling
1106 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001107 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001108 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001109 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001110 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001111 * In particular, calling psa_hash_abort() after the operation has been
1112 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1113 * psa_hash_verify() is safe and has no effect.
1114 *
1115 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001116 *
Gilles Peskine28538492018-07-11 17:34:00 +02001117 * \retval #PSA_SUCCESS
Gilles Peskine28538492018-07-11 17:34:00 +02001118 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1119 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001120 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001121 * \retval #PSA_ERROR_BAD_STATE
1122 * The library has not been previously initialized by psa_crypto_init().
1123 * It is implementation-dependent whether a failure to initialize
1124 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001125 */
1126psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001127
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001128/** Clone a hash operation.
1129 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001130 * This function copies the state of an ongoing hash operation to
1131 * a new operation object. In other words, this function is equivalent
1132 * to calling psa_hash_setup() on \p target_operation with the same
1133 * algorithm that \p source_operation was set up for, then
1134 * psa_hash_update() on \p target_operation with the same input that
1135 * that was passed to \p source_operation. After this function returns, the
1136 * two objects are independent, i.e. subsequent calls involving one of
1137 * the objects do not affect the other object.
1138 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001139 * \param[in] source_operation The active hash operation to clone.
1140 * \param[in,out] target_operation The operation object to set up.
1141 * It must be initialized but not active.
1142 *
1143 * \retval #PSA_SUCCESS
1144 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001145 * The \p source_operation state is not valid (it must be active).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001146 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001147 * The \p target_operation state is not valid (it must be inactive).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001148 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1149 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001150 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001151 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001152 * \retval #PSA_ERROR_BAD_STATE
1153 * The library has not been previously initialized by psa_crypto_init().
1154 * It is implementation-dependent whether a failure to initialize
1155 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001156 */
1157psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1158 psa_hash_operation_t *target_operation);
1159
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001160/**@}*/
1161
Gilles Peskine8c9def32018-02-08 10:02:12 +01001162/** \defgroup MAC Message authentication codes
1163 * @{
1164 */
1165
Gilles Peskine69647a42019-01-14 20:18:12 +01001166/** Calculate the MAC (message authentication code) of a message.
1167 *
1168 * \note To verify the MAC of a message against an
1169 * expected value, use psa_mac_verify() instead.
1170 * Beware that comparing integrity or authenticity data such as
1171 * MAC values with a function such as \c memcmp is risky
1172 * because the time taken by the comparison may leak information
1173 * about the MAC value which could allow an attacker to guess
1174 * a valid MAC and thereby bypass security controls.
1175 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001176 * \param key Identifier of the key to use for the operation. It
1177 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001178 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001179 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001180 * \param[in] input Buffer containing the input message.
1181 * \param input_length Size of the \p input buffer in bytes.
1182 * \param[out] mac Buffer where the MAC value is to be written.
1183 * \param mac_size Size of the \p mac buffer in bytes.
1184 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001185 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001186 *
1187 * \retval #PSA_SUCCESS
1188 * Success.
1189 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001190 * \retval #PSA_ERROR_NOT_PERMITTED
1191 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001192 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001193 * \retval #PSA_ERROR_NOT_SUPPORTED
1194 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001195 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1196 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001197 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1198 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1199 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001200 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001201 * \retval #PSA_ERROR_STORAGE_FAILURE
1202 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001203 * \retval #PSA_ERROR_BAD_STATE
1204 * The library has not been previously initialized by psa_crypto_init().
1205 * It is implementation-dependent whether a failure to initialize
1206 * results in this error code.
1207 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001208psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001209 psa_algorithm_t alg,
1210 const uint8_t *input,
1211 size_t input_length,
1212 uint8_t *mac,
1213 size_t mac_size,
1214 size_t *mac_length);
1215
1216/** Calculate the MAC of a message and compare it with a reference value.
1217 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001218 * \param key Identifier of the key to use for the operation. It
1219 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001220 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001221 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001222 * \param[in] input Buffer containing the input message.
1223 * \param input_length Size of the \p input buffer in bytes.
1224 * \param[out] mac Buffer containing the expected MAC value.
1225 * \param mac_length Size of the \p mac buffer in bytes.
1226 *
1227 * \retval #PSA_SUCCESS
1228 * The expected MAC is identical to the actual MAC of the input.
1229 * \retval #PSA_ERROR_INVALID_SIGNATURE
1230 * The MAC of the message was calculated successfully, but it
1231 * differs from the expected value.
1232 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001233 * \retval #PSA_ERROR_NOT_PERMITTED
1234 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001235 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001236 * \retval #PSA_ERROR_NOT_SUPPORTED
1237 * \p alg is not supported or is not a MAC algorithm.
1238 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1239 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1240 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001241 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001242 * \retval #PSA_ERROR_STORAGE_FAILURE
1243 * The key could not be retrieved from storage.
1244 * \retval #PSA_ERROR_BAD_STATE
1245 * The library has not been previously initialized by psa_crypto_init().
1246 * It is implementation-dependent whether a failure to initialize
1247 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001248 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001249psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
Gilles Peskinea05602d2019-01-17 15:25:52 +01001250 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001251 const uint8_t *input,
1252 size_t input_length,
1253 const uint8_t *mac,
Gilles Peskine13faa2d2020-01-30 16:32:21 +01001254 size_t mac_length);
Gilles Peskine69647a42019-01-14 20:18:12 +01001255
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001256/** The type of the state data structure for multipart MAC operations.
1257 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001258 * Before calling any function on a MAC operation object, the application must
1259 * initialize it by any of the following means:
1260 * - Set the structure to all-bits-zero, for example:
1261 * \code
1262 * psa_mac_operation_t operation;
1263 * memset(&operation, 0, sizeof(operation));
1264 * \endcode
1265 * - Initialize the structure to logical zero values, for example:
1266 * \code
1267 * psa_mac_operation_t operation = {0};
1268 * \endcode
1269 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1270 * for example:
1271 * \code
1272 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1273 * \endcode
1274 * - Assign the result of the function psa_mac_operation_init()
1275 * to the structure, for example:
1276 * \code
1277 * psa_mac_operation_t operation;
1278 * operation = psa_mac_operation_init();
1279 * \endcode
1280 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001281 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001282 * make any assumptions about the content of this structure except
1283 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001284typedef struct psa_mac_operation_s psa_mac_operation_t;
1285
Jaeden Amero769ce272019-01-04 11:48:03 +00001286/** \def PSA_MAC_OPERATION_INIT
1287 *
1288 * This macro returns a suitable initializer for a MAC operation object of type
1289 * #psa_mac_operation_t.
1290 */
1291#ifdef __DOXYGEN_ONLY__
1292/* This is an example definition for documentation purposes.
1293 * Implementations should define a suitable value in `crypto_struct.h`.
1294 */
1295#define PSA_MAC_OPERATION_INIT {0}
1296#endif
1297
1298/** Return an initial value for a MAC operation object.
1299 */
1300static psa_mac_operation_t psa_mac_operation_init(void);
1301
Gilles Peskinef45adda2019-01-14 18:29:18 +01001302/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001303 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001304 * This function sets up the calculation of the MAC
1305 * (message authentication code) of a byte string.
1306 * To verify the MAC of a message against an
1307 * expected value, use psa_mac_verify_setup() instead.
1308 *
1309 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001310 * -# Allocate an operation object which will be passed to all the functions
1311 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001312 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001313 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001314 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001315 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1316 * of the message each time. The MAC that is calculated is the MAC
1317 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001318 * -# At the end of the message, call psa_mac_sign_finish() to finish
1319 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001320 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001321 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1322 * operation will need to be reset by a call to psa_mac_abort(). The
1323 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001324 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001325 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001326 * After a successful call to psa_mac_sign_setup(), the application must
1327 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001328 * - A successful call to psa_mac_sign_finish().
1329 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001330 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001331 * \param[in,out] operation The operation object to set up. It must have
1332 * been initialized as per the documentation for
1333 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001334 * \param key Identifier of the key to use for the operation. It
1335 * must remain valid until the operation terminates.
1336 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001337 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001338 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001339 *
Gilles Peskine28538492018-07-11 17:34:00 +02001340 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001341 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001342 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001343 * \retval #PSA_ERROR_NOT_PERMITTED
1344 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001345 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001346 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001347 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001348 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1349 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1350 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001351 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001352 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001353 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001354 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001355 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001356 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001357 * The library has not been previously initialized by psa_crypto_init().
1358 * It is implementation-dependent whether a failure to initialize
1359 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001360 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001361psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001362 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001363 psa_algorithm_t alg);
1364
Gilles Peskinef45adda2019-01-14 18:29:18 +01001365/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001366 *
1367 * This function sets up the verification of the MAC
1368 * (message authentication code) of a byte string against an expected value.
1369 *
1370 * The sequence of operations to verify a MAC is as follows:
1371 * -# Allocate an operation object which will be passed to all the functions
1372 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001373 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001374 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001375 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001376 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1377 * of the message each time. The MAC that is calculated is the MAC
1378 * of the concatenation of these messages in order.
1379 * -# At the end of the message, call psa_mac_verify_finish() to finish
1380 * calculating the actual MAC of the message and verify it against
1381 * the expected value.
1382 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001383 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1384 * operation will need to be reset by a call to psa_mac_abort(). The
1385 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001386 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001387 *
1388 * After a successful call to psa_mac_verify_setup(), the application must
1389 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001390 * - A successful call to psa_mac_verify_finish().
1391 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001392 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001393 * \param[in,out] operation The operation object to set up. It must have
1394 * been initialized as per the documentation for
1395 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001396 * \param key Identifier of the key to use for the operation. It
1397 * must remain valid until the operation terminates.
1398 * It must allow the usage
1399 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001400 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1401 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001402 *
Gilles Peskine28538492018-07-11 17:34:00 +02001403 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001404 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001405 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001406 * \retval #PSA_ERROR_NOT_PERMITTED
1407 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001408 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001409 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001410 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001411 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1412 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1413 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001414 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001415 * \retval #PSA_ERROR_STORAGE_FAILURE
1416 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001417 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001418 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001419 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001420 * The library has not been previously initialized by psa_crypto_init().
1421 * It is implementation-dependent whether a failure to initialize
1422 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001423 */
1424psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001425 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001426 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001427
Gilles Peskinedcd14942018-07-12 00:30:52 +02001428/** Add a message fragment to a multipart MAC operation.
1429 *
1430 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1431 * before calling this function.
1432 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001433 * If this function returns an error status, the operation enters an error
1434 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001435 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001436 * \param[in,out] operation Active MAC operation.
1437 * \param[in] input Buffer containing the message fragment to add to
1438 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001439 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001440 *
1441 * \retval #PSA_SUCCESS
1442 * Success.
1443 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001444 * The operation state is not valid (it must be active).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001445 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1446 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1447 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001448 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001449 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001450 * \retval #PSA_ERROR_BAD_STATE
1451 * The library has not been previously initialized by psa_crypto_init().
1452 * It is implementation-dependent whether a failure to initialize
1453 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001454 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001455psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1456 const uint8_t *input,
1457 size_t input_length);
1458
Gilles Peskinedcd14942018-07-12 00:30:52 +02001459/** Finish the calculation of the MAC of a message.
1460 *
1461 * The application must call psa_mac_sign_setup() before calling this function.
1462 * This function calculates the MAC of the message formed by concatenating
1463 * the inputs passed to preceding calls to psa_mac_update().
1464 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001465 * When this function returns successfuly, the operation becomes inactive.
1466 * If this function returns an error status, the operation enters an error
1467 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001468 *
1469 * \warning Applications should not call this function if they expect
1470 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1471 * Beware that comparing integrity or authenticity data such as
1472 * MAC values with a function such as \c memcmp is risky
1473 * because the time taken by the comparison may leak information
1474 * about the MAC value which could allow an attacker to guess
1475 * a valid MAC and thereby bypass security controls.
1476 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001477 * \param[in,out] operation Active MAC operation.
1478 * \param[out] mac Buffer where the MAC value is to be written.
1479 * \param mac_size Size of the \p mac buffer in bytes.
1480 * \param[out] mac_length On success, the number of bytes
1481 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001482 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001483 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001484 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001485 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001486 *
1487 * \retval #PSA_SUCCESS
1488 * Success.
1489 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001490 * The operation state is not valid (it must be an active mac sign
1491 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001492 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001493 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001494 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1495 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1496 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1497 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001498 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001499 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001500 * \retval #PSA_ERROR_BAD_STATE
1501 * The library has not been previously initialized by psa_crypto_init().
1502 * It is implementation-dependent whether a failure to initialize
1503 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001504 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001505psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1506 uint8_t *mac,
1507 size_t mac_size,
1508 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001509
Gilles Peskinedcd14942018-07-12 00:30:52 +02001510/** Finish the calculation of the MAC of a message and compare it with
1511 * an expected value.
1512 *
1513 * The application must call psa_mac_verify_setup() before calling this function.
1514 * This function calculates the MAC of the message formed by concatenating
1515 * the inputs passed to preceding calls to psa_mac_update(). It then
1516 * compares the calculated MAC with the expected MAC passed as a
1517 * parameter to this function.
1518 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001519 * When this function returns successfuly, the operation becomes inactive.
1520 * If this function returns an error status, the operation enters an error
1521 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001522 *
1523 * \note Implementations shall make the best effort to ensure that the
1524 * comparison between the actual MAC and the expected MAC is performed
1525 * in constant time.
1526 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001527 * \param[in,out] operation Active MAC operation.
1528 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001529 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001530 *
1531 * \retval #PSA_SUCCESS
1532 * The expected MAC is identical to the actual MAC of the message.
1533 * \retval #PSA_ERROR_INVALID_SIGNATURE
1534 * The MAC of the message was calculated successfully, but it
1535 * differs from the expected MAC.
1536 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001537 * The operation state is not valid (it must be an active mac verify
1538 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001539 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1540 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1541 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001542 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001543 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001544 * \retval #PSA_ERROR_BAD_STATE
1545 * The library has not been previously initialized by psa_crypto_init().
1546 * It is implementation-dependent whether a failure to initialize
1547 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001548 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001549psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1550 const uint8_t *mac,
1551 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001552
Gilles Peskinedcd14942018-07-12 00:30:52 +02001553/** Abort a MAC operation.
1554 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001555 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001556 * \p operation structure itself. Once aborted, the operation object
1557 * can be reused for another operation by calling
1558 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001559 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001560 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001561 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001562 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001563 * In particular, calling psa_mac_abort() after the operation has been
1564 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1565 * psa_mac_verify_finish() is safe and has no effect.
1566 *
1567 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001568 *
1569 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02001570 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1571 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001572 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001573 * \retval #PSA_ERROR_BAD_STATE
1574 * The library has not been previously initialized by psa_crypto_init().
1575 * It is implementation-dependent whether a failure to initialize
1576 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001577 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001578psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1579
1580/**@}*/
1581
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001582/** \defgroup cipher Symmetric ciphers
1583 * @{
1584 */
1585
Gilles Peskine69647a42019-01-14 20:18:12 +01001586/** Encrypt a message using a symmetric cipher.
1587 *
1588 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001589 * vector). Use the multipart operation interface with a
1590 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001591 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001592 * \param key Identifier of the key to use for the operation.
Ronald Cron96783552020-10-19 12:06:30 +02001593 * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001594 * \param alg The cipher algorithm to compute
1595 * (\c PSA_ALG_XXX value such that
1596 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1597 * \param[in] input Buffer containing the message to encrypt.
1598 * \param input_length Size of the \p input buffer in bytes.
1599 * \param[out] output Buffer where the output is to be written.
1600 * The output contains the IV followed by
1601 * the ciphertext proper.
1602 * \param output_size Size of the \p output buffer in bytes.
1603 * \param[out] output_length On success, the number of bytes
1604 * that make up the output.
1605 *
1606 * \retval #PSA_SUCCESS
1607 * Success.
1608 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001609 * \retval #PSA_ERROR_NOT_PERMITTED
1610 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001611 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001612 * \retval #PSA_ERROR_NOT_SUPPORTED
1613 * \p alg is not supported or is not a cipher algorithm.
1614 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1615 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1616 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1617 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001618 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001619 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001620 * \retval #PSA_ERROR_BAD_STATE
1621 * The library has not been previously initialized by psa_crypto_init().
1622 * It is implementation-dependent whether a failure to initialize
1623 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001624 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001625psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001626 psa_algorithm_t alg,
1627 const uint8_t *input,
1628 size_t input_length,
1629 uint8_t *output,
1630 size_t output_size,
1631 size_t *output_length);
1632
1633/** Decrypt a message using a symmetric cipher.
1634 *
1635 * This function decrypts a message encrypted with a symmetric cipher.
1636 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001637 * \param key Identifier of the key to use for the operation.
Gilles Peskine69647a42019-01-14 20:18:12 +01001638 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001639 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001640 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001641 * \param alg The cipher algorithm to compute
1642 * (\c PSA_ALG_XXX value such that
1643 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1644 * \param[in] input Buffer containing the message to decrypt.
1645 * This consists of the IV followed by the
1646 * ciphertext proper.
1647 * \param input_length Size of the \p input buffer in bytes.
1648 * \param[out] output Buffer where the plaintext is to be written.
1649 * \param output_size Size of the \p output buffer in bytes.
1650 * \param[out] output_length On success, the number of bytes
1651 * that make up the output.
1652 *
1653 * \retval #PSA_SUCCESS
1654 * Success.
1655 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001656 * \retval #PSA_ERROR_NOT_PERMITTED
1657 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001658 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001659 * \retval #PSA_ERROR_NOT_SUPPORTED
1660 * \p alg is not supported or is not a cipher algorithm.
1661 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1662 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1663 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1664 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001665 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001666 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001667 * \retval #PSA_ERROR_BAD_STATE
1668 * The library has not been previously initialized by psa_crypto_init().
1669 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001670 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001671 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001672psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001673 psa_algorithm_t alg,
1674 const uint8_t *input,
1675 size_t input_length,
1676 uint8_t *output,
1677 size_t output_size,
1678 size_t *output_length);
1679
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001680/** The type of the state data structure for multipart cipher operations.
1681 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001682 * Before calling any function on a cipher operation object, the application
1683 * must initialize it by any of the following means:
1684 * - Set the structure to all-bits-zero, for example:
1685 * \code
1686 * psa_cipher_operation_t operation;
1687 * memset(&operation, 0, sizeof(operation));
1688 * \endcode
1689 * - Initialize the structure to logical zero values, for example:
1690 * \code
1691 * psa_cipher_operation_t operation = {0};
1692 * \endcode
1693 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1694 * for example:
1695 * \code
1696 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1697 * \endcode
1698 * - Assign the result of the function psa_cipher_operation_init()
1699 * to the structure, for example:
1700 * \code
1701 * psa_cipher_operation_t operation;
1702 * operation = psa_cipher_operation_init();
1703 * \endcode
1704 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001705 * This is an implementation-defined \c struct. Applications should not
1706 * make any assumptions about the content of this structure except
1707 * as directed by the documentation of a specific implementation. */
1708typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1709
Jaeden Amero5bae2272019-01-04 11:48:27 +00001710/** \def PSA_CIPHER_OPERATION_INIT
1711 *
1712 * This macro returns a suitable initializer for a cipher operation object of
1713 * type #psa_cipher_operation_t.
1714 */
1715#ifdef __DOXYGEN_ONLY__
1716/* This is an example definition for documentation purposes.
1717 * Implementations should define a suitable value in `crypto_struct.h`.
1718 */
1719#define PSA_CIPHER_OPERATION_INIT {0}
1720#endif
1721
1722/** Return an initial value for a cipher operation object.
1723 */
1724static psa_cipher_operation_t psa_cipher_operation_init(void);
1725
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001726/** Set the key for a multipart symmetric encryption operation.
1727 *
1728 * The sequence of operations to encrypt a message with a symmetric cipher
1729 * is as follows:
1730 * -# Allocate an operation object which will be passed to all the functions
1731 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001732 * -# Initialize the operation object with one of the methods described in the
1733 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001734 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001735 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001736 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001737 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001738 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001739 * requires a specific IV value.
1740 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1741 * of the message each time.
1742 * -# Call psa_cipher_finish().
1743 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001744 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1745 * the operation will need to be reset by a call to psa_cipher_abort(). The
1746 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001747 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001748 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001749 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001750 * eventually terminate the operation. The following events terminate an
1751 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001752 * - A successful call to psa_cipher_finish().
1753 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001754 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001755 * \param[in,out] operation The operation object to set up. It must have
1756 * been initialized as per the documentation for
1757 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001758 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001759 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001760 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001761 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001762 * \param alg The cipher algorithm to compute
1763 * (\c PSA_ALG_XXX value such that
1764 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001765 *
Gilles Peskine28538492018-07-11 17:34:00 +02001766 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001767 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001768 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001769 * \retval #PSA_ERROR_NOT_PERMITTED
1770 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001771 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001772 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001773 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001774 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1775 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1776 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001777 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001778 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001779 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001780 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001781 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001782 * The library has not been previously initialized by psa_crypto_init().
1783 * It is implementation-dependent whether a failure to initialize
1784 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001785 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001786psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001787 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001788 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001789
1790/** Set the key for a multipart symmetric decryption operation.
1791 *
1792 * The sequence of operations to decrypt a message with a symmetric cipher
1793 * is as follows:
1794 * -# Allocate an operation object which will be passed to all the functions
1795 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001796 * -# Initialize the operation object with one of the methods described in the
1797 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001798 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001799 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001800 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001801 * decryption. If the IV is prepended to the ciphertext, you can call
1802 * psa_cipher_update() on a buffer containing the IV followed by the
1803 * beginning of the message.
1804 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1805 * of the message each time.
1806 * -# Call psa_cipher_finish().
1807 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001808 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1809 * the operation will need to be reset by a call to psa_cipher_abort(). The
1810 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001811 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001812 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001813 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001814 * eventually terminate the operation. The following events terminate an
1815 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001816 * - A successful call to psa_cipher_finish().
1817 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001818 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001819 * \param[in,out] operation The operation object to set up. It must have
1820 * been initialized as per the documentation for
1821 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001822 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001823 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001824 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001825 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001826 * \param alg The cipher algorithm to compute
1827 * (\c PSA_ALG_XXX value such that
1828 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001829 *
Gilles Peskine28538492018-07-11 17:34:00 +02001830 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001831 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001832 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001833 * \retval #PSA_ERROR_NOT_PERMITTED
1834 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001835 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001836 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001837 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001838 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1839 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1840 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001841 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001842 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001843 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001844 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001845 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001846 * The library has not been previously initialized by psa_crypto_init().
1847 * It is implementation-dependent whether a failure to initialize
1848 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001849 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001850psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001851 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001852 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001853
Gilles Peskinedcd14942018-07-12 00:30:52 +02001854/** Generate an IV for a symmetric encryption operation.
1855 *
1856 * This function generates a random IV (initialization vector), nonce
1857 * or initial counter value for the encryption operation as appropriate
1858 * for the chosen algorithm, key type and key size.
1859 *
1860 * The application must call psa_cipher_encrypt_setup() before
1861 * calling this function.
1862 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001863 * If this function returns an error status, the operation enters an error
1864 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001865 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001866 * \param[in,out] operation Active cipher operation.
1867 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001868 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001869 * \param[out] iv_length On success, the number of bytes of the
1870 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001871 *
1872 * \retval #PSA_SUCCESS
1873 * Success.
1874 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001875 * The operation state is not valid (it must be active, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001876 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001877 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001878 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1879 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1880 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001881 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001882 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001883 * \retval #PSA_ERROR_BAD_STATE
1884 * The library has not been previously initialized by psa_crypto_init().
1885 * It is implementation-dependent whether a failure to initialize
1886 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001887 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001888psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001889 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001890 size_t iv_size,
1891 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001892
Gilles Peskinedcd14942018-07-12 00:30:52 +02001893/** Set the IV for a symmetric encryption or decryption operation.
1894 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001895 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001896 * or initial counter value for the encryption or decryption operation.
1897 *
1898 * The application must call psa_cipher_encrypt_setup() before
1899 * calling this function.
1900 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001901 * If this function returns an error status, the operation enters an error
1902 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001903 *
1904 * \note When encrypting, applications should use psa_cipher_generate_iv()
1905 * instead of this function, unless implementing a protocol that requires
1906 * a non-random IV.
1907 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001908 * \param[in,out] operation Active cipher operation.
1909 * \param[in] iv Buffer containing the IV to use.
1910 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001911 *
1912 * \retval #PSA_SUCCESS
1913 * Success.
1914 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001915 * The operation state is not valid (it must be an active cipher
1916 * encrypt operation, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001917 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001918 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001919 * or the chosen algorithm does not use an IV.
1920 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1921 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1922 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001923 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001924 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001925 * \retval #PSA_ERROR_BAD_STATE
1926 * The library has not been previously initialized by psa_crypto_init().
1927 * It is implementation-dependent whether a failure to initialize
1928 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001929 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001930psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001931 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001932 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001933
Gilles Peskinedcd14942018-07-12 00:30:52 +02001934/** Encrypt or decrypt a message fragment in an active cipher operation.
1935 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001936 * Before calling this function, you must:
1937 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1938 * The choice of setup function determines whether this function
1939 * encrypts or decrypts its input.
1940 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1941 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001942 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001943 * If this function returns an error status, the operation enters an error
1944 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001945 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001946 * \param[in,out] operation Active cipher operation.
1947 * \param[in] input Buffer containing the message fragment to
1948 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001949 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001950 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001951 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001952 * \param[out] output_length On success, the number of bytes
1953 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001954 *
1955 * \retval #PSA_SUCCESS
1956 * Success.
1957 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001958 * The operation state is not valid (it must be active, with an IV set
1959 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001960 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1961 * The size of the \p output buffer is too small.
1962 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1963 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1964 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001965 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001966 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001967 * \retval #PSA_ERROR_BAD_STATE
1968 * The library has not been previously initialized by psa_crypto_init().
1969 * It is implementation-dependent whether a failure to initialize
1970 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001971 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001972psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1973 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001974 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001975 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001976 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001977 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001978
Gilles Peskinedcd14942018-07-12 00:30:52 +02001979/** Finish encrypting or decrypting a message in a cipher operation.
1980 *
1981 * The application must call psa_cipher_encrypt_setup() or
1982 * psa_cipher_decrypt_setup() before calling this function. The choice
1983 * of setup function determines whether this function encrypts or
1984 * decrypts its input.
1985 *
1986 * This function finishes the encryption or decryption of the message
1987 * formed by concatenating the inputs passed to preceding calls to
1988 * psa_cipher_update().
1989 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001990 * When this function returns successfuly, the operation becomes inactive.
1991 * If this function returns an error status, the operation enters an error
1992 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001993 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001994 * \param[in,out] operation Active cipher operation.
1995 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001996 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001997 * \param[out] output_length On success, the number of bytes
1998 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001999 *
2000 * \retval #PSA_SUCCESS
2001 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02002002 * \retval #PSA_ERROR_INVALID_ARGUMENT
2003 * The total input size passed to this operation is not valid for
2004 * this particular algorithm. For example, the algorithm is a based
2005 * on block cipher and requires a whole number of blocks, but the
2006 * total input size is not a multiple of the block size.
2007 * \retval #PSA_ERROR_INVALID_PADDING
2008 * This is a decryption operation for an algorithm that includes
2009 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002010 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002011 * The operation state is not valid (it must be active, with an IV set
2012 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02002013 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2014 * The size of the \p output buffer is too small.
2015 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2016 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2017 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002018 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002019 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002020 * \retval #PSA_ERROR_BAD_STATE
2021 * The library has not been previously initialized by psa_crypto_init().
2022 * It is implementation-dependent whether a failure to initialize
2023 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002024 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002025psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002026 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002027 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002028 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002029
Gilles Peskinedcd14942018-07-12 00:30:52 +02002030/** Abort a cipher operation.
2031 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002032 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002033 * \p operation structure itself. Once aborted, the operation object
2034 * can be reused for another operation by calling
2035 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002036 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002037 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002038 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002039 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002040 * In particular, calling psa_cipher_abort() after the operation has been
2041 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2042 * is safe and has no effect.
2043 *
2044 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002045 *
2046 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02002047 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2048 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002049 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002050 * \retval #PSA_ERROR_BAD_STATE
2051 * The library has not been previously initialized by psa_crypto_init().
2052 * It is implementation-dependent whether a failure to initialize
2053 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002054 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002055psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2056
2057/**@}*/
2058
Gilles Peskine3b555712018-03-03 21:27:57 +01002059/** \defgroup aead Authenticated encryption with associated data (AEAD)
2060 * @{
2061 */
2062
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002063/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002064 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002065 * \param key Identifier of the key to use for the
2066 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002067 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002068 * \param alg The AEAD algorithm to compute
2069 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002070 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002071 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002072 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002073 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002074 * but not encrypted.
2075 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002076 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002077 * encrypted.
2078 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002079 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002080 * encrypted data. The additional data is not
2081 * part of this output. For algorithms where the
2082 * encrypted data and the authentication tag
2083 * are defined as separate outputs, the
2084 * authentication tag is appended to the
2085 * encrypted data.
2086 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2087 * This must be at least
2088 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2089 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002090 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002091 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002092 *
Gilles Peskine28538492018-07-11 17:34:00 +02002093 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002094 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002095 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002096 * \retval #PSA_ERROR_NOT_PERMITTED
2097 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002098 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002099 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002100 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002101 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002102 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2103 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002104 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2105 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002106 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002107 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002108 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002109 * The library has not been previously initialized by psa_crypto_init().
2110 * It is implementation-dependent whether a failure to initialize
2111 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002112 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002113psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002114 psa_algorithm_t alg,
2115 const uint8_t *nonce,
2116 size_t nonce_length,
2117 const uint8_t *additional_data,
2118 size_t additional_data_length,
2119 const uint8_t *plaintext,
2120 size_t plaintext_length,
2121 uint8_t *ciphertext,
2122 size_t ciphertext_size,
2123 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002124
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002125/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002126 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002127 * \param key Identifier of the key to use for the
2128 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002129 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002130 * \param alg The AEAD algorithm to compute
2131 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002132 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002133 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002134 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002135 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002136 * but not encrypted.
2137 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002138 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002139 * encrypted. For algorithms where the
2140 * encrypted data and the authentication tag
2141 * are defined as separate inputs, the buffer
2142 * must contain the encrypted data followed
2143 * by the authentication tag.
2144 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002145 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002146 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2147 * This must be at least
2148 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2149 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002150 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002151 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002152 *
Gilles Peskine28538492018-07-11 17:34:00 +02002153 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002154 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002155 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002156 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002157 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002158 * \retval #PSA_ERROR_NOT_PERMITTED
2159 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002160 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002161 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002162 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002163 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002164 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2165 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002166 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2167 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002168 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002169 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002170 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002171 * The library has not been previously initialized by psa_crypto_init().
2172 * It is implementation-dependent whether a failure to initialize
2173 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002174 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002175psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002176 psa_algorithm_t alg,
2177 const uint8_t *nonce,
2178 size_t nonce_length,
2179 const uint8_t *additional_data,
2180 size_t additional_data_length,
2181 const uint8_t *ciphertext,
2182 size_t ciphertext_length,
2183 uint8_t *plaintext,
2184 size_t plaintext_size,
2185 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002186
Gilles Peskine30a9e412019-01-14 18:36:12 +01002187/** The type of the state data structure for multipart AEAD operations.
2188 *
2189 * Before calling any function on an AEAD operation object, the application
2190 * must initialize it by any of the following means:
2191 * - Set the structure to all-bits-zero, for example:
2192 * \code
2193 * psa_aead_operation_t operation;
2194 * memset(&operation, 0, sizeof(operation));
2195 * \endcode
2196 * - Initialize the structure to logical zero values, for example:
2197 * \code
2198 * psa_aead_operation_t operation = {0};
2199 * \endcode
2200 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2201 * for example:
2202 * \code
2203 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2204 * \endcode
2205 * - Assign the result of the function psa_aead_operation_init()
2206 * to the structure, for example:
2207 * \code
2208 * psa_aead_operation_t operation;
2209 * operation = psa_aead_operation_init();
2210 * \endcode
2211 *
2212 * This is an implementation-defined \c struct. Applications should not
2213 * make any assumptions about the content of this structure except
2214 * as directed by the documentation of a specific implementation. */
2215typedef struct psa_aead_operation_s psa_aead_operation_t;
2216
2217/** \def PSA_AEAD_OPERATION_INIT
2218 *
2219 * This macro returns a suitable initializer for an AEAD operation object of
2220 * type #psa_aead_operation_t.
2221 */
2222#ifdef __DOXYGEN_ONLY__
2223/* This is an example definition for documentation purposes.
2224 * Implementations should define a suitable value in `crypto_struct.h`.
2225 */
2226#define PSA_AEAD_OPERATION_INIT {0}
2227#endif
2228
2229/** Return an initial value for an AEAD operation object.
2230 */
2231static psa_aead_operation_t psa_aead_operation_init(void);
2232
2233/** Set the key for a multipart authenticated encryption operation.
2234 *
2235 * The sequence of operations to encrypt a message with authentication
2236 * is as follows:
2237 * -# Allocate an operation object which will be passed to all the functions
2238 * listed here.
2239 * -# Initialize the operation object with one of the methods described in the
2240 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002241 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002242 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002243 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2244 * inputs to the subsequent calls to psa_aead_update_ad() and
2245 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2246 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002247 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2248 * generate or set the nonce. You should use
2249 * psa_aead_generate_nonce() unless the protocol you are implementing
2250 * requires a specific nonce value.
2251 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2252 * of the non-encrypted additional authenticated data each time.
2253 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002254 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002255 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002256 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002257 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2258 * the operation will need to be reset by a call to psa_aead_abort(). The
2259 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002260 * has been initialized.
2261 *
2262 * After a successful call to psa_aead_encrypt_setup(), the application must
2263 * eventually terminate the operation. The following events terminate an
2264 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002265 * - A successful call to psa_aead_finish().
2266 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002267 *
2268 * \param[in,out] operation The operation object to set up. It must have
2269 * been initialized as per the documentation for
2270 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002271 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002272 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002273 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002274 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002275 * \param alg The AEAD algorithm to compute
2276 * (\c PSA_ALG_XXX value such that
2277 * #PSA_ALG_IS_AEAD(\p alg) is true).
2278 *
2279 * \retval #PSA_SUCCESS
2280 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002281 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002282 * The operation state is not valid (it must be inactive).
Ronald Cron96783552020-10-19 12:06:30 +02002283 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002284 * \retval #PSA_ERROR_NOT_PERMITTED
2285 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002286 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002287 * \retval #PSA_ERROR_NOT_SUPPORTED
2288 * \p alg is not supported or is not an AEAD algorithm.
2289 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2290 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2291 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002292 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002293 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002294 * \retval #PSA_ERROR_BAD_STATE
2295 * The library has not been previously initialized by psa_crypto_init().
2296 * It is implementation-dependent whether a failure to initialize
2297 * results in this error code.
2298 */
2299psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002300 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002301 psa_algorithm_t alg);
2302
2303/** Set the key for a multipart authenticated decryption operation.
2304 *
2305 * The sequence of operations to decrypt a message with authentication
2306 * is as follows:
2307 * -# Allocate an operation object which will be passed to all the functions
2308 * listed here.
2309 * -# Initialize the operation object with one of the methods described in the
2310 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002311 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002312 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002313 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2314 * inputs to the subsequent calls to psa_aead_update_ad() and
2315 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2316 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002317 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2318 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2319 * of the non-encrypted additional authenticated data each time.
2320 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002321 * of the ciphertext to decrypt each time.
2322 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002323 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002324 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2325 * the operation will need to be reset by a call to psa_aead_abort(). The
2326 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002327 * has been initialized.
2328 *
2329 * After a successful call to psa_aead_decrypt_setup(), the application must
2330 * eventually terminate the operation. The following events terminate an
2331 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002332 * - A successful call to psa_aead_verify().
2333 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002334 *
2335 * \param[in,out] operation The operation object to set up. It must have
2336 * been initialized as per the documentation for
2337 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002338 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002339 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002340 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002341 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002342 * \param alg The AEAD algorithm to compute
2343 * (\c PSA_ALG_XXX value such that
2344 * #PSA_ALG_IS_AEAD(\p alg) is true).
2345 *
2346 * \retval #PSA_SUCCESS
2347 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002348 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002349 * The operation state is not valid (it must be inactive).
Ronald Cron96783552020-10-19 12:06:30 +02002350 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002351 * \retval #PSA_ERROR_NOT_PERMITTED
2352 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002353 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002354 * \retval #PSA_ERROR_NOT_SUPPORTED
2355 * \p alg is not supported or is not an AEAD algorithm.
2356 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2357 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2358 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002359 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002360 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002361 * \retval #PSA_ERROR_BAD_STATE
2362 * The library has not been previously initialized by psa_crypto_init().
2363 * It is implementation-dependent whether a failure to initialize
2364 * results in this error code.
2365 */
2366psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002367 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002368 psa_algorithm_t alg);
2369
2370/** Generate a random nonce for an authenticated encryption operation.
2371 *
2372 * This function generates a random nonce for the authenticated encryption
2373 * operation with an appropriate size for the chosen algorithm, key type
2374 * and key size.
2375 *
2376 * The application must call psa_aead_encrypt_setup() before
2377 * calling this function.
2378 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002379 * If this function returns an error status, the operation enters an error
2380 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002381 *
2382 * \param[in,out] operation Active AEAD operation.
2383 * \param[out] nonce Buffer where the generated nonce is to be
2384 * written.
2385 * \param nonce_size Size of the \p nonce buffer in bytes.
2386 * \param[out] nonce_length On success, the number of bytes of the
2387 * generated nonce.
2388 *
2389 * \retval #PSA_SUCCESS
2390 * Success.
2391 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002392 * The operation state is not valid (it must be an active aead encrypt
Ronald Cron96783552020-10-19 12:06:30 +02002393 * operation, with no nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002394 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2395 * The size of the \p nonce buffer is too small.
2396 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2397 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2398 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002399 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002400 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002401 * \retval #PSA_ERROR_BAD_STATE
2402 * The library has not been previously initialized by psa_crypto_init().
2403 * It is implementation-dependent whether a failure to initialize
2404 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002405 */
2406psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002407 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002408 size_t nonce_size,
2409 size_t *nonce_length);
2410
2411/** Set the nonce for an authenticated encryption or decryption operation.
2412 *
2413 * This function sets the nonce for the authenticated
2414 * encryption or decryption operation.
2415 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002416 * The application must call psa_aead_encrypt_setup() or
2417 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002418 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002419 * If this function returns an error status, the operation enters an error
2420 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002421 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002422 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002423 * instead of this function, unless implementing a protocol that requires
2424 * a non-random IV.
2425 *
2426 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002427 * \param[in] nonce Buffer containing the nonce to use.
2428 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002429 *
2430 * \retval #PSA_SUCCESS
2431 * Success.
2432 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002433 * The operation state is not valid (it must be active, with no nonce
2434 * set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002435 * \retval #PSA_ERROR_INVALID_ARGUMENT
2436 * The size of \p nonce is not acceptable for the chosen algorithm.
2437 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2438 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2439 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002440 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002441 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002442 * \retval #PSA_ERROR_BAD_STATE
2443 * The library has not been previously initialized by psa_crypto_init().
2444 * It is implementation-dependent whether a failure to initialize
2445 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002446 */
2447psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002448 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002449 size_t nonce_length);
2450
Gilles Peskinebc59c852019-01-17 15:26:08 +01002451/** Declare the lengths of the message and additional data for AEAD.
2452 *
2453 * The application must call this function before calling
2454 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2455 * the operation requires it. If the algorithm does not require it,
2456 * calling this function is optional, but if this function is called
2457 * then the implementation must enforce the lengths.
2458 *
2459 * You may call this function before or after setting the nonce with
2460 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2461 *
2462 * - For #PSA_ALG_CCM, calling this function is required.
2463 * - For the other AEAD algorithms defined in this specification, calling
2464 * this function is not required.
2465 * - For vendor-defined algorithm, refer to the vendor documentation.
2466 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002467 * If this function returns an error status, the operation enters an error
2468 * state and must be aborted by calling psa_aead_abort().
2469 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002470 * \param[in,out] operation Active AEAD operation.
2471 * \param ad_length Size of the non-encrypted additional
2472 * authenticated data in bytes.
2473 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2474 *
2475 * \retval #PSA_SUCCESS
2476 * Success.
2477 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002478 * The operation state is not valid (it must be active, and
2479 * psa_aead_update_ad() and psa_aead_update() must not have been
2480 * called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002481 * \retval #PSA_ERROR_INVALID_ARGUMENT
2482 * At least one of the lengths is not acceptable for the chosen
2483 * algorithm.
2484 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2485 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2486 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002487 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002488 * \retval #PSA_ERROR_BAD_STATE
2489 * The library has not been previously initialized by psa_crypto_init().
2490 * It is implementation-dependent whether a failure to initialize
2491 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002492 */
2493psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2494 size_t ad_length,
2495 size_t plaintext_length);
2496
Gilles Peskine30a9e412019-01-14 18:36:12 +01002497/** Pass additional data to an active AEAD operation.
2498 *
2499 * Additional data is authenticated, but not encrypted.
2500 *
2501 * You may call this function multiple times to pass successive fragments
2502 * of the additional data. You may not call this function after passing
2503 * data to encrypt or decrypt with psa_aead_update().
2504 *
2505 * Before calling this function, you must:
2506 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2507 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2508 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002509 * If this function returns an error status, the operation enters an error
2510 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002511 *
2512 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2513 * there is no guarantee that the input is valid. Therefore, until
2514 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2515 * treat the input as untrusted and prepare to undo any action that
2516 * depends on the input if psa_aead_verify() returns an error status.
2517 *
2518 * \param[in,out] operation Active AEAD operation.
2519 * \param[in] input Buffer containing the fragment of
2520 * additional data.
2521 * \param input_length Size of the \p input buffer in bytes.
2522 *
2523 * \retval #PSA_SUCCESS
2524 * Success.
2525 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002526 * The operation state is not valid (it must be active, have a nonce
2527 * set, have lengths set if required by the algorithm, and
2528 * psa_aead_update() must not have been called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002529 * \retval #PSA_ERROR_INVALID_ARGUMENT
2530 * The total input length overflows the additional data length that
2531 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002532 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2533 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2534 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002535 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002536 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002537 * \retval #PSA_ERROR_BAD_STATE
2538 * The library has not been previously initialized by psa_crypto_init().
2539 * It is implementation-dependent whether a failure to initialize
2540 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002541 */
2542psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2543 const uint8_t *input,
2544 size_t input_length);
2545
2546/** Encrypt or decrypt a message fragment in an active AEAD operation.
2547 *
2548 * Before calling this function, you must:
2549 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2550 * The choice of setup function determines whether this function
2551 * encrypts or decrypts its input.
2552 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2553 * 3. Call psa_aead_update_ad() to pass all the additional data.
2554 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002555 * If this function returns an error status, the operation enters an error
2556 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002557 *
2558 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2559 * there is no guarantee that the input is valid. Therefore, until
2560 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2561 * - Do not use the output in any way other than storing it in a
2562 * confidential location. If you take any action that depends
2563 * on the tentative decrypted data, this action will need to be
2564 * undone if the input turns out not to be valid. Furthermore,
2565 * if an adversary can observe that this action took place
2566 * (for example through timing), they may be able to use this
2567 * fact as an oracle to decrypt any message encrypted with the
2568 * same key.
2569 * - In particular, do not copy the output anywhere but to a
2570 * memory or storage space that you have exclusive access to.
2571 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002572 * This function does not require the input to be aligned to any
2573 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002574 * a whole block at a time, it must consume all the input provided, but
2575 * it may delay the end of the corresponding output until a subsequent
2576 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2577 * provides sufficient input. The amount of data that can be delayed
2578 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002579 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002580 * \param[in,out] operation Active AEAD operation.
2581 * \param[in] input Buffer containing the message fragment to
2582 * encrypt or decrypt.
2583 * \param input_length Size of the \p input buffer in bytes.
2584 * \param[out] output Buffer where the output is to be written.
2585 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002586 * This must be at least
2587 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2588 * \p input_length) where \c alg is the
2589 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002590 * \param[out] output_length On success, the number of bytes
2591 * that make up the returned output.
2592 *
2593 * \retval #PSA_SUCCESS
2594 * Success.
2595 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002596 * The operation state is not valid (it must be active, have a nonce
2597 * set, and have lengths set if required by the algorithm).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002598 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2599 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002600 * You can determine a sufficient buffer size by calling
2601 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2602 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002603 * \retval #PSA_ERROR_INVALID_ARGUMENT
2604 * The total length of input to psa_aead_update_ad() so far is
2605 * less than the additional data length that was previously
2606 * specified with psa_aead_set_lengths().
2607 * \retval #PSA_ERROR_INVALID_ARGUMENT
2608 * The total input length overflows the plaintext length that
2609 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002610 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2611 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2612 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002613 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002614 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002615 * \retval #PSA_ERROR_BAD_STATE
2616 * The library has not been previously initialized by psa_crypto_init().
2617 * It is implementation-dependent whether a failure to initialize
2618 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002619 */
2620psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2621 const uint8_t *input,
2622 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002623 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002624 size_t output_size,
2625 size_t *output_length);
2626
2627/** Finish encrypting a message in an AEAD operation.
2628 *
2629 * The operation must have been set up with psa_aead_encrypt_setup().
2630 *
2631 * This function finishes the authentication of the additional data
2632 * formed by concatenating the inputs passed to preceding calls to
2633 * psa_aead_update_ad() with the plaintext formed by concatenating the
2634 * inputs passed to preceding calls to psa_aead_update().
2635 *
2636 * This function has two output buffers:
2637 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002638 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002639 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002640 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002641 * that the operation performs.
2642 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002643 * When this function returns successfuly, the operation becomes inactive.
2644 * If this function returns an error status, the operation enters an error
2645 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002646 *
2647 * \param[in,out] operation Active AEAD operation.
2648 * \param[out] ciphertext Buffer where the last part of the ciphertext
2649 * is to be written.
2650 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002651 * This must be at least
2652 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2653 * \c alg is the algorithm that is being
2654 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002655 * \param[out] ciphertext_length On success, the number of bytes of
2656 * returned ciphertext.
2657 * \param[out] tag Buffer where the authentication tag is
2658 * to be written.
2659 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002660 * This must be at least
2661 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2662 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002663 * \param[out] tag_length On success, the number of bytes
2664 * that make up the returned tag.
2665 *
2666 * \retval #PSA_SUCCESS
2667 * Success.
2668 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002669 * The operation state is not valid (it must be an active encryption
2670 * operation with a nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002671 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002672 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002673 * You can determine a sufficient buffer size for \p ciphertext by
2674 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2675 * where \c alg is the algorithm that is being calculated.
2676 * You can determine a sufficient buffer size for \p tag by
2677 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002678 * \retval #PSA_ERROR_INVALID_ARGUMENT
2679 * The total length of input to psa_aead_update_ad() so far is
2680 * less than the additional data length that was previously
2681 * specified with psa_aead_set_lengths().
2682 * \retval #PSA_ERROR_INVALID_ARGUMENT
2683 * The total length of input to psa_aead_update() so far is
2684 * less than the plaintext length that was previously
2685 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002686 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2687 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2688 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002689 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002690 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002691 * \retval #PSA_ERROR_BAD_STATE
2692 * The library has not been previously initialized by psa_crypto_init().
2693 * It is implementation-dependent whether a failure to initialize
2694 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002695 */
2696psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002697 uint8_t *ciphertext,
2698 size_t ciphertext_size,
2699 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002700 uint8_t *tag,
2701 size_t tag_size,
2702 size_t *tag_length);
2703
2704/** Finish authenticating and decrypting a message in an AEAD operation.
2705 *
2706 * The operation must have been set up with psa_aead_decrypt_setup().
2707 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002708 * This function finishes the authenticated decryption of the message
2709 * components:
2710 *
2711 * - The additional data consisting of the concatenation of the inputs
2712 * passed to preceding calls to psa_aead_update_ad().
2713 * - The ciphertext consisting of the concatenation of the inputs passed to
2714 * preceding calls to psa_aead_update().
2715 * - The tag passed to this function call.
2716 *
2717 * If the authentication tag is correct, this function outputs any remaining
2718 * plaintext and reports success. If the authentication tag is not correct,
2719 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002720 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002721 * When this function returns successfuly, the operation becomes inactive.
2722 * If this function returns an error status, the operation enters an error
2723 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002724 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002725 * \note Implementations shall make the best effort to ensure that the
2726 * comparison between the actual tag and the expected tag is performed
2727 * in constant time.
2728 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002729 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002730 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002731 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002732 * from previous calls to psa_aead_update()
2733 * that could not be processed until the end
2734 * of the input.
2735 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002736 * This must be at least
2737 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2738 * \c alg is the algorithm that is being
2739 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002740 * \param[out] plaintext_length On success, the number of bytes of
2741 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002742 * \param[in] tag Buffer containing the authentication tag.
2743 * \param tag_length Size of the \p tag buffer in bytes.
2744 *
2745 * \retval #PSA_SUCCESS
2746 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002747 * \retval #PSA_ERROR_INVALID_SIGNATURE
2748 * The calculations were successful, but the authentication tag is
2749 * not correct.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002750 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002751 * The operation state is not valid (it must be an active decryption
2752 * operation with a nonce set).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002753 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2754 * The size of the \p plaintext buffer is too small.
2755 * You can determine a sufficient buffer size for \p plaintext by
2756 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2757 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002758 * \retval #PSA_ERROR_INVALID_ARGUMENT
2759 * The total length of input to psa_aead_update_ad() so far is
2760 * less than the additional data length that was previously
2761 * specified with psa_aead_set_lengths().
2762 * \retval #PSA_ERROR_INVALID_ARGUMENT
2763 * The total length of input to psa_aead_update() so far is
2764 * less than the plaintext length that was previously
2765 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002766 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2767 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2768 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002769 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002770 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002771 * \retval #PSA_ERROR_BAD_STATE
2772 * The library has not been previously initialized by psa_crypto_init().
2773 * It is implementation-dependent whether a failure to initialize
2774 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002775 */
2776psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002777 uint8_t *plaintext,
2778 size_t plaintext_size,
2779 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002780 const uint8_t *tag,
2781 size_t tag_length);
2782
2783/** Abort an AEAD operation.
2784 *
2785 * Aborting an operation frees all associated resources except for the
2786 * \p operation structure itself. Once aborted, the operation object
2787 * can be reused for another operation by calling
2788 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2789 *
2790 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002791 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002792 *
2793 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002794 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2795 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002796 *
2797 * \param[in,out] operation Initialized AEAD operation.
2798 *
2799 * \retval #PSA_SUCCESS
Gilles Peskine30a9e412019-01-14 18:36:12 +01002800 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2801 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002802 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002803 * \retval #PSA_ERROR_BAD_STATE
2804 * The library has not been previously initialized by psa_crypto_init().
2805 * It is implementation-dependent whether a failure to initialize
2806 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002807 */
2808psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2809
Gilles Peskine3b555712018-03-03 21:27:57 +01002810/**@}*/
2811
Gilles Peskine20035e32018-02-03 22:44:14 +01002812/** \defgroup asymmetric Asymmetric cryptography
2813 * @{
2814 */
2815
2816/**
2817 * \brief Sign a hash or short message with a private key.
2818 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002819 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002820 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002821 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2822 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2823 * to determine the hash algorithm to use.
2824 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002825 * \param key Identifier of the key to use for the operation.
2826 * It must be an asymmetric key pair. The key must
Ronald Cron96783552020-10-19 12:06:30 +02002827 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002828 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002829 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002830 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002831 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002832 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002833 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002834 * \param[out] signature_length On success, the number of bytes
2835 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002836 *
Gilles Peskine28538492018-07-11 17:34:00 +02002837 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002838 * \retval #PSA_ERROR_INVALID_HANDLE
2839 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002840 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002841 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002842 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002843 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002844 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002845 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002846 * \retval #PSA_ERROR_NOT_SUPPORTED
2847 * \retval #PSA_ERROR_INVALID_ARGUMENT
2848 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2849 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2850 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002851 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002852 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002853 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002854 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002855 * The library has not been previously initialized by psa_crypto_init().
2856 * It is implementation-dependent whether a failure to initialize
2857 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002858 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002859psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002860 psa_algorithm_t alg,
2861 const uint8_t *hash,
2862 size_t hash_length,
2863 uint8_t *signature,
2864 size_t signature_size,
2865 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002866
2867/**
2868 * \brief Verify the signature a hash or short message using a public key.
2869 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002870 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002871 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002872 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2873 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2874 * to determine the hash algorithm to use.
2875 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002876 * \param key Identifier of the key to use for the operation. It
2877 * must be a public key or an asymmetric key pair. The
Ronald Cron96783552020-10-19 12:06:30 +02002878 * key must allow the usage
2879 * #PSA_KEY_USAGE_VERIFY_HASH.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002880 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002881 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002882 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002883 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002884 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002885 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002886 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002887 *
Gilles Peskine28538492018-07-11 17:34:00 +02002888 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002889 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002890 * \retval #PSA_ERROR_INVALID_HANDLE
2891 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002892 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002893 * The calculation was perfomed successfully, but the passed
2894 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002895 * \retval #PSA_ERROR_NOT_SUPPORTED
2896 * \retval #PSA_ERROR_INVALID_ARGUMENT
2897 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2898 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2899 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002900 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002901 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002902 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002903 * The library has not been previously initialized by psa_crypto_init().
2904 * It is implementation-dependent whether a failure to initialize
2905 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002906 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002907psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002908 psa_algorithm_t alg,
2909 const uint8_t *hash,
2910 size_t hash_length,
2911 const uint8_t *signature,
2912 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002913
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002914/**
2915 * \brief Encrypt a short message with a public key.
2916 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002917 * \param key Identifer of the key to use for the operation.
2918 * It must be a public key or an asymmetric key
2919 * pair. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002920 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002921 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02002922 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002923 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002924 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002925 * \param[in] salt A salt or label, if supported by the
2926 * encryption algorithm.
2927 * If the algorithm does not support a
2928 * salt, pass \c NULL.
2929 * If the algorithm supports an optional
2930 * salt and you do not want to pass a salt,
2931 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002932 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002933 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2934 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002935 * \param salt_length Size of the \p salt buffer in bytes.
2936 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002937 * \param[out] output Buffer where the encrypted message is to
2938 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002939 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002940 * \param[out] output_length On success, the number of bytes
2941 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002942 *
Gilles Peskine28538492018-07-11 17:34:00 +02002943 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002944 * \retval #PSA_ERROR_INVALID_HANDLE
2945 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002946 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002947 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002948 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002949 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002950 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002951 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002952 * \retval #PSA_ERROR_NOT_SUPPORTED
2953 * \retval #PSA_ERROR_INVALID_ARGUMENT
2954 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2955 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2956 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002957 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002958 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002959 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002960 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002961 * The library has not been previously initialized by psa_crypto_init().
2962 * It is implementation-dependent whether a failure to initialize
2963 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002964 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002965psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002966 psa_algorithm_t alg,
2967 const uint8_t *input,
2968 size_t input_length,
2969 const uint8_t *salt,
2970 size_t salt_length,
2971 uint8_t *output,
2972 size_t output_size,
2973 size_t *output_length);
2974
2975/**
2976 * \brief Decrypt a short message with a private key.
2977 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002978 * \param key Identifier of the key to use for the operation.
2979 * It must be an asymmetric key pair. It must
Ronald Cron96783552020-10-19 12:06:30 +02002980 * allow the usage #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002981 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02002982 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002983 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002984 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002985 * \param[in] salt A salt or label, if supported by the
2986 * encryption algorithm.
2987 * If the algorithm does not support a
2988 * salt, pass \c NULL.
2989 * If the algorithm supports an optional
2990 * salt and you do not want to pass a salt,
2991 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002992 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002993 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2994 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002995 * \param salt_length Size of the \p salt buffer in bytes.
2996 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002997 * \param[out] output Buffer where the decrypted message is to
2998 * be written.
2999 * \param output_size Size of the \c output buffer in bytes.
3000 * \param[out] output_length On success, the number of bytes
3001 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003002 *
Gilles Peskine28538492018-07-11 17:34:00 +02003003 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003004 * \retval #PSA_ERROR_INVALID_HANDLE
3005 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003006 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003007 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003008 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003009 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003010 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003011 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003012 * \retval #PSA_ERROR_NOT_SUPPORTED
3013 * \retval #PSA_ERROR_INVALID_ARGUMENT
3014 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3015 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3016 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003017 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003018 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003019 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3020 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003021 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003022 * The library has not been previously initialized by psa_crypto_init().
3023 * It is implementation-dependent whether a failure to initialize
3024 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003025 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003026psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003027 psa_algorithm_t alg,
3028 const uint8_t *input,
3029 size_t input_length,
3030 const uint8_t *salt,
3031 size_t salt_length,
3032 uint8_t *output,
3033 size_t output_size,
3034 size_t *output_length);
3035
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003036/**@}*/
3037
Gilles Peskine35675b62019-05-16 17:26:11 +02003038/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003039 * @{
3040 */
3041
Gilles Peskine35675b62019-05-16 17:26:11 +02003042/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003043 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003044 * Before calling any function on a key derivation operation object, the
3045 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003046 * - Set the structure to all-bits-zero, for example:
3047 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003048 * psa_key_derivation_operation_t operation;
3049 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003050 * \endcode
3051 * - Initialize the structure to logical zero values, for example:
3052 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003053 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003054 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003055 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003056 * for example:
3057 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003058 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003059 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003060 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003061 * to the structure, for example:
3062 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003063 * psa_key_derivation_operation_t operation;
3064 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003065 * \endcode
3066 *
3067 * This is an implementation-defined \c struct. Applications should not
3068 * make any assumptions about the content of this structure except
3069 * as directed by the documentation of a specific implementation.
3070 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003071typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003072
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003073/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003074 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003075 * This macro returns a suitable initializer for a key derivation operation
3076 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003077 */
3078#ifdef __DOXYGEN_ONLY__
3079/* This is an example definition for documentation purposes.
3080 * Implementations should define a suitable value in `crypto_struct.h`.
3081 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003082#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003083#endif
3084
Gilles Peskine35675b62019-05-16 17:26:11 +02003085/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003086 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003087static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003088
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003089/** Set up a key derivation operation.
3090 *
3091 * A key derivation algorithm takes some inputs and uses them to generate
3092 * a byte stream in a deterministic way.
3093 * This byte stream can be used to produce keys and other
3094 * cryptographic material.
3095 *
3096 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003097 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3098 * -# Call psa_key_derivation_setup() to select the algorithm.
3099 * -# Provide the inputs for the key derivation by calling
3100 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3101 * as appropriate. Which inputs are needed, in what order, and whether
3102 * they may be keys and if so of what type depends on the algorithm.
3103 * -# Optionally set the operation's maximum capacity with
3104 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3105 * of or after providing inputs. For some algorithms, this step is mandatory
3106 * because the output depends on the maximum capacity.
3107 * -# To derive a key, call psa_key_derivation_output_key().
3108 * To derive a byte string for a different purpose, call
3109 * psa_key_derivation_output_bytes().
3110 * Successive calls to these functions use successive output bytes
3111 * calculated by the key derivation algorithm.
3112 * -# Clean up the key derivation operation object with
3113 * psa_key_derivation_abort().
3114 *
3115 * If this function returns an error, the key derivation operation object is
3116 * not changed.
3117 *
3118 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3119 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003120 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003121 * Implementations must reject an attempt to derive a key of size 0.
3122 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003123 * \param[in,out] operation The key derivation operation object
3124 * to set up. It must
3125 * have been initialized but not set up yet.
3126 * \param alg The key derivation algorithm to compute
3127 * (\c PSA_ALG_XXX value such that
3128 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3129 *
3130 * \retval #PSA_SUCCESS
3131 * Success.
3132 * \retval #PSA_ERROR_INVALID_ARGUMENT
3133 * \c alg is not a key derivation algorithm.
3134 * \retval #PSA_ERROR_NOT_SUPPORTED
3135 * \c alg is not supported or is not a key derivation algorithm.
3136 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3137 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3138 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003139 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003140 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003141 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003142 * The operation state is not valid (it must be inactive).
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003143 * \retval #PSA_ERROR_BAD_STATE
3144 * The library has not been previously initialized by psa_crypto_init().
3145 * It is implementation-dependent whether a failure to initialize
3146 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003147 */
3148psa_status_t psa_key_derivation_setup(
3149 psa_key_derivation_operation_t *operation,
3150 psa_algorithm_t alg);
3151
Gilles Peskine35675b62019-05-16 17:26:11 +02003152/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003153 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003154 * The capacity of a key derivation is the maximum number of bytes that it can
3155 * return. When you get *N* bytes of output from a key derivation operation,
3156 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003157 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003158 * \param[in] operation The operation to query.
3159 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003160 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003161 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003162 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003163 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003164 * The operation state is not valid (it must be active).
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003165 * \retval #PSA_ERROR_HARDWARE_FAILURE
3166 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003167 * \retval #PSA_ERROR_BAD_STATE
3168 * The library has not been previously initialized by psa_crypto_init().
3169 * It is implementation-dependent whether a failure to initialize
3170 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003171 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003172psa_status_t psa_key_derivation_get_capacity(
3173 const psa_key_derivation_operation_t *operation,
3174 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003175
Gilles Peskine35675b62019-05-16 17:26:11 +02003176/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003177 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003178 * The capacity of a key derivation operation is the maximum number of bytes
3179 * that the key derivation operation can return from this point onwards.
3180 *
3181 * \param[in,out] operation The key derivation operation object to modify.
3182 * \param capacity The new capacity of the operation.
3183 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003184 * current capacity.
3185 *
3186 * \retval #PSA_SUCCESS
3187 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003188 * \p capacity is larger than the operation's current capacity.
3189 * In this case, the operation object remains valid and its capacity
3190 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003191 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003192 * The operation state is not valid (it must be active).
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003193 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003194 * \retval #PSA_ERROR_HARDWARE_FAILURE
3195 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003196 * \retval #PSA_ERROR_BAD_STATE
3197 * The library has not been previously initialized by psa_crypto_init().
3198 * It is implementation-dependent whether a failure to initialize
3199 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003200 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003201psa_status_t psa_key_derivation_set_capacity(
3202 psa_key_derivation_operation_t *operation,
3203 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003204
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003205/** Use the maximum possible capacity for a key derivation operation.
3206 *
3207 * Use this value as the capacity argument when setting up a key derivation
3208 * to indicate that the operation should have the maximum possible capacity.
3209 * The value of the maximum possible capacity depends on the key derivation
3210 * algorithm.
3211 */
3212#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3213
3214/** Provide an input for key derivation or key agreement.
3215 *
3216 * Which inputs are required and in what order depends on the algorithm.
3217 * Refer to the documentation of each key derivation or key agreement
3218 * algorithm for information.
3219 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003220 * This function passes direct inputs, which is usually correct for
3221 * non-secret inputs. To pass a secret input, which should be in a key
3222 * object, call psa_key_derivation_input_key() instead of this function.
3223 * Refer to the documentation of individual step types
3224 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3225 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003226 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003227 * If this function returns an error status, the operation enters an error
3228 * state and must be aborted by calling psa_key_derivation_abort().
3229 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003230 * \param[in,out] operation The key derivation operation object to use.
3231 * It must have been set up with
3232 * psa_key_derivation_setup() and must not
3233 * have produced any output yet.
3234 * \param step Which step the input data is for.
3235 * \param[in] data Input data to use.
3236 * \param data_length Size of the \p data buffer in bytes.
3237 *
3238 * \retval #PSA_SUCCESS
3239 * Success.
3240 * \retval #PSA_ERROR_INVALID_ARGUMENT
3241 * \c step is not compatible with the operation's algorithm.
3242 * \retval #PSA_ERROR_INVALID_ARGUMENT
3243 * \c step does not allow direct inputs.
3244 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3245 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3246 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003247 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003248 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003249 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003250 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003251 * \retval #PSA_ERROR_BAD_STATE
3252 * The library has not been previously initialized by psa_crypto_init().
3253 * It is implementation-dependent whether a failure to initialize
3254 * results in this error code.
3255 */
3256psa_status_t psa_key_derivation_input_bytes(
3257 psa_key_derivation_operation_t *operation,
3258 psa_key_derivation_step_t step,
3259 const uint8_t *data,
3260 size_t data_length);
3261
3262/** Provide an input for key derivation in the form of a key.
3263 *
3264 * Which inputs are required and in what order depends on the algorithm.
3265 * Refer to the documentation of each key derivation or key agreement
3266 * algorithm for information.
3267 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003268 * This function obtains input from a key object, which is usually correct for
3269 * secret inputs or for non-secret personalization strings kept in the key
3270 * store. To pass a non-secret parameter which is not in the key store,
3271 * call psa_key_derivation_input_bytes() instead of this function.
3272 * Refer to the documentation of individual step types
3273 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3274 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003275 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003276 * If this function returns an error status, the operation enters an error
3277 * state and must be aborted by calling psa_key_derivation_abort().
3278 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003279 * \param[in,out] operation The key derivation operation object to use.
3280 * It must have been set up with
3281 * psa_key_derivation_setup() and must not
3282 * have produced any output yet.
3283 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003284 * \param key Identifier of the key. It must have an
3285 * appropriate type for step and must allow the
Ronald Cron96783552020-10-19 12:06:30 +02003286 * usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003287 *
3288 * \retval #PSA_SUCCESS
3289 * Success.
3290 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003291 * \retval #PSA_ERROR_NOT_PERMITTED
3292 * \retval #PSA_ERROR_INVALID_ARGUMENT
3293 * \c step is not compatible with the operation's algorithm.
3294 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine224b0d62019-09-23 18:13:17 +02003295 * \c step does not allow key inputs of the given type
3296 * or does not allow key inputs at all.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003297 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3298 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3299 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003300 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003301 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003302 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003303 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003304 * \retval #PSA_ERROR_BAD_STATE
3305 * The library has not been previously initialized by psa_crypto_init().
3306 * It is implementation-dependent whether a failure to initialize
3307 * results in this error code.
3308 */
3309psa_status_t psa_key_derivation_input_key(
3310 psa_key_derivation_operation_t *operation,
3311 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003312 mbedtls_svc_key_id_t key);
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003313
3314/** Perform a key agreement and use the shared secret as input to a key
3315 * derivation.
3316 *
3317 * A key agreement algorithm takes two inputs: a private key \p private_key
3318 * a public key \p peer_key.
3319 * The result of this function is passed as input to a key derivation.
3320 * The output of this key derivation can be extracted by reading from the
3321 * resulting operation to produce keys and other cryptographic material.
3322 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003323 * If this function returns an error status, the operation enters an error
3324 * state and must be aborted by calling psa_key_derivation_abort().
3325 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003326 * \param[in,out] operation The key derivation operation object to use.
3327 * It must have been set up with
3328 * psa_key_derivation_setup() with a
3329 * key agreement and derivation algorithm
3330 * \c alg (\c PSA_ALG_XXX value such that
3331 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3332 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3333 * is false).
3334 * The operation must be ready for an
3335 * input of the type given by \p step.
3336 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003337 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003338 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003339 * \param[in] peer_key Public key of the peer. The peer key must be in the
3340 * same format that psa_import_key() accepts for the
3341 * public key type corresponding to the type of
3342 * private_key. That is, this function performs the
3343 * equivalent of
3344 * #psa_import_key(...,
3345 * `peer_key`, `peer_key_length`) where
3346 * with key attributes indicating the public key
3347 * type corresponding to the type of `private_key`.
3348 * For example, for EC keys, this means that peer_key
3349 * is interpreted as a point on the curve that the
3350 * private key is on. The standard formats for public
3351 * keys are documented in the documentation of
3352 * psa_export_public_key().
3353 * \param peer_key_length Size of \p peer_key in bytes.
3354 *
3355 * \retval #PSA_SUCCESS
3356 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01003357 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003358 * The operation state is not valid for this key agreement \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003359 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003360 * \retval #PSA_ERROR_NOT_PERMITTED
3361 * \retval #PSA_ERROR_INVALID_ARGUMENT
3362 * \c private_key is not compatible with \c alg,
3363 * or \p peer_key is not valid for \c alg or not compatible with
3364 * \c private_key.
3365 * \retval #PSA_ERROR_NOT_SUPPORTED
3366 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskine224b0d62019-09-23 18:13:17 +02003367 * \retval #PSA_ERROR_INVALID_ARGUMENT
3368 * \c step does not allow an input resulting from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003369 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3370 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3371 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003372 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003373 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003374 * \retval #PSA_ERROR_BAD_STATE
3375 * The library has not been previously initialized by psa_crypto_init().
3376 * It is implementation-dependent whether a failure to initialize
3377 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003378 */
3379psa_status_t psa_key_derivation_key_agreement(
3380 psa_key_derivation_operation_t *operation,
3381 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003382 mbedtls_svc_key_id_t private_key,
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003383 const uint8_t *peer_key,
3384 size_t peer_key_length);
3385
Gilles Peskine35675b62019-05-16 17:26:11 +02003386/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003387 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003388 * This function calculates output bytes from a key derivation algorithm and
3389 * return those bytes.
3390 * If you view the key derivation's output as a stream of bytes, this
3391 * function destructively reads the requested number of bytes from the
3392 * stream.
3393 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003394 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003395 * If this function returns an error status other than
3396 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003397 * state and must be aborted by calling psa_key_derivation_abort().
3398 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003399 * \param[in,out] operation The key derivation operation object to read from.
3400 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003401 * \param output_length Number of bytes to output.
3402 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003403 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003404 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003405 * The operation's capacity was less than
3406 * \p output_length bytes. Note that in this case,
3407 * no output is written to the output buffer.
3408 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003409 * subsequent calls to this function will not
3410 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003411 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003412 * The operation state is not valid (it must be active and completed
3413 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003414 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3415 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3416 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003417 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003418 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003419 * \retval #PSA_ERROR_BAD_STATE
3420 * The library has not been previously initialized by psa_crypto_init().
3421 * It is implementation-dependent whether a failure to initialize
3422 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003423 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003424psa_status_t psa_key_derivation_output_bytes(
3425 psa_key_derivation_operation_t *operation,
3426 uint8_t *output,
3427 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003428
Gilles Peskine35675b62019-05-16 17:26:11 +02003429/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003430 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003431 * This function calculates output bytes from a key derivation algorithm
3432 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003433 * The key's location, usage policy, type and size are taken from
3434 * \p attributes.
3435 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003436 * If you view the key derivation's output as a stream of bytes, this
3437 * function destructively reads as many bytes as required from the
3438 * stream.
3439 * The operation's capacity decreases by the number of bytes read.
3440 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003441 * If this function returns an error status other than
3442 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003443 * state and must be aborted by calling psa_key_derivation_abort().
3444 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003445 * How much output is produced and consumed from the operation, and how
3446 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003447 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003448 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003449 * of a given size, this function is functionally equivalent to
3450 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003451 * and passing the resulting output to #psa_import_key.
3452 * However, this function has a security benefit:
3453 * if the implementation provides an isolation boundary then
3454 * the key material is not exposed outside the isolation boundary.
3455 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003456 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003457 * The following key types defined in this specification follow this scheme:
3458 *
3459 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003460 * - #PSA_KEY_TYPE_ARC4;
3461 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003462 * - #PSA_KEY_TYPE_DERIVE;
3463 * - #PSA_KEY_TYPE_HMAC.
3464 *
3465 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003466 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003467 * Montgomery curve), this function always draws a byte string whose
3468 * length is determined by the curve, and sets the mandatory bits
3469 * accordingly. That is:
3470 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01003471 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003472 * string and process it as specified in RFC 7748 &sect;5.
Paul Elliott8ff510a2020-06-02 17:19:28 +01003473 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003474 * string and process it as specified in RFC 7748 &sect;5.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003475 *
3476 * - For key types for which the key is represented by a single sequence of
3477 * \p bits bits with constraints as to which bit sequences are acceptable,
3478 * this function draws a byte string of length (\p bits / 8) bytes rounded
3479 * up to the nearest whole number of bytes. If the resulting byte string
3480 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3481 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003482 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003483 * for the output produced by psa_export_key().
3484 * The following key types defined in this specification follow this scheme:
3485 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003486 * - #PSA_KEY_TYPE_DES.
3487 * Force-set the parity bits, but discard forbidden weak keys.
3488 * For 2-key and 3-key triple-DES, the three keys are generated
3489 * successively (for example, for 3-key triple-DES,
3490 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3491 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003492 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003493 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003494 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003495 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003496 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003497 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003498 * Weierstrass curve).
3499 * For these key types, interpret the byte string as integer
3500 * in big-endian order. Discard it if it is not in the range
3501 * [0, *N* - 2] where *N* is the boundary of the private key domain
3502 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003503 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003504 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003505 * This method allows compliance to NIST standards, specifically
3506 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003507 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3508 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3509 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3510 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003511 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003512 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003513 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003514 * implementation-defined.
3515 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003516 * In all cases, the data that is read is discarded from the operation.
3517 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003518 *
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003519 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3520 * the input to that step must be provided with psa_key_derivation_input_key().
3521 * Future versions of this specification may include additional restrictions
3522 * on the derived key based on the attributes and strength of the secret key.
3523 *
Gilles Peskine20628592019-04-19 19:29:50 +02003524 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003525 * \param[in,out] operation The key derivation operation object to read from.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003526 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003527 * key. For persistent keys, this is the key
3528 * identifier defined in \p attributes.
3529 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003530 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003531 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003532 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003533 * If the key is persistent, the key material and the key's metadata
3534 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003535 * \retval #PSA_ERROR_ALREADY_EXISTS
3536 * This is an attempt to create a persistent key, and there is
3537 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003538 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003539 * There was not enough data to create the desired key.
3540 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003541 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003542 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003543 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003544 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003545 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003546 * \retval #PSA_ERROR_INVALID_ARGUMENT
3547 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003548 * \retval #PSA_ERROR_NOT_PERMITTED
3549 * The #PSA_KEY_DERIVATION_INPUT_SECRET input was not provided through
3550 * a key.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003551 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003552 * The operation state is not valid (it must be active and completed
3553 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003554 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3555 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3556 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3557 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003558 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003559 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003560 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003561 * The library has not been previously initialized by psa_crypto_init().
3562 * It is implementation-dependent whether a failure to initialize
3563 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003564 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003565psa_status_t psa_key_derivation_output_key(
3566 const psa_key_attributes_t *attributes,
3567 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003568 mbedtls_svc_key_id_t *key);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003569
Gilles Peskine35675b62019-05-16 17:26:11 +02003570/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003571 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003572 * Aborting an operation frees all associated resources except for the \c
3573 * operation structure itself. Once aborted, the operation object can be reused
3574 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003575 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003576 * This function may be called at any time after the operation
3577 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003578 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003579 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3580 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003581 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003582 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003583 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003584 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003585 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3586 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003587 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003588 * \retval #PSA_ERROR_BAD_STATE
3589 * The library has not been previously initialized by psa_crypto_init().
3590 * It is implementation-dependent whether a failure to initialize
3591 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003592 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003593psa_status_t psa_key_derivation_abort(
3594 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003595
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003596/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003597 *
3598 * \warning The raw result of a key agreement algorithm such as finite-field
3599 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3600 * not be used directly as key material. It should instead be passed as
3601 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003602 * a key derivation, use psa_key_derivation_key_agreement() and other
3603 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003604 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003605 * \param alg The key agreement algorithm to compute
3606 * (\c PSA_ALG_XXX value such that
3607 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3608 * is true).
Ronald Croncf56a0a2020-08-04 09:51:30 +02003609 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003610 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003611 * \param[in] peer_key Public key of the peer. It must be
3612 * in the same format that psa_import_key()
3613 * accepts. The standard formats for public
3614 * keys are documented in the documentation
3615 * of psa_export_public_key().
3616 * \param peer_key_length Size of \p peer_key in bytes.
3617 * \param[out] output Buffer where the decrypted message is to
3618 * be written.
3619 * \param output_size Size of the \c output buffer in bytes.
3620 * \param[out] output_length On success, the number of bytes
3621 * that make up the returned output.
3622 *
3623 * \retval #PSA_SUCCESS
3624 * Success.
3625 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003626 * \retval #PSA_ERROR_NOT_PERMITTED
3627 * \retval #PSA_ERROR_INVALID_ARGUMENT
3628 * \p alg is not a key agreement algorithm
3629 * \retval #PSA_ERROR_INVALID_ARGUMENT
3630 * \p private_key is not compatible with \p alg,
3631 * or \p peer_key is not valid for \p alg or not compatible with
3632 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003633 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3634 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003635 * \retval #PSA_ERROR_NOT_SUPPORTED
3636 * \p alg is not a supported key agreement algorithm.
3637 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3638 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3639 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003640 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003641 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003642 * \retval #PSA_ERROR_BAD_STATE
3643 * The library has not been previously initialized by psa_crypto_init().
3644 * It is implementation-dependent whether a failure to initialize
3645 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003646 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003647psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003648 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02003649 const uint8_t *peer_key,
3650 size_t peer_key_length,
3651 uint8_t *output,
3652 size_t output_size,
3653 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003654
Gilles Peskineea0fb492018-07-12 17:17:20 +02003655/**@}*/
3656
Gilles Peskineedd76872018-07-20 17:42:05 +02003657/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003658 * @{
3659 */
3660
3661/**
3662 * \brief Generate random bytes.
3663 *
3664 * \warning This function **can** fail! Callers MUST check the return status
3665 * and MUST NOT use the content of the output buffer if the return
3666 * status is not #PSA_SUCCESS.
3667 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003668 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003669 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003670 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003671 * \param output_size Number of bytes to generate and output.
3672 *
Gilles Peskine28538492018-07-11 17:34:00 +02003673 * \retval #PSA_SUCCESS
3674 * \retval #PSA_ERROR_NOT_SUPPORTED
3675 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003676 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003677 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3678 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003679 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003680 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003681 * The library has not been previously initialized by psa_crypto_init().
3682 * It is implementation-dependent whether a failure to initialize
3683 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003684 */
3685psa_status_t psa_generate_random(uint8_t *output,
3686 size_t output_size);
3687
3688/**
3689 * \brief Generate a key or key pair.
3690 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003691 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02003692 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003693 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003694 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003695 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003696 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003697 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003698 * the public exponent is 65537.
3699 * The modulus is a product of two probabilistic primes
3700 * between 2^{n-1} and 2^n where n is the bit size specified in the
3701 * attributes.
3702 *
Gilles Peskine20628592019-04-19 19:29:50 +02003703 * \param[in] attributes The attributes for the new key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003704 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003705 * key. For persistent keys, this is the key
3706 * identifier defined in \p attributes.
3707 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003708 *
Gilles Peskine28538492018-07-11 17:34:00 +02003709 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003710 * Success.
3711 * If the key is persistent, the key material and the key's metadata
3712 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003713 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003714 * This is an attempt to create a persistent key, and there is
3715 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003716 * \retval #PSA_ERROR_NOT_SUPPORTED
3717 * \retval #PSA_ERROR_INVALID_ARGUMENT
3718 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3719 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3720 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3721 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003722 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003723 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3724 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003725 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003726 * The library has not been previously initialized by psa_crypto_init().
3727 * It is implementation-dependent whether a failure to initialize
3728 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003729 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003730psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003731 mbedtls_svc_key_id_t *key);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003732
3733/**@}*/
3734
Gilles Peskinee59236f2018-01-27 23:32:46 +01003735#ifdef __cplusplus
3736}
3737#endif
3738
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003739/* The file "crypto_sizes.h" contains definitions for size calculation
3740 * macros whose definitions are implementation-specific. */
3741#include "crypto_sizes.h"
3742
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003743/* The file "crypto_struct.h" contains definitions for
3744 * implementation-specific structs that are declared above. */
3745#include "crypto_struct.h"
3746
3747/* The file "crypto_extra.h" contains vendor-specific definitions. This
3748 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003749#include "crypto_extra.h"
3750
3751#endif /* PSA_CRYPTO_H */