blob: 0ecc41ff208c56329a3f37aa3cd73674816df3db [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.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100862 * \retval #PSA_ERROR_INVALID_ARGUMENT
863 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100864 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
865 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
866 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200867 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100868 * \retval #PSA_ERROR_BAD_STATE
869 * The library has not been previously initialized by psa_crypto_init().
870 * It is implementation-dependent whether a failure to initialize
871 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100872 */
873psa_status_t psa_hash_compare(psa_algorithm_t alg,
874 const uint8_t *input,
875 size_t input_length,
876 const uint8_t *hash,
877 const size_t hash_length);
878
Gilles Peskine308b91d2018-02-08 09:47:44 +0100879/** The type of the state data structure for multipart hash operations.
880 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000881 * Before calling any function on a hash operation object, the application must
882 * initialize it by any of the following means:
883 * - Set the structure to all-bits-zero, for example:
884 * \code
885 * psa_hash_operation_t operation;
886 * memset(&operation, 0, sizeof(operation));
887 * \endcode
888 * - Initialize the structure to logical zero values, for example:
889 * \code
890 * psa_hash_operation_t operation = {0};
891 * \endcode
892 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
893 * for example:
894 * \code
895 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
896 * \endcode
897 * - Assign the result of the function psa_hash_operation_init()
898 * to the structure, for example:
899 * \code
900 * psa_hash_operation_t operation;
901 * operation = psa_hash_operation_init();
902 * \endcode
903 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100904 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100905 * make any assumptions about the content of this structure except
906 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100907typedef struct psa_hash_operation_s psa_hash_operation_t;
908
Jaeden Amero6a25b412019-01-04 11:47:44 +0000909/** \def PSA_HASH_OPERATION_INIT
910 *
911 * This macro returns a suitable initializer for a hash operation object
912 * of type #psa_hash_operation_t.
913 */
914#ifdef __DOXYGEN_ONLY__
915/* This is an example definition for documentation purposes.
916 * Implementations should define a suitable value in `crypto_struct.h`.
917 */
918#define PSA_HASH_OPERATION_INIT {0}
919#endif
920
921/** Return an initial value for a hash operation object.
922 */
923static psa_hash_operation_t psa_hash_operation_init(void);
924
Gilles Peskinef45adda2019-01-14 18:29:18 +0100925/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100926 *
927 * The sequence of operations to calculate a hash (message digest)
928 * is as follows:
929 * -# Allocate an operation object which will be passed to all the functions
930 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000931 * -# Initialize the operation object with one of the methods described in the
932 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200933 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100934 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100935 * of the message each time. The hash that is calculated is the hash
936 * of the concatenation of these messages in order.
937 * -# To calculate the hash, call psa_hash_finish().
938 * To compare the hash with an expected value, call psa_hash_verify().
939 *
940 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000941 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100942 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200943 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100944 * eventually terminate the operation. The following events terminate an
945 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100946 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100947 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100948 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000949 * \param[in,out] operation The operation object to set up. It must have
950 * been initialized as per the documentation for
951 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200952 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
953 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100954 *
Gilles Peskine28538492018-07-11 17:34:00 +0200955 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100956 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200957 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200958 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100959 * \retval #PSA_ERROR_BAD_STATE
960 * The operation state is not valid (already set up and not
961 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200962 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
963 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
964 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200965 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100966 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawe970d652019-08-08 14:40:04 +0100967 * \p operation is either not initialized or is in use
968 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100969 * The library has not been previously initialized by psa_crypto_init().
970 * It is implementation-dependent whether a failure to initialize
971 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100972 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200973psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100974 psa_algorithm_t alg);
975
Gilles Peskine308b91d2018-02-08 09:47:44 +0100976/** Add a message fragment to a multipart hash operation.
977 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200978 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100979 *
980 * If this function returns an error status, the operation becomes inactive.
981 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200982 * \param[in,out] operation Active hash operation.
983 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200984 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100985 *
Gilles Peskine28538492018-07-11 17:34:00 +0200986 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100987 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200988 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100989 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200990 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
991 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
992 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200993 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100994 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +0100995 * The operation state is not valid.
996 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100997 * The library has not been previously initialized by psa_crypto_init().
998 * It is implementation-dependent whether a failure to initialize
999 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001000 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001001psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1002 const uint8_t *input,
1003 size_t input_length);
1004
Gilles Peskine308b91d2018-02-08 09:47:44 +01001005/** Finish the calculation of the hash of a message.
1006 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001007 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001008 * This function calculates the hash of the message formed by concatenating
1009 * the inputs passed to preceding calls to psa_hash_update().
1010 *
1011 * When this function returns, the operation becomes inactive.
1012 *
1013 * \warning Applications should not call this function if they expect
1014 * a specific value for the hash. Call psa_hash_verify() instead.
1015 * Beware that comparing integrity or authenticity data such as
1016 * hash values with a function such as \c memcmp is risky
1017 * because the time taken by the comparison may leak information
1018 * about the hashed data which could allow an attacker to guess
1019 * a valid hash and thereby bypass security controls.
1020 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001021 * \param[in,out] operation Active hash operation.
1022 * \param[out] hash Buffer where the hash is to be written.
1023 * \param hash_size Size of the \p hash buffer in bytes.
1024 * \param[out] hash_length On success, the number of bytes
1025 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001026 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001027 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001028 *
Gilles Peskine28538492018-07-11 17:34:00 +02001029 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001030 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001031 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001032 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001033 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001034 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001035 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001036 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001037 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1038 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1039 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001040 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001041 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001042 * The operation state is not valid.
1043 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001044 * The library has not been previously initialized by psa_crypto_init().
1045 * It is implementation-dependent whether a failure to initialize
1046 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001047 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001048psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1049 uint8_t *hash,
1050 size_t hash_size,
1051 size_t *hash_length);
1052
Gilles Peskine308b91d2018-02-08 09:47:44 +01001053/** Finish the calculation of the hash of a message and compare it with
1054 * an expected value.
1055 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001056 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001057 * This function calculates the hash of the message formed by concatenating
1058 * the inputs passed to preceding calls to psa_hash_update(). It then
1059 * compares the calculated hash with the expected hash passed as a
1060 * parameter to this function.
1061 *
1062 * When this function returns, the operation becomes inactive.
1063 *
Gilles Peskine19067982018-03-20 17:54:53 +01001064 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001065 * comparison between the actual hash and the expected hash is performed
1066 * in constant time.
1067 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001068 * \param[in,out] operation Active hash operation.
1069 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001070 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001071 *
Gilles Peskine28538492018-07-11 17:34:00 +02001072 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001073 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001074 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001075 * The hash of the message was calculated successfully, but it
1076 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001077 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001078 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001079 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1080 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1081 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001082 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001083 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001084 * The operation state is not valid.
1085 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001086 * The library has not been previously initialized by psa_crypto_init().
1087 * It is implementation-dependent whether a failure to initialize
1088 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001089 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001090psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1091 const uint8_t *hash,
1092 size_t hash_length);
1093
Gilles Peskine308b91d2018-02-08 09:47:44 +01001094/** Abort a hash operation.
1095 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001096 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001097 * \p operation structure itself. Once aborted, the operation object
1098 * can be reused for another operation by calling
1099 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001100 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001101 * You may call this function any time after the operation object has
1102 * been initialized by any of the following methods:
1103 * - A call to psa_hash_setup(), whether it succeeds or not.
1104 * - Initializing the \c struct to all-bits-zero.
1105 * - Initializing the \c struct to logical zeros, e.g.
1106 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001107 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001108 * In particular, calling psa_hash_abort() after the operation has been
1109 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1110 * psa_hash_verify() is safe and has no effect.
1111 *
1112 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001113 *
Gilles Peskine28538492018-07-11 17:34:00 +02001114 * \retval #PSA_SUCCESS
1115 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001116 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001117 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1118 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001119 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001120 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001121 * The operation state is not valid.
1122 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001123 * The library has not been previously initialized by psa_crypto_init().
1124 * It is implementation-dependent whether a failure to initialize
1125 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001126 */
1127psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001128
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001129/** Clone a hash operation.
1130 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001131 * This function copies the state of an ongoing hash operation to
1132 * a new operation object. In other words, this function is equivalent
1133 * to calling psa_hash_setup() on \p target_operation with the same
1134 * algorithm that \p source_operation was set up for, then
1135 * psa_hash_update() on \p target_operation with the same input that
1136 * that was passed to \p source_operation. After this function returns, the
1137 * two objects are independent, i.e. subsequent calls involving one of
1138 * the objects do not affect the other object.
1139 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001140 * \param[in] source_operation The active hash operation to clone.
1141 * \param[in,out] target_operation The operation object to set up.
1142 * It must be initialized but not active.
1143 *
1144 * \retval #PSA_SUCCESS
1145 * \retval #PSA_ERROR_BAD_STATE
1146 * \p source_operation is not an active hash operation.
1147 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001148 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001149 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1150 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001151 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001152 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001153 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001154 * The operation state is either not initialized or has already been setup.
1155 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001156 * The library has not been previously initialized by psa_crypto_init().
1157 * It is implementation-dependent whether a failure to initialize
1158 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001159 */
1160psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1161 psa_hash_operation_t *target_operation);
1162
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001163/**@}*/
1164
Gilles Peskine8c9def32018-02-08 10:02:12 +01001165/** \defgroup MAC Message authentication codes
1166 * @{
1167 */
1168
Gilles Peskine69647a42019-01-14 20:18:12 +01001169/** Calculate the MAC (message authentication code) of a message.
1170 *
1171 * \note To verify the MAC of a message against an
1172 * expected value, use psa_mac_verify() instead.
1173 * Beware that comparing integrity or authenticity data such as
1174 * MAC values with a function such as \c memcmp is risky
1175 * because the time taken by the comparison may leak information
1176 * about the MAC value which could allow an attacker to guess
1177 * a valid MAC and thereby bypass security controls.
1178 *
1179 * \param handle Handle to the key to use for the operation.
1180 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001181 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001182 * \param[in] input Buffer containing the input message.
1183 * \param input_length Size of the \p input buffer in bytes.
1184 * \param[out] mac Buffer where the MAC value is to be written.
1185 * \param mac_size Size of the \p mac buffer in bytes.
1186 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001187 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001188 *
1189 * \retval #PSA_SUCCESS
1190 * Success.
1191 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001192 * \retval #PSA_ERROR_NOT_PERMITTED
1193 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001194 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001195 * \retval #PSA_ERROR_NOT_SUPPORTED
1196 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001197 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1198 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001199 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1200 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1201 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001202 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001203 * \retval #PSA_ERROR_STORAGE_FAILURE
1204 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001205 * \retval #PSA_ERROR_BAD_STATE
1206 * The library has not been previously initialized by psa_crypto_init().
1207 * It is implementation-dependent whether a failure to initialize
1208 * results in this error code.
1209 */
1210psa_status_t psa_mac_compute(psa_key_handle_t handle,
1211 psa_algorithm_t alg,
1212 const uint8_t *input,
1213 size_t input_length,
1214 uint8_t *mac,
1215 size_t mac_size,
1216 size_t *mac_length);
1217
1218/** Calculate the MAC of a message and compare it with a reference value.
1219 *
1220 * \param handle Handle to the key to use for the operation.
1221 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001222 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001223 * \param[in] input Buffer containing the input message.
1224 * \param input_length Size of the \p input buffer in bytes.
1225 * \param[out] mac Buffer containing the expected MAC value.
1226 * \param mac_length Size of the \p mac buffer in bytes.
1227 *
1228 * \retval #PSA_SUCCESS
1229 * The expected MAC is identical to the actual MAC of the input.
1230 * \retval #PSA_ERROR_INVALID_SIGNATURE
1231 * The MAC of the message was calculated successfully, but it
1232 * differs from the expected value.
1233 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001234 * \retval #PSA_ERROR_NOT_PERMITTED
1235 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001236 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001237 * \retval #PSA_ERROR_NOT_SUPPORTED
1238 * \p alg is not supported or is not a MAC algorithm.
1239 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1240 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1241 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001242 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001243 * \retval #PSA_ERROR_STORAGE_FAILURE
1244 * The key could not be retrieved from storage.
1245 * \retval #PSA_ERROR_BAD_STATE
1246 * The library has not been previously initialized by psa_crypto_init().
1247 * It is implementation-dependent whether a failure to initialize
1248 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001249 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001250psa_status_t psa_mac_verify(psa_key_handle_t handle,
1251 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001252 const uint8_t *input,
1253 size_t input_length,
1254 const uint8_t *mac,
1255 const size_t mac_length);
1256
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001257/** The type of the state data structure for multipart MAC operations.
1258 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001259 * Before calling any function on a MAC operation object, the application must
1260 * initialize it by any of the following means:
1261 * - Set the structure to all-bits-zero, for example:
1262 * \code
1263 * psa_mac_operation_t operation;
1264 * memset(&operation, 0, sizeof(operation));
1265 * \endcode
1266 * - Initialize the structure to logical zero values, for example:
1267 * \code
1268 * psa_mac_operation_t operation = {0};
1269 * \endcode
1270 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1271 * for example:
1272 * \code
1273 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1274 * \endcode
1275 * - Assign the result of the function psa_mac_operation_init()
1276 * to the structure, for example:
1277 * \code
1278 * psa_mac_operation_t operation;
1279 * operation = psa_mac_operation_init();
1280 * \endcode
1281 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001282 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001283 * make any assumptions about the content of this structure except
1284 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001285typedef struct psa_mac_operation_s psa_mac_operation_t;
1286
Jaeden Amero769ce272019-01-04 11:48:03 +00001287/** \def PSA_MAC_OPERATION_INIT
1288 *
1289 * This macro returns a suitable initializer for a MAC operation object of type
1290 * #psa_mac_operation_t.
1291 */
1292#ifdef __DOXYGEN_ONLY__
1293/* This is an example definition for documentation purposes.
1294 * Implementations should define a suitable value in `crypto_struct.h`.
1295 */
1296#define PSA_MAC_OPERATION_INIT {0}
1297#endif
1298
1299/** Return an initial value for a MAC operation object.
1300 */
1301static psa_mac_operation_t psa_mac_operation_init(void);
1302
Gilles Peskinef45adda2019-01-14 18:29:18 +01001303/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001304 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001305 * This function sets up the calculation of the MAC
1306 * (message authentication code) of a byte string.
1307 * To verify the MAC of a message against an
1308 * expected value, use psa_mac_verify_setup() instead.
1309 *
1310 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001311 * -# Allocate an operation object which will be passed to all the functions
1312 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001313 * -# Initialize the operation object with one of the methods described in the
1314 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001315 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001316 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1317 * of the message each time. The MAC that is calculated is the MAC
1318 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001319 * -# At the end of the message, call psa_mac_sign_finish() to finish
1320 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001321 *
1322 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001323 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001324 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001325 * After a successful call to psa_mac_sign_setup(), the application must
1326 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001327 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001328 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001329 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001330 * \param[in,out] operation The operation object to set up. It must have
1331 * been initialized as per the documentation for
1332 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001333 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001334 * It must remain valid until the operation
1335 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001336 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001337 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001338 *
Gilles Peskine28538492018-07-11 17:34:00 +02001339 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001340 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001341 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001342 * \retval #PSA_ERROR_NOT_PERMITTED
1343 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001344 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001345 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001346 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001347 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1348 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1349 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001350 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001351 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001352 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001353 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001354 * The operation state is not valid (already set up and not
1355 * subsequently completed).
1356 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001357 * The library has not been previously initialized by psa_crypto_init().
1358 * It is implementation-dependent whether a failure to initialize
1359 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001360 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001361psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001362 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001363 psa_algorithm_t alg);
1364
Gilles Peskinef45adda2019-01-14 18:29:18 +01001365/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001366 *
1367 * This function sets up the verification of the MAC
1368 * (message authentication code) of a byte string against an expected value.
1369 *
1370 * The sequence of operations to verify a MAC is as follows:
1371 * -# Allocate an operation object which will be passed to all the functions
1372 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001373 * -# Initialize the operation object with one of the methods described in the
1374 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001375 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001376 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1377 * of the message each time. The MAC that is calculated is the MAC
1378 * of the concatenation of these messages in order.
1379 * -# At the end of the message, call psa_mac_verify_finish() to finish
1380 * calculating the actual MAC of the message and verify it against
1381 * the expected value.
1382 *
1383 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001384 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001385 *
1386 * After a successful call to psa_mac_verify_setup(), the application must
1387 * eventually terminate the operation through one of the following methods:
1388 * - A failed call to psa_mac_update().
1389 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1390 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001391 * \param[in,out] operation The operation object to set up. It must have
1392 * been initialized as per the documentation for
1393 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001394 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001395 * It must remain valid until the operation
1396 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001397 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1398 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001399 *
Gilles Peskine28538492018-07-11 17:34:00 +02001400 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001401 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001402 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001403 * \retval #PSA_ERROR_NOT_PERMITTED
1404 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001405 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001406 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001407 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001408 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1409 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1410 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001411 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001412 * \retval #PSA_ERROR_STORAGE_FAILURE
1413 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001414 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001415 * The operation state is not valid (already set up and not
1416 * subsequently completed).
1417 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001418 * The library has not been previously initialized by psa_crypto_init().
1419 * It is implementation-dependent whether a failure to initialize
1420 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001421 */
1422psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001423 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001424 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001425
Gilles Peskinedcd14942018-07-12 00:30:52 +02001426/** Add a message fragment to a multipart MAC operation.
1427 *
1428 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1429 * before calling this function.
1430 *
1431 * If this function returns an error status, the operation becomes inactive.
1432 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001433 * \param[in,out] operation Active MAC operation.
1434 * \param[in] input Buffer containing the message fragment to add to
1435 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001436 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001437 *
1438 * \retval #PSA_SUCCESS
1439 * Success.
1440 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001441 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001442 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1443 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1444 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001445 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001446 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001447 * \retval #PSA_ERROR_BAD_STATE
1448 * The library has not been previously initialized by psa_crypto_init().
1449 * It is implementation-dependent whether a failure to initialize
1450 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001451 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001452psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1453 const uint8_t *input,
1454 size_t input_length);
1455
Gilles Peskinedcd14942018-07-12 00:30:52 +02001456/** Finish the calculation of the MAC of a message.
1457 *
1458 * The application must call psa_mac_sign_setup() before calling this function.
1459 * This function calculates the MAC of the message formed by concatenating
1460 * the inputs passed to preceding calls to psa_mac_update().
1461 *
1462 * When this function returns, the operation becomes inactive.
1463 *
1464 * \warning Applications should not call this function if they expect
1465 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1466 * Beware that comparing integrity or authenticity data such as
1467 * MAC values with a function such as \c memcmp is risky
1468 * because the time taken by the comparison may leak information
1469 * about the MAC value which could allow an attacker to guess
1470 * a valid MAC and thereby bypass security controls.
1471 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001472 * \param[in,out] operation Active MAC operation.
1473 * \param[out] mac Buffer where the MAC value is to be written.
1474 * \param mac_size Size of the \p mac buffer in bytes.
1475 * \param[out] mac_length On success, the number of bytes
1476 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001477 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001478 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001479 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001480 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001481 *
1482 * \retval #PSA_SUCCESS
1483 * Success.
1484 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001485 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001486 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001487 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001488 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1489 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1490 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1491 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001492 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001493 * \retval #PSA_ERROR_BAD_STATE
1494 * The library has not been previously initialized by psa_crypto_init().
1495 * It is implementation-dependent whether a failure to initialize
1496 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001497 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001498psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1499 uint8_t *mac,
1500 size_t mac_size,
1501 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001502
Gilles Peskinedcd14942018-07-12 00:30:52 +02001503/** Finish the calculation of the MAC of a message and compare it with
1504 * an expected value.
1505 *
1506 * The application must call psa_mac_verify_setup() before calling this function.
1507 * This function calculates the MAC of the message formed by concatenating
1508 * the inputs passed to preceding calls to psa_mac_update(). It then
1509 * compares the calculated MAC with the expected MAC passed as a
1510 * parameter to this function.
1511 *
1512 * When this function returns, the operation becomes inactive.
1513 *
1514 * \note Implementations shall make the best effort to ensure that the
1515 * comparison between the actual MAC and the expected MAC is performed
1516 * in constant time.
1517 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001518 * \param[in,out] operation Active MAC operation.
1519 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001520 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001521 *
1522 * \retval #PSA_SUCCESS
1523 * The expected MAC is identical to the actual MAC of the message.
1524 * \retval #PSA_ERROR_INVALID_SIGNATURE
1525 * The MAC of the message was calculated successfully, but it
1526 * differs from the expected MAC.
1527 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001528 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001529 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1530 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1531 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001532 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001533 * \retval #PSA_ERROR_BAD_STATE
1534 * The library has not been previously initialized by psa_crypto_init().
1535 * It is implementation-dependent whether a failure to initialize
1536 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001537 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001538psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1539 const uint8_t *mac,
1540 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001541
Gilles Peskinedcd14942018-07-12 00:30:52 +02001542/** Abort a MAC operation.
1543 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001544 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001545 * \p operation structure itself. Once aborted, the operation object
1546 * can be reused for another operation by calling
1547 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001548 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001549 * You may call this function any time after the operation object has
1550 * been initialized by any of the following methods:
1551 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1552 * it succeeds or not.
1553 * - Initializing the \c struct to all-bits-zero.
1554 * - Initializing the \c struct to logical zeros, e.g.
1555 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001556 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001557 * In particular, calling psa_mac_abort() after the operation has been
1558 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1559 * psa_mac_verify_finish() is safe and has no effect.
1560 *
1561 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001562 *
1563 * \retval #PSA_SUCCESS
1564 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001565 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001566 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1567 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001568 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001569 * \retval #PSA_ERROR_BAD_STATE
1570 * The library has not been previously initialized by psa_crypto_init().
1571 * It is implementation-dependent whether a failure to initialize
1572 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001573 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001574psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1575
1576/**@}*/
1577
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001578/** \defgroup cipher Symmetric ciphers
1579 * @{
1580 */
1581
Gilles Peskine69647a42019-01-14 20:18:12 +01001582/** Encrypt a message using a symmetric cipher.
1583 *
1584 * This function encrypts a message with a random IV (initialization
1585 * vector).
1586 *
1587 * \param handle Handle to the key to use for the operation.
1588 * It must remain valid until the operation
1589 * terminates.
1590 * \param alg The cipher algorithm to compute
1591 * (\c PSA_ALG_XXX value such that
1592 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1593 * \param[in] input Buffer containing the message to encrypt.
1594 * \param input_length Size of the \p input buffer in bytes.
1595 * \param[out] output Buffer where the output is to be written.
1596 * The output contains the IV followed by
1597 * the ciphertext proper.
1598 * \param output_size Size of the \p output buffer in bytes.
1599 * \param[out] output_length On success, the number of bytes
1600 * that make up the output.
1601 *
1602 * \retval #PSA_SUCCESS
1603 * Success.
1604 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001605 * \retval #PSA_ERROR_NOT_PERMITTED
1606 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001607 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001608 * \retval #PSA_ERROR_NOT_SUPPORTED
1609 * \p alg is not supported or is not a cipher algorithm.
1610 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1611 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1612 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1613 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001614 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001615 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001616 * \retval #PSA_ERROR_BAD_STATE
1617 * The library has not been previously initialized by psa_crypto_init().
1618 * It is implementation-dependent whether a failure to initialize
1619 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001620 */
1621psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1622 psa_algorithm_t alg,
1623 const uint8_t *input,
1624 size_t input_length,
1625 uint8_t *output,
1626 size_t output_size,
1627 size_t *output_length);
1628
1629/** Decrypt a message using a symmetric cipher.
1630 *
1631 * This function decrypts a message encrypted with a symmetric cipher.
1632 *
1633 * \param handle Handle to the key to use for the operation.
1634 * It must remain valid until the operation
1635 * terminates.
1636 * \param alg The cipher algorithm to compute
1637 * (\c PSA_ALG_XXX value such that
1638 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1639 * \param[in] input Buffer containing the message to decrypt.
1640 * This consists of the IV followed by the
1641 * ciphertext proper.
1642 * \param input_length Size of the \p input buffer in bytes.
1643 * \param[out] output Buffer where the plaintext is to be written.
1644 * \param output_size Size of the \p output buffer in bytes.
1645 * \param[out] output_length On success, the number of bytes
1646 * that make up the output.
1647 *
1648 * \retval #PSA_SUCCESS
1649 * Success.
1650 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001651 * \retval #PSA_ERROR_NOT_PERMITTED
1652 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001653 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001654 * \retval #PSA_ERROR_NOT_SUPPORTED
1655 * \p alg is not supported or is not a cipher algorithm.
1656 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1657 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1658 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1659 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001660 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001661 * \retval #PSA_ERROR_BAD_STATE
1662 * The library has not been previously initialized by psa_crypto_init().
1663 * It is implementation-dependent whether a failure to initialize
1664 * results in this error code. * \retval #PSA_ERROR_CORRUPTION_DETECTED
1665
Gilles Peskine69647a42019-01-14 20:18:12 +01001666 */
1667psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1668 psa_algorithm_t alg,
1669 const uint8_t *input,
1670 size_t input_length,
1671 uint8_t *output,
1672 size_t output_size,
1673 size_t *output_length);
1674
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001675/** The type of the state data structure for multipart cipher operations.
1676 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001677 * Before calling any function on a cipher operation object, the application
1678 * must initialize it by any of the following means:
1679 * - Set the structure to all-bits-zero, for example:
1680 * \code
1681 * psa_cipher_operation_t operation;
1682 * memset(&operation, 0, sizeof(operation));
1683 * \endcode
1684 * - Initialize the structure to logical zero values, for example:
1685 * \code
1686 * psa_cipher_operation_t operation = {0};
1687 * \endcode
1688 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1689 * for example:
1690 * \code
1691 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1692 * \endcode
1693 * - Assign the result of the function psa_cipher_operation_init()
1694 * to the structure, for example:
1695 * \code
1696 * psa_cipher_operation_t operation;
1697 * operation = psa_cipher_operation_init();
1698 * \endcode
1699 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001700 * This is an implementation-defined \c struct. Applications should not
1701 * make any assumptions about the content of this structure except
1702 * as directed by the documentation of a specific implementation. */
1703typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1704
Jaeden Amero5bae2272019-01-04 11:48:27 +00001705/** \def PSA_CIPHER_OPERATION_INIT
1706 *
1707 * This macro returns a suitable initializer for a cipher operation object of
1708 * type #psa_cipher_operation_t.
1709 */
1710#ifdef __DOXYGEN_ONLY__
1711/* This is an example definition for documentation purposes.
1712 * Implementations should define a suitable value in `crypto_struct.h`.
1713 */
1714#define PSA_CIPHER_OPERATION_INIT {0}
1715#endif
1716
1717/** Return an initial value for a cipher operation object.
1718 */
1719static psa_cipher_operation_t psa_cipher_operation_init(void);
1720
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001721/** Set the key for a multipart symmetric encryption operation.
1722 *
1723 * The sequence of operations to encrypt a message with a symmetric cipher
1724 * is as follows:
1725 * -# Allocate an operation object which will be passed to all the functions
1726 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001727 * -# Initialize the operation object with one of the methods described in the
1728 * documentation for #psa_cipher_operation_t, e.g.
1729 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001730 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001731 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001732 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001733 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001734 * requires a specific IV value.
1735 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1736 * of the message each time.
1737 * -# Call psa_cipher_finish().
1738 *
1739 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001740 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001741 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001742 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001743 * eventually terminate the operation. The following events terminate an
1744 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001745 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001746 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001747 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001748 * \param[in,out] operation The operation object to set up. It must have
1749 * been initialized as per the documentation for
1750 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001751 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001752 * It must remain valid until the operation
1753 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001754 * \param alg The cipher algorithm to compute
1755 * (\c PSA_ALG_XXX value such that
1756 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001757 *
Gilles Peskine28538492018-07-11 17:34:00 +02001758 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001759 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001760 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001761 * \retval #PSA_ERROR_NOT_PERMITTED
1762 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001763 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001764 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001765 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001766 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1767 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1768 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001769 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001770 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001771 * The operation state is not valid (already set up and not
1772 * subsequently completed).
1773 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001774 * The library has not been previously initialized by psa_crypto_init().
1775 * It is implementation-dependent whether a failure to initialize
1776 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001777 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001778psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001779 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001780 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001781
1782/** Set the key for a multipart symmetric decryption operation.
1783 *
1784 * The sequence of operations to decrypt a message with a symmetric cipher
1785 * is as follows:
1786 * -# Allocate an operation object which will be passed to all the functions
1787 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001788 * -# Initialize the operation object with one of the methods described in the
1789 * documentation for #psa_cipher_operation_t, e.g.
1790 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001791 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001792 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001793 * decryption. If the IV is prepended to the ciphertext, you can call
1794 * psa_cipher_update() on a buffer containing the IV followed by the
1795 * beginning of the message.
1796 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1797 * of the message each time.
1798 * -# Call psa_cipher_finish().
1799 *
1800 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001801 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001802 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001803 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001804 * eventually terminate the operation. The following events terminate an
1805 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001806 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001807 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001808 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001809 * \param[in,out] operation The operation object to set up. It must have
1810 * been initialized as per the documentation for
1811 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001812 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001813 * It must remain valid until the operation
1814 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001815 * \param alg The cipher algorithm to compute
1816 * (\c PSA_ALG_XXX value such that
1817 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001818 *
Gilles Peskine28538492018-07-11 17:34:00 +02001819 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001820 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001821 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001822 * \retval #PSA_ERROR_NOT_PERMITTED
1823 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001824 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001825 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001826 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001827 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1828 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1829 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001830 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001831 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001832 * The operation state is not valid (already set up and not
1833 * subsequently completed).
1834 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001835 * The library has not been previously initialized by psa_crypto_init().
1836 * It is implementation-dependent whether a failure to initialize
1837 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001838 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001839psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001840 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001841 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001842
Gilles Peskinedcd14942018-07-12 00:30:52 +02001843/** Generate an IV for a symmetric encryption operation.
1844 *
1845 * This function generates a random IV (initialization vector), nonce
1846 * or initial counter value for the encryption operation as appropriate
1847 * for the chosen algorithm, key type and key size.
1848 *
1849 * The application must call psa_cipher_encrypt_setup() before
1850 * calling this function.
1851 *
1852 * If this function returns an error status, the operation becomes inactive.
1853 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001854 * \param[in,out] operation Active cipher operation.
1855 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001856 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001857 * \param[out] iv_length On success, the number of bytes of the
1858 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001859 *
1860 * \retval #PSA_SUCCESS
1861 * Success.
1862 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001863 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001864 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001865 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001866 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1867 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1868 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001869 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001870 * \retval #PSA_ERROR_BAD_STATE
1871 * The library has not been previously initialized by psa_crypto_init().
1872 * It is implementation-dependent whether a failure to initialize
1873 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001874 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001875psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001876 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001877 size_t iv_size,
1878 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001879
Gilles Peskinedcd14942018-07-12 00:30:52 +02001880/** Set the IV for a symmetric encryption or decryption operation.
1881 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001882 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001883 * or initial counter value for the encryption or decryption operation.
1884 *
1885 * The application must call psa_cipher_encrypt_setup() before
1886 * calling this function.
1887 *
1888 * If this function returns an error status, the operation becomes inactive.
1889 *
1890 * \note When encrypting, applications should use psa_cipher_generate_iv()
1891 * instead of this function, unless implementing a protocol that requires
1892 * a non-random IV.
1893 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001894 * \param[in,out] operation Active cipher operation.
1895 * \param[in] iv Buffer containing the IV to use.
1896 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001897 *
1898 * \retval #PSA_SUCCESS
1899 * Success.
1900 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001901 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001902 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001903 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001904 * or the chosen algorithm does not use an IV.
1905 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1906 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1907 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001908 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001909 * \retval #PSA_ERROR_BAD_STATE
1910 * The library has not been previously initialized by psa_crypto_init().
1911 * It is implementation-dependent whether a failure to initialize
1912 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001913 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001914psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001915 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001916 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001917
Gilles Peskinedcd14942018-07-12 00:30:52 +02001918/** Encrypt or decrypt a message fragment in an active cipher operation.
1919 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001920 * Before calling this function, you must:
1921 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1922 * The choice of setup function determines whether this function
1923 * encrypts or decrypts its input.
1924 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1925 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001926 *
1927 * If this function returns an error status, the operation becomes inactive.
1928 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001929 * \param[in,out] operation Active cipher operation.
1930 * \param[in] input Buffer containing the message fragment to
1931 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001932 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001933 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001934 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001935 * \param[out] output_length On success, the number of bytes
1936 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001937 *
1938 * \retval #PSA_SUCCESS
1939 * Success.
1940 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001941 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001942 * not set, or already completed).
1943 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1944 * The size of the \p output buffer is too small.
1945 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1946 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1947 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001948 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001949 * \retval #PSA_ERROR_BAD_STATE
1950 * The library has not been previously initialized by psa_crypto_init().
1951 * It is implementation-dependent whether a failure to initialize
1952 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001953 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001954psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1955 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001956 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001957 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001958 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001959 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001960
Gilles Peskinedcd14942018-07-12 00:30:52 +02001961/** Finish encrypting or decrypting a message in a cipher operation.
1962 *
1963 * The application must call psa_cipher_encrypt_setup() or
1964 * psa_cipher_decrypt_setup() before calling this function. The choice
1965 * of setup function determines whether this function encrypts or
1966 * decrypts its input.
1967 *
1968 * This function finishes the encryption or decryption of the message
1969 * formed by concatenating the inputs passed to preceding calls to
1970 * psa_cipher_update().
1971 *
1972 * When this function returns, the operation becomes inactive.
1973 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001974 * \param[in,out] operation Active cipher operation.
1975 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001976 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001977 * \param[out] output_length On success, the number of bytes
1978 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001979 *
1980 * \retval #PSA_SUCCESS
1981 * Success.
1982 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001983 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001984 * not set, or already completed).
1985 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1986 * The size of the \p output buffer is too small.
1987 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1988 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1989 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001990 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001991 * \retval #PSA_ERROR_BAD_STATE
1992 * The library has not been previously initialized by psa_crypto_init().
1993 * It is implementation-dependent whether a failure to initialize
1994 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001995 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001996psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001997 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001998 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001999 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002000
Gilles Peskinedcd14942018-07-12 00:30:52 +02002001/** Abort a cipher operation.
2002 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002003 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002004 * \p operation structure itself. Once aborted, the operation object
2005 * can be reused for another operation by calling
2006 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002007 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002008 * You may call this function any time after the operation object has
2009 * been initialized by any of the following methods:
2010 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2011 * whether it succeeds or not.
2012 * - Initializing the \c struct to all-bits-zero.
2013 * - Initializing the \c struct to logical zeros, e.g.
2014 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002015 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002016 * In particular, calling psa_cipher_abort() after the operation has been
2017 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2018 * is safe and has no effect.
2019 *
2020 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002021 *
2022 * \retval #PSA_SUCCESS
2023 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002024 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002025 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2026 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002027 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002028 * \retval #PSA_ERROR_BAD_STATE
2029 * The library has not been previously initialized by psa_crypto_init().
2030 * It is implementation-dependent whether a failure to initialize
2031 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002032 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002033psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2034
2035/**@}*/
2036
Gilles Peskine3b555712018-03-03 21:27:57 +01002037/** \defgroup aead Authenticated encryption with associated data (AEAD)
2038 * @{
2039 */
2040
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002041/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002042 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002043 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002044 * \param alg The AEAD algorithm to compute
2045 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002046 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002047 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002048 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002049 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002050 * but not encrypted.
2051 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002052 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002053 * encrypted.
2054 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002055 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002056 * encrypted data. The additional data is not
2057 * part of this output. For algorithms where the
2058 * encrypted data and the authentication tag
2059 * are defined as separate outputs, the
2060 * authentication tag is appended to the
2061 * encrypted data.
2062 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2063 * This must be at least
2064 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2065 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002066 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002067 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002068 *
Gilles Peskine28538492018-07-11 17:34:00 +02002069 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002070 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002071 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002072 * \retval #PSA_ERROR_NOT_PERMITTED
2073 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002074 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002075 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002076 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002077 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2078 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2079 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002080 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002081 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002082 * The library has not been previously initialized by psa_crypto_init().
2083 * It is implementation-dependent whether a failure to initialize
2084 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002085 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002086psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002087 psa_algorithm_t alg,
2088 const uint8_t *nonce,
2089 size_t nonce_length,
2090 const uint8_t *additional_data,
2091 size_t additional_data_length,
2092 const uint8_t *plaintext,
2093 size_t plaintext_length,
2094 uint8_t *ciphertext,
2095 size_t ciphertext_size,
2096 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002097
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002098/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002099 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002100 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002101 * \param alg The AEAD algorithm to compute
2102 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002103 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002104 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002105 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002106 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002107 * but not encrypted.
2108 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002109 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002110 * encrypted. For algorithms where the
2111 * encrypted data and the authentication tag
2112 * are defined as separate inputs, the buffer
2113 * must contain the encrypted data followed
2114 * by the authentication tag.
2115 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002116 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002117 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2118 * This must be at least
2119 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2120 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002121 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002122 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002123 *
Gilles Peskine28538492018-07-11 17:34:00 +02002124 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002125 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002126 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002127 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002128 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002129 * \retval #PSA_ERROR_NOT_PERMITTED
2130 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002131 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002132 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002133 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002134 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002135 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2136 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002137 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2138 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002139 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002140 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002141 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002142 * The library has not been previously initialized by psa_crypto_init().
2143 * It is implementation-dependent whether a failure to initialize
2144 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002145 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002146psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002147 psa_algorithm_t alg,
2148 const uint8_t *nonce,
2149 size_t nonce_length,
2150 const uint8_t *additional_data,
2151 size_t additional_data_length,
2152 const uint8_t *ciphertext,
2153 size_t ciphertext_length,
2154 uint8_t *plaintext,
2155 size_t plaintext_size,
2156 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002157
Gilles Peskine30a9e412019-01-14 18:36:12 +01002158/** The type of the state data structure for multipart AEAD operations.
2159 *
2160 * Before calling any function on an AEAD operation object, the application
2161 * must initialize it by any of the following means:
2162 * - Set the structure to all-bits-zero, for example:
2163 * \code
2164 * psa_aead_operation_t operation;
2165 * memset(&operation, 0, sizeof(operation));
2166 * \endcode
2167 * - Initialize the structure to logical zero values, for example:
2168 * \code
2169 * psa_aead_operation_t operation = {0};
2170 * \endcode
2171 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2172 * for example:
2173 * \code
2174 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2175 * \endcode
2176 * - Assign the result of the function psa_aead_operation_init()
2177 * to the structure, for example:
2178 * \code
2179 * psa_aead_operation_t operation;
2180 * operation = psa_aead_operation_init();
2181 * \endcode
2182 *
2183 * This is an implementation-defined \c struct. Applications should not
2184 * make any assumptions about the content of this structure except
2185 * as directed by the documentation of a specific implementation. */
2186typedef struct psa_aead_operation_s psa_aead_operation_t;
2187
2188/** \def PSA_AEAD_OPERATION_INIT
2189 *
2190 * This macro returns a suitable initializer for an AEAD operation object of
2191 * type #psa_aead_operation_t.
2192 */
2193#ifdef __DOXYGEN_ONLY__
2194/* This is an example definition for documentation purposes.
2195 * Implementations should define a suitable value in `crypto_struct.h`.
2196 */
2197#define PSA_AEAD_OPERATION_INIT {0}
2198#endif
2199
2200/** Return an initial value for an AEAD operation object.
2201 */
2202static psa_aead_operation_t psa_aead_operation_init(void);
2203
2204/** Set the key for a multipart authenticated encryption operation.
2205 *
2206 * The sequence of operations to encrypt a message with authentication
2207 * is as follows:
2208 * -# Allocate an operation object which will be passed to all the functions
2209 * listed here.
2210 * -# Initialize the operation object with one of the methods described in the
2211 * documentation for #psa_aead_operation_t, e.g.
2212 * PSA_AEAD_OPERATION_INIT.
2213 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002214 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2215 * inputs to the subsequent calls to psa_aead_update_ad() and
2216 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2217 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002218 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2219 * generate or set the nonce. You should use
2220 * psa_aead_generate_nonce() unless the protocol you are implementing
2221 * requires a specific nonce value.
2222 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2223 * of the non-encrypted additional authenticated data each time.
2224 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002225 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002226 * -# Call psa_aead_finish().
2227 *
2228 * The application may call psa_aead_abort() at any time after the operation
2229 * has been initialized.
2230 *
2231 * After a successful call to psa_aead_encrypt_setup(), the application must
2232 * eventually terminate the operation. The following events terminate an
2233 * operation:
2234 * - A failed call to any of the \c psa_aead_xxx functions.
2235 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2236 *
2237 * \param[in,out] operation The operation object to set up. It must have
2238 * been initialized as per the documentation for
2239 * #psa_aead_operation_t and not yet in use.
2240 * \param handle Handle to the key to use for the operation.
2241 * It must remain valid until the operation
2242 * terminates.
2243 * \param alg The AEAD algorithm to compute
2244 * (\c PSA_ALG_XXX value such that
2245 * #PSA_ALG_IS_AEAD(\p alg) is true).
2246 *
2247 * \retval #PSA_SUCCESS
2248 * Success.
2249 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002250 * \retval #PSA_ERROR_NOT_PERMITTED
2251 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002252 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002253 * \retval #PSA_ERROR_NOT_SUPPORTED
2254 * \p alg is not supported or is not an AEAD algorithm.
2255 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2256 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2257 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002258 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002259 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002260 * \retval #PSA_ERROR_BAD_STATE
2261 * The library has not been previously initialized by psa_crypto_init().
2262 * It is implementation-dependent whether a failure to initialize
2263 * results in this error code.
2264 */
2265psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2266 psa_key_handle_t handle,
2267 psa_algorithm_t alg);
2268
2269/** Set the key for a multipart authenticated decryption operation.
2270 *
2271 * The sequence of operations to decrypt a message with authentication
2272 * is as follows:
2273 * -# Allocate an operation object which will be passed to all the functions
2274 * listed here.
2275 * -# Initialize the operation object with one of the methods described in the
2276 * documentation for #psa_aead_operation_t, e.g.
2277 * PSA_AEAD_OPERATION_INIT.
2278 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002279 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2280 * inputs to the subsequent calls to psa_aead_update_ad() and
2281 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2282 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002283 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2284 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2285 * of the non-encrypted additional authenticated data each time.
2286 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002287 * of the ciphertext to decrypt each time.
2288 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002289 *
2290 * The application may call psa_aead_abort() at any time after the operation
2291 * has been initialized.
2292 *
2293 * After a successful call to psa_aead_decrypt_setup(), the application must
2294 * eventually terminate the operation. The following events terminate an
2295 * operation:
2296 * - A failed call to any of the \c psa_aead_xxx functions.
2297 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2298 *
2299 * \param[in,out] operation The operation object to set up. It must have
2300 * been initialized as per the documentation for
2301 * #psa_aead_operation_t and not yet in use.
2302 * \param handle Handle to the key to use for the operation.
2303 * It must remain valid until the operation
2304 * terminates.
2305 * \param alg The AEAD algorithm to compute
2306 * (\c PSA_ALG_XXX value such that
2307 * #PSA_ALG_IS_AEAD(\p alg) is true).
2308 *
2309 * \retval #PSA_SUCCESS
2310 * Success.
2311 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002312 * \retval #PSA_ERROR_NOT_PERMITTED
2313 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002314 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002315 * \retval #PSA_ERROR_NOT_SUPPORTED
2316 * \p alg is not supported or is not an AEAD algorithm.
2317 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2318 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2319 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002320 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002321 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002322 * \retval #PSA_ERROR_BAD_STATE
2323 * The library has not been previously initialized by psa_crypto_init().
2324 * It is implementation-dependent whether a failure to initialize
2325 * results in this error code.
2326 */
2327psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2328 psa_key_handle_t handle,
2329 psa_algorithm_t alg);
2330
2331/** Generate a random nonce for an authenticated encryption operation.
2332 *
2333 * This function generates a random nonce for the authenticated encryption
2334 * operation with an appropriate size for the chosen algorithm, key type
2335 * and key size.
2336 *
2337 * The application must call psa_aead_encrypt_setup() before
2338 * calling this function.
2339 *
2340 * If this function returns an error status, the operation becomes inactive.
2341 *
2342 * \param[in,out] operation Active AEAD operation.
2343 * \param[out] nonce Buffer where the generated nonce is to be
2344 * written.
2345 * \param nonce_size Size of the \p nonce buffer in bytes.
2346 * \param[out] nonce_length On success, the number of bytes of the
2347 * generated nonce.
2348 *
2349 * \retval #PSA_SUCCESS
2350 * Success.
2351 * \retval #PSA_ERROR_BAD_STATE
2352 * The operation state is not valid (not set up, or nonce already set).
2353 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2354 * The size of the \p nonce buffer is too small.
2355 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2356 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2357 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002358 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002359 * \retval #PSA_ERROR_BAD_STATE
2360 * The library has not been previously initialized by psa_crypto_init().
2361 * It is implementation-dependent whether a failure to initialize
2362 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002363 */
2364psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002365 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002366 size_t nonce_size,
2367 size_t *nonce_length);
2368
2369/** Set the nonce for an authenticated encryption or decryption operation.
2370 *
2371 * This function sets the nonce for the authenticated
2372 * encryption or decryption operation.
2373 *
2374 * The application must call psa_aead_encrypt_setup() before
2375 * calling this function.
2376 *
2377 * If this function returns an error status, the operation becomes inactive.
2378 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002379 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002380 * instead of this function, unless implementing a protocol that requires
2381 * a non-random IV.
2382 *
2383 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002384 * \param[in] nonce Buffer containing the nonce to use.
2385 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002386 *
2387 * \retval #PSA_SUCCESS
2388 * Success.
2389 * \retval #PSA_ERROR_BAD_STATE
2390 * The operation state is not valid (not set up, or nonce already set).
2391 * \retval #PSA_ERROR_INVALID_ARGUMENT
2392 * The size of \p nonce is not acceptable for the chosen algorithm.
2393 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2394 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2395 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002396 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002397 * \retval #PSA_ERROR_BAD_STATE
2398 * The library has not been previously initialized by psa_crypto_init().
2399 * It is implementation-dependent whether a failure to initialize
2400 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002401 */
2402psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002403 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002404 size_t nonce_length);
2405
Gilles Peskinebc59c852019-01-17 15:26:08 +01002406/** Declare the lengths of the message and additional data for AEAD.
2407 *
2408 * The application must call this function before calling
2409 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2410 * the operation requires it. If the algorithm does not require it,
2411 * calling this function is optional, but if this function is called
2412 * then the implementation must enforce the lengths.
2413 *
2414 * You may call this function before or after setting the nonce with
2415 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2416 *
2417 * - For #PSA_ALG_CCM, calling this function is required.
2418 * - For the other AEAD algorithms defined in this specification, calling
2419 * this function is not required.
2420 * - For vendor-defined algorithm, refer to the vendor documentation.
2421 *
2422 * \param[in,out] operation Active AEAD operation.
2423 * \param ad_length Size of the non-encrypted additional
2424 * authenticated data in bytes.
2425 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2426 *
2427 * \retval #PSA_SUCCESS
2428 * Success.
2429 * \retval #PSA_ERROR_BAD_STATE
2430 * The operation state is not valid (not set up, already completed,
2431 * or psa_aead_update_ad() or psa_aead_update() already called).
2432 * \retval #PSA_ERROR_INVALID_ARGUMENT
2433 * At least one of the lengths is not acceptable for the chosen
2434 * algorithm.
2435 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2436 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2437 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002438 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002439 * \retval #PSA_ERROR_BAD_STATE
2440 * The library has not been previously initialized by psa_crypto_init().
2441 * It is implementation-dependent whether a failure to initialize
2442 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002443 */
2444psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2445 size_t ad_length,
2446 size_t plaintext_length);
2447
Gilles Peskine30a9e412019-01-14 18:36:12 +01002448/** Pass additional data to an active AEAD operation.
2449 *
2450 * Additional data is authenticated, but not encrypted.
2451 *
2452 * You may call this function multiple times to pass successive fragments
2453 * of the additional data. You may not call this function after passing
2454 * data to encrypt or decrypt with psa_aead_update().
2455 *
2456 * Before calling this function, you must:
2457 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2458 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2459 *
2460 * If this function returns an error status, the operation becomes inactive.
2461 *
2462 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2463 * there is no guarantee that the input is valid. Therefore, until
2464 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2465 * treat the input as untrusted and prepare to undo any action that
2466 * depends on the input if psa_aead_verify() returns an error status.
2467 *
2468 * \param[in,out] operation Active AEAD operation.
2469 * \param[in] input Buffer containing the fragment of
2470 * additional data.
2471 * \param input_length Size of the \p input buffer in bytes.
2472 *
2473 * \retval #PSA_SUCCESS
2474 * Success.
2475 * \retval #PSA_ERROR_BAD_STATE
2476 * The operation state is not valid (not set up, nonce not set,
2477 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002478 * \retval #PSA_ERROR_INVALID_ARGUMENT
2479 * The total input length overflows the additional data length that
2480 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002481 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2482 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2483 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002484 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002485 * \retval #PSA_ERROR_BAD_STATE
2486 * The library has not been previously initialized by psa_crypto_init().
2487 * It is implementation-dependent whether a failure to initialize
2488 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002489 */
2490psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2491 const uint8_t *input,
2492 size_t input_length);
2493
2494/** Encrypt or decrypt a message fragment in an active AEAD operation.
2495 *
2496 * Before calling this function, you must:
2497 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2498 * The choice of setup function determines whether this function
2499 * encrypts or decrypts its input.
2500 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2501 * 3. Call psa_aead_update_ad() to pass all the additional data.
2502 *
2503 * If this function returns an error status, the operation becomes inactive.
2504 *
2505 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2506 * there is no guarantee that the input is valid. Therefore, until
2507 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2508 * - Do not use the output in any way other than storing it in a
2509 * confidential location. If you take any action that depends
2510 * on the tentative decrypted data, this action will need to be
2511 * undone if the input turns out not to be valid. Furthermore,
2512 * if an adversary can observe that this action took place
2513 * (for example through timing), they may be able to use this
2514 * fact as an oracle to decrypt any message encrypted with the
2515 * same key.
2516 * - In particular, do not copy the output anywhere but to a
2517 * memory or storage space that you have exclusive access to.
2518 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002519 * This function does not require the input to be aligned to any
2520 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002521 * a whole block at a time, it must consume all the input provided, but
2522 * it may delay the end of the corresponding output until a subsequent
2523 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2524 * provides sufficient input. The amount of data that can be delayed
2525 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002526 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002527 * \param[in,out] operation Active AEAD operation.
2528 * \param[in] input Buffer containing the message fragment to
2529 * encrypt or decrypt.
2530 * \param input_length Size of the \p input buffer in bytes.
2531 * \param[out] output Buffer where the output is to be written.
2532 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002533 * This must be at least
2534 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2535 * \p input_length) where \c alg is the
2536 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002537 * \param[out] output_length On success, the number of bytes
2538 * that make up the returned output.
2539 *
2540 * \retval #PSA_SUCCESS
2541 * Success.
2542 * \retval #PSA_ERROR_BAD_STATE
2543 * The operation state is not valid (not set up, nonce not set
2544 * or already completed).
2545 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2546 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002547 * You can determine a sufficient buffer size by calling
2548 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2549 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002550 * \retval #PSA_ERROR_INVALID_ARGUMENT
2551 * The total length of input to psa_aead_update_ad() so far is
2552 * less than the additional data length that was previously
2553 * specified with psa_aead_set_lengths().
2554 * \retval #PSA_ERROR_INVALID_ARGUMENT
2555 * The total input length overflows the plaintext length that
2556 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002557 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2558 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2559 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002560 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002561 * \retval #PSA_ERROR_BAD_STATE
2562 * The library has not been previously initialized by psa_crypto_init().
2563 * It is implementation-dependent whether a failure to initialize
2564 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002565 */
2566psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2567 const uint8_t *input,
2568 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002569 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002570 size_t output_size,
2571 size_t *output_length);
2572
2573/** Finish encrypting a message in an AEAD operation.
2574 *
2575 * The operation must have been set up with psa_aead_encrypt_setup().
2576 *
2577 * This function finishes the authentication of the additional data
2578 * formed by concatenating the inputs passed to preceding calls to
2579 * psa_aead_update_ad() with the plaintext formed by concatenating the
2580 * inputs passed to preceding calls to psa_aead_update().
2581 *
2582 * This function has two output buffers:
2583 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002584 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002585 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002586 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002587 * that the operation performs.
2588 *
2589 * When this function returns, the operation becomes inactive.
2590 *
2591 * \param[in,out] operation Active AEAD operation.
2592 * \param[out] ciphertext Buffer where the last part of the ciphertext
2593 * is to be written.
2594 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002595 * This must be at least
2596 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2597 * \c alg is the algorithm that is being
2598 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002599 * \param[out] ciphertext_length On success, the number of bytes of
2600 * returned ciphertext.
2601 * \param[out] tag Buffer where the authentication tag is
2602 * to be written.
2603 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002604 * This must be at least
2605 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2606 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002607 * \param[out] tag_length On success, the number of bytes
2608 * that make up the returned tag.
2609 *
2610 * \retval #PSA_SUCCESS
2611 * Success.
2612 * \retval #PSA_ERROR_BAD_STATE
2613 * The operation state is not valid (not set up, nonce not set,
2614 * decryption, or already completed).
2615 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002616 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002617 * You can determine a sufficient buffer size for \p ciphertext by
2618 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2619 * where \c alg is the algorithm that is being calculated.
2620 * You can determine a sufficient buffer size for \p tag by
2621 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002622 * \retval #PSA_ERROR_INVALID_ARGUMENT
2623 * The total length of input to psa_aead_update_ad() so far is
2624 * less than the additional data length that was previously
2625 * specified with psa_aead_set_lengths().
2626 * \retval #PSA_ERROR_INVALID_ARGUMENT
2627 * The total length of input to psa_aead_update() so far is
2628 * less than the plaintext length that was previously
2629 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002630 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2631 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2632 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002633 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002634 * \retval #PSA_ERROR_BAD_STATE
2635 * The library has not been previously initialized by psa_crypto_init().
2636 * It is implementation-dependent whether a failure to initialize
2637 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002638 */
2639psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002640 uint8_t *ciphertext,
2641 size_t ciphertext_size,
2642 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002643 uint8_t *tag,
2644 size_t tag_size,
2645 size_t *tag_length);
2646
2647/** Finish authenticating and decrypting a message in an AEAD operation.
2648 *
2649 * The operation must have been set up with psa_aead_decrypt_setup().
2650 *
2651 * This function finishes the authentication of the additional data
2652 * formed by concatenating the inputs passed to preceding calls to
2653 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2654 * inputs passed to preceding calls to psa_aead_update().
2655 *
2656 * When this function returns, the operation becomes inactive.
2657 *
2658 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002659 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002660 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002661 * from previous calls to psa_aead_update()
2662 * that could not be processed until the end
2663 * of the input.
2664 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002665 * This must be at least
2666 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2667 * \c alg is the algorithm that is being
2668 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002669 * \param[out] plaintext_length On success, the number of bytes of
2670 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002671 * \param[in] tag Buffer containing the authentication tag.
2672 * \param tag_length Size of the \p tag buffer in bytes.
2673 *
2674 * \retval #PSA_SUCCESS
2675 * Success.
2676 * \retval #PSA_ERROR_BAD_STATE
2677 * The operation state is not valid (not set up, nonce not set,
2678 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002679 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2680 * The size of the \p plaintext buffer is too small.
2681 * You can determine a sufficient buffer size for \p plaintext by
2682 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2683 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002684 * \retval #PSA_ERROR_INVALID_ARGUMENT
2685 * The total length of input to psa_aead_update_ad() so far is
2686 * less than the additional data length that was previously
2687 * specified with psa_aead_set_lengths().
2688 * \retval #PSA_ERROR_INVALID_ARGUMENT
2689 * The total length of input to psa_aead_update() so far is
2690 * less than the plaintext length that was previously
2691 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002692 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2693 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2694 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002695 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002696 * \retval #PSA_ERROR_BAD_STATE
2697 * The library has not been previously initialized by psa_crypto_init().
2698 * It is implementation-dependent whether a failure to initialize
2699 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002700 */
2701psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002702 uint8_t *plaintext,
2703 size_t plaintext_size,
2704 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002705 const uint8_t *tag,
2706 size_t tag_length);
2707
2708/** Abort an AEAD operation.
2709 *
2710 * Aborting an operation frees all associated resources except for the
2711 * \p operation structure itself. Once aborted, the operation object
2712 * can be reused for another operation by calling
2713 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2714 *
2715 * You may call this function any time after the operation object has
2716 * been initialized by any of the following methods:
2717 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2718 * whether it succeeds or not.
2719 * - Initializing the \c struct to all-bits-zero.
2720 * - Initializing the \c struct to logical zeros, e.g.
2721 * `psa_aead_operation_t operation = {0}`.
2722 *
2723 * In particular, calling psa_aead_abort() after the operation has been
2724 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2725 * is safe and has no effect.
2726 *
2727 * \param[in,out] operation Initialized AEAD operation.
2728 *
2729 * \retval #PSA_SUCCESS
2730 * \retval #PSA_ERROR_BAD_STATE
2731 * \p operation is not an active AEAD operation.
2732 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2733 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002734 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002735 * \retval #PSA_ERROR_BAD_STATE
2736 * The library has not been previously initialized by psa_crypto_init().
2737 * It is implementation-dependent whether a failure to initialize
2738 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002739 */
2740psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2741
Gilles Peskine3b555712018-03-03 21:27:57 +01002742/**@}*/
2743
Gilles Peskine20035e32018-02-03 22:44:14 +01002744/** \defgroup asymmetric Asymmetric cryptography
2745 * @{
2746 */
2747
2748/**
2749 * \brief Sign a hash or short message with a private key.
2750 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002751 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002752 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002753 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2754 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2755 * to determine the hash algorithm to use.
2756 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002757 * \param handle Handle to the key to use for the operation.
2758 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002759 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002760 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002761 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002762 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002763 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002764 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002765 * \param[out] signature_length On success, the number of bytes
2766 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002767 *
Gilles Peskine28538492018-07-11 17:34:00 +02002768 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002769 * \retval #PSA_ERROR_INVALID_HANDLE
2770 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002771 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002772 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002773 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002774 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002775 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002776 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002777 * \retval #PSA_ERROR_NOT_SUPPORTED
2778 * \retval #PSA_ERROR_INVALID_ARGUMENT
2779 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2780 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2781 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002782 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002783 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002784 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002785 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002786 * The library has not been previously initialized by psa_crypto_init().
2787 * It is implementation-dependent whether a failure to initialize
2788 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002789 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002790psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002791 psa_algorithm_t alg,
2792 const uint8_t *hash,
2793 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002794 uint8_t *signature,
2795 size_t signature_size,
2796 size_t *signature_length);
2797
2798/**
2799 * \brief Verify the signature a hash or short message using a public key.
2800 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002801 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002802 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002803 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2804 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2805 * to determine the hash algorithm to use.
2806 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002807 * \param handle Handle to the key to use for the operation.
2808 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002809 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002810 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002811 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002812 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002813 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002814 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002815 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002816 *
Gilles Peskine28538492018-07-11 17:34:00 +02002817 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002818 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002819 * \retval #PSA_ERROR_INVALID_HANDLE
2820 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002821 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002822 * The calculation was perfomed successfully, but the passed
2823 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002824 * \retval #PSA_ERROR_NOT_SUPPORTED
2825 * \retval #PSA_ERROR_INVALID_ARGUMENT
2826 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2827 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2828 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002829 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002830 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002831 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002832 * The library has not been previously initialized by psa_crypto_init().
2833 * It is implementation-dependent whether a failure to initialize
2834 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002835 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002836psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002837 psa_algorithm_t alg,
2838 const uint8_t *hash,
2839 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002840 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002841 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002842
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002843/**
2844 * \brief Encrypt a short message with a public key.
2845 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002846 * \param handle Handle to the key to use for the operation.
2847 * It must be a public key or an asymmetric
2848 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002849 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002850 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002851 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002852 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002853 * \param[in] salt A salt or label, if supported by the
2854 * encryption algorithm.
2855 * If the algorithm does not support a
2856 * salt, pass \c NULL.
2857 * If the algorithm supports an optional
2858 * salt and you do not want to pass a salt,
2859 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002860 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002861 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2862 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002863 * \param salt_length Size of the \p salt buffer in bytes.
2864 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002865 * \param[out] output Buffer where the encrypted message is to
2866 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002867 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002868 * \param[out] output_length On success, the number of bytes
2869 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002870 *
Gilles Peskine28538492018-07-11 17:34:00 +02002871 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002872 * \retval #PSA_ERROR_INVALID_HANDLE
2873 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002874 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002875 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002876 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002877 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002878 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002879 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002880 * \retval #PSA_ERROR_NOT_SUPPORTED
2881 * \retval #PSA_ERROR_INVALID_ARGUMENT
2882 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2883 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2884 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002885 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002886 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002887 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002888 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002889 * The library has not been previously initialized by psa_crypto_init().
2890 * It is implementation-dependent whether a failure to initialize
2891 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002892 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002893psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002894 psa_algorithm_t alg,
2895 const uint8_t *input,
2896 size_t input_length,
2897 const uint8_t *salt,
2898 size_t salt_length,
2899 uint8_t *output,
2900 size_t output_size,
2901 size_t *output_length);
2902
2903/**
2904 * \brief Decrypt a short message with a private key.
2905 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002906 * \param handle Handle to the key to use for the operation.
2907 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002908 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002909 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002910 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002911 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002912 * \param[in] salt A salt or label, if supported by the
2913 * encryption algorithm.
2914 * If the algorithm does not support a
2915 * salt, pass \c NULL.
2916 * If the algorithm supports an optional
2917 * salt and you do not want to pass a salt,
2918 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002919 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002920 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2921 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002922 * \param salt_length Size of the \p salt buffer in bytes.
2923 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002924 * \param[out] output Buffer where the decrypted message is to
2925 * be written.
2926 * \param output_size Size of the \c output buffer in bytes.
2927 * \param[out] output_length On success, the number of bytes
2928 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002929 *
Gilles Peskine28538492018-07-11 17:34:00 +02002930 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002931 * \retval #PSA_ERROR_INVALID_HANDLE
2932 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002933 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002934 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002935 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002936 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002937 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002938 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002939 * \retval #PSA_ERROR_NOT_SUPPORTED
2940 * \retval #PSA_ERROR_INVALID_ARGUMENT
2941 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2942 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2943 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002944 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002945 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002946 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2947 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002948 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002949 * The library has not been previously initialized by psa_crypto_init().
2950 * It is implementation-dependent whether a failure to initialize
2951 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002952 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002953psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002954 psa_algorithm_t alg,
2955 const uint8_t *input,
2956 size_t input_length,
2957 const uint8_t *salt,
2958 size_t salt_length,
2959 uint8_t *output,
2960 size_t output_size,
2961 size_t *output_length);
2962
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002963/**@}*/
2964
Gilles Peskine35675b62019-05-16 17:26:11 +02002965/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002966 * @{
2967 */
2968
Gilles Peskine35675b62019-05-16 17:26:11 +02002969/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002970 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002971 * Before calling any function on a key derivation operation object, the
2972 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002973 * - Set the structure to all-bits-zero, for example:
2974 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002975 * psa_key_derivation_operation_t operation;
2976 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002977 * \endcode
2978 * - Initialize the structure to logical zero values, for example:
2979 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002980 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002981 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002982 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002983 * for example:
2984 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002985 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002986 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002987 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002988 * to the structure, for example:
2989 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002990 * psa_key_derivation_operation_t operation;
2991 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002992 * \endcode
2993 *
2994 * This is an implementation-defined \c struct. Applications should not
2995 * make any assumptions about the content of this structure except
2996 * as directed by the documentation of a specific implementation.
2997 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02002998typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002999
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003000/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003001 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003002 * This macro returns a suitable initializer for a key derivation operation
3003 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003004 */
3005#ifdef __DOXYGEN_ONLY__
3006/* This is an example definition for documentation purposes.
3007 * Implementations should define a suitable value in `crypto_struct.h`.
3008 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003009#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003010#endif
3011
Gilles Peskine35675b62019-05-16 17:26:11 +02003012/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003013 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003014static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003015
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003016/** Set up a key derivation operation.
3017 *
3018 * A key derivation algorithm takes some inputs and uses them to generate
3019 * a byte stream in a deterministic way.
3020 * This byte stream can be used to produce keys and other
3021 * cryptographic material.
3022 *
3023 * To derive a key:
3024 * - Start with an initialized object of type #psa_key_derivation_operation_t.
3025 * - Call psa_key_derivation_setup() to select the algorithm.
3026 * - Provide the inputs for the key derivation by calling
3027 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3028 * as appropriate. Which inputs are needed, in what order, and whether
3029 * they may be keys and if so of what type depends on the algorithm.
3030 * - Optionally set the operation's maximum capacity with
3031 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3032 * of or after providing inputs. For some algorithms, this step is mandatory
3033 * because the output depends on the maximum capacity.
3034 * - To derive a key, call psa_key_derivation_output_key().
3035 * To derive a byte string for a different purpose, call
3036 * - psa_key_derivation_output_bytes().
3037 * Successive calls to these functions use successive output bytes
3038 * calculated by the key derivation algorithm.
3039 * - Clean up the key derivation operation object with
3040 * psa_key_derivation_abort().
3041 *
3042 * \param[in,out] operation The key derivation operation object
3043 * to set up. It must
3044 * have been initialized but not set up yet.
3045 * \param alg The key derivation algorithm to compute
3046 * (\c PSA_ALG_XXX value such that
3047 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3048 *
3049 * \retval #PSA_SUCCESS
3050 * Success.
3051 * \retval #PSA_ERROR_INVALID_ARGUMENT
3052 * \c alg is not a key derivation algorithm.
3053 * \retval #PSA_ERROR_NOT_SUPPORTED
3054 * \c alg is not supported or is not a key derivation algorithm.
3055 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3056 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3057 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003058 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003059 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01003060 * The operation state is either not initialized or has already been setup.
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003061 * \retval #PSA_ERROR_BAD_STATE
3062 * The library has not been previously initialized by psa_crypto_init().
3063 * It is implementation-dependent whether a failure to initialize
3064 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003065 */
3066psa_status_t psa_key_derivation_setup(
3067 psa_key_derivation_operation_t *operation,
3068 psa_algorithm_t alg);
3069
Gilles Peskine35675b62019-05-16 17:26:11 +02003070/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003071 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003072 * The capacity of a key derivation is the maximum number of bytes that it can
3073 * return. When you get *N* bytes of output from a key derivation operation,
3074 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003075 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003076 * \param[in] operation The operation to query.
3077 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003078 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003079 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003080 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003081 * \retval #PSA_ERROR_BAD_STATE
3082 * The operation state is not valid.
3083 * \retval #PSA_ERROR_BAD_STATE
3084 * The library has not been previously initialized by psa_crypto_init().
3085 * It is implementation-dependent whether a failure to initialize
3086 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003087 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003088psa_status_t psa_key_derivation_get_capacity(
3089 const psa_key_derivation_operation_t *operation,
3090 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003091
Gilles Peskine35675b62019-05-16 17:26:11 +02003092/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003093 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003094 * The capacity of a key derivation operation is the maximum number of bytes
3095 * that the key derivation operation can return from this point onwards.
3096 *
3097 * \param[in,out] operation The key derivation operation object to modify.
3098 * \param capacity The new capacity of the operation.
3099 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003100 * current capacity.
3101 *
3102 * \retval #PSA_SUCCESS
3103 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003104 * \p capacity is larger than the operation's current capacity.
3105 * In this case, the operation object remains valid and its capacity
3106 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003107 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003108 * The operation state is not valid.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003109 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003110 * \retval #PSA_ERROR_BAD_STATE
3111 * The library has not been previously initialized by psa_crypto_init().
3112 * It is implementation-dependent whether a failure to initialize
3113 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003114 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003115psa_status_t psa_key_derivation_set_capacity(
3116 psa_key_derivation_operation_t *operation,
3117 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003118
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003119/** Use the maximum possible capacity for a key derivation operation.
3120 *
3121 * Use this value as the capacity argument when setting up a key derivation
3122 * to indicate that the operation should have the maximum possible capacity.
3123 * The value of the maximum possible capacity depends on the key derivation
3124 * algorithm.
3125 */
3126#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3127
3128/** Provide an input for key derivation or key agreement.
3129 *
3130 * Which inputs are required and in what order depends on the algorithm.
3131 * Refer to the documentation of each key derivation or key agreement
3132 * algorithm for information.
3133 *
3134 * This function passes direct inputs. Some inputs must be passed as keys
3135 * using psa_key_derivation_input_key() instead of this function. Refer to
3136 * the documentation of individual step types for information.
3137 *
3138 * \param[in,out] operation The key derivation operation object to use.
3139 * It must have been set up with
3140 * psa_key_derivation_setup() and must not
3141 * have produced any output yet.
3142 * \param step Which step the input data is for.
3143 * \param[in] data Input data to use.
3144 * \param data_length Size of the \p data buffer in bytes.
3145 *
3146 * \retval #PSA_SUCCESS
3147 * Success.
3148 * \retval #PSA_ERROR_INVALID_ARGUMENT
3149 * \c step is not compatible with the operation's algorithm.
3150 * \retval #PSA_ERROR_INVALID_ARGUMENT
3151 * \c step does not allow direct inputs.
3152 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3153 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3154 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003155 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003156 * \retval #PSA_ERROR_BAD_STATE
3157 * The value of \p step is not valid given the state of \p operation.
3158 * \retval #PSA_ERROR_BAD_STATE
3159 * The library has not been previously initialized by psa_crypto_init().
3160 * It is implementation-dependent whether a failure to initialize
3161 * results in this error code.
3162 */
3163psa_status_t psa_key_derivation_input_bytes(
3164 psa_key_derivation_operation_t *operation,
3165 psa_key_derivation_step_t step,
3166 const uint8_t *data,
3167 size_t data_length);
3168
3169/** Provide an input for key derivation in the form of a key.
3170 *
3171 * Which inputs are required and in what order depends on the algorithm.
3172 * Refer to the documentation of each key derivation or key agreement
3173 * algorithm for information.
3174 *
3175 * This function passes key inputs. Some inputs must be passed as keys
3176 * of the appropriate type using this function, while others must be
3177 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3178 * the documentation of individual step types for information.
3179 *
3180 * \param[in,out] operation The key derivation operation object to use.
3181 * It must have been set up with
3182 * psa_key_derivation_setup() and must not
3183 * have produced any output yet.
3184 * \param step Which step the input data is for.
3185 * \param handle Handle to the key. It must have an
3186 * appropriate type for \p step and must
3187 * allow the usage #PSA_KEY_USAGE_DERIVE.
3188 *
3189 * \retval #PSA_SUCCESS
3190 * Success.
3191 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003192 * \retval #PSA_ERROR_NOT_PERMITTED
3193 * \retval #PSA_ERROR_INVALID_ARGUMENT
3194 * \c step is not compatible with the operation's algorithm.
3195 * \retval #PSA_ERROR_INVALID_ARGUMENT
3196 * \c step does not allow key inputs.
3197 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3198 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3199 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003200 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003201 * \retval #PSA_ERROR_BAD_STATE
3202 * The value of \p step is not valid given the state of \p operation.
3203 * \retval #PSA_ERROR_BAD_STATE
3204 * The library has not been previously initialized by psa_crypto_init().
3205 * It is implementation-dependent whether a failure to initialize
3206 * results in this error code.
3207 */
3208psa_status_t psa_key_derivation_input_key(
3209 psa_key_derivation_operation_t *operation,
3210 psa_key_derivation_step_t step,
3211 psa_key_handle_t handle);
3212
3213/** Perform a key agreement and use the shared secret as input to a key
3214 * derivation.
3215 *
3216 * A key agreement algorithm takes two inputs: a private key \p private_key
3217 * a public key \p peer_key.
3218 * The result of this function is passed as input to a key derivation.
3219 * The output of this key derivation can be extracted by reading from the
3220 * resulting operation to produce keys and other cryptographic material.
3221 *
3222 * \param[in,out] operation The key derivation operation object to use.
3223 * It must have been set up with
3224 * psa_key_derivation_setup() with a
3225 * key agreement and derivation algorithm
3226 * \c alg (\c PSA_ALG_XXX value such that
3227 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3228 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3229 * is false).
3230 * The operation must be ready for an
3231 * input of the type given by \p step.
3232 * \param step Which step the input data is for.
3233 * \param private_key Handle to the private key to use.
3234 * \param[in] peer_key Public key of the peer. The peer key must be in the
3235 * same format that psa_import_key() accepts for the
3236 * public key type corresponding to the type of
3237 * private_key. That is, this function performs the
3238 * equivalent of
3239 * #psa_import_key(...,
3240 * `peer_key`, `peer_key_length`) where
3241 * with key attributes indicating the public key
3242 * type corresponding to the type of `private_key`.
3243 * For example, for EC keys, this means that peer_key
3244 * is interpreted as a point on the curve that the
3245 * private key is on. The standard formats for public
3246 * keys are documented in the documentation of
3247 * psa_export_public_key().
3248 * \param peer_key_length Size of \p peer_key in bytes.
3249 *
3250 * \retval #PSA_SUCCESS
3251 * Success.
3252 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003253 * \retval #PSA_ERROR_NOT_PERMITTED
3254 * \retval #PSA_ERROR_INVALID_ARGUMENT
3255 * \c private_key is not compatible with \c alg,
3256 * or \p peer_key is not valid for \c alg or not compatible with
3257 * \c private_key.
3258 * \retval #PSA_ERROR_NOT_SUPPORTED
3259 * \c alg is not supported or is not a key derivation algorithm.
3260 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3261 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3262 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003263 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003264 * \retval #PSA_ERROR_BAD_STATE
3265 * The library has not been previously initialized by psa_crypto_init().
3266 * It is implementation-dependent whether a failure to initialize
3267 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003268 */
3269psa_status_t psa_key_derivation_key_agreement(
3270 psa_key_derivation_operation_t *operation,
3271 psa_key_derivation_step_t step,
3272 psa_key_handle_t private_key,
3273 const uint8_t *peer_key,
3274 size_t peer_key_length);
3275
Gilles Peskine35675b62019-05-16 17:26:11 +02003276/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003277 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003278 * This function calculates output bytes from a key derivation algorithm and
3279 * return those bytes.
3280 * If you view the key derivation's output as a stream of bytes, this
3281 * function destructively reads the requested number of bytes from the
3282 * stream.
3283 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003284 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003285 * \param[in,out] operation The key derivation operation object to read from.
3286 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003287 * \param output_length Number of bytes to output.
3288 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003289 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003290 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003291 * The operation's capacity was less than
3292 * \p output_length bytes. Note that in this case,
3293 * no output is written to the output buffer.
3294 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003295 * subsequent calls to this function will not
3296 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003297 * \retval #PSA_ERROR_BAD_STATE
3298 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3299 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3300 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003301 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003302 * \retval #PSA_ERROR_BAD_STATE
3303 * The library has not been previously initialized by psa_crypto_init().
3304 * It is implementation-dependent whether a failure to initialize
3305 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003306 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003307psa_status_t psa_key_derivation_output_bytes(
3308 psa_key_derivation_operation_t *operation,
3309 uint8_t *output,
3310 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003311
Gilles Peskine35675b62019-05-16 17:26:11 +02003312/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003313 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003314 * This function calculates output bytes from a key derivation algorithm
3315 * and uses those bytes to generate a key deterministically.
3316 * If you view the key derivation's output as a stream of bytes, this
3317 * function destructively reads as many bytes as required from the
3318 * stream.
3319 * The operation's capacity decreases by the number of bytes read.
3320 *
3321 * How much output is produced and consumed from the operation, and how
3322 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003323 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003324 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003325 * of a given size, this function is functionally equivalent to
3326 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003327 * and passing the resulting output to #psa_import_key.
3328 * However, this function has a security benefit:
3329 * if the implementation provides an isolation boundary then
3330 * the key material is not exposed outside the isolation boundary.
3331 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003332 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003333 * The following key types defined in this specification follow this scheme:
3334 *
3335 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003336 * - #PSA_KEY_TYPE_ARC4;
3337 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003338 * - #PSA_KEY_TYPE_DERIVE;
3339 * - #PSA_KEY_TYPE_HMAC.
3340 *
3341 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003342 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003343 * Montgomery curve), this function always draws a byte string whose
3344 * length is determined by the curve, and sets the mandatory bits
3345 * accordingly. That is:
3346 *
3347 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3348 * and process it as specified in RFC 7748 &sect;5.
3349 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3350 * and process it as specified in RFC 7748 &sect;5.
3351 *
3352 * - For key types for which the key is represented by a single sequence of
3353 * \p bits bits with constraints as to which bit sequences are acceptable,
3354 * this function draws a byte string of length (\p bits / 8) bytes rounded
3355 * up to the nearest whole number of bytes. If the resulting byte string
3356 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3357 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003358 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003359 * for the output produced by psa_export_key().
3360 * The following key types defined in this specification follow this scheme:
3361 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003362 * - #PSA_KEY_TYPE_DES.
3363 * Force-set the parity bits, but discard forbidden weak keys.
3364 * For 2-key and 3-key triple-DES, the three keys are generated
3365 * successively (for example, for 3-key triple-DES,
3366 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3367 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003368 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003369 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003370 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003371 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003372 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003373 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003374 * Weierstrass curve).
3375 * For these key types, interpret the byte string as integer
3376 * in big-endian order. Discard it if it is not in the range
3377 * [0, *N* - 2] where *N* is the boundary of the private key domain
3378 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003379 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003380 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003381 * This method allows compliance to NIST standards, specifically
3382 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003383 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3384 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3385 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3386 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003387 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003388 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003389 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003390 * implementation-defined.
3391 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003392 * In all cases, the data that is read is discarded from the operation.
3393 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003394 *
Gilles Peskine20628592019-04-19 19:29:50 +02003395 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003396 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003397 * \param[out] handle On success, a handle to the newly created key.
3398 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003399 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003400 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003401 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003402 * If the key is persistent, the key material and the key's metadata
3403 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003404 * \retval #PSA_ERROR_ALREADY_EXISTS
3405 * This is an attempt to create a persistent key, and there is
3406 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003407 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003408 * There was not enough data to create the desired key.
3409 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003410 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003411 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003412 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003413 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003414 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003415 * \retval #PSA_ERROR_INVALID_ARGUMENT
3416 * The provided key attributes are not valid for the operation.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003417 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003418 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3419 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3420 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3421 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003422 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003423 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003424 * The library has not been previously initialized by psa_crypto_init().
3425 * It is implementation-dependent whether a failure to initialize
3426 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003427 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003428psa_status_t psa_key_derivation_output_key(
3429 const psa_key_attributes_t *attributes,
3430 psa_key_derivation_operation_t *operation,
3431 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003432
Gilles Peskine35675b62019-05-16 17:26:11 +02003433/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003434 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003435 * Once a key derivation operation has been aborted, its capacity is zero.
3436 * Aborting an operation frees all associated resources except for the
3437 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003438 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003439 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003440 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003441 * psa_key_derivation_operation_init() or a zero value. In particular,
3442 * it is valid to call psa_key_derivation_abort() twice, or to call
3443 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003444 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003445 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003446 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003447 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003448 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003449 * \retval #PSA_SUCCESS
3450 * \retval #PSA_ERROR_BAD_STATE
3451 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3452 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003453 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003454 * \retval #PSA_ERROR_BAD_STATE
3455 * The library has not been previously initialized by psa_crypto_init().
3456 * It is implementation-dependent whether a failure to initialize
3457 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003458 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003459psa_status_t psa_key_derivation_abort(
3460 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003461
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003462/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003463 *
3464 * \warning The raw result of a key agreement algorithm such as finite-field
3465 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3466 * not be used directly as key material. It should instead be passed as
3467 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003468 * a key derivation, use psa_key_derivation_key_agreement() and other
3469 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003470 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003471 * \param alg The key agreement algorithm to compute
3472 * (\c PSA_ALG_XXX value such that
3473 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3474 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003475 * \param private_key Handle to the private key to use.
3476 * \param[in] peer_key Public key of the peer. It must be
3477 * in the same format that psa_import_key()
3478 * accepts. The standard formats for public
3479 * keys are documented in the documentation
3480 * of psa_export_public_key().
3481 * \param peer_key_length Size of \p peer_key in bytes.
3482 * \param[out] output Buffer where the decrypted message is to
3483 * be written.
3484 * \param output_size Size of the \c output buffer in bytes.
3485 * \param[out] output_length On success, the number of bytes
3486 * that make up the returned output.
3487 *
3488 * \retval #PSA_SUCCESS
3489 * Success.
3490 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003491 * \retval #PSA_ERROR_NOT_PERMITTED
3492 * \retval #PSA_ERROR_INVALID_ARGUMENT
3493 * \p alg is not a key agreement algorithm
3494 * \retval #PSA_ERROR_INVALID_ARGUMENT
3495 * \p private_key is not compatible with \p alg,
3496 * or \p peer_key is not valid for \p alg or not compatible with
3497 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003498 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3499 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003500 * \retval #PSA_ERROR_NOT_SUPPORTED
3501 * \p alg is not a supported key agreement algorithm.
3502 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3503 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3504 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003505 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003506 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003507 * \retval #PSA_ERROR_BAD_STATE
3508 * The library has not been previously initialized by psa_crypto_init().
3509 * It is implementation-dependent whether a failure to initialize
3510 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003511 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003512psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3513 psa_key_handle_t private_key,
3514 const uint8_t *peer_key,
3515 size_t peer_key_length,
3516 uint8_t *output,
3517 size_t output_size,
3518 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003519
Gilles Peskineea0fb492018-07-12 17:17:20 +02003520/**@}*/
3521
Gilles Peskineedd76872018-07-20 17:42:05 +02003522/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003523 * @{
3524 */
3525
3526/**
3527 * \brief Generate random bytes.
3528 *
3529 * \warning This function **can** fail! Callers MUST check the return status
3530 * and MUST NOT use the content of the output buffer if the return
3531 * status is not #PSA_SUCCESS.
3532 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003533 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003534 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003535 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003536 * \param output_size Number of bytes to generate and output.
3537 *
Gilles Peskine28538492018-07-11 17:34:00 +02003538 * \retval #PSA_SUCCESS
3539 * \retval #PSA_ERROR_NOT_SUPPORTED
3540 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003541 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003542 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3543 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003544 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003545 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003546 * The library has not been previously initialized by psa_crypto_init().
3547 * It is implementation-dependent whether a failure to initialize
3548 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003549 */
3550psa_status_t psa_generate_random(uint8_t *output,
3551 size_t output_size);
3552
3553/**
3554 * \brief Generate a key or key pair.
3555 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003556 * The key is generated randomly.
3557 * Its location, policy, type and size are taken from \p attributes.
3558 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003559 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003560 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003561 * the public exponent is 65537.
3562 * The modulus is a product of two probabilistic primes
3563 * between 2^{n-1} and 2^n where n is the bit size specified in the
3564 * attributes.
3565 *
Gilles Peskine20628592019-04-19 19:29:50 +02003566 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003567 * \param[out] handle On success, a handle to the newly created key.
3568 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003569 *
Gilles Peskine28538492018-07-11 17:34:00 +02003570 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003571 * Success.
3572 * If the key is persistent, the key material and the key's metadata
3573 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003574 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003575 * This is an attempt to create a persistent key, and there is
3576 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003577 * \retval #PSA_ERROR_NOT_SUPPORTED
3578 * \retval #PSA_ERROR_INVALID_ARGUMENT
3579 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3580 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3581 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3582 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003583 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003584 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3585 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003586 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003587 * The library has not been previously initialized by psa_crypto_init().
3588 * It is implementation-dependent whether a failure to initialize
3589 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003590 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003591psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003592 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003593
3594/**@}*/
3595
Gilles Peskinee59236f2018-01-27 23:32:46 +01003596#ifdef __cplusplus
3597}
3598#endif
3599
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003600/* The file "crypto_sizes.h" contains definitions for size calculation
3601 * macros whose definitions are implementation-specific. */
3602#include "crypto_sizes.h"
3603
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003604/* The file "crypto_struct.h" contains definitions for
3605 * implementation-specific structs that are declared above. */
3606#include "crypto_struct.h"
3607
3608/* The file "crypto_extra.h" contains vendor-specific definitions. This
3609 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003610#include "crypto_extra.h"
3611
3612#endif /* PSA_CRYPTO_H */