blob: fdebabb26e1b8f867bd189b87466b58a08789890 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02006 * Copyright The Mbed TLS Contributors
Jaeden Amerocab54942018-07-25 13:26:13 +01007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
39/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010040#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010041
Gilles Peskinee59236f2018-01-27 23:32:46 +010042#ifdef __cplusplus
43extern "C" {
44#endif
45
Gilles Peskinef3b731e2018-12-12 13:38:31 +010046/* The file "crypto_types.h" declares types that encode errors,
47 * algorithms, key types, policies, etc. */
48#include "crypto_types.h"
49
Andrew Thoelke02b372b2019-10-02 09:32:21 +010050/** \defgroup version API version
Adrian L. Shawd89338a2019-09-19 13:32:57 +010051 * @{
52 */
53
54/**
55 * The major version of this implementation of the PSA Crypto API
56 */
57#define PSA_CRYPTO_API_VERSION_MAJOR 1
58
59/**
60 * The minor version of this implementation of the PSA Crypto API
61 */
62#define PSA_CRYPTO_API_VERSION_MINOR 0
63
64/**@}*/
65
Gilles Peskinef3b731e2018-12-12 13:38:31 +010066/* The file "crypto_values.h" declares macros to build and analyze values
67 * of integral types defined in "crypto_types.h". */
68#include "crypto_values.h"
69
70/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010071 * @{
72 */
73
74/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010075 * \brief Library initialization.
76 *
77 * Applications must call this function before calling any other
78 * function in this module.
79 *
80 * Applications may call this function more than once. Once a call
81 * succeeds, subsequent calls are guaranteed to succeed.
82 *
itayzafrir18617092018-09-16 12:22:41 +030083 * If the application calls other functions before calling psa_crypto_init(),
84 * the behavior is undefined. Implementations are encouraged to either perform
85 * the operation as if the library had been initialized or to return
86 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
87 * implementations should not return a success status if the lack of
88 * initialization may have security implications, for example due to improper
89 * seeding of the random number generator.
90 *
Gilles Peskine28538492018-07-11 17:34:00 +020091 * \retval #PSA_SUCCESS
92 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
gabor-mezei-arm452b0a32020-11-09 17:42:55 +010093 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine28538492018-07-11 17:34:00 +020094 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
95 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +020096 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +020097 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
gabor-mezei-arm452b0a32020-11-09 17:42:55 +010098 * \retval #PSA_ERROR_STORAGE_FAILURE
99 * \retval #PSA_ERROR_DATA_INVALID
100 * \retval #PSA_ERROR_DATA_CORRUPT
Gilles Peskinee59236f2018-01-27 23:32:46 +0100101 */
102psa_status_t psa_crypto_init(void);
103
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100104/**@}*/
105
Gilles Peskine105f67f2019-07-23 18:16:05 +0200106/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +0200107 * @{
108 */
109
Gilles Peskinea0c06552019-05-21 15:54:54 +0200110/** \def PSA_KEY_ATTRIBUTES_INIT
111 *
112 * This macro returns a suitable initializer for a key attribute structure
113 * of type #psa_key_attributes_t.
114 */
115#ifdef __DOXYGEN_ONLY__
116/* This is an example definition for documentation purposes.
117 * Implementations should define a suitable value in `crypto_struct.h`.
118 */
119#define PSA_KEY_ATTRIBUTES_INIT {0}
120#endif
121
122/** Return an initial value for a key attributes structure.
123 */
124static psa_key_attributes_t psa_key_attributes_init(void);
125
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200126/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200127 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200128 * If the attribute structure currently declares the key as volatile (which
129 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200130 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200131 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200132 * This function does not access storage, it merely stores the given
133 * value in the structure.
134 * The persistent key will be written to storage when the attribute
135 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200136 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200137 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200138 *
Gilles Peskine20628592019-04-19 19:29:50 +0200139 * This function may be declared as `static` (i.e. without external
140 * linkage). This function may be provided as a function-like macro,
141 * but in this case it must evaluate each of its arguments exactly once.
142 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200143 * \param[out] attributes The attribute structure to write to.
144 * \param key The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200145 */
Ronald Cron71016a92020-08-28 19:01:50 +0200146static void psa_set_key_id( psa_key_attributes_t *attributes,
147 mbedtls_svc_key_id_t key );
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200148
Ronald Cron6b5ff532020-10-16 14:38:19 +0200149#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
150/** Set the owner identifier of a key.
151 *
152 * When key identifiers encode key owner identifiers, psa_set_key_id() does
153 * not allow to define in key attributes the owner of volatile keys as
154 * psa_set_key_id() enforces the key to be persistent.
155 *
156 * This function allows to set in key attributes the owner identifier of a
157 * key. It is intended to be used for volatile keys. For persistent keys,
158 * it is recommended to use the PSA Cryptography API psa_set_key_id() to define
159 * the owner of a key.
160 *
161 * \param[out] attributes The attribute structure to write to.
162 * \param owner_id The key owner identifier.
163 */
164static void mbedtls_set_key_owner_id( psa_key_attributes_t *attributes,
165 mbedtls_key_owner_id_t owner_id );
166#endif
167
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200168/** Set the location of a persistent key.
169 *
170 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200171 * with psa_set_key_id(). By default, a key that has a persistent identifier
172 * is stored in the default storage area identifier by
173 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
174 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200175 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200176 * This function does not access storage, it merely stores the given
177 * value in the structure.
178 * The persistent key will be written to storage when the attribute
179 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200180 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200181 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200182 *
183 * This function may be declared as `static` (i.e. without external
184 * linkage). This function may be provided as a function-like macro,
185 * but in this case it must evaluate each of its arguments exactly once.
186 *
187 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200188 * \param lifetime The lifetime for the key.
189 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200190 * key will be volatile, and the key identifier
191 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200192 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200193static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
194 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200195
Gilles Peskine20628592019-04-19 19:29:50 +0200196/** Retrieve the key identifier from key attributes.
197 *
198 * This function may be declared as `static` (i.e. without external
199 * linkage). This function may be provided as a function-like macro,
200 * but in this case it must evaluate its argument exactly once.
201 *
202 * \param[in] attributes The key attribute structure to query.
203 *
204 * \return The persistent identifier stored in the attribute structure.
205 * This value is unspecified if the attribute structure declares
206 * the key as volatile.
207 */
Ronald Cron71016a92020-08-28 19:01:50 +0200208static mbedtls_svc_key_id_t psa_get_key_id(
209 const psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200210
Gilles Peskine20628592019-04-19 19:29:50 +0200211/** Retrieve the lifetime from key attributes.
212 *
213 * This function may be declared as `static` (i.e. without external
214 * linkage). This function may be provided as a function-like macro,
215 * but in this case it must evaluate its argument exactly once.
216 *
217 * \param[in] attributes The key attribute structure to query.
218 *
219 * \return The lifetime value stored in the attribute structure.
220 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200221static psa_key_lifetime_t psa_get_key_lifetime(
222 const psa_key_attributes_t *attributes);
223
Gilles Peskine20628592019-04-19 19:29:50 +0200224/** Declare usage flags for a key.
225 *
226 * Usage flags are part of a key's usage policy. They encode what
227 * kind of operations are permitted on the key. For more details,
228 * refer to the documentation of the type #psa_key_usage_t.
229 *
230 * This function overwrites any usage flags
231 * previously set in \p attributes.
232 *
233 * This function may be declared as `static` (i.e. without external
234 * linkage). This function may be provided as a function-like macro,
235 * but in this case it must evaluate each of its arguments exactly once.
236 *
237 * \param[out] attributes The attribute structure to write to.
238 * \param usage_flags The usage flags to write.
239 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200240static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
241 psa_key_usage_t usage_flags);
242
Gilles Peskine20628592019-04-19 19:29:50 +0200243/** Retrieve the usage flags from key attributes.
244 *
245 * This function may be declared as `static` (i.e. without external
246 * linkage). This function may be provided as a function-like macro,
247 * but in this case it must evaluate its argument exactly once.
248 *
249 * \param[in] attributes The key attribute structure to query.
250 *
251 * \return The usage flags stored in the attribute structure.
252 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200253static psa_key_usage_t psa_get_key_usage_flags(
254 const psa_key_attributes_t *attributes);
255
Gilles Peskine20628592019-04-19 19:29:50 +0200256/** Declare the permitted algorithm policy for a key.
257 *
258 * The permitted algorithm policy of a key encodes which algorithm or
Gilles Peskinea170d922019-09-12 16:59:37 +0200259 * algorithms are permitted to be used with this key. The following
260 * algorithm policies are supported:
261 * - 0 does not allow any cryptographic operation with the key. The key
262 * may be used for non-cryptographic actions such as exporting (if
263 * permitted by the usage flags).
264 * - An algorithm value permits this particular algorithm.
265 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
266 * signature scheme with any hash algorithm.
Gilles Peskine20628592019-04-19 19:29:50 +0200267 *
268 * This function overwrites any algorithm policy
269 * previously set in \p attributes.
270 *
271 * This function may be declared as `static` (i.e. without external
272 * linkage). This function may be provided as a function-like macro,
273 * but in this case it must evaluate each of its arguments exactly once.
274 *
275 * \param[out] attributes The attribute structure to write to.
276 * \param alg The permitted algorithm policy to write.
277 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200278static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
279 psa_algorithm_t alg);
280
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100281
Gilles Peskine20628592019-04-19 19:29:50 +0200282/** Retrieve the algorithm policy from key attributes.
283 *
284 * This function may be declared as `static` (i.e. without external
285 * linkage). This function may be provided as a function-like macro,
286 * but in this case it must evaluate its argument exactly once.
287 *
288 * \param[in] attributes The key attribute structure to query.
289 *
290 * \return The algorithm stored in the attribute structure.
291 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200292static psa_algorithm_t psa_get_key_algorithm(
293 const psa_key_attributes_t *attributes);
294
Gilles Peskine20628592019-04-19 19:29:50 +0200295/** Declare the type of a key.
296 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200297 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200298 * previously set in \p attributes.
299 *
300 * This function may be declared as `static` (i.e. without external
301 * linkage). This function may be provided as a function-like macro,
302 * but in this case it must evaluate each of its arguments exactly once.
303 *
304 * \param[out] attributes The attribute structure to write to.
305 * \param type The key type to write.
Gilles Peskinea170d922019-09-12 16:59:37 +0200306 * If this is 0, the key type in \p attributes
307 * becomes unspecified.
Gilles Peskine20628592019-04-19 19:29:50 +0200308 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200309static void psa_set_key_type(psa_key_attributes_t *attributes,
310 psa_key_type_t type);
311
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100312
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200313/** Declare the size of a key.
314 *
315 * This function overwrites any key size previously set in \p attributes.
316 *
317 * This function may be declared as `static` (i.e. without external
318 * linkage). This function may be provided as a function-like macro,
319 * but in this case it must evaluate each of its arguments exactly once.
320 *
321 * \param[out] attributes The attribute structure to write to.
322 * \param bits The key size in bits.
Gilles Peskinea170d922019-09-12 16:59:37 +0200323 * If this is 0, the key size in \p attributes
Gilles Peskine05c900b2019-09-12 18:29:43 +0200324 * becomes unspecified. Keys of size 0 are
325 * not supported.
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200326 */
327static void psa_set_key_bits(psa_key_attributes_t *attributes,
328 size_t bits);
329
Gilles Peskine20628592019-04-19 19:29:50 +0200330/** Retrieve the key type from key attributes.
331 *
332 * This function may be declared as `static` (i.e. without external
333 * linkage). This function may be provided as a function-like macro,
334 * but in this case it must evaluate its argument exactly once.
335 *
336 * \param[in] attributes The key attribute structure to query.
337 *
338 * \return The key type stored in the attribute structure.
339 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200340static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
341
Gilles Peskine20628592019-04-19 19:29:50 +0200342/** Retrieve the key size from key attributes.
343 *
344 * This function may be declared as `static` (i.e. without external
345 * linkage). This function may be provided as a function-like macro,
346 * but in this case it must evaluate its argument exactly once.
347 *
348 * \param[in] attributes The key attribute structure to query.
349 *
350 * \return The key size stored in the attribute structure, in bits.
351 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200352static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
353
Gilles Peskine20628592019-04-19 19:29:50 +0200354/** Retrieve the attributes of a key.
355 *
356 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200357 * psa_reset_key_attributes(). It then copies the attributes of
358 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200359 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200360 * \note This function may allocate memory or other resources.
361 * Once you have called this function on an attribute structure,
362 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200363 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200364 * \param[in] key Identifier of the key to query.
Gilles Peskine20628592019-04-19 19:29:50 +0200365 * \param[in,out] attributes On success, the attributes of the key.
366 * On failure, equivalent to a
367 * freshly-initialized structure.
368 *
369 * \retval #PSA_SUCCESS
370 * \retval #PSA_ERROR_INVALID_HANDLE
371 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
372 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100373 * \retval #PSA_ERROR_CORRUPTION_DETECTED
374 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100375 * \retval #PSA_ERROR_DATA_CORRUPT
376 * \retval #PSA_ERROR_DATA_INVALID
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100377 * \retval #PSA_ERROR_BAD_STATE
378 * The library has not been previously initialized by psa_crypto_init().
379 * It is implementation-dependent whether a failure to initialize
380 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200381 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200382psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
Gilles Peskine4747d192019-04-17 15:05:45 +0200383 psa_key_attributes_t *attributes);
384
Gilles Peskine20628592019-04-19 19:29:50 +0200385/** Reset a key attribute structure to a freshly initialized state.
386 *
387 * You must initialize the attribute structure as described in the
388 * documentation of the type #psa_key_attributes_t before calling this
389 * function. Once the structure has been initialized, you may call this
390 * function at any time.
391 *
392 * This function frees any auxiliary resources that the structure
393 * may contain.
394 *
395 * \param[in,out] attributes The attribute structure to reset.
396 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200397void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200398
Gilles Peskine87a5e562019-04-17 12:28:25 +0200399/**@}*/
400
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100401/** \defgroup key_management Key management
402 * @{
403 */
404
Ronald Cron277a85f2020-08-04 15:49:48 +0200405/** Remove non-essential copies of key material from memory.
406 *
407 * If the key identifier designates a volatile key, this functions does not do
408 * anything and returns successfully.
409 *
410 * If the key identifier designates a persistent key, then this function will
411 * free all resources associated with the key in volatile memory. The key
412 * data in persistent storage is not affected and the key can still be used.
413 *
414 * \param key Identifier of the key to purge.
415 *
416 * \retval #PSA_SUCCESS
417 * The key material will have been removed from memory if it is not
418 * currently required.
419 * \retval #PSA_ERROR_INVALID_ARGUMENT
420 * \p key is not a valid key identifier.
421 * \retval #PSA_ERROR_BAD_STATE
422 * The library has not been previously initialized by psa_crypto_init().
423 * It is implementation-dependent whether a failure to initialize
424 * results in this error code.
425 */
426psa_status_t psa_purge_key(mbedtls_svc_key_id_t key);
427
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100428/** Make a copy of a key.
429 *
430 * Copy key material from one location to another.
431 *
432 * This function is primarily useful to copy a key from one location
433 * to another, since it populates a key using the material from
434 * another key which may have a different lifetime.
435 *
436 * This function may be used to share a key with a different party,
437 * subject to implementation-defined restrictions on key sharing.
438 *
439 * The policy on the source key must have the usage flag
440 * #PSA_KEY_USAGE_COPY set.
441 * This flag is sufficient to permit the copy if the key has the lifetime
442 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
443 * Some secure elements do not provide a way to copy a key without
444 * making it extractable from the secure element. If a key is located
445 * in such a secure element, then the key must have both usage flags
446 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
447 * a copy of the key outside the secure element.
448 *
449 * The resulting key may only be used in a way that conforms to
450 * both the policy of the original key and the policy specified in
451 * the \p attributes parameter:
452 * - The usage flags on the resulting key are the bitwise-and of the
453 * usage flags on the source policy and the usage flags in \p attributes.
454 * - If both allow the same algorithm or wildcard-based
455 * algorithm policy, the resulting key has the same algorithm policy.
456 * - If either of the policies allows an algorithm and the other policy
457 * allows a wildcard-based algorithm policy that includes this algorithm,
458 * the resulting key allows the same algorithm.
459 * - If the policies do not allow any algorithm in common, this function
460 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
461 *
462 * The effect of this function on implementation-defined attributes is
463 * implementation-defined.
464 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200465 * \param source_key The key to copy. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +0200466 * #PSA_KEY_USAGE_COPY. If a private or secret key is
Ronald Croncf56a0a2020-08-04 09:51:30 +0200467 * being copied outside of a secure element it must
Ronald Cron96783552020-10-19 12:06:30 +0200468 * also allow #PSA_KEY_USAGE_EXPORT.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100469 * \param[in] attributes The attributes for the new key.
470 * They are used as follows:
471 * - The key type and size may be 0. If either is
472 * nonzero, it must match the corresponding
473 * attribute of the source key.
474 * - The key location (the lifetime and, for
475 * persistent keys, the key identifier) is
476 * used directly.
477 * - The policy constraints (usage flags and
478 * algorithm policy) are combined from
479 * the source key and \p attributes so that
480 * both sets of restrictions apply, as
481 * described in the documentation of this function.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200482 * \param[out] target_key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +0200483 * key. For persistent keys, this is the key
484 * identifier defined in \p attributes.
485 * \c 0 on failure.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100486 *
487 * \retval #PSA_SUCCESS
488 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200489 * \p source_key is invalid.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100490 * \retval #PSA_ERROR_ALREADY_EXISTS
491 * This is an attempt to create a persistent key, and there is
492 * already a persistent key with the given identifier.
493 * \retval #PSA_ERROR_INVALID_ARGUMENT
494 * The lifetime or identifier in \p attributes are invalid.
495 * \retval #PSA_ERROR_INVALID_ARGUMENT
496 * The policy constraints on the source and specified in
497 * \p attributes are incompatible.
498 * \retval #PSA_ERROR_INVALID_ARGUMENT
499 * \p attributes specifies a key type or key size
500 * which does not match the attributes of the source key.
501 * \retval #PSA_ERROR_NOT_PERMITTED
502 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
503 * \retval #PSA_ERROR_NOT_PERMITTED
504 * The source key is not exportable and its lifetime does not
505 * allow copying it to the target's lifetime.
506 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
507 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
508 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
509 * \retval #PSA_ERROR_HARDWARE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100510 * \retval #PSA_ERROR_DATA_INVALID
511 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100512 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100513 * \retval #PSA_ERROR_CORRUPTION_DETECTED
514 * \retval #PSA_ERROR_BAD_STATE
515 * The library has not been previously initialized by psa_crypto_init().
516 * It is implementation-dependent whether a failure to initialize
517 * results in this error code.
518 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200519psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100520 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200521 mbedtls_svc_key_id_t *target_key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100522
523
524/**
525 * \brief Destroy a key.
526 *
527 * This function destroys a key from both volatile
528 * memory and, if applicable, non-volatile storage. Implementations shall
529 * make a best effort to ensure that that the key material cannot be recovered.
530 *
531 * This function also erases any metadata such as policies and frees
Ronald Croncf56a0a2020-08-04 09:51:30 +0200532 * resources associated with the key.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100533 *
534 * If a key is currently in use in a multipart operation, then destroying the
535 * key will cause the multipart operation to fail.
536 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200537 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
Ronald Cron96783552020-10-19 12:06:30 +0200538 * return #PSA_SUCCESS.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100539 *
540 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +0200541 * \p key was a valid identifier and the key material that it
542 * referred to has been erased. Alternatively, \p key is \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100543 * \retval #PSA_ERROR_NOT_PERMITTED
544 * The key cannot be erased because it is
545 * read-only, either due to a policy or due to physical restrictions.
546 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200547 * \p key is not a valid identifier nor \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100548 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
549 * There was an failure in communication with the cryptoprocessor.
550 * The key material may still be present in the cryptoprocessor.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100551 * \retval #PSA_ERROR_DATA_INVALID
gabor-mezei-arm86326a92020-11-30 16:50:34 +0100552 * This error is typically a result of either storage corruption on a
553 * cleartext storage backend, or an attempt to read data that was
554 * written by an incompatible version of the library.
555 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100556 * The storage is corrupted. Implementations shall make a best effort
557 * to erase key material even in this stage, however applications
558 * should be aware that it may be impossible to guarantee that the
559 * key material is not recoverable in such cases.
560 * \retval #PSA_ERROR_CORRUPTION_DETECTED
561 * An unexpected condition which is not a storage corruption or
562 * a communication failure occurred. The cryptoprocessor may have
563 * been compromised.
564 * \retval #PSA_ERROR_BAD_STATE
565 * The library has not been previously initialized by psa_crypto_init().
566 * It is implementation-dependent whether a failure to initialize
567 * results in this error code.
568 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200569psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100570
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100571/**@}*/
572
573/** \defgroup import_export Key import and export
574 * @{
575 */
576
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100577/**
578 * \brief Import a key in binary format.
579 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100580 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100581 * documentation of psa_export_public_key() for the format of public keys
582 * and to the documentation of psa_export_key() for the format for
583 * other key types.
584 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200585 * The key data determines the key size. The attributes may optionally
586 * specify a key size; in this case it must match the size determined
587 * from the key data. A key size of 0 in \p attributes indicates that
588 * the key size is solely determined by the key data.
589 *
590 * Implementations must reject an attempt to import a key of size 0.
591 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100592 * This specification supports a single format for each key type.
593 * Implementations may support other formats as long as the standard
594 * format is supported. Implementations that support other formats
595 * should ensure that the formats are clearly unambiguous so as to
596 * minimize the risk that an invalid input is accidentally interpreted
597 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100598 *
Gilles Peskine20628592019-04-19 19:29:50 +0200599 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200600 * The key size is always determined from the
601 * \p data buffer.
602 * If the key size in \p attributes is nonzero,
603 * it must be equal to the size from \p data.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200604 * \param[out] key On success, an identifier to the newly created key.
Ronald Cron4067d1c2020-10-19 13:34:38 +0200605 * For persistent keys, this is the key identifier
606 * defined in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200607 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100608 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200609 * buffer is interpreted according to the type declared
610 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200611 * All implementations must support at least the format
612 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100613 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200614 * the chosen type. Implementations may allow other
615 * formats, but should be conservative: implementations
616 * should err on the side of rejecting content if it
617 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200618 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100619 *
Gilles Peskine28538492018-07-11 17:34:00 +0200620 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100621 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100622 * If the key is persistent, the key material and the key's metadata
623 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200624 * \retval #PSA_ERROR_ALREADY_EXISTS
625 * This is an attempt to create a persistent key, and there is
626 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200627 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200628 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200629 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200630 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200631 * The key attributes, as a whole, are invalid.
632 * \retval #PSA_ERROR_INVALID_ARGUMENT
633 * The key data is not correctly formatted.
634 * \retval #PSA_ERROR_INVALID_ARGUMENT
635 * The size in \p attributes is nonzero and does not match the size
636 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200637 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
638 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
639 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100640 * \retval #PSA_ERROR_DATA_CORRUPT
641 * \retval #PSA_ERROR_DATA_INVALID
Darryl Greend49a4992018-06-18 17:27:26 +0100642 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200643 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200644 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300645 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300646 * The library has not been previously initialized by psa_crypto_init().
647 * It is implementation-dependent whether a failure to initialize
648 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100649 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200650psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100651 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200652 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200653 mbedtls_svc_key_id_t *key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100654
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100655
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100656
657/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100658 * \brief Export a key in binary format.
659 *
660 * The output of this function can be passed to psa_import_key() to
661 * create an equivalent object.
662 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100663 * If the implementation of psa_import_key() supports other formats
664 * beyond the format specified here, the output from psa_export_key()
665 * must use the representation specified here, not the original
666 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100667 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100668 * For standard key types, the output format is as follows:
669 *
670 * - For symmetric keys (including MAC keys), the format is the
671 * raw bytes of the key.
672 * - For DES, the key data consists of 8 bytes. The parity bits must be
673 * correct.
674 * - For Triple-DES, the format is the concatenation of the
675 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200676 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200677 * is the non-encrypted DER encoding of the representation defined by
678 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
679 * ```
680 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200681 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200682 * modulus INTEGER, -- n
683 * publicExponent INTEGER, -- e
684 * privateExponent INTEGER, -- d
685 * prime1 INTEGER, -- p
686 * prime2 INTEGER, -- q
687 * exponent1 INTEGER, -- d mod (p-1)
688 * exponent2 INTEGER, -- d mod (q-1)
689 * coefficient INTEGER, -- (inverse of q) mod p
690 * }
691 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200692 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200693 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100694 * a representation of the private value as a `ceiling(m/8)`-byte string
695 * where `m` is the bit size associated with the curve, i.e. the bit size
696 * of the order of the curve's coordinate field. This byte string is
697 * in little-endian order for Montgomery curves (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +0100698 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
699 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
700 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
Steven Cooreman6f5cc712020-06-11 16:40:41 +0200701 * For Weierstrass curves, this is the content of the `privateKey` field of
702 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
703 * the format is defined by RFC 7748, and output is masked according to §5.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200704 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200705 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000706 * format is the representation of the private key `x` as a big-endian byte
707 * string. The length of the byte string is the private key size in bytes
708 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200709 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
710 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100711 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200712 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
713 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200714 * \param key Identifier of the key to export. It must allow the
Ronald Cron96783552020-10-19 12:06:30 +0200715 * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
Ronald Croncf56a0a2020-08-04 09:51:30 +0200716 * key.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200717 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200718 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200719 * \param[out] data_length On success, the number of bytes
720 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100721 *
Gilles Peskine28538492018-07-11 17:34:00 +0200722 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100723 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200724 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200725 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100726 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200727 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
728 * The size of the \p data buffer is too small. You can determine a
729 * sufficient buffer size by calling
730 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
731 * where \c type is the key type
732 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200733 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
734 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200735 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100736 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100737 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300738 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300739 * The library has not been previously initialized by psa_crypto_init().
740 * It is implementation-dependent whether a failure to initialize
741 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100742 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200743psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100744 uint8_t *data,
745 size_t data_size,
746 size_t *data_length);
747
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100748/**
749 * \brief Export a public key or the public part of a key pair in binary format.
750 *
751 * The output of this function can be passed to psa_import_key() to
752 * create an object that is equivalent to the public key.
753 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000754 * This specification supports a single format for each key type.
755 * Implementations may support other formats as long as the standard
756 * format is supported. Implementations that support other formats
757 * should ensure that the formats are clearly unambiguous so as to
758 * minimize the risk that an invalid input is accidentally interpreted
759 * according to a different format.
760 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000761 * For standard key types, the output format is as follows:
762 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
763 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
764 * ```
765 * RSAPublicKey ::= SEQUENCE {
766 * modulus INTEGER, -- n
767 * publicExponent INTEGER } -- e
768 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000769 * - For elliptic curve public keys (key types for which
770 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
771 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
772 * Let `m` be the bit size associated with the curve, i.e. the bit size of
773 * `q` for a curve over `F_q`. The representation consists of:
774 * - The byte 0x04;
775 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
776 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200777 * - For Diffie-Hellman key exchange public keys (key types for which
778 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000779 * the format is the representation of the public key `y = g^x mod p` as a
780 * big-endian byte string. The length of the byte string is the length of the
781 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100782 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200783 * Exporting a public key object or the public part of a key pair is
784 * always permitted, regardless of the key's usage flags.
785 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200786 * \param key Identifier of the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200787 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200788 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200789 * \param[out] data_length On success, the number of bytes
790 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100791 *
Gilles Peskine28538492018-07-11 17:34:00 +0200792 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100793 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200794 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200795 * The key is neither a public key nor a key pair.
796 * \retval #PSA_ERROR_NOT_SUPPORTED
797 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
798 * The size of the \p data buffer is too small. You can determine a
799 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200800 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200801 * where \c type is the key type
802 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200803 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
804 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200805 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100806 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100807 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300808 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300809 * The library has not been previously initialized by psa_crypto_init().
810 * It is implementation-dependent whether a failure to initialize
811 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100812 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200813psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100814 uint8_t *data,
815 size_t data_size,
816 size_t *data_length);
817
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100818
Gilles Peskine20035e32018-02-03 22:44:14 +0100819
820/**@}*/
821
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100822/** \defgroup hash Message digests
823 * @{
824 */
825
Gilles Peskine69647a42019-01-14 20:18:12 +0100826/** Calculate the hash (digest) of a message.
827 *
828 * \note To verify the hash of a message against an
829 * expected value, use psa_hash_compare() instead.
830 *
831 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
832 * such that #PSA_ALG_IS_HASH(\p alg) is true).
833 * \param[in] input Buffer containing the message to hash.
834 * \param input_length Size of the \p input buffer in bytes.
835 * \param[out] hash Buffer where the hash is to be written.
836 * \param hash_size Size of the \p hash buffer in bytes.
837 * \param[out] hash_length On success, the number of bytes
838 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100839 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100840 *
841 * \retval #PSA_SUCCESS
842 * Success.
843 * \retval #PSA_ERROR_NOT_SUPPORTED
844 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100845 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100846 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
847 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100848 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
849 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
850 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200851 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100852 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100853 * \retval #PSA_ERROR_BAD_STATE
854 * The library has not been previously initialized by psa_crypto_init().
855 * It is implementation-dependent whether a failure to initialize
856 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100857 */
858psa_status_t psa_hash_compute(psa_algorithm_t alg,
859 const uint8_t *input,
860 size_t input_length,
861 uint8_t *hash,
862 size_t hash_size,
863 size_t *hash_length);
864
865/** Calculate the hash (digest) of a message and compare it with a
866 * reference value.
867 *
868 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
869 * such that #PSA_ALG_IS_HASH(\p alg) is true).
870 * \param[in] input Buffer containing the message to hash.
871 * \param input_length Size of the \p input buffer in bytes.
872 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100873 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100874 *
875 * \retval #PSA_SUCCESS
876 * The expected hash is identical to the actual hash of the input.
877 * \retval #PSA_ERROR_INVALID_SIGNATURE
878 * The hash of the message was calculated successfully, but it
879 * differs from the expected hash.
880 * \retval #PSA_ERROR_NOT_SUPPORTED
881 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100882 * \retval #PSA_ERROR_INVALID_ARGUMENT
883 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100884 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
885 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
886 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200887 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100888 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100889 * \retval #PSA_ERROR_BAD_STATE
890 * The library has not been previously initialized by psa_crypto_init().
891 * It is implementation-dependent whether a failure to initialize
892 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100893 */
894psa_status_t psa_hash_compare(psa_algorithm_t alg,
895 const uint8_t *input,
896 size_t input_length,
897 const uint8_t *hash,
Gilles Peskinefa710f52019-12-02 14:31:48 +0100898 size_t hash_length);
Gilles Peskine69647a42019-01-14 20:18:12 +0100899
Gilles Peskine308b91d2018-02-08 09:47:44 +0100900/** The type of the state data structure for multipart hash operations.
901 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000902 * Before calling any function on a hash operation object, the application must
903 * initialize it by any of the following means:
904 * - Set the structure to all-bits-zero, for example:
905 * \code
906 * psa_hash_operation_t operation;
907 * memset(&operation, 0, sizeof(operation));
908 * \endcode
909 * - Initialize the structure to logical zero values, for example:
910 * \code
911 * psa_hash_operation_t operation = {0};
912 * \endcode
913 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
914 * for example:
915 * \code
916 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
917 * \endcode
918 * - Assign the result of the function psa_hash_operation_init()
919 * to the structure, for example:
920 * \code
921 * psa_hash_operation_t operation;
922 * operation = psa_hash_operation_init();
923 * \endcode
924 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100925 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100926 * make any assumptions about the content of this structure except
927 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100928typedef struct psa_hash_operation_s psa_hash_operation_t;
929
Jaeden Amero6a25b412019-01-04 11:47:44 +0000930/** \def PSA_HASH_OPERATION_INIT
931 *
932 * This macro returns a suitable initializer for a hash operation object
933 * of type #psa_hash_operation_t.
934 */
935#ifdef __DOXYGEN_ONLY__
936/* This is an example definition for documentation purposes.
937 * Implementations should define a suitable value in `crypto_struct.h`.
938 */
939#define PSA_HASH_OPERATION_INIT {0}
940#endif
941
942/** Return an initial value for a hash operation object.
943 */
944static psa_hash_operation_t psa_hash_operation_init(void);
945
Gilles Peskinef45adda2019-01-14 18:29:18 +0100946/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100947 *
948 * The sequence of operations to calculate a hash (message digest)
949 * is as follows:
950 * -# Allocate an operation object which will be passed to all the functions
951 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000952 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100953 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200954 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100955 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100956 * of the message each time. The hash that is calculated is the hash
957 * of the concatenation of these messages in order.
958 * -# To calculate the hash, call psa_hash_finish().
959 * To compare the hash with an expected value, call psa_hash_verify().
960 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100961 * If an error occurs at any step after a call to psa_hash_setup(), the
962 * operation will need to be reset by a call to psa_hash_abort(). The
963 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000964 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100965 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200966 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100967 * eventually terminate the operation. The following events terminate an
968 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100969 * - A successful call to psa_hash_finish() or psa_hash_verify().
970 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100971 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000972 * \param[in,out] operation The operation object to set up. It must have
973 * been initialized as per the documentation for
974 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200975 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
976 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100977 *
Gilles Peskine28538492018-07-11 17:34:00 +0200978 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100979 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200980 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100981 * \p alg is not a supported hash algorithm.
982 * \retval #PSA_ERROR_INVALID_ARGUMENT
983 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100984 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100985 * The operation state is not valid (it must be inactive).
Gilles Peskine28538492018-07-11 17:34:00 +0200986 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
987 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
988 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200989 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100990 * \retval #PSA_ERROR_BAD_STATE
991 * The library has not been previously initialized by psa_crypto_init().
992 * It is implementation-dependent whether a failure to initialize
993 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100994 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200995psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100996 psa_algorithm_t alg);
997
Gilles Peskine308b91d2018-02-08 09:47:44 +0100998/** Add a message fragment to a multipart hash operation.
999 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001000 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001001 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001002 * If this function returns an error status, the operation enters an error
1003 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001004 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001005 * \param[in,out] operation Active hash operation.
1006 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001007 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001008 *
Gilles Peskine28538492018-07-11 17:34:00 +02001009 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001010 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001011 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001012 * The operation state is not valid (it muct be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001013 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1014 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1015 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001016 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001017 * \retval #PSA_ERROR_BAD_STATE
1018 * The library has not been previously initialized by psa_crypto_init().
1019 * It is implementation-dependent whether a failure to initialize
1020 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001021 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001022psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1023 const uint8_t *input,
1024 size_t input_length);
1025
Gilles Peskine308b91d2018-02-08 09:47:44 +01001026/** Finish the calculation of the hash of a message.
1027 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001028 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001029 * This function calculates the hash of the message formed by concatenating
1030 * the inputs passed to preceding calls to psa_hash_update().
1031 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001032 * When this function returns successfuly, the operation becomes inactive.
1033 * If this function returns an error status, the operation enters an error
1034 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001035 *
1036 * \warning Applications should not call this function if they expect
1037 * a specific value for the hash. Call psa_hash_verify() instead.
1038 * Beware that comparing integrity or authenticity data such as
1039 * hash values with a function such as \c memcmp is risky
1040 * because the time taken by the comparison may leak information
1041 * about the hashed data which could allow an attacker to guess
1042 * a valid hash and thereby bypass security controls.
1043 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001044 * \param[in,out] operation Active hash operation.
1045 * \param[out] hash Buffer where the hash is to be written.
1046 * \param hash_size Size of the \p hash buffer in bytes.
1047 * \param[out] hash_length On success, the number of bytes
1048 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001049 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001050 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001051 *
Gilles Peskine28538492018-07-11 17:34:00 +02001052 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001053 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001054 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001055 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001056 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001057 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001058 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001059 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001060 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1061 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1062 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001063 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001064 * \retval #PSA_ERROR_BAD_STATE
1065 * The library has not been previously initialized by psa_crypto_init().
1066 * It is implementation-dependent whether a failure to initialize
1067 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001068 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001069psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1070 uint8_t *hash,
1071 size_t hash_size,
1072 size_t *hash_length);
1073
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074/** Finish the calculation of the hash of a message and compare it with
1075 * an expected value.
1076 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001077 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001078 * This function calculates the hash of the message formed by concatenating
1079 * the inputs passed to preceding calls to psa_hash_update(). It then
1080 * compares the calculated hash with the expected hash passed as a
1081 * parameter to this function.
1082 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001083 * When this function returns successfuly, the operation becomes inactive.
1084 * If this function returns an error status, the operation enters an error
1085 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001086 *
Gilles Peskine19067982018-03-20 17:54:53 +01001087 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001088 * comparison between the actual hash and the expected hash is performed
1089 * in constant time.
1090 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001091 * \param[in,out] operation Active hash operation.
1092 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001093 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001094 *
Gilles Peskine28538492018-07-11 17:34:00 +02001095 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001096 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001097 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001098 * The hash of the message was calculated successfully, but it
1099 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001100 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001101 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001102 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1103 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1104 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001105 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001106 * \retval #PSA_ERROR_BAD_STATE
1107 * The library has not been previously initialized by psa_crypto_init().
1108 * It is implementation-dependent whether a failure to initialize
1109 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001110 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001111psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1112 const uint8_t *hash,
1113 size_t hash_length);
1114
Gilles Peskine308b91d2018-02-08 09:47:44 +01001115/** Abort a hash operation.
1116 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001117 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001118 * \p operation structure itself. Once aborted, the operation object
1119 * can be reused for another operation by calling
1120 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001121 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001122 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001123 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001124 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001125 * In particular, calling psa_hash_abort() after the operation has been
1126 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1127 * psa_hash_verify() is safe and has no effect.
1128 *
1129 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001130 *
Gilles Peskine28538492018-07-11 17:34:00 +02001131 * \retval #PSA_SUCCESS
Gilles Peskine28538492018-07-11 17:34:00 +02001132 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1133 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001134 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001135 * \retval #PSA_ERROR_BAD_STATE
1136 * The library has not been previously initialized by psa_crypto_init().
1137 * It is implementation-dependent whether a failure to initialize
1138 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001139 */
1140psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001141
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001142/** Clone a hash operation.
1143 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001144 * This function copies the state of an ongoing hash operation to
1145 * a new operation object. In other words, this function is equivalent
1146 * to calling psa_hash_setup() on \p target_operation with the same
1147 * algorithm that \p source_operation was set up for, then
1148 * psa_hash_update() on \p target_operation with the same input that
1149 * that was passed to \p source_operation. After this function returns, the
1150 * two objects are independent, i.e. subsequent calls involving one of
1151 * the objects do not affect the other object.
1152 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001153 * \param[in] source_operation The active hash operation to clone.
1154 * \param[in,out] target_operation The operation object to set up.
1155 * It must be initialized but not active.
1156 *
1157 * \retval #PSA_SUCCESS
1158 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001159 * The \p source_operation state is not valid (it must be active).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001160 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001161 * The \p target_operation state is not valid (it must be inactive).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001162 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1163 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001164 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001165 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001166 * \retval #PSA_ERROR_BAD_STATE
1167 * The library has not been previously initialized by psa_crypto_init().
1168 * It is implementation-dependent whether a failure to initialize
1169 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001170 */
1171psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1172 psa_hash_operation_t *target_operation);
1173
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001174/**@}*/
1175
Gilles Peskine8c9def32018-02-08 10:02:12 +01001176/** \defgroup MAC Message authentication codes
1177 * @{
1178 */
1179
Gilles Peskine69647a42019-01-14 20:18:12 +01001180/** Calculate the MAC (message authentication code) of a message.
1181 *
1182 * \note To verify the MAC of a message against an
1183 * expected value, use psa_mac_verify() instead.
1184 * Beware that comparing integrity or authenticity data such as
1185 * MAC values with a function such as \c memcmp is risky
1186 * because the time taken by the comparison may leak information
1187 * about the MAC value which could allow an attacker to guess
1188 * a valid MAC and thereby bypass security controls.
1189 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001190 * \param key Identifier of the key to use for the operation. It
1191 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001192 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001193 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001194 * \param[in] input Buffer containing the input message.
1195 * \param input_length Size of the \p input buffer in bytes.
1196 * \param[out] mac Buffer where the MAC value is to be written.
1197 * \param mac_size Size of the \p mac buffer in bytes.
1198 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001199 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001200 *
1201 * \retval #PSA_SUCCESS
1202 * Success.
1203 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001204 * \retval #PSA_ERROR_NOT_PERMITTED
1205 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001206 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001207 * \retval #PSA_ERROR_NOT_SUPPORTED
1208 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001209 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1210 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001211 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1212 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1213 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001214 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001215 * \retval #PSA_ERROR_STORAGE_FAILURE
1216 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001217 * \retval #PSA_ERROR_BAD_STATE
1218 * The library has not been previously initialized by psa_crypto_init().
1219 * It is implementation-dependent whether a failure to initialize
1220 * results in this error code.
1221 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001222psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001223 psa_algorithm_t alg,
1224 const uint8_t *input,
1225 size_t input_length,
1226 uint8_t *mac,
1227 size_t mac_size,
1228 size_t *mac_length);
1229
1230/** Calculate the MAC of a message and compare it with a reference value.
1231 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001232 * \param key Identifier of the key to use for the operation. It
1233 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001234 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001235 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001236 * \param[in] input Buffer containing the input message.
1237 * \param input_length Size of the \p input buffer in bytes.
1238 * \param[out] mac Buffer containing the expected MAC value.
1239 * \param mac_length Size of the \p mac buffer in bytes.
1240 *
1241 * \retval #PSA_SUCCESS
1242 * The expected MAC is identical to the actual MAC of the input.
1243 * \retval #PSA_ERROR_INVALID_SIGNATURE
1244 * The MAC of the message was calculated successfully, but it
1245 * differs from the expected value.
1246 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001247 * \retval #PSA_ERROR_NOT_PERMITTED
1248 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001249 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001250 * \retval #PSA_ERROR_NOT_SUPPORTED
1251 * \p alg is not supported or is not a MAC algorithm.
1252 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1253 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1254 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001255 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001256 * \retval #PSA_ERROR_STORAGE_FAILURE
1257 * The key could not be retrieved from storage.
1258 * \retval #PSA_ERROR_BAD_STATE
1259 * The library has not been previously initialized by psa_crypto_init().
1260 * It is implementation-dependent whether a failure to initialize
1261 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001262 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001263psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
Gilles Peskinea05602d2019-01-17 15:25:52 +01001264 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001265 const uint8_t *input,
1266 size_t input_length,
1267 const uint8_t *mac,
Gilles Peskine13faa2d2020-01-30 16:32:21 +01001268 size_t mac_length);
Gilles Peskine69647a42019-01-14 20:18:12 +01001269
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001270/** The type of the state data structure for multipart MAC operations.
1271 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001272 * Before calling any function on a MAC operation object, the application must
1273 * initialize it by any of the following means:
1274 * - Set the structure to all-bits-zero, for example:
1275 * \code
1276 * psa_mac_operation_t operation;
1277 * memset(&operation, 0, sizeof(operation));
1278 * \endcode
1279 * - Initialize the structure to logical zero values, for example:
1280 * \code
1281 * psa_mac_operation_t operation = {0};
1282 * \endcode
1283 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1284 * for example:
1285 * \code
1286 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1287 * \endcode
1288 * - Assign the result of the function psa_mac_operation_init()
1289 * to the structure, for example:
1290 * \code
1291 * psa_mac_operation_t operation;
1292 * operation = psa_mac_operation_init();
1293 * \endcode
1294 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001295 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001296 * make any assumptions about the content of this structure except
1297 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001298typedef struct psa_mac_operation_s psa_mac_operation_t;
1299
Jaeden Amero769ce272019-01-04 11:48:03 +00001300/** \def PSA_MAC_OPERATION_INIT
1301 *
1302 * This macro returns a suitable initializer for a MAC operation object of type
1303 * #psa_mac_operation_t.
1304 */
1305#ifdef __DOXYGEN_ONLY__
1306/* This is an example definition for documentation purposes.
1307 * Implementations should define a suitable value in `crypto_struct.h`.
1308 */
1309#define PSA_MAC_OPERATION_INIT {0}
1310#endif
1311
1312/** Return an initial value for a MAC operation object.
1313 */
1314static psa_mac_operation_t psa_mac_operation_init(void);
1315
Gilles Peskinef45adda2019-01-14 18:29:18 +01001316/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001317 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001318 * This function sets up the calculation of the MAC
1319 * (message authentication code) of a byte string.
1320 * To verify the MAC of a message against an
1321 * expected value, use psa_mac_verify_setup() instead.
1322 *
1323 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001324 * -# Allocate an operation object which will be passed to all the functions
1325 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001326 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001327 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001328 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001329 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1330 * of the message each time. The MAC that is calculated is the MAC
1331 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001332 * -# At the end of the message, call psa_mac_sign_finish() to finish
1333 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001334 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001335 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1336 * operation will need to be reset by a call to psa_mac_abort(). The
1337 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001338 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001339 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001340 * After a successful call to psa_mac_sign_setup(), the application must
1341 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001342 * - A successful call to psa_mac_sign_finish().
1343 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001344 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001345 * \param[in,out] operation The operation object to set up. It must have
1346 * been initialized as per the documentation for
1347 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001348 * \param key Identifier of the key to use for the operation. It
1349 * must remain valid until the operation terminates.
1350 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001351 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001352 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001353 *
Gilles Peskine28538492018-07-11 17:34:00 +02001354 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001355 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001356 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001357 * \retval #PSA_ERROR_NOT_PERMITTED
1358 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001359 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001360 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001361 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001362 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1363 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1364 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001365 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001366 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001367 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001368 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001369 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001370 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001371 * The library has not been previously initialized by psa_crypto_init().
1372 * It is implementation-dependent whether a failure to initialize
1373 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001374 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001375psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001376 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001377 psa_algorithm_t alg);
1378
Gilles Peskinef45adda2019-01-14 18:29:18 +01001379/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001380 *
1381 * This function sets up the verification of the MAC
1382 * (message authentication code) of a byte string against an expected value.
1383 *
1384 * The sequence of operations to verify a MAC is as follows:
1385 * -# Allocate an operation object which will be passed to all the functions
1386 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001387 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001388 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001389 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001390 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1391 * of the message each time. The MAC that is calculated is the MAC
1392 * of the concatenation of these messages in order.
1393 * -# At the end of the message, call psa_mac_verify_finish() to finish
1394 * calculating the actual MAC of the message and verify it against
1395 * the expected value.
1396 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001397 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1398 * operation will need to be reset by a call to psa_mac_abort(). The
1399 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001400 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001401 *
1402 * After a successful call to psa_mac_verify_setup(), the application must
1403 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001404 * - A successful call to psa_mac_verify_finish().
1405 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001406 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001407 * \param[in,out] operation The operation object to set up. It must have
1408 * been initialized as per the documentation for
1409 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001410 * \param key Identifier of the key to use for the operation. It
1411 * must remain valid until the operation terminates.
1412 * It must allow the usage
1413 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001414 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1415 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001416 *
Gilles Peskine28538492018-07-11 17:34:00 +02001417 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001418 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001419 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001420 * \retval #PSA_ERROR_NOT_PERMITTED
1421 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001422 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001423 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001424 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001425 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1426 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1427 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001428 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001429 * \retval #PSA_ERROR_STORAGE_FAILURE
1430 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001431 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001432 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001433 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001434 * The library has not been previously initialized by psa_crypto_init().
1435 * It is implementation-dependent whether a failure to initialize
1436 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001437 */
1438psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001439 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001440 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001441
Gilles Peskinedcd14942018-07-12 00:30:52 +02001442/** Add a message fragment to a multipart MAC operation.
1443 *
1444 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1445 * before calling this function.
1446 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001447 * If this function returns an error status, the operation enters an error
1448 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001449 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001450 * \param[in,out] operation Active MAC operation.
1451 * \param[in] input Buffer containing the message fragment to add to
1452 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001453 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001454 *
1455 * \retval #PSA_SUCCESS
1456 * Success.
1457 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001458 * The operation state is not valid (it must be active).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001459 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1460 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1461 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001462 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001463 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001464 * \retval #PSA_ERROR_BAD_STATE
1465 * The library has not been previously initialized by psa_crypto_init().
1466 * It is implementation-dependent whether a failure to initialize
1467 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001468 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001469psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1470 const uint8_t *input,
1471 size_t input_length);
1472
Gilles Peskinedcd14942018-07-12 00:30:52 +02001473/** Finish the calculation of the MAC of a message.
1474 *
1475 * The application must call psa_mac_sign_setup() before calling this function.
1476 * This function calculates the MAC of the message formed by concatenating
1477 * the inputs passed to preceding calls to psa_mac_update().
1478 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001479 * When this function returns successfuly, the operation becomes inactive.
1480 * If this function returns an error status, the operation enters an error
1481 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001482 *
1483 * \warning Applications should not call this function if they expect
1484 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1485 * Beware that comparing integrity or authenticity data such as
1486 * MAC values with a function such as \c memcmp is risky
1487 * because the time taken by the comparison may leak information
1488 * about the MAC value which could allow an attacker to guess
1489 * a valid MAC and thereby bypass security controls.
1490 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001491 * \param[in,out] operation Active MAC operation.
1492 * \param[out] mac Buffer where the MAC value is to be written.
1493 * \param mac_size Size of the \p mac buffer in bytes.
1494 * \param[out] mac_length On success, the number of bytes
1495 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001496 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001497 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001498 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001499 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001500 *
1501 * \retval #PSA_SUCCESS
1502 * Success.
1503 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001504 * The operation state is not valid (it must be an active mac sign
1505 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001506 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001507 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001508 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1509 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1510 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1511 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001512 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001513 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001514 * \retval #PSA_ERROR_BAD_STATE
1515 * The library has not been previously initialized by psa_crypto_init().
1516 * It is implementation-dependent whether a failure to initialize
1517 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001518 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001519psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1520 uint8_t *mac,
1521 size_t mac_size,
1522 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001523
Gilles Peskinedcd14942018-07-12 00:30:52 +02001524/** Finish the calculation of the MAC of a message and compare it with
1525 * an expected value.
1526 *
1527 * The application must call psa_mac_verify_setup() before calling this function.
1528 * This function calculates the MAC of the message formed by concatenating
1529 * the inputs passed to preceding calls to psa_mac_update(). It then
1530 * compares the calculated MAC with the expected MAC passed as a
1531 * parameter to this function.
1532 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001533 * When this function returns successfuly, the operation becomes inactive.
1534 * If this function returns an error status, the operation enters an error
1535 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001536 *
1537 * \note Implementations shall make the best effort to ensure that the
1538 * comparison between the actual MAC and the expected MAC is performed
1539 * in constant time.
1540 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001541 * \param[in,out] operation Active MAC operation.
1542 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001543 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001544 *
1545 * \retval #PSA_SUCCESS
1546 * The expected MAC is identical to the actual MAC of the message.
1547 * \retval #PSA_ERROR_INVALID_SIGNATURE
1548 * The MAC of the message was calculated successfully, but it
1549 * differs from the expected MAC.
1550 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001551 * The operation state is not valid (it must be an active mac verify
1552 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001553 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1554 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1555 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001556 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001557 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001558 * \retval #PSA_ERROR_BAD_STATE
1559 * The library has not been previously initialized by psa_crypto_init().
1560 * It is implementation-dependent whether a failure to initialize
1561 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001562 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001563psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1564 const uint8_t *mac,
1565 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001566
Gilles Peskinedcd14942018-07-12 00:30:52 +02001567/** Abort a MAC operation.
1568 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001569 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001570 * \p operation structure itself. Once aborted, the operation object
1571 * can be reused for another operation by calling
1572 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001573 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001574 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001575 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001576 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001577 * In particular, calling psa_mac_abort() after the operation has been
1578 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1579 * psa_mac_verify_finish() is safe and has no effect.
1580 *
1581 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001582 *
1583 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02001584 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1585 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001586 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001587 * \retval #PSA_ERROR_BAD_STATE
1588 * The library has not been previously initialized by psa_crypto_init().
1589 * It is implementation-dependent whether a failure to initialize
1590 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001591 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001592psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1593
1594/**@}*/
1595
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001596/** \defgroup cipher Symmetric ciphers
1597 * @{
1598 */
1599
Gilles Peskine69647a42019-01-14 20:18:12 +01001600/** Encrypt a message using a symmetric cipher.
1601 *
1602 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001603 * vector). Use the multipart operation interface with a
1604 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001605 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001606 * \param key Identifier of the key to use for the operation.
Ronald Cron96783552020-10-19 12:06:30 +02001607 * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001608 * \param alg The cipher algorithm to compute
1609 * (\c PSA_ALG_XXX value such that
1610 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1611 * \param[in] input Buffer containing the message to encrypt.
1612 * \param input_length Size of the \p input buffer in bytes.
1613 * \param[out] output Buffer where the output is to be written.
1614 * The output contains the IV followed by
1615 * the ciphertext proper.
1616 * \param output_size Size of the \p output buffer in bytes.
1617 * \param[out] output_length On success, the number of bytes
1618 * that make up the output.
1619 *
1620 * \retval #PSA_SUCCESS
1621 * Success.
1622 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001623 * \retval #PSA_ERROR_NOT_PERMITTED
1624 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001625 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001626 * \retval #PSA_ERROR_NOT_SUPPORTED
1627 * \p alg is not supported or is not a cipher algorithm.
1628 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1629 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1630 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1631 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001632 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001633 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001634 * \retval #PSA_ERROR_BAD_STATE
1635 * The library has not been previously initialized by psa_crypto_init().
1636 * It is implementation-dependent whether a failure to initialize
1637 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001638 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001639psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001640 psa_algorithm_t alg,
1641 const uint8_t *input,
1642 size_t input_length,
1643 uint8_t *output,
1644 size_t output_size,
1645 size_t *output_length);
1646
1647/** Decrypt a message using a symmetric cipher.
1648 *
1649 * This function decrypts a message encrypted with a symmetric cipher.
1650 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001651 * \param key Identifier of the key to use for the operation.
Gilles Peskine69647a42019-01-14 20:18:12 +01001652 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001653 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001654 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001655 * \param alg The cipher algorithm to compute
1656 * (\c PSA_ALG_XXX value such that
1657 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1658 * \param[in] input Buffer containing the message to decrypt.
1659 * This consists of the IV followed by the
1660 * ciphertext proper.
1661 * \param input_length Size of the \p input buffer in bytes.
1662 * \param[out] output Buffer where the plaintext is to be written.
1663 * \param output_size Size of the \p output buffer in bytes.
1664 * \param[out] output_length On success, the number of bytes
1665 * that make up the output.
1666 *
1667 * \retval #PSA_SUCCESS
1668 * Success.
1669 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001670 * \retval #PSA_ERROR_NOT_PERMITTED
1671 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001672 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001673 * \retval #PSA_ERROR_NOT_SUPPORTED
1674 * \p alg is not supported or is not a cipher algorithm.
1675 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1676 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1677 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1678 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001679 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001680 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001681 * \retval #PSA_ERROR_BAD_STATE
1682 * The library has not been previously initialized by psa_crypto_init().
1683 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001684 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001685 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001686psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001687 psa_algorithm_t alg,
1688 const uint8_t *input,
1689 size_t input_length,
1690 uint8_t *output,
1691 size_t output_size,
1692 size_t *output_length);
1693
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001694/** The type of the state data structure for multipart cipher operations.
1695 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001696 * Before calling any function on a cipher operation object, the application
1697 * must initialize it by any of the following means:
1698 * - Set the structure to all-bits-zero, for example:
1699 * \code
1700 * psa_cipher_operation_t operation;
1701 * memset(&operation, 0, sizeof(operation));
1702 * \endcode
1703 * - Initialize the structure to logical zero values, for example:
1704 * \code
1705 * psa_cipher_operation_t operation = {0};
1706 * \endcode
1707 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1708 * for example:
1709 * \code
1710 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1711 * \endcode
1712 * - Assign the result of the function psa_cipher_operation_init()
1713 * to the structure, for example:
1714 * \code
1715 * psa_cipher_operation_t operation;
1716 * operation = psa_cipher_operation_init();
1717 * \endcode
1718 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001719 * This is an implementation-defined \c struct. Applications should not
1720 * make any assumptions about the content of this structure except
1721 * as directed by the documentation of a specific implementation. */
1722typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1723
Jaeden Amero5bae2272019-01-04 11:48:27 +00001724/** \def PSA_CIPHER_OPERATION_INIT
1725 *
1726 * This macro returns a suitable initializer for a cipher operation object of
1727 * type #psa_cipher_operation_t.
1728 */
1729#ifdef __DOXYGEN_ONLY__
1730/* This is an example definition for documentation purposes.
1731 * Implementations should define a suitable value in `crypto_struct.h`.
1732 */
1733#define PSA_CIPHER_OPERATION_INIT {0}
1734#endif
1735
1736/** Return an initial value for a cipher operation object.
1737 */
1738static psa_cipher_operation_t psa_cipher_operation_init(void);
1739
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001740/** Set the key for a multipart symmetric encryption operation.
1741 *
1742 * The sequence of operations to encrypt a message with a symmetric cipher
1743 * is as follows:
1744 * -# Allocate an operation object which will be passed to all the functions
1745 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001746 * -# Initialize the operation object with one of the methods described in the
1747 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001748 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001749 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001750 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001751 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001752 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001753 * requires a specific IV value.
1754 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1755 * of the message each time.
1756 * -# Call psa_cipher_finish().
1757 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001758 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1759 * the operation will need to be reset by a call to psa_cipher_abort(). The
1760 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001761 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001762 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001763 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001764 * eventually terminate the operation. The following events terminate an
1765 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001766 * - A successful call to psa_cipher_finish().
1767 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001768 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001769 * \param[in,out] operation The operation object to set up. It must have
1770 * been initialized as per the documentation for
1771 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001772 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001773 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001774 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001775 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001776 * \param alg The cipher algorithm to compute
1777 * (\c PSA_ALG_XXX value such that
1778 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001779 *
Gilles Peskine28538492018-07-11 17:34:00 +02001780 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001781 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001782 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001783 * \retval #PSA_ERROR_NOT_PERMITTED
1784 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001785 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001786 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001787 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001788 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1789 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1790 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001791 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001792 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001793 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001794 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001795 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001796 * The library has not been previously initialized by psa_crypto_init().
1797 * It is implementation-dependent whether a failure to initialize
1798 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001799 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001800psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001801 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001802 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001803
1804/** Set the key for a multipart symmetric decryption operation.
1805 *
1806 * The sequence of operations to decrypt a message with a symmetric cipher
1807 * is as follows:
1808 * -# Allocate an operation object which will be passed to all the functions
1809 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001810 * -# Initialize the operation object with one of the methods described in the
1811 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001812 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001813 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001814 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001815 * decryption. If the IV is prepended to the ciphertext, you can call
1816 * psa_cipher_update() on a buffer containing the IV followed by the
1817 * beginning of the message.
1818 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1819 * of the message each time.
1820 * -# Call psa_cipher_finish().
1821 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001822 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1823 * the operation will need to be reset by a call to psa_cipher_abort(). The
1824 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001825 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001826 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001827 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001828 * eventually terminate the operation. The following events terminate an
1829 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001830 * - A successful call to psa_cipher_finish().
1831 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001832 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001833 * \param[in,out] operation The operation object to set up. It must have
1834 * been initialized as per the documentation for
1835 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001836 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001837 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001838 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02001839 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001840 * \param alg The cipher algorithm to compute
1841 * (\c PSA_ALG_XXX value such that
1842 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001843 *
Gilles Peskine28538492018-07-11 17:34:00 +02001844 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001845 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001846 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001847 * \retval #PSA_ERROR_NOT_PERMITTED
1848 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001849 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001850 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001851 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001852 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1853 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1854 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001855 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001856 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001857 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001858 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001859 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001860 * The library has not been previously initialized by psa_crypto_init().
1861 * It is implementation-dependent whether a failure to initialize
1862 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001863 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001864psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001865 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001866 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001867
Gilles Peskinedcd14942018-07-12 00:30:52 +02001868/** Generate an IV for a symmetric encryption operation.
1869 *
1870 * This function generates a random IV (initialization vector), nonce
1871 * or initial counter value for the encryption operation as appropriate
1872 * for the chosen algorithm, key type and key size.
1873 *
1874 * The application must call psa_cipher_encrypt_setup() before
1875 * calling this function.
1876 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001877 * If this function returns an error status, the operation enters an error
1878 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001879 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001880 * \param[in,out] operation Active cipher operation.
1881 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001882 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001883 * \param[out] iv_length On success, the number of bytes of the
1884 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001885 *
1886 * \retval #PSA_SUCCESS
1887 * Success.
1888 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001889 * The operation state is not valid (it must be active, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001890 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001891 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001892 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1893 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1894 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001895 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001896 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001897 * \retval #PSA_ERROR_BAD_STATE
1898 * The library has not been previously initialized by psa_crypto_init().
1899 * It is implementation-dependent whether a failure to initialize
1900 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001901 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001902psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001903 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001904 size_t iv_size,
1905 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001906
Gilles Peskinedcd14942018-07-12 00:30:52 +02001907/** Set the IV for a symmetric encryption or decryption operation.
1908 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001909 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001910 * or initial counter value for the encryption or decryption operation.
1911 *
1912 * The application must call psa_cipher_encrypt_setup() before
1913 * calling this function.
1914 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001915 * If this function returns an error status, the operation enters an error
1916 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001917 *
1918 * \note When encrypting, applications should use psa_cipher_generate_iv()
1919 * instead of this function, unless implementing a protocol that requires
1920 * a non-random IV.
1921 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001922 * \param[in,out] operation Active cipher operation.
1923 * \param[in] iv Buffer containing the IV to use.
1924 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001925 *
1926 * \retval #PSA_SUCCESS
1927 * Success.
1928 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001929 * The operation state is not valid (it must be an active cipher
1930 * encrypt operation, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001931 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001932 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001933 * or the chosen algorithm does not use an IV.
1934 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1935 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1936 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001937 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001938 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001939 * \retval #PSA_ERROR_BAD_STATE
1940 * The library has not been previously initialized by psa_crypto_init().
1941 * It is implementation-dependent whether a failure to initialize
1942 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001943 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001944psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001945 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001946 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001947
Gilles Peskinedcd14942018-07-12 00:30:52 +02001948/** Encrypt or decrypt a message fragment in an active cipher operation.
1949 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001950 * Before calling this function, you must:
1951 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1952 * The choice of setup function determines whether this function
1953 * encrypts or decrypts its input.
1954 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1955 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001956 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001957 * If this function returns an error status, the operation enters an error
1958 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001959 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001960 * \param[in,out] operation Active cipher operation.
1961 * \param[in] input Buffer containing the message fragment to
1962 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001963 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001964 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001965 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001966 * \param[out] output_length On success, the number of bytes
1967 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001968 *
1969 * \retval #PSA_SUCCESS
1970 * Success.
1971 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001972 * The operation state is not valid (it must be active, with an IV set
1973 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001974 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1975 * The size of the \p output buffer is too small.
1976 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1977 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1978 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001979 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001980 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001981 * \retval #PSA_ERROR_BAD_STATE
1982 * The library has not been previously initialized by psa_crypto_init().
1983 * It is implementation-dependent whether a failure to initialize
1984 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001985 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001986psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1987 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001988 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001989 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001990 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001991 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001992
Gilles Peskinedcd14942018-07-12 00:30:52 +02001993/** Finish encrypting or decrypting a message in a cipher operation.
1994 *
1995 * The application must call psa_cipher_encrypt_setup() or
1996 * psa_cipher_decrypt_setup() before calling this function. The choice
1997 * of setup function determines whether this function encrypts or
1998 * decrypts its input.
1999 *
2000 * This function finishes the encryption or decryption of the message
2001 * formed by concatenating the inputs passed to preceding calls to
2002 * psa_cipher_update().
2003 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002004 * When this function returns successfuly, the operation becomes inactive.
2005 * If this function returns an error status, the operation enters an error
2006 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002007 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002008 * \param[in,out] operation Active cipher operation.
2009 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002010 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002011 * \param[out] output_length On success, the number of bytes
2012 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002013 *
2014 * \retval #PSA_SUCCESS
2015 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02002016 * \retval #PSA_ERROR_INVALID_ARGUMENT
2017 * The total input size passed to this operation is not valid for
2018 * this particular algorithm. For example, the algorithm is a based
2019 * on block cipher and requires a whole number of blocks, but the
2020 * total input size is not a multiple of the block size.
2021 * \retval #PSA_ERROR_INVALID_PADDING
2022 * This is a decryption operation for an algorithm that includes
2023 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002024 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002025 * The operation state is not valid (it must be active, with an IV set
2026 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02002027 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2028 * The size of the \p output buffer is too small.
2029 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2030 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2031 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002032 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002033 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002034 * \retval #PSA_ERROR_BAD_STATE
2035 * The library has not been previously initialized by psa_crypto_init().
2036 * It is implementation-dependent whether a failure to initialize
2037 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002038 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002039psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002040 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002041 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002042 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002043
Gilles Peskinedcd14942018-07-12 00:30:52 +02002044/** Abort a cipher operation.
2045 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002046 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002047 * \p operation structure itself. Once aborted, the operation object
2048 * can be reused for another operation by calling
2049 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002050 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002051 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002052 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002053 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002054 * In particular, calling psa_cipher_abort() after the operation has been
2055 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2056 * is safe and has no effect.
2057 *
2058 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002059 *
2060 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02002061 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2062 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002063 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002064 * \retval #PSA_ERROR_BAD_STATE
2065 * The library has not been previously initialized by psa_crypto_init().
2066 * It is implementation-dependent whether a failure to initialize
2067 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002068 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002069psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2070
2071/**@}*/
2072
Gilles Peskine3b555712018-03-03 21:27:57 +01002073/** \defgroup aead Authenticated encryption with associated data (AEAD)
2074 * @{
2075 */
2076
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002077/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002078 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002079 * \param key Identifier of the key to use for the
2080 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002081 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002082 * \param alg The AEAD algorithm to compute
2083 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002084 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002085 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002086 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002087 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002088 * but not encrypted.
2089 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002090 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002091 * encrypted.
2092 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002093 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002094 * encrypted data. The additional data is not
2095 * part of this output. For algorithms where the
2096 * encrypted data and the authentication tag
2097 * are defined as separate outputs, the
2098 * authentication tag is appended to the
2099 * encrypted data.
2100 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2101 * This must be at least
2102 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2103 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002104 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002105 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002106 *
Gilles Peskine28538492018-07-11 17:34:00 +02002107 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002108 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002109 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002110 * \retval #PSA_ERROR_NOT_PERMITTED
2111 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002112 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002113 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002114 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002115 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002116 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2117 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002118 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2119 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002120 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002121 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002122 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002123 * The library has not been previously initialized by psa_crypto_init().
2124 * It is implementation-dependent whether a failure to initialize
2125 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002126 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002127psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002128 psa_algorithm_t alg,
2129 const uint8_t *nonce,
2130 size_t nonce_length,
2131 const uint8_t *additional_data,
2132 size_t additional_data_length,
2133 const uint8_t *plaintext,
2134 size_t plaintext_length,
2135 uint8_t *ciphertext,
2136 size_t ciphertext_size,
2137 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002138
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002139/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002140 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002141 * \param key Identifier of the key to use for the
2142 * operation. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002143 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002144 * \param alg The AEAD algorithm to compute
2145 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002146 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002147 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002148 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002149 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002150 * but not encrypted.
2151 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002152 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002153 * encrypted. For algorithms where the
2154 * encrypted data and the authentication tag
2155 * are defined as separate inputs, the buffer
2156 * must contain the encrypted data followed
2157 * by the authentication tag.
2158 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002159 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002160 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2161 * This must be at least
2162 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2163 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002164 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002165 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002166 *
Gilles Peskine28538492018-07-11 17:34:00 +02002167 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002168 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002169 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002170 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002171 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002172 * \retval #PSA_ERROR_NOT_PERMITTED
2173 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002174 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002175 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002176 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002177 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002178 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2179 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002180 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2181 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002182 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002183 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002184 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002185 * The library has not been previously initialized by psa_crypto_init().
2186 * It is implementation-dependent whether a failure to initialize
2187 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002188 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002189psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002190 psa_algorithm_t alg,
2191 const uint8_t *nonce,
2192 size_t nonce_length,
2193 const uint8_t *additional_data,
2194 size_t additional_data_length,
2195 const uint8_t *ciphertext,
2196 size_t ciphertext_length,
2197 uint8_t *plaintext,
2198 size_t plaintext_size,
2199 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002200
Gilles Peskine30a9e412019-01-14 18:36:12 +01002201/** The type of the state data structure for multipart AEAD operations.
2202 *
2203 * Before calling any function on an AEAD operation object, the application
2204 * must initialize it by any of the following means:
2205 * - Set the structure to all-bits-zero, for example:
2206 * \code
2207 * psa_aead_operation_t operation;
2208 * memset(&operation, 0, sizeof(operation));
2209 * \endcode
2210 * - Initialize the structure to logical zero values, for example:
2211 * \code
2212 * psa_aead_operation_t operation = {0};
2213 * \endcode
2214 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2215 * for example:
2216 * \code
2217 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2218 * \endcode
2219 * - Assign the result of the function psa_aead_operation_init()
2220 * to the structure, for example:
2221 * \code
2222 * psa_aead_operation_t operation;
2223 * operation = psa_aead_operation_init();
2224 * \endcode
2225 *
2226 * This is an implementation-defined \c struct. Applications should not
2227 * make any assumptions about the content of this structure except
2228 * as directed by the documentation of a specific implementation. */
2229typedef struct psa_aead_operation_s psa_aead_operation_t;
2230
2231/** \def PSA_AEAD_OPERATION_INIT
2232 *
2233 * This macro returns a suitable initializer for an AEAD operation object of
2234 * type #psa_aead_operation_t.
2235 */
2236#ifdef __DOXYGEN_ONLY__
2237/* This is an example definition for documentation purposes.
2238 * Implementations should define a suitable value in `crypto_struct.h`.
2239 */
2240#define PSA_AEAD_OPERATION_INIT {0}
2241#endif
2242
2243/** Return an initial value for an AEAD operation object.
2244 */
2245static psa_aead_operation_t psa_aead_operation_init(void);
2246
2247/** Set the key for a multipart authenticated encryption operation.
2248 *
2249 * The sequence of operations to encrypt a message with authentication
2250 * is as follows:
2251 * -# Allocate an operation object which will be passed to all the functions
2252 * listed here.
2253 * -# Initialize the operation object with one of the methods described in the
2254 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002255 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002256 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002257 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2258 * inputs to the subsequent calls to psa_aead_update_ad() and
2259 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2260 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002261 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2262 * generate or set the nonce. You should use
2263 * psa_aead_generate_nonce() unless the protocol you are implementing
2264 * requires a specific nonce value.
2265 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2266 * of the non-encrypted additional authenticated data each time.
2267 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002268 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002269 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002270 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002271 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2272 * the operation will need to be reset by a call to psa_aead_abort(). The
2273 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002274 * has been initialized.
2275 *
2276 * After a successful call to psa_aead_encrypt_setup(), the application must
2277 * eventually terminate the operation. The following events terminate an
2278 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002279 * - A successful call to psa_aead_finish().
2280 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002281 *
2282 * \param[in,out] operation The operation object to set up. It must have
2283 * been initialized as per the documentation for
2284 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002285 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002286 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002287 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002288 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002289 * \param alg The AEAD algorithm to compute
2290 * (\c PSA_ALG_XXX value such that
2291 * #PSA_ALG_IS_AEAD(\p alg) is true).
2292 *
2293 * \retval #PSA_SUCCESS
2294 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002295 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002296 * The operation state is not valid (it must be inactive).
Ronald Cron96783552020-10-19 12:06:30 +02002297 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002298 * \retval #PSA_ERROR_NOT_PERMITTED
2299 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002300 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002301 * \retval #PSA_ERROR_NOT_SUPPORTED
2302 * \p alg is not supported or is not an AEAD algorithm.
2303 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2304 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2305 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002306 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002307 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002308 * \retval #PSA_ERROR_BAD_STATE
2309 * The library has not been previously initialized by psa_crypto_init().
2310 * It is implementation-dependent whether a failure to initialize
2311 * results in this error code.
2312 */
2313psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002314 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002315 psa_algorithm_t alg);
2316
2317/** Set the key for a multipart authenticated decryption operation.
2318 *
2319 * The sequence of operations to decrypt a message with authentication
2320 * is as follows:
2321 * -# Allocate an operation object which will be passed to all the functions
2322 * listed here.
2323 * -# Initialize the operation object with one of the methods described in the
2324 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002325 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002326 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002327 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2328 * inputs to the subsequent calls to psa_aead_update_ad() and
2329 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2330 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002331 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2332 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2333 * of the non-encrypted additional authenticated data each time.
2334 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002335 * of the ciphertext to decrypt each time.
2336 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002337 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002338 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2339 * the operation will need to be reset by a call to psa_aead_abort(). The
2340 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002341 * has been initialized.
2342 *
2343 * After a successful call to psa_aead_decrypt_setup(), the application must
2344 * eventually terminate the operation. The following events terminate an
2345 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002346 * - A successful call to psa_aead_verify().
2347 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002348 *
2349 * \param[in,out] operation The operation object to set up. It must have
2350 * been initialized as per the documentation for
2351 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002352 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002353 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002354 * terminates. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002355 * #PSA_KEY_USAGE_DECRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002356 * \param alg The AEAD algorithm to compute
2357 * (\c PSA_ALG_XXX value such that
2358 * #PSA_ALG_IS_AEAD(\p alg) is true).
2359 *
2360 * \retval #PSA_SUCCESS
2361 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002362 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002363 * The operation state is not valid (it must be inactive).
Ronald Cron96783552020-10-19 12:06:30 +02002364 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002365 * \retval #PSA_ERROR_NOT_PERMITTED
2366 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002367 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002368 * \retval #PSA_ERROR_NOT_SUPPORTED
2369 * \p alg is not supported or is not an AEAD algorithm.
2370 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2371 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2372 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002373 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002374 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002375 * \retval #PSA_ERROR_BAD_STATE
2376 * The library has not been previously initialized by psa_crypto_init().
2377 * It is implementation-dependent whether a failure to initialize
2378 * results in this error code.
2379 */
2380psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002381 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002382 psa_algorithm_t alg);
2383
2384/** Generate a random nonce for an authenticated encryption operation.
2385 *
2386 * This function generates a random nonce for the authenticated encryption
2387 * operation with an appropriate size for the chosen algorithm, key type
2388 * and key size.
2389 *
2390 * The application must call psa_aead_encrypt_setup() before
2391 * calling this function.
2392 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002393 * If this function returns an error status, the operation enters an error
2394 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002395 *
2396 * \param[in,out] operation Active AEAD operation.
2397 * \param[out] nonce Buffer where the generated nonce is to be
2398 * written.
2399 * \param nonce_size Size of the \p nonce buffer in bytes.
2400 * \param[out] nonce_length On success, the number of bytes of the
2401 * generated nonce.
2402 *
2403 * \retval #PSA_SUCCESS
2404 * Success.
2405 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002406 * The operation state is not valid (it must be an active aead encrypt
Ronald Cron96783552020-10-19 12:06:30 +02002407 * operation, with no nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002408 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2409 * The size of the \p nonce buffer is too small.
2410 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2411 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2412 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002413 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002414 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002415 * \retval #PSA_ERROR_BAD_STATE
2416 * The library has not been previously initialized by psa_crypto_init().
2417 * It is implementation-dependent whether a failure to initialize
2418 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002419 */
2420psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002421 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002422 size_t nonce_size,
2423 size_t *nonce_length);
2424
2425/** Set the nonce for an authenticated encryption or decryption operation.
2426 *
2427 * This function sets the nonce for the authenticated
2428 * encryption or decryption operation.
2429 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002430 * The application must call psa_aead_encrypt_setup() or
2431 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002432 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002433 * If this function returns an error status, the operation enters an error
2434 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002435 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002436 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002437 * instead of this function, unless implementing a protocol that requires
2438 * a non-random IV.
2439 *
2440 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002441 * \param[in] nonce Buffer containing the nonce to use.
2442 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002443 *
2444 * \retval #PSA_SUCCESS
2445 * Success.
2446 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002447 * The operation state is not valid (it must be active, with no nonce
2448 * set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002449 * \retval #PSA_ERROR_INVALID_ARGUMENT
2450 * The size of \p nonce is not acceptable for the chosen algorithm.
2451 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2452 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2453 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002454 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002455 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002456 * \retval #PSA_ERROR_BAD_STATE
2457 * The library has not been previously initialized by psa_crypto_init().
2458 * It is implementation-dependent whether a failure to initialize
2459 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002460 */
2461psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002462 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002463 size_t nonce_length);
2464
Gilles Peskinebc59c852019-01-17 15:26:08 +01002465/** Declare the lengths of the message and additional data for AEAD.
2466 *
2467 * The application must call this function before calling
2468 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2469 * the operation requires it. If the algorithm does not require it,
2470 * calling this function is optional, but if this function is called
2471 * then the implementation must enforce the lengths.
2472 *
2473 * You may call this function before or after setting the nonce with
2474 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2475 *
2476 * - For #PSA_ALG_CCM, calling this function is required.
2477 * - For the other AEAD algorithms defined in this specification, calling
2478 * this function is not required.
2479 * - For vendor-defined algorithm, refer to the vendor documentation.
2480 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002481 * If this function returns an error status, the operation enters an error
2482 * state and must be aborted by calling psa_aead_abort().
2483 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002484 * \param[in,out] operation Active AEAD operation.
2485 * \param ad_length Size of the non-encrypted additional
2486 * authenticated data in bytes.
2487 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2488 *
2489 * \retval #PSA_SUCCESS
2490 * Success.
2491 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002492 * The operation state is not valid (it must be active, and
2493 * psa_aead_update_ad() and psa_aead_update() must not have been
2494 * called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002495 * \retval #PSA_ERROR_INVALID_ARGUMENT
2496 * At least one of the lengths is not acceptable for the chosen
2497 * algorithm.
2498 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2499 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2500 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002501 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002502 * \retval #PSA_ERROR_BAD_STATE
2503 * The library has not been previously initialized by psa_crypto_init().
2504 * It is implementation-dependent whether a failure to initialize
2505 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002506 */
2507psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2508 size_t ad_length,
2509 size_t plaintext_length);
2510
Gilles Peskine30a9e412019-01-14 18:36:12 +01002511/** Pass additional data to an active AEAD operation.
2512 *
2513 * Additional data is authenticated, but not encrypted.
2514 *
2515 * You may call this function multiple times to pass successive fragments
2516 * of the additional data. You may not call this function after passing
2517 * data to encrypt or decrypt with psa_aead_update().
2518 *
2519 * Before calling this function, you must:
2520 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2521 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2522 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002523 * If this function returns an error status, the operation enters an error
2524 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002525 *
2526 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2527 * there is no guarantee that the input is valid. Therefore, until
2528 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2529 * treat the input as untrusted and prepare to undo any action that
2530 * depends on the input if psa_aead_verify() returns an error status.
2531 *
2532 * \param[in,out] operation Active AEAD operation.
2533 * \param[in] input Buffer containing the fragment of
2534 * additional data.
2535 * \param input_length Size of the \p input buffer in bytes.
2536 *
2537 * \retval #PSA_SUCCESS
2538 * Success.
2539 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002540 * The operation state is not valid (it must be active, have a nonce
2541 * set, have lengths set if required by the algorithm, and
2542 * psa_aead_update() must not have been called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002543 * \retval #PSA_ERROR_INVALID_ARGUMENT
2544 * The total input length overflows the additional data length that
2545 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002546 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2547 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2548 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002549 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002550 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002551 * \retval #PSA_ERROR_BAD_STATE
2552 * The library has not been previously initialized by psa_crypto_init().
2553 * It is implementation-dependent whether a failure to initialize
2554 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002555 */
2556psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2557 const uint8_t *input,
2558 size_t input_length);
2559
2560/** Encrypt or decrypt a message fragment in an active AEAD operation.
2561 *
2562 * Before calling this function, you must:
2563 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2564 * The choice of setup function determines whether this function
2565 * encrypts or decrypts its input.
2566 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2567 * 3. Call psa_aead_update_ad() to pass all the additional data.
2568 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002569 * If this function returns an error status, the operation enters an error
2570 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002571 *
2572 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2573 * there is no guarantee that the input is valid. Therefore, until
2574 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2575 * - Do not use the output in any way other than storing it in a
2576 * confidential location. If you take any action that depends
2577 * on the tentative decrypted data, this action will need to be
2578 * undone if the input turns out not to be valid. Furthermore,
2579 * if an adversary can observe that this action took place
2580 * (for example through timing), they may be able to use this
2581 * fact as an oracle to decrypt any message encrypted with the
2582 * same key.
2583 * - In particular, do not copy the output anywhere but to a
2584 * memory or storage space that you have exclusive access to.
2585 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002586 * This function does not require the input to be aligned to any
2587 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002588 * a whole block at a time, it must consume all the input provided, but
2589 * it may delay the end of the corresponding output until a subsequent
2590 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2591 * provides sufficient input. The amount of data that can be delayed
2592 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002593 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002594 * \param[in,out] operation Active AEAD operation.
2595 * \param[in] input Buffer containing the message fragment to
2596 * encrypt or decrypt.
2597 * \param input_length Size of the \p input buffer in bytes.
2598 * \param[out] output Buffer where the output is to be written.
2599 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002600 * This must be at least
2601 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2602 * \p input_length) where \c alg is the
2603 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002604 * \param[out] output_length On success, the number of bytes
2605 * that make up the returned output.
2606 *
2607 * \retval #PSA_SUCCESS
2608 * Success.
2609 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002610 * The operation state is not valid (it must be active, have a nonce
2611 * set, and have lengths set if required by the algorithm).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002612 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2613 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002614 * You can determine a sufficient buffer size by calling
2615 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2616 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002617 * \retval #PSA_ERROR_INVALID_ARGUMENT
2618 * The total length of input to psa_aead_update_ad() so far is
2619 * less than the additional data length that was previously
2620 * specified with psa_aead_set_lengths().
2621 * \retval #PSA_ERROR_INVALID_ARGUMENT
2622 * The total input length overflows the plaintext length that
2623 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002624 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2625 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2626 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002627 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002628 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002629 * \retval #PSA_ERROR_BAD_STATE
2630 * The library has not been previously initialized by psa_crypto_init().
2631 * It is implementation-dependent whether a failure to initialize
2632 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002633 */
2634psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2635 const uint8_t *input,
2636 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002637 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002638 size_t output_size,
2639 size_t *output_length);
2640
2641/** Finish encrypting a message in an AEAD operation.
2642 *
2643 * The operation must have been set up with psa_aead_encrypt_setup().
2644 *
2645 * This function finishes the authentication of the additional data
2646 * formed by concatenating the inputs passed to preceding calls to
2647 * psa_aead_update_ad() with the plaintext formed by concatenating the
2648 * inputs passed to preceding calls to psa_aead_update().
2649 *
2650 * This function has two output buffers:
2651 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002652 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002653 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002654 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002655 * that the operation performs.
2656 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002657 * When this function returns successfuly, the operation becomes inactive.
2658 * If this function returns an error status, the operation enters an error
2659 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002660 *
2661 * \param[in,out] operation Active AEAD operation.
2662 * \param[out] ciphertext Buffer where the last part of the ciphertext
2663 * is to be written.
2664 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002665 * This must be at least
2666 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2667 * \c alg is the algorithm that is being
2668 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002669 * \param[out] ciphertext_length On success, the number of bytes of
2670 * returned ciphertext.
2671 * \param[out] tag Buffer where the authentication tag is
2672 * to be written.
2673 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002674 * This must be at least
2675 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2676 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002677 * \param[out] tag_length On success, the number of bytes
2678 * that make up the returned tag.
2679 *
2680 * \retval #PSA_SUCCESS
2681 * Success.
2682 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002683 * The operation state is not valid (it must be an active encryption
2684 * operation with a nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002685 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002686 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002687 * You can determine a sufficient buffer size for \p ciphertext by
2688 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2689 * where \c alg is the algorithm that is being calculated.
2690 * You can determine a sufficient buffer size for \p tag by
2691 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002692 * \retval #PSA_ERROR_INVALID_ARGUMENT
2693 * The total length of input to psa_aead_update_ad() so far is
2694 * less than the additional data length that was previously
2695 * specified with psa_aead_set_lengths().
2696 * \retval #PSA_ERROR_INVALID_ARGUMENT
2697 * The total length of input to psa_aead_update() so far is
2698 * less than the plaintext length that was previously
2699 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002700 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2701 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2702 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002703 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002704 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002705 * \retval #PSA_ERROR_BAD_STATE
2706 * The library has not been previously initialized by psa_crypto_init().
2707 * It is implementation-dependent whether a failure to initialize
2708 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002709 */
2710psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002711 uint8_t *ciphertext,
2712 size_t ciphertext_size,
2713 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002714 uint8_t *tag,
2715 size_t tag_size,
2716 size_t *tag_length);
2717
2718/** Finish authenticating and decrypting a message in an AEAD operation.
2719 *
2720 * The operation must have been set up with psa_aead_decrypt_setup().
2721 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002722 * This function finishes the authenticated decryption of the message
2723 * components:
2724 *
2725 * - The additional data consisting of the concatenation of the inputs
2726 * passed to preceding calls to psa_aead_update_ad().
2727 * - The ciphertext consisting of the concatenation of the inputs passed to
2728 * preceding calls to psa_aead_update().
2729 * - The tag passed to this function call.
2730 *
2731 * If the authentication tag is correct, this function outputs any remaining
2732 * plaintext and reports success. If the authentication tag is not correct,
2733 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002734 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002735 * When this function returns successfuly, the operation becomes inactive.
2736 * If this function returns an error status, the operation enters an error
2737 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002738 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002739 * \note Implementations shall make the best effort to ensure that the
2740 * comparison between the actual tag and the expected tag is performed
2741 * in constant time.
2742 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002743 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002744 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002745 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002746 * from previous calls to psa_aead_update()
2747 * that could not be processed until the end
2748 * of the input.
2749 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002750 * This must be at least
2751 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2752 * \c alg is the algorithm that is being
2753 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002754 * \param[out] plaintext_length On success, the number of bytes of
2755 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002756 * \param[in] tag Buffer containing the authentication tag.
2757 * \param tag_length Size of the \p tag buffer in bytes.
2758 *
2759 * \retval #PSA_SUCCESS
2760 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002761 * \retval #PSA_ERROR_INVALID_SIGNATURE
2762 * The calculations were successful, but the authentication tag is
2763 * not correct.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002764 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002765 * The operation state is not valid (it must be an active decryption
2766 * operation with a nonce set).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002767 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2768 * The size of the \p plaintext buffer is too small.
2769 * You can determine a sufficient buffer size for \p plaintext by
2770 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2771 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002772 * \retval #PSA_ERROR_INVALID_ARGUMENT
2773 * The total length of input to psa_aead_update_ad() so far is
2774 * less than the additional data length that was previously
2775 * specified with psa_aead_set_lengths().
2776 * \retval #PSA_ERROR_INVALID_ARGUMENT
2777 * The total length of input to psa_aead_update() so far is
2778 * less than the plaintext length that was previously
2779 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002780 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2781 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2782 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002783 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002784 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002785 * \retval #PSA_ERROR_BAD_STATE
2786 * The library has not been previously initialized by psa_crypto_init().
2787 * It is implementation-dependent whether a failure to initialize
2788 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002789 */
2790psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002791 uint8_t *plaintext,
2792 size_t plaintext_size,
2793 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002794 const uint8_t *tag,
2795 size_t tag_length);
2796
2797/** Abort an AEAD operation.
2798 *
2799 * Aborting an operation frees all associated resources except for the
2800 * \p operation structure itself. Once aborted, the operation object
2801 * can be reused for another operation by calling
2802 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2803 *
2804 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002805 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002806 *
2807 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002808 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2809 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002810 *
2811 * \param[in,out] operation Initialized AEAD operation.
2812 *
2813 * \retval #PSA_SUCCESS
Gilles Peskine30a9e412019-01-14 18:36:12 +01002814 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2815 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002816 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002817 * \retval #PSA_ERROR_BAD_STATE
2818 * The library has not been previously initialized by psa_crypto_init().
2819 * It is implementation-dependent whether a failure to initialize
2820 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002821 */
2822psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2823
Gilles Peskine3b555712018-03-03 21:27:57 +01002824/**@}*/
2825
Gilles Peskine20035e32018-02-03 22:44:14 +01002826/** \defgroup asymmetric Asymmetric cryptography
2827 * @{
2828 */
2829
2830/**
2831 * \brief Sign a hash or short message with a private key.
2832 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002833 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002834 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002835 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2836 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2837 * to determine the hash algorithm to use.
2838 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002839 * \param key Identifier of the key to use for the operation.
2840 * It must be an asymmetric key pair. The key must
Ronald Cron96783552020-10-19 12:06:30 +02002841 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002842 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002843 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002844 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002845 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002846 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002847 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002848 * \param[out] signature_length On success, the number of bytes
2849 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002850 *
Gilles Peskine28538492018-07-11 17:34:00 +02002851 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002852 * \retval #PSA_ERROR_INVALID_HANDLE
2853 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002854 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002855 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002856 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002857 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002858 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002859 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002860 * \retval #PSA_ERROR_NOT_SUPPORTED
2861 * \retval #PSA_ERROR_INVALID_ARGUMENT
2862 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2863 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2864 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002865 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002866 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002867 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002868 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002869 * The library has not been previously initialized by psa_crypto_init().
2870 * It is implementation-dependent whether a failure to initialize
2871 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002872 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002873psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002874 psa_algorithm_t alg,
2875 const uint8_t *hash,
2876 size_t hash_length,
2877 uint8_t *signature,
2878 size_t signature_size,
2879 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002880
2881/**
2882 * \brief Verify the signature a hash or short message using a public key.
2883 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002884 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002885 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002886 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2887 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2888 * to determine the hash algorithm to use.
2889 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002890 * \param key Identifier of the key to use for the operation. It
2891 * must be a public key or an asymmetric key pair. The
Ronald Cron96783552020-10-19 12:06:30 +02002892 * key must allow the usage
2893 * #PSA_KEY_USAGE_VERIFY_HASH.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002894 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002895 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002896 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002897 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002898 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002899 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002900 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002901 *
Gilles Peskine28538492018-07-11 17:34:00 +02002902 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002903 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002904 * \retval #PSA_ERROR_INVALID_HANDLE
2905 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002906 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002907 * The calculation was perfomed successfully, but the passed
2908 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002909 * \retval #PSA_ERROR_NOT_SUPPORTED
2910 * \retval #PSA_ERROR_INVALID_ARGUMENT
2911 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2912 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2913 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002914 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002915 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002916 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002917 * The library has not been previously initialized by psa_crypto_init().
2918 * It is implementation-dependent whether a failure to initialize
2919 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002920 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002921psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002922 psa_algorithm_t alg,
2923 const uint8_t *hash,
2924 size_t hash_length,
2925 const uint8_t *signature,
2926 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002927
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002928/**
2929 * \brief Encrypt a short message with a public key.
2930 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002931 * \param key Identifer of the key to use for the operation.
2932 * It must be a public key or an asymmetric key
2933 * pair. It must allow the usage
Ronald Cron96783552020-10-19 12:06:30 +02002934 * #PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002935 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02002936 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002937 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002938 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002939 * \param[in] salt A salt or label, if supported by the
2940 * encryption algorithm.
2941 * If the algorithm does not support a
2942 * salt, pass \c NULL.
2943 * If the algorithm supports an optional
2944 * salt and you do not want to pass a salt,
2945 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002946 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002947 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2948 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002949 * \param salt_length Size of the \p salt buffer in bytes.
2950 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002951 * \param[out] output Buffer where the encrypted message is to
2952 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002953 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002954 * \param[out] output_length On success, the number of bytes
2955 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002956 *
Gilles Peskine28538492018-07-11 17:34:00 +02002957 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002958 * \retval #PSA_ERROR_INVALID_HANDLE
2959 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002960 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002961 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002962 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002963 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002964 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002965 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002966 * \retval #PSA_ERROR_NOT_SUPPORTED
2967 * \retval #PSA_ERROR_INVALID_ARGUMENT
2968 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2969 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2970 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002971 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002972 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002973 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002974 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002975 * The library has not been previously initialized by psa_crypto_init().
2976 * It is implementation-dependent whether a failure to initialize
2977 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002978 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002979psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002980 psa_algorithm_t alg,
2981 const uint8_t *input,
2982 size_t input_length,
2983 const uint8_t *salt,
2984 size_t salt_length,
2985 uint8_t *output,
2986 size_t output_size,
2987 size_t *output_length);
2988
2989/**
2990 * \brief Decrypt a short message with a private key.
2991 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002992 * \param key Identifier of the key to use for the operation.
2993 * It must be an asymmetric key pair. It must
Ronald Cron96783552020-10-19 12:06:30 +02002994 * allow the usage #PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002995 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02002996 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002997 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002998 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002999 * \param[in] salt A salt or label, if supported by the
3000 * encryption algorithm.
3001 * If the algorithm does not support a
3002 * salt, pass \c NULL.
3003 * If the algorithm supports an optional
3004 * salt and you do not want to pass a salt,
3005 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003006 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003007 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3008 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003009 * \param salt_length Size of the \p salt buffer in bytes.
3010 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003011 * \param[out] output Buffer where the decrypted message is to
3012 * be written.
3013 * \param output_size Size of the \c output buffer in bytes.
3014 * \param[out] output_length On success, the number of bytes
3015 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003016 *
Gilles Peskine28538492018-07-11 17:34:00 +02003017 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003018 * \retval #PSA_ERROR_INVALID_HANDLE
3019 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003020 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003021 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003022 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003023 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003024 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02003025 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003026 * \retval #PSA_ERROR_NOT_SUPPORTED
3027 * \retval #PSA_ERROR_INVALID_ARGUMENT
3028 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3029 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3030 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003031 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003032 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003033 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3034 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003035 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003036 * The library has not been previously initialized by psa_crypto_init().
3037 * It is implementation-dependent whether a failure to initialize
3038 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003039 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003040psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003041 psa_algorithm_t alg,
3042 const uint8_t *input,
3043 size_t input_length,
3044 const uint8_t *salt,
3045 size_t salt_length,
3046 uint8_t *output,
3047 size_t output_size,
3048 size_t *output_length);
3049
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003050/**@}*/
3051
Gilles Peskine35675b62019-05-16 17:26:11 +02003052/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003053 * @{
3054 */
3055
Gilles Peskine35675b62019-05-16 17:26:11 +02003056/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003057 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003058 * Before calling any function on a key derivation operation object, the
3059 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003060 * - Set the structure to all-bits-zero, for example:
3061 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003062 * psa_key_derivation_operation_t operation;
3063 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003064 * \endcode
3065 * - Initialize the structure to logical zero values, for example:
3066 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003067 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003068 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003069 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003070 * for example:
3071 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003072 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003073 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003074 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003075 * to the structure, for example:
3076 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003077 * psa_key_derivation_operation_t operation;
3078 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003079 * \endcode
3080 *
3081 * This is an implementation-defined \c struct. Applications should not
3082 * make any assumptions about the content of this structure except
3083 * as directed by the documentation of a specific implementation.
3084 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003085typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003086
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003087/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003088 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003089 * This macro returns a suitable initializer for a key derivation operation
3090 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003091 */
3092#ifdef __DOXYGEN_ONLY__
3093/* This is an example definition for documentation purposes.
3094 * Implementations should define a suitable value in `crypto_struct.h`.
3095 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003096#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003097#endif
3098
Gilles Peskine35675b62019-05-16 17:26:11 +02003099/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003100 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003101static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003102
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003103/** Set up a key derivation operation.
3104 *
3105 * A key derivation algorithm takes some inputs and uses them to generate
3106 * a byte stream in a deterministic way.
3107 * This byte stream can be used to produce keys and other
3108 * cryptographic material.
3109 *
3110 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003111 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3112 * -# Call psa_key_derivation_setup() to select the algorithm.
3113 * -# Provide the inputs for the key derivation by calling
3114 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3115 * as appropriate. Which inputs are needed, in what order, and whether
3116 * they may be keys and if so of what type depends on the algorithm.
3117 * -# Optionally set the operation's maximum capacity with
3118 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3119 * of or after providing inputs. For some algorithms, this step is mandatory
3120 * because the output depends on the maximum capacity.
3121 * -# To derive a key, call psa_key_derivation_output_key().
3122 * To derive a byte string for a different purpose, call
3123 * psa_key_derivation_output_bytes().
3124 * Successive calls to these functions use successive output bytes
3125 * calculated by the key derivation algorithm.
3126 * -# Clean up the key derivation operation object with
3127 * psa_key_derivation_abort().
3128 *
3129 * If this function returns an error, the key derivation operation object is
3130 * not changed.
3131 *
3132 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3133 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003134 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003135 * Implementations must reject an attempt to derive a key of size 0.
3136 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003137 * \param[in,out] operation The key derivation operation object
3138 * to set up. It must
3139 * have been initialized but not set up yet.
3140 * \param alg The key derivation algorithm to compute
3141 * (\c PSA_ALG_XXX value such that
3142 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3143 *
3144 * \retval #PSA_SUCCESS
3145 * Success.
3146 * \retval #PSA_ERROR_INVALID_ARGUMENT
3147 * \c alg is not a key derivation algorithm.
3148 * \retval #PSA_ERROR_NOT_SUPPORTED
3149 * \c alg is not supported or is not a key derivation algorithm.
3150 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3151 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3152 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003153 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003154 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003155 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003156 * The operation state is not valid (it must be inactive).
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003157 * \retval #PSA_ERROR_BAD_STATE
3158 * The library has not been previously initialized by psa_crypto_init().
3159 * It is implementation-dependent whether a failure to initialize
3160 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003161 */
3162psa_status_t psa_key_derivation_setup(
3163 psa_key_derivation_operation_t *operation,
3164 psa_algorithm_t alg);
3165
Gilles Peskine35675b62019-05-16 17:26:11 +02003166/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003167 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003168 * The capacity of a key derivation is the maximum number of bytes that it can
3169 * return. When you get *N* bytes of output from a key derivation operation,
3170 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003171 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003172 * \param[in] operation The operation to query.
3173 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003174 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003175 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003176 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003177 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003178 * The operation state is not valid (it must be active).
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003179 * \retval #PSA_ERROR_HARDWARE_FAILURE
3180 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003181 * \retval #PSA_ERROR_BAD_STATE
3182 * The library has not been previously initialized by psa_crypto_init().
3183 * It is implementation-dependent whether a failure to initialize
3184 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003185 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003186psa_status_t psa_key_derivation_get_capacity(
3187 const psa_key_derivation_operation_t *operation,
3188 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003189
Gilles Peskine35675b62019-05-16 17:26:11 +02003190/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003191 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003192 * The capacity of a key derivation operation is the maximum number of bytes
3193 * that the key derivation operation can return from this point onwards.
3194 *
3195 * \param[in,out] operation The key derivation operation object to modify.
3196 * \param capacity The new capacity of the operation.
3197 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003198 * current capacity.
3199 *
3200 * \retval #PSA_SUCCESS
3201 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003202 * \p capacity is larger than the operation's current capacity.
3203 * In this case, the operation object remains valid and its capacity
3204 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003205 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003206 * The operation state is not valid (it must be active).
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003207 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003208 * \retval #PSA_ERROR_HARDWARE_FAILURE
3209 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003210 * \retval #PSA_ERROR_BAD_STATE
3211 * The library has not been previously initialized by psa_crypto_init().
3212 * It is implementation-dependent whether a failure to initialize
3213 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003214 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003215psa_status_t psa_key_derivation_set_capacity(
3216 psa_key_derivation_operation_t *operation,
3217 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003218
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003219/** Use the maximum possible capacity for a key derivation operation.
3220 *
3221 * Use this value as the capacity argument when setting up a key derivation
3222 * to indicate that the operation should have the maximum possible capacity.
3223 * The value of the maximum possible capacity depends on the key derivation
3224 * algorithm.
3225 */
3226#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3227
3228/** Provide an input for key derivation or key agreement.
3229 *
3230 * Which inputs are required and in what order depends on the algorithm.
3231 * Refer to the documentation of each key derivation or key agreement
3232 * algorithm for information.
3233 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003234 * This function passes direct inputs, which is usually correct for
3235 * non-secret inputs. To pass a secret input, which should be in a key
3236 * object, call psa_key_derivation_input_key() instead of this function.
3237 * Refer to the documentation of individual step types
3238 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3239 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003240 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003241 * If this function returns an error status, the operation enters an error
3242 * state and must be aborted by calling psa_key_derivation_abort().
3243 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003244 * \param[in,out] operation The key derivation operation object to use.
3245 * It must have been set up with
3246 * psa_key_derivation_setup() and must not
3247 * have produced any output yet.
3248 * \param step Which step the input data is for.
3249 * \param[in] data Input data to use.
3250 * \param data_length Size of the \p data buffer in bytes.
3251 *
3252 * \retval #PSA_SUCCESS
3253 * Success.
3254 * \retval #PSA_ERROR_INVALID_ARGUMENT
3255 * \c step is not compatible with the operation's algorithm.
3256 * \retval #PSA_ERROR_INVALID_ARGUMENT
3257 * \c step does not allow direct inputs.
3258 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3259 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3260 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003261 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003262 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003263 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003264 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003265 * \retval #PSA_ERROR_BAD_STATE
3266 * The library has not been previously initialized by psa_crypto_init().
3267 * It is implementation-dependent whether a failure to initialize
3268 * results in this error code.
3269 */
3270psa_status_t psa_key_derivation_input_bytes(
3271 psa_key_derivation_operation_t *operation,
3272 psa_key_derivation_step_t step,
3273 const uint8_t *data,
3274 size_t data_length);
3275
3276/** Provide an input for key derivation in the form of a key.
3277 *
3278 * Which inputs are required and in what order depends on the algorithm.
3279 * Refer to the documentation of each key derivation or key agreement
3280 * algorithm for information.
3281 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003282 * This function obtains input from a key object, which is usually correct for
3283 * secret inputs or for non-secret personalization strings kept in the key
3284 * store. To pass a non-secret parameter which is not in the key store,
3285 * call psa_key_derivation_input_bytes() instead of this function.
3286 * Refer to the documentation of individual step types
3287 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3288 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003289 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003290 * If this function returns an error status, the operation enters an error
3291 * state and must be aborted by calling psa_key_derivation_abort().
3292 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003293 * \param[in,out] operation The key derivation operation object to use.
3294 * It must have been set up with
3295 * psa_key_derivation_setup() and must not
3296 * have produced any output yet.
3297 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003298 * \param key Identifier of the key. It must have an
3299 * appropriate type for step and must allow the
Ronald Cron96783552020-10-19 12:06:30 +02003300 * usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003301 *
3302 * \retval #PSA_SUCCESS
3303 * Success.
3304 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003305 * \retval #PSA_ERROR_NOT_PERMITTED
3306 * \retval #PSA_ERROR_INVALID_ARGUMENT
3307 * \c step is not compatible with the operation's algorithm.
3308 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine224b0d62019-09-23 18:13:17 +02003309 * \c step does not allow key inputs of the given type
3310 * or does not allow key inputs at all.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003311 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3312 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3313 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003314 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003315 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003316 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003317 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003318 * \retval #PSA_ERROR_BAD_STATE
3319 * The library has not been previously initialized by psa_crypto_init().
3320 * It is implementation-dependent whether a failure to initialize
3321 * results in this error code.
3322 */
3323psa_status_t psa_key_derivation_input_key(
3324 psa_key_derivation_operation_t *operation,
3325 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003326 mbedtls_svc_key_id_t key);
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003327
3328/** Perform a key agreement and use the shared secret as input to a key
3329 * derivation.
3330 *
3331 * A key agreement algorithm takes two inputs: a private key \p private_key
3332 * a public key \p peer_key.
3333 * The result of this function is passed as input to a key derivation.
3334 * The output of this key derivation can be extracted by reading from the
3335 * resulting operation to produce keys and other cryptographic material.
3336 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003337 * If this function returns an error status, the operation enters an error
3338 * state and must be aborted by calling psa_key_derivation_abort().
3339 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003340 * \param[in,out] operation The key derivation operation object to use.
3341 * It must have been set up with
3342 * psa_key_derivation_setup() with a
3343 * key agreement and derivation algorithm
3344 * \c alg (\c PSA_ALG_XXX value such that
3345 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3346 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3347 * is false).
3348 * The operation must be ready for an
3349 * input of the type given by \p step.
3350 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003351 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003352 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003353 * \param[in] peer_key Public key of the peer. The peer key must be in the
3354 * same format that psa_import_key() accepts for the
3355 * public key type corresponding to the type of
3356 * private_key. That is, this function performs the
3357 * equivalent of
3358 * #psa_import_key(...,
3359 * `peer_key`, `peer_key_length`) where
3360 * with key attributes indicating the public key
3361 * type corresponding to the type of `private_key`.
3362 * For example, for EC keys, this means that peer_key
3363 * is interpreted as a point on the curve that the
3364 * private key is on. The standard formats for public
3365 * keys are documented in the documentation of
3366 * psa_export_public_key().
3367 * \param peer_key_length Size of \p peer_key in bytes.
3368 *
3369 * \retval #PSA_SUCCESS
3370 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01003371 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003372 * The operation state is not valid for this key agreement \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003373 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003374 * \retval #PSA_ERROR_NOT_PERMITTED
3375 * \retval #PSA_ERROR_INVALID_ARGUMENT
3376 * \c private_key is not compatible with \c alg,
3377 * or \p peer_key is not valid for \c alg or not compatible with
3378 * \c private_key.
3379 * \retval #PSA_ERROR_NOT_SUPPORTED
3380 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskine224b0d62019-09-23 18:13:17 +02003381 * \retval #PSA_ERROR_INVALID_ARGUMENT
3382 * \c step does not allow an input resulting from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003383 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3384 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3385 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003386 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003387 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003388 * \retval #PSA_ERROR_BAD_STATE
3389 * The library has not been previously initialized by psa_crypto_init().
3390 * It is implementation-dependent whether a failure to initialize
3391 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003392 */
3393psa_status_t psa_key_derivation_key_agreement(
3394 psa_key_derivation_operation_t *operation,
3395 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003396 mbedtls_svc_key_id_t private_key,
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003397 const uint8_t *peer_key,
3398 size_t peer_key_length);
3399
Gilles Peskine35675b62019-05-16 17:26:11 +02003400/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003401 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003402 * This function calculates output bytes from a key derivation algorithm and
3403 * return those bytes.
3404 * If you view the key derivation's output as a stream of bytes, this
3405 * function destructively reads the requested number of bytes from the
3406 * stream.
3407 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003408 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003409 * If this function returns an error status other than
3410 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003411 * state and must be aborted by calling psa_key_derivation_abort().
3412 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003413 * \param[in,out] operation The key derivation operation object to read from.
3414 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003415 * \param output_length Number of bytes to output.
3416 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003417 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003418 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003419 * The operation's capacity was less than
3420 * \p output_length bytes. Note that in this case,
3421 * no output is written to the output buffer.
3422 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003423 * subsequent calls to this function will not
3424 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003425 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003426 * The operation state is not valid (it must be active and completed
3427 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003428 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3429 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3430 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003431 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003432 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003433 * \retval #PSA_ERROR_BAD_STATE
3434 * The library has not been previously initialized by psa_crypto_init().
3435 * It is implementation-dependent whether a failure to initialize
3436 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003437 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003438psa_status_t psa_key_derivation_output_bytes(
3439 psa_key_derivation_operation_t *operation,
3440 uint8_t *output,
3441 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003442
Gilles Peskine35675b62019-05-16 17:26:11 +02003443/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003444 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003445 * This function calculates output bytes from a key derivation algorithm
3446 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003447 * The key's location, usage policy, type and size are taken from
3448 * \p attributes.
3449 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003450 * If you view the key derivation's output as a stream of bytes, this
3451 * function destructively reads as many bytes as required from the
3452 * stream.
3453 * The operation's capacity decreases by the number of bytes read.
3454 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003455 * If this function returns an error status other than
3456 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003457 * state and must be aborted by calling psa_key_derivation_abort().
3458 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003459 * How much output is produced and consumed from the operation, and how
3460 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003461 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003462 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003463 * of a given size, this function is functionally equivalent to
3464 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003465 * and passing the resulting output to #psa_import_key.
3466 * However, this function has a security benefit:
3467 * if the implementation provides an isolation boundary then
3468 * the key material is not exposed outside the isolation boundary.
3469 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003470 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003471 * The following key types defined in this specification follow this scheme:
3472 *
3473 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003474 * - #PSA_KEY_TYPE_ARC4;
3475 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003476 * - #PSA_KEY_TYPE_DERIVE;
3477 * - #PSA_KEY_TYPE_HMAC.
3478 *
3479 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003480 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003481 * Montgomery curve), this function always draws a byte string whose
3482 * length is determined by the curve, and sets the mandatory bits
3483 * accordingly. That is:
3484 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01003485 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003486 * string and process it as specified in RFC 7748 &sect;5.
Paul Elliott8ff510a2020-06-02 17:19:28 +01003487 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003488 * string and process it as specified in RFC 7748 &sect;5.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003489 *
3490 * - For key types for which the key is represented by a single sequence of
3491 * \p bits bits with constraints as to which bit sequences are acceptable,
3492 * this function draws a byte string of length (\p bits / 8) bytes rounded
3493 * up to the nearest whole number of bytes. If the resulting byte string
3494 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3495 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003496 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003497 * for the output produced by psa_export_key().
3498 * The following key types defined in this specification follow this scheme:
3499 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003500 * - #PSA_KEY_TYPE_DES.
3501 * Force-set the parity bits, but discard forbidden weak keys.
3502 * For 2-key and 3-key triple-DES, the three keys are generated
3503 * successively (for example, for 3-key triple-DES,
3504 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3505 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003506 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003507 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003508 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003509 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003510 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003511 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003512 * Weierstrass curve).
3513 * For these key types, interpret the byte string as integer
3514 * in big-endian order. Discard it if it is not in the range
3515 * [0, *N* - 2] where *N* is the boundary of the private key domain
3516 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003517 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003518 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003519 * This method allows compliance to NIST standards, specifically
3520 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003521 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3522 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3523 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3524 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003525 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003526 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003527 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003528 * implementation-defined.
3529 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003530 * In all cases, the data that is read is discarded from the operation.
3531 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003532 *
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003533 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3534 * the input to that step must be provided with psa_key_derivation_input_key().
3535 * Future versions of this specification may include additional restrictions
3536 * on the derived key based on the attributes and strength of the secret key.
3537 *
Gilles Peskine20628592019-04-19 19:29:50 +02003538 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003539 * \param[in,out] operation The key derivation operation object to read from.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003540 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003541 * key. For persistent keys, this is the key
3542 * identifier defined in \p attributes.
3543 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003544 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003545 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003546 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003547 * If the key is persistent, the key material and the key's metadata
3548 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003549 * \retval #PSA_ERROR_ALREADY_EXISTS
3550 * This is an attempt to create a persistent key, and there is
3551 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003552 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003553 * There was not enough data to create the desired key.
3554 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003555 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003556 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003557 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003558 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003559 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003560 * \retval #PSA_ERROR_INVALID_ARGUMENT
3561 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003562 * \retval #PSA_ERROR_NOT_PERMITTED
3563 * The #PSA_KEY_DERIVATION_INPUT_SECRET input was not provided through
3564 * a key.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003565 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003566 * The operation state is not valid (it must be active and completed
3567 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003568 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3569 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3570 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3571 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003572 * \retval #PSA_ERROR_CORRUPTION_DETECTED
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01003573 * \retval #PSA_ERROR_DATA_INVALID
3574 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01003575 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003576 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003577 * The library has not been previously initialized by psa_crypto_init().
3578 * It is implementation-dependent whether a failure to initialize
3579 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003580 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003581psa_status_t psa_key_derivation_output_key(
3582 const psa_key_attributes_t *attributes,
3583 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003584 mbedtls_svc_key_id_t *key);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003585
Gilles Peskine35675b62019-05-16 17:26:11 +02003586/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003587 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003588 * Aborting an operation frees all associated resources except for the \c
3589 * operation structure itself. Once aborted, the operation object can be reused
3590 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003591 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003592 * This function may be called at any time after the operation
3593 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003594 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003595 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3596 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003597 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003598 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003599 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003600 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003601 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3602 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003603 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003604 * \retval #PSA_ERROR_BAD_STATE
3605 * The library has not been previously initialized by psa_crypto_init().
3606 * It is implementation-dependent whether a failure to initialize
3607 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003608 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003609psa_status_t psa_key_derivation_abort(
3610 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003611
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003612/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003613 *
3614 * \warning The raw result of a key agreement algorithm such as finite-field
3615 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3616 * not be used directly as key material. It should instead be passed as
3617 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003618 * a key derivation, use psa_key_derivation_key_agreement() and other
3619 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003620 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003621 * \param alg The key agreement algorithm to compute
3622 * (\c PSA_ALG_XXX value such that
3623 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3624 * is true).
Ronald Croncf56a0a2020-08-04 09:51:30 +02003625 * \param private_key Identifier of the private key to use. It must
Ronald Cron96783552020-10-19 12:06:30 +02003626 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003627 * \param[in] peer_key Public key of the peer. It must be
3628 * in the same format that psa_import_key()
3629 * accepts. The standard formats for public
3630 * keys are documented in the documentation
3631 * of psa_export_public_key().
3632 * \param peer_key_length Size of \p peer_key in bytes.
3633 * \param[out] output Buffer where the decrypted message is to
3634 * be written.
3635 * \param output_size Size of the \c output buffer in bytes.
3636 * \param[out] output_length On success, the number of bytes
3637 * that make up the returned output.
3638 *
3639 * \retval #PSA_SUCCESS
3640 * Success.
3641 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003642 * \retval #PSA_ERROR_NOT_PERMITTED
3643 * \retval #PSA_ERROR_INVALID_ARGUMENT
3644 * \p alg is not a key agreement algorithm
3645 * \retval #PSA_ERROR_INVALID_ARGUMENT
3646 * \p private_key is not compatible with \p alg,
3647 * or \p peer_key is not valid for \p alg or not compatible with
3648 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003649 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3650 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003651 * \retval #PSA_ERROR_NOT_SUPPORTED
3652 * \p alg is not a supported key agreement algorithm.
3653 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3654 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3655 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003656 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003657 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003658 * \retval #PSA_ERROR_BAD_STATE
3659 * The library has not been previously initialized by psa_crypto_init().
3660 * It is implementation-dependent whether a failure to initialize
3661 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003662 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003663psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003664 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02003665 const uint8_t *peer_key,
3666 size_t peer_key_length,
3667 uint8_t *output,
3668 size_t output_size,
3669 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003670
Gilles Peskineea0fb492018-07-12 17:17:20 +02003671/**@}*/
3672
Gilles Peskineedd76872018-07-20 17:42:05 +02003673/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003674 * @{
3675 */
3676
3677/**
3678 * \brief Generate random bytes.
3679 *
3680 * \warning This function **can** fail! Callers MUST check the return status
3681 * and MUST NOT use the content of the output buffer if the return
3682 * status is not #PSA_SUCCESS.
3683 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003684 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003685 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003686 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003687 * \param output_size Number of bytes to generate and output.
3688 *
Gilles Peskine28538492018-07-11 17:34:00 +02003689 * \retval #PSA_SUCCESS
3690 * \retval #PSA_ERROR_NOT_SUPPORTED
3691 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003692 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003693 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3694 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003695 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003696 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003697 * The library has not been previously initialized by psa_crypto_init().
3698 * It is implementation-dependent whether a failure to initialize
3699 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003700 */
3701psa_status_t psa_generate_random(uint8_t *output,
3702 size_t output_size);
3703
3704/**
3705 * \brief Generate a key or key pair.
3706 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003707 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02003708 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003709 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003710 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003711 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003712 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003713 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003714 * the public exponent is 65537.
3715 * The modulus is a product of two probabilistic primes
3716 * between 2^{n-1} and 2^n where n is the bit size specified in the
3717 * attributes.
3718 *
Gilles Peskine20628592019-04-19 19:29:50 +02003719 * \param[in] attributes The attributes for the new key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003720 * \param[out] key On success, an identifier for the newly created
Ronald Cron4067d1c2020-10-19 13:34:38 +02003721 * key. For persistent keys, this is the key
3722 * identifier defined in \p attributes.
3723 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003724 *
Gilles Peskine28538492018-07-11 17:34:00 +02003725 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003726 * Success.
3727 * If the key is persistent, the key material and the key's metadata
3728 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003729 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003730 * This is an attempt to create a persistent key, and there is
3731 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003732 * \retval #PSA_ERROR_NOT_SUPPORTED
3733 * \retval #PSA_ERROR_INVALID_ARGUMENT
3734 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3735 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3736 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3737 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003738 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003739 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01003740 * \retval #PSA_ERROR_DATA_INVALID
3741 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01003742 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003743 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003744 * The library has not been previously initialized by psa_crypto_init().
3745 * It is implementation-dependent whether a failure to initialize
3746 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003747 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003748psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003749 mbedtls_svc_key_id_t *key);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003750
3751/**@}*/
3752
Gilles Peskinee59236f2018-01-27 23:32:46 +01003753#ifdef __cplusplus
3754}
3755#endif
3756
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003757/* The file "crypto_sizes.h" contains definitions for size calculation
3758 * macros whose definitions are implementation-specific. */
3759#include "crypto_sizes.h"
3760
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003761/* The file "crypto_struct.h" contains definitions for
3762 * implementation-specific structs that are declared above. */
3763#include "crypto_struct.h"
3764
3765/* The file "crypto_extra.h" contains vendor-specific definitions. This
3766 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003767#include "crypto_extra.h"
3768
3769#endif /* PSA_CRYPTO_H */