blob: a3a821d45204da04dde9eaa7bd6e85881b1c8297 [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/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +020089 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +020090 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
Gilles Peskine105f67f2019-07-23 18:16:05 +020096/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +020097 * @{
98 */
99
Gilles Peskinea0c06552019-05-21 15:54:54 +0200100/** \def PSA_KEY_ATTRIBUTES_INIT
101 *
102 * This macro returns a suitable initializer for a key attribute structure
103 * of type #psa_key_attributes_t.
104 */
105#ifdef __DOXYGEN_ONLY__
106/* This is an example definition for documentation purposes.
107 * Implementations should define a suitable value in `crypto_struct.h`.
108 */
109#define PSA_KEY_ATTRIBUTES_INIT {0}
110#endif
111
112/** Return an initial value for a key attributes structure.
113 */
114static psa_key_attributes_t psa_key_attributes_init(void);
115
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200116/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200117 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200118 * If the attribute structure currently declares the key as volatile (which
119 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200120 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200121 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200122 * This function does not access storage, it merely stores the given
123 * value in the structure.
124 * The persistent key will be written to storage when the attribute
125 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200126 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200127 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200128 *
Gilles Peskine20628592019-04-19 19:29:50 +0200129 * This function may be declared as `static` (i.e. without external
130 * linkage). This function may be provided as a function-like macro,
131 * but in this case it must evaluate each of its arguments exactly once.
132 *
133 * \param[out] attributes The attribute structure to write to.
134 * \param id The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200135 */
136static void psa_set_key_id(psa_key_attributes_t *attributes,
137 psa_key_id_t id);
138
139/** Set the location of a persistent key.
140 *
141 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200142 * with psa_set_key_id(). By default, a key that has a persistent identifier
143 * is stored in the default storage area identifier by
144 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
145 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200146 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200147 * This function does not access storage, it merely stores the given
148 * value in the structure.
149 * The persistent key will be written to storage when the attribute
150 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200151 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200152 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200153 *
154 * This function may be declared as `static` (i.e. without external
155 * linkage). This function may be provided as a function-like macro,
156 * but in this case it must evaluate each of its arguments exactly once.
157 *
158 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200159 * \param lifetime The lifetime for the key.
160 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200161 * key will be volatile, and the key identifier
162 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200163 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200164static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
165 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200166
Gilles Peskine20628592019-04-19 19:29:50 +0200167/** Retrieve the key identifier from key attributes.
168 *
169 * This function may be declared as `static` (i.e. without external
170 * linkage). This function may be provided as a function-like macro,
171 * but in this case it must evaluate its argument exactly once.
172 *
173 * \param[in] attributes The key attribute structure to query.
174 *
175 * \return The persistent identifier stored in the attribute structure.
176 * This value is unspecified if the attribute structure declares
177 * the key as volatile.
178 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200179static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
180
Gilles Peskine20628592019-04-19 19:29:50 +0200181/** Retrieve the lifetime from key attributes.
182 *
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 its argument exactly once.
186 *
187 * \param[in] attributes The key attribute structure to query.
188 *
189 * \return The lifetime value stored in the attribute structure.
190 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200191static psa_key_lifetime_t psa_get_key_lifetime(
192 const psa_key_attributes_t *attributes);
193
Gilles Peskine20628592019-04-19 19:29:50 +0200194/** Declare usage flags for a key.
195 *
196 * Usage flags are part of a key's usage policy. They encode what
197 * kind of operations are permitted on the key. For more details,
198 * refer to the documentation of the type #psa_key_usage_t.
199 *
200 * This function overwrites any usage flags
201 * previously set in \p attributes.
202 *
203 * This function may be declared as `static` (i.e. without external
204 * linkage). This function may be provided as a function-like macro,
205 * but in this case it must evaluate each of its arguments exactly once.
206 *
207 * \param[out] attributes The attribute structure to write to.
208 * \param usage_flags The usage flags to write.
209 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200210static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
211 psa_key_usage_t usage_flags);
212
Gilles Peskine20628592019-04-19 19:29:50 +0200213/** Retrieve the usage flags from key attributes.
214 *
215 * This function may be declared as `static` (i.e. without external
216 * linkage). This function may be provided as a function-like macro,
217 * but in this case it must evaluate its argument exactly once.
218 *
219 * \param[in] attributes The key attribute structure to query.
220 *
221 * \return The usage flags stored in the attribute structure.
222 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200223static psa_key_usage_t psa_get_key_usage_flags(
224 const psa_key_attributes_t *attributes);
225
Gilles Peskine20628592019-04-19 19:29:50 +0200226/** Declare the permitted algorithm policy for a key.
227 *
228 * The permitted algorithm policy of a key encodes which algorithm or
229 * algorithms are permitted to be used with this key.
230 *
231 * This function overwrites any algorithm policy
232 * previously set in \p attributes.
233 *
234 * This function may be declared as `static` (i.e. without external
235 * linkage). This function may be provided as a function-like macro,
236 * but in this case it must evaluate each of its arguments exactly once.
237 *
238 * \param[out] attributes The attribute structure to write to.
239 * \param alg The permitted algorithm policy to write.
240 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200241static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
242 psa_algorithm_t alg);
243
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100244
Gilles Peskine20628592019-04-19 19:29:50 +0200245/** Retrieve the algorithm policy from key attributes.
246 *
247 * This function may be declared as `static` (i.e. without external
248 * linkage). This function may be provided as a function-like macro,
249 * but in this case it must evaluate its argument exactly once.
250 *
251 * \param[in] attributes The key attribute structure to query.
252 *
253 * \return The algorithm stored in the attribute structure.
254 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200255static psa_algorithm_t psa_get_key_algorithm(
256 const psa_key_attributes_t *attributes);
257
Gilles Peskine20628592019-04-19 19:29:50 +0200258/** Declare the type of a key.
259 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200260 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200261 * previously set in \p attributes.
262 *
263 * This function may be declared as `static` (i.e. without external
264 * linkage). This function may be provided as a function-like macro,
265 * but in this case it must evaluate each of its arguments exactly once.
266 *
267 * \param[out] attributes The attribute structure to write to.
268 * \param type The key type to write.
269 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200270static void psa_set_key_type(psa_key_attributes_t *attributes,
271 psa_key_type_t type);
272
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100273
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200274/** Declare the size of a key.
275 *
276 * This function overwrites any key size previously set in \p attributes.
277 *
278 * This function may be declared as `static` (i.e. without external
279 * linkage). This function may be provided as a function-like macro,
280 * but in this case it must evaluate each of its arguments exactly once.
281 *
282 * \param[out] attributes The attribute structure to write to.
283 * \param bits The key size in bits.
284 */
285static void psa_set_key_bits(psa_key_attributes_t *attributes,
286 size_t bits);
287
Gilles Peskine20628592019-04-19 19:29:50 +0200288/** Retrieve the key type from key attributes.
289 *
290 * This function may be declared as `static` (i.e. without external
291 * linkage). This function may be provided as a function-like macro,
292 * but in this case it must evaluate its argument exactly once.
293 *
294 * \param[in] attributes The key attribute structure to query.
295 *
296 * \return The key type stored in the attribute structure.
297 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200298static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
299
Gilles Peskine20628592019-04-19 19:29:50 +0200300/** Retrieve the key size from key attributes.
301 *
302 * This function may be declared as `static` (i.e. without external
303 * linkage). This function may be provided as a function-like macro,
304 * but in this case it must evaluate its argument exactly once.
305 *
306 * \param[in] attributes The key attribute structure to query.
307 *
308 * \return The key size stored in the attribute structure, in bits.
309 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200310static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
311
Gilles Peskine20628592019-04-19 19:29:50 +0200312/** Retrieve the attributes of a key.
313 *
314 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200315 * psa_reset_key_attributes(). It then copies the attributes of
316 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200317 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200318 * \note This function may allocate memory or other resources.
319 * Once you have called this function on an attribute structure,
320 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200321 *
Gilles Peskine20628592019-04-19 19:29:50 +0200322 * \param[in] handle Handle to the key to query.
323 * \param[in,out] attributes On success, the attributes of the key.
324 * On failure, equivalent to a
325 * freshly-initialized structure.
326 *
327 * \retval #PSA_SUCCESS
328 * \retval #PSA_ERROR_INVALID_HANDLE
329 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
330 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100331 * \retval #PSA_ERROR_CORRUPTION_DETECTED
332 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100333 * \retval #PSA_ERROR_BAD_STATE
334 * The library has not been previously initialized by psa_crypto_init().
335 * It is implementation-dependent whether a failure to initialize
336 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200337 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200338psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
339 psa_key_attributes_t *attributes);
340
Gilles Peskine20628592019-04-19 19:29:50 +0200341/** Reset a key attribute structure to a freshly initialized state.
342 *
343 * You must initialize the attribute structure as described in the
344 * documentation of the type #psa_key_attributes_t before calling this
345 * function. Once the structure has been initialized, you may call this
346 * function at any time.
347 *
348 * This function frees any auxiliary resources that the structure
349 * may contain.
350 *
351 * \param[in,out] attributes The attribute structure to reset.
352 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200353void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200354
Gilles Peskine87a5e562019-04-17 12:28:25 +0200355/**@}*/
356
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357/** \defgroup key_management Key management
358 * @{
359 */
360
Gilles Peskinef535eb22018-11-30 14:08:36 +0100361/** Open a handle to an existing persistent key.
362 *
Gilles Peskine4754cde2019-05-21 15:56:29 +0200363 * Open a handle to a persistent key. A key is persistent if it was created
364 * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
365 * always has a nonzero key identifier, set with psa_set_key_id() when
366 * creating the key. Implementations may provide additional pre-provisioned
Andrew Thoelke203491c2019-08-21 17:55:30 +0100367 * keys that can be opened with psa_open_key(). Such keys have a key identifier
368 * in the vendor range, as documented in the description of #psa_key_id_t.
Gilles Peskine4754cde2019-05-21 15:56:29 +0200369 *
370 * The application must eventually close the handle with psa_close_key()
371 * to release associated resources. If the application dies without calling
372 * psa_close_key(), the implementation should perform the equivalent of a
373 * call to psa_close_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100374 *
Andrew Thoelke9741b112019-08-21 18:20:41 +0100375 * Some implementations permit an application to open the same key multiple
376 * times. Applications that rely on this behavior will not be portable to
377 * implementations that only permit a single key handle to be opened. See
378 * also :ref:\`key-handles\`.
379 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100380 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100381 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100382 *
383 * \retval #PSA_SUCCESS
384 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100385 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100386 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Andrew Thoelke9741b112019-08-21 18:20:41 +0100387 * The implementation does not have sufficient resources to open the
388 * key. This can be due to reaching an implementation limit on the
389 * number of open keys, the number of open key handles, or available
390 * memory.
David Saadab4ecc272019-02-14 13:48:10 +0200391 * \retval #PSA_ERROR_DOES_NOT_EXIST
Andrew Thoelke9741b112019-08-21 18:20:41 +0100392 * There is no persistent key with key identifier \p id.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100393 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrew Thoelke9741b112019-08-21 18:20:41 +0100394 * \p id is not a valid persistent key identifier.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100395 * \retval #PSA_ERROR_NOT_PERMITTED
396 * The specified key exists, but the application does not have the
397 * permission to access it. Note that this specification does not
398 * define any way to create such a key, but it may be possible
399 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200400 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawd789dc12019-08-12 15:06:48 +0100401 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine225010f2019-05-06 18:44:55 +0200402 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100403 * \retval #PSA_ERROR_BAD_STATE
404 * The library has not been previously initialized by psa_crypto_init().
405 * It is implementation-dependent whether a failure to initialize
406 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100407 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200408psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100409 psa_key_handle_t *handle);
410
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100411
Gilles Peskinef535eb22018-11-30 14:08:36 +0100412/** Close a key handle.
413 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100414 * If the handle designates a volatile key, this will destroy the key material
415 * and free all associated resources, just like psa_destroy_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100416 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100417 * If this is the last open handle to a persistent key, then closing the handle
418 * will free all resources associated with the key in volatile memory. The key
419 * data in persistent storage is not affected and can be opened again later
420 * with a call to psa_open_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100421 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100422 * Closing the key handle makes the handle invalid, and the key handle
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100423 * must not be used again by the application.
Andrew Thoelke3daba812019-08-21 22:46:56 +0100424 *
425 * If the key is currently in use in a multipart operation, then closing the
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100426 * last remaining handle to the key will abort the multipart operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100427 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100428 * \param handle The key handle to close.
429 *
430 * \retval #PSA_SUCCESS
431 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100432 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100433 * \retval #PSA_ERROR_BAD_STATE
434 * The library has not been previously initialized by psa_crypto_init().
435 * It is implementation-dependent whether a failure to initialize
436 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100437 */
438psa_status_t psa_close_key(psa_key_handle_t handle);
439
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100440/**@}*/
441
442/** \defgroup import_export Key import and export
443 * @{
444 */
445
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100446/**
447 * \brief Import a key in binary format.
448 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100449 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100450 * documentation of psa_export_public_key() for the format of public keys
451 * and to the documentation of psa_export_key() for the format for
452 * other key types.
453 *
454 * This specification supports a single format for each key type.
455 * Implementations may support other formats as long as the standard
456 * format is supported. Implementations that support other formats
457 * should ensure that the formats are clearly unambiguous so as to
458 * minimize the risk that an invalid input is accidentally interpreted
459 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100460 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100461
Gilles Peskine20628592019-04-19 19:29:50 +0200462 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200463 * The key size is always determined from the
464 * \p data buffer.
465 * If the key size in \p attributes is nonzero,
466 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200467 * \param[out] handle On success, a handle to the newly created key.
468 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100469 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200470 * buffer is interpreted according to the type declared
471 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200472 * All implementations must support at least the format
473 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100474 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200475 * the chosen type. Implementations may allow other
476 * formats, but should be conservative: implementations
477 * should err on the side of rejecting content if it
478 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200479 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100480 *
Gilles Peskine28538492018-07-11 17:34:00 +0200481 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100482 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100483 * If the key is persistent, the key material and the key's metadata
484 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200485 * \retval #PSA_ERROR_ALREADY_EXISTS
486 * This is an attempt to create a persistent key, and there is
487 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200488 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200489 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200490 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200491 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200492 * The key attributes, as a whole, are invalid.
493 * \retval #PSA_ERROR_INVALID_ARGUMENT
494 * The key data is not correctly formatted.
495 * \retval #PSA_ERROR_INVALID_ARGUMENT
496 * The size in \p attributes is nonzero and does not match the size
497 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200498 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
499 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
500 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100501 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200502 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200503 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300504 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300505 * The library has not been previously initialized by psa_crypto_init().
506 * It is implementation-dependent whether a failure to initialize
507 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100508 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200509psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100510 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200511 size_t data_length,
512 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100513
514/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100515 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200516 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100517 * This function destroys a key from both volatile
Gilles Peskine154bd952018-04-19 08:38:16 +0200518 * memory and, if applicable, non-volatile storage. Implementations shall
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100519 * make a best effort to ensure that that the key material cannot be recovered.
Gilles Peskine154bd952018-04-19 08:38:16 +0200520 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100521 * This function also erases any metadata such as policies and frees all
522 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200523 *
Andrew Thoelke07f16b72019-08-21 22:48:47 +0100524 * Destroying a key will invalidate all existing handles to the key.
525 *
526 * If the key is currently in use in a multipart operation, then destroying the
527 * key will abort the multipart operation.
528 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100529 * \param handle Handle to the key to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100530 *
Gilles Peskine28538492018-07-11 17:34:00 +0200531 * \retval #PSA_SUCCESS
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100532 * The key material has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200533 * \retval #PSA_ERROR_NOT_PERMITTED
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100534 * The key cannot be erased because it is
Gilles Peskine65eb8582018-04-19 08:28:58 +0200535 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100536 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200537 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200538 * There was an failure in communication with the cryptoprocessor.
539 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200540 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200541 * The storage is corrupted. Implementations shall make a best effort
542 * to erase key material even in this stage, however applications
543 * should be aware that it may be impossible to guarantee that the
544 * key material is not recoverable in such cases.
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200545 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200546 * An unexpected condition which is not a storage corruption or
547 * a communication failure occurred. The cryptoprocessor may have
548 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300549 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300550 * The library has not been previously initialized by psa_crypto_init().
551 * It is implementation-dependent whether a failure to initialize
552 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100553 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100554psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100555
556/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100557 * \brief Export a key in binary format.
558 *
559 * The output of this function can be passed to psa_import_key() to
560 * create an equivalent object.
561 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100562 * If the implementation of psa_import_key() supports other formats
563 * beyond the format specified here, the output from psa_export_key()
564 * must use the representation specified here, not the original
565 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100566 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100567 * For standard key types, the output format is as follows:
568 *
569 * - For symmetric keys (including MAC keys), the format is the
570 * raw bytes of the key.
571 * - For DES, the key data consists of 8 bytes. The parity bits must be
572 * correct.
573 * - For Triple-DES, the format is the concatenation of the
574 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200575 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200576 * is the non-encrypted DER encoding of the representation defined by
577 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
578 * ```
579 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200580 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200581 * modulus INTEGER, -- n
582 * publicExponent INTEGER, -- e
583 * privateExponent INTEGER, -- d
584 * prime1 INTEGER, -- p
585 * prime2 INTEGER, -- q
586 * exponent1 INTEGER, -- d mod (p-1)
587 * exponent2 INTEGER, -- d mod (q-1)
588 * coefficient INTEGER, -- (inverse of q) mod p
589 * }
590 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200591 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200592 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100593 * a representation of the private value as a `ceiling(m/8)`-byte string
594 * where `m` is the bit size associated with the curve, i.e. the bit size
595 * of the order of the curve's coordinate field. This byte string is
596 * in little-endian order for Montgomery curves (curve types
597 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
598 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
599 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100600 * This is the content of the `privateKey` field of the `ECPrivateKey`
601 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200602 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200603 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000604 * format is the representation of the private key `x` as a big-endian byte
605 * string. The length of the byte string is the private key size in bytes
606 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200607 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
608 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100609 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200610 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
611 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100612 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200613 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200614 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200615 * \param[out] data_length On success, the number of bytes
616 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100617 *
Gilles Peskine28538492018-07-11 17:34:00 +0200618 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100619 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200620 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200621 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100622 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200623 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
624 * The size of the \p data buffer is too small. You can determine a
625 * sufficient buffer size by calling
626 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
627 * where \c type is the key type
628 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200629 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
630 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200631 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100632 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100633 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300634 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300635 * The library has not been previously initialized by psa_crypto_init().
636 * It is implementation-dependent whether a failure to initialize
637 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100638 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100639psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100640 uint8_t *data,
641 size_t data_size,
642 size_t *data_length);
643
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100644/**
645 * \brief Export a public key or the public part of a key pair in binary format.
646 *
647 * The output of this function can be passed to psa_import_key() to
648 * create an object that is equivalent to the public key.
649 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000650 * This specification supports a single format for each key type.
651 * Implementations may support other formats as long as the standard
652 * format is supported. Implementations that support other formats
653 * should ensure that the formats are clearly unambiguous so as to
654 * minimize the risk that an invalid input is accidentally interpreted
655 * according to a different format.
656 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000657 * For standard key types, the output format is as follows:
658 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
659 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
660 * ```
661 * RSAPublicKey ::= SEQUENCE {
662 * modulus INTEGER, -- n
663 * publicExponent INTEGER } -- e
664 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000665 * - For elliptic curve public keys (key types for which
666 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
667 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
668 * Let `m` be the bit size associated with the curve, i.e. the bit size of
669 * `q` for a curve over `F_q`. The representation consists of:
670 * - The byte 0x04;
671 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
672 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200673 * - For Diffie-Hellman key exchange public keys (key types for which
674 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000675 * the format is the representation of the public key `y = g^x mod p` as a
676 * big-endian byte string. The length of the byte string is the length of the
677 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100678 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200679 * Exporting a public key object or the public part of a key pair is
680 * always permitted, regardless of the key's usage flags.
681 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100682 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200683 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200684 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200685 * \param[out] data_length On success, the number of bytes
686 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100687 *
Gilles Peskine28538492018-07-11 17:34:00 +0200688 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100689 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200690 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200691 * The key is neither a public key nor a key pair.
692 * \retval #PSA_ERROR_NOT_SUPPORTED
693 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
694 * The size of the \p data buffer is too small. You can determine a
695 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200696 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200697 * where \c type is the key type
698 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200699 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
700 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200701 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100702 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100703 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300704 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300705 * The library has not been previously initialized by psa_crypto_init().
706 * It is implementation-dependent whether a failure to initialize
707 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100708 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100709psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100710 uint8_t *data,
711 size_t data_size,
712 size_t *data_length);
713
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100714/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100715 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100716 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000717 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100718 * This function is primarily useful to copy a key from one location
719 * to another, since it populates a key using the material from
720 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200721 *
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100722 * This function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100723 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100724 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200725 * The policy on the source key must have the usage flag
726 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200727 * This flag is sufficient to permit the copy if the key has the lifetime
728 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
729 * Some secure elements do not provide a way to copy a key without
730 * making it extractable from the secure element. If a key is located
731 * in such a secure element, then the key must have both usage flags
732 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
733 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200734 *
Gilles Peskine20628592019-04-19 19:29:50 +0200735 * The resulting key may only be used in a way that conforms to
736 * both the policy of the original key and the policy specified in
737 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100738 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200739 * usage flags on the source policy and the usage flags in \p attributes.
740 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100741 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200742 * - If either of the policies allows an algorithm and the other policy
743 * allows a wildcard-based algorithm policy that includes this algorithm,
744 * the resulting key allows the same algorithm.
745 * - If the policies do not allow any algorithm in common, this function
746 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200747 *
Gilles Peskine20628592019-04-19 19:29:50 +0200748 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100749 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200750 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100751 * \param source_handle The key to copy. It must be a valid key handle.
Gilles Peskine20628592019-04-19 19:29:50 +0200752 * \param[in] attributes The attributes for the new key.
753 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200754 * - The key type and size may be 0. If either is
755 * nonzero, it must match the corresponding
756 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200757 * - The key location (the lifetime and, for
758 * persistent keys, the key identifier) is
759 * used directly.
760 * - The policy constraints (usage flags and
761 * algorithm policy) are combined from
762 * the source key and \p attributes so that
763 * both sets of restrictions apply, as
764 * described in the documentation of this function.
765 * \param[out] target_handle On success, a handle to the newly created key.
766 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200767 *
768 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100769 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200770 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200771 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200772 * This is an attempt to create a persistent key, and there is
773 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200774 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200775 * The lifetime or identifier in \p attributes are invalid.
776 * \retval #PSA_ERROR_INVALID_ARGUMENT
777 * The policy constraints on the source and specified in
778 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200779 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200780 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200781 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100782 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200783 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
784 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100785 * The source key is not exportable and its lifetime does not
786 * allow copying it to the target's lifetime.
787 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
788 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200789 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
790 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shaw60b03202019-08-06 17:26:16 +0100791 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200792 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100793 * \retval #PSA_ERROR_BAD_STATE
794 * The library has not been previously initialized by psa_crypto_init().
795 * It is implementation-dependent whether a failure to initialize
796 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100797 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100798psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200799 const psa_key_attributes_t *attributes,
800 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100801
802/**@}*/
803
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100804/** \defgroup hash Message digests
805 * @{
806 */
807
Gilles Peskine69647a42019-01-14 20:18:12 +0100808/** Calculate the hash (digest) of a message.
809 *
810 * \note To verify the hash of a message against an
811 * expected value, use psa_hash_compare() instead.
812 *
813 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
814 * such that #PSA_ALG_IS_HASH(\p alg) is true).
815 * \param[in] input Buffer containing the message to hash.
816 * \param input_length Size of the \p input buffer in bytes.
817 * \param[out] hash Buffer where the hash is to be written.
818 * \param hash_size Size of the \p hash buffer in bytes.
819 * \param[out] hash_length On success, the number of bytes
820 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100821 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100822 *
823 * \retval #PSA_SUCCESS
824 * Success.
825 * \retval #PSA_ERROR_NOT_SUPPORTED
826 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100827 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
828 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100829 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
830 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
831 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200832 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100833 * \retval #PSA_ERROR_BAD_STATE
834 * The library has not been previously initialized by psa_crypto_init().
835 * It is implementation-dependent whether a failure to initialize
836 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100837 */
838psa_status_t psa_hash_compute(psa_algorithm_t alg,
839 const uint8_t *input,
840 size_t input_length,
841 uint8_t *hash,
842 size_t hash_size,
843 size_t *hash_length);
844
845/** Calculate the hash (digest) of a message and compare it with a
846 * reference value.
847 *
848 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
849 * such that #PSA_ALG_IS_HASH(\p alg) is true).
850 * \param[in] input Buffer containing the message to hash.
851 * \param input_length Size of the \p input buffer in bytes.
852 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100853 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100854 *
855 * \retval #PSA_SUCCESS
856 * The expected hash is identical to the actual hash of the input.
857 * \retval #PSA_ERROR_INVALID_SIGNATURE
858 * The hash of the message was calculated successfully, but it
859 * differs from the expected hash.
860 * \retval #PSA_ERROR_NOT_SUPPORTED
861 * \p alg is not supported or is not a hash algorithm.
862 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
863 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
864 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200865 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100866 * \retval #PSA_ERROR_BAD_STATE
867 * The library has not been previously initialized by psa_crypto_init().
868 * It is implementation-dependent whether a failure to initialize
869 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100870 */
871psa_status_t psa_hash_compare(psa_algorithm_t alg,
872 const uint8_t *input,
873 size_t input_length,
874 const uint8_t *hash,
875 const size_t hash_length);
876
Gilles Peskine308b91d2018-02-08 09:47:44 +0100877/** The type of the state data structure for multipart hash operations.
878 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000879 * Before calling any function on a hash operation object, the application must
880 * initialize it by any of the following means:
881 * - Set the structure to all-bits-zero, for example:
882 * \code
883 * psa_hash_operation_t operation;
884 * memset(&operation, 0, sizeof(operation));
885 * \endcode
886 * - Initialize the structure to logical zero values, for example:
887 * \code
888 * psa_hash_operation_t operation = {0};
889 * \endcode
890 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
891 * for example:
892 * \code
893 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
894 * \endcode
895 * - Assign the result of the function psa_hash_operation_init()
896 * to the structure, for example:
897 * \code
898 * psa_hash_operation_t operation;
899 * operation = psa_hash_operation_init();
900 * \endcode
901 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100902 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100903 * make any assumptions about the content of this structure except
904 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100905typedef struct psa_hash_operation_s psa_hash_operation_t;
906
Jaeden Amero6a25b412019-01-04 11:47:44 +0000907/** \def PSA_HASH_OPERATION_INIT
908 *
909 * This macro returns a suitable initializer for a hash operation object
910 * of type #psa_hash_operation_t.
911 */
912#ifdef __DOXYGEN_ONLY__
913/* This is an example definition for documentation purposes.
914 * Implementations should define a suitable value in `crypto_struct.h`.
915 */
916#define PSA_HASH_OPERATION_INIT {0}
917#endif
918
919/** Return an initial value for a hash operation object.
920 */
921static psa_hash_operation_t psa_hash_operation_init(void);
922
Gilles Peskinef45adda2019-01-14 18:29:18 +0100923/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100924 *
925 * The sequence of operations to calculate a hash (message digest)
926 * is as follows:
927 * -# Allocate an operation object which will be passed to all the functions
928 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000929 * -# Initialize the operation object with one of the methods described in the
930 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200931 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100932 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100933 * of the message each time. The hash that is calculated is the hash
934 * of the concatenation of these messages in order.
935 * -# To calculate the hash, call psa_hash_finish().
936 * To compare the hash with an expected value, call psa_hash_verify().
937 *
938 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000939 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100940 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200941 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100942 * eventually terminate the operation. The following events terminate an
943 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100944 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100945 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100946 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000947 * \param[in,out] operation The operation object to set up. It must have
948 * been initialized as per the documentation for
949 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200950 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
951 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100952 *
Gilles Peskine28538492018-07-11 17:34:00 +0200953 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100954 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200955 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200956 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100957 * \retval #PSA_ERROR_BAD_STATE
958 * The operation state is not valid (already set up and not
959 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200960 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
961 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
962 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200963 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100964 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawe970d652019-08-08 14:40:04 +0100965 * \p operation is either not initialized or is in use
966 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100967 * The library has not been previously initialized by psa_crypto_init().
968 * It is implementation-dependent whether a failure to initialize
969 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100970 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200971psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100972 psa_algorithm_t alg);
973
Gilles Peskine308b91d2018-02-08 09:47:44 +0100974/** Add a message fragment to a multipart hash operation.
975 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200976 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100977 *
978 * If this function returns an error status, the operation becomes inactive.
979 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200980 * \param[in,out] operation Active hash operation.
981 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200982 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100983 *
Gilles Peskine28538492018-07-11 17:34:00 +0200984 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100985 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200986 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100987 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200988 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
989 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
990 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200991 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100992 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +0100993 * The operation state is not valid.
994 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100995 * The library has not been previously initialized by psa_crypto_init().
996 * It is implementation-dependent whether a failure to initialize
997 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100998 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100999psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1000 const uint8_t *input,
1001 size_t input_length);
1002
Gilles Peskine308b91d2018-02-08 09:47:44 +01001003/** Finish the calculation of the hash of a message.
1004 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001005 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001006 * This function calculates the hash of the message formed by concatenating
1007 * the inputs passed to preceding calls to psa_hash_update().
1008 *
1009 * When this function returns, the operation becomes inactive.
1010 *
1011 * \warning Applications should not call this function if they expect
1012 * a specific value for the hash. Call psa_hash_verify() instead.
1013 * Beware that comparing integrity or authenticity data such as
1014 * hash values with a function such as \c memcmp is risky
1015 * because the time taken by the comparison may leak information
1016 * about the hashed data which could allow an attacker to guess
1017 * a valid hash and thereby bypass security controls.
1018 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001019 * \param[in,out] operation Active hash operation.
1020 * \param[out] hash Buffer where the hash is to be written.
1021 * \param hash_size Size of the \p hash buffer in bytes.
1022 * \param[out] hash_length On success, the number of bytes
1023 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001024 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001025 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001026 *
Gilles Peskine28538492018-07-11 17:34:00 +02001027 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001028 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001029 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001030 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001031 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001032 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001033 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001034 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001035 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1036 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1037 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001038 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001039 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001040 * The operation state is not valid.
1041 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001042 * The library has not been previously initialized by psa_crypto_init().
1043 * It is implementation-dependent whether a failure to initialize
1044 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001045 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001046psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1047 uint8_t *hash,
1048 size_t hash_size,
1049 size_t *hash_length);
1050
Gilles Peskine308b91d2018-02-08 09:47:44 +01001051/** Finish the calculation of the hash of a message and compare it with
1052 * an expected value.
1053 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001054 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001055 * This function calculates the hash of the message formed by concatenating
1056 * the inputs passed to preceding calls to psa_hash_update(). It then
1057 * compares the calculated hash with the expected hash passed as a
1058 * parameter to this function.
1059 *
1060 * When this function returns, the operation becomes inactive.
1061 *
Gilles Peskine19067982018-03-20 17:54:53 +01001062 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001063 * comparison between the actual hash and the expected hash is performed
1064 * in constant time.
1065 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001066 * \param[in,out] operation Active hash operation.
1067 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001068 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001069 *
Gilles Peskine28538492018-07-11 17:34:00 +02001070 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001071 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001072 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001073 * The hash of the message was calculated successfully, but it
1074 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001075 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001076 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001077 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1078 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1079 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001080 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001081 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001082 * The operation state is not valid.
1083 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001084 * The library has not been previously initialized by psa_crypto_init().
1085 * It is implementation-dependent whether a failure to initialize
1086 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001087 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001088psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1089 const uint8_t *hash,
1090 size_t hash_length);
1091
Gilles Peskine308b91d2018-02-08 09:47:44 +01001092/** Abort a hash operation.
1093 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001094 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001095 * \p operation structure itself. Once aborted, the operation object
1096 * can be reused for another operation by calling
1097 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001098 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001099 * You may call this function any time after the operation object has
1100 * been initialized by any of the following methods:
1101 * - A call to psa_hash_setup(), whether it succeeds or not.
1102 * - Initializing the \c struct to all-bits-zero.
1103 * - Initializing the \c struct to logical zeros, e.g.
1104 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001105 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001106 * In particular, calling psa_hash_abort() after the operation has been
1107 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1108 * psa_hash_verify() is safe and has no effect.
1109 *
1110 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001111 *
Gilles Peskine28538492018-07-11 17:34:00 +02001112 * \retval #PSA_SUCCESS
1113 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001114 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001115 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1116 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001117 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001118 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001119 * The operation state is not valid.
1120 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001121 * The library has not been previously initialized by psa_crypto_init().
1122 * It is implementation-dependent whether a failure to initialize
1123 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001124 */
1125psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001126
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001127/** Clone a hash operation.
1128 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001129 * This function copies the state of an ongoing hash operation to
1130 * a new operation object. In other words, this function is equivalent
1131 * to calling psa_hash_setup() on \p target_operation with the same
1132 * algorithm that \p source_operation was set up for, then
1133 * psa_hash_update() on \p target_operation with the same input that
1134 * that was passed to \p source_operation. After this function returns, the
1135 * two objects are independent, i.e. subsequent calls involving one of
1136 * the objects do not affect the other object.
1137 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001138 * \param[in] source_operation The active hash operation to clone.
1139 * \param[in,out] target_operation The operation object to set up.
1140 * It must be initialized but not active.
1141 *
1142 * \retval #PSA_SUCCESS
1143 * \retval #PSA_ERROR_BAD_STATE
1144 * \p source_operation is not an active hash operation.
1145 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001146 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001147 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1148 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001149 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001150 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001151 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001152 * The operation state is either not initialized or has already been setup.
1153 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001154 * The library has not been previously initialized by psa_crypto_init().
1155 * It is implementation-dependent whether a failure to initialize
1156 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001157 */
1158psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1159 psa_hash_operation_t *target_operation);
1160
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001161/**@}*/
1162
Gilles Peskine8c9def32018-02-08 10:02:12 +01001163/** \defgroup MAC Message authentication codes
1164 * @{
1165 */
1166
Gilles Peskine69647a42019-01-14 20:18:12 +01001167/** Calculate the MAC (message authentication code) of a message.
1168 *
1169 * \note To verify the MAC of a message against an
1170 * expected value, use psa_mac_verify() instead.
1171 * Beware that comparing integrity or authenticity data such as
1172 * MAC values with a function such as \c memcmp is risky
1173 * because the time taken by the comparison may leak information
1174 * about the MAC value which could allow an attacker to guess
1175 * a valid MAC and thereby bypass security controls.
1176 *
1177 * \param handle Handle to the key to use for the operation.
1178 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001179 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001180 * \param[in] input Buffer containing the input message.
1181 * \param input_length Size of the \p input buffer in bytes.
1182 * \param[out] mac Buffer where the MAC value is to be written.
1183 * \param mac_size Size of the \p mac buffer in bytes.
1184 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001185 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001186 *
1187 * \retval #PSA_SUCCESS
1188 * Success.
1189 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001190 * \retval #PSA_ERROR_NOT_PERMITTED
1191 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001192 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001193 * \retval #PSA_ERROR_NOT_SUPPORTED
1194 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001195 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1196 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001197 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1198 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1199 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001200 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001201 * \retval #PSA_ERROR_STORAGE_FAILURE
1202 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001203 * \retval #PSA_ERROR_BAD_STATE
1204 * The library has not been previously initialized by psa_crypto_init().
1205 * It is implementation-dependent whether a failure to initialize
1206 * results in this error code.
1207 */
1208psa_status_t psa_mac_compute(psa_key_handle_t handle,
1209 psa_algorithm_t alg,
1210 const uint8_t *input,
1211 size_t input_length,
1212 uint8_t *mac,
1213 size_t mac_size,
1214 size_t *mac_length);
1215
1216/** Calculate the MAC of a message and compare it with a reference value.
1217 *
1218 * \param handle Handle to the key to use for the operation.
1219 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001220 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001221 * \param[in] input Buffer containing the input message.
1222 * \param input_length Size of the \p input buffer in bytes.
1223 * \param[out] mac Buffer containing the expected MAC value.
1224 * \param mac_length Size of the \p mac buffer in bytes.
1225 *
1226 * \retval #PSA_SUCCESS
1227 * The expected MAC is identical to the actual MAC of the input.
1228 * \retval #PSA_ERROR_INVALID_SIGNATURE
1229 * The MAC of the message was calculated successfully, but it
1230 * differs from the expected value.
1231 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001232 * \retval #PSA_ERROR_NOT_PERMITTED
1233 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001234 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001235 * \retval #PSA_ERROR_NOT_SUPPORTED
1236 * \p alg is not supported or is not a MAC algorithm.
1237 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1238 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1239 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001240 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001241 * \retval #PSA_ERROR_STORAGE_FAILURE
1242 * The key could not be retrieved from storage.
1243 * \retval #PSA_ERROR_BAD_STATE
1244 * The library has not been previously initialized by psa_crypto_init().
1245 * It is implementation-dependent whether a failure to initialize
1246 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001247 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001248psa_status_t psa_mac_verify(psa_key_handle_t handle,
1249 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001250 const uint8_t *input,
1251 size_t input_length,
1252 const uint8_t *mac,
1253 const size_t mac_length);
1254
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001255/** The type of the state data structure for multipart MAC operations.
1256 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001257 * Before calling any function on a MAC operation object, the application must
1258 * initialize it by any of the following means:
1259 * - Set the structure to all-bits-zero, for example:
1260 * \code
1261 * psa_mac_operation_t operation;
1262 * memset(&operation, 0, sizeof(operation));
1263 * \endcode
1264 * - Initialize the structure to logical zero values, for example:
1265 * \code
1266 * psa_mac_operation_t operation = {0};
1267 * \endcode
1268 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1269 * for example:
1270 * \code
1271 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1272 * \endcode
1273 * - Assign the result of the function psa_mac_operation_init()
1274 * to the structure, for example:
1275 * \code
1276 * psa_mac_operation_t operation;
1277 * operation = psa_mac_operation_init();
1278 * \endcode
1279 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001280 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001281 * make any assumptions about the content of this structure except
1282 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001283typedef struct psa_mac_operation_s psa_mac_operation_t;
1284
Jaeden Amero769ce272019-01-04 11:48:03 +00001285/** \def PSA_MAC_OPERATION_INIT
1286 *
1287 * This macro returns a suitable initializer for a MAC operation object of type
1288 * #psa_mac_operation_t.
1289 */
1290#ifdef __DOXYGEN_ONLY__
1291/* This is an example definition for documentation purposes.
1292 * Implementations should define a suitable value in `crypto_struct.h`.
1293 */
1294#define PSA_MAC_OPERATION_INIT {0}
1295#endif
1296
1297/** Return an initial value for a MAC operation object.
1298 */
1299static psa_mac_operation_t psa_mac_operation_init(void);
1300
Gilles Peskinef45adda2019-01-14 18:29:18 +01001301/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001302 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001303 * This function sets up the calculation of the MAC
1304 * (message authentication code) of a byte string.
1305 * To verify the MAC of a message against an
1306 * expected value, use psa_mac_verify_setup() instead.
1307 *
1308 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001309 * -# Allocate an operation object which will be passed to all the functions
1310 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001311 * -# Initialize the operation object with one of the methods described in the
1312 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001313 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001314 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1315 * of the message each time. The MAC that is calculated is the MAC
1316 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001317 * -# At the end of the message, call psa_mac_sign_finish() to finish
1318 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001319 *
1320 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001321 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001322 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001323 * After a successful call to psa_mac_sign_setup(), the application must
1324 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001325 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001326 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001327 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001328 * \param[in,out] operation The operation object to set up. It must have
1329 * been initialized as per the documentation for
1330 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001331 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001332 * It must remain valid until the operation
1333 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001334 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001335 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001336 *
Gilles Peskine28538492018-07-11 17:34:00 +02001337 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001338 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001339 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001340 * \retval #PSA_ERROR_NOT_PERMITTED
1341 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001342 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001343 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001344 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001345 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1346 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1347 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001348 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001349 * \retval #PSA_ERROR_STORAGE_FAILURE
1350 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001351 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001352 * The operation state is not valid (already set up and not
1353 * subsequently completed).
1354 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001355 * The library has not been previously initialized by psa_crypto_init().
1356 * It is implementation-dependent whether a failure to initialize
1357 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001358 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001359psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001360 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001361 psa_algorithm_t alg);
1362
Gilles Peskinef45adda2019-01-14 18:29:18 +01001363/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001364 *
1365 * This function sets up the verification of the MAC
1366 * (message authentication code) of a byte string against an expected value.
1367 *
1368 * The sequence of operations to verify a MAC is as follows:
1369 * -# Allocate an operation object which will be passed to all the functions
1370 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001371 * -# Initialize the operation object with one of the methods described in the
1372 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001373 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001374 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1375 * of the message each time. The MAC that is calculated is the MAC
1376 * of the concatenation of these messages in order.
1377 * -# At the end of the message, call psa_mac_verify_finish() to finish
1378 * calculating the actual MAC of the message and verify it against
1379 * the expected value.
1380 *
1381 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001382 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001383 *
1384 * After a successful call to psa_mac_verify_setup(), the application must
1385 * eventually terminate the operation through one of the following methods:
1386 * - A failed call to psa_mac_update().
1387 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1388 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001389 * \param[in,out] operation The operation object to set up. It must have
1390 * been initialized as per the documentation for
1391 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001392 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001393 * It must remain valid until the operation
1394 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001395 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1396 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001397 *
Gilles Peskine28538492018-07-11 17:34:00 +02001398 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001399 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001400 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001401 * \retval #PSA_ERROR_NOT_PERMITTED
1402 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001403 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001404 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001405 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001406 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1407 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1408 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001409 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001410 * \retval #PSA_ERROR_STORAGE_FAILURE
1411 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001412 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001413 * The operation state is not valid (already set up and not
1414 * subsequently completed).
1415 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001416 * The library has not been previously initialized by psa_crypto_init().
1417 * It is implementation-dependent whether a failure to initialize
1418 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001419 */
1420psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001421 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001422 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001423
Gilles Peskinedcd14942018-07-12 00:30:52 +02001424/** Add a message fragment to a multipart MAC operation.
1425 *
1426 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1427 * before calling this function.
1428 *
1429 * If this function returns an error status, the operation becomes inactive.
1430 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001431 * \param[in,out] operation Active MAC operation.
1432 * \param[in] input Buffer containing the message fragment to add to
1433 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001434 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001435 *
1436 * \retval #PSA_SUCCESS
1437 * Success.
1438 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001439 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001440 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1441 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1442 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001443 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001444 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001445 * \retval #PSA_ERROR_BAD_STATE
1446 * The library has not been previously initialized by psa_crypto_init().
1447 * It is implementation-dependent whether a failure to initialize
1448 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001449 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001450psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1451 const uint8_t *input,
1452 size_t input_length);
1453
Gilles Peskinedcd14942018-07-12 00:30:52 +02001454/** Finish the calculation of the MAC of a message.
1455 *
1456 * The application must call psa_mac_sign_setup() before calling this function.
1457 * This function calculates the MAC of the message formed by concatenating
1458 * the inputs passed to preceding calls to psa_mac_update().
1459 *
1460 * When this function returns, the operation becomes inactive.
1461 *
1462 * \warning Applications should not call this function if they expect
1463 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1464 * Beware that comparing integrity or authenticity data such as
1465 * MAC values with a function such as \c memcmp is risky
1466 * because the time taken by the comparison may leak information
1467 * about the MAC value which could allow an attacker to guess
1468 * a valid MAC and thereby bypass security controls.
1469 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001470 * \param[in,out] operation Active MAC operation.
1471 * \param[out] mac Buffer where the MAC value is to be written.
1472 * \param mac_size Size of the \p mac buffer in bytes.
1473 * \param[out] mac_length On success, the number of bytes
1474 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001475 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001476 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001477 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001478 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001479 *
1480 * \retval #PSA_SUCCESS
1481 * Success.
1482 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001483 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001484 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001485 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001486 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1487 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1488 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1489 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001490 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001491 * \retval #PSA_ERROR_BAD_STATE
1492 * The library has not been previously initialized by psa_crypto_init().
1493 * It is implementation-dependent whether a failure to initialize
1494 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001495 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001496psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1497 uint8_t *mac,
1498 size_t mac_size,
1499 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001500
Gilles Peskinedcd14942018-07-12 00:30:52 +02001501/** Finish the calculation of the MAC of a message and compare it with
1502 * an expected value.
1503 *
1504 * The application must call psa_mac_verify_setup() before calling this function.
1505 * This function calculates the MAC of the message formed by concatenating
1506 * the inputs passed to preceding calls to psa_mac_update(). It then
1507 * compares the calculated MAC with the expected MAC passed as a
1508 * parameter to this function.
1509 *
1510 * When this function returns, the operation becomes inactive.
1511 *
1512 * \note Implementations shall make the best effort to ensure that the
1513 * comparison between the actual MAC and the expected MAC is performed
1514 * in constant time.
1515 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001516 * \param[in,out] operation Active MAC operation.
1517 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001518 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001519 *
1520 * \retval #PSA_SUCCESS
1521 * The expected MAC is identical to the actual MAC of the message.
1522 * \retval #PSA_ERROR_INVALID_SIGNATURE
1523 * The MAC of the message was calculated successfully, but it
1524 * differs from the expected MAC.
1525 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001526 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001527 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1528 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1529 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001530 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001531 * \retval #PSA_ERROR_BAD_STATE
1532 * The library has not been previously initialized by psa_crypto_init().
1533 * It is implementation-dependent whether a failure to initialize
1534 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001535 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001536psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1537 const uint8_t *mac,
1538 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001539
Gilles Peskinedcd14942018-07-12 00:30:52 +02001540/** Abort a MAC operation.
1541 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001542 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001543 * \p operation structure itself. Once aborted, the operation object
1544 * can be reused for another operation by calling
1545 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001546 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001547 * You may call this function any time after the operation object has
1548 * been initialized by any of the following methods:
1549 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1550 * it succeeds or not.
1551 * - Initializing the \c struct to all-bits-zero.
1552 * - Initializing the \c struct to logical zeros, e.g.
1553 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001554 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001555 * In particular, calling psa_mac_abort() after the operation has been
1556 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1557 * psa_mac_verify_finish() is safe and has no effect.
1558 *
1559 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001560 *
1561 * \retval #PSA_SUCCESS
1562 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001563 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001564 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1565 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001566 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001567 * \retval #PSA_ERROR_BAD_STATE
1568 * The library has not been previously initialized by psa_crypto_init().
1569 * It is implementation-dependent whether a failure to initialize
1570 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001571 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001572psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1573
1574/**@}*/
1575
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001576/** \defgroup cipher Symmetric ciphers
1577 * @{
1578 */
1579
Gilles Peskine69647a42019-01-14 20:18:12 +01001580/** Encrypt a message using a symmetric cipher.
1581 *
1582 * This function encrypts a message with a random IV (initialization
1583 * vector).
1584 *
1585 * \param handle Handle to the key to use for the operation.
1586 * It must remain valid until the operation
1587 * terminates.
1588 * \param alg The cipher algorithm to compute
1589 * (\c PSA_ALG_XXX value such that
1590 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1591 * \param[in] input Buffer containing the message to encrypt.
1592 * \param input_length Size of the \p input buffer in bytes.
1593 * \param[out] output Buffer where the output is to be written.
1594 * The output contains the IV followed by
1595 * the ciphertext proper.
1596 * \param output_size Size of the \p output buffer in bytes.
1597 * \param[out] output_length On success, the number of bytes
1598 * that make up the output.
1599 *
1600 * \retval #PSA_SUCCESS
1601 * Success.
1602 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001603 * \retval #PSA_ERROR_NOT_PERMITTED
1604 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001605 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001606 * \retval #PSA_ERROR_NOT_SUPPORTED
1607 * \p alg is not supported or is not a cipher algorithm.
1608 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1609 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1610 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1611 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001612 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001613 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001614 * \retval #PSA_ERROR_BAD_STATE
1615 * The library has not been previously initialized by psa_crypto_init().
1616 * It is implementation-dependent whether a failure to initialize
1617 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001618 */
1619psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1620 psa_algorithm_t alg,
1621 const uint8_t *input,
1622 size_t input_length,
1623 uint8_t *output,
1624 size_t output_size,
1625 size_t *output_length);
1626
1627/** Decrypt a message using a symmetric cipher.
1628 *
1629 * This function decrypts a message encrypted with a symmetric cipher.
1630 *
1631 * \param handle Handle to the key to use for the operation.
1632 * It must remain valid until the operation
1633 * terminates.
1634 * \param alg The cipher algorithm to compute
1635 * (\c PSA_ALG_XXX value such that
1636 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1637 * \param[in] input Buffer containing the message to decrypt.
1638 * This consists of the IV followed by the
1639 * ciphertext proper.
1640 * \param input_length Size of the \p input buffer in bytes.
1641 * \param[out] output Buffer where the plaintext is to be written.
1642 * \param output_size Size of the \p output buffer in bytes.
1643 * \param[out] output_length On success, the number of bytes
1644 * that make up the output.
1645 *
1646 * \retval #PSA_SUCCESS
1647 * Success.
1648 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001649 * \retval #PSA_ERROR_NOT_PERMITTED
1650 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001651 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001652 * \retval #PSA_ERROR_NOT_SUPPORTED
1653 * \p alg is not supported or is not a cipher algorithm.
1654 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1655 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1656 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1657 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001658 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001659 * \retval #PSA_ERROR_BAD_STATE
1660 * The library has not been previously initialized by psa_crypto_init().
1661 * It is implementation-dependent whether a failure to initialize
1662 * results in this error code. * \retval #PSA_ERROR_CORRUPTION_DETECTED
1663
Gilles Peskine69647a42019-01-14 20:18:12 +01001664 */
1665psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1666 psa_algorithm_t alg,
1667 const uint8_t *input,
1668 size_t input_length,
1669 uint8_t *output,
1670 size_t output_size,
1671 size_t *output_length);
1672
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001673/** The type of the state data structure for multipart cipher operations.
1674 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001675 * Before calling any function on a cipher operation object, the application
1676 * must initialize it by any of the following means:
1677 * - Set the structure to all-bits-zero, for example:
1678 * \code
1679 * psa_cipher_operation_t operation;
1680 * memset(&operation, 0, sizeof(operation));
1681 * \endcode
1682 * - Initialize the structure to logical zero values, for example:
1683 * \code
1684 * psa_cipher_operation_t operation = {0};
1685 * \endcode
1686 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1687 * for example:
1688 * \code
1689 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1690 * \endcode
1691 * - Assign the result of the function psa_cipher_operation_init()
1692 * to the structure, for example:
1693 * \code
1694 * psa_cipher_operation_t operation;
1695 * operation = psa_cipher_operation_init();
1696 * \endcode
1697 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001698 * This is an implementation-defined \c struct. Applications should not
1699 * make any assumptions about the content of this structure except
1700 * as directed by the documentation of a specific implementation. */
1701typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1702
Jaeden Amero5bae2272019-01-04 11:48:27 +00001703/** \def PSA_CIPHER_OPERATION_INIT
1704 *
1705 * This macro returns a suitable initializer for a cipher operation object of
1706 * type #psa_cipher_operation_t.
1707 */
1708#ifdef __DOXYGEN_ONLY__
1709/* This is an example definition for documentation purposes.
1710 * Implementations should define a suitable value in `crypto_struct.h`.
1711 */
1712#define PSA_CIPHER_OPERATION_INIT {0}
1713#endif
1714
1715/** Return an initial value for a cipher operation object.
1716 */
1717static psa_cipher_operation_t psa_cipher_operation_init(void);
1718
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001719/** Set the key for a multipart symmetric encryption operation.
1720 *
1721 * The sequence of operations to encrypt a message with a symmetric cipher
1722 * is as follows:
1723 * -# Allocate an operation object which will be passed to all the functions
1724 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001725 * -# Initialize the operation object with one of the methods described in the
1726 * documentation for #psa_cipher_operation_t, e.g.
1727 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001728 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001729 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001730 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001731 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001732 * requires a specific IV value.
1733 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1734 * of the message each time.
1735 * -# Call psa_cipher_finish().
1736 *
1737 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001738 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001739 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001740 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001741 * eventually terminate the operation. The following events terminate an
1742 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001743 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001744 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001745 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001746 * \param[in,out] operation The operation object to set up. It must have
1747 * been initialized as per the documentation for
1748 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001749 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001750 * It must remain valid until the operation
1751 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001752 * \param alg The cipher algorithm to compute
1753 * (\c PSA_ALG_XXX value such that
1754 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001755 *
Gilles Peskine28538492018-07-11 17:34:00 +02001756 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001757 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001758 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001759 * \retval #PSA_ERROR_NOT_PERMITTED
1760 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001761 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001762 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001763 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001764 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1765 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1766 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001767 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001768 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001769 * The operation state is not valid (already set up and not
1770 * subsequently completed).
1771 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001772 * The library has not been previously initialized by psa_crypto_init().
1773 * It is implementation-dependent whether a failure to initialize
1774 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001775 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001776psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001777 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001778 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001779
1780/** Set the key for a multipart symmetric decryption operation.
1781 *
1782 * The sequence of operations to decrypt a message with a symmetric cipher
1783 * is as follows:
1784 * -# Allocate an operation object which will be passed to all the functions
1785 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001786 * -# Initialize the operation object with one of the methods described in the
1787 * documentation for #psa_cipher_operation_t, e.g.
1788 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001789 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001790 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001791 * decryption. If the IV is prepended to the ciphertext, you can call
1792 * psa_cipher_update() on a buffer containing the IV followed by the
1793 * beginning of the message.
1794 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1795 * of the message each time.
1796 * -# Call psa_cipher_finish().
1797 *
1798 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001799 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001800 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001801 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001802 * eventually terminate the operation. The following events terminate an
1803 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001804 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001805 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001806 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001807 * \param[in,out] operation The operation object to set up. It must have
1808 * been initialized as per the documentation for
1809 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001810 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001811 * It must remain valid until the operation
1812 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001813 * \param alg The cipher algorithm to compute
1814 * (\c PSA_ALG_XXX value such that
1815 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001816 *
Gilles Peskine28538492018-07-11 17:34:00 +02001817 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001818 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001819 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001820 * \retval #PSA_ERROR_NOT_PERMITTED
1821 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001822 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001823 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001824 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001825 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1826 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1827 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001828 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001829 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001830 * The operation state is not valid (already set up and not
1831 * subsequently completed).
1832 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001833 * The library has not been previously initialized by psa_crypto_init().
1834 * It is implementation-dependent whether a failure to initialize
1835 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001836 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001837psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001838 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001839 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001840
Gilles Peskinedcd14942018-07-12 00:30:52 +02001841/** Generate an IV for a symmetric encryption operation.
1842 *
1843 * This function generates a random IV (initialization vector), nonce
1844 * or initial counter value for the encryption operation as appropriate
1845 * for the chosen algorithm, key type and key size.
1846 *
1847 * The application must call psa_cipher_encrypt_setup() before
1848 * calling this function.
1849 *
1850 * If this function returns an error status, the operation becomes inactive.
1851 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001852 * \param[in,out] operation Active cipher operation.
1853 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001854 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001855 * \param[out] iv_length On success, the number of bytes of the
1856 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001857 *
1858 * \retval #PSA_SUCCESS
1859 * Success.
1860 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001861 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001862 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001863 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001864 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1865 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1866 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001867 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001868 * \retval #PSA_ERROR_BAD_STATE
1869 * The library has not been previously initialized by psa_crypto_init().
1870 * It is implementation-dependent whether a failure to initialize
1871 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001872 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001873psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001874 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001875 size_t iv_size,
1876 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001877
Gilles Peskinedcd14942018-07-12 00:30:52 +02001878/** Set the IV for a symmetric encryption or decryption operation.
1879 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001880 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001881 * or initial counter value for the encryption or decryption operation.
1882 *
1883 * The application must call psa_cipher_encrypt_setup() before
1884 * calling this function.
1885 *
1886 * If this function returns an error status, the operation becomes inactive.
1887 *
1888 * \note When encrypting, applications should use psa_cipher_generate_iv()
1889 * instead of this function, unless implementing a protocol that requires
1890 * a non-random IV.
1891 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001892 * \param[in,out] operation Active cipher operation.
1893 * \param[in] iv Buffer containing the IV to use.
1894 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001895 *
1896 * \retval #PSA_SUCCESS
1897 * Success.
1898 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001899 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001900 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001901 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001902 * or the chosen algorithm does not use an IV.
1903 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1904 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1905 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001906 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001907 * \retval #PSA_ERROR_BAD_STATE
1908 * The library has not been previously initialized by psa_crypto_init().
1909 * It is implementation-dependent whether a failure to initialize
1910 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001911 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001912psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001913 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001914 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001915
Gilles Peskinedcd14942018-07-12 00:30:52 +02001916/** Encrypt or decrypt a message fragment in an active cipher operation.
1917 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001918 * Before calling this function, you must:
1919 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1920 * The choice of setup function determines whether this function
1921 * encrypts or decrypts its input.
1922 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1923 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001924 *
1925 * If this function returns an error status, the operation becomes inactive.
1926 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001927 * \param[in,out] operation Active cipher operation.
1928 * \param[in] input Buffer containing the message fragment to
1929 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001930 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001931 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001932 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001933 * \param[out] output_length On success, the number of bytes
1934 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001935 *
1936 * \retval #PSA_SUCCESS
1937 * Success.
1938 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001939 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001940 * not set, or already completed).
1941 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1942 * The size of the \p output buffer is too small.
1943 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1944 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1945 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001946 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001947 * \retval #PSA_ERROR_BAD_STATE
1948 * The library has not been previously initialized by psa_crypto_init().
1949 * It is implementation-dependent whether a failure to initialize
1950 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001951 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001952psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1953 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001954 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001955 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001956 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001957 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001958
Gilles Peskinedcd14942018-07-12 00:30:52 +02001959/** Finish encrypting or decrypting a message in a cipher operation.
1960 *
1961 * The application must call psa_cipher_encrypt_setup() or
1962 * psa_cipher_decrypt_setup() before calling this function. The choice
1963 * of setup function determines whether this function encrypts or
1964 * decrypts its input.
1965 *
1966 * This function finishes the encryption or decryption of the message
1967 * formed by concatenating the inputs passed to preceding calls to
1968 * psa_cipher_update().
1969 *
1970 * When this function returns, the operation becomes inactive.
1971 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001972 * \param[in,out] operation Active cipher operation.
1973 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001974 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001975 * \param[out] output_length On success, the number of bytes
1976 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001977 *
1978 * \retval #PSA_SUCCESS
1979 * Success.
1980 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001981 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001982 * not set, or already completed).
1983 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1984 * The size of the \p output buffer is too small.
1985 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1986 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1987 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001988 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001989 * \retval #PSA_ERROR_BAD_STATE
1990 * The library has not been previously initialized by psa_crypto_init().
1991 * It is implementation-dependent whether a failure to initialize
1992 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001993 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001994psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001995 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001996 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001997 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001998
Gilles Peskinedcd14942018-07-12 00:30:52 +02001999/** Abort a cipher operation.
2000 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002001 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002002 * \p operation structure itself. Once aborted, the operation object
2003 * can be reused for another operation by calling
2004 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002005 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002006 * You may call this function any time after the operation object has
2007 * been initialized by any of the following methods:
2008 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2009 * whether it succeeds or not.
2010 * - Initializing the \c struct to all-bits-zero.
2011 * - Initializing the \c struct to logical zeros, e.g.
2012 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002013 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002014 * In particular, calling psa_cipher_abort() after the operation has been
2015 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2016 * is safe and has no effect.
2017 *
2018 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002019 *
2020 * \retval #PSA_SUCCESS
2021 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002022 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002023 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2024 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002025 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002026 * \retval #PSA_ERROR_BAD_STATE
2027 * The library has not been previously initialized by psa_crypto_init().
2028 * It is implementation-dependent whether a failure to initialize
2029 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002030 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002031psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2032
2033/**@}*/
2034
Gilles Peskine3b555712018-03-03 21:27:57 +01002035/** \defgroup aead Authenticated encryption with associated data (AEAD)
2036 * @{
2037 */
2038
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002039/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002041 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002042 * \param alg The AEAD algorithm to compute
2043 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002044 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002045 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002046 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002047 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002048 * but not encrypted.
2049 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002050 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002051 * encrypted.
2052 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002053 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002054 * encrypted data. The additional data is not
2055 * part of this output. For algorithms where the
2056 * encrypted data and the authentication tag
2057 * are defined as separate outputs, the
2058 * authentication tag is appended to the
2059 * encrypted data.
2060 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2061 * This must be at least
2062 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2063 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002064 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002065 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002066 *
Gilles Peskine28538492018-07-11 17:34:00 +02002067 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002068 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002069 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002070 * \retval #PSA_ERROR_NOT_PERMITTED
2071 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002072 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002073 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002074 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002075 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2076 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2077 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002078 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002079 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002080 * The library has not been previously initialized by psa_crypto_init().
2081 * It is implementation-dependent whether a failure to initialize
2082 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002083 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002084psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002085 psa_algorithm_t alg,
2086 const uint8_t *nonce,
2087 size_t nonce_length,
2088 const uint8_t *additional_data,
2089 size_t additional_data_length,
2090 const uint8_t *plaintext,
2091 size_t plaintext_length,
2092 uint8_t *ciphertext,
2093 size_t ciphertext_size,
2094 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002095
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002096/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002097 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002098 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002099 * \param alg The AEAD algorithm to compute
2100 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002101 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002102 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002103 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002104 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002105 * but not encrypted.
2106 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002107 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002108 * encrypted. For algorithms where the
2109 * encrypted data and the authentication tag
2110 * are defined as separate inputs, the buffer
2111 * must contain the encrypted data followed
2112 * by the authentication tag.
2113 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002114 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002115 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2116 * This must be at least
2117 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2118 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002119 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002120 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002121 *
Gilles Peskine28538492018-07-11 17:34:00 +02002122 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002123 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002124 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002125 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002126 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002127 * \retval #PSA_ERROR_NOT_PERMITTED
2128 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002129 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002130 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002131 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002132 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002133 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2134 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002135 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2136 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002137 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002138 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002139 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002140 * The library has not been previously initialized by psa_crypto_init().
2141 * It is implementation-dependent whether a failure to initialize
2142 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002143 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002144psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002145 psa_algorithm_t alg,
2146 const uint8_t *nonce,
2147 size_t nonce_length,
2148 const uint8_t *additional_data,
2149 size_t additional_data_length,
2150 const uint8_t *ciphertext,
2151 size_t ciphertext_length,
2152 uint8_t *plaintext,
2153 size_t plaintext_size,
2154 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002155
Gilles Peskine30a9e412019-01-14 18:36:12 +01002156/** The type of the state data structure for multipart AEAD operations.
2157 *
2158 * Before calling any function on an AEAD operation object, the application
2159 * must initialize it by any of the following means:
2160 * - Set the structure to all-bits-zero, for example:
2161 * \code
2162 * psa_aead_operation_t operation;
2163 * memset(&operation, 0, sizeof(operation));
2164 * \endcode
2165 * - Initialize the structure to logical zero values, for example:
2166 * \code
2167 * psa_aead_operation_t operation = {0};
2168 * \endcode
2169 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2170 * for example:
2171 * \code
2172 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2173 * \endcode
2174 * - Assign the result of the function psa_aead_operation_init()
2175 * to the structure, for example:
2176 * \code
2177 * psa_aead_operation_t operation;
2178 * operation = psa_aead_operation_init();
2179 * \endcode
2180 *
2181 * This is an implementation-defined \c struct. Applications should not
2182 * make any assumptions about the content of this structure except
2183 * as directed by the documentation of a specific implementation. */
2184typedef struct psa_aead_operation_s psa_aead_operation_t;
2185
2186/** \def PSA_AEAD_OPERATION_INIT
2187 *
2188 * This macro returns a suitable initializer for an AEAD operation object of
2189 * type #psa_aead_operation_t.
2190 */
2191#ifdef __DOXYGEN_ONLY__
2192/* This is an example definition for documentation purposes.
2193 * Implementations should define a suitable value in `crypto_struct.h`.
2194 */
2195#define PSA_AEAD_OPERATION_INIT {0}
2196#endif
2197
2198/** Return an initial value for an AEAD operation object.
2199 */
2200static psa_aead_operation_t psa_aead_operation_init(void);
2201
2202/** Set the key for a multipart authenticated encryption operation.
2203 *
2204 * The sequence of operations to encrypt a message with authentication
2205 * is as follows:
2206 * -# Allocate an operation object which will be passed to all the functions
2207 * listed here.
2208 * -# Initialize the operation object with one of the methods described in the
2209 * documentation for #psa_aead_operation_t, e.g.
2210 * PSA_AEAD_OPERATION_INIT.
2211 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002212 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2213 * inputs to the subsequent calls to psa_aead_update_ad() and
2214 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2215 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002216 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2217 * generate or set the nonce. You should use
2218 * psa_aead_generate_nonce() unless the protocol you are implementing
2219 * requires a specific nonce value.
2220 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2221 * of the non-encrypted additional authenticated data each time.
2222 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002223 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002224 * -# Call psa_aead_finish().
2225 *
2226 * The application may call psa_aead_abort() at any time after the operation
2227 * has been initialized.
2228 *
2229 * After a successful call to psa_aead_encrypt_setup(), the application must
2230 * eventually terminate the operation. The following events terminate an
2231 * operation:
2232 * - A failed call to any of the \c psa_aead_xxx functions.
2233 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2234 *
2235 * \param[in,out] operation The operation object to set up. It must have
2236 * been initialized as per the documentation for
2237 * #psa_aead_operation_t and not yet in use.
2238 * \param handle Handle to the key to use for the operation.
2239 * It must remain valid until the operation
2240 * terminates.
2241 * \param alg The AEAD algorithm to compute
2242 * (\c PSA_ALG_XXX value such that
2243 * #PSA_ALG_IS_AEAD(\p alg) is true).
2244 *
2245 * \retval #PSA_SUCCESS
2246 * Success.
2247 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002248 * \retval #PSA_ERROR_NOT_PERMITTED
2249 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002250 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002251 * \retval #PSA_ERROR_NOT_SUPPORTED
2252 * \p alg is not supported or is not an AEAD algorithm.
2253 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2254 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2255 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002256 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002257 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002258 * \retval #PSA_ERROR_BAD_STATE
2259 * The library has not been previously initialized by psa_crypto_init().
2260 * It is implementation-dependent whether a failure to initialize
2261 * results in this error code.
2262 */
2263psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2264 psa_key_handle_t handle,
2265 psa_algorithm_t alg);
2266
2267/** Set the key for a multipart authenticated decryption operation.
2268 *
2269 * The sequence of operations to decrypt a message with authentication
2270 * is as follows:
2271 * -# Allocate an operation object which will be passed to all the functions
2272 * listed here.
2273 * -# Initialize the operation object with one of the methods described in the
2274 * documentation for #psa_aead_operation_t, e.g.
2275 * PSA_AEAD_OPERATION_INIT.
2276 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002277 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2278 * inputs to the subsequent calls to psa_aead_update_ad() and
2279 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2280 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002281 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2282 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2283 * of the non-encrypted additional authenticated data each time.
2284 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002285 * of the ciphertext to decrypt each time.
2286 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002287 *
2288 * The application may call psa_aead_abort() at any time after the operation
2289 * has been initialized.
2290 *
2291 * After a successful call to psa_aead_decrypt_setup(), the application must
2292 * eventually terminate the operation. The following events terminate an
2293 * operation:
2294 * - A failed call to any of the \c psa_aead_xxx functions.
2295 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2296 *
2297 * \param[in,out] operation The operation object to set up. It must have
2298 * been initialized as per the documentation for
2299 * #psa_aead_operation_t and not yet in use.
2300 * \param handle Handle to the key to use for the operation.
2301 * It must remain valid until the operation
2302 * terminates.
2303 * \param alg The AEAD algorithm to compute
2304 * (\c PSA_ALG_XXX value such that
2305 * #PSA_ALG_IS_AEAD(\p alg) is true).
2306 *
2307 * \retval #PSA_SUCCESS
2308 * Success.
2309 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002310 * \retval #PSA_ERROR_NOT_PERMITTED
2311 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002312 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002313 * \retval #PSA_ERROR_NOT_SUPPORTED
2314 * \p alg is not supported or is not an AEAD algorithm.
2315 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2316 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2317 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002318 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002319 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002320 * \retval #PSA_ERROR_BAD_STATE
2321 * The library has not been previously initialized by psa_crypto_init().
2322 * It is implementation-dependent whether a failure to initialize
2323 * results in this error code.
2324 */
2325psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2326 psa_key_handle_t handle,
2327 psa_algorithm_t alg);
2328
2329/** Generate a random nonce for an authenticated encryption operation.
2330 *
2331 * This function generates a random nonce for the authenticated encryption
2332 * operation with an appropriate size for the chosen algorithm, key type
2333 * and key size.
2334 *
2335 * The application must call psa_aead_encrypt_setup() before
2336 * calling this function.
2337 *
2338 * If this function returns an error status, the operation becomes inactive.
2339 *
2340 * \param[in,out] operation Active AEAD operation.
2341 * \param[out] nonce Buffer where the generated nonce is to be
2342 * written.
2343 * \param nonce_size Size of the \p nonce buffer in bytes.
2344 * \param[out] nonce_length On success, the number of bytes of the
2345 * generated nonce.
2346 *
2347 * \retval #PSA_SUCCESS
2348 * Success.
2349 * \retval #PSA_ERROR_BAD_STATE
2350 * The operation state is not valid (not set up, or nonce already set).
2351 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2352 * The size of the \p nonce buffer is too small.
2353 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2354 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2355 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002356 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002357 * \retval #PSA_ERROR_BAD_STATE
2358 * The library has not been previously initialized by psa_crypto_init().
2359 * It is implementation-dependent whether a failure to initialize
2360 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002361 */
2362psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002363 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002364 size_t nonce_size,
2365 size_t *nonce_length);
2366
2367/** Set the nonce for an authenticated encryption or decryption operation.
2368 *
2369 * This function sets the nonce for the authenticated
2370 * encryption or decryption operation.
2371 *
2372 * The application must call psa_aead_encrypt_setup() before
2373 * calling this function.
2374 *
2375 * If this function returns an error status, the operation becomes inactive.
2376 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002377 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002378 * instead of this function, unless implementing a protocol that requires
2379 * a non-random IV.
2380 *
2381 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002382 * \param[in] nonce Buffer containing the nonce to use.
2383 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002384 *
2385 * \retval #PSA_SUCCESS
2386 * Success.
2387 * \retval #PSA_ERROR_BAD_STATE
2388 * The operation state is not valid (not set up, or nonce already set).
2389 * \retval #PSA_ERROR_INVALID_ARGUMENT
2390 * The size of \p nonce is not acceptable for the chosen algorithm.
2391 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2392 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2393 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002394 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002395 * \retval #PSA_ERROR_BAD_STATE
2396 * The library has not been previously initialized by psa_crypto_init().
2397 * It is implementation-dependent whether a failure to initialize
2398 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002399 */
2400psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002401 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002402 size_t nonce_length);
2403
Gilles Peskinebc59c852019-01-17 15:26:08 +01002404/** Declare the lengths of the message and additional data for AEAD.
2405 *
2406 * The application must call this function before calling
2407 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2408 * the operation requires it. If the algorithm does not require it,
2409 * calling this function is optional, but if this function is called
2410 * then the implementation must enforce the lengths.
2411 *
2412 * You may call this function before or after setting the nonce with
2413 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2414 *
2415 * - For #PSA_ALG_CCM, calling this function is required.
2416 * - For the other AEAD algorithms defined in this specification, calling
2417 * this function is not required.
2418 * - For vendor-defined algorithm, refer to the vendor documentation.
2419 *
2420 * \param[in,out] operation Active AEAD operation.
2421 * \param ad_length Size of the non-encrypted additional
2422 * authenticated data in bytes.
2423 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2424 *
2425 * \retval #PSA_SUCCESS
2426 * Success.
2427 * \retval #PSA_ERROR_BAD_STATE
2428 * The operation state is not valid (not set up, already completed,
2429 * or psa_aead_update_ad() or psa_aead_update() already called).
2430 * \retval #PSA_ERROR_INVALID_ARGUMENT
2431 * At least one of the lengths is not acceptable for the chosen
2432 * algorithm.
2433 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2434 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2435 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002436 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002437 * \retval #PSA_ERROR_BAD_STATE
2438 * The library has not been previously initialized by psa_crypto_init().
2439 * It is implementation-dependent whether a failure to initialize
2440 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002441 */
2442psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2443 size_t ad_length,
2444 size_t plaintext_length);
2445
Gilles Peskine30a9e412019-01-14 18:36:12 +01002446/** Pass additional data to an active AEAD operation.
2447 *
2448 * Additional data is authenticated, but not encrypted.
2449 *
2450 * You may call this function multiple times to pass successive fragments
2451 * of the additional data. You may not call this function after passing
2452 * data to encrypt or decrypt with psa_aead_update().
2453 *
2454 * Before calling this function, you must:
2455 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2456 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2457 *
2458 * If this function returns an error status, the operation becomes inactive.
2459 *
2460 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2461 * there is no guarantee that the input is valid. Therefore, until
2462 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2463 * treat the input as untrusted and prepare to undo any action that
2464 * depends on the input if psa_aead_verify() returns an error status.
2465 *
2466 * \param[in,out] operation Active AEAD operation.
2467 * \param[in] input Buffer containing the fragment of
2468 * additional data.
2469 * \param input_length Size of the \p input buffer in bytes.
2470 *
2471 * \retval #PSA_SUCCESS
2472 * Success.
2473 * \retval #PSA_ERROR_BAD_STATE
2474 * The operation state is not valid (not set up, nonce not set,
2475 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002476 * \retval #PSA_ERROR_INVALID_ARGUMENT
2477 * The total input length overflows the additional data length that
2478 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002479 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2480 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2481 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002482 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002483 * \retval #PSA_ERROR_BAD_STATE
2484 * The library has not been previously initialized by psa_crypto_init().
2485 * It is implementation-dependent whether a failure to initialize
2486 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002487 */
2488psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2489 const uint8_t *input,
2490 size_t input_length);
2491
2492/** Encrypt or decrypt a message fragment in an active AEAD operation.
2493 *
2494 * Before calling this function, you must:
2495 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2496 * The choice of setup function determines whether this function
2497 * encrypts or decrypts its input.
2498 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2499 * 3. Call psa_aead_update_ad() to pass all the additional data.
2500 *
2501 * If this function returns an error status, the operation becomes inactive.
2502 *
2503 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2504 * there is no guarantee that the input is valid. Therefore, until
2505 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2506 * - Do not use the output in any way other than storing it in a
2507 * confidential location. If you take any action that depends
2508 * on the tentative decrypted data, this action will need to be
2509 * undone if the input turns out not to be valid. Furthermore,
2510 * if an adversary can observe that this action took place
2511 * (for example through timing), they may be able to use this
2512 * fact as an oracle to decrypt any message encrypted with the
2513 * same key.
2514 * - In particular, do not copy the output anywhere but to a
2515 * memory or storage space that you have exclusive access to.
2516 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002517 * This function does not require the input to be aligned to any
2518 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002519 * a whole block at a time, it must consume all the input provided, but
2520 * it may delay the end of the corresponding output until a subsequent
2521 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2522 * provides sufficient input. The amount of data that can be delayed
2523 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002524 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002525 * \param[in,out] operation Active AEAD operation.
2526 * \param[in] input Buffer containing the message fragment to
2527 * encrypt or decrypt.
2528 * \param input_length Size of the \p input buffer in bytes.
2529 * \param[out] output Buffer where the output is to be written.
2530 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002531 * This must be at least
2532 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2533 * \p input_length) where \c alg is the
2534 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002535 * \param[out] output_length On success, the number of bytes
2536 * that make up the returned output.
2537 *
2538 * \retval #PSA_SUCCESS
2539 * Success.
2540 * \retval #PSA_ERROR_BAD_STATE
2541 * The operation state is not valid (not set up, nonce not set
2542 * or already completed).
2543 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2544 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002545 * You can determine a sufficient buffer size by calling
2546 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2547 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002548 * \retval #PSA_ERROR_INVALID_ARGUMENT
2549 * The total length of input to psa_aead_update_ad() so far is
2550 * less than the additional data length that was previously
2551 * specified with psa_aead_set_lengths().
2552 * \retval #PSA_ERROR_INVALID_ARGUMENT
2553 * The total input length overflows the plaintext length that
2554 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002555 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2556 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2557 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002558 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002559 * \retval #PSA_ERROR_BAD_STATE
2560 * The library has not been previously initialized by psa_crypto_init().
2561 * It is implementation-dependent whether a failure to initialize
2562 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002563 */
2564psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2565 const uint8_t *input,
2566 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002567 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002568 size_t output_size,
2569 size_t *output_length);
2570
2571/** Finish encrypting a message in an AEAD operation.
2572 *
2573 * The operation must have been set up with psa_aead_encrypt_setup().
2574 *
2575 * This function finishes the authentication of the additional data
2576 * formed by concatenating the inputs passed to preceding calls to
2577 * psa_aead_update_ad() with the plaintext formed by concatenating the
2578 * inputs passed to preceding calls to psa_aead_update().
2579 *
2580 * This function has two output buffers:
2581 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002582 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002583 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002584 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002585 * that the operation performs.
2586 *
2587 * When this function returns, the operation becomes inactive.
2588 *
2589 * \param[in,out] operation Active AEAD operation.
2590 * \param[out] ciphertext Buffer where the last part of the ciphertext
2591 * is to be written.
2592 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002593 * This must be at least
2594 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2595 * \c alg is the algorithm that is being
2596 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002597 * \param[out] ciphertext_length On success, the number of bytes of
2598 * returned ciphertext.
2599 * \param[out] tag Buffer where the authentication tag is
2600 * to be written.
2601 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002602 * This must be at least
2603 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2604 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002605 * \param[out] tag_length On success, the number of bytes
2606 * that make up the returned tag.
2607 *
2608 * \retval #PSA_SUCCESS
2609 * Success.
2610 * \retval #PSA_ERROR_BAD_STATE
2611 * The operation state is not valid (not set up, nonce not set,
2612 * decryption, or already completed).
2613 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002614 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002615 * You can determine a sufficient buffer size for \p ciphertext by
2616 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2617 * where \c alg is the algorithm that is being calculated.
2618 * You can determine a sufficient buffer size for \p tag by
2619 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002620 * \retval #PSA_ERROR_INVALID_ARGUMENT
2621 * The total length of input to psa_aead_update_ad() so far is
2622 * less than the additional data length that was previously
2623 * specified with psa_aead_set_lengths().
2624 * \retval #PSA_ERROR_INVALID_ARGUMENT
2625 * The total length of input to psa_aead_update() so far is
2626 * less than the plaintext length that was previously
2627 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002628 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2629 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2630 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002631 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002632 * \retval #PSA_ERROR_BAD_STATE
2633 * The library has not been previously initialized by psa_crypto_init().
2634 * It is implementation-dependent whether a failure to initialize
2635 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002636 */
2637psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002638 uint8_t *ciphertext,
2639 size_t ciphertext_size,
2640 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002641 uint8_t *tag,
2642 size_t tag_size,
2643 size_t *tag_length);
2644
2645/** Finish authenticating and decrypting a message in an AEAD operation.
2646 *
2647 * The operation must have been set up with psa_aead_decrypt_setup().
2648 *
2649 * This function finishes the authentication of the additional data
2650 * formed by concatenating the inputs passed to preceding calls to
2651 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2652 * inputs passed to preceding calls to psa_aead_update().
2653 *
2654 * When this function returns, the operation becomes inactive.
2655 *
2656 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002657 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002658 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002659 * from previous calls to psa_aead_update()
2660 * that could not be processed until the end
2661 * of the input.
2662 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002663 * This must be at least
2664 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2665 * \c alg is the algorithm that is being
2666 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002667 * \param[out] plaintext_length On success, the number of bytes of
2668 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002669 * \param[in] tag Buffer containing the authentication tag.
2670 * \param tag_length Size of the \p tag buffer in bytes.
2671 *
2672 * \retval #PSA_SUCCESS
2673 * Success.
2674 * \retval #PSA_ERROR_BAD_STATE
2675 * The operation state is not valid (not set up, nonce not set,
2676 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002677 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2678 * The size of the \p plaintext buffer is too small.
2679 * You can determine a sufficient buffer size for \p plaintext by
2680 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2681 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002682 * \retval #PSA_ERROR_INVALID_ARGUMENT
2683 * The total length of input to psa_aead_update_ad() so far is
2684 * less than the additional data length that was previously
2685 * specified with psa_aead_set_lengths().
2686 * \retval #PSA_ERROR_INVALID_ARGUMENT
2687 * The total length of input to psa_aead_update() so far is
2688 * less than the plaintext length that was previously
2689 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002690 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2691 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2692 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002693 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002694 * \retval #PSA_ERROR_BAD_STATE
2695 * The library has not been previously initialized by psa_crypto_init().
2696 * It is implementation-dependent whether a failure to initialize
2697 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002698 */
2699psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002700 uint8_t *plaintext,
2701 size_t plaintext_size,
2702 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002703 const uint8_t *tag,
2704 size_t tag_length);
2705
2706/** Abort an AEAD operation.
2707 *
2708 * Aborting an operation frees all associated resources except for the
2709 * \p operation structure itself. Once aborted, the operation object
2710 * can be reused for another operation by calling
2711 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2712 *
2713 * You may call this function any time after the operation object has
2714 * been initialized by any of the following methods:
2715 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2716 * whether it succeeds or not.
2717 * - Initializing the \c struct to all-bits-zero.
2718 * - Initializing the \c struct to logical zeros, e.g.
2719 * `psa_aead_operation_t operation = {0}`.
2720 *
2721 * In particular, calling psa_aead_abort() after the operation has been
2722 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2723 * is safe and has no effect.
2724 *
2725 * \param[in,out] operation Initialized AEAD operation.
2726 *
2727 * \retval #PSA_SUCCESS
2728 * \retval #PSA_ERROR_BAD_STATE
2729 * \p operation is not an active AEAD operation.
2730 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2731 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002732 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002733 * \retval #PSA_ERROR_BAD_STATE
2734 * The library has not been previously initialized by psa_crypto_init().
2735 * It is implementation-dependent whether a failure to initialize
2736 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002737 */
2738psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2739
Gilles Peskine3b555712018-03-03 21:27:57 +01002740/**@}*/
2741
Gilles Peskine20035e32018-02-03 22:44:14 +01002742/** \defgroup asymmetric Asymmetric cryptography
2743 * @{
2744 */
2745
2746/**
2747 * \brief Sign a hash or short message with a private key.
2748 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002749 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002750 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002751 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2752 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2753 * to determine the hash algorithm to use.
2754 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002755 * \param handle Handle to the key to use for the operation.
2756 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002757 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002758 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002759 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002760 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002761 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002762 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002763 * \param[out] signature_length On success, the number of bytes
2764 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002765 *
Gilles Peskine28538492018-07-11 17:34:00 +02002766 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002767 * \retval #PSA_ERROR_INVALID_HANDLE
2768 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002769 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002770 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002771 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002772 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002773 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002774 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002775 * \retval #PSA_ERROR_NOT_SUPPORTED
2776 * \retval #PSA_ERROR_INVALID_ARGUMENT
2777 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2778 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2779 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002780 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002781 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002782 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002783 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002784 * The library has not been previously initialized by psa_crypto_init().
2785 * It is implementation-dependent whether a failure to initialize
2786 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002787 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002788psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002789 psa_algorithm_t alg,
2790 const uint8_t *hash,
2791 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002792 uint8_t *signature,
2793 size_t signature_size,
2794 size_t *signature_length);
2795
2796/**
2797 * \brief Verify the signature a hash or short message using a public key.
2798 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002799 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002800 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002801 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2802 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2803 * to determine the hash algorithm to use.
2804 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002805 * \param handle Handle to the key to use for the operation.
2806 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002807 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002808 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002809 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002810 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002811 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002812 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002813 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002814 *
Gilles Peskine28538492018-07-11 17:34:00 +02002815 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002816 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002817 * \retval #PSA_ERROR_INVALID_HANDLE
2818 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002819 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002820 * The calculation was perfomed successfully, but the passed
2821 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002822 * \retval #PSA_ERROR_NOT_SUPPORTED
2823 * \retval #PSA_ERROR_INVALID_ARGUMENT
2824 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2825 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2826 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002827 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002828 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002829 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002830 * The library has not been previously initialized by psa_crypto_init().
2831 * It is implementation-dependent whether a failure to initialize
2832 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002833 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002834psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002835 psa_algorithm_t alg,
2836 const uint8_t *hash,
2837 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002838 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002839 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002840
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002841/**
2842 * \brief Encrypt a short message with a public key.
2843 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002844 * \param handle Handle to the key to use for the operation.
2845 * It must be a public key or an asymmetric
2846 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002847 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002848 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002849 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002850 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002851 * \param[in] salt A salt or label, if supported by the
2852 * encryption algorithm.
2853 * If the algorithm does not support a
2854 * salt, pass \c NULL.
2855 * If the algorithm supports an optional
2856 * salt and you do not want to pass a salt,
2857 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002858 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002859 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2860 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002861 * \param salt_length Size of the \p salt buffer in bytes.
2862 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002863 * \param[out] output Buffer where the encrypted message is to
2864 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002865 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002866 * \param[out] output_length On success, the number of bytes
2867 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002868 *
Gilles Peskine28538492018-07-11 17:34:00 +02002869 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002870 * \retval #PSA_ERROR_INVALID_HANDLE
2871 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002872 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002873 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002874 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002875 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002876 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002877 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002878 * \retval #PSA_ERROR_NOT_SUPPORTED
2879 * \retval #PSA_ERROR_INVALID_ARGUMENT
2880 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2881 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2882 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002883 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002884 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002885 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002886 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002887 * The library has not been previously initialized by psa_crypto_init().
2888 * It is implementation-dependent whether a failure to initialize
2889 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002890 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002891psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002892 psa_algorithm_t alg,
2893 const uint8_t *input,
2894 size_t input_length,
2895 const uint8_t *salt,
2896 size_t salt_length,
2897 uint8_t *output,
2898 size_t output_size,
2899 size_t *output_length);
2900
2901/**
2902 * \brief Decrypt a short message with a private key.
2903 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002904 * \param handle Handle to the key to use for the operation.
2905 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002906 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002907 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002908 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002909 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002910 * \param[in] salt A salt or label, if supported by the
2911 * encryption algorithm.
2912 * If the algorithm does not support a
2913 * salt, pass \c NULL.
2914 * If the algorithm supports an optional
2915 * salt and you do not want to pass a salt,
2916 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002917 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002918 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2919 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002920 * \param salt_length Size of the \p salt buffer in bytes.
2921 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002922 * \param[out] output Buffer where the decrypted message is to
2923 * be written.
2924 * \param output_size Size of the \c output buffer in bytes.
2925 * \param[out] output_length On success, the number of bytes
2926 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002927 *
Gilles Peskine28538492018-07-11 17:34:00 +02002928 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002929 * \retval #PSA_ERROR_INVALID_HANDLE
2930 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002931 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002932 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002933 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002934 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002935 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002936 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002937 * \retval #PSA_ERROR_NOT_SUPPORTED
2938 * \retval #PSA_ERROR_INVALID_ARGUMENT
2939 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2940 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2941 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002942 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002943 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002944 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2945 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002946 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002947 * The library has not been previously initialized by psa_crypto_init().
2948 * It is implementation-dependent whether a failure to initialize
2949 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002950 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002951psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002952 psa_algorithm_t alg,
2953 const uint8_t *input,
2954 size_t input_length,
2955 const uint8_t *salt,
2956 size_t salt_length,
2957 uint8_t *output,
2958 size_t output_size,
2959 size_t *output_length);
2960
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002961/**@}*/
2962
Gilles Peskine35675b62019-05-16 17:26:11 +02002963/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002964 * @{
2965 */
2966
Gilles Peskine35675b62019-05-16 17:26:11 +02002967/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002968 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002969 * Before calling any function on a key derivation operation object, the
2970 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002971 * - Set the structure to all-bits-zero, for example:
2972 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002973 * psa_key_derivation_operation_t operation;
2974 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002975 * \endcode
2976 * - Initialize the structure to logical zero values, for example:
2977 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002978 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002979 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002980 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002981 * for example:
2982 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002983 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002984 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002985 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002986 * to the structure, for example:
2987 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002988 * psa_key_derivation_operation_t operation;
2989 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002990 * \endcode
2991 *
2992 * This is an implementation-defined \c struct. Applications should not
2993 * make any assumptions about the content of this structure except
2994 * as directed by the documentation of a specific implementation.
2995 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02002996typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002997
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002998/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002999 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003000 * This macro returns a suitable initializer for a key derivation operation
3001 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003002 */
3003#ifdef __DOXYGEN_ONLY__
3004/* This is an example definition for documentation purposes.
3005 * Implementations should define a suitable value in `crypto_struct.h`.
3006 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003007#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003008#endif
3009
Gilles Peskine35675b62019-05-16 17:26:11 +02003010/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003011 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003012static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003013
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003014/** Set up a key derivation operation.
3015 *
3016 * A key derivation algorithm takes some inputs and uses them to generate
3017 * a byte stream in a deterministic way.
3018 * This byte stream can be used to produce keys and other
3019 * cryptographic material.
3020 *
3021 * To derive a key:
3022 * - Start with an initialized object of type #psa_key_derivation_operation_t.
3023 * - Call psa_key_derivation_setup() to select the algorithm.
3024 * - Provide the inputs for the key derivation by calling
3025 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3026 * as appropriate. Which inputs are needed, in what order, and whether
3027 * they may be keys and if so of what type depends on the algorithm.
3028 * - Optionally set the operation's maximum capacity with
3029 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3030 * of or after providing inputs. For some algorithms, this step is mandatory
3031 * because the output depends on the maximum capacity.
3032 * - To derive a key, call psa_key_derivation_output_key().
3033 * To derive a byte string for a different purpose, call
3034 * - psa_key_derivation_output_bytes().
3035 * Successive calls to these functions use successive output bytes
3036 * calculated by the key derivation algorithm.
3037 * - Clean up the key derivation operation object with
3038 * psa_key_derivation_abort().
3039 *
3040 * \param[in,out] operation The key derivation operation object
3041 * to set up. It must
3042 * have been initialized but not set up yet.
3043 * \param alg The key derivation algorithm to compute
3044 * (\c PSA_ALG_XXX value such that
3045 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3046 *
3047 * \retval #PSA_SUCCESS
3048 * Success.
3049 * \retval #PSA_ERROR_INVALID_ARGUMENT
3050 * \c alg is not a key derivation algorithm.
3051 * \retval #PSA_ERROR_NOT_SUPPORTED
3052 * \c alg is not supported or is not a key derivation algorithm.
3053 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3054 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3055 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003056 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003057 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01003058 * The operation state is either not initialized or has already been setup.
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003059 * \retval #PSA_ERROR_BAD_STATE
3060 * The library has not been previously initialized by psa_crypto_init().
3061 * It is implementation-dependent whether a failure to initialize
3062 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003063 */
3064psa_status_t psa_key_derivation_setup(
3065 psa_key_derivation_operation_t *operation,
3066 psa_algorithm_t alg);
3067
Gilles Peskine35675b62019-05-16 17:26:11 +02003068/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003069 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003070 * The capacity of a key derivation is the maximum number of bytes that it can
3071 * return. When you get *N* bytes of output from a key derivation operation,
3072 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003073 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003074 * \param[in] operation The operation to query.
3075 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003076 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003077 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003078 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003079 * \retval #PSA_ERROR_BAD_STATE
3080 * The operation state is not valid.
3081 * \retval #PSA_ERROR_BAD_STATE
3082 * The library has not been previously initialized by psa_crypto_init().
3083 * It is implementation-dependent whether a failure to initialize
3084 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003085 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003086psa_status_t psa_key_derivation_get_capacity(
3087 const psa_key_derivation_operation_t *operation,
3088 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003089
Gilles Peskine35675b62019-05-16 17:26:11 +02003090/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003091 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003092 * The capacity of a key derivation operation is the maximum number of bytes
3093 * that the key derivation operation can return from this point onwards.
3094 *
3095 * \param[in,out] operation The key derivation operation object to modify.
3096 * \param capacity The new capacity of the operation.
3097 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003098 * current capacity.
3099 *
3100 * \retval #PSA_SUCCESS
3101 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003102 * \p capacity is larger than the operation's current capacity.
3103 * In this case, the operation object remains valid and its capacity
3104 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003105 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003106 * The operation state is not valid.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003107 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003108 * \retval #PSA_ERROR_BAD_STATE
3109 * The library has not been previously initialized by psa_crypto_init().
3110 * It is implementation-dependent whether a failure to initialize
3111 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003112 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003113psa_status_t psa_key_derivation_set_capacity(
3114 psa_key_derivation_operation_t *operation,
3115 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003116
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003117/** Use the maximum possible capacity for a key derivation operation.
3118 *
3119 * Use this value as the capacity argument when setting up a key derivation
3120 * to indicate that the operation should have the maximum possible capacity.
3121 * The value of the maximum possible capacity depends on the key derivation
3122 * algorithm.
3123 */
3124#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3125
3126/** Provide an input for key derivation or key agreement.
3127 *
3128 * Which inputs are required and in what order depends on the algorithm.
3129 * Refer to the documentation of each key derivation or key agreement
3130 * algorithm for information.
3131 *
3132 * This function passes direct inputs. Some inputs must be passed as keys
3133 * using psa_key_derivation_input_key() instead of this function. Refer to
3134 * the documentation of individual step types for information.
3135 *
3136 * \param[in,out] operation The key derivation operation object to use.
3137 * It must have been set up with
3138 * psa_key_derivation_setup() and must not
3139 * have produced any output yet.
3140 * \param step Which step the input data is for.
3141 * \param[in] data Input data to use.
3142 * \param data_length Size of the \p data buffer in bytes.
3143 *
3144 * \retval #PSA_SUCCESS
3145 * Success.
3146 * \retval #PSA_ERROR_INVALID_ARGUMENT
3147 * \c step is not compatible with the operation's algorithm.
3148 * \retval #PSA_ERROR_INVALID_ARGUMENT
3149 * \c step does not allow direct inputs.
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
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003154 * \retval #PSA_ERROR_BAD_STATE
3155 * The value of \p step is not valid given the state of \p operation.
3156 * \retval #PSA_ERROR_BAD_STATE
3157 * The library has not been previously initialized by psa_crypto_init().
3158 * It is implementation-dependent whether a failure to initialize
3159 * results in this error code.
3160 */
3161psa_status_t psa_key_derivation_input_bytes(
3162 psa_key_derivation_operation_t *operation,
3163 psa_key_derivation_step_t step,
3164 const uint8_t *data,
3165 size_t data_length);
3166
3167/** Provide an input for key derivation in the form of a key.
3168 *
3169 * Which inputs are required and in what order depends on the algorithm.
3170 * Refer to the documentation of each key derivation or key agreement
3171 * algorithm for information.
3172 *
3173 * This function passes key inputs. Some inputs must be passed as keys
3174 * of the appropriate type using this function, while others must be
3175 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3176 * the documentation of individual step types for information.
3177 *
3178 * \param[in,out] operation The key derivation operation object to use.
3179 * It must have been set up with
3180 * psa_key_derivation_setup() and must not
3181 * have produced any output yet.
3182 * \param step Which step the input data is for.
3183 * \param handle Handle to the key. It must have an
3184 * appropriate type for \p step and must
3185 * allow the usage #PSA_KEY_USAGE_DERIVE.
3186 *
3187 * \retval #PSA_SUCCESS
3188 * Success.
3189 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003190 * \retval #PSA_ERROR_NOT_PERMITTED
3191 * \retval #PSA_ERROR_INVALID_ARGUMENT
3192 * \c step is not compatible with the operation's algorithm.
3193 * \retval #PSA_ERROR_INVALID_ARGUMENT
3194 * \c step does not allow key inputs.
3195 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3196 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3197 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003198 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003199 * \retval #PSA_ERROR_BAD_STATE
3200 * The value of \p step is not valid given the state of \p operation.
3201 * \retval #PSA_ERROR_BAD_STATE
3202 * The library has not been previously initialized by psa_crypto_init().
3203 * It is implementation-dependent whether a failure to initialize
3204 * results in this error code.
3205 */
3206psa_status_t psa_key_derivation_input_key(
3207 psa_key_derivation_operation_t *operation,
3208 psa_key_derivation_step_t step,
3209 psa_key_handle_t handle);
3210
3211/** Perform a key agreement and use the shared secret as input to a key
3212 * derivation.
3213 *
3214 * A key agreement algorithm takes two inputs: a private key \p private_key
3215 * a public key \p peer_key.
3216 * The result of this function is passed as input to a key derivation.
3217 * The output of this key derivation can be extracted by reading from the
3218 * resulting operation to produce keys and other cryptographic material.
3219 *
3220 * \param[in,out] operation The key derivation operation object to use.
3221 * It must have been set up with
3222 * psa_key_derivation_setup() with a
3223 * key agreement and derivation algorithm
3224 * \c alg (\c PSA_ALG_XXX value such that
3225 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3226 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3227 * is false).
3228 * The operation must be ready for an
3229 * input of the type given by \p step.
3230 * \param step Which step the input data is for.
3231 * \param private_key Handle to the private key to use.
3232 * \param[in] peer_key Public key of the peer. The peer key must be in the
3233 * same format that psa_import_key() accepts for the
3234 * public key type corresponding to the type of
3235 * private_key. That is, this function performs the
3236 * equivalent of
3237 * #psa_import_key(...,
3238 * `peer_key`, `peer_key_length`) where
3239 * with key attributes indicating the public key
3240 * type corresponding to the type of `private_key`.
3241 * For example, for EC keys, this means that peer_key
3242 * is interpreted as a point on the curve that the
3243 * private key is on. The standard formats for public
3244 * keys are documented in the documentation of
3245 * psa_export_public_key().
3246 * \param peer_key_length Size of \p peer_key in bytes.
3247 *
3248 * \retval #PSA_SUCCESS
3249 * Success.
3250 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003251 * \retval #PSA_ERROR_NOT_PERMITTED
3252 * \retval #PSA_ERROR_INVALID_ARGUMENT
3253 * \c private_key is not compatible with \c alg,
3254 * or \p peer_key is not valid for \c alg or not compatible with
3255 * \c private_key.
3256 * \retval #PSA_ERROR_NOT_SUPPORTED
3257 * \c alg is not supported or is not a key derivation algorithm.
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. Shawdec47b62019-08-07 14:25:38 +01003262 * \retval #PSA_ERROR_BAD_STATE
3263 * The library has not been previously initialized by psa_crypto_init().
3264 * It is implementation-dependent whether a failure to initialize
3265 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003266 */
3267psa_status_t psa_key_derivation_key_agreement(
3268 psa_key_derivation_operation_t *operation,
3269 psa_key_derivation_step_t step,
3270 psa_key_handle_t private_key,
3271 const uint8_t *peer_key,
3272 size_t peer_key_length);
3273
Gilles Peskine35675b62019-05-16 17:26:11 +02003274/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003275 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003276 * This function calculates output bytes from a key derivation algorithm and
3277 * return those bytes.
3278 * If you view the key derivation's output as a stream of bytes, this
3279 * function destructively reads the requested number of bytes from the
3280 * stream.
3281 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003282 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003283 * \param[in,out] operation The key derivation operation object to read from.
3284 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003285 * \param output_length Number of bytes to output.
3286 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003287 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003288 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003289 * The operation's capacity was less than
3290 * \p output_length bytes. Note that in this case,
3291 * no output is written to the output buffer.
3292 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003293 * subsequent calls to this function will not
3294 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003295 * \retval #PSA_ERROR_BAD_STATE
3296 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3297 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3298 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003299 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003300 * \retval #PSA_ERROR_BAD_STATE
3301 * The library has not been previously initialized by psa_crypto_init().
3302 * It is implementation-dependent whether a failure to initialize
3303 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003304 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003305psa_status_t psa_key_derivation_output_bytes(
3306 psa_key_derivation_operation_t *operation,
3307 uint8_t *output,
3308 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003309
Gilles Peskine35675b62019-05-16 17:26:11 +02003310/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003311 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003312 * This function calculates output bytes from a key derivation algorithm
3313 * and uses those bytes to generate a key deterministically.
3314 * If you view the key derivation's output as a stream of bytes, this
3315 * function destructively reads as many bytes as required from the
3316 * stream.
3317 * The operation's capacity decreases by the number of bytes read.
3318 *
3319 * How much output is produced and consumed from the operation, and how
3320 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003321 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003322 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003323 * of a given size, this function is functionally equivalent to
3324 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003325 * and passing the resulting output to #psa_import_key.
3326 * However, this function has a security benefit:
3327 * if the implementation provides an isolation boundary then
3328 * the key material is not exposed outside the isolation boundary.
3329 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003330 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003331 * The following key types defined in this specification follow this scheme:
3332 *
3333 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003334 * - #PSA_KEY_TYPE_ARC4;
3335 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003336 * - #PSA_KEY_TYPE_DERIVE;
3337 * - #PSA_KEY_TYPE_HMAC.
3338 *
3339 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003340 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003341 * Montgomery curve), this function always draws a byte string whose
3342 * length is determined by the curve, and sets the mandatory bits
3343 * accordingly. That is:
3344 *
3345 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3346 * and process it as specified in RFC 7748 &sect;5.
3347 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3348 * and process it as specified in RFC 7748 &sect;5.
3349 *
3350 * - For key types for which the key is represented by a single sequence of
3351 * \p bits bits with constraints as to which bit sequences are acceptable,
3352 * this function draws a byte string of length (\p bits / 8) bytes rounded
3353 * up to the nearest whole number of bytes. If the resulting byte string
3354 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3355 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003356 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003357 * for the output produced by psa_export_key().
3358 * The following key types defined in this specification follow this scheme:
3359 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003360 * - #PSA_KEY_TYPE_DES.
3361 * Force-set the parity bits, but discard forbidden weak keys.
3362 * For 2-key and 3-key triple-DES, the three keys are generated
3363 * successively (for example, for 3-key triple-DES,
3364 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3365 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003366 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003367 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003368 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003369 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003370 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003371 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003372 * Weierstrass curve).
3373 * For these key types, interpret the byte string as integer
3374 * in big-endian order. Discard it if it is not in the range
3375 * [0, *N* - 2] where *N* is the boundary of the private key domain
3376 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003377 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003378 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003379 * This method allows compliance to NIST standards, specifically
3380 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003381 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3382 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3383 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3384 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003385 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003386 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003387 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003388 * implementation-defined.
3389 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003390 * In all cases, the data that is read is discarded from the operation.
3391 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003392 *
Gilles Peskine20628592019-04-19 19:29:50 +02003393 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003394 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003395 * \param[out] handle On success, a handle to the newly created key.
3396 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003397 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003398 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003399 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003400 * If the key is persistent, the key material and the key's metadata
3401 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003402 * \retval #PSA_ERROR_ALREADY_EXISTS
3403 * This is an attempt to create a persistent key, and there is
3404 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003405 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003406 * There was not enough data to create the desired key.
3407 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003408 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003409 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003410 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003411 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003412 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003413 * \retval #PSA_ERROR_INVALID_ARGUMENT
3414 * The provided key attributes are not valid for the operation.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003415 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003416 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3417 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3418 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3419 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003420 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003421 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003422 * The library has not been previously initialized by psa_crypto_init().
3423 * It is implementation-dependent whether a failure to initialize
3424 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003425 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003426psa_status_t psa_key_derivation_output_key(
3427 const psa_key_attributes_t *attributes,
3428 psa_key_derivation_operation_t *operation,
3429 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003430
Gilles Peskine35675b62019-05-16 17:26:11 +02003431/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003432 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003433 * Once a key derivation operation has been aborted, its capacity is zero.
3434 * Aborting an operation frees all associated resources except for the
3435 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003436 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003437 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003438 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003439 * psa_key_derivation_operation_init() or a zero value. In particular,
3440 * it is valid to call psa_key_derivation_abort() twice, or to call
3441 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003442 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003443 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003444 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003445 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003446 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003447 * \retval #PSA_SUCCESS
3448 * \retval #PSA_ERROR_BAD_STATE
3449 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3450 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003451 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003452 * \retval #PSA_ERROR_BAD_STATE
3453 * The library has not been previously initialized by psa_crypto_init().
3454 * It is implementation-dependent whether a failure to initialize
3455 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003456 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003457psa_status_t psa_key_derivation_abort(
3458 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003459
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003460/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003461 *
3462 * \warning The raw result of a key agreement algorithm such as finite-field
3463 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3464 * not be used directly as key material. It should instead be passed as
3465 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003466 * a key derivation, use psa_key_derivation_key_agreement() and other
3467 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003468 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003469 * \param alg The key agreement algorithm to compute
3470 * (\c PSA_ALG_XXX value such that
3471 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3472 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003473 * \param private_key Handle to the private key to use.
3474 * \param[in] peer_key Public key of the peer. It must be
3475 * in the same format that psa_import_key()
3476 * accepts. The standard formats for public
3477 * keys are documented in the documentation
3478 * of psa_export_public_key().
3479 * \param peer_key_length Size of \p peer_key in bytes.
3480 * \param[out] output Buffer where the decrypted message is to
3481 * be written.
3482 * \param output_size Size of the \c output buffer in bytes.
3483 * \param[out] output_length On success, the number of bytes
3484 * that make up the returned output.
3485 *
3486 * \retval #PSA_SUCCESS
3487 * Success.
3488 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003489 * \retval #PSA_ERROR_NOT_PERMITTED
3490 * \retval #PSA_ERROR_INVALID_ARGUMENT
3491 * \p alg is not a key agreement algorithm
3492 * \retval #PSA_ERROR_INVALID_ARGUMENT
3493 * \p private_key is not compatible with \p alg,
3494 * or \p peer_key is not valid for \p alg or not compatible with
3495 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003496 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3497 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003498 * \retval #PSA_ERROR_NOT_SUPPORTED
3499 * \p alg is not a supported key agreement algorithm.
3500 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3501 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3502 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003503 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003504 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003505 * \retval #PSA_ERROR_BAD_STATE
3506 * The library has not been previously initialized by psa_crypto_init().
3507 * It is implementation-dependent whether a failure to initialize
3508 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003509 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003510psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3511 psa_key_handle_t private_key,
3512 const uint8_t *peer_key,
3513 size_t peer_key_length,
3514 uint8_t *output,
3515 size_t output_size,
3516 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003517
Gilles Peskineea0fb492018-07-12 17:17:20 +02003518/**@}*/
3519
Gilles Peskineedd76872018-07-20 17:42:05 +02003520/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003521 * @{
3522 */
3523
3524/**
3525 * \brief Generate random bytes.
3526 *
3527 * \warning This function **can** fail! Callers MUST check the return status
3528 * and MUST NOT use the content of the output buffer if the return
3529 * status is not #PSA_SUCCESS.
3530 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003531 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003532 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003533 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003534 * \param output_size Number of bytes to generate and output.
3535 *
Gilles Peskine28538492018-07-11 17:34:00 +02003536 * \retval #PSA_SUCCESS
3537 * \retval #PSA_ERROR_NOT_SUPPORTED
3538 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003539 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003540 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3541 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003542 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003543 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003544 * The library has not been previously initialized by psa_crypto_init().
3545 * It is implementation-dependent whether a failure to initialize
3546 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003547 */
3548psa_status_t psa_generate_random(uint8_t *output,
3549 size_t output_size);
3550
3551/**
3552 * \brief Generate a key or key pair.
3553 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003554 * The key is generated randomly.
3555 * Its location, policy, type and size are taken from \p attributes.
3556 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003557 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003558 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003559 * the public exponent is 65537.
3560 * The modulus is a product of two probabilistic primes
3561 * between 2^{n-1} and 2^n where n is the bit size specified in the
3562 * attributes.
3563 *
Gilles Peskine20628592019-04-19 19:29:50 +02003564 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003565 * \param[out] handle On success, a handle to the newly created key.
3566 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003567 *
Gilles Peskine28538492018-07-11 17:34:00 +02003568 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003569 * Success.
3570 * If the key is persistent, the key material and the key's metadata
3571 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003572 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003573 * This is an attempt to create a persistent key, and there is
3574 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003575 * \retval #PSA_ERROR_NOT_SUPPORTED
3576 * \retval #PSA_ERROR_INVALID_ARGUMENT
3577 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3578 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3579 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3580 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003581 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003582 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3583 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003584 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003585 * The library has not been previously initialized by psa_crypto_init().
3586 * It is implementation-dependent whether a failure to initialize
3587 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003588 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003589psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003590 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003591
3592/**@}*/
3593
Gilles Peskinee59236f2018-01-27 23:32:46 +01003594#ifdef __cplusplus
3595}
3596#endif
3597
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003598/* The file "crypto_sizes.h" contains definitions for size calculation
3599 * macros whose definitions are implementation-specific. */
3600#include "crypto_sizes.h"
3601
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003602/* The file "crypto_struct.h" contains definitions for
3603 * implementation-specific structs that are declared above. */
3604#include "crypto_struct.h"
3605
3606/* The file "crypto_extra.h" contains vendor-specific definitions. This
3607 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003608#include "crypto_extra.h"
3609
3610#endif /* PSA_CRYPTO_H */