blob: bf7fd18fa8d9266508f546e6681914f58eb788a4 [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
itayzafrir18617092018-09-16 12:22:41 +0300645 * The library has not been previously initialized by psa_crypto_init().
646 * It is implementation-dependent whether a failure to initialize
647 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100648 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200649psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100650 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200651 size_t data_length,
652 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100653
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100654
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100655
656/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100657 * \brief Export a key in binary format.
658 *
659 * The output of this function can be passed to psa_import_key() to
660 * create an equivalent object.
661 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100662 * If the implementation of psa_import_key() supports other formats
663 * beyond the format specified here, the output from psa_export_key()
664 * must use the representation specified here, not the original
665 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100666 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100667 * For standard key types, the output format is as follows:
668 *
669 * - For symmetric keys (including MAC keys), the format is the
670 * raw bytes of the key.
671 * - For DES, the key data consists of 8 bytes. The parity bits must be
672 * correct.
673 * - For Triple-DES, the format is the concatenation of the
674 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200675 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200676 * is the non-encrypted DER encoding of the representation defined by
677 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
678 * ```
679 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200680 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200681 * modulus INTEGER, -- n
682 * publicExponent INTEGER, -- e
683 * privateExponent INTEGER, -- d
684 * prime1 INTEGER, -- p
685 * prime2 INTEGER, -- q
686 * exponent1 INTEGER, -- d mod (p-1)
687 * exponent2 INTEGER, -- d mod (q-1)
688 * coefficient INTEGER, -- (inverse of q) mod p
689 * }
690 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200691 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200692 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100693 * a representation of the private value as a `ceiling(m/8)`-byte string
694 * where `m` is the bit size associated with the curve, i.e. the bit size
695 * of the order of the curve's coordinate field. This byte string is
696 * in little-endian order for Montgomery curves (curve types
697 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
698 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
699 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100700 * This is the content of the `privateKey` field of the `ECPrivateKey`
701 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200702 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200703 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000704 * format is the representation of the private key `x` as a big-endian byte
705 * string. The length of the byte string is the private key size in bytes
706 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200707 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
708 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100709 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200710 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
711 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100712 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200713 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200714 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200715 * \param[out] data_length On success, the number of bytes
716 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100717 *
Gilles Peskine28538492018-07-11 17:34:00 +0200718 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100719 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200720 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200721 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100722 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200723 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
724 * The size of the \p data buffer is too small. You can determine a
725 * sufficient buffer size by calling
726 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
727 * where \c type is the key type
728 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200729 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
730 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200731 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100732 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100733 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300734 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300735 * The library has not been previously initialized by psa_crypto_init().
736 * It is implementation-dependent whether a failure to initialize
737 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100739psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 uint8_t *data,
741 size_t data_size,
742 size_t *data_length);
743
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100744/**
745 * \brief Export a public key or the public part of a key pair in binary format.
746 *
747 * The output of this function can be passed to psa_import_key() to
748 * create an object that is equivalent to the public key.
749 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000750 * This specification supports a single format for each key type.
751 * Implementations may support other formats as long as the standard
752 * format is supported. Implementations that support other formats
753 * should ensure that the formats are clearly unambiguous so as to
754 * minimize the risk that an invalid input is accidentally interpreted
755 * according to a different format.
756 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000757 * For standard key types, the output format is as follows:
758 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
759 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
760 * ```
761 * RSAPublicKey ::= SEQUENCE {
762 * modulus INTEGER, -- n
763 * publicExponent INTEGER } -- e
764 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000765 * - For elliptic curve public keys (key types for which
766 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
767 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
768 * Let `m` be the bit size associated with the curve, i.e. the bit size of
769 * `q` for a curve over `F_q`. The representation consists of:
770 * - The byte 0x04;
771 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
772 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200773 * - For Diffie-Hellman key exchange public keys (key types for which
774 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000775 * the format is the representation of the public key `y = g^x mod p` as a
776 * big-endian byte string. The length of the byte string is the length of the
777 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100778 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200779 * Exporting a public key object or the public part of a key pair is
780 * always permitted, regardless of the key's usage flags.
781 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100782 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200783 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200784 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200785 * \param[out] data_length On success, the number of bytes
786 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100787 *
Gilles Peskine28538492018-07-11 17:34:00 +0200788 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100789 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200790 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200791 * The key is neither a public key nor a key pair.
792 * \retval #PSA_ERROR_NOT_SUPPORTED
793 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
794 * The size of the \p data buffer is too small. You can determine a
795 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200796 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200797 * where \c type is the key type
798 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200799 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
800 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200801 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100802 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100803 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300804 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300805 * The library has not been previously initialized by psa_crypto_init().
806 * It is implementation-dependent whether a failure to initialize
807 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100808 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100809psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100810 uint8_t *data,
811 size_t data_size,
812 size_t *data_length);
813
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100814
Gilles Peskine20035e32018-02-03 22:44:14 +0100815
816/**@}*/
817
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100818/** \defgroup hash Message digests
819 * @{
820 */
821
Gilles Peskine69647a42019-01-14 20:18:12 +0100822/** Calculate the hash (digest) of a message.
823 *
824 * \note To verify the hash of a message against an
825 * expected value, use psa_hash_compare() instead.
826 *
827 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
828 * such that #PSA_ALG_IS_HASH(\p alg) is true).
829 * \param[in] input Buffer containing the message to hash.
830 * \param input_length Size of the \p input buffer in bytes.
831 * \param[out] hash Buffer where the hash is to be written.
832 * \param hash_size Size of the \p hash buffer in bytes.
833 * \param[out] hash_length On success, the number of bytes
834 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100835 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100836 *
837 * \retval #PSA_SUCCESS
838 * Success.
839 * \retval #PSA_ERROR_NOT_SUPPORTED
840 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100841 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100842 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
843 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100844 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
845 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
846 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200847 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100848 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100849 * \retval #PSA_ERROR_BAD_STATE
850 * The library has not been previously initialized by psa_crypto_init().
851 * It is implementation-dependent whether a failure to initialize
852 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100853 */
854psa_status_t psa_hash_compute(psa_algorithm_t alg,
855 const uint8_t *input,
856 size_t input_length,
857 uint8_t *hash,
858 size_t hash_size,
859 size_t *hash_length);
860
861/** Calculate the hash (digest) of a message and compare it with a
862 * reference value.
863 *
864 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
865 * such that #PSA_ALG_IS_HASH(\p alg) is true).
866 * \param[in] input Buffer containing the message to hash.
867 * \param input_length Size of the \p input buffer in bytes.
868 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100869 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100870 *
871 * \retval #PSA_SUCCESS
872 * The expected hash is identical to the actual hash of the input.
873 * \retval #PSA_ERROR_INVALID_SIGNATURE
874 * The hash of the message was calculated successfully, but it
875 * differs from the expected hash.
876 * \retval #PSA_ERROR_NOT_SUPPORTED
877 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100878 * \retval #PSA_ERROR_INVALID_ARGUMENT
879 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100880 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
881 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
882 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200883 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100884 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100885 * \retval #PSA_ERROR_BAD_STATE
886 * The library has not been previously initialized by psa_crypto_init().
887 * It is implementation-dependent whether a failure to initialize
888 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100889 */
890psa_status_t psa_hash_compare(psa_algorithm_t alg,
891 const uint8_t *input,
892 size_t input_length,
893 const uint8_t *hash,
894 const size_t hash_length);
895
Gilles Peskine308b91d2018-02-08 09:47:44 +0100896/** The type of the state data structure for multipart hash operations.
897 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000898 * Before calling any function on a hash operation object, the application must
899 * initialize it by any of the following means:
900 * - Set the structure to all-bits-zero, for example:
901 * \code
902 * psa_hash_operation_t operation;
903 * memset(&operation, 0, sizeof(operation));
904 * \endcode
905 * - Initialize the structure to logical zero values, for example:
906 * \code
907 * psa_hash_operation_t operation = {0};
908 * \endcode
909 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
910 * for example:
911 * \code
912 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
913 * \endcode
914 * - Assign the result of the function psa_hash_operation_init()
915 * to the structure, for example:
916 * \code
917 * psa_hash_operation_t operation;
918 * operation = psa_hash_operation_init();
919 * \endcode
920 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100921 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100922 * make any assumptions about the content of this structure except
923 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100924typedef struct psa_hash_operation_s psa_hash_operation_t;
925
Jaeden Amero6a25b412019-01-04 11:47:44 +0000926/** \def PSA_HASH_OPERATION_INIT
927 *
928 * This macro returns a suitable initializer for a hash operation object
929 * of type #psa_hash_operation_t.
930 */
931#ifdef __DOXYGEN_ONLY__
932/* This is an example definition for documentation purposes.
933 * Implementations should define a suitable value in `crypto_struct.h`.
934 */
935#define PSA_HASH_OPERATION_INIT {0}
936#endif
937
938/** Return an initial value for a hash operation object.
939 */
940static psa_hash_operation_t psa_hash_operation_init(void);
941
Gilles Peskinef45adda2019-01-14 18:29:18 +0100942/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100943 *
944 * The sequence of operations to calculate a hash (message digest)
945 * is as follows:
946 * -# Allocate an operation object which will be passed to all the functions
947 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000948 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100949 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200950 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100951 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100952 * of the message each time. The hash that is calculated is the hash
953 * of the concatenation of these messages in order.
954 * -# To calculate the hash, call psa_hash_finish().
955 * To compare the hash with an expected value, call psa_hash_verify().
956 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100957 * If an error occurs at any step after a call to psa_hash_setup(), the
958 * operation will need to be reset by a call to psa_hash_abort(). The
959 * 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:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100965 * - A successful call to psa_hash_finish() or psa_hash_verify().
966 * - A call to 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
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100981 * The operation state is not valid (it must be inactive).
Gilles Peskine28538492018-07-11 17:34:00 +0200982 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
983 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
984 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200985 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100986 * \retval #PSA_ERROR_BAD_STATE
987 * The library has not been previously initialized by psa_crypto_init().
988 * It is implementation-dependent whether a failure to initialize
989 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100990 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200991psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100992 psa_algorithm_t alg);
993
Gilles Peskine308b91d2018-02-08 09:47:44 +0100994/** Add a message fragment to a multipart hash operation.
995 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200996 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100997 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100998 * If this function returns an error status, the operation enters an error
999 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001000 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001001 * \param[in,out] operation Active hash operation.
1002 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001003 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001004 *
Gilles Peskine28538492018-07-11 17:34:00 +02001005 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001006 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001007 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001008 * The operation state is not valid (it muct be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001009 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1010 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1011 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001012 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001013 * \retval #PSA_ERROR_BAD_STATE
1014 * The library has not been previously initialized by psa_crypto_init().
1015 * It is implementation-dependent whether a failure to initialize
1016 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001017 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001018psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1019 const uint8_t *input,
1020 size_t input_length);
1021
Gilles Peskine308b91d2018-02-08 09:47:44 +01001022/** Finish the calculation of the hash of a message.
1023 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001024 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001025 * This function calculates the hash of the message formed by concatenating
1026 * the inputs passed to preceding calls to psa_hash_update().
1027 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001028 * When this function returns successfuly, the operation becomes inactive.
1029 * If this function returns an error status, the operation enters an error
1030 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001031 *
1032 * \warning Applications should not call this function if they expect
1033 * a specific value for the hash. Call psa_hash_verify() instead.
1034 * Beware that comparing integrity or authenticity data such as
1035 * hash values with a function such as \c memcmp is risky
1036 * because the time taken by the comparison may leak information
1037 * about the hashed data which could allow an attacker to guess
1038 * a valid hash and thereby bypass security controls.
1039 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001040 * \param[in,out] operation Active hash operation.
1041 * \param[out] hash Buffer where the hash is to be written.
1042 * \param hash_size Size of the \p hash buffer in bytes.
1043 * \param[out] hash_length On success, the number of bytes
1044 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001045 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001046 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001047 *
Gilles Peskine28538492018-07-11 17:34:00 +02001048 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001049 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001050 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001051 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001052 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001053 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001054 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001055 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001056 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1057 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1058 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001059 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001060 * \retval #PSA_ERROR_BAD_STATE
1061 * The library has not been previously initialized by psa_crypto_init().
1062 * It is implementation-dependent whether a failure to initialize
1063 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001064 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001065psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1066 uint8_t *hash,
1067 size_t hash_size,
1068 size_t *hash_length);
1069
Gilles Peskine308b91d2018-02-08 09:47:44 +01001070/** Finish the calculation of the hash of a message and compare it with
1071 * an expected value.
1072 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001073 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074 * This function calculates the hash of the message formed by concatenating
1075 * the inputs passed to preceding calls to psa_hash_update(). It then
1076 * compares the calculated hash with the expected hash passed as a
1077 * parameter to this function.
1078 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001079 * When this function returns successfuly, the operation becomes inactive.
1080 * If this function returns an error status, the operation enters an error
1081 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001082 *
Gilles Peskine19067982018-03-20 17:54:53 +01001083 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001084 * comparison between the actual hash and the expected hash is performed
1085 * in constant time.
1086 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001087 * \param[in,out] operation Active hash operation.
1088 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001089 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001090 *
Gilles Peskine28538492018-07-11 17:34:00 +02001091 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001092 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001093 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001094 * The hash of the message was calculated successfully, but it
1095 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001096 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001097 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001098 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1099 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1100 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001101 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001102 * \retval #PSA_ERROR_BAD_STATE
1103 * The library has not been previously initialized by psa_crypto_init().
1104 * It is implementation-dependent whether a failure to initialize
1105 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001106 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001107psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1108 const uint8_t *hash,
1109 size_t hash_length);
1110
Gilles Peskine308b91d2018-02-08 09:47:44 +01001111/** Abort a hash operation.
1112 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001113 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001114 * \p operation structure itself. Once aborted, the operation object
1115 * can be reused for another operation by calling
1116 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001117 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001118 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001119 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001120 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001121 * In particular, calling psa_hash_abort() after the operation has been
1122 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1123 * psa_hash_verify() is safe and has no effect.
1124 *
1125 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001126 *
Gilles Peskine28538492018-07-11 17:34:00 +02001127 * \retval #PSA_SUCCESS
Gilles Peskine28538492018-07-11 17:34:00 +02001128 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1129 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001130 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001131 * \retval #PSA_ERROR_BAD_STATE
1132 * The library has not been previously initialized by psa_crypto_init().
1133 * It is implementation-dependent whether a failure to initialize
1134 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001135 */
1136psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001137
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001138/** Clone a hash operation.
1139 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001140 * This function copies the state of an ongoing hash operation to
1141 * a new operation object. In other words, this function is equivalent
1142 * to calling psa_hash_setup() on \p target_operation with the same
1143 * algorithm that \p source_operation was set up for, then
1144 * psa_hash_update() on \p target_operation with the same input that
1145 * that was passed to \p source_operation. After this function returns, the
1146 * two objects are independent, i.e. subsequent calls involving one of
1147 * the objects do not affect the other object.
1148 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001149 * \param[in] source_operation The active hash operation to clone.
1150 * \param[in,out] target_operation The operation object to set up.
1151 * It must be initialized but not active.
1152 *
1153 * \retval #PSA_SUCCESS
1154 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001155 * The \p source_operation state is not valid (it must be active).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001156 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001157 * The \p target_operation state is not valid (it must be inactive).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001158 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1159 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001160 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001161 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001162 * \retval #PSA_ERROR_BAD_STATE
1163 * The library has not been previously initialized by psa_crypto_init().
1164 * It is implementation-dependent whether a failure to initialize
1165 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001166 */
1167psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1168 psa_hash_operation_t *target_operation);
1169
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001170/**@}*/
1171
Gilles Peskine8c9def32018-02-08 10:02:12 +01001172/** \defgroup MAC Message authentication codes
1173 * @{
1174 */
1175
Gilles Peskine69647a42019-01-14 20:18:12 +01001176/** Calculate the MAC (message authentication code) of a message.
1177 *
1178 * \note To verify the MAC of a message against an
1179 * expected value, use psa_mac_verify() instead.
1180 * Beware that comparing integrity or authenticity data such as
1181 * MAC values with a function such as \c memcmp is risky
1182 * because the time taken by the comparison may leak information
1183 * about the MAC value which could allow an attacker to guess
1184 * a valid MAC and thereby bypass security controls.
1185 *
1186 * \param handle Handle to the key to use for the operation.
1187 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001188 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001189 * \param[in] input Buffer containing the input message.
1190 * \param input_length Size of the \p input buffer in bytes.
1191 * \param[out] mac Buffer where the MAC value is to be written.
1192 * \param mac_size Size of the \p mac buffer in bytes.
1193 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001194 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001195 *
1196 * \retval #PSA_SUCCESS
1197 * Success.
1198 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001199 * \retval #PSA_ERROR_NOT_PERMITTED
1200 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001201 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001202 * \retval #PSA_ERROR_NOT_SUPPORTED
1203 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001204 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1205 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001206 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1207 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1208 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001209 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001210 * \retval #PSA_ERROR_STORAGE_FAILURE
1211 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001212 * \retval #PSA_ERROR_BAD_STATE
1213 * The library has not been previously initialized by psa_crypto_init().
1214 * It is implementation-dependent whether a failure to initialize
1215 * results in this error code.
1216 */
1217psa_status_t psa_mac_compute(psa_key_handle_t handle,
1218 psa_algorithm_t alg,
1219 const uint8_t *input,
1220 size_t input_length,
1221 uint8_t *mac,
1222 size_t mac_size,
1223 size_t *mac_length);
1224
1225/** Calculate the MAC of a message and compare it with a reference value.
1226 *
1227 * \param handle Handle to the key to use for the operation.
1228 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001229 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001230 * \param[in] input Buffer containing the input message.
1231 * \param input_length Size of the \p input buffer in bytes.
1232 * \param[out] mac Buffer containing the expected MAC value.
1233 * \param mac_length Size of the \p mac buffer in bytes.
1234 *
1235 * \retval #PSA_SUCCESS
1236 * The expected MAC is identical to the actual MAC of the input.
1237 * \retval #PSA_ERROR_INVALID_SIGNATURE
1238 * The MAC of the message was calculated successfully, but it
1239 * differs from the expected value.
1240 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001241 * \retval #PSA_ERROR_NOT_PERMITTED
1242 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001243 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001244 * \retval #PSA_ERROR_NOT_SUPPORTED
1245 * \p alg is not supported or is not a MAC algorithm.
1246 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1247 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1248 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001249 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001250 * \retval #PSA_ERROR_STORAGE_FAILURE
1251 * The key could not be retrieved from storage.
1252 * \retval #PSA_ERROR_BAD_STATE
1253 * The library has not been previously initialized by psa_crypto_init().
1254 * It is implementation-dependent whether a failure to initialize
1255 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001256 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001257psa_status_t psa_mac_verify(psa_key_handle_t handle,
1258 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001259 const uint8_t *input,
1260 size_t input_length,
1261 const uint8_t *mac,
1262 const size_t mac_length);
1263
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001264/** The type of the state data structure for multipart MAC operations.
1265 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001266 * Before calling any function on a MAC operation object, the application must
1267 * initialize it by any of the following means:
1268 * - Set the structure to all-bits-zero, for example:
1269 * \code
1270 * psa_mac_operation_t operation;
1271 * memset(&operation, 0, sizeof(operation));
1272 * \endcode
1273 * - Initialize the structure to logical zero values, for example:
1274 * \code
1275 * psa_mac_operation_t operation = {0};
1276 * \endcode
1277 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1278 * for example:
1279 * \code
1280 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1281 * \endcode
1282 * - Assign the result of the function psa_mac_operation_init()
1283 * to the structure, for example:
1284 * \code
1285 * psa_mac_operation_t operation;
1286 * operation = psa_mac_operation_init();
1287 * \endcode
1288 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001289 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001290 * make any assumptions about the content of this structure except
1291 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001292typedef struct psa_mac_operation_s psa_mac_operation_t;
1293
Jaeden Amero769ce272019-01-04 11:48:03 +00001294/** \def PSA_MAC_OPERATION_INIT
1295 *
1296 * This macro returns a suitable initializer for a MAC operation object of type
1297 * #psa_mac_operation_t.
1298 */
1299#ifdef __DOXYGEN_ONLY__
1300/* This is an example definition for documentation purposes.
1301 * Implementations should define a suitable value in `crypto_struct.h`.
1302 */
1303#define PSA_MAC_OPERATION_INIT {0}
1304#endif
1305
1306/** Return an initial value for a MAC operation object.
1307 */
1308static psa_mac_operation_t psa_mac_operation_init(void);
1309
Gilles Peskinef45adda2019-01-14 18:29:18 +01001310/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001311 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001312 * This function sets up the calculation of the MAC
1313 * (message authentication code) of a byte string.
1314 * To verify the MAC of a message against an
1315 * expected value, use psa_mac_verify_setup() instead.
1316 *
1317 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001318 * -# Allocate an operation object which will be passed to all the functions
1319 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001320 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001321 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001322 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001323 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1324 * of the message each time. The MAC that is calculated is the MAC
1325 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001326 * -# At the end of the message, call psa_mac_sign_finish() to finish
1327 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001328 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001329 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1330 * operation will need to be reset by a call to psa_mac_abort(). The
1331 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001332 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001333 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001334 * After a successful call to psa_mac_sign_setup(), the application must
1335 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001336 * - A successful call to psa_mac_sign_finish().
1337 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001338 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001339 * \param[in,out] operation The operation object to set up. It must have
1340 * been initialized as per the documentation for
1341 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001342 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001343 * It must remain valid until the operation
1344 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001345 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001346 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001347 *
Gilles Peskine28538492018-07-11 17:34:00 +02001348 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001349 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001350 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001351 * \retval #PSA_ERROR_NOT_PERMITTED
1352 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001353 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001354 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001355 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001356 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1357 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1358 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001359 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001360 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001361 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001362 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001363 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001364 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001365 * The library has not been previously initialized by psa_crypto_init().
1366 * It is implementation-dependent whether a failure to initialize
1367 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001368 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001369psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001370 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001371 psa_algorithm_t alg);
1372
Gilles Peskinef45adda2019-01-14 18:29:18 +01001373/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001374 *
1375 * This function sets up the verification of the MAC
1376 * (message authentication code) of a byte string against an expected value.
1377 *
1378 * The sequence of operations to verify a MAC is as follows:
1379 * -# Allocate an operation object which will be passed to all the functions
1380 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001381 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001382 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001383 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001384 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1385 * of the message each time. The MAC that is calculated is the MAC
1386 * of the concatenation of these messages in order.
1387 * -# At the end of the message, call psa_mac_verify_finish() to finish
1388 * calculating the actual MAC of the message and verify it against
1389 * the expected value.
1390 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001391 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1392 * operation will need to be reset by a call to psa_mac_abort(). The
1393 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001394 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001395 *
1396 * After a successful call to psa_mac_verify_setup(), the application must
1397 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001398 * - A successful call to psa_mac_verify_finish().
1399 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001400 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001401 * \param[in,out] operation The operation object to set up. It must have
1402 * been initialized as per the documentation for
1403 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001404 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001405 * It must remain valid until the operation
1406 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001407 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1408 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001409 *
Gilles Peskine28538492018-07-11 17:34:00 +02001410 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001411 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001412 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001413 * \retval #PSA_ERROR_NOT_PERMITTED
1414 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001415 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001416 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001417 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001418 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1419 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1420 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001421 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001422 * \retval #PSA_ERROR_STORAGE_FAILURE
1423 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001424 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001425 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001426 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001427 * The library has not been previously initialized by psa_crypto_init().
1428 * It is implementation-dependent whether a failure to initialize
1429 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001430 */
1431psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001432 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001433 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001434
Gilles Peskinedcd14942018-07-12 00:30:52 +02001435/** Add a message fragment to a multipart MAC operation.
1436 *
1437 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1438 * before calling this function.
1439 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001440 * If this function returns an error status, the operation enters an error
1441 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001442 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001443 * \param[in,out] operation Active MAC operation.
1444 * \param[in] input Buffer containing the message fragment to add to
1445 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001446 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001447 *
1448 * \retval #PSA_SUCCESS
1449 * Success.
1450 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001451 * The operation state is not valid (it must be active).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001452 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1453 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1454 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001455 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001456 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001457 * \retval #PSA_ERROR_BAD_STATE
1458 * The library has not been previously initialized by psa_crypto_init().
1459 * It is implementation-dependent whether a failure to initialize
1460 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001461 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001462psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1463 const uint8_t *input,
1464 size_t input_length);
1465
Gilles Peskinedcd14942018-07-12 00:30:52 +02001466/** Finish the calculation of the MAC of a message.
1467 *
1468 * The application must call psa_mac_sign_setup() before calling this function.
1469 * This function calculates the MAC of the message formed by concatenating
1470 * the inputs passed to preceding calls to psa_mac_update().
1471 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001472 * When this function returns successfuly, the operation becomes inactive.
1473 * If this function returns an error status, the operation enters an error
1474 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001475 *
1476 * \warning Applications should not call this function if they expect
1477 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1478 * Beware that comparing integrity or authenticity data such as
1479 * MAC values with a function such as \c memcmp is risky
1480 * because the time taken by the comparison may leak information
1481 * about the MAC value which could allow an attacker to guess
1482 * a valid MAC and thereby bypass security controls.
1483 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001484 * \param[in,out] operation Active MAC operation.
1485 * \param[out] mac Buffer where the MAC value is to be written.
1486 * \param mac_size Size of the \p mac buffer in bytes.
1487 * \param[out] mac_length On success, the number of bytes
1488 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001489 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001490 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001491 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001492 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001493 *
1494 * \retval #PSA_SUCCESS
1495 * Success.
1496 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001497 * The operation state is not valid (it must be an active mac sign
1498 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001499 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001500 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001501 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1502 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1503 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1504 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001505 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001506 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001507 * \retval #PSA_ERROR_BAD_STATE
1508 * The library has not been previously initialized by psa_crypto_init().
1509 * It is implementation-dependent whether a failure to initialize
1510 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001511 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001512psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1513 uint8_t *mac,
1514 size_t mac_size,
1515 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001516
Gilles Peskinedcd14942018-07-12 00:30:52 +02001517/** Finish the calculation of the MAC of a message and compare it with
1518 * an expected value.
1519 *
1520 * The application must call psa_mac_verify_setup() before calling this function.
1521 * This function calculates the MAC of the message formed by concatenating
1522 * the inputs passed to preceding calls to psa_mac_update(). It then
1523 * compares the calculated MAC with the expected MAC passed as a
1524 * parameter to this function.
1525 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001526 * When this function returns successfuly, the operation becomes inactive.
1527 * If this function returns an error status, the operation enters an error
1528 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001529 *
1530 * \note Implementations shall make the best effort to ensure that the
1531 * comparison between the actual MAC and the expected MAC is performed
1532 * in constant time.
1533 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001534 * \param[in,out] operation Active MAC operation.
1535 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001536 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001537 *
1538 * \retval #PSA_SUCCESS
1539 * The expected MAC is identical to the actual MAC of the message.
1540 * \retval #PSA_ERROR_INVALID_SIGNATURE
1541 * The MAC of the message was calculated successfully, but it
1542 * differs from the expected MAC.
1543 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001544 * The operation state is not valid (it must be an active mac verify
1545 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001546 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1547 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1548 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001549 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001550 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001551 * \retval #PSA_ERROR_BAD_STATE
1552 * The library has not been previously initialized by psa_crypto_init().
1553 * It is implementation-dependent whether a failure to initialize
1554 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001555 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001556psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1557 const uint8_t *mac,
1558 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001559
Gilles Peskinedcd14942018-07-12 00:30:52 +02001560/** Abort a MAC operation.
1561 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001562 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001563 * \p operation structure itself. Once aborted, the operation object
1564 * can be reused for another operation by calling
1565 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001566 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001567 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001568 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001569 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001570 * In particular, calling psa_mac_abort() after the operation has been
1571 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1572 * psa_mac_verify_finish() is safe and has no effect.
1573 *
1574 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001575 *
1576 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02001577 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1578 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001579 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001580 * \retval #PSA_ERROR_BAD_STATE
1581 * The library has not been previously initialized by psa_crypto_init().
1582 * It is implementation-dependent whether a failure to initialize
1583 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001584 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001585psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1586
1587/**@}*/
1588
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001589/** \defgroup cipher Symmetric ciphers
1590 * @{
1591 */
1592
Gilles Peskine69647a42019-01-14 20:18:12 +01001593/** Encrypt a message using a symmetric cipher.
1594 *
1595 * This function encrypts a message with a random IV (initialization
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001596 * vector). Use the multipart #psa_cipher_operation_t object to provide
1597 * other foms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001598 *
1599 * \param handle Handle to the key to use for the operation.
1600 * It must remain valid until the operation
1601 * terminates.
1602 * \param alg The cipher algorithm to compute
1603 * (\c PSA_ALG_XXX value such that
1604 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1605 * \param[in] input Buffer containing the message to encrypt.
1606 * \param input_length Size of the \p input buffer in bytes.
1607 * \param[out] output Buffer where the output is to be written.
1608 * The output contains the IV followed by
1609 * the ciphertext proper.
1610 * \param output_size Size of the \p output buffer in bytes.
1611 * \param[out] output_length On success, the number of bytes
1612 * that make up the output.
1613 *
1614 * \retval #PSA_SUCCESS
1615 * Success.
1616 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001617 * \retval #PSA_ERROR_NOT_PERMITTED
1618 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001619 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001620 * \retval #PSA_ERROR_NOT_SUPPORTED
1621 * \p alg is not supported or is not a cipher algorithm.
1622 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1623 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1624 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1625 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001626 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001627 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001628 * \retval #PSA_ERROR_BAD_STATE
1629 * The library has not been previously initialized by psa_crypto_init().
1630 * It is implementation-dependent whether a failure to initialize
1631 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001632 */
1633psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1634 psa_algorithm_t alg,
1635 const uint8_t *input,
1636 size_t input_length,
1637 uint8_t *output,
1638 size_t output_size,
1639 size_t *output_length);
1640
1641/** Decrypt a message using a symmetric cipher.
1642 *
1643 * This function decrypts a message encrypted with a symmetric cipher.
1644 *
1645 * \param handle Handle to the key to use for the operation.
1646 * It must remain valid until the operation
1647 * terminates.
1648 * \param alg The cipher algorithm to compute
1649 * (\c PSA_ALG_XXX value such that
1650 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1651 * \param[in] input Buffer containing the message to decrypt.
1652 * This consists of the IV followed by the
1653 * ciphertext proper.
1654 * \param input_length Size of the \p input buffer in bytes.
1655 * \param[out] output Buffer where the plaintext is to be written.
1656 * \param output_size Size of the \p output buffer in bytes.
1657 * \param[out] output_length On success, the number of bytes
1658 * that make up the output.
1659 *
1660 * \retval #PSA_SUCCESS
1661 * Success.
1662 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001663 * \retval #PSA_ERROR_NOT_PERMITTED
1664 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001665 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001666 * \retval #PSA_ERROR_NOT_SUPPORTED
1667 * \p alg is not supported or is not a cipher algorithm.
1668 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1669 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1670 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1671 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001672 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001673 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001674 * \retval #PSA_ERROR_BAD_STATE
1675 * The library has not been previously initialized by psa_crypto_init().
1676 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001677 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001678 */
1679psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1680 psa_algorithm_t alg,
1681 const uint8_t *input,
1682 size_t input_length,
1683 uint8_t *output,
1684 size_t output_size,
1685 size_t *output_length);
1686
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001687/** The type of the state data structure for multipart cipher operations.
1688 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001689 * Before calling any function on a cipher operation object, the application
1690 * must initialize it by any of the following means:
1691 * - Set the structure to all-bits-zero, for example:
1692 * \code
1693 * psa_cipher_operation_t operation;
1694 * memset(&operation, 0, sizeof(operation));
1695 * \endcode
1696 * - Initialize the structure to logical zero values, for example:
1697 * \code
1698 * psa_cipher_operation_t operation = {0};
1699 * \endcode
1700 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1701 * for example:
1702 * \code
1703 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1704 * \endcode
1705 * - Assign the result of the function psa_cipher_operation_init()
1706 * to the structure, for example:
1707 * \code
1708 * psa_cipher_operation_t operation;
1709 * operation = psa_cipher_operation_init();
1710 * \endcode
1711 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001712 * This is an implementation-defined \c struct. Applications should not
1713 * make any assumptions about the content of this structure except
1714 * as directed by the documentation of a specific implementation. */
1715typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1716
Jaeden Amero5bae2272019-01-04 11:48:27 +00001717/** \def PSA_CIPHER_OPERATION_INIT
1718 *
1719 * This macro returns a suitable initializer for a cipher operation object of
1720 * type #psa_cipher_operation_t.
1721 */
1722#ifdef __DOXYGEN_ONLY__
1723/* This is an example definition for documentation purposes.
1724 * Implementations should define a suitable value in `crypto_struct.h`.
1725 */
1726#define PSA_CIPHER_OPERATION_INIT {0}
1727#endif
1728
1729/** Return an initial value for a cipher operation object.
1730 */
1731static psa_cipher_operation_t psa_cipher_operation_init(void);
1732
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001733/** Set the key for a multipart symmetric encryption operation.
1734 *
1735 * The sequence of operations to encrypt a message with a symmetric cipher
1736 * is as follows:
1737 * -# Allocate an operation object which will be passed to all the functions
1738 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001739 * -# Initialize the operation object with one of the methods described in the
1740 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001741 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001742 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001743 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001744 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001745 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001746 * requires a specific IV value.
1747 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1748 * of the message each time.
1749 * -# Call psa_cipher_finish().
1750 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001751 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1752 * the operation will need to be reset by a call to psa_cipher_abort(). The
1753 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001754 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001755 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001756 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001757 * eventually terminate the operation. The following events terminate an
1758 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001759 * - A successful call to psa_cipher_finish().
1760 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001761 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001762 * \param[in,out] operation The operation object to set up. It must have
1763 * been initialized as per the documentation for
1764 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001765 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001766 * It must remain valid until the operation
1767 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001768 * \param alg The cipher algorithm to compute
1769 * (\c PSA_ALG_XXX value such that
1770 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001771 *
Gilles Peskine28538492018-07-11 17:34:00 +02001772 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001773 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001774 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001775 * \retval #PSA_ERROR_NOT_PERMITTED
1776 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001777 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001778 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001779 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001780 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1781 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1782 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001783 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001784 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001785 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001786 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001787 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001788 * The library has not been previously initialized by psa_crypto_init().
1789 * It is implementation-dependent whether a failure to initialize
1790 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001791 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001792psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001793 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001794 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001795
1796/** Set the key for a multipart symmetric decryption operation.
1797 *
1798 * The sequence of operations to decrypt a message with a symmetric cipher
1799 * is as follows:
1800 * -# Allocate an operation object which will be passed to all the functions
1801 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001802 * -# Initialize the operation object with one of the methods described in the
1803 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001804 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001805 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001806 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001807 * decryption. If the IV is prepended to the ciphertext, you can call
1808 * psa_cipher_update() on a buffer containing the IV followed by the
1809 * beginning of the message.
1810 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1811 * of the message each time.
1812 * -# Call psa_cipher_finish().
1813 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001814 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1815 * the operation will need to be reset by a call to psa_cipher_abort(). The
1816 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001817 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001818 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001819 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001820 * eventually terminate the operation. The following events terminate an
1821 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001822 * - A successful call to psa_cipher_finish().
1823 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001824 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001825 * \param[in,out] operation The operation object to set up. It must have
1826 * been initialized as per the documentation for
1827 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001828 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001829 * It must remain valid until the operation
1830 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001831 * \param alg The cipher algorithm to compute
1832 * (\c PSA_ALG_XXX value such that
1833 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001834 *
Gilles Peskine28538492018-07-11 17:34:00 +02001835 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001836 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001837 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001838 * \retval #PSA_ERROR_NOT_PERMITTED
1839 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001840 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001841 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001842 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001843 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1844 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1845 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001846 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001847 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001848 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001849 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001850 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001851 * The library has not been previously initialized by psa_crypto_init().
1852 * It is implementation-dependent whether a failure to initialize
1853 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001854 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001855psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001856 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001857 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001858
Gilles Peskinedcd14942018-07-12 00:30:52 +02001859/** Generate an IV for a symmetric encryption operation.
1860 *
1861 * This function generates a random IV (initialization vector), nonce
1862 * or initial counter value for the encryption operation as appropriate
1863 * for the chosen algorithm, key type and key size.
1864 *
1865 * The application must call psa_cipher_encrypt_setup() before
1866 * calling this function.
1867 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001868 * If this function returns an error status, the operation enters an error
1869 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001870 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001871 * \param[in,out] operation Active cipher operation.
1872 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001873 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001874 * \param[out] iv_length On success, the number of bytes of the
1875 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001876 *
1877 * \retval #PSA_SUCCESS
1878 * Success.
1879 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001880 * The operation state is not valid (it must be active, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001881 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001882 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001883 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1884 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1885 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001886 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001887 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001888 * \retval #PSA_ERROR_BAD_STATE
1889 * The library has not been previously initialized by psa_crypto_init().
1890 * It is implementation-dependent whether a failure to initialize
1891 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001892 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001893psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001894 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001895 size_t iv_size,
1896 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001897
Gilles Peskinedcd14942018-07-12 00:30:52 +02001898/** Set the IV for a symmetric encryption or decryption operation.
1899 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001900 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001901 * or initial counter value for the encryption or decryption operation.
1902 *
1903 * The application must call psa_cipher_encrypt_setup() before
1904 * calling this function.
1905 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001906 * If this function returns an error status, the operation enters an error
1907 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001908 *
1909 * \note When encrypting, applications should use psa_cipher_generate_iv()
1910 * instead of this function, unless implementing a protocol that requires
1911 * a non-random IV.
1912 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001913 * \param[in,out] operation Active cipher operation.
1914 * \param[in] iv Buffer containing the IV to use.
1915 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001916 *
1917 * \retval #PSA_SUCCESS
1918 * Success.
1919 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001920 * The operation state is not valid (it must be an active cipher
1921 * encrypt operation, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001922 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001923 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001924 * or the chosen algorithm does not use an IV.
1925 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1926 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1927 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001928 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001929 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001930 * \retval #PSA_ERROR_BAD_STATE
1931 * The library has not been previously initialized by psa_crypto_init().
1932 * It is implementation-dependent whether a failure to initialize
1933 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001934 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001935psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001936 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001937 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001938
Gilles Peskinedcd14942018-07-12 00:30:52 +02001939/** Encrypt or decrypt a message fragment in an active cipher operation.
1940 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001941 * Before calling this function, you must:
1942 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1943 * The choice of setup function determines whether this function
1944 * encrypts or decrypts its input.
1945 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1946 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001947 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001948 * If this function returns an error status, the operation enters an error
1949 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001950 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001951 * \param[in,out] operation Active cipher operation.
1952 * \param[in] input Buffer containing the message fragment to
1953 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001954 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001955 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001956 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001957 * \param[out] output_length On success, the number of bytes
1958 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001959 *
1960 * \retval #PSA_SUCCESS
1961 * Success.
1962 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001963 * The operation state is not valid (it must be active, with an IV set
1964 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001965 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1966 * The size of the \p output buffer is too small.
1967 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1968 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1969 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001970 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001971 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001972 * \retval #PSA_ERROR_BAD_STATE
1973 * The library has not been previously initialized by psa_crypto_init().
1974 * It is implementation-dependent whether a failure to initialize
1975 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001976 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001977psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1978 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001979 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001980 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001981 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001982 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001983
Gilles Peskinedcd14942018-07-12 00:30:52 +02001984/** Finish encrypting or decrypting a message in a cipher operation.
1985 *
1986 * The application must call psa_cipher_encrypt_setup() or
1987 * psa_cipher_decrypt_setup() before calling this function. The choice
1988 * of setup function determines whether this function encrypts or
1989 * decrypts its input.
1990 *
1991 * This function finishes the encryption or decryption of the message
1992 * formed by concatenating the inputs passed to preceding calls to
1993 * psa_cipher_update().
1994 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001995 * When this function returns successfuly, the operation becomes inactive.
1996 * If this function returns an error status, the operation enters an error
1997 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001998 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001999 * \param[in,out] operation Active cipher operation.
2000 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002001 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002002 * \param[out] output_length On success, the number of bytes
2003 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002004 *
2005 * \retval #PSA_SUCCESS
2006 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02002007 * \retval #PSA_ERROR_INVALID_ARGUMENT
2008 * The total input size passed to this operation is not valid for
2009 * this particular algorithm. For example, the algorithm is a based
2010 * on block cipher and requires a whole number of blocks, but the
2011 * total input size is not a multiple of the block size.
2012 * \retval #PSA_ERROR_INVALID_PADDING
2013 * This is a decryption operation for an algorithm that includes
2014 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002015 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002016 * The operation state is not valid (it must be active, with an IV set
2017 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02002018 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2019 * The size of the \p output buffer is too small.
2020 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2021 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2022 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002023 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002024 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002025 * \retval #PSA_ERROR_BAD_STATE
2026 * The library has not been previously initialized by psa_crypto_init().
2027 * It is implementation-dependent whether a failure to initialize
2028 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002029 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002030psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002031 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002032 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002033 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002034
Gilles Peskinedcd14942018-07-12 00:30:52 +02002035/** Abort a cipher operation.
2036 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002037 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002038 * \p operation structure itself. Once aborted, the operation object
2039 * can be reused for another operation by calling
2040 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002041 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002042 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002043 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002044 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002045 * In particular, calling psa_cipher_abort() after the operation has been
2046 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2047 * is safe and has no effect.
2048 *
2049 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002050 *
2051 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02002052 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2053 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002054 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002055 * \retval #PSA_ERROR_BAD_STATE
2056 * The library has not been previously initialized by psa_crypto_init().
2057 * It is implementation-dependent whether a failure to initialize
2058 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002059 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002060psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2061
2062/**@}*/
2063
Gilles Peskine3b555712018-03-03 21:27:57 +01002064/** \defgroup aead Authenticated encryption with associated data (AEAD)
2065 * @{
2066 */
2067
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002068/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002069 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002070 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002071 * \param alg The AEAD algorithm to compute
2072 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002073 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002074 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002075 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002076 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002077 * but not encrypted.
2078 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002079 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002080 * encrypted.
2081 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002082 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002083 * encrypted data. The additional data is not
2084 * part of this output. For algorithms where the
2085 * encrypted data and the authentication tag
2086 * are defined as separate outputs, the
2087 * authentication tag is appended to the
2088 * encrypted data.
2089 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2090 * This must be at least
2091 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2092 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002093 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002094 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002095 *
Gilles Peskine28538492018-07-11 17:34:00 +02002096 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002097 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002098 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002099 * \retval #PSA_ERROR_NOT_PERMITTED
2100 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002101 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002102 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002103 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002104 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002105 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2106 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002107 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2108 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002109 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002110 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002111 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002112 * The library has not been previously initialized by psa_crypto_init().
2113 * It is implementation-dependent whether a failure to initialize
2114 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002115 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002116psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002117 psa_algorithm_t alg,
2118 const uint8_t *nonce,
2119 size_t nonce_length,
2120 const uint8_t *additional_data,
2121 size_t additional_data_length,
2122 const uint8_t *plaintext,
2123 size_t plaintext_length,
2124 uint8_t *ciphertext,
2125 size_t ciphertext_size,
2126 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002127
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002128/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002129 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002130 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002131 * \param alg The AEAD algorithm to compute
2132 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002133 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002134 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002135 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002136 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002137 * but not encrypted.
2138 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002139 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002140 * encrypted. For algorithms where the
2141 * encrypted data and the authentication tag
2142 * are defined as separate inputs, the buffer
2143 * must contain the encrypted data followed
2144 * by the authentication tag.
2145 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002146 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002147 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2148 * This must be at least
2149 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2150 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002151 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002152 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002153 *
Gilles Peskine28538492018-07-11 17:34:00 +02002154 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002155 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002156 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002157 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002158 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002159 * \retval #PSA_ERROR_NOT_PERMITTED
2160 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002161 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002162 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002163 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002164 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002165 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2166 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002167 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2168 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002169 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002170 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002171 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002172 * The library has not been previously initialized by psa_crypto_init().
2173 * It is implementation-dependent whether a failure to initialize
2174 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002175 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002176psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002177 psa_algorithm_t alg,
2178 const uint8_t *nonce,
2179 size_t nonce_length,
2180 const uint8_t *additional_data,
2181 size_t additional_data_length,
2182 const uint8_t *ciphertext,
2183 size_t ciphertext_length,
2184 uint8_t *plaintext,
2185 size_t plaintext_size,
2186 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002187
Gilles Peskine30a9e412019-01-14 18:36:12 +01002188/** The type of the state data structure for multipart AEAD operations.
2189 *
2190 * Before calling any function on an AEAD operation object, the application
2191 * must initialize it by any of the following means:
2192 * - Set the structure to all-bits-zero, for example:
2193 * \code
2194 * psa_aead_operation_t operation;
2195 * memset(&operation, 0, sizeof(operation));
2196 * \endcode
2197 * - Initialize the structure to logical zero values, for example:
2198 * \code
2199 * psa_aead_operation_t operation = {0};
2200 * \endcode
2201 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2202 * for example:
2203 * \code
2204 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2205 * \endcode
2206 * - Assign the result of the function psa_aead_operation_init()
2207 * to the structure, for example:
2208 * \code
2209 * psa_aead_operation_t operation;
2210 * operation = psa_aead_operation_init();
2211 * \endcode
2212 *
2213 * This is an implementation-defined \c struct. Applications should not
2214 * make any assumptions about the content of this structure except
2215 * as directed by the documentation of a specific implementation. */
2216typedef struct psa_aead_operation_s psa_aead_operation_t;
2217
2218/** \def PSA_AEAD_OPERATION_INIT
2219 *
2220 * This macro returns a suitable initializer for an AEAD operation object of
2221 * type #psa_aead_operation_t.
2222 */
2223#ifdef __DOXYGEN_ONLY__
2224/* This is an example definition for documentation purposes.
2225 * Implementations should define a suitable value in `crypto_struct.h`.
2226 */
2227#define PSA_AEAD_OPERATION_INIT {0}
2228#endif
2229
2230/** Return an initial value for an AEAD operation object.
2231 */
2232static psa_aead_operation_t psa_aead_operation_init(void);
2233
2234/** Set the key for a multipart authenticated encryption operation.
2235 *
2236 * The sequence of operations to encrypt a message with authentication
2237 * is as follows:
2238 * -# Allocate an operation object which will be passed to all the functions
2239 * listed here.
2240 * -# Initialize the operation object with one of the methods described in the
2241 * documentation for #psa_aead_operation_t, e.g.
2242 * PSA_AEAD_OPERATION_INIT.
2243 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002244 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2245 * inputs to the subsequent calls to psa_aead_update_ad() and
2246 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2247 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002248 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2249 * generate or set the nonce. You should use
2250 * psa_aead_generate_nonce() unless the protocol you are implementing
2251 * requires a specific nonce value.
2252 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2253 * of the non-encrypted additional authenticated data each time.
2254 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002255 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002256 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002257 *
2258 * The application may call psa_aead_abort() at any time after the operation
2259 * has been initialized.
2260 *
2261 * After a successful call to psa_aead_encrypt_setup(), the application must
2262 * eventually terminate the operation. The following events terminate an
2263 * operation:
2264 * - A failed call to any of the \c psa_aead_xxx functions.
2265 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2266 *
2267 * \param[in,out] operation The operation object to set up. It must have
2268 * been initialized as per the documentation for
2269 * #psa_aead_operation_t and not yet in use.
2270 * \param handle Handle to the key to use for the operation.
2271 * It must remain valid until the operation
2272 * terminates.
2273 * \param alg The AEAD algorithm to compute
2274 * (\c PSA_ALG_XXX value such that
2275 * #PSA_ALG_IS_AEAD(\p alg) is true).
2276 *
2277 * \retval #PSA_SUCCESS
2278 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002279 * \retval #PSA_ERROR_BAD_STATE
2280 * The operation state is not valid (already set up and not
2281 * subsequently completed).
2282 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002283 * \retval #PSA_ERROR_NOT_PERMITTED
2284 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002285 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002286 * \retval #PSA_ERROR_NOT_SUPPORTED
2287 * \p alg is not supported or is not an AEAD algorithm.
2288 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2289 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2290 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002291 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002292 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002293 * \retval #PSA_ERROR_BAD_STATE
2294 * The library has not been previously initialized by psa_crypto_init().
2295 * It is implementation-dependent whether a failure to initialize
2296 * results in this error code.
2297 */
2298psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2299 psa_key_handle_t handle,
2300 psa_algorithm_t alg);
2301
2302/** Set the key for a multipart authenticated decryption operation.
2303 *
2304 * The sequence of operations to decrypt a message with authentication
2305 * is as follows:
2306 * -# Allocate an operation object which will be passed to all the functions
2307 * listed here.
2308 * -# Initialize the operation object with one of the methods described in the
2309 * documentation for #psa_aead_operation_t, e.g.
2310 * PSA_AEAD_OPERATION_INIT.
2311 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002312 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2313 * inputs to the subsequent calls to psa_aead_update_ad() and
2314 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2315 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002316 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2317 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2318 * of the non-encrypted additional authenticated data each time.
2319 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002320 * of the ciphertext to decrypt each time.
2321 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002322 *
2323 * The application may call psa_aead_abort() at any time after the operation
2324 * has been initialized.
2325 *
2326 * After a successful call to psa_aead_decrypt_setup(), the application must
2327 * eventually terminate the operation. The following events terminate an
2328 * operation:
2329 * - A failed call to any of the \c psa_aead_xxx functions.
2330 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2331 *
2332 * \param[in,out] operation The operation object to set up. It must have
2333 * been initialized as per the documentation for
2334 * #psa_aead_operation_t and not yet in use.
2335 * \param handle Handle to the key to use for the operation.
2336 * It must remain valid until the operation
2337 * terminates.
2338 * \param alg The AEAD algorithm to compute
2339 * (\c PSA_ALG_XXX value such that
2340 * #PSA_ALG_IS_AEAD(\p alg) is true).
2341 *
2342 * \retval #PSA_SUCCESS
2343 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002344 * \retval #PSA_ERROR_BAD_STATE
2345 * The operation state is not valid (already set up and not
2346 * subsequently completed).
2347 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002348 * \retval #PSA_ERROR_NOT_PERMITTED
2349 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002350 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002351 * \retval #PSA_ERROR_NOT_SUPPORTED
2352 * \p alg is not supported or is not an AEAD algorithm.
2353 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2354 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2355 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002356 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002357 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002358 * \retval #PSA_ERROR_BAD_STATE
2359 * The library has not been previously initialized by psa_crypto_init().
2360 * It is implementation-dependent whether a failure to initialize
2361 * results in this error code.
2362 */
2363psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2364 psa_key_handle_t handle,
2365 psa_algorithm_t alg);
2366
2367/** Generate a random nonce for an authenticated encryption operation.
2368 *
2369 * This function generates a random nonce for the authenticated encryption
2370 * operation with an appropriate size for the chosen algorithm, key type
2371 * and key size.
2372 *
2373 * The application must call psa_aead_encrypt_setup() before
2374 * calling this function.
2375 *
2376 * If this function returns an error status, the operation becomes inactive.
2377 *
2378 * \param[in,out] operation Active AEAD operation.
2379 * \param[out] nonce Buffer where the generated nonce is to be
2380 * written.
2381 * \param nonce_size Size of the \p nonce buffer in bytes.
2382 * \param[out] nonce_length On success, the number of bytes of the
2383 * generated nonce.
2384 *
2385 * \retval #PSA_SUCCESS
2386 * Success.
2387 * \retval #PSA_ERROR_BAD_STATE
2388 * The operation state is not valid (not set up, or nonce already set).
2389 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2390 * The size of the \p nonce buffer is too small.
2391 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2392 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2393 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002394 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002395 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002396 * \retval #PSA_ERROR_BAD_STATE
2397 * The library has not been previously initialized by psa_crypto_init().
2398 * It is implementation-dependent whether a failure to initialize
2399 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002400 */
2401psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002402 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002403 size_t nonce_size,
2404 size_t *nonce_length);
2405
2406/** Set the nonce for an authenticated encryption or decryption operation.
2407 *
2408 * This function sets the nonce for the authenticated
2409 * encryption or decryption operation.
2410 *
2411 * The application must call psa_aead_encrypt_setup() before
2412 * calling this function.
2413 *
2414 * If this function returns an error status, the operation becomes inactive.
2415 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002416 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002417 * instead of this function, unless implementing a protocol that requires
2418 * a non-random IV.
2419 *
2420 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002421 * \param[in] nonce Buffer containing the nonce to use.
2422 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002423 *
2424 * \retval #PSA_SUCCESS
2425 * Success.
2426 * \retval #PSA_ERROR_BAD_STATE
2427 * The operation state is not valid (not set up, or nonce already set).
2428 * \retval #PSA_ERROR_INVALID_ARGUMENT
2429 * The size of \p nonce is not acceptable for the chosen algorithm.
2430 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2431 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2432 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002433 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002434 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002435 * \retval #PSA_ERROR_BAD_STATE
2436 * The library has not been previously initialized by psa_crypto_init().
2437 * It is implementation-dependent whether a failure to initialize
2438 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002439 */
2440psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002441 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002442 size_t nonce_length);
2443
Gilles Peskinebc59c852019-01-17 15:26:08 +01002444/** Declare the lengths of the message and additional data for AEAD.
2445 *
2446 * The application must call this function before calling
2447 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2448 * the operation requires it. If the algorithm does not require it,
2449 * calling this function is optional, but if this function is called
2450 * then the implementation must enforce the lengths.
2451 *
2452 * You may call this function before or after setting the nonce with
2453 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2454 *
2455 * - For #PSA_ALG_CCM, calling this function is required.
2456 * - For the other AEAD algorithms defined in this specification, calling
2457 * this function is not required.
2458 * - For vendor-defined algorithm, refer to the vendor documentation.
2459 *
2460 * \param[in,out] operation Active AEAD operation.
2461 * \param ad_length Size of the non-encrypted additional
2462 * authenticated data in bytes.
2463 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2464 *
2465 * \retval #PSA_SUCCESS
2466 * Success.
2467 * \retval #PSA_ERROR_BAD_STATE
2468 * The operation state is not valid (not set up, already completed,
2469 * or psa_aead_update_ad() or psa_aead_update() already called).
2470 * \retval #PSA_ERROR_INVALID_ARGUMENT
2471 * At least one of the lengths is not acceptable for the chosen
2472 * algorithm.
2473 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2474 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2475 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002476 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002477 * \retval #PSA_ERROR_BAD_STATE
2478 * The library has not been previously initialized by psa_crypto_init().
2479 * It is implementation-dependent whether a failure to initialize
2480 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002481 */
2482psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2483 size_t ad_length,
2484 size_t plaintext_length);
2485
Gilles Peskine30a9e412019-01-14 18:36:12 +01002486/** Pass additional data to an active AEAD operation.
2487 *
2488 * Additional data is authenticated, but not encrypted.
2489 *
2490 * You may call this function multiple times to pass successive fragments
2491 * of the additional data. You may not call this function after passing
2492 * data to encrypt or decrypt with psa_aead_update().
2493 *
2494 * Before calling this function, you must:
2495 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2496 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2497 *
2498 * If this function returns an error status, the operation becomes inactive.
2499 *
2500 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2501 * there is no guarantee that the input is valid. Therefore, until
2502 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2503 * treat the input as untrusted and prepare to undo any action that
2504 * depends on the input if psa_aead_verify() returns an error status.
2505 *
2506 * \param[in,out] operation Active AEAD operation.
2507 * \param[in] input Buffer containing the fragment of
2508 * additional data.
2509 * \param input_length Size of the \p input buffer in bytes.
2510 *
2511 * \retval #PSA_SUCCESS
2512 * Success.
2513 * \retval #PSA_ERROR_BAD_STATE
2514 * The operation state is not valid (not set up, nonce not set,
2515 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002516 * \retval #PSA_ERROR_INVALID_ARGUMENT
2517 * The total input length overflows the additional data length that
2518 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002519 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2520 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2521 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002522 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002523 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002524 * \retval #PSA_ERROR_BAD_STATE
2525 * The library has not been previously initialized by psa_crypto_init().
2526 * It is implementation-dependent whether a failure to initialize
2527 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002528 */
2529psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2530 const uint8_t *input,
2531 size_t input_length);
2532
2533/** Encrypt or decrypt a message fragment in an active AEAD operation.
2534 *
2535 * Before calling this function, you must:
2536 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2537 * The choice of setup function determines whether this function
2538 * encrypts or decrypts its input.
2539 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2540 * 3. Call psa_aead_update_ad() to pass all the additional data.
2541 *
2542 * If this function returns an error status, the operation becomes inactive.
2543 *
2544 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2545 * there is no guarantee that the input is valid. Therefore, until
2546 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2547 * - Do not use the output in any way other than storing it in a
2548 * confidential location. If you take any action that depends
2549 * on the tentative decrypted data, this action will need to be
2550 * undone if the input turns out not to be valid. Furthermore,
2551 * if an adversary can observe that this action took place
2552 * (for example through timing), they may be able to use this
2553 * fact as an oracle to decrypt any message encrypted with the
2554 * same key.
2555 * - In particular, do not copy the output anywhere but to a
2556 * memory or storage space that you have exclusive access to.
2557 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002558 * This function does not require the input to be aligned to any
2559 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002560 * a whole block at a time, it must consume all the input provided, but
2561 * it may delay the end of the corresponding output until a subsequent
2562 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2563 * provides sufficient input. The amount of data that can be delayed
2564 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002565 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002566 * \param[in,out] operation Active AEAD operation.
2567 * \param[in] input Buffer containing the message fragment to
2568 * encrypt or decrypt.
2569 * \param input_length Size of the \p input buffer in bytes.
2570 * \param[out] output Buffer where the output is to be written.
2571 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002572 * This must be at least
2573 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2574 * \p input_length) where \c alg is the
2575 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002576 * \param[out] output_length On success, the number of bytes
2577 * that make up the returned output.
2578 *
2579 * \retval #PSA_SUCCESS
2580 * Success.
2581 * \retval #PSA_ERROR_BAD_STATE
2582 * The operation state is not valid (not set up, nonce not set
2583 * or already completed).
2584 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2585 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002586 * You can determine a sufficient buffer size by calling
2587 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2588 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002589 * \retval #PSA_ERROR_INVALID_ARGUMENT
2590 * The total length of input to psa_aead_update_ad() so far is
2591 * less than the additional data length that was previously
2592 * specified with psa_aead_set_lengths().
2593 * \retval #PSA_ERROR_INVALID_ARGUMENT
2594 * The total input length overflows the plaintext length that
2595 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002596 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2597 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2598 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002599 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002600 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002601 * \retval #PSA_ERROR_BAD_STATE
2602 * The library has not been previously initialized by psa_crypto_init().
2603 * It is implementation-dependent whether a failure to initialize
2604 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002605 */
2606psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2607 const uint8_t *input,
2608 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002609 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002610 size_t output_size,
2611 size_t *output_length);
2612
2613/** Finish encrypting a message in an AEAD operation.
2614 *
2615 * The operation must have been set up with psa_aead_encrypt_setup().
2616 *
2617 * This function finishes the authentication of the additional data
2618 * formed by concatenating the inputs passed to preceding calls to
2619 * psa_aead_update_ad() with the plaintext formed by concatenating the
2620 * inputs passed to preceding calls to psa_aead_update().
2621 *
2622 * This function has two output buffers:
2623 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002624 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002625 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002626 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002627 * that the operation performs.
2628 *
2629 * When this function returns, the operation becomes inactive.
2630 *
2631 * \param[in,out] operation Active AEAD operation.
2632 * \param[out] ciphertext Buffer where the last part of the ciphertext
2633 * is to be written.
2634 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002635 * This must be at least
2636 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2637 * \c alg is the algorithm that is being
2638 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002639 * \param[out] ciphertext_length On success, the number of bytes of
2640 * returned ciphertext.
2641 * \param[out] tag Buffer where the authentication tag is
2642 * to be written.
2643 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002644 * This must be at least
2645 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2646 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002647 * \param[out] tag_length On success, the number of bytes
2648 * that make up the returned tag.
2649 *
2650 * \retval #PSA_SUCCESS
2651 * Success.
2652 * \retval #PSA_ERROR_BAD_STATE
2653 * The operation state is not valid (not set up, nonce not set,
2654 * decryption, or already completed).
2655 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002656 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002657 * You can determine a sufficient buffer size for \p ciphertext by
2658 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2659 * where \c alg is the algorithm that is being calculated.
2660 * You can determine a sufficient buffer size for \p tag by
2661 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002662 * \retval #PSA_ERROR_INVALID_ARGUMENT
2663 * The total length of input to psa_aead_update_ad() so far is
2664 * less than the additional data length that was previously
2665 * specified with psa_aead_set_lengths().
2666 * \retval #PSA_ERROR_INVALID_ARGUMENT
2667 * The total length of input to psa_aead_update() so far is
2668 * less than the plaintext length that was previously
2669 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002670 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2671 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2672 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002673 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002674 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002675 * \retval #PSA_ERROR_BAD_STATE
2676 * The library has not been previously initialized by psa_crypto_init().
2677 * It is implementation-dependent whether a failure to initialize
2678 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002679 */
2680psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002681 uint8_t *ciphertext,
2682 size_t ciphertext_size,
2683 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002684 uint8_t *tag,
2685 size_t tag_size,
2686 size_t *tag_length);
2687
2688/** Finish authenticating and decrypting a message in an AEAD operation.
2689 *
2690 * The operation must have been set up with psa_aead_decrypt_setup().
2691 *
2692 * This function finishes the authentication of the additional data
2693 * formed by concatenating the inputs passed to preceding calls to
2694 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2695 * inputs passed to preceding calls to psa_aead_update().
2696 *
2697 * When this function returns, the operation becomes inactive.
2698 *
2699 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002700 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002701 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002702 * from previous calls to psa_aead_update()
2703 * that could not be processed until the end
2704 * of the input.
2705 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002706 * This must be at least
2707 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2708 * \c alg is the algorithm that is being
2709 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002710 * \param[out] plaintext_length On success, the number of bytes of
2711 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002712 * \param[in] tag Buffer containing the authentication tag.
2713 * \param tag_length Size of the \p tag buffer in bytes.
2714 *
2715 * \retval #PSA_SUCCESS
2716 * Success.
2717 * \retval #PSA_ERROR_BAD_STATE
2718 * The operation state is not valid (not set up, nonce not set,
2719 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002720 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2721 * The size of the \p plaintext buffer is too small.
2722 * You can determine a sufficient buffer size for \p plaintext by
2723 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2724 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002725 * \retval #PSA_ERROR_INVALID_ARGUMENT
2726 * The total length of input to psa_aead_update_ad() so far is
2727 * less than the additional data length that was previously
2728 * specified with psa_aead_set_lengths().
2729 * \retval #PSA_ERROR_INVALID_ARGUMENT
2730 * The total length of input to psa_aead_update() so far is
2731 * less than the plaintext length that was previously
2732 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002733 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2734 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2735 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002736 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002737 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002738 * \retval #PSA_ERROR_BAD_STATE
2739 * The library has not been previously initialized by psa_crypto_init().
2740 * It is implementation-dependent whether a failure to initialize
2741 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002742 */
2743psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002744 uint8_t *plaintext,
2745 size_t plaintext_size,
2746 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002747 const uint8_t *tag,
2748 size_t tag_length);
2749
2750/** Abort an AEAD operation.
2751 *
2752 * Aborting an operation frees all associated resources except for the
2753 * \p operation structure itself. Once aborted, the operation object
2754 * can be reused for another operation by calling
2755 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2756 *
2757 * You may call this function any time after the operation object has
2758 * been initialized by any of the following methods:
2759 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2760 * whether it succeeds or not.
2761 * - Initializing the \c struct to all-bits-zero.
2762 * - Initializing the \c struct to logical zeros, e.g.
2763 * `psa_aead_operation_t operation = {0}`.
2764 *
2765 * In particular, calling psa_aead_abort() after the operation has been
2766 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2767 * is safe and has no effect.
2768 *
2769 * \param[in,out] operation Initialized AEAD operation.
2770 *
2771 * \retval #PSA_SUCCESS
2772 * \retval #PSA_ERROR_BAD_STATE
2773 * \p operation is not an active AEAD operation.
2774 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2775 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002776 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002777 * \retval #PSA_ERROR_BAD_STATE
2778 * The library has not been previously initialized by psa_crypto_init().
2779 * It is implementation-dependent whether a failure to initialize
2780 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002781 */
2782psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2783
Gilles Peskine3b555712018-03-03 21:27:57 +01002784/**@}*/
2785
Gilles Peskine20035e32018-02-03 22:44:14 +01002786/** \defgroup asymmetric Asymmetric cryptography
2787 * @{
2788 */
2789
2790/**
2791 * \brief Sign a hash or short message with a private key.
2792 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002793 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002794 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002795 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2796 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2797 * to determine the hash algorithm to use.
2798 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002799 * \param handle Handle to the key to use for the operation.
2800 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002801 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002802 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002803 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002804 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002805 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002806 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002807 * \param[out] signature_length On success, the number of bytes
2808 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002809 *
Gilles Peskine28538492018-07-11 17:34:00 +02002810 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002811 * \retval #PSA_ERROR_INVALID_HANDLE
2812 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002813 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002814 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002815 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002816 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002817 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002818 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002819 * \retval #PSA_ERROR_NOT_SUPPORTED
2820 * \retval #PSA_ERROR_INVALID_ARGUMENT
2821 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2822 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2823 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002824 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002825 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002826 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002827 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002828 * The library has not been previously initialized by psa_crypto_init().
2829 * It is implementation-dependent whether a failure to initialize
2830 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002831 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002832psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002833 psa_algorithm_t alg,
2834 const uint8_t *hash,
2835 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002836 uint8_t *signature,
2837 size_t signature_size,
2838 size_t *signature_length);
2839
2840/**
2841 * \brief Verify the signature a hash or short message using a public key.
2842 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002843 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002844 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002845 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2846 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2847 * to determine the hash algorithm to use.
2848 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002849 * \param handle Handle to the key to use for the operation.
2850 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002851 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002852 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002853 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002854 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002855 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002856 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002857 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002858 *
Gilles Peskine28538492018-07-11 17:34:00 +02002859 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002860 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002861 * \retval #PSA_ERROR_INVALID_HANDLE
2862 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002863 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002864 * The calculation was perfomed successfully, but the passed
2865 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002866 * \retval #PSA_ERROR_NOT_SUPPORTED
2867 * \retval #PSA_ERROR_INVALID_ARGUMENT
2868 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2869 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2870 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002871 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002872 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002873 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002874 * The library has not been previously initialized by psa_crypto_init().
2875 * It is implementation-dependent whether a failure to initialize
2876 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002877 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002878psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002879 psa_algorithm_t alg,
2880 const uint8_t *hash,
2881 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002882 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002883 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002884
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002885/**
2886 * \brief Encrypt a short message with a public key.
2887 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002888 * \param handle Handle to the key to use for the operation.
2889 * It must be a public key or an asymmetric
2890 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002891 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002892 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002893 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002894 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002895 * \param[in] salt A salt or label, if supported by the
2896 * encryption algorithm.
2897 * If the algorithm does not support a
2898 * salt, pass \c NULL.
2899 * If the algorithm supports an optional
2900 * salt and you do not want to pass a salt,
2901 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002902 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002903 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2904 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002905 * \param salt_length Size of the \p salt buffer in bytes.
2906 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002907 * \param[out] output Buffer where the encrypted message is to
2908 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002909 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002910 * \param[out] output_length On success, the number of bytes
2911 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002912 *
Gilles Peskine28538492018-07-11 17:34:00 +02002913 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002914 * \retval #PSA_ERROR_INVALID_HANDLE
2915 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002916 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002917 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002918 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002919 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002920 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002921 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002922 * \retval #PSA_ERROR_NOT_SUPPORTED
2923 * \retval #PSA_ERROR_INVALID_ARGUMENT
2924 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2925 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2926 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002927 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002928 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002929 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002930 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002931 * The library has not been previously initialized by psa_crypto_init().
2932 * It is implementation-dependent whether a failure to initialize
2933 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002934 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002935psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002936 psa_algorithm_t alg,
2937 const uint8_t *input,
2938 size_t input_length,
2939 const uint8_t *salt,
2940 size_t salt_length,
2941 uint8_t *output,
2942 size_t output_size,
2943 size_t *output_length);
2944
2945/**
2946 * \brief Decrypt a short message with a private key.
2947 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002948 * \param handle Handle to the key to use for the operation.
2949 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002950 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002951 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002952 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002953 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002954 * \param[in] salt A salt or label, if supported by the
2955 * encryption algorithm.
2956 * If the algorithm does not support a
2957 * salt, pass \c NULL.
2958 * If the algorithm supports an optional
2959 * salt and you do not want to pass a salt,
2960 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002961 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002962 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2963 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002964 * \param salt_length Size of the \p salt buffer in bytes.
2965 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002966 * \param[out] output Buffer where the decrypted message is to
2967 * be written.
2968 * \param output_size Size of the \c output buffer in bytes.
2969 * \param[out] output_length On success, the number of bytes
2970 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002971 *
Gilles Peskine28538492018-07-11 17:34:00 +02002972 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002973 * \retval #PSA_ERROR_INVALID_HANDLE
2974 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002975 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002976 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002977 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002978 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002979 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002980 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002981 * \retval #PSA_ERROR_NOT_SUPPORTED
2982 * \retval #PSA_ERROR_INVALID_ARGUMENT
2983 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2984 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2985 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002986 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002987 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002988 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2989 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002990 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002991 * The library has not been previously initialized by psa_crypto_init().
2992 * It is implementation-dependent whether a failure to initialize
2993 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002994 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002995psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002996 psa_algorithm_t alg,
2997 const uint8_t *input,
2998 size_t input_length,
2999 const uint8_t *salt,
3000 size_t salt_length,
3001 uint8_t *output,
3002 size_t output_size,
3003 size_t *output_length);
3004
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003005/**@}*/
3006
Gilles Peskine35675b62019-05-16 17:26:11 +02003007/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003008 * @{
3009 */
3010
Gilles Peskine35675b62019-05-16 17:26:11 +02003011/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003012 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003013 * Before calling any function on a key derivation operation object, the
3014 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003015 * - Set the structure to all-bits-zero, for example:
3016 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003017 * psa_key_derivation_operation_t operation;
3018 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003019 * \endcode
3020 * - Initialize the structure to logical zero values, for example:
3021 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003022 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003023 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003024 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003025 * for example:
3026 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003027 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003028 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003029 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003030 * to the structure, for example:
3031 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003032 * psa_key_derivation_operation_t operation;
3033 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003034 * \endcode
3035 *
3036 * This is an implementation-defined \c struct. Applications should not
3037 * make any assumptions about the content of this structure except
3038 * as directed by the documentation of a specific implementation.
3039 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003040typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003041
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003042/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003043 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003044 * This macro returns a suitable initializer for a key derivation operation
3045 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003046 */
3047#ifdef __DOXYGEN_ONLY__
3048/* This is an example definition for documentation purposes.
3049 * Implementations should define a suitable value in `crypto_struct.h`.
3050 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003051#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003052#endif
3053
Gilles Peskine35675b62019-05-16 17:26:11 +02003054/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003055 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003056static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003057
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003058/** Set up a key derivation operation.
3059 *
3060 * A key derivation algorithm takes some inputs and uses them to generate
3061 * a byte stream in a deterministic way.
3062 * This byte stream can be used to produce keys and other
3063 * cryptographic material.
3064 *
3065 * To derive a key:
3066 * - Start with an initialized object of type #psa_key_derivation_operation_t.
3067 * - Call psa_key_derivation_setup() to select the algorithm.
3068 * - Provide the inputs for the key derivation by calling
3069 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3070 * as appropriate. Which inputs are needed, in what order, and whether
3071 * they may be keys and if so of what type depends on the algorithm.
3072 * - Optionally set the operation's maximum capacity with
3073 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3074 * of or after providing inputs. For some algorithms, this step is mandatory
3075 * because the output depends on the maximum capacity.
3076 * - To derive a key, call psa_key_derivation_output_key().
3077 * To derive a byte string for a different purpose, call
3078 * - psa_key_derivation_output_bytes().
3079 * Successive calls to these functions use successive output bytes
3080 * calculated by the key derivation algorithm.
3081 * - Clean up the key derivation operation object with
3082 * psa_key_derivation_abort().
3083 *
3084 * \param[in,out] operation The key derivation operation object
3085 * to set up. It must
3086 * have been initialized but not set up yet.
3087 * \param alg The key derivation algorithm to compute
3088 * (\c PSA_ALG_XXX value such that
3089 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3090 *
3091 * \retval #PSA_SUCCESS
3092 * Success.
3093 * \retval #PSA_ERROR_INVALID_ARGUMENT
3094 * \c alg is not a key derivation algorithm.
3095 * \retval #PSA_ERROR_NOT_SUPPORTED
3096 * \c alg is not supported or is not a key derivation algorithm.
3097 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3098 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3099 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003100 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003101 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003102 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01003103 * The operation state is either not initialized or has already been setup.
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003104 * \retval #PSA_ERROR_BAD_STATE
3105 * The library has not been previously initialized by psa_crypto_init().
3106 * It is implementation-dependent whether a failure to initialize
3107 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003108 */
3109psa_status_t psa_key_derivation_setup(
3110 psa_key_derivation_operation_t *operation,
3111 psa_algorithm_t alg);
3112
Gilles Peskine35675b62019-05-16 17:26:11 +02003113/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003114 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003115 * The capacity of a key derivation is the maximum number of bytes that it can
3116 * return. When you get *N* bytes of output from a key derivation operation,
3117 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003118 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003119 * \param[in] operation The operation to query.
3120 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003121 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003122 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003123 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003124 * \retval #PSA_ERROR_BAD_STATE
3125 * The operation state is not valid.
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003126 * \retval #PSA_ERROR_HARDWARE_FAILURE
3127 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003128 * \retval #PSA_ERROR_BAD_STATE
3129 * The library has not been previously initialized by psa_crypto_init().
3130 * It is implementation-dependent whether a failure to initialize
3131 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003132 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003133psa_status_t psa_key_derivation_get_capacity(
3134 const psa_key_derivation_operation_t *operation,
3135 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003136
Gilles Peskine35675b62019-05-16 17:26:11 +02003137/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003138 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003139 * The capacity of a key derivation operation is the maximum number of bytes
3140 * that the key derivation operation can return from this point onwards.
3141 *
3142 * \param[in,out] operation The key derivation operation object to modify.
3143 * \param capacity The new capacity of the operation.
3144 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003145 * current capacity.
3146 *
3147 * \retval #PSA_SUCCESS
3148 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003149 * \p capacity is larger than the operation's current capacity.
3150 * In this case, the operation object remains valid and its capacity
3151 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003152 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003153 * The operation state is not valid.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003154 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003155 * \retval #PSA_ERROR_HARDWARE_FAILURE
3156 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003157 * \retval #PSA_ERROR_BAD_STATE
3158 * The library has not been previously initialized by psa_crypto_init().
3159 * It is implementation-dependent whether a failure to initialize
3160 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003161 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003162psa_status_t psa_key_derivation_set_capacity(
3163 psa_key_derivation_operation_t *operation,
3164 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003165
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003166/** Use the maximum possible capacity for a key derivation operation.
3167 *
3168 * Use this value as the capacity argument when setting up a key derivation
3169 * to indicate that the operation should have the maximum possible capacity.
3170 * The value of the maximum possible capacity depends on the key derivation
3171 * algorithm.
3172 */
3173#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3174
3175/** Provide an input for key derivation or key agreement.
3176 *
3177 * Which inputs are required and in what order depends on the algorithm.
3178 * Refer to the documentation of each key derivation or key agreement
3179 * algorithm for information.
3180 *
3181 * This function passes direct inputs. Some inputs must be passed as keys
3182 * using psa_key_derivation_input_key() instead of this function. Refer to
3183 * the documentation of individual step types for information.
3184 *
3185 * \param[in,out] operation The key derivation operation object to use.
3186 * It must have been set up with
3187 * psa_key_derivation_setup() and must not
3188 * have produced any output yet.
3189 * \param step Which step the input data is for.
3190 * \param[in] data Input data to use.
3191 * \param data_length Size of the \p data buffer in bytes.
3192 *
3193 * \retval #PSA_SUCCESS
3194 * Success.
3195 * \retval #PSA_ERROR_INVALID_ARGUMENT
3196 * \c step is not compatible with the operation's algorithm.
3197 * \retval #PSA_ERROR_INVALID_ARGUMENT
3198 * \c step does not allow direct inputs.
3199 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3200 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3201 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003202 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003203 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003204 * \retval #PSA_ERROR_BAD_STATE
3205 * The value of \p step is not valid given the state of \p operation.
3206 * \retval #PSA_ERROR_BAD_STATE
3207 * The library has not been previously initialized by psa_crypto_init().
3208 * It is implementation-dependent whether a failure to initialize
3209 * results in this error code.
3210 */
3211psa_status_t psa_key_derivation_input_bytes(
3212 psa_key_derivation_operation_t *operation,
3213 psa_key_derivation_step_t step,
3214 const uint8_t *data,
3215 size_t data_length);
3216
3217/** Provide an input for key derivation in the form of a key.
3218 *
3219 * Which inputs are required and in what order depends on the algorithm.
3220 * Refer to the documentation of each key derivation or key agreement
3221 * algorithm for information.
3222 *
3223 * This function passes key inputs. Some inputs must be passed as keys
3224 * of the appropriate type using this function, while others must be
3225 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3226 * the documentation of individual step types for information.
3227 *
3228 * \param[in,out] operation The key derivation operation object to use.
3229 * It must have been set up with
3230 * psa_key_derivation_setup() and must not
3231 * have produced any output yet.
3232 * \param step Which step the input data is for.
3233 * \param handle Handle to the key. It must have an
3234 * appropriate type for \p step and must
3235 * allow the usage #PSA_KEY_USAGE_DERIVE.
3236 *
3237 * \retval #PSA_SUCCESS
3238 * Success.
3239 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003240 * \retval #PSA_ERROR_NOT_PERMITTED
3241 * \retval #PSA_ERROR_INVALID_ARGUMENT
3242 * \c step is not compatible with the operation's algorithm.
3243 * \retval #PSA_ERROR_INVALID_ARGUMENT
3244 * \c step does not allow key inputs.
3245 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3246 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3247 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003248 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003249 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003250 * \retval #PSA_ERROR_BAD_STATE
3251 * The value of \p step is not valid given the state of \p operation.
3252 * \retval #PSA_ERROR_BAD_STATE
3253 * The library has not been previously initialized by psa_crypto_init().
3254 * It is implementation-dependent whether a failure to initialize
3255 * results in this error code.
3256 */
3257psa_status_t psa_key_derivation_input_key(
3258 psa_key_derivation_operation_t *operation,
3259 psa_key_derivation_step_t step,
3260 psa_key_handle_t handle);
3261
3262/** Perform a key agreement and use the shared secret as input to a key
3263 * derivation.
3264 *
3265 * A key agreement algorithm takes two inputs: a private key \p private_key
3266 * a public key \p peer_key.
3267 * The result of this function is passed as input to a key derivation.
3268 * The output of this key derivation can be extracted by reading from the
3269 * resulting operation to produce keys and other cryptographic material.
3270 *
3271 * \param[in,out] operation The key derivation operation object to use.
3272 * It must have been set up with
3273 * psa_key_derivation_setup() with a
3274 * key agreement and derivation algorithm
3275 * \c alg (\c PSA_ALG_XXX value such that
3276 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3277 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3278 * is false).
3279 * The operation must be ready for an
3280 * input of the type given by \p step.
3281 * \param step Which step the input data is for.
3282 * \param private_key Handle to the private key to use.
3283 * \param[in] peer_key Public key of the peer. The peer key must be in the
3284 * same format that psa_import_key() accepts for the
3285 * public key type corresponding to the type of
3286 * private_key. That is, this function performs the
3287 * equivalent of
3288 * #psa_import_key(...,
3289 * `peer_key`, `peer_key_length`) where
3290 * with key attributes indicating the public key
3291 * type corresponding to the type of `private_key`.
3292 * For example, for EC keys, this means that peer_key
3293 * is interpreted as a point on the curve that the
3294 * private key is on. The standard formats for public
3295 * keys are documented in the documentation of
3296 * psa_export_public_key().
3297 * \param peer_key_length Size of \p peer_key in bytes.
3298 *
3299 * \retval #PSA_SUCCESS
3300 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01003301 * \retval #PSA_ERROR_BAD_STATE
3302 * The value of \p step is not valid for a key agreement given the
3303 * state of \p operation.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003304 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003305 * \retval #PSA_ERROR_NOT_PERMITTED
3306 * \retval #PSA_ERROR_INVALID_ARGUMENT
3307 * \c private_key is not compatible with \c alg,
3308 * or \p peer_key is not valid for \c alg or not compatible with
3309 * \c private_key.
3310 * \retval #PSA_ERROR_NOT_SUPPORTED
3311 * \c alg is not supported or is not a key derivation algorithm.
3312 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3313 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3314 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003315 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003316 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003317 * \retval #PSA_ERROR_BAD_STATE
3318 * The library has not been previously initialized by psa_crypto_init().
3319 * It is implementation-dependent whether a failure to initialize
3320 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003321 */
3322psa_status_t psa_key_derivation_key_agreement(
3323 psa_key_derivation_operation_t *operation,
3324 psa_key_derivation_step_t step,
3325 psa_key_handle_t private_key,
3326 const uint8_t *peer_key,
3327 size_t peer_key_length);
3328
Gilles Peskine35675b62019-05-16 17:26:11 +02003329/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003330 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003331 * This function calculates output bytes from a key derivation algorithm and
3332 * return those bytes.
3333 * If you view the key derivation's output as a stream of bytes, this
3334 * function destructively reads the requested number of bytes from the
3335 * stream.
3336 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003337 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003338 * \param[in,out] operation The key derivation operation object to read from.
3339 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003340 * \param output_length Number of bytes to output.
3341 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003342 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003343 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003344 * The operation's capacity was less than
3345 * \p output_length bytes. Note that in this case,
3346 * no output is written to the output buffer.
3347 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003348 * subsequent calls to this function will not
3349 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003350 * \retval #PSA_ERROR_BAD_STATE
3351 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3352 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3353 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003354 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003355 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003356 * \retval #PSA_ERROR_BAD_STATE
3357 * The library has not been previously initialized by psa_crypto_init().
3358 * It is implementation-dependent whether a failure to initialize
3359 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003360 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003361psa_status_t psa_key_derivation_output_bytes(
3362 psa_key_derivation_operation_t *operation,
3363 uint8_t *output,
3364 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003365
Gilles Peskine35675b62019-05-16 17:26:11 +02003366/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003367 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003368 * This function calculates output bytes from a key derivation algorithm
3369 * and uses those bytes to generate a key deterministically.
3370 * If you view the key derivation's output as a stream of bytes, this
3371 * function destructively reads as many bytes as required from the
3372 * stream.
3373 * The operation's capacity decreases by the number of bytes read.
3374 *
3375 * How much output is produced and consumed from the operation, and how
3376 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003377 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003378 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003379 * of a given size, this function is functionally equivalent to
3380 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003381 * and passing the resulting output to #psa_import_key.
3382 * However, this function has a security benefit:
3383 * if the implementation provides an isolation boundary then
3384 * the key material is not exposed outside the isolation boundary.
3385 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003386 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003387 * The following key types defined in this specification follow this scheme:
3388 *
3389 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003390 * - #PSA_KEY_TYPE_ARC4;
3391 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003392 * - #PSA_KEY_TYPE_DERIVE;
3393 * - #PSA_KEY_TYPE_HMAC.
3394 *
3395 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003396 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003397 * Montgomery curve), this function always draws a byte string whose
3398 * length is determined by the curve, and sets the mandatory bits
3399 * accordingly. That is:
3400 *
3401 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3402 * and process it as specified in RFC 7748 &sect;5.
3403 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3404 * and process it as specified in RFC 7748 &sect;5.
3405 *
3406 * - For key types for which the key is represented by a single sequence of
3407 * \p bits bits with constraints as to which bit sequences are acceptable,
3408 * this function draws a byte string of length (\p bits / 8) bytes rounded
3409 * up to the nearest whole number of bytes. If the resulting byte string
3410 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3411 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003412 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003413 * for the output produced by psa_export_key().
3414 * The following key types defined in this specification follow this scheme:
3415 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003416 * - #PSA_KEY_TYPE_DES.
3417 * Force-set the parity bits, but discard forbidden weak keys.
3418 * For 2-key and 3-key triple-DES, the three keys are generated
3419 * successively (for example, for 3-key triple-DES,
3420 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3421 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003422 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003423 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003424 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003425 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003426 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003427 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003428 * Weierstrass curve).
3429 * For these key types, interpret the byte string as integer
3430 * in big-endian order. Discard it if it is not in the range
3431 * [0, *N* - 2] where *N* is the boundary of the private key domain
3432 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003433 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003434 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003435 * This method allows compliance to NIST standards, specifically
3436 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003437 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3438 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3439 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3440 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003441 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003442 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003443 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003444 * implementation-defined.
3445 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003446 * In all cases, the data that is read is discarded from the operation.
3447 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003448 *
Gilles Peskine20628592019-04-19 19:29:50 +02003449 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003450 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003451 * \param[out] handle On success, a handle to the newly created key.
3452 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003453 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003454 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003455 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003456 * If the key is persistent, the key material and the key's metadata
3457 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003458 * \retval #PSA_ERROR_ALREADY_EXISTS
3459 * This is an attempt to create a persistent key, and there is
3460 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003461 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003462 * There was not enough data to create the desired key.
3463 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003464 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003465 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003466 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003467 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003468 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003469 * \retval #PSA_ERROR_INVALID_ARGUMENT
3470 * The provided key attributes are not valid for the operation.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003471 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003472 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3473 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3474 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3475 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003476 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003477 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003478 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003479 * The library has not been previously initialized by psa_crypto_init().
3480 * It is implementation-dependent whether a failure to initialize
3481 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003482 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003483psa_status_t psa_key_derivation_output_key(
3484 const psa_key_attributes_t *attributes,
3485 psa_key_derivation_operation_t *operation,
3486 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003487
Gilles Peskine35675b62019-05-16 17:26:11 +02003488/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003489 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003490 * Once a key derivation operation has been aborted, its capacity is zero.
3491 * Aborting an operation frees all associated resources except for the
3492 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003493 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003494 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003495 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003496 * psa_key_derivation_operation_init() or a zero value. In particular,
3497 * it is valid to call psa_key_derivation_abort() twice, or to call
3498 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003499 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003500 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003501 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003502 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003503 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003504 * \retval #PSA_SUCCESS
3505 * \retval #PSA_ERROR_BAD_STATE
3506 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3507 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003508 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003509 * \retval #PSA_ERROR_BAD_STATE
3510 * The library has not been previously initialized by psa_crypto_init().
3511 * It is implementation-dependent whether a failure to initialize
3512 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003513 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003514psa_status_t psa_key_derivation_abort(
3515 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003516
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003517/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003518 *
3519 * \warning The raw result of a key agreement algorithm such as finite-field
3520 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3521 * not be used directly as key material. It should instead be passed as
3522 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003523 * a key derivation, use psa_key_derivation_key_agreement() and other
3524 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003525 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003526 * \param alg The key agreement algorithm to compute
3527 * (\c PSA_ALG_XXX value such that
3528 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3529 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003530 * \param private_key Handle to the private key to use.
3531 * \param[in] peer_key Public key of the peer. It must be
3532 * in the same format that psa_import_key()
3533 * accepts. The standard formats for public
3534 * keys are documented in the documentation
3535 * of psa_export_public_key().
3536 * \param peer_key_length Size of \p peer_key in bytes.
3537 * \param[out] output Buffer where the decrypted message is to
3538 * be written.
3539 * \param output_size Size of the \c output buffer in bytes.
3540 * \param[out] output_length On success, the number of bytes
3541 * that make up the returned output.
3542 *
3543 * \retval #PSA_SUCCESS
3544 * Success.
3545 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003546 * \retval #PSA_ERROR_NOT_PERMITTED
3547 * \retval #PSA_ERROR_INVALID_ARGUMENT
3548 * \p alg is not a key agreement algorithm
3549 * \retval #PSA_ERROR_INVALID_ARGUMENT
3550 * \p private_key is not compatible with \p alg,
3551 * or \p peer_key is not valid for \p alg or not compatible with
3552 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003553 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3554 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003555 * \retval #PSA_ERROR_NOT_SUPPORTED
3556 * \p alg is not a supported key agreement algorithm.
3557 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3558 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3559 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003560 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003561 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003562 * \retval #PSA_ERROR_BAD_STATE
3563 * The library has not been previously initialized by psa_crypto_init().
3564 * It is implementation-dependent whether a failure to initialize
3565 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003566 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003567psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3568 psa_key_handle_t private_key,
3569 const uint8_t *peer_key,
3570 size_t peer_key_length,
3571 uint8_t *output,
3572 size_t output_size,
3573 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003574
Gilles Peskineea0fb492018-07-12 17:17:20 +02003575/**@}*/
3576
Gilles Peskineedd76872018-07-20 17:42:05 +02003577/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003578 * @{
3579 */
3580
3581/**
3582 * \brief Generate random bytes.
3583 *
3584 * \warning This function **can** fail! Callers MUST check the return status
3585 * and MUST NOT use the content of the output buffer if the return
3586 * status is not #PSA_SUCCESS.
3587 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003588 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003589 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003590 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003591 * \param output_size Number of bytes to generate and output.
3592 *
Gilles Peskine28538492018-07-11 17:34:00 +02003593 * \retval #PSA_SUCCESS
3594 * \retval #PSA_ERROR_NOT_SUPPORTED
3595 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003596 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003597 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3598 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003599 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003600 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003601 * The library has not been previously initialized by psa_crypto_init().
3602 * It is implementation-dependent whether a failure to initialize
3603 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003604 */
3605psa_status_t psa_generate_random(uint8_t *output,
3606 size_t output_size);
3607
3608/**
3609 * \brief Generate a key or key pair.
3610 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003611 * The key is generated randomly.
3612 * Its location, policy, type and size are taken from \p attributes.
3613 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003614 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003615 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003616 * the public exponent is 65537.
3617 * The modulus is a product of two probabilistic primes
3618 * between 2^{n-1} and 2^n where n is the bit size specified in the
3619 * attributes.
3620 *
Gilles Peskine20628592019-04-19 19:29:50 +02003621 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003622 * \param[out] handle On success, a handle to the newly created key.
3623 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003624 *
Gilles Peskine28538492018-07-11 17:34:00 +02003625 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003626 * Success.
3627 * If the key is persistent, the key material and the key's metadata
3628 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003629 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003630 * This is an attempt to create a persistent key, and there is
3631 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003632 * \retval #PSA_ERROR_NOT_SUPPORTED
3633 * \retval #PSA_ERROR_INVALID_ARGUMENT
3634 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3635 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3636 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3637 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003638 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003639 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3640 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003641 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003642 * The library has not been previously initialized by psa_crypto_init().
3643 * It is implementation-dependent whether a failure to initialize
3644 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003645 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003646psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003647 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003648
3649/**@}*/
3650
Gilles Peskinee59236f2018-01-27 23:32:46 +01003651#ifdef __cplusplus
3652}
3653#endif
3654
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003655/* The file "crypto_sizes.h" contains definitions for size calculation
3656 * macros whose definitions are implementation-specific. */
3657#include "crypto_sizes.h"
3658
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003659/* The file "crypto_struct.h" contains definitions for
3660 * implementation-specific structs that are declared above. */
3661#include "crypto_struct.h"
3662
3663/* The file "crypto_extra.h" contains vendor-specific definitions. This
3664 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003665#include "crypto_extra.h"
3666
3667#endif /* PSA_CRYPTO_H */