blob: 1517f0edd4fc616bf040b3e34c51b127769e3e8a [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 *
Andrew Thoelkede183412019-09-05 09:38:06 +0100370 * The application must eventually close the handle with psa_close_key() or
371 * psa_destroy_key() to release associated resources. If the application dies
372 * without calling one of these functions, the implementation should perform
373 * the equivalent of a 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
Andrew Thoelkede183412019-09-05 09:38:06 +0100376 * times. If this is successful, each call to psa_open_key() will return a
377 * different key handle.
378 *
379 * \note Applications that rely on opening a key multiple times will not be
380 * portable to implementations that only permit a single key handle to be
381 * opened. See also :ref:\`key-handles\`.
Andrew Thoelke9741b112019-08-21 18:20:41 +0100382 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100383 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100384 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100385 *
386 * \retval #PSA_SUCCESS
387 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100388 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100389 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Andrew Thoelke9741b112019-08-21 18:20:41 +0100390 * The implementation does not have sufficient resources to open the
391 * key. This can be due to reaching an implementation limit on the
392 * number of open keys, the number of open key handles, or available
393 * memory.
David Saadab4ecc272019-02-14 13:48:10 +0200394 * \retval #PSA_ERROR_DOES_NOT_EXIST
Andrew Thoelke9741b112019-08-21 18:20:41 +0100395 * There is no persistent key with key identifier \p id.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100396 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrew Thoelke9741b112019-08-21 18:20:41 +0100397 * \p id is not a valid persistent key identifier.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100398 * \retval #PSA_ERROR_NOT_PERMITTED
399 * The specified key exists, but the application does not have the
400 * permission to access it. Note that this specification does not
401 * define any way to create such a key, but it may be possible
402 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200403 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawd789dc12019-08-12 15:06:48 +0100404 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine225010f2019-05-06 18:44:55 +0200405 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100406 * \retval #PSA_ERROR_BAD_STATE
407 * The library has not been previously initialized by psa_crypto_init().
408 * It is implementation-dependent whether a failure to initialize
409 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100410 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200411psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100412 psa_key_handle_t *handle);
413
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100414
Gilles Peskinef535eb22018-11-30 14:08:36 +0100415/** Close a key handle.
416 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100417 * If the handle designates a volatile key, this will destroy the key material
418 * and free all associated resources, just like psa_destroy_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100419 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100420 * If this is the last open handle to a persistent key, then closing the handle
421 * will free all resources associated with the key in volatile memory. The key
422 * data in persistent storage is not affected and can be opened again later
423 * with a call to psa_open_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100424 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100425 * Closing the key handle makes the handle invalid, and the key handle
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100426 * must not be used again by the application.
Andrew Thoelke3daba812019-08-21 22:46:56 +0100427 *
Andrew Thoelke970629f2019-09-09 09:56:34 +0100428 * \note If the key handle was used to set up an active
Andrew Thoelkede183412019-09-05 09:38:06 +0100429 * :ref:\`multipart operation <multipart-operations>\`, then closing the
430 * key handle can cause the multipart operation to fail. Applications should
431 * maintain the key handle until after the multipart operation has finished.
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100432 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100433 * \param handle The key handle to close.
434 *
435 * \retval #PSA_SUCCESS
436 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100437 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawf97c8522019-08-15 13:27:12 +0100438 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100439 * \retval #PSA_ERROR_BAD_STATE
440 * The library has not been previously initialized by psa_crypto_init().
441 * It is implementation-dependent whether a failure to initialize
442 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100443 */
444psa_status_t psa_close_key(psa_key_handle_t handle);
445
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100446/** Make a copy of a key.
447 *
448 * Copy key material from one location to another.
449 *
450 * This function is primarily useful to copy a key from one location
451 * to another, since it populates a key using the material from
452 * another key which may have a different lifetime.
453 *
454 * This function may be used to share a key with a different party,
455 * subject to implementation-defined restrictions on key sharing.
456 *
457 * The policy on the source key must have the usage flag
458 * #PSA_KEY_USAGE_COPY set.
459 * This flag is sufficient to permit the copy if the key has the lifetime
460 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
461 * Some secure elements do not provide a way to copy a key without
462 * making it extractable from the secure element. If a key is located
463 * in such a secure element, then the key must have both usage flags
464 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
465 * a copy of the key outside the secure element.
466 *
467 * The resulting key may only be used in a way that conforms to
468 * both the policy of the original key and the policy specified in
469 * the \p attributes parameter:
470 * - The usage flags on the resulting key are the bitwise-and of the
471 * usage flags on the source policy and the usage flags in \p attributes.
472 * - If both allow the same algorithm or wildcard-based
473 * algorithm policy, the resulting key has the same algorithm policy.
474 * - If either of the policies allows an algorithm and the other policy
475 * allows a wildcard-based algorithm policy that includes this algorithm,
476 * the resulting key allows the same algorithm.
477 * - If the policies do not allow any algorithm in common, this function
478 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
479 *
480 * The effect of this function on implementation-defined attributes is
481 * implementation-defined.
482 *
483 * \param source_handle The key to copy. It must be a valid key handle.
484 * \param[in] attributes The attributes for the new key.
485 * They are used as follows:
486 * - The key type and size may be 0. If either is
487 * nonzero, it must match the corresponding
488 * attribute of the source key.
489 * - The key location (the lifetime and, for
490 * persistent keys, the key identifier) is
491 * used directly.
492 * - The policy constraints (usage flags and
493 * algorithm policy) are combined from
494 * the source key and \p attributes so that
495 * both sets of restrictions apply, as
496 * described in the documentation of this function.
497 * \param[out] target_handle On success, a handle to the newly created key.
498 * \c 0 on failure.
499 *
500 * \retval #PSA_SUCCESS
501 * \retval #PSA_ERROR_INVALID_HANDLE
502 * \p source_handle is invalid.
503 * \retval #PSA_ERROR_ALREADY_EXISTS
504 * This is an attempt to create a persistent key, and there is
505 * already a persistent key with the given identifier.
506 * \retval #PSA_ERROR_INVALID_ARGUMENT
507 * The lifetime or identifier in \p attributes are invalid.
508 * \retval #PSA_ERROR_INVALID_ARGUMENT
509 * The policy constraints on the source and specified in
510 * \p attributes are incompatible.
511 * \retval #PSA_ERROR_INVALID_ARGUMENT
512 * \p attributes specifies a key type or key size
513 * which does not match the attributes of the source key.
514 * \retval #PSA_ERROR_NOT_PERMITTED
515 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
516 * \retval #PSA_ERROR_NOT_PERMITTED
517 * The source key is not exportable and its lifetime does not
518 * allow copying it to the target's lifetime.
519 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
520 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
521 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
522 * \retval #PSA_ERROR_HARDWARE_FAILURE
523 * \retval #PSA_ERROR_STORAGE_FAILURE
524 * \retval #PSA_ERROR_CORRUPTION_DETECTED
525 * \retval #PSA_ERROR_BAD_STATE
526 * The library has not been previously initialized by psa_crypto_init().
527 * It is implementation-dependent whether a failure to initialize
528 * results in this error code.
529 */
530psa_status_t psa_copy_key(psa_key_handle_t source_handle,
531 const psa_key_attributes_t *attributes,
532 psa_key_handle_t *target_handle);
533
534
535/**
536 * \brief Destroy a key.
537 *
538 * This function destroys a key from both volatile
539 * memory and, if applicable, non-volatile storage. Implementations shall
540 * make a best effort to ensure that that the key material cannot be recovered.
541 *
542 * This function also erases any metadata such as policies and frees
543 * resources associated with the key. To free all resources associated with
544 * the key, all handles to the key must be closed or destroyed.
545 *
546 * Destroying the key makes the handle invalid, and the key handle
547 * must not be used again by the application. Using other open handles to the
548 * destroyed key in a cryptographic operation will result in an error.
549 *
550 * If a key is currently in use in a multipart operation, then destroying the
551 * key will cause the multipart operation to fail.
552 *
553 * \param handle Handle to the key to erase.
554 *
555 * \retval #PSA_SUCCESS
556 * The key material has been erased.
557 * \retval #PSA_ERROR_NOT_PERMITTED
558 * The key cannot be erased because it is
559 * read-only, either due to a policy or due to physical restrictions.
560 * \retval #PSA_ERROR_INVALID_HANDLE
561 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
562 * There was an failure in communication with the cryptoprocessor.
563 * The key material may still be present in the cryptoprocessor.
564 * \retval #PSA_ERROR_STORAGE_FAILURE
565 * The storage is corrupted. Implementations shall make a best effort
566 * to erase key material even in this stage, however applications
567 * should be aware that it may be impossible to guarantee that the
568 * key material is not recoverable in such cases.
569 * \retval #PSA_ERROR_CORRUPTION_DETECTED
570 * An unexpected condition which is not a storage corruption or
571 * a communication failure occurred. The cryptoprocessor may have
572 * been compromised.
573 * \retval #PSA_ERROR_BAD_STATE
574 * The library has not been previously initialized by psa_crypto_init().
575 * It is implementation-dependent whether a failure to initialize
576 * results in this error code.
577 */
578psa_status_t psa_destroy_key(psa_key_handle_t handle);
579
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100580/**@}*/
581
582/** \defgroup import_export Key import and export
583 * @{
584 */
585
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100586/**
587 * \brief Import a key in binary format.
588 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100589 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100590 * documentation of psa_export_public_key() for the format of public keys
591 * and to the documentation of psa_export_key() for the format for
592 * other key types.
593 *
594 * This specification supports a single format for each key type.
595 * Implementations may support other formats as long as the standard
596 * format is supported. Implementations that support other formats
597 * should ensure that the formats are clearly unambiguous so as to
598 * minimize the risk that an invalid input is accidentally interpreted
599 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100600 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100601
Gilles Peskine20628592019-04-19 19:29:50 +0200602 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200603 * The key size is always determined from the
604 * \p data buffer.
605 * If the key size in \p attributes is nonzero,
606 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200607 * \param[out] handle On success, a handle to the newly created key.
608 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100609 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200610 * buffer is interpreted according to the type declared
611 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200612 * All implementations must support at least the format
613 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100614 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200615 * the chosen type. Implementations may allow other
616 * formats, but should be conservative: implementations
617 * should err on the side of rejecting content if it
618 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200619 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100620 *
Gilles Peskine28538492018-07-11 17:34:00 +0200621 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100622 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100623 * If the key is persistent, the key material and the key's metadata
624 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200625 * \retval #PSA_ERROR_ALREADY_EXISTS
626 * This is an attempt to create a persistent key, and there is
627 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200628 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200629 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200630 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200631 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200632 * The key attributes, as a whole, are invalid.
633 * \retval #PSA_ERROR_INVALID_ARGUMENT
634 * The key data is not correctly formatted.
635 * \retval #PSA_ERROR_INVALID_ARGUMENT
636 * The size in \p attributes is nonzero and does not match the size
637 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200638 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
639 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
640 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100641 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200642 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200643 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300644 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw97d3bc32019-08-08 14:40:04 +0100645 * \p operation is either not initialized or is in use
646 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300647 * The library has not been previously initialized by psa_crypto_init().
648 * It is implementation-dependent whether a failure to initialize
649 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100650 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200651psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100652 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200653 size_t data_length,
654 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100655
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100656
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100657
658/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100659 * \brief Export a key in binary format.
660 *
661 * The output of this function can be passed to psa_import_key() to
662 * create an equivalent object.
663 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100664 * If the implementation of psa_import_key() supports other formats
665 * beyond the format specified here, the output from psa_export_key()
666 * must use the representation specified here, not the original
667 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100668 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100669 * For standard key types, the output format is as follows:
670 *
671 * - For symmetric keys (including MAC keys), the format is the
672 * raw bytes of the key.
673 * - For DES, the key data consists of 8 bytes. The parity bits must be
674 * correct.
675 * - For Triple-DES, the format is the concatenation of the
676 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200677 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200678 * is the non-encrypted DER encoding of the representation defined by
679 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
680 * ```
681 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200682 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200683 * modulus INTEGER, -- n
684 * publicExponent INTEGER, -- e
685 * privateExponent INTEGER, -- d
686 * prime1 INTEGER, -- p
687 * prime2 INTEGER, -- q
688 * exponent1 INTEGER, -- d mod (p-1)
689 * exponent2 INTEGER, -- d mod (q-1)
690 * coefficient INTEGER, -- (inverse of q) mod p
691 * }
692 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200693 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200694 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100695 * a representation of the private value as a `ceiling(m/8)`-byte string
696 * where `m` is the bit size associated with the curve, i.e. the bit size
697 * of the order of the curve's coordinate field. This byte string is
698 * in little-endian order for Montgomery curves (curve types
699 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
700 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
701 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100702 * This is the content of the `privateKey` field of the `ECPrivateKey`
703 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200704 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200705 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000706 * format is the representation of the private key `x` as a big-endian byte
707 * string. The length of the byte string is the private key size in bytes
708 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200709 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
710 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100711 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200712 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
713 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100714 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200715 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200716 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200717 * \param[out] data_length On success, the number of bytes
718 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100719 *
Gilles Peskine28538492018-07-11 17:34:00 +0200720 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100721 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200722 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200723 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100724 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200725 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
726 * The size of the \p data buffer is too small. You can determine a
727 * sufficient buffer size by calling
728 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
729 * where \c type is the key type
730 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200731 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
732 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200733 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100734 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100735 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300736 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300737 * The library has not been previously initialized by psa_crypto_init().
738 * It is implementation-dependent whether a failure to initialize
739 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100741psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100742 uint8_t *data,
743 size_t data_size,
744 size_t *data_length);
745
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100746/**
747 * \brief Export a public key or the public part of a key pair in binary format.
748 *
749 * The output of this function can be passed to psa_import_key() to
750 * create an object that is equivalent to the public key.
751 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000752 * This specification supports a single format for each key type.
753 * Implementations may support other formats as long as the standard
754 * format is supported. Implementations that support other formats
755 * should ensure that the formats are clearly unambiguous so as to
756 * minimize the risk that an invalid input is accidentally interpreted
757 * according to a different format.
758 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000759 * For standard key types, the output format is as follows:
760 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
761 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
762 * ```
763 * RSAPublicKey ::= SEQUENCE {
764 * modulus INTEGER, -- n
765 * publicExponent INTEGER } -- e
766 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000767 * - For elliptic curve public keys (key types for which
768 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
769 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
770 * Let `m` be the bit size associated with the curve, i.e. the bit size of
771 * `q` for a curve over `F_q`. The representation consists of:
772 * - The byte 0x04;
773 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
774 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200775 * - For Diffie-Hellman key exchange public keys (key types for which
776 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000777 * the format is the representation of the public key `y = g^x mod p` as a
778 * big-endian byte string. The length of the byte string is the length of the
779 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100780 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200781 * Exporting a public key object or the public part of a key pair is
782 * always permitted, regardless of the key's usage flags.
783 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100784 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200785 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200786 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200787 * \param[out] data_length On success, the number of bytes
788 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100789 *
Gilles Peskine28538492018-07-11 17:34:00 +0200790 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100791 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200792 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200793 * The key is neither a public key nor a key pair.
794 * \retval #PSA_ERROR_NOT_SUPPORTED
795 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
796 * The size of the \p data buffer is too small. You can determine a
797 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200798 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200799 * where \c type is the key type
800 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200801 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
802 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200803 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100804 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100805 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300806 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300807 * The library has not been previously initialized by psa_crypto_init().
808 * It is implementation-dependent whether a failure to initialize
809 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100810 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100811psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100812 uint8_t *data,
813 size_t data_size,
814 size_t *data_length);
815
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100816
Gilles Peskine20035e32018-02-03 22:44:14 +0100817
818/**@}*/
819
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100820/** \defgroup hash Message digests
821 * @{
822 */
823
Gilles Peskine69647a42019-01-14 20:18:12 +0100824/** Calculate the hash (digest) of a message.
825 *
826 * \note To verify the hash of a message against an
827 * expected value, use psa_hash_compare() instead.
828 *
829 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
830 * such that #PSA_ALG_IS_HASH(\p alg) is true).
831 * \param[in] input Buffer containing the message to hash.
832 * \param input_length Size of the \p input buffer in bytes.
833 * \param[out] hash Buffer where the hash is to be written.
834 * \param hash_size Size of the \p hash buffer in bytes.
835 * \param[out] hash_length On success, the number of bytes
836 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100837 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100838 *
839 * \retval #PSA_SUCCESS
840 * Success.
841 * \retval #PSA_ERROR_NOT_SUPPORTED
842 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100843 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100844 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
845 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100846 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
847 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
848 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200849 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100850 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100851 * \retval #PSA_ERROR_BAD_STATE
852 * The library has not been previously initialized by psa_crypto_init().
853 * It is implementation-dependent whether a failure to initialize
854 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100855 */
856psa_status_t psa_hash_compute(psa_algorithm_t alg,
857 const uint8_t *input,
858 size_t input_length,
859 uint8_t *hash,
860 size_t hash_size,
861 size_t *hash_length);
862
863/** Calculate the hash (digest) of a message and compare it with a
864 * reference value.
865 *
866 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
867 * such that #PSA_ALG_IS_HASH(\p alg) is true).
868 * \param[in] input Buffer containing the message to hash.
869 * \param input_length Size of the \p input buffer in bytes.
870 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100871 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100872 *
873 * \retval #PSA_SUCCESS
874 * The expected hash is identical to the actual hash of the input.
875 * \retval #PSA_ERROR_INVALID_SIGNATURE
876 * The hash of the message was calculated successfully, but it
877 * differs from the expected hash.
878 * \retval #PSA_ERROR_NOT_SUPPORTED
879 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100880 * \retval #PSA_ERROR_INVALID_ARGUMENT
881 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100882 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
883 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
884 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200885 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100886 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100887 * \retval #PSA_ERROR_BAD_STATE
888 * The library has not been previously initialized by psa_crypto_init().
889 * It is implementation-dependent whether a failure to initialize
890 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100891 */
892psa_status_t psa_hash_compare(psa_algorithm_t alg,
893 const uint8_t *input,
894 size_t input_length,
895 const uint8_t *hash,
896 const size_t hash_length);
897
Gilles Peskine308b91d2018-02-08 09:47:44 +0100898/** The type of the state data structure for multipart hash operations.
899 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000900 * Before calling any function on a hash operation object, the application must
901 * initialize it by any of the following means:
902 * - Set the structure to all-bits-zero, for example:
903 * \code
904 * psa_hash_operation_t operation;
905 * memset(&operation, 0, sizeof(operation));
906 * \endcode
907 * - Initialize the structure to logical zero values, for example:
908 * \code
909 * psa_hash_operation_t operation = {0};
910 * \endcode
911 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
912 * for example:
913 * \code
914 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
915 * \endcode
916 * - Assign the result of the function psa_hash_operation_init()
917 * to the structure, for example:
918 * \code
919 * psa_hash_operation_t operation;
920 * operation = psa_hash_operation_init();
921 * \endcode
922 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100923 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100924 * make any assumptions about the content of this structure except
925 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100926typedef struct psa_hash_operation_s psa_hash_operation_t;
927
Jaeden Amero6a25b412019-01-04 11:47:44 +0000928/** \def PSA_HASH_OPERATION_INIT
929 *
930 * This macro returns a suitable initializer for a hash operation object
931 * of type #psa_hash_operation_t.
932 */
933#ifdef __DOXYGEN_ONLY__
934/* This is an example definition for documentation purposes.
935 * Implementations should define a suitable value in `crypto_struct.h`.
936 */
937#define PSA_HASH_OPERATION_INIT {0}
938#endif
939
940/** Return an initial value for a hash operation object.
941 */
942static psa_hash_operation_t psa_hash_operation_init(void);
943
Gilles Peskinef45adda2019-01-14 18:29:18 +0100944/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100945 *
946 * The sequence of operations to calculate a hash (message digest)
947 * is as follows:
948 * -# Allocate an operation object which will be passed to all the functions
949 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000950 * -# Initialize the operation object with one of the methods described in the
951 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200952 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100953 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100954 * of the message each time. The hash that is calculated is the hash
955 * of the concatenation of these messages in order.
956 * -# To calculate the hash, call psa_hash_finish().
957 * To compare the hash with an expected value, call psa_hash_verify().
958 *
959 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000960 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100961 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200962 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100963 * eventually terminate the operation. The following events terminate an
964 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100965 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100966 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100967 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000968 * \param[in,out] operation The operation object to set up. It must have
969 * been initialized as per the documentation for
970 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200971 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
972 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100973 *
Gilles Peskine28538492018-07-11 17:34:00 +0200974 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100975 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200976 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100977 * \p alg is not a supported hash algorithm.
978 * \retval #PSA_ERROR_INVALID_ARGUMENT
979 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100980 * \retval #PSA_ERROR_BAD_STATE
981 * The operation state is not valid (already set up and not
982 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200983 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
984 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
985 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200986 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100987 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawe970d652019-08-08 14:40:04 +0100988 * \p operation is either not initialized or is in use
989 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100990 * The library has not been previously initialized by psa_crypto_init().
991 * It is implementation-dependent whether a failure to initialize
992 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100993 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200994psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100995 psa_algorithm_t alg);
996
Gilles Peskine308b91d2018-02-08 09:47:44 +0100997/** Add a message fragment to a multipart hash operation.
998 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200999 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001000 *
1001 * If this function returns an error status, the operation becomes inactive.
1002 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001003 * \param[in,out] operation Active hash operation.
1004 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001005 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001006 *
Gilles Peskine28538492018-07-11 17:34:00 +02001007 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001008 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001009 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001010 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001011 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1012 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1013 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001014 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001015 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001016 * The operation state is not valid.
1017 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001018 * The library has not been previously initialized by psa_crypto_init().
1019 * It is implementation-dependent whether a failure to initialize
1020 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001021 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001022psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1023 const uint8_t *input,
1024 size_t input_length);
1025
Gilles Peskine308b91d2018-02-08 09:47:44 +01001026/** Finish the calculation of the hash of a message.
1027 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001028 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001029 * This function calculates the hash of the message formed by concatenating
1030 * the inputs passed to preceding calls to psa_hash_update().
1031 *
1032 * When this function returns, the operation becomes inactive.
1033 *
1034 * \warning Applications should not call this function if they expect
1035 * a specific value for the hash. Call psa_hash_verify() instead.
1036 * Beware that comparing integrity or authenticity data such as
1037 * hash values with a function such as \c memcmp is risky
1038 * because the time taken by the comparison may leak information
1039 * about the hashed data which could allow an attacker to guess
1040 * a valid hash and thereby bypass security controls.
1041 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001042 * \param[in,out] operation Active hash operation.
1043 * \param[out] hash Buffer where the hash is to be written.
1044 * \param hash_size Size of the \p hash buffer in bytes.
1045 * \param[out] hash_length On success, the number of bytes
1046 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001047 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001048 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001049 *
Gilles Peskine28538492018-07-11 17:34:00 +02001050 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001051 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001052 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001053 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001054 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001055 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001056 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001057 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001058 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1059 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1060 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001061 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001062 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001063 * The operation state is not valid.
1064 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001065 * The library has not been previously initialized by psa_crypto_init().
1066 * It is implementation-dependent whether a failure to initialize
1067 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001068 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001069psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1070 uint8_t *hash,
1071 size_t hash_size,
1072 size_t *hash_length);
1073
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074/** Finish the calculation of the hash of a message and compare it with
1075 * an expected value.
1076 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001077 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001078 * This function calculates the hash of the message formed by concatenating
1079 * the inputs passed to preceding calls to psa_hash_update(). It then
1080 * compares the calculated hash with the expected hash passed as a
1081 * parameter to this function.
1082 *
1083 * When this function returns, the operation becomes inactive.
1084 *
Gilles Peskine19067982018-03-20 17:54:53 +01001085 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001086 * comparison between the actual hash and the expected hash is performed
1087 * in constant time.
1088 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001089 * \param[in,out] operation Active hash operation.
1090 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001091 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001092 *
Gilles Peskine28538492018-07-11 17:34:00 +02001093 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001094 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001095 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001096 * The hash of the message was calculated successfully, but it
1097 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001098 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001099 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001100 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1101 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1102 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001103 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001104 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001105 * The operation state is not valid.
1106 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001107 * The library has not been previously initialized by psa_crypto_init().
1108 * It is implementation-dependent whether a failure to initialize
1109 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001110 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001111psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1112 const uint8_t *hash,
1113 size_t hash_length);
1114
Gilles Peskine308b91d2018-02-08 09:47:44 +01001115/** Abort a hash operation.
1116 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001117 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001118 * \p operation structure itself. Once aborted, the operation object
1119 * can be reused for another operation by calling
1120 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001121 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001122 * You may call this function any time after the operation object has
1123 * been initialized by any of the following methods:
1124 * - A call to psa_hash_setup(), whether it succeeds or not.
1125 * - Initializing the \c struct to all-bits-zero.
1126 * - Initializing the \c struct to logical zeros, e.g.
1127 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001128 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001129 * In particular, calling psa_hash_abort() after the operation has been
1130 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1131 * psa_hash_verify() is safe and has no effect.
1132 *
1133 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001134 *
Gilles Peskine28538492018-07-11 17:34:00 +02001135 * \retval #PSA_SUCCESS
1136 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001137 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001138 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1139 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001140 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001141 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001142 * The operation state is not valid.
1143 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001144 * The library has not been previously initialized by psa_crypto_init().
1145 * It is implementation-dependent whether a failure to initialize
1146 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001147 */
1148psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001149
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001150/** Clone a hash operation.
1151 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001152 * This function copies the state of an ongoing hash operation to
1153 * a new operation object. In other words, this function is equivalent
1154 * to calling psa_hash_setup() on \p target_operation with the same
1155 * algorithm that \p source_operation was set up for, then
1156 * psa_hash_update() on \p target_operation with the same input that
1157 * that was passed to \p source_operation. After this function returns, the
1158 * two objects are independent, i.e. subsequent calls involving one of
1159 * the objects do not affect the other object.
1160 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001161 * \param[in] source_operation The active hash operation to clone.
1162 * \param[in,out] target_operation The operation object to set up.
1163 * It must be initialized but not active.
1164 *
1165 * \retval #PSA_SUCCESS
1166 * \retval #PSA_ERROR_BAD_STATE
1167 * \p source_operation is not an active hash operation.
1168 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001169 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001170 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1171 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001172 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001173 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001174 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001175 * The operation state is either not initialized or has already been setup.
1176 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw8f7cd1e2019-08-08 14:49:01 +01001177 * The operation state is either not initialized or has already been setup.
1178 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001179 * The library has not been previously initialized by psa_crypto_init().
1180 * It is implementation-dependent whether a failure to initialize
1181 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001182 */
1183psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1184 psa_hash_operation_t *target_operation);
1185
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001186/**@}*/
1187
Gilles Peskine8c9def32018-02-08 10:02:12 +01001188/** \defgroup MAC Message authentication codes
1189 * @{
1190 */
1191
Gilles Peskine69647a42019-01-14 20:18:12 +01001192/** Calculate the MAC (message authentication code) of a message.
1193 *
1194 * \note To verify the MAC of a message against an
1195 * expected value, use psa_mac_verify() instead.
1196 * Beware that comparing integrity or authenticity data such as
1197 * MAC values with a function such as \c memcmp is risky
1198 * because the time taken by the comparison may leak information
1199 * about the MAC value which could allow an attacker to guess
1200 * a valid MAC and thereby bypass security controls.
1201 *
1202 * \param handle Handle to the key to use for the operation.
1203 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001204 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001205 * \param[in] input Buffer containing the input message.
1206 * \param input_length Size of the \p input buffer in bytes.
1207 * \param[out] mac Buffer where the MAC value is to be written.
1208 * \param mac_size Size of the \p mac buffer in bytes.
1209 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001210 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001211 *
1212 * \retval #PSA_SUCCESS
1213 * Success.
1214 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001215 * \retval #PSA_ERROR_NOT_PERMITTED
1216 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001217 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001218 * \retval #PSA_ERROR_NOT_SUPPORTED
1219 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001220 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1221 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001222 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1223 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1224 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001225 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001226 * \retval #PSA_ERROR_STORAGE_FAILURE
1227 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001228 * \retval #PSA_ERROR_BAD_STATE
1229 * The library has not been previously initialized by psa_crypto_init().
1230 * It is implementation-dependent whether a failure to initialize
1231 * results in this error code.
1232 */
1233psa_status_t psa_mac_compute(psa_key_handle_t handle,
1234 psa_algorithm_t alg,
1235 const uint8_t *input,
1236 size_t input_length,
1237 uint8_t *mac,
1238 size_t mac_size,
1239 size_t *mac_length);
1240
1241/** Calculate the MAC of a message and compare it with a reference value.
1242 *
1243 * \param handle Handle to the key to use for the operation.
1244 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001245 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001246 * \param[in] input Buffer containing the input message.
1247 * \param input_length Size of the \p input buffer in bytes.
1248 * \param[out] mac Buffer containing the expected MAC value.
1249 * \param mac_length Size of the \p mac buffer in bytes.
1250 *
1251 * \retval #PSA_SUCCESS
1252 * The expected MAC is identical to the actual MAC of the input.
1253 * \retval #PSA_ERROR_INVALID_SIGNATURE
1254 * The MAC of the message was calculated successfully, but it
1255 * differs from the expected value.
1256 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001257 * \retval #PSA_ERROR_NOT_PERMITTED
1258 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001259 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001260 * \retval #PSA_ERROR_NOT_SUPPORTED
1261 * \p alg is not supported or is not a MAC algorithm.
1262 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1263 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1264 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001265 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001266 * \retval #PSA_ERROR_STORAGE_FAILURE
1267 * The key could not be retrieved from storage.
1268 * \retval #PSA_ERROR_BAD_STATE
1269 * The library has not been previously initialized by psa_crypto_init().
1270 * It is implementation-dependent whether a failure to initialize
1271 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001272 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001273psa_status_t psa_mac_verify(psa_key_handle_t handle,
1274 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001275 const uint8_t *input,
1276 size_t input_length,
1277 const uint8_t *mac,
1278 const size_t mac_length);
1279
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001280/** The type of the state data structure for multipart MAC operations.
1281 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001282 * Before calling any function on a MAC operation object, the application must
1283 * initialize it by any of the following means:
1284 * - Set the structure to all-bits-zero, for example:
1285 * \code
1286 * psa_mac_operation_t operation;
1287 * memset(&operation, 0, sizeof(operation));
1288 * \endcode
1289 * - Initialize the structure to logical zero values, for example:
1290 * \code
1291 * psa_mac_operation_t operation = {0};
1292 * \endcode
1293 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1294 * for example:
1295 * \code
1296 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1297 * \endcode
1298 * - Assign the result of the function psa_mac_operation_init()
1299 * to the structure, for example:
1300 * \code
1301 * psa_mac_operation_t operation;
1302 * operation = psa_mac_operation_init();
1303 * \endcode
1304 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001305 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001306 * make any assumptions about the content of this structure except
1307 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001308typedef struct psa_mac_operation_s psa_mac_operation_t;
1309
Jaeden Amero769ce272019-01-04 11:48:03 +00001310/** \def PSA_MAC_OPERATION_INIT
1311 *
1312 * This macro returns a suitable initializer for a MAC operation object of type
1313 * #psa_mac_operation_t.
1314 */
1315#ifdef __DOXYGEN_ONLY__
1316/* This is an example definition for documentation purposes.
1317 * Implementations should define a suitable value in `crypto_struct.h`.
1318 */
1319#define PSA_MAC_OPERATION_INIT {0}
1320#endif
1321
1322/** Return an initial value for a MAC operation object.
1323 */
1324static psa_mac_operation_t psa_mac_operation_init(void);
1325
Gilles Peskinef45adda2019-01-14 18:29:18 +01001326/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001327 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001328 * This function sets up the calculation of the MAC
1329 * (message authentication code) of a byte string.
1330 * To verify the MAC of a message against an
1331 * expected value, use psa_mac_verify_setup() instead.
1332 *
1333 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001334 * -# Allocate an operation object which will be passed to all the functions
1335 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001336 * -# Initialize the operation object with one of the methods described in the
1337 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001338 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001339 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1340 * of the message each time. The MAC that is calculated is the MAC
1341 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001342 * -# At the end of the message, call psa_mac_sign_finish() to finish
1343 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001344 *
1345 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001346 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001347 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001348 * After a successful call to psa_mac_sign_setup(), the application must
1349 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001350 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001351 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001352 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001353 * \param[in,out] operation The operation object to set up. It must have
1354 * been initialized as per the documentation for
1355 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001356 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001357 * It must remain valid until the operation
1358 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001359 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001360 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001361 *
Gilles Peskine28538492018-07-11 17:34:00 +02001362 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001363 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001364 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001365 * \retval #PSA_ERROR_NOT_PERMITTED
1366 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001367 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001368 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001369 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001370 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1371 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1372 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001373 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001374 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001375 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001376 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001377 * The operation state is not valid (already set up and not
1378 * subsequently completed).
1379 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001380 * The library has not been previously initialized by psa_crypto_init().
1381 * It is implementation-dependent whether a failure to initialize
1382 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001383 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001384psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001385 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001386 psa_algorithm_t alg);
1387
Gilles Peskinef45adda2019-01-14 18:29:18 +01001388/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001389 *
1390 * This function sets up the verification of the MAC
1391 * (message authentication code) of a byte string against an expected value.
1392 *
1393 * The sequence of operations to verify a MAC is as follows:
1394 * -# Allocate an operation object which will be passed to all the functions
1395 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001396 * -# Initialize the operation object with one of the methods described in the
1397 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001398 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001399 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1400 * of the message each time. The MAC that is calculated is the MAC
1401 * of the concatenation of these messages in order.
1402 * -# At the end of the message, call psa_mac_verify_finish() to finish
1403 * calculating the actual MAC of the message and verify it against
1404 * the expected value.
1405 *
1406 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001407 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001408 *
1409 * After a successful call to psa_mac_verify_setup(), the application must
1410 * eventually terminate the operation through one of the following methods:
1411 * - A failed call to psa_mac_update().
1412 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1413 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001414 * \param[in,out] operation The operation object to set up. It must have
1415 * been initialized as per the documentation for
1416 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001417 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001418 * It must remain valid until the operation
1419 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001420 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1421 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001422 *
Gilles Peskine28538492018-07-11 17:34:00 +02001423 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001424 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001425 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001426 * \retval #PSA_ERROR_NOT_PERMITTED
1427 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001428 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001429 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001430 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001431 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1432 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1433 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001434 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001435 * \retval #PSA_ERROR_STORAGE_FAILURE
1436 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001437 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001438 * The operation state is not valid (already set up and not
1439 * subsequently completed).
1440 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001441 * The library has not been previously initialized by psa_crypto_init().
1442 * It is implementation-dependent whether a failure to initialize
1443 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001444 */
1445psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001446 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001447 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001448
Gilles Peskinedcd14942018-07-12 00:30:52 +02001449/** Add a message fragment to a multipart MAC operation.
1450 *
1451 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1452 * before calling this function.
1453 *
1454 * If this function returns an error status, the operation becomes inactive.
1455 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001456 * \param[in,out] operation Active MAC operation.
1457 * \param[in] input Buffer containing the message fragment to add to
1458 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001459 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001460 *
1461 * \retval #PSA_SUCCESS
1462 * Success.
1463 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001464 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001465 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1466 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1467 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001468 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001469 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001470 * \retval #PSA_ERROR_BAD_STATE
1471 * The library has not been previously initialized by psa_crypto_init().
1472 * It is implementation-dependent whether a failure to initialize
1473 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001474 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001475psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1476 const uint8_t *input,
1477 size_t input_length);
1478
Gilles Peskinedcd14942018-07-12 00:30:52 +02001479/** Finish the calculation of the MAC of a message.
1480 *
1481 * The application must call psa_mac_sign_setup() before calling this function.
1482 * This function calculates the MAC of the message formed by concatenating
1483 * the inputs passed to preceding calls to psa_mac_update().
1484 *
1485 * When this function returns, the operation becomes inactive.
1486 *
1487 * \warning Applications should not call this function if they expect
1488 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1489 * Beware that comparing integrity or authenticity data such as
1490 * MAC values with a function such as \c memcmp is risky
1491 * because the time taken by the comparison may leak information
1492 * about the MAC value which could allow an attacker to guess
1493 * a valid MAC and thereby bypass security controls.
1494 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001495 * \param[in,out] operation Active MAC operation.
1496 * \param[out] mac Buffer where the MAC value is to be written.
1497 * \param mac_size Size of the \p mac buffer in bytes.
1498 * \param[out] mac_length On success, the number of bytes
1499 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001500 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001501 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001502 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001503 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001504 *
1505 * \retval #PSA_SUCCESS
1506 * Success.
1507 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001508 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001509 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001510 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001511 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1512 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1513 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1514 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001515 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001516 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001517 * \retval #PSA_ERROR_BAD_STATE
1518 * The library has not been previously initialized by psa_crypto_init().
1519 * It is implementation-dependent whether a failure to initialize
1520 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001521 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001522psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1523 uint8_t *mac,
1524 size_t mac_size,
1525 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001526
Gilles Peskinedcd14942018-07-12 00:30:52 +02001527/** Finish the calculation of the MAC of a message and compare it with
1528 * an expected value.
1529 *
1530 * The application must call psa_mac_verify_setup() before calling this function.
1531 * This function calculates the MAC of the message formed by concatenating
1532 * the inputs passed to preceding calls to psa_mac_update(). It then
1533 * compares the calculated MAC with the expected MAC passed as a
1534 * parameter to this function.
1535 *
1536 * When this function returns, the operation becomes inactive.
1537 *
1538 * \note Implementations shall make the best effort to ensure that the
1539 * comparison between the actual MAC and the expected MAC is performed
1540 * in constant time.
1541 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001542 * \param[in,out] operation Active MAC operation.
1543 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001544 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001545 *
1546 * \retval #PSA_SUCCESS
1547 * The expected MAC is identical to the actual MAC of the message.
1548 * \retval #PSA_ERROR_INVALID_SIGNATURE
1549 * The MAC of the message was calculated successfully, but it
1550 * differs from the expected MAC.
1551 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001552 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001553 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1554 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1555 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001556 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001557 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001558 * \retval #PSA_ERROR_BAD_STATE
1559 * The library has not been previously initialized by psa_crypto_init().
1560 * It is implementation-dependent whether a failure to initialize
1561 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001562 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001563psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1564 const uint8_t *mac,
1565 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001566
Gilles Peskinedcd14942018-07-12 00:30:52 +02001567/** Abort a MAC operation.
1568 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001569 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001570 * \p operation structure itself. Once aborted, the operation object
1571 * can be reused for another operation by calling
1572 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001573 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001574 * You may call this function any time after the operation object has
1575 * been initialized by any of the following methods:
1576 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1577 * it succeeds or not.
1578 * - Initializing the \c struct to all-bits-zero.
1579 * - Initializing the \c struct to logical zeros, e.g.
1580 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001581 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001582 * In particular, calling psa_mac_abort() after the operation has been
1583 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1584 * psa_mac_verify_finish() is safe and has no effect.
1585 *
1586 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001587 *
1588 * \retval #PSA_SUCCESS
1589 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001590 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001591 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1592 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001593 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001594 * \retval #PSA_ERROR_BAD_STATE
1595 * The library has not been previously initialized by psa_crypto_init().
1596 * It is implementation-dependent whether a failure to initialize
1597 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001598 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001599psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1600
1601/**@}*/
1602
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001603/** \defgroup cipher Symmetric ciphers
1604 * @{
1605 */
1606
Gilles Peskine69647a42019-01-14 20:18:12 +01001607/** Encrypt a message using a symmetric cipher.
1608 *
1609 * This function encrypts a message with a random IV (initialization
1610 * vector).
1611 *
1612 * \param handle Handle to the key to use for the operation.
1613 * It must remain valid until the operation
1614 * terminates.
1615 * \param alg The cipher algorithm to compute
1616 * (\c PSA_ALG_XXX value such that
1617 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1618 * \param[in] input Buffer containing the message to encrypt.
1619 * \param input_length Size of the \p input buffer in bytes.
1620 * \param[out] output Buffer where the output is to be written.
1621 * The output contains the IV followed by
1622 * the ciphertext proper.
1623 * \param output_size Size of the \p output buffer in bytes.
1624 * \param[out] output_length On success, the number of bytes
1625 * that make up the output.
1626 *
1627 * \retval #PSA_SUCCESS
1628 * Success.
1629 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001630 * \retval #PSA_ERROR_NOT_PERMITTED
1631 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001632 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001633 * \retval #PSA_ERROR_NOT_SUPPORTED
1634 * \p alg is not supported or is not a cipher algorithm.
1635 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1636 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1637 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1638 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001639 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001640 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001641 * \retval #PSA_ERROR_BAD_STATE
1642 * The library has not been previously initialized by psa_crypto_init().
1643 * It is implementation-dependent whether a failure to initialize
1644 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001645 */
1646psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1647 psa_algorithm_t alg,
1648 const uint8_t *input,
1649 size_t input_length,
1650 uint8_t *output,
1651 size_t output_size,
1652 size_t *output_length);
1653
1654/** Decrypt a message using a symmetric cipher.
1655 *
1656 * This function decrypts a message encrypted with a symmetric cipher.
1657 *
1658 * \param handle Handle to the key to use for the operation.
1659 * It must remain valid until the operation
1660 * terminates.
1661 * \param alg The cipher algorithm to compute
1662 * (\c PSA_ALG_XXX value such that
1663 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1664 * \param[in] input Buffer containing the message to decrypt.
1665 * This consists of the IV followed by the
1666 * ciphertext proper.
1667 * \param input_length Size of the \p input buffer in bytes.
1668 * \param[out] output Buffer where the plaintext is to be written.
1669 * \param output_size Size of the \p output buffer in bytes.
1670 * \param[out] output_length On success, the number of bytes
1671 * that make up the output.
1672 *
1673 * \retval #PSA_SUCCESS
1674 * Success.
1675 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001676 * \retval #PSA_ERROR_NOT_PERMITTED
1677 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001678 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001679 * \retval #PSA_ERROR_NOT_SUPPORTED
1680 * \p alg is not supported or is not a cipher algorithm.
1681 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1682 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1683 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1684 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001685 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001686 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001687 * \retval #PSA_ERROR_BAD_STATE
1688 * The library has not been previously initialized by psa_crypto_init().
1689 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001690 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001691 */
1692psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1693 psa_algorithm_t alg,
1694 const uint8_t *input,
1695 size_t input_length,
1696 uint8_t *output,
1697 size_t output_size,
1698 size_t *output_length);
1699
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001700/** The type of the state data structure for multipart cipher operations.
1701 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001702 * Before calling any function on a cipher operation object, the application
1703 * must initialize it by any of the following means:
1704 * - Set the structure to all-bits-zero, for example:
1705 * \code
1706 * psa_cipher_operation_t operation;
1707 * memset(&operation, 0, sizeof(operation));
1708 * \endcode
1709 * - Initialize the structure to logical zero values, for example:
1710 * \code
1711 * psa_cipher_operation_t operation = {0};
1712 * \endcode
1713 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1714 * for example:
1715 * \code
1716 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1717 * \endcode
1718 * - Assign the result of the function psa_cipher_operation_init()
1719 * to the structure, for example:
1720 * \code
1721 * psa_cipher_operation_t operation;
1722 * operation = psa_cipher_operation_init();
1723 * \endcode
1724 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001725 * This is an implementation-defined \c struct. Applications should not
1726 * make any assumptions about the content of this structure except
1727 * as directed by the documentation of a specific implementation. */
1728typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1729
Jaeden Amero5bae2272019-01-04 11:48:27 +00001730/** \def PSA_CIPHER_OPERATION_INIT
1731 *
1732 * This macro returns a suitable initializer for a cipher operation object of
1733 * type #psa_cipher_operation_t.
1734 */
1735#ifdef __DOXYGEN_ONLY__
1736/* This is an example definition for documentation purposes.
1737 * Implementations should define a suitable value in `crypto_struct.h`.
1738 */
1739#define PSA_CIPHER_OPERATION_INIT {0}
1740#endif
1741
1742/** Return an initial value for a cipher operation object.
1743 */
1744static psa_cipher_operation_t psa_cipher_operation_init(void);
1745
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001746/** Set the key for a multipart symmetric encryption operation.
1747 *
1748 * The sequence of operations to encrypt a message with a symmetric cipher
1749 * is as follows:
1750 * -# Allocate an operation object which will be passed to all the functions
1751 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001752 * -# Initialize the operation object with one of the methods described in the
1753 * documentation for #psa_cipher_operation_t, e.g.
1754 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001755 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001756 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001757 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001758 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001759 * requires a specific IV value.
1760 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1761 * of the message each time.
1762 * -# Call psa_cipher_finish().
1763 *
1764 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001765 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001766 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001767 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001768 * eventually terminate the operation. The following events terminate an
1769 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001770 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001771 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001772 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001773 * \param[in,out] operation The operation object to set up. It must have
1774 * been initialized as per the documentation for
1775 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001776 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001777 * It must remain valid until the operation
1778 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001779 * \param alg The cipher algorithm to compute
1780 * (\c PSA_ALG_XXX value such that
1781 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001782 *
Gilles Peskine28538492018-07-11 17:34:00 +02001783 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001784 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001785 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001786 * \retval #PSA_ERROR_NOT_PERMITTED
1787 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001788 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001789 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001790 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001791 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1792 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1793 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001794 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001795 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001796 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001797 * The operation state is not valid (already set up and not
1798 * subsequently completed).
1799 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001800 * The library has not been previously initialized by psa_crypto_init().
1801 * It is implementation-dependent whether a failure to initialize
1802 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001803 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001804psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001805 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001806 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001807
1808/** Set the key for a multipart symmetric decryption operation.
1809 *
1810 * The sequence of operations to decrypt a message with a symmetric cipher
1811 * is as follows:
1812 * -# Allocate an operation object which will be passed to all the functions
1813 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001814 * -# Initialize the operation object with one of the methods described in the
1815 * documentation for #psa_cipher_operation_t, e.g.
1816 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001817 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001818 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001819 * decryption. If the IV is prepended to the ciphertext, you can call
1820 * psa_cipher_update() on a buffer containing the IV followed by the
1821 * beginning of the message.
1822 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1823 * of the message each time.
1824 * -# Call psa_cipher_finish().
1825 *
1826 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001827 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001828 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001829 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001830 * eventually terminate the operation. The following events terminate an
1831 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001832 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001833 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001834 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001835 * \param[in,out] operation The operation object to set up. It must have
1836 * been initialized as per the documentation for
1837 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001838 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001839 * It must remain valid until the operation
1840 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001841 * \param alg The cipher algorithm to compute
1842 * (\c PSA_ALG_XXX value such that
1843 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001844 *
Gilles Peskine28538492018-07-11 17:34:00 +02001845 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001846 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001847 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001848 * \retval #PSA_ERROR_NOT_PERMITTED
1849 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001850 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001851 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001852 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001853 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1854 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1855 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001856 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001857 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001858 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001859 * The operation state is not valid (already set up and not
1860 * subsequently completed).
1861 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001862 * The library has not been previously initialized by psa_crypto_init().
1863 * It is implementation-dependent whether a failure to initialize
1864 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001865 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001866psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001867 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001868 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001869
Gilles Peskinedcd14942018-07-12 00:30:52 +02001870/** Generate an IV for a symmetric encryption operation.
1871 *
1872 * This function generates a random IV (initialization vector), nonce
1873 * or initial counter value for the encryption operation as appropriate
1874 * for the chosen algorithm, key type and key size.
1875 *
1876 * The application must call psa_cipher_encrypt_setup() before
1877 * calling this function.
1878 *
1879 * If this function returns an error status, the operation becomes inactive.
1880 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001881 * \param[in,out] operation Active cipher operation.
1882 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001883 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001884 * \param[out] iv_length On success, the number of bytes of the
1885 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001886 *
1887 * \retval #PSA_SUCCESS
1888 * Success.
1889 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001890 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001891 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001892 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001893 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1894 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1895 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001896 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001897 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001898 * \retval #PSA_ERROR_BAD_STATE
1899 * The library has not been previously initialized by psa_crypto_init().
1900 * It is implementation-dependent whether a failure to initialize
1901 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001902 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001903psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001904 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001905 size_t iv_size,
1906 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001907
Gilles Peskinedcd14942018-07-12 00:30:52 +02001908/** Set the IV for a symmetric encryption or decryption operation.
1909 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001910 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001911 * or initial counter value for the encryption or decryption operation.
1912 *
1913 * The application must call psa_cipher_encrypt_setup() before
1914 * calling this function.
1915 *
1916 * If this function returns an error status, the operation becomes inactive.
1917 *
1918 * \note When encrypting, applications should use psa_cipher_generate_iv()
1919 * instead of this function, unless implementing a protocol that requires
1920 * a non-random IV.
1921 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001922 * \param[in,out] operation Active cipher operation.
1923 * \param[in] iv Buffer containing the IV to use.
1924 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001925 *
1926 * \retval #PSA_SUCCESS
1927 * Success.
1928 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001929 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001930 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001931 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001932 * or the chosen algorithm does not use an IV.
1933 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1934 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1935 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001936 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001937 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001938 * \retval #PSA_ERROR_BAD_STATE
1939 * The library has not been previously initialized by psa_crypto_init().
1940 * It is implementation-dependent whether a failure to initialize
1941 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001942 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001943psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001944 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001945 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001946
Gilles Peskinedcd14942018-07-12 00:30:52 +02001947/** Encrypt or decrypt a message fragment in an active cipher operation.
1948 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001949 * Before calling this function, you must:
1950 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1951 * The choice of setup function determines whether this function
1952 * encrypts or decrypts its input.
1953 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1954 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001955 *
1956 * If this function returns an error status, the operation becomes inactive.
1957 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001958 * \param[in,out] operation Active cipher operation.
1959 * \param[in] input Buffer containing the message fragment to
1960 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001961 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001962 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001963 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001964 * \param[out] output_length On success, the number of bytes
1965 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001966 *
1967 * \retval #PSA_SUCCESS
1968 * Success.
1969 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001970 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001971 * not set, or already completed).
1972 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1973 * The size of the \p output buffer is too small.
1974 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1975 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1976 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001977 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001978 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001979 * \retval #PSA_ERROR_BAD_STATE
1980 * The library has not been previously initialized by psa_crypto_init().
1981 * It is implementation-dependent whether a failure to initialize
1982 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001983 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001984psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1985 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001986 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001987 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001988 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001989 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001990
Gilles Peskinedcd14942018-07-12 00:30:52 +02001991/** Finish encrypting or decrypting a message in a cipher operation.
1992 *
1993 * The application must call psa_cipher_encrypt_setup() or
1994 * psa_cipher_decrypt_setup() before calling this function. The choice
1995 * of setup function determines whether this function encrypts or
1996 * decrypts its input.
1997 *
1998 * This function finishes the encryption or decryption of the message
1999 * formed by concatenating the inputs passed to preceding calls to
2000 * psa_cipher_update().
2001 *
2002 * When this function returns, the operation becomes inactive.
2003 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002004 * \param[in,out] operation Active cipher operation.
2005 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002006 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002007 * \param[out] output_length On success, the number of bytes
2008 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002009 *
2010 * \retval #PSA_SUCCESS
2011 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02002012 * \retval #PSA_ERROR_INVALID_ARGUMENT
2013 * The total input size passed to this operation is not valid for
2014 * this particular algorithm. For example, the algorithm is a based
2015 * on block cipher and requires a whole number of blocks, but the
2016 * total input size is not a multiple of the block size.
2017 * \retval #PSA_ERROR_INVALID_PADDING
2018 * This is a decryption operation for an algorithm that includes
2019 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002020 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002021 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02002022 * not set, or already completed).
2023 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2024 * The size of the \p output buffer is too small.
2025 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2026 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2027 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002028 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002029 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002030 * \retval #PSA_ERROR_BAD_STATE
2031 * The library has not been previously initialized by psa_crypto_init().
2032 * It is implementation-dependent whether a failure to initialize
2033 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002034 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002035psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002036 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002037 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002038 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002039
Gilles Peskinedcd14942018-07-12 00:30:52 +02002040/** Abort a cipher operation.
2041 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002042 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002043 * \p operation structure itself. Once aborted, the operation object
2044 * can be reused for another operation by calling
2045 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002046 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002047 * You may call this function any time after the operation object has
2048 * been initialized by any of the following methods:
2049 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2050 * whether it succeeds or not.
2051 * - Initializing the \c struct to all-bits-zero.
2052 * - Initializing the \c struct to logical zeros, e.g.
2053 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002054 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002055 * In particular, calling psa_cipher_abort() after the operation has been
2056 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2057 * is safe and has no effect.
2058 *
2059 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002060 *
2061 * \retval #PSA_SUCCESS
2062 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002063 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002064 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2065 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002066 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002067 * \retval #PSA_ERROR_BAD_STATE
2068 * The library has not been previously initialized by psa_crypto_init().
2069 * It is implementation-dependent whether a failure to initialize
2070 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002071 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002072psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2073
2074/**@}*/
2075
Gilles Peskine3b555712018-03-03 21:27:57 +01002076/** \defgroup aead Authenticated encryption with associated data (AEAD)
2077 * @{
2078 */
2079
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002080/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002081 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002082 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002083 * \param alg The AEAD algorithm to compute
2084 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002085 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002086 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002087 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002088 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002089 * but not encrypted.
2090 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002091 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002092 * encrypted.
2093 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002094 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002095 * encrypted data. The additional data is not
2096 * part of this output. For algorithms where the
2097 * encrypted data and the authentication tag
2098 * are defined as separate outputs, the
2099 * authentication tag is appended to the
2100 * encrypted data.
2101 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2102 * This must be at least
2103 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2104 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002105 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002106 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002107 *
Gilles Peskine28538492018-07-11 17:34:00 +02002108 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002109 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002110 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002111 * \retval #PSA_ERROR_NOT_PERMITTED
2112 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002113 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002114 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002115 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002116 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002117 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2118 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002119 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2120 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002121 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002122 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002123 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002124 * The library has not been previously initialized by psa_crypto_init().
2125 * It is implementation-dependent whether a failure to initialize
2126 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002127 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002128psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002129 psa_algorithm_t alg,
2130 const uint8_t *nonce,
2131 size_t nonce_length,
2132 const uint8_t *additional_data,
2133 size_t additional_data_length,
2134 const uint8_t *plaintext,
2135 size_t plaintext_length,
2136 uint8_t *ciphertext,
2137 size_t ciphertext_size,
2138 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002139
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002140/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002141 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002142 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002143 * \param alg The AEAD algorithm to compute
2144 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002145 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002146 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002147 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002148 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002149 * but not encrypted.
2150 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002151 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002152 * encrypted. For algorithms where the
2153 * encrypted data and the authentication tag
2154 * are defined as separate inputs, the buffer
2155 * must contain the encrypted data followed
2156 * by the authentication tag.
2157 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002158 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002159 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2160 * This must be at least
2161 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2162 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002163 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002164 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002165 *
Gilles Peskine28538492018-07-11 17:34:00 +02002166 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002167 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002168 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002169 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002170 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002171 * \retval #PSA_ERROR_NOT_PERMITTED
2172 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002173 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002174 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002175 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002176 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002177 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2178 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002179 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2180 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002181 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002182 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002183 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002184 * The library has not been previously initialized by psa_crypto_init().
2185 * It is implementation-dependent whether a failure to initialize
2186 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002187 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002188psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002189 psa_algorithm_t alg,
2190 const uint8_t *nonce,
2191 size_t nonce_length,
2192 const uint8_t *additional_data,
2193 size_t additional_data_length,
2194 const uint8_t *ciphertext,
2195 size_t ciphertext_length,
2196 uint8_t *plaintext,
2197 size_t plaintext_size,
2198 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002199
Gilles Peskine30a9e412019-01-14 18:36:12 +01002200/** The type of the state data structure for multipart AEAD operations.
2201 *
2202 * Before calling any function on an AEAD operation object, the application
2203 * must initialize it by any of the following means:
2204 * - Set the structure to all-bits-zero, for example:
2205 * \code
2206 * psa_aead_operation_t operation;
2207 * memset(&operation, 0, sizeof(operation));
2208 * \endcode
2209 * - Initialize the structure to logical zero values, for example:
2210 * \code
2211 * psa_aead_operation_t operation = {0};
2212 * \endcode
2213 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2214 * for example:
2215 * \code
2216 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2217 * \endcode
2218 * - Assign the result of the function psa_aead_operation_init()
2219 * to the structure, for example:
2220 * \code
2221 * psa_aead_operation_t operation;
2222 * operation = psa_aead_operation_init();
2223 * \endcode
2224 *
2225 * This is an implementation-defined \c struct. Applications should not
2226 * make any assumptions about the content of this structure except
2227 * as directed by the documentation of a specific implementation. */
2228typedef struct psa_aead_operation_s psa_aead_operation_t;
2229
2230/** \def PSA_AEAD_OPERATION_INIT
2231 *
2232 * This macro returns a suitable initializer for an AEAD operation object of
2233 * type #psa_aead_operation_t.
2234 */
2235#ifdef __DOXYGEN_ONLY__
2236/* This is an example definition for documentation purposes.
2237 * Implementations should define a suitable value in `crypto_struct.h`.
2238 */
2239#define PSA_AEAD_OPERATION_INIT {0}
2240#endif
2241
2242/** Return an initial value for an AEAD operation object.
2243 */
2244static psa_aead_operation_t psa_aead_operation_init(void);
2245
2246/** Set the key for a multipart authenticated encryption operation.
2247 *
2248 * The sequence of operations to encrypt a message with authentication
2249 * is as follows:
2250 * -# Allocate an operation object which will be passed to all the functions
2251 * listed here.
2252 * -# Initialize the operation object with one of the methods described in the
2253 * documentation for #psa_aead_operation_t, e.g.
2254 * PSA_AEAD_OPERATION_INIT.
2255 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002256 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2257 * inputs to the subsequent calls to psa_aead_update_ad() and
2258 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2259 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002260 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2261 * generate or set the nonce. You should use
2262 * psa_aead_generate_nonce() unless the protocol you are implementing
2263 * requires a specific nonce value.
2264 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2265 * of the non-encrypted additional authenticated data each time.
2266 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002267 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002268 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002269 *
2270 * The application may call psa_aead_abort() at any time after the operation
2271 * has been initialized.
2272 *
2273 * After a successful call to psa_aead_encrypt_setup(), the application must
2274 * eventually terminate the operation. The following events terminate an
2275 * operation:
2276 * - A failed call to any of the \c psa_aead_xxx functions.
2277 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2278 *
2279 * \param[in,out] operation The operation object to set up. It must have
2280 * been initialized as per the documentation for
2281 * #psa_aead_operation_t and not yet in use.
2282 * \param handle Handle to the key to use for the operation.
2283 * It must remain valid until the operation
2284 * terminates.
2285 * \param alg The AEAD algorithm to compute
2286 * (\c PSA_ALG_XXX value such that
2287 * #PSA_ALG_IS_AEAD(\p alg) is true).
2288 *
2289 * \retval #PSA_SUCCESS
2290 * Success.
2291 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002292 * \retval #PSA_ERROR_NOT_PERMITTED
2293 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002294 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002295 * \retval #PSA_ERROR_NOT_SUPPORTED
2296 * \p alg is not supported or is not an AEAD algorithm.
2297 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2298 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2299 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002300 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002301 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002302 * \retval #PSA_ERROR_BAD_STATE
2303 * The library has not been previously initialized by psa_crypto_init().
2304 * It is implementation-dependent whether a failure to initialize
2305 * results in this error code.
2306 */
2307psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2308 psa_key_handle_t handle,
2309 psa_algorithm_t alg);
2310
2311/** Set the key for a multipart authenticated decryption operation.
2312 *
2313 * The sequence of operations to decrypt a message with authentication
2314 * is as follows:
2315 * -# Allocate an operation object which will be passed to all the functions
2316 * listed here.
2317 * -# Initialize the operation object with one of the methods described in the
2318 * documentation for #psa_aead_operation_t, e.g.
2319 * PSA_AEAD_OPERATION_INIT.
2320 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002321 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2322 * inputs to the subsequent calls to psa_aead_update_ad() and
2323 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2324 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002325 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2326 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2327 * of the non-encrypted additional authenticated data each time.
2328 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002329 * of the ciphertext to decrypt each time.
2330 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002331 *
2332 * The application may call psa_aead_abort() at any time after the operation
2333 * has been initialized.
2334 *
2335 * After a successful call to psa_aead_decrypt_setup(), the application must
2336 * eventually terminate the operation. The following events terminate an
2337 * operation:
2338 * - A failed call to any of the \c psa_aead_xxx functions.
2339 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2340 *
2341 * \param[in,out] operation The operation object to set up. It must have
2342 * been initialized as per the documentation for
2343 * #psa_aead_operation_t and not yet in use.
2344 * \param handle Handle to the key to use for the operation.
2345 * It must remain valid until the operation
2346 * terminates.
2347 * \param alg The AEAD algorithm to compute
2348 * (\c PSA_ALG_XXX value such that
2349 * #PSA_ALG_IS_AEAD(\p alg) is true).
2350 *
2351 * \retval #PSA_SUCCESS
2352 * Success.
2353 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002354 * \retval #PSA_ERROR_NOT_PERMITTED
2355 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002356 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002357 * \retval #PSA_ERROR_NOT_SUPPORTED
2358 * \p alg is not supported or is not an AEAD algorithm.
2359 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2360 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2361 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002362 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002363 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002364 * \retval #PSA_ERROR_BAD_STATE
2365 * The library has not been previously initialized by psa_crypto_init().
2366 * It is implementation-dependent whether a failure to initialize
2367 * results in this error code.
2368 */
2369psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2370 psa_key_handle_t handle,
2371 psa_algorithm_t alg);
2372
2373/** Generate a random nonce for an authenticated encryption operation.
2374 *
2375 * This function generates a random nonce for the authenticated encryption
2376 * operation with an appropriate size for the chosen algorithm, key type
2377 * and key size.
2378 *
2379 * The application must call psa_aead_encrypt_setup() before
2380 * calling this function.
2381 *
2382 * If this function returns an error status, the operation becomes inactive.
2383 *
2384 * \param[in,out] operation Active AEAD operation.
2385 * \param[out] nonce Buffer where the generated nonce is to be
2386 * written.
2387 * \param nonce_size Size of the \p nonce buffer in bytes.
2388 * \param[out] nonce_length On success, the number of bytes of the
2389 * generated nonce.
2390 *
2391 * \retval #PSA_SUCCESS
2392 * Success.
2393 * \retval #PSA_ERROR_BAD_STATE
2394 * The operation state is not valid (not set up, or nonce already set).
2395 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2396 * The size of the \p nonce buffer is too small.
2397 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2398 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2399 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002400 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002401 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002402 * \retval #PSA_ERROR_BAD_STATE
2403 * The library has not been previously initialized by psa_crypto_init().
2404 * It is implementation-dependent whether a failure to initialize
2405 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002406 */
2407psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002408 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002409 size_t nonce_size,
2410 size_t *nonce_length);
2411
2412/** Set the nonce for an authenticated encryption or decryption operation.
2413 *
2414 * This function sets the nonce for the authenticated
2415 * encryption or decryption operation.
2416 *
2417 * The application must call psa_aead_encrypt_setup() before
2418 * calling this function.
2419 *
2420 * If this function returns an error status, the operation becomes inactive.
2421 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002422 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002423 * instead of this function, unless implementing a protocol that requires
2424 * a non-random IV.
2425 *
2426 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002427 * \param[in] nonce Buffer containing the nonce to use.
2428 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002429 *
2430 * \retval #PSA_SUCCESS
2431 * Success.
2432 * \retval #PSA_ERROR_BAD_STATE
2433 * The operation state is not valid (not set up, or nonce already set).
2434 * \retval #PSA_ERROR_INVALID_ARGUMENT
2435 * The size of \p nonce is not acceptable for the chosen algorithm.
2436 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2437 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2438 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002439 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002440 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002441 * \retval #PSA_ERROR_BAD_STATE
2442 * The library has not been previously initialized by psa_crypto_init().
2443 * It is implementation-dependent whether a failure to initialize
2444 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002445 */
2446psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002447 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002448 size_t nonce_length);
2449
Gilles Peskinebc59c852019-01-17 15:26:08 +01002450/** Declare the lengths of the message and additional data for AEAD.
2451 *
2452 * The application must call this function before calling
2453 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2454 * the operation requires it. If the algorithm does not require it,
2455 * calling this function is optional, but if this function is called
2456 * then the implementation must enforce the lengths.
2457 *
2458 * You may call this function before or after setting the nonce with
2459 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2460 *
2461 * - For #PSA_ALG_CCM, calling this function is required.
2462 * - For the other AEAD algorithms defined in this specification, calling
2463 * this function is not required.
2464 * - For vendor-defined algorithm, refer to the vendor documentation.
2465 *
2466 * \param[in,out] operation Active AEAD operation.
2467 * \param ad_length Size of the non-encrypted additional
2468 * authenticated data in bytes.
2469 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2470 *
2471 * \retval #PSA_SUCCESS
2472 * Success.
2473 * \retval #PSA_ERROR_BAD_STATE
2474 * The operation state is not valid (not set up, already completed,
2475 * or psa_aead_update_ad() or psa_aead_update() already called).
2476 * \retval #PSA_ERROR_INVALID_ARGUMENT
2477 * At least one of the lengths is not acceptable for the chosen
2478 * algorithm.
2479 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2480 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2481 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002482 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002483 * \retval #PSA_ERROR_BAD_STATE
2484 * The library has not been previously initialized by psa_crypto_init().
2485 * It is implementation-dependent whether a failure to initialize
2486 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002487 */
2488psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2489 size_t ad_length,
2490 size_t plaintext_length);
2491
Gilles Peskine30a9e412019-01-14 18:36:12 +01002492/** Pass additional data to an active AEAD operation.
2493 *
2494 * Additional data is authenticated, but not encrypted.
2495 *
2496 * You may call this function multiple times to pass successive fragments
2497 * of the additional data. You may not call this function after passing
2498 * data to encrypt or decrypt with psa_aead_update().
2499 *
2500 * Before calling this function, you must:
2501 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2502 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2503 *
2504 * If this function returns an error status, the operation becomes inactive.
2505 *
2506 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2507 * there is no guarantee that the input is valid. Therefore, until
2508 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2509 * treat the input as untrusted and prepare to undo any action that
2510 * depends on the input if psa_aead_verify() returns an error status.
2511 *
2512 * \param[in,out] operation Active AEAD operation.
2513 * \param[in] input Buffer containing the fragment of
2514 * additional data.
2515 * \param input_length Size of the \p input buffer in bytes.
2516 *
2517 * \retval #PSA_SUCCESS
2518 * Success.
2519 * \retval #PSA_ERROR_BAD_STATE
2520 * The operation state is not valid (not set up, nonce not set,
2521 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002522 * \retval #PSA_ERROR_INVALID_ARGUMENT
2523 * The total input length overflows the additional data length that
2524 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002525 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2526 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2527 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002528 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002529 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002530 * \retval #PSA_ERROR_BAD_STATE
2531 * The library has not been previously initialized by psa_crypto_init().
2532 * It is implementation-dependent whether a failure to initialize
2533 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002534 */
2535psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2536 const uint8_t *input,
2537 size_t input_length);
2538
2539/** Encrypt or decrypt a message fragment in an active AEAD operation.
2540 *
2541 * Before calling this function, you must:
2542 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2543 * The choice of setup function determines whether this function
2544 * encrypts or decrypts its input.
2545 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2546 * 3. Call psa_aead_update_ad() to pass all the additional data.
2547 *
2548 * If this function returns an error status, the operation becomes inactive.
2549 *
2550 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2551 * there is no guarantee that the input is valid. Therefore, until
2552 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2553 * - Do not use the output in any way other than storing it in a
2554 * confidential location. If you take any action that depends
2555 * on the tentative decrypted data, this action will need to be
2556 * undone if the input turns out not to be valid. Furthermore,
2557 * if an adversary can observe that this action took place
2558 * (for example through timing), they may be able to use this
2559 * fact as an oracle to decrypt any message encrypted with the
2560 * same key.
2561 * - In particular, do not copy the output anywhere but to a
2562 * memory or storage space that you have exclusive access to.
2563 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002564 * This function does not require the input to be aligned to any
2565 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002566 * a whole block at a time, it must consume all the input provided, but
2567 * it may delay the end of the corresponding output until a subsequent
2568 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2569 * provides sufficient input. The amount of data that can be delayed
2570 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002571 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002572 * \param[in,out] operation Active AEAD operation.
2573 * \param[in] input Buffer containing the message fragment to
2574 * encrypt or decrypt.
2575 * \param input_length Size of the \p input buffer in bytes.
2576 * \param[out] output Buffer where the output is to be written.
2577 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002578 * This must be at least
2579 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2580 * \p input_length) where \c alg is the
2581 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002582 * \param[out] output_length On success, the number of bytes
2583 * that make up the returned output.
2584 *
2585 * \retval #PSA_SUCCESS
2586 * Success.
2587 * \retval #PSA_ERROR_BAD_STATE
2588 * The operation state is not valid (not set up, nonce not set
2589 * or already completed).
2590 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2591 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002592 * You can determine a sufficient buffer size by calling
2593 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2594 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002595 * \retval #PSA_ERROR_INVALID_ARGUMENT
2596 * The total length of input to psa_aead_update_ad() so far is
2597 * less than the additional data length that was previously
2598 * specified with psa_aead_set_lengths().
2599 * \retval #PSA_ERROR_INVALID_ARGUMENT
2600 * The total input length overflows the plaintext length that
2601 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002602 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2603 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2604 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002605 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002606 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002607 * \retval #PSA_ERROR_BAD_STATE
2608 * The library has not been previously initialized by psa_crypto_init().
2609 * It is implementation-dependent whether a failure to initialize
2610 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002611 */
2612psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2613 const uint8_t *input,
2614 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002615 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002616 size_t output_size,
2617 size_t *output_length);
2618
2619/** Finish encrypting a message in an AEAD operation.
2620 *
2621 * The operation must have been set up with psa_aead_encrypt_setup().
2622 *
2623 * This function finishes the authentication of the additional data
2624 * formed by concatenating the inputs passed to preceding calls to
2625 * psa_aead_update_ad() with the plaintext formed by concatenating the
2626 * inputs passed to preceding calls to psa_aead_update().
2627 *
2628 * This function has two output buffers:
2629 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002630 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002631 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002632 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002633 * that the operation performs.
2634 *
2635 * When this function returns, the operation becomes inactive.
2636 *
2637 * \param[in,out] operation Active AEAD operation.
2638 * \param[out] ciphertext Buffer where the last part of the ciphertext
2639 * is to be written.
2640 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002641 * This must be at least
2642 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2643 * \c alg is the algorithm that is being
2644 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002645 * \param[out] ciphertext_length On success, the number of bytes of
2646 * returned ciphertext.
2647 * \param[out] tag Buffer where the authentication tag is
2648 * to be written.
2649 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002650 * This must be at least
2651 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2652 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002653 * \param[out] tag_length On success, the number of bytes
2654 * that make up the returned tag.
2655 *
2656 * \retval #PSA_SUCCESS
2657 * Success.
2658 * \retval #PSA_ERROR_BAD_STATE
2659 * The operation state is not valid (not set up, nonce not set,
2660 * decryption, or already completed).
2661 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002662 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002663 * You can determine a sufficient buffer size for \p ciphertext by
2664 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2665 * where \c alg is the algorithm that is being calculated.
2666 * You can determine a sufficient buffer size for \p tag by
2667 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002668 * \retval #PSA_ERROR_INVALID_ARGUMENT
2669 * The total length of input to psa_aead_update_ad() so far is
2670 * less than the additional data length that was previously
2671 * specified with psa_aead_set_lengths().
2672 * \retval #PSA_ERROR_INVALID_ARGUMENT
2673 * The total length of input to psa_aead_update() so far is
2674 * less than the plaintext length that was previously
2675 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002676 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2677 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2678 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002679 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002680 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002681 * \retval #PSA_ERROR_BAD_STATE
2682 * The library has not been previously initialized by psa_crypto_init().
2683 * It is implementation-dependent whether a failure to initialize
2684 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002685 */
2686psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002687 uint8_t *ciphertext,
2688 size_t ciphertext_size,
2689 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002690 uint8_t *tag,
2691 size_t tag_size,
2692 size_t *tag_length);
2693
2694/** Finish authenticating and decrypting a message in an AEAD operation.
2695 *
2696 * The operation must have been set up with psa_aead_decrypt_setup().
2697 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002698 * This function finishes the authenticated decryption of the message
2699 * components:
2700 *
2701 * - The additional data consisting of the concatenation of the inputs
2702 * passed to preceding calls to psa_aead_update_ad().
2703 * - The ciphertext consisting of the concatenation of the inputs passed to
2704 * preceding calls to psa_aead_update().
2705 * - The tag passed to this function call.
2706 *
2707 * If the authentication tag is correct, this function outputs any remaining
2708 * plaintext and reports success. If the authentication tag is not correct,
2709 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002710 *
2711 * When this function returns, the operation becomes inactive.
2712 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002713 * \note Implementations shall make the best effort to ensure that the
2714 * comparison between the actual tag and the expected tag is performed
2715 * in constant time.
2716 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002717 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002718 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002719 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002720 * from previous calls to psa_aead_update()
2721 * that could not be processed until the end
2722 * of the input.
2723 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002724 * This must be at least
2725 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2726 * \c alg is the algorithm that is being
2727 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002728 * \param[out] plaintext_length On success, the number of bytes of
2729 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002730 * \param[in] tag Buffer containing the authentication tag.
2731 * \param tag_length Size of the \p tag buffer in bytes.
2732 *
2733 * \retval #PSA_SUCCESS
2734 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002735 * \retval #PSA_ERROR_INVALID_SIGNATURE
2736 * The calculations were successful, but the authentication tag is
2737 * not correct.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002738 * \retval #PSA_ERROR_BAD_STATE
2739 * The operation state is not valid (not set up, nonce not set,
2740 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002741 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2742 * The size of the \p plaintext buffer is too small.
2743 * You can determine a sufficient buffer size for \p plaintext by
2744 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2745 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002746 * \retval #PSA_ERROR_INVALID_ARGUMENT
2747 * The total length of input to psa_aead_update_ad() so far is
2748 * less than the additional data length that was previously
2749 * specified with psa_aead_set_lengths().
2750 * \retval #PSA_ERROR_INVALID_ARGUMENT
2751 * The total length of input to psa_aead_update() so far is
2752 * less than the plaintext length that was previously
2753 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002754 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2755 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2756 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002757 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002758 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002759 * \retval #PSA_ERROR_BAD_STATE
2760 * The library has not been previously initialized by psa_crypto_init().
2761 * It is implementation-dependent whether a failure to initialize
2762 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002763 */
2764psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002765 uint8_t *plaintext,
2766 size_t plaintext_size,
2767 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002768 const uint8_t *tag,
2769 size_t tag_length);
2770
2771/** Abort an AEAD operation.
2772 *
2773 * Aborting an operation frees all associated resources except for the
2774 * \p operation structure itself. Once aborted, the operation object
2775 * can be reused for another operation by calling
2776 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2777 *
2778 * You may call this function any time after the operation object has
2779 * been initialized by any of the following methods:
2780 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2781 * whether it succeeds or not.
2782 * - Initializing the \c struct to all-bits-zero.
2783 * - Initializing the \c struct to logical zeros, e.g.
2784 * `psa_aead_operation_t operation = {0}`.
2785 *
2786 * In particular, calling psa_aead_abort() after the operation has been
2787 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2788 * is safe and has no effect.
2789 *
2790 * \param[in,out] operation Initialized AEAD operation.
2791 *
2792 * \retval #PSA_SUCCESS
2793 * \retval #PSA_ERROR_BAD_STATE
2794 * \p operation is not an active AEAD operation.
2795 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2796 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002797 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002798 * \retval #PSA_ERROR_BAD_STATE
2799 * The library has not been previously initialized by psa_crypto_init().
2800 * It is implementation-dependent whether a failure to initialize
2801 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002802 */
2803psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2804
Gilles Peskine3b555712018-03-03 21:27:57 +01002805/**@}*/
2806
Gilles Peskine20035e32018-02-03 22:44:14 +01002807/** \defgroup asymmetric Asymmetric cryptography
2808 * @{
2809 */
2810
2811/**
2812 * \brief Sign a hash or short message with a private key.
2813 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002814 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002815 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002816 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2817 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2818 * to determine the hash algorithm to use.
2819 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002820 * \param handle Handle to the key to use for the operation.
2821 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002822 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002823 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002824 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002825 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002826 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002827 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002828 * \param[out] signature_length On success, the number of bytes
2829 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002830 *
Gilles Peskine28538492018-07-11 17:34:00 +02002831 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002832 * \retval #PSA_ERROR_INVALID_HANDLE
2833 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002834 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002835 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002836 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002837 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002838 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002839 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002840 * \retval #PSA_ERROR_NOT_SUPPORTED
2841 * \retval #PSA_ERROR_INVALID_ARGUMENT
2842 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2843 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2844 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002845 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002846 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002847 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002848 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002849 * The library has not been previously initialized by psa_crypto_init().
2850 * It is implementation-dependent whether a failure to initialize
2851 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002852 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002853psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002854 psa_algorithm_t alg,
2855 const uint8_t *hash,
2856 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002857 uint8_t *signature,
2858 size_t signature_size,
2859 size_t *signature_length);
2860
2861/**
2862 * \brief Verify the signature a hash or short message using a public key.
2863 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002864 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002865 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002866 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2867 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2868 * to determine the hash algorithm to use.
2869 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002870 * \param handle Handle to the key to use for the operation.
2871 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002872 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002873 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002874 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002875 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002876 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002877 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002878 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002879 *
Gilles Peskine28538492018-07-11 17:34:00 +02002880 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002881 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002882 * \retval #PSA_ERROR_INVALID_HANDLE
2883 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002884 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002885 * The calculation was perfomed successfully, but the passed
2886 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002887 * \retval #PSA_ERROR_NOT_SUPPORTED
2888 * \retval #PSA_ERROR_INVALID_ARGUMENT
2889 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2890 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2891 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002892 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002893 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002894 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002895 * The library has not been previously initialized by psa_crypto_init().
2896 * It is implementation-dependent whether a failure to initialize
2897 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002898 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002899psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002900 psa_algorithm_t alg,
2901 const uint8_t *hash,
2902 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002903 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002904 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002905
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002906/**
2907 * \brief Encrypt a short message with a public key.
2908 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002909 * \param handle Handle to the key to use for the operation.
2910 * It must be a public key or an asymmetric
2911 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002912 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002913 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002914 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002915 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002916 * \param[in] salt A salt or label, if supported by the
2917 * encryption algorithm.
2918 * If the algorithm does not support a
2919 * salt, pass \c NULL.
2920 * If the algorithm supports an optional
2921 * salt and you do not want to pass a salt,
2922 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002923 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002924 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2925 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002926 * \param salt_length Size of the \p salt buffer in bytes.
2927 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002928 * \param[out] output Buffer where the encrypted message is to
2929 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002930 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002931 * \param[out] output_length On success, the number of bytes
2932 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002933 *
Gilles Peskine28538492018-07-11 17:34:00 +02002934 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002935 * \retval #PSA_ERROR_INVALID_HANDLE
2936 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002937 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002938 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002939 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002940 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002941 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002942 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002943 * \retval #PSA_ERROR_NOT_SUPPORTED
2944 * \retval #PSA_ERROR_INVALID_ARGUMENT
2945 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2946 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2947 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002948 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002949 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002950 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002951 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002952 * The library has not been previously initialized by psa_crypto_init().
2953 * It is implementation-dependent whether a failure to initialize
2954 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002955 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002956psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002957 psa_algorithm_t alg,
2958 const uint8_t *input,
2959 size_t input_length,
2960 const uint8_t *salt,
2961 size_t salt_length,
2962 uint8_t *output,
2963 size_t output_size,
2964 size_t *output_length);
2965
2966/**
2967 * \brief Decrypt a short message with a private key.
2968 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002969 * \param handle Handle to the key to use for the operation.
2970 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002971 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002972 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002973 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002974 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002975 * \param[in] salt A salt or label, if supported by the
2976 * encryption algorithm.
2977 * If the algorithm does not support a
2978 * salt, pass \c NULL.
2979 * If the algorithm supports an optional
2980 * salt and you do not want to pass a salt,
2981 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002982 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002983 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2984 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002985 * \param salt_length Size of the \p salt buffer in bytes.
2986 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002987 * \param[out] output Buffer where the decrypted message is to
2988 * be written.
2989 * \param output_size Size of the \c output buffer in bytes.
2990 * \param[out] output_length On success, the number of bytes
2991 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002992 *
Gilles Peskine28538492018-07-11 17:34:00 +02002993 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002994 * \retval #PSA_ERROR_INVALID_HANDLE
2995 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002996 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002997 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002998 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002999 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003000 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003001 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02003002 * \retval #PSA_ERROR_NOT_SUPPORTED
3003 * \retval #PSA_ERROR_INVALID_ARGUMENT
3004 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3005 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3006 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003007 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003008 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003009 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3010 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003011 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003012 * The library has not been previously initialized by psa_crypto_init().
3013 * It is implementation-dependent whether a failure to initialize
3014 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003015 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003016psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003017 psa_algorithm_t alg,
3018 const uint8_t *input,
3019 size_t input_length,
3020 const uint8_t *salt,
3021 size_t salt_length,
3022 uint8_t *output,
3023 size_t output_size,
3024 size_t *output_length);
3025
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003026/**@}*/
3027
Gilles Peskine35675b62019-05-16 17:26:11 +02003028/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003029 * @{
3030 */
3031
Gilles Peskine35675b62019-05-16 17:26:11 +02003032/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003033 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003034 * Before calling any function on a key derivation operation object, the
3035 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003036 * - Set the structure to all-bits-zero, for example:
3037 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003038 * psa_key_derivation_operation_t operation;
3039 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003040 * \endcode
3041 * - Initialize the structure to logical zero values, for example:
3042 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003043 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003044 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003045 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003046 * for example:
3047 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003048 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003049 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003050 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003051 * to the structure, for example:
3052 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003053 * psa_key_derivation_operation_t operation;
3054 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003055 * \endcode
3056 *
3057 * This is an implementation-defined \c struct. Applications should not
3058 * make any assumptions about the content of this structure except
3059 * as directed by the documentation of a specific implementation.
3060 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003061typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003062
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003063/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003064 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003065 * This macro returns a suitable initializer for a key derivation operation
3066 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003067 */
3068#ifdef __DOXYGEN_ONLY__
3069/* This is an example definition for documentation purposes.
3070 * Implementations should define a suitable value in `crypto_struct.h`.
3071 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003072#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003073#endif
3074
Gilles Peskine35675b62019-05-16 17:26:11 +02003075/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003076 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003077static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003078
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003079/** Set up a key derivation operation.
3080 *
3081 * A key derivation algorithm takes some inputs and uses them to generate
3082 * a byte stream in a deterministic way.
3083 * This byte stream can be used to produce keys and other
3084 * cryptographic material.
3085 *
3086 * To derive a key:
3087 * - Start with an initialized object of type #psa_key_derivation_operation_t.
3088 * - Call psa_key_derivation_setup() to select the algorithm.
3089 * - Provide the inputs for the key derivation by calling
3090 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3091 * as appropriate. Which inputs are needed, in what order, and whether
3092 * they may be keys and if so of what type depends on the algorithm.
3093 * - Optionally set the operation's maximum capacity with
3094 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3095 * of or after providing inputs. For some algorithms, this step is mandatory
3096 * because the output depends on the maximum capacity.
3097 * - To derive a key, call psa_key_derivation_output_key().
3098 * To derive a byte string for a different purpose, call
3099 * - psa_key_derivation_output_bytes().
3100 * Successive calls to these functions use successive output bytes
3101 * calculated by the key derivation algorithm.
3102 * - Clean up the key derivation operation object with
3103 * psa_key_derivation_abort().
3104 *
3105 * \param[in,out] operation The key derivation operation object
3106 * to set up. It must
3107 * have been initialized but not set up yet.
3108 * \param alg The key derivation algorithm to compute
3109 * (\c PSA_ALG_XXX value such that
3110 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3111 *
3112 * \retval #PSA_SUCCESS
3113 * Success.
3114 * \retval #PSA_ERROR_INVALID_ARGUMENT
3115 * \c alg is not a key derivation algorithm.
3116 * \retval #PSA_ERROR_NOT_SUPPORTED
3117 * \c alg is not supported or is not a key derivation algorithm.
3118 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3119 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3120 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003121 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003122 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003123 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01003124 * The operation state is either not initialized or has already been setup.
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003125 * \retval #PSA_ERROR_BAD_STATE
3126 * The library has not been previously initialized by psa_crypto_init().
3127 * It is implementation-dependent whether a failure to initialize
3128 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003129 */
3130psa_status_t psa_key_derivation_setup(
3131 psa_key_derivation_operation_t *operation,
3132 psa_algorithm_t alg);
3133
Gilles Peskine35675b62019-05-16 17:26:11 +02003134/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003135 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003136 * The capacity of a key derivation is the maximum number of bytes that it can
3137 * return. When you get *N* bytes of output from a key derivation operation,
3138 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003139 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003140 * \param[in] operation The operation to query.
3141 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003142 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003143 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003144 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003145 * \retval #PSA_ERROR_BAD_STATE
3146 * The operation state is not valid.
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003147 * \retval #PSA_ERROR_HARDWARE_FAILURE
3148 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003149 * \retval #PSA_ERROR_BAD_STATE
3150 * The library has not been previously initialized by psa_crypto_init().
3151 * It is implementation-dependent whether a failure to initialize
3152 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003153 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003154psa_status_t psa_key_derivation_get_capacity(
3155 const psa_key_derivation_operation_t *operation,
3156 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003157
Gilles Peskine35675b62019-05-16 17:26:11 +02003158/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003159 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003160 * The capacity of a key derivation operation is the maximum number of bytes
3161 * that the key derivation operation can return from this point onwards.
3162 *
3163 * \param[in,out] operation The key derivation operation object to modify.
3164 * \param capacity The new capacity of the operation.
3165 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003166 * current capacity.
3167 *
3168 * \retval #PSA_SUCCESS
3169 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003170 * \p capacity is larger than the operation's current capacity.
3171 * In this case, the operation object remains valid and its capacity
3172 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003173 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003174 * The operation state is not valid.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003175 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003176 * \retval #PSA_ERROR_HARDWARE_FAILURE
3177 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003178 * \retval #PSA_ERROR_BAD_STATE
3179 * The library has not been previously initialized by psa_crypto_init().
3180 * It is implementation-dependent whether a failure to initialize
3181 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003182 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003183psa_status_t psa_key_derivation_set_capacity(
3184 psa_key_derivation_operation_t *operation,
3185 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003186
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003187/** Use the maximum possible capacity for a key derivation operation.
3188 *
3189 * Use this value as the capacity argument when setting up a key derivation
3190 * to indicate that the operation should have the maximum possible capacity.
3191 * The value of the maximum possible capacity depends on the key derivation
3192 * algorithm.
3193 */
3194#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3195
3196/** Provide an input for key derivation or key agreement.
3197 *
3198 * Which inputs are required and in what order depends on the algorithm.
3199 * Refer to the documentation of each key derivation or key agreement
3200 * algorithm for information.
3201 *
3202 * This function passes direct inputs. Some inputs must be passed as keys
3203 * using psa_key_derivation_input_key() instead of this function. Refer to
3204 * the documentation of individual step types for information.
3205 *
3206 * \param[in,out] operation The key derivation operation object to use.
3207 * It must have been set up with
3208 * psa_key_derivation_setup() and must not
3209 * have produced any output yet.
3210 * \param step Which step the input data is for.
3211 * \param[in] data Input data to use.
3212 * \param data_length Size of the \p data buffer in bytes.
3213 *
3214 * \retval #PSA_SUCCESS
3215 * Success.
3216 * \retval #PSA_ERROR_INVALID_ARGUMENT
3217 * \c step is not compatible with the operation's algorithm.
3218 * \retval #PSA_ERROR_INVALID_ARGUMENT
3219 * \c step does not allow direct inputs.
3220 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3221 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3222 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003223 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003224 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003225 * \retval #PSA_ERROR_BAD_STATE
3226 * The value of \p step is not valid given the state of \p operation.
3227 * \retval #PSA_ERROR_BAD_STATE
3228 * The library has not been previously initialized by psa_crypto_init().
3229 * It is implementation-dependent whether a failure to initialize
3230 * results in this error code.
3231 */
3232psa_status_t psa_key_derivation_input_bytes(
3233 psa_key_derivation_operation_t *operation,
3234 psa_key_derivation_step_t step,
3235 const uint8_t *data,
3236 size_t data_length);
3237
3238/** Provide an input for key derivation in the form of a key.
3239 *
3240 * Which inputs are required and in what order depends on the algorithm.
3241 * Refer to the documentation of each key derivation or key agreement
3242 * algorithm for information.
3243 *
3244 * This function passes key inputs. Some inputs must be passed as keys
3245 * of the appropriate type using this function, while others must be
3246 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3247 * the documentation of individual step types for information.
3248 *
3249 * \param[in,out] operation The key derivation operation object to use.
3250 * It must have been set up with
3251 * psa_key_derivation_setup() and must not
3252 * have produced any output yet.
3253 * \param step Which step the input data is for.
3254 * \param handle Handle to the key. It must have an
3255 * appropriate type for \p step and must
3256 * allow the usage #PSA_KEY_USAGE_DERIVE.
3257 *
3258 * \retval #PSA_SUCCESS
3259 * Success.
3260 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003261 * \retval #PSA_ERROR_NOT_PERMITTED
3262 * \retval #PSA_ERROR_INVALID_ARGUMENT
3263 * \c step is not compatible with the operation's algorithm.
3264 * \retval #PSA_ERROR_INVALID_ARGUMENT
3265 * \c step does not allow key inputs.
3266 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3267 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3268 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003269 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003270 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003271 * \retval #PSA_ERROR_BAD_STATE
3272 * The value of \p step is not valid given the state of \p operation.
3273 * \retval #PSA_ERROR_BAD_STATE
3274 * The library has not been previously initialized by psa_crypto_init().
3275 * It is implementation-dependent whether a failure to initialize
3276 * results in this error code.
3277 */
3278psa_status_t psa_key_derivation_input_key(
3279 psa_key_derivation_operation_t *operation,
3280 psa_key_derivation_step_t step,
3281 psa_key_handle_t handle);
3282
3283/** Perform a key agreement and use the shared secret as input to a key
3284 * derivation.
3285 *
3286 * A key agreement algorithm takes two inputs: a private key \p private_key
3287 * a public key \p peer_key.
3288 * The result of this function is passed as input to a key derivation.
3289 * The output of this key derivation can be extracted by reading from the
3290 * resulting operation to produce keys and other cryptographic material.
3291 *
3292 * \param[in,out] operation The key derivation operation object to use.
3293 * It must have been set up with
3294 * psa_key_derivation_setup() with a
3295 * key agreement and derivation algorithm
3296 * \c alg (\c PSA_ALG_XXX value such that
3297 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3298 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3299 * is false).
3300 * The operation must be ready for an
3301 * input of the type given by \p step.
3302 * \param step Which step the input data is for.
3303 * \param private_key Handle to the private key to use.
3304 * \param[in] peer_key Public key of the peer. The peer key must be in the
3305 * same format that psa_import_key() accepts for the
3306 * public key type corresponding to the type of
3307 * private_key. That is, this function performs the
3308 * equivalent of
3309 * #psa_import_key(...,
3310 * `peer_key`, `peer_key_length`) where
3311 * with key attributes indicating the public key
3312 * type corresponding to the type of `private_key`.
3313 * For example, for EC keys, this means that peer_key
3314 * is interpreted as a point on the curve that the
3315 * private key is on. The standard formats for public
3316 * keys are documented in the documentation of
3317 * psa_export_public_key().
3318 * \param peer_key_length Size of \p peer_key in bytes.
3319 *
3320 * \retval #PSA_SUCCESS
3321 * Success.
3322 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003323 * \retval #PSA_ERROR_NOT_PERMITTED
3324 * \retval #PSA_ERROR_INVALID_ARGUMENT
3325 * \c private_key is not compatible with \c alg,
3326 * or \p peer_key is not valid for \c alg or not compatible with
3327 * \c private_key.
3328 * \retval #PSA_ERROR_NOT_SUPPORTED
3329 * \c alg is not supported or is not a key derivation algorithm.
3330 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3331 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3332 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003333 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003334 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003335 * \retval #PSA_ERROR_BAD_STATE
3336 * The library has not been previously initialized by psa_crypto_init().
3337 * It is implementation-dependent whether a failure to initialize
3338 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003339 */
3340psa_status_t psa_key_derivation_key_agreement(
3341 psa_key_derivation_operation_t *operation,
3342 psa_key_derivation_step_t step,
3343 psa_key_handle_t private_key,
3344 const uint8_t *peer_key,
3345 size_t peer_key_length);
3346
Gilles Peskine35675b62019-05-16 17:26:11 +02003347/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003348 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003349 * This function calculates output bytes from a key derivation algorithm and
3350 * return those bytes.
3351 * If you view the key derivation's output as a stream of bytes, this
3352 * function destructively reads the requested number of bytes from the
3353 * stream.
3354 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003355 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003356 * \param[in,out] operation The key derivation operation object to read from.
3357 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003358 * \param output_length Number of bytes to output.
3359 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003360 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003361 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003362 * The operation's capacity was less than
3363 * \p output_length bytes. Note that in this case,
3364 * no output is written to the output buffer.
3365 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003366 * subsequent calls to this function will not
3367 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003368 * \retval #PSA_ERROR_BAD_STATE
3369 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3370 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3371 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003372 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003373 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003374 * \retval #PSA_ERROR_BAD_STATE
3375 * The library has not been previously initialized by psa_crypto_init().
3376 * It is implementation-dependent whether a failure to initialize
3377 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003378 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003379psa_status_t psa_key_derivation_output_bytes(
3380 psa_key_derivation_operation_t *operation,
3381 uint8_t *output,
3382 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003383
Gilles Peskine35675b62019-05-16 17:26:11 +02003384/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003385 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003386 * This function calculates output bytes from a key derivation algorithm
3387 * and uses those bytes to generate a key deterministically.
3388 * If you view the key derivation's output as a stream of bytes, this
3389 * function destructively reads as many bytes as required from the
3390 * stream.
3391 * The operation's capacity decreases by the number of bytes read.
3392 *
3393 * How much output is produced and consumed from the operation, and how
3394 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003395 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003396 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003397 * of a given size, this function is functionally equivalent to
3398 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003399 * and passing the resulting output to #psa_import_key.
3400 * However, this function has a security benefit:
3401 * if the implementation provides an isolation boundary then
3402 * the key material is not exposed outside the isolation boundary.
3403 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003404 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003405 * The following key types defined in this specification follow this scheme:
3406 *
3407 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003408 * - #PSA_KEY_TYPE_ARC4;
3409 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003410 * - #PSA_KEY_TYPE_DERIVE;
3411 * - #PSA_KEY_TYPE_HMAC.
3412 *
3413 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003414 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003415 * Montgomery curve), this function always draws a byte string whose
3416 * length is determined by the curve, and sets the mandatory bits
3417 * accordingly. That is:
3418 *
3419 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3420 * and process it as specified in RFC 7748 &sect;5.
3421 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3422 * and process it as specified in RFC 7748 &sect;5.
3423 *
3424 * - For key types for which the key is represented by a single sequence of
3425 * \p bits bits with constraints as to which bit sequences are acceptable,
3426 * this function draws a byte string of length (\p bits / 8) bytes rounded
3427 * up to the nearest whole number of bytes. If the resulting byte string
3428 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3429 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003430 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003431 * for the output produced by psa_export_key().
3432 * The following key types defined in this specification follow this scheme:
3433 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003434 * - #PSA_KEY_TYPE_DES.
3435 * Force-set the parity bits, but discard forbidden weak keys.
3436 * For 2-key and 3-key triple-DES, the three keys are generated
3437 * successively (for example, for 3-key triple-DES,
3438 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3439 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003440 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003441 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003442 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003443 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003444 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003445 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003446 * Weierstrass curve).
3447 * For these key types, interpret the byte string as integer
3448 * in big-endian order. Discard it if it is not in the range
3449 * [0, *N* - 2] where *N* is the boundary of the private key domain
3450 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003451 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003452 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003453 * This method allows compliance to NIST standards, specifically
3454 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003455 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3456 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3457 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3458 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003459 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003460 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003461 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003462 * implementation-defined.
3463 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003464 * In all cases, the data that is read is discarded from the operation.
3465 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003466 *
Gilles Peskine20628592019-04-19 19:29:50 +02003467 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003468 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003469 * \param[out] handle On success, a handle to the newly created key.
3470 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003471 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003472 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003473 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003474 * If the key is persistent, the key material and the key's metadata
3475 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003476 * \retval #PSA_ERROR_ALREADY_EXISTS
3477 * This is an attempt to create a persistent key, and there is
3478 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003479 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003480 * There was not enough data to create the desired key.
3481 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003482 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003483 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003484 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003485 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003486 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003487 * \retval #PSA_ERROR_INVALID_ARGUMENT
3488 * The provided key attributes are not valid for the operation.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003489 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003490 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3491 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3492 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3493 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003494 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003495 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003496 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003497 * The library has not been previously initialized by psa_crypto_init().
3498 * It is implementation-dependent whether a failure to initialize
3499 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003500 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003501psa_status_t psa_key_derivation_output_key(
3502 const psa_key_attributes_t *attributes,
3503 psa_key_derivation_operation_t *operation,
3504 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003505
Gilles Peskine35675b62019-05-16 17:26:11 +02003506/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003507 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003508 * Once a key derivation operation has been aborted, its capacity is zero.
3509 * Aborting an operation frees all associated resources except for the
3510 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003511 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003512 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003513 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003514 * psa_key_derivation_operation_init() or a zero value. In particular,
3515 * it is valid to call psa_key_derivation_abort() twice, or to call
3516 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003517 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003518 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003519 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003520 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003521 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003522 * \retval #PSA_SUCCESS
3523 * \retval #PSA_ERROR_BAD_STATE
3524 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3525 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003526 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003527 * \retval #PSA_ERROR_BAD_STATE
3528 * The library has not been previously initialized by psa_crypto_init().
3529 * It is implementation-dependent whether a failure to initialize
3530 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003531 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003532psa_status_t psa_key_derivation_abort(
3533 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003534
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003535/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003536 *
3537 * \warning The raw result of a key agreement algorithm such as finite-field
3538 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3539 * not be used directly as key material. It should instead be passed as
3540 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003541 * a key derivation, use psa_key_derivation_key_agreement() and other
3542 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003543 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003544 * \param alg The key agreement algorithm to compute
3545 * (\c PSA_ALG_XXX value such that
3546 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3547 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003548 * \param private_key Handle to the private key to use.
3549 * \param[in] peer_key Public key of the peer. It must be
3550 * in the same format that psa_import_key()
3551 * accepts. The standard formats for public
3552 * keys are documented in the documentation
3553 * of psa_export_public_key().
3554 * \param peer_key_length Size of \p peer_key in bytes.
3555 * \param[out] output Buffer where the decrypted message is to
3556 * be written.
3557 * \param output_size Size of the \c output buffer in bytes.
3558 * \param[out] output_length On success, the number of bytes
3559 * that make up the returned output.
3560 *
3561 * \retval #PSA_SUCCESS
3562 * Success.
3563 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003564 * \retval #PSA_ERROR_NOT_PERMITTED
3565 * \retval #PSA_ERROR_INVALID_ARGUMENT
3566 * \p alg is not a key agreement algorithm
3567 * \retval #PSA_ERROR_INVALID_ARGUMENT
3568 * \p private_key is not compatible with \p alg,
3569 * or \p peer_key is not valid for \p alg or not compatible with
3570 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003571 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3572 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003573 * \retval #PSA_ERROR_NOT_SUPPORTED
3574 * \p alg is not a supported key agreement algorithm.
3575 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3576 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3577 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003578 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003579 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003580 * \retval #PSA_ERROR_BAD_STATE
3581 * The library has not been previously initialized by psa_crypto_init().
3582 * It is implementation-dependent whether a failure to initialize
3583 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003584 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003585psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3586 psa_key_handle_t private_key,
3587 const uint8_t *peer_key,
3588 size_t peer_key_length,
3589 uint8_t *output,
3590 size_t output_size,
3591 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003592
Gilles Peskineea0fb492018-07-12 17:17:20 +02003593/**@}*/
3594
Gilles Peskineedd76872018-07-20 17:42:05 +02003595/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003596 * @{
3597 */
3598
3599/**
3600 * \brief Generate random bytes.
3601 *
3602 * \warning This function **can** fail! Callers MUST check the return status
3603 * and MUST NOT use the content of the output buffer if the return
3604 * status is not #PSA_SUCCESS.
3605 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003606 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003607 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003608 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003609 * \param output_size Number of bytes to generate and output.
3610 *
Gilles Peskine28538492018-07-11 17:34:00 +02003611 * \retval #PSA_SUCCESS
3612 * \retval #PSA_ERROR_NOT_SUPPORTED
3613 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003614 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003615 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3616 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003617 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003618 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003619 * The library has not been previously initialized by psa_crypto_init().
3620 * It is implementation-dependent whether a failure to initialize
3621 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003622 */
3623psa_status_t psa_generate_random(uint8_t *output,
3624 size_t output_size);
3625
3626/**
3627 * \brief Generate a key or key pair.
3628 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003629 * The key is generated randomly.
3630 * Its location, policy, type and size are taken from \p attributes.
3631 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003632 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003633 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003634 * the public exponent is 65537.
3635 * The modulus is a product of two probabilistic primes
3636 * between 2^{n-1} and 2^n where n is the bit size specified in the
3637 * attributes.
3638 *
Gilles Peskine20628592019-04-19 19:29:50 +02003639 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003640 * \param[out] handle On success, a handle to the newly created key.
3641 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003642 *
Gilles Peskine28538492018-07-11 17:34:00 +02003643 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003644 * Success.
3645 * If the key is persistent, the key material and the key's metadata
3646 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003647 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003648 * This is an attempt to create a persistent key, and there is
3649 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003650 * \retval #PSA_ERROR_NOT_SUPPORTED
3651 * \retval #PSA_ERROR_INVALID_ARGUMENT
3652 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3653 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3654 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3655 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003656 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003657 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3658 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003659 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003660 * The library has not been previously initialized by psa_crypto_init().
3661 * It is implementation-dependent whether a failure to initialize
3662 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003663 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003664psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003665 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003666
3667/**@}*/
3668
Gilles Peskinee59236f2018-01-27 23:32:46 +01003669#ifdef __cplusplus
3670}
3671#endif
3672
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003673/* The file "crypto_sizes.h" contains definitions for size calculation
3674 * macros whose definitions are implementation-specific. */
3675#include "crypto_sizes.h"
3676
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003677/* The file "crypto_struct.h" contains definitions for
3678 * implementation-specific structs that are declared above. */
3679#include "crypto_struct.h"
3680
3681/* The file "crypto_extra.h" contains vendor-specific definitions. This
3682 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003683#include "crypto_extra.h"
3684
3685#endif /* PSA_CRYPTO_H */