blob: 71b1de231d1e1fec41d4274de3e681674840f2be [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +020089 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +020090 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
Gilles Peskine105f67f2019-07-23 18:16:05 +020096/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +020097 * @{
98 */
99
Gilles Peskinea0c06552019-05-21 15:54:54 +0200100/** \def PSA_KEY_ATTRIBUTES_INIT
101 *
102 * This macro returns a suitable initializer for a key attribute structure
103 * of type #psa_key_attributes_t.
104 */
105#ifdef __DOXYGEN_ONLY__
106/* This is an example definition for documentation purposes.
107 * Implementations should define a suitable value in `crypto_struct.h`.
108 */
109#define PSA_KEY_ATTRIBUTES_INIT {0}
110#endif
111
112/** Return an initial value for a key attributes structure.
113 */
114static psa_key_attributes_t psa_key_attributes_init(void);
115
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200116/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200117 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200118 * If the attribute structure currently declares the key as volatile (which
119 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200120 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200121 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200122 * This function does not access storage, it merely stores the given
123 * value in the structure.
124 * The persistent key will be written to storage when the attribute
125 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200126 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200127 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200128 *
Gilles Peskine20628592019-04-19 19:29:50 +0200129 * This function may be declared as `static` (i.e. without external
130 * linkage). This function may be provided as a function-like macro,
131 * but in this case it must evaluate each of its arguments exactly once.
132 *
133 * \param[out] attributes The attribute structure to write to.
134 * \param id The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200135 */
136static void psa_set_key_id(psa_key_attributes_t *attributes,
137 psa_key_id_t id);
138
139/** Set the location of a persistent key.
140 *
141 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200142 * with psa_set_key_id(). By default, a key that has a persistent identifier
143 * is stored in the default storage area identifier by
144 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
145 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200146 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200147 * This function does not access storage, it merely stores the given
148 * value in the structure.
149 * The persistent key will be written to storage when the attribute
150 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200151 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200152 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200153 *
154 * This function may be declared as `static` (i.e. without external
155 * linkage). This function may be provided as a function-like macro,
156 * but in this case it must evaluate each of its arguments exactly once.
157 *
158 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200159 * \param lifetime The lifetime for the key.
160 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200161 * key will be volatile, and the key identifier
162 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200163 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200164static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
165 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200166
Gilles Peskine20628592019-04-19 19:29:50 +0200167/** Retrieve the key identifier from key attributes.
168 *
169 * This function may be declared as `static` (i.e. without external
170 * linkage). This function may be provided as a function-like macro,
171 * but in this case it must evaluate its argument exactly once.
172 *
173 * \param[in] attributes The key attribute structure to query.
174 *
175 * \return The persistent identifier stored in the attribute structure.
176 * This value is unspecified if the attribute structure declares
177 * the key as volatile.
178 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200179static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
180
Gilles Peskine20628592019-04-19 19:29:50 +0200181/** Retrieve the lifetime from key attributes.
182 *
183 * This function may be declared as `static` (i.e. without external
184 * linkage). This function may be provided as a function-like macro,
185 * but in this case it must evaluate its argument exactly once.
186 *
187 * \param[in] attributes The key attribute structure to query.
188 *
189 * \return The lifetime value stored in the attribute structure.
190 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200191static psa_key_lifetime_t psa_get_key_lifetime(
192 const psa_key_attributes_t *attributes);
193
Gilles Peskine20628592019-04-19 19:29:50 +0200194/** Declare usage flags for a key.
195 *
196 * Usage flags are part of a key's usage policy. They encode what
197 * kind of operations are permitted on the key. For more details,
198 * refer to the documentation of the type #psa_key_usage_t.
199 *
200 * This function overwrites any usage flags
201 * previously set in \p attributes.
202 *
203 * This function may be declared as `static` (i.e. without external
204 * linkage). This function may be provided as a function-like macro,
205 * but in this case it must evaluate each of its arguments exactly once.
206 *
207 * \param[out] attributes The attribute structure to write to.
208 * \param usage_flags The usage flags to write.
209 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200210static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
211 psa_key_usage_t usage_flags);
212
Gilles Peskine20628592019-04-19 19:29:50 +0200213/** Retrieve the usage flags from key attributes.
214 *
215 * This function may be declared as `static` (i.e. without external
216 * linkage). This function may be provided as a function-like macro,
217 * but in this case it must evaluate its argument exactly once.
218 *
219 * \param[in] attributes The key attribute structure to query.
220 *
221 * \return The usage flags stored in the attribute structure.
222 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200223static psa_key_usage_t psa_get_key_usage_flags(
224 const psa_key_attributes_t *attributes);
225
Gilles Peskine20628592019-04-19 19:29:50 +0200226/** Declare the permitted algorithm policy for a key.
227 *
228 * The permitted algorithm policy of a key encodes which algorithm or
229 * algorithms are permitted to be used with this key.
230 *
231 * This function overwrites any algorithm policy
232 * previously set in \p attributes.
233 *
234 * This function may be declared as `static` (i.e. without external
235 * linkage). This function may be provided as a function-like macro,
236 * but in this case it must evaluate each of its arguments exactly once.
237 *
238 * \param[out] attributes The attribute structure to write to.
239 * \param alg The permitted algorithm policy to write.
240 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200241static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
242 psa_algorithm_t alg);
243
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100244
Gilles Peskine20628592019-04-19 19:29:50 +0200245/** Retrieve the algorithm policy from key attributes.
246 *
247 * This function may be declared as `static` (i.e. without external
248 * linkage). This function may be provided as a function-like macro,
249 * but in this case it must evaluate its argument exactly once.
250 *
251 * \param[in] attributes The key attribute structure to query.
252 *
253 * \return The algorithm stored in the attribute structure.
254 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200255static psa_algorithm_t psa_get_key_algorithm(
256 const psa_key_attributes_t *attributes);
257
Gilles Peskine20628592019-04-19 19:29:50 +0200258/** Declare the type of a key.
259 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200260 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200261 * previously set in \p attributes.
262 *
263 * This function may be declared as `static` (i.e. without external
264 * linkage). This function may be provided as a function-like macro,
265 * but in this case it must evaluate each of its arguments exactly once.
266 *
267 * \param[out] attributes The attribute structure to write to.
268 * \param type The key type to write.
269 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200270static void psa_set_key_type(psa_key_attributes_t *attributes,
271 psa_key_type_t type);
272
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100273
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200274/** Declare the size of a key.
275 *
276 * This function overwrites any key size previously set in \p attributes.
277 *
278 * This function may be declared as `static` (i.e. without external
279 * linkage). This function may be provided as a function-like macro,
280 * but in this case it must evaluate each of its arguments exactly once.
281 *
282 * \param[out] attributes The attribute structure to write to.
283 * \param bits The key size in bits.
284 */
285static void psa_set_key_bits(psa_key_attributes_t *attributes,
286 size_t bits);
287
Gilles Peskine20628592019-04-19 19:29:50 +0200288/** Retrieve the key type from key attributes.
289 *
290 * This function may be declared as `static` (i.e. without external
291 * linkage). This function may be provided as a function-like macro,
292 * but in this case it must evaluate its argument exactly once.
293 *
294 * \param[in] attributes The key attribute structure to query.
295 *
296 * \return The key type stored in the attribute structure.
297 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200298static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
299
Gilles Peskine20628592019-04-19 19:29:50 +0200300/** Retrieve the key size from key attributes.
301 *
302 * This function may be declared as `static` (i.e. without external
303 * linkage). This function may be provided as a function-like macro,
304 * but in this case it must evaluate its argument exactly once.
305 *
306 * \param[in] attributes The key attribute structure to query.
307 *
308 * \return The key size stored in the attribute structure, in bits.
309 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200310static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
311
Gilles Peskine20628592019-04-19 19:29:50 +0200312/** Retrieve the attributes of a key.
313 *
314 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200315 * psa_reset_key_attributes(). It then copies the attributes of
316 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200317 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200318 * \note This function may allocate memory or other resources.
319 * Once you have called this function on an attribute structure,
320 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200321 *
Gilles Peskine20628592019-04-19 19:29:50 +0200322 * \param[in] handle Handle to the key to query.
323 * \param[in,out] attributes On success, the attributes of the key.
324 * On failure, equivalent to a
325 * freshly-initialized structure.
326 *
327 * \retval #PSA_SUCCESS
328 * \retval #PSA_ERROR_INVALID_HANDLE
329 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
330 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100331 * \retval #PSA_ERROR_CORRUPTION_DETECTED
332 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100333 * \retval #PSA_ERROR_BAD_STATE
334 * The library has not been previously initialized by psa_crypto_init().
335 * It is implementation-dependent whether a failure to initialize
336 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200337 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200338psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
339 psa_key_attributes_t *attributes);
340
Gilles Peskine20628592019-04-19 19:29:50 +0200341/** Reset a key attribute structure to a freshly initialized state.
342 *
343 * You must initialize the attribute structure as described in the
344 * documentation of the type #psa_key_attributes_t before calling this
345 * function. Once the structure has been initialized, you may call this
346 * function at any time.
347 *
348 * This function frees any auxiliary resources that the structure
349 * may contain.
350 *
351 * \param[in,out] attributes The attribute structure to reset.
352 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200353void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200354
Gilles Peskine87a5e562019-04-17 12:28:25 +0200355/**@}*/
356
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357/** \defgroup key_management Key management
358 * @{
359 */
360
Gilles Peskinef535eb22018-11-30 14:08:36 +0100361/** Open a handle to an existing persistent key.
362 *
Gilles Peskine4754cde2019-05-21 15:56:29 +0200363 * Open a handle to a persistent key. A key is persistent if it was created
364 * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
365 * always has a nonzero key identifier, set with psa_set_key_id() when
366 * creating the key. Implementations may provide additional pre-provisioned
Andrew Thoelke203491c2019-08-21 17:55:30 +0100367 * keys that can be opened with psa_open_key(). Such keys have a key identifier
368 * in the vendor range, as documented in the description of #psa_key_id_t.
Gilles Peskine4754cde2019-05-21 15:56:29 +0200369 *
370 * The application must eventually close the handle with psa_close_key()
371 * to release associated resources. If the application dies without calling
372 * psa_close_key(), the implementation should perform the equivalent of a
373 * call to psa_close_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100374 *
Andrew Thoelke9741b112019-08-21 18:20:41 +0100375 * Some implementations permit an application to open the same key multiple
376 * times. Applications that rely on this behavior will not be portable to
377 * implementations that only permit a single key handle to be opened. See
378 * also :ref:\`key-handles\`.
379 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100380 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100381 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100382 *
383 * \retval #PSA_SUCCESS
384 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100385 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100386 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Andrew Thoelke9741b112019-08-21 18:20:41 +0100387 * The implementation does not have sufficient resources to open the
388 * key. This can be due to reaching an implementation limit on the
389 * number of open keys, the number of open key handles, or available
390 * memory.
David Saadab4ecc272019-02-14 13:48:10 +0200391 * \retval #PSA_ERROR_DOES_NOT_EXIST
Andrew Thoelke9741b112019-08-21 18:20:41 +0100392 * There is no persistent key with key identifier \p id.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100393 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrew Thoelke9741b112019-08-21 18:20:41 +0100394 * \p id is not a valid persistent key identifier.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100395 * \retval #PSA_ERROR_NOT_PERMITTED
396 * The specified key exists, but the application does not have the
397 * permission to access it. Note that this specification does not
398 * define any way to create such a key, but it may be possible
399 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200400 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawd789dc12019-08-12 15:06:48 +0100401 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine225010f2019-05-06 18:44:55 +0200402 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100403 * \retval #PSA_ERROR_BAD_STATE
404 * The library has not been previously initialized by psa_crypto_init().
405 * It is implementation-dependent whether a failure to initialize
406 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100407 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200408psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100409 psa_key_handle_t *handle);
410
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100411
Gilles Peskinef535eb22018-11-30 14:08:36 +0100412/** Close a key handle.
413 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100414 * If the handle designates a volatile key, this will destroy the key material
415 * and free all associated resources, just like psa_destroy_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100416 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100417 * If this is the last open handle to a persistent key, then closing the handle
418 * will free all resources associated with the key in volatile memory. The key
419 * data in persistent storage is not affected and can be opened again later
420 * with a call to psa_open_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100421 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100422 * Closing the key handle makes the handle invalid, and the key handle
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100423 * must not be used again by the application.
Andrew Thoelke3daba812019-08-21 22:46:56 +0100424 *
425 * If the key is currently in use in a multipart operation, then closing the
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100426 * last remaining handle to the key will abort the multipart operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100427 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100428 * \param handle The key handle to close.
429 *
430 * \retval #PSA_SUCCESS
431 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100432 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawf97c8522019-08-15 13:27:12 +0100433 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100434 * \retval #PSA_ERROR_BAD_STATE
435 * The library has not been previously initialized by psa_crypto_init().
436 * It is implementation-dependent whether a failure to initialize
437 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100438 */
439psa_status_t psa_close_key(psa_key_handle_t handle);
440
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100441/**@}*/
442
443/** \defgroup import_export Key import and export
444 * @{
445 */
446
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100447/**
448 * \brief Import a key in binary format.
449 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100450 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100451 * documentation of psa_export_public_key() for the format of public keys
452 * and to the documentation of psa_export_key() for the format for
453 * other key types.
454 *
455 * This specification supports a single format for each key type.
456 * Implementations may support other formats as long as the standard
457 * format is supported. Implementations that support other formats
458 * should ensure that the formats are clearly unambiguous so as to
459 * minimize the risk that an invalid input is accidentally interpreted
460 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100461 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100462
Gilles Peskine20628592019-04-19 19:29:50 +0200463 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200464 * The key size is always determined from the
465 * \p data buffer.
466 * If the key size in \p attributes is nonzero,
467 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200468 * \param[out] handle On success, a handle to the newly created key.
469 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100470 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200471 * buffer is interpreted according to the type declared
472 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200473 * All implementations must support at least the format
474 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100475 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200476 * the chosen type. Implementations may allow other
477 * formats, but should be conservative: implementations
478 * should err on the side of rejecting content if it
479 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200480 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100481 *
Gilles Peskine28538492018-07-11 17:34:00 +0200482 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100483 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100484 * If the key is persistent, the key material and the key's metadata
485 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200486 * \retval #PSA_ERROR_ALREADY_EXISTS
487 * This is an attempt to create a persistent key, and there is
488 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200489 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200490 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200491 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200492 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200493 * The key attributes, as a whole, are invalid.
494 * \retval #PSA_ERROR_INVALID_ARGUMENT
495 * The key data is not correctly formatted.
496 * \retval #PSA_ERROR_INVALID_ARGUMENT
497 * The size in \p attributes is nonzero and does not match the size
498 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200499 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
500 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
501 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100502 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200503 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200504 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300505 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw97d3bc32019-08-08 14:40:04 +0100506 * \p operation is either not initialized or is in use
507 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300508 * The library has not been previously initialized by psa_crypto_init().
509 * It is implementation-dependent whether a failure to initialize
510 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100511 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200512psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100513 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200514 size_t data_length,
515 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100516
517/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100518 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200519 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100520 * This function destroys a key from both volatile
Gilles Peskine154bd952018-04-19 08:38:16 +0200521 * memory and, if applicable, non-volatile storage. Implementations shall
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100522 * make a best effort to ensure that that the key material cannot be recovered.
Gilles Peskine154bd952018-04-19 08:38:16 +0200523 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100524 * This function also erases any metadata such as policies and frees all
525 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200526 *
Andrew Thoelke07f16b72019-08-21 22:48:47 +0100527 * Destroying a key will invalidate all existing handles to the key.
528 *
529 * If the key is currently in use in a multipart operation, then destroying the
530 * key will abort the multipart operation.
531 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100532 * \param handle Handle to the key to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100533 *
Gilles Peskine28538492018-07-11 17:34:00 +0200534 * \retval #PSA_SUCCESS
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100535 * The key material has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200536 * \retval #PSA_ERROR_NOT_PERMITTED
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100537 * The key cannot be erased because it is
Gilles Peskine65eb8582018-04-19 08:28:58 +0200538 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100539 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200540 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200541 * There was an failure in communication with the cryptoprocessor.
542 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200543 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200544 * The storage is corrupted. Implementations shall make a best effort
545 * to erase key material even in this stage, however applications
546 * should be aware that it may be impossible to guarantee that the
547 * key material is not recoverable in such cases.
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200548 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200549 * An unexpected condition which is not a storage corruption or
550 * a communication failure occurred. The cryptoprocessor may have
551 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300552 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300553 * The library has not been previously initialized by psa_crypto_init().
554 * It is implementation-dependent whether a failure to initialize
555 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100556 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100557psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100558
559/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100560 * \brief Export a key in binary format.
561 *
562 * The output of this function can be passed to psa_import_key() to
563 * create an equivalent object.
564 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100565 * If the implementation of psa_import_key() supports other formats
566 * beyond the format specified here, the output from psa_export_key()
567 * must use the representation specified here, not the original
568 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100569 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100570 * For standard key types, the output format is as follows:
571 *
572 * - For symmetric keys (including MAC keys), the format is the
573 * raw bytes of the key.
574 * - For DES, the key data consists of 8 bytes. The parity bits must be
575 * correct.
576 * - For Triple-DES, the format is the concatenation of the
577 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200578 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200579 * is the non-encrypted DER encoding of the representation defined by
580 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
581 * ```
582 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200583 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200584 * modulus INTEGER, -- n
585 * publicExponent INTEGER, -- e
586 * privateExponent INTEGER, -- d
587 * prime1 INTEGER, -- p
588 * prime2 INTEGER, -- q
589 * exponent1 INTEGER, -- d mod (p-1)
590 * exponent2 INTEGER, -- d mod (q-1)
591 * coefficient INTEGER, -- (inverse of q) mod p
592 * }
593 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200594 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200595 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100596 * a representation of the private value as a `ceiling(m/8)`-byte string
597 * where `m` is the bit size associated with the curve, i.e. the bit size
598 * of the order of the curve's coordinate field. This byte string is
599 * in little-endian order for Montgomery curves (curve types
600 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
601 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
602 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100603 * This is the content of the `privateKey` field of the `ECPrivateKey`
604 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200605 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200606 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000607 * format is the representation of the private key `x` as a big-endian byte
608 * string. The length of the byte string is the private key size in bytes
609 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200610 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
611 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100612 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200613 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
614 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100615 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200616 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200617 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200618 * \param[out] data_length On success, the number of bytes
619 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100620 *
Gilles Peskine28538492018-07-11 17:34:00 +0200621 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100622 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200623 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200624 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100625 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200626 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
627 * The size of the \p data buffer is too small. You can determine a
628 * sufficient buffer size by calling
629 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
630 * where \c type is the key type
631 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200632 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
633 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200634 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100635 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100636 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300637 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300638 * The library has not been previously initialized by psa_crypto_init().
639 * It is implementation-dependent whether a failure to initialize
640 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100641 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100642psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100643 uint8_t *data,
644 size_t data_size,
645 size_t *data_length);
646
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100647/**
648 * \brief Export a public key or the public part of a key pair in binary format.
649 *
650 * The output of this function can be passed to psa_import_key() to
651 * create an object that is equivalent to the public key.
652 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000653 * This specification supports a single format for each key type.
654 * Implementations may support other formats as long as the standard
655 * format is supported. Implementations that support other formats
656 * should ensure that the formats are clearly unambiguous so as to
657 * minimize the risk that an invalid input is accidentally interpreted
658 * according to a different format.
659 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000660 * For standard key types, the output format is as follows:
661 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
662 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
663 * ```
664 * RSAPublicKey ::= SEQUENCE {
665 * modulus INTEGER, -- n
666 * publicExponent INTEGER } -- e
667 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000668 * - For elliptic curve public keys (key types for which
669 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
670 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
671 * Let `m` be the bit size associated with the curve, i.e. the bit size of
672 * `q` for a curve over `F_q`. The representation consists of:
673 * - The byte 0x04;
674 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
675 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200676 * - For Diffie-Hellman key exchange public keys (key types for which
677 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000678 * the format is the representation of the public key `y = g^x mod p` as a
679 * big-endian byte string. The length of the byte string is the length of the
680 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100681 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200682 * Exporting a public key object or the public part of a key pair is
683 * always permitted, regardless of the key's usage flags.
684 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100685 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200686 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200687 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200688 * \param[out] data_length On success, the number of bytes
689 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100690 *
Gilles Peskine28538492018-07-11 17:34:00 +0200691 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100692 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200693 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200694 * The key is neither a public key nor a key pair.
695 * \retval #PSA_ERROR_NOT_SUPPORTED
696 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
697 * The size of the \p data buffer is too small. You can determine a
698 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200699 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200700 * where \c type is the key type
701 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200702 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
703 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200704 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100705 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100706 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300707 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300708 * The library has not been previously initialized by psa_crypto_init().
709 * It is implementation-dependent whether a failure to initialize
710 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100711 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100712psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100713 uint8_t *data,
714 size_t data_size,
715 size_t *data_length);
716
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100717/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100718 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100719 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000720 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100721 * This function is primarily useful to copy a key from one location
722 * to another, since it populates a key using the material from
723 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200724 *
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100725 * This function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100726 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100727 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200728 * The policy on the source key must have the usage flag
729 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200730 * This flag is sufficient to permit the copy if the key has the lifetime
731 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
732 * Some secure elements do not provide a way to copy a key without
733 * making it extractable from the secure element. If a key is located
734 * in such a secure element, then the key must have both usage flags
735 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
736 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200737 *
Gilles Peskine20628592019-04-19 19:29:50 +0200738 * The resulting key may only be used in a way that conforms to
739 * both the policy of the original key and the policy specified in
740 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100741 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200742 * usage flags on the source policy and the usage flags in \p attributes.
743 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100744 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200745 * - If either of the policies allows an algorithm and the other policy
746 * allows a wildcard-based algorithm policy that includes this algorithm,
747 * the resulting key allows the same algorithm.
748 * - If the policies do not allow any algorithm in common, this function
749 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200750 *
Gilles Peskine20628592019-04-19 19:29:50 +0200751 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100752 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200753 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100754 * \param source_handle The key to copy. It must be a valid key handle.
Gilles Peskine20628592019-04-19 19:29:50 +0200755 * \param[in] attributes The attributes for the new key.
756 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200757 * - The key type and size may be 0. If either is
758 * nonzero, it must match the corresponding
759 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200760 * - The key location (the lifetime and, for
761 * persistent keys, the key identifier) is
762 * used directly.
763 * - The policy constraints (usage flags and
764 * algorithm policy) are combined from
765 * the source key and \p attributes so that
766 * both sets of restrictions apply, as
767 * described in the documentation of this function.
768 * \param[out] target_handle On success, a handle to the newly created key.
769 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200770 *
771 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100772 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200773 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200774 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200775 * This is an attempt to create a persistent key, and there is
776 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200777 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200778 * The lifetime or identifier in \p attributes are invalid.
779 * \retval #PSA_ERROR_INVALID_ARGUMENT
780 * The policy constraints on the source and specified in
781 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200782 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200783 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200784 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100785 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200786 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
787 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100788 * The source key is not exportable and its lifetime does not
789 * allow copying it to the target's lifetime.
790 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
791 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200792 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
793 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shaw60b03202019-08-06 17:26:16 +0100794 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200795 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100796 * \retval #PSA_ERROR_BAD_STATE
797 * The library has not been previously initialized by psa_crypto_init().
798 * It is implementation-dependent whether a failure to initialize
799 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100800 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100801psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200802 const psa_key_attributes_t *attributes,
803 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100804
805/**@}*/
806
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100807/** \defgroup hash Message digests
808 * @{
809 */
810
Gilles Peskine69647a42019-01-14 20:18:12 +0100811/** Calculate the hash (digest) of a message.
812 *
813 * \note To verify the hash of a message against an
814 * expected value, use psa_hash_compare() instead.
815 *
816 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
817 * such that #PSA_ALG_IS_HASH(\p alg) is true).
818 * \param[in] input Buffer containing the message to hash.
819 * \param input_length Size of the \p input buffer in bytes.
820 * \param[out] hash Buffer where the hash is to be written.
821 * \param hash_size Size of the \p hash buffer in bytes.
822 * \param[out] hash_length On success, the number of bytes
823 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100824 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100825 *
826 * \retval #PSA_SUCCESS
827 * Success.
828 * \retval #PSA_ERROR_NOT_SUPPORTED
829 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100830 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
831 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100832 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
833 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
834 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200835 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw15731c12019-08-06 16:21:00 +0100836 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100837 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100838 * \retval #PSA_ERROR_BAD_STATE
839 * The library has not been previously initialized by psa_crypto_init().
840 * It is implementation-dependent whether a failure to initialize
841 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100842 */
843psa_status_t psa_hash_compute(psa_algorithm_t alg,
844 const uint8_t *input,
845 size_t input_length,
846 uint8_t *hash,
847 size_t hash_size,
848 size_t *hash_length);
849
850/** Calculate the hash (digest) of a message and compare it with a
851 * reference value.
852 *
853 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
854 * such that #PSA_ALG_IS_HASH(\p alg) is true).
855 * \param[in] input Buffer containing the message to hash.
856 * \param input_length Size of the \p input buffer in bytes.
857 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100858 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100859 *
860 * \retval #PSA_SUCCESS
861 * The expected hash is identical to the actual hash of the input.
862 * \retval #PSA_ERROR_INVALID_SIGNATURE
863 * The hash of the message was calculated successfully, but it
864 * differs from the expected hash.
865 * \retval #PSA_ERROR_NOT_SUPPORTED
866 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100867 * \retval #PSA_ERROR_INVALID_ARGUMENT
868 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100869 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
870 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
871 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200872 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2a889782019-08-06 17:22:41 +0100873 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100874 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100875 * \retval #PSA_ERROR_BAD_STATE
876 * The library has not been previously initialized by psa_crypto_init().
877 * It is implementation-dependent whether a failure to initialize
878 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100879 */
880psa_status_t psa_hash_compare(psa_algorithm_t alg,
881 const uint8_t *input,
882 size_t input_length,
883 const uint8_t *hash,
884 const size_t hash_length);
885
Gilles Peskine308b91d2018-02-08 09:47:44 +0100886/** The type of the state data structure for multipart hash operations.
887 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000888 * Before calling any function on a hash operation object, the application must
889 * initialize it by any of the following means:
890 * - Set the structure to all-bits-zero, for example:
891 * \code
892 * psa_hash_operation_t operation;
893 * memset(&operation, 0, sizeof(operation));
894 * \endcode
895 * - Initialize the structure to logical zero values, for example:
896 * \code
897 * psa_hash_operation_t operation = {0};
898 * \endcode
899 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
900 * for example:
901 * \code
902 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
903 * \endcode
904 * - Assign the result of the function psa_hash_operation_init()
905 * to the structure, for example:
906 * \code
907 * psa_hash_operation_t operation;
908 * operation = psa_hash_operation_init();
909 * \endcode
910 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100911 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100912 * make any assumptions about the content of this structure except
913 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100914typedef struct psa_hash_operation_s psa_hash_operation_t;
915
Jaeden Amero6a25b412019-01-04 11:47:44 +0000916/** \def PSA_HASH_OPERATION_INIT
917 *
918 * This macro returns a suitable initializer for a hash operation object
919 * of type #psa_hash_operation_t.
920 */
921#ifdef __DOXYGEN_ONLY__
922/* This is an example definition for documentation purposes.
923 * Implementations should define a suitable value in `crypto_struct.h`.
924 */
925#define PSA_HASH_OPERATION_INIT {0}
926#endif
927
928/** Return an initial value for a hash operation object.
929 */
930static psa_hash_operation_t psa_hash_operation_init(void);
931
Gilles Peskinef45adda2019-01-14 18:29:18 +0100932/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100933 *
934 * The sequence of operations to calculate a hash (message digest)
935 * is as follows:
936 * -# Allocate an operation object which will be passed to all the functions
937 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000938 * -# Initialize the operation object with one of the methods described in the
939 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200940 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100941 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100942 * of the message each time. The hash that is calculated is the hash
943 * of the concatenation of these messages in order.
944 * -# To calculate the hash, call psa_hash_finish().
945 * To compare the hash with an expected value, call psa_hash_verify().
946 *
947 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000948 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100949 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200950 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100951 * eventually terminate the operation. The following events terminate an
952 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100953 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100954 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100955 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000956 * \param[in,out] operation The operation object to set up. It must have
957 * been initialized as per the documentation for
958 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200959 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
960 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100961 *
Gilles Peskine28538492018-07-11 17:34:00 +0200962 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100963 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200964 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100965 * \p alg is not a supported hash algorithm.
966 * \retval #PSA_ERROR_INVALID_ARGUMENT
967 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100968 * \retval #PSA_ERROR_BAD_STATE
969 * The operation state is not valid (already set up and not
970 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200971 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
972 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
973 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200974 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100975 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawe970d652019-08-08 14:40:04 +0100976 * \p operation is either not initialized or is in use
977 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100978 * The library has not been previously initialized by psa_crypto_init().
979 * It is implementation-dependent whether a failure to initialize
980 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100981 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200982psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100983 psa_algorithm_t alg);
984
Gilles Peskine308b91d2018-02-08 09:47:44 +0100985/** Add a message fragment to a multipart hash operation.
986 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200987 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100988 *
989 * If this function returns an error status, the operation becomes inactive.
990 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200991 * \param[in,out] operation Active hash operation.
992 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200993 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100994 *
Gilles Peskine28538492018-07-11 17:34:00 +0200995 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100996 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200997 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100998 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200999 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1000 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1001 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001002 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001003 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001004 * The operation state is not valid.
1005 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001006 * The library has not been previously initialized by psa_crypto_init().
1007 * It is implementation-dependent whether a failure to initialize
1008 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001009 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001010psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1011 const uint8_t *input,
1012 size_t input_length);
1013
Gilles Peskine308b91d2018-02-08 09:47:44 +01001014/** Finish the calculation of the hash of a message.
1015 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001016 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001017 * This function calculates the hash of the message formed by concatenating
1018 * the inputs passed to preceding calls to psa_hash_update().
1019 *
1020 * When this function returns, the operation becomes inactive.
1021 *
1022 * \warning Applications should not call this function if they expect
1023 * a specific value for the hash. Call psa_hash_verify() instead.
1024 * Beware that comparing integrity or authenticity data such as
1025 * hash values with a function such as \c memcmp is risky
1026 * because the time taken by the comparison may leak information
1027 * about the hashed data which could allow an attacker to guess
1028 * a valid hash and thereby bypass security controls.
1029 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001030 * \param[in,out] operation Active hash operation.
1031 * \param[out] hash Buffer where the hash is to be written.
1032 * \param hash_size Size of the \p hash buffer in bytes.
1033 * \param[out] hash_length On success, the number of bytes
1034 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001035 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001036 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001037 *
Gilles Peskine28538492018-07-11 17:34:00 +02001038 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001039 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001040 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001041 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001042 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001043 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001044 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001045 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001046 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1047 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1048 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001049 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001050 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001051 * The operation state is not valid.
1052 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001053 * The library has not been previously initialized by psa_crypto_init().
1054 * It is implementation-dependent whether a failure to initialize
1055 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001056 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001057psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1058 uint8_t *hash,
1059 size_t hash_size,
1060 size_t *hash_length);
1061
Gilles Peskine308b91d2018-02-08 09:47:44 +01001062/** Finish the calculation of the hash of a message and compare it with
1063 * an expected value.
1064 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001065 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001066 * This function calculates the hash of the message formed by concatenating
1067 * the inputs passed to preceding calls to psa_hash_update(). It then
1068 * compares the calculated hash with the expected hash passed as a
1069 * parameter to this function.
1070 *
1071 * When this function returns, the operation becomes inactive.
1072 *
Gilles Peskine19067982018-03-20 17:54:53 +01001073 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074 * comparison between the actual hash and the expected hash is performed
1075 * in constant time.
1076 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001077 * \param[in,out] operation Active hash operation.
1078 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001079 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001080 *
Gilles Peskine28538492018-07-11 17:34:00 +02001081 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001082 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001083 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001084 * The hash of the message was calculated successfully, but it
1085 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001086 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001087 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001088 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1089 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1090 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001091 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001092 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001093 * The operation state is not valid.
1094 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001095 * The library has not been previously initialized by psa_crypto_init().
1096 * It is implementation-dependent whether a failure to initialize
1097 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001098 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001099psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1100 const uint8_t *hash,
1101 size_t hash_length);
1102
Gilles Peskine308b91d2018-02-08 09:47:44 +01001103/** Abort a hash operation.
1104 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001105 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001106 * \p operation structure itself. Once aborted, the operation object
1107 * can be reused for another operation by calling
1108 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001109 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001110 * You may call this function any time after the operation object has
1111 * been initialized by any of the following methods:
1112 * - A call to psa_hash_setup(), whether it succeeds or not.
1113 * - Initializing the \c struct to all-bits-zero.
1114 * - Initializing the \c struct to logical zeros, e.g.
1115 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001116 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001117 * In particular, calling psa_hash_abort() after the operation has been
1118 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1119 * psa_hash_verify() is safe and has no effect.
1120 *
1121 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001122 *
Gilles Peskine28538492018-07-11 17:34:00 +02001123 * \retval #PSA_SUCCESS
1124 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001125 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001126 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1127 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001128 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001129 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001130 * The operation state is not valid.
1131 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001132 * 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
1155 * \p source_operation is not an active hash operation.
1156 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001157 * \p target_operation is active.
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
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001163 * The operation state is either not initialized or has already been setup.
1164 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001165 * The library has not been previously initialized by psa_crypto_init().
1166 * It is implementation-dependent whether a failure to initialize
1167 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001168 */
1169psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1170 psa_hash_operation_t *target_operation);
1171
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001172/**@}*/
1173
Gilles Peskine8c9def32018-02-08 10:02:12 +01001174/** \defgroup MAC Message authentication codes
1175 * @{
1176 */
1177
Gilles Peskine69647a42019-01-14 20:18:12 +01001178/** Calculate the MAC (message authentication code) of a message.
1179 *
1180 * \note To verify the MAC of a message against an
1181 * expected value, use psa_mac_verify() instead.
1182 * Beware that comparing integrity or authenticity data such as
1183 * MAC values with a function such as \c memcmp is risky
1184 * because the time taken by the comparison may leak information
1185 * about the MAC value which could allow an attacker to guess
1186 * a valid MAC and thereby bypass security controls.
1187 *
1188 * \param handle Handle to the key to use for the operation.
1189 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001190 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001191 * \param[in] input Buffer containing the input message.
1192 * \param input_length Size of the \p input buffer in bytes.
1193 * \param[out] mac Buffer where the MAC value is to be written.
1194 * \param mac_size Size of the \p mac buffer in bytes.
1195 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001196 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001197 *
1198 * \retval #PSA_SUCCESS
1199 * Success.
1200 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001201 * \retval #PSA_ERROR_NOT_PERMITTED
1202 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001203 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001204 * \retval #PSA_ERROR_NOT_SUPPORTED
1205 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001206 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1207 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001208 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1209 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1210 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001211 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001212 * \retval #PSA_ERROR_STORAGE_FAILURE
1213 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001214 * \retval #PSA_ERROR_BAD_STATE
1215 * The library has not been previously initialized by psa_crypto_init().
1216 * It is implementation-dependent whether a failure to initialize
1217 * results in this error code.
1218 */
1219psa_status_t psa_mac_compute(psa_key_handle_t handle,
1220 psa_algorithm_t alg,
1221 const uint8_t *input,
1222 size_t input_length,
1223 uint8_t *mac,
1224 size_t mac_size,
1225 size_t *mac_length);
1226
1227/** Calculate the MAC of a message and compare it with a reference value.
1228 *
1229 * \param handle Handle to the key to use for the operation.
1230 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001231 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001232 * \param[in] input Buffer containing the input message.
1233 * \param input_length Size of the \p input buffer in bytes.
1234 * \param[out] mac Buffer containing the expected MAC value.
1235 * \param mac_length Size of the \p mac buffer in bytes.
1236 *
1237 * \retval #PSA_SUCCESS
1238 * The expected MAC is identical to the actual MAC of the input.
1239 * \retval #PSA_ERROR_INVALID_SIGNATURE
1240 * The MAC of the message was calculated successfully, but it
1241 * differs from the expected value.
1242 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001243 * \retval #PSA_ERROR_NOT_PERMITTED
1244 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001245 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001246 * \retval #PSA_ERROR_NOT_SUPPORTED
1247 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shaw1f42a842019-08-07 15:59:33 +01001248 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1249 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001250 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1251 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1252 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001253 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001254 * \retval #PSA_ERROR_STORAGE_FAILURE
1255 * The key could not be retrieved from storage.
1256 * \retval #PSA_ERROR_BAD_STATE
1257 * The library has not been previously initialized by psa_crypto_init().
1258 * It is implementation-dependent whether a failure to initialize
1259 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001260 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001261psa_status_t psa_mac_verify(psa_key_handle_t handle,
1262 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001263 const uint8_t *input,
1264 size_t input_length,
1265 const uint8_t *mac,
1266 const size_t mac_length);
1267
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001268/** The type of the state data structure for multipart MAC operations.
1269 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001270 * Before calling any function on a MAC operation object, the application must
1271 * initialize it by any of the following means:
1272 * - Set the structure to all-bits-zero, for example:
1273 * \code
1274 * psa_mac_operation_t operation;
1275 * memset(&operation, 0, sizeof(operation));
1276 * \endcode
1277 * - Initialize the structure to logical zero values, for example:
1278 * \code
1279 * psa_mac_operation_t operation = {0};
1280 * \endcode
1281 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1282 * for example:
1283 * \code
1284 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1285 * \endcode
1286 * - Assign the result of the function psa_mac_operation_init()
1287 * to the structure, for example:
1288 * \code
1289 * psa_mac_operation_t operation;
1290 * operation = psa_mac_operation_init();
1291 * \endcode
1292 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001293 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001294 * make any assumptions about the content of this structure except
1295 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001296typedef struct psa_mac_operation_s psa_mac_operation_t;
1297
Jaeden Amero769ce272019-01-04 11:48:03 +00001298/** \def PSA_MAC_OPERATION_INIT
1299 *
1300 * This macro returns a suitable initializer for a MAC operation object of type
1301 * #psa_mac_operation_t.
1302 */
1303#ifdef __DOXYGEN_ONLY__
1304/* This is an example definition for documentation purposes.
1305 * Implementations should define a suitable value in `crypto_struct.h`.
1306 */
1307#define PSA_MAC_OPERATION_INIT {0}
1308#endif
1309
1310/** Return an initial value for a MAC operation object.
1311 */
1312static psa_mac_operation_t psa_mac_operation_init(void);
1313
Gilles Peskinef45adda2019-01-14 18:29:18 +01001314/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001315 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001316 * This function sets up the calculation of the MAC
1317 * (message authentication code) of a byte string.
1318 * To verify the MAC of a message against an
1319 * expected value, use psa_mac_verify_setup() instead.
1320 *
1321 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001322 * -# Allocate an operation object which will be passed to all the functions
1323 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001324 * -# Initialize the operation object with one of the methods described in the
1325 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001326 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001327 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1328 * of the message each time. The MAC that is calculated is the MAC
1329 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001330 * -# At the end of the message, call psa_mac_sign_finish() to finish
1331 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001332 *
1333 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001334 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001335 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001336 * After a successful call to psa_mac_sign_setup(), the application must
1337 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001338 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001339 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001340 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001341 * \param[in,out] operation The operation object to set up. It must have
1342 * been initialized as per the documentation for
1343 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001344 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001345 * It must remain valid until the operation
1346 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001347 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001348 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001349 *
Gilles Peskine28538492018-07-11 17:34:00 +02001350 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001351 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001352 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001353 * \retval #PSA_ERROR_NOT_PERMITTED
1354 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001355 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001356 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001357 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001358 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1359 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1360 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001361 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001362 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001363 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001364 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001365 * The operation state is not valid (already set up and not
1366 * subsequently completed).
1367 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001368 * The library has not been previously initialized by psa_crypto_init().
1369 * It is implementation-dependent whether a failure to initialize
1370 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001371 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001372psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001373 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001374 psa_algorithm_t alg);
1375
Gilles Peskinef45adda2019-01-14 18:29:18 +01001376/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001377 *
1378 * This function sets up the verification of the MAC
1379 * (message authentication code) of a byte string against an expected value.
1380 *
1381 * The sequence of operations to verify a MAC is as follows:
1382 * -# Allocate an operation object which will be passed to all the functions
1383 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001384 * -# Initialize the operation object with one of the methods described in the
1385 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001386 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001387 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1388 * of the message each time. The MAC that is calculated is the MAC
1389 * of the concatenation of these messages in order.
1390 * -# At the end of the message, call psa_mac_verify_finish() to finish
1391 * calculating the actual MAC of the message and verify it against
1392 * the expected value.
1393 *
1394 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001395 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001396 *
1397 * After a successful call to psa_mac_verify_setup(), the application must
1398 * eventually terminate the operation through one of the following methods:
1399 * - A failed call to psa_mac_update().
1400 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1401 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001402 * \param[in,out] operation The operation object to set up. It must have
1403 * been initialized as per the documentation for
1404 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001405 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001406 * It must remain valid until the operation
1407 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001408 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1409 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001410 *
Gilles Peskine28538492018-07-11 17:34:00 +02001411 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001412 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001413 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001414 * \retval #PSA_ERROR_NOT_PERMITTED
1415 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001416 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001417 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001418 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001419 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1420 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1421 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001422 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001423 * \retval #PSA_ERROR_STORAGE_FAILURE
1424 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001425 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001426 * The operation state is not valid (already set up and not
1427 * subsequently completed).
1428 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001429 * The library has not been previously initialized by psa_crypto_init().
1430 * It is implementation-dependent whether a failure to initialize
1431 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001432 */
1433psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001434 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001435 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001436
Gilles Peskinedcd14942018-07-12 00:30:52 +02001437/** Add a message fragment to a multipart MAC operation.
1438 *
1439 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1440 * before calling this function.
1441 *
1442 * If this function returns an error status, the operation becomes inactive.
1443 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001444 * \param[in,out] operation Active MAC operation.
1445 * \param[in] input Buffer containing the message fragment to add to
1446 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001447 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001448 *
1449 * \retval #PSA_SUCCESS
1450 * Success.
1451 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001452 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001453 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1454 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1455 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001456 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001457 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001458 * \retval #PSA_ERROR_BAD_STATE
1459 * The library has not been previously initialized by psa_crypto_init().
1460 * It is implementation-dependent whether a failure to initialize
1461 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001462 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001463psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1464 const uint8_t *input,
1465 size_t input_length);
1466
Gilles Peskinedcd14942018-07-12 00:30:52 +02001467/** Finish the calculation of the MAC of a message.
1468 *
1469 * The application must call psa_mac_sign_setup() before calling this function.
1470 * This function calculates the MAC of the message formed by concatenating
1471 * the inputs passed to preceding calls to psa_mac_update().
1472 *
1473 * When this function returns, the operation becomes inactive.
1474 *
1475 * \warning Applications should not call this function if they expect
1476 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1477 * Beware that comparing integrity or authenticity data such as
1478 * MAC values with a function such as \c memcmp is risky
1479 * because the time taken by the comparison may leak information
1480 * about the MAC value which could allow an attacker to guess
1481 * a valid MAC and thereby bypass security controls.
1482 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001483 * \param[in,out] operation Active MAC operation.
1484 * \param[out] mac Buffer where the MAC value is to be written.
1485 * \param mac_size Size of the \p mac buffer in bytes.
1486 * \param[out] mac_length On success, the number of bytes
1487 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001488 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001489 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001490 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001491 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001492 *
1493 * \retval #PSA_SUCCESS
1494 * Success.
1495 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001496 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001497 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001498 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001499 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1500 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1501 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1502 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001503 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001504 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001505 * \retval #PSA_ERROR_BAD_STATE
1506 * The library has not been previously initialized by psa_crypto_init().
1507 * It is implementation-dependent whether a failure to initialize
1508 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001509 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001510psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1511 uint8_t *mac,
1512 size_t mac_size,
1513 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001514
Gilles Peskinedcd14942018-07-12 00:30:52 +02001515/** Finish the calculation of the MAC of a message and compare it with
1516 * an expected value.
1517 *
1518 * The application must call psa_mac_verify_setup() before calling this function.
1519 * This function calculates the MAC of the message formed by concatenating
1520 * the inputs passed to preceding calls to psa_mac_update(). It then
1521 * compares the calculated MAC with the expected MAC passed as a
1522 * parameter to this function.
1523 *
1524 * When this function returns, the operation becomes inactive.
1525 *
1526 * \note Implementations shall make the best effort to ensure that the
1527 * comparison between the actual MAC and the expected MAC is performed
1528 * in constant time.
1529 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001530 * \param[in,out] operation Active MAC operation.
1531 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001532 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001533 *
1534 * \retval #PSA_SUCCESS
1535 * The expected MAC is identical to the actual MAC of the message.
1536 * \retval #PSA_ERROR_INVALID_SIGNATURE
1537 * The MAC of the message was calculated successfully, but it
1538 * differs from the expected MAC.
1539 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001540 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001541 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1542 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1543 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001544 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001545 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001546 * \retval #PSA_ERROR_BAD_STATE
1547 * The library has not been previously initialized by psa_crypto_init().
1548 * It is implementation-dependent whether a failure to initialize
1549 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001550 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001551psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1552 const uint8_t *mac,
1553 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001554
Gilles Peskinedcd14942018-07-12 00:30:52 +02001555/** Abort a MAC operation.
1556 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001557 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001558 * \p operation structure itself. Once aborted, the operation object
1559 * can be reused for another operation by calling
1560 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001561 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001562 * You may call this function any time after the operation object has
1563 * been initialized by any of the following methods:
1564 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1565 * it succeeds or not.
1566 * - Initializing the \c struct to all-bits-zero.
1567 * - Initializing the \c struct to logical zeros, e.g.
1568 * `psa_mac_operation_t operation = {0}`.
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
1577 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001578 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001579 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1580 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001581 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw650229b2019-08-07 10:47:47 +01001582 * \retval #PSA_ERROR_STORAGE_FAILURE
1583 * The key could not be retrieved from storage.
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001584 * \retval #PSA_ERROR_BAD_STATE
1585 * The library has not been previously initialized by psa_crypto_init().
1586 * It is implementation-dependent whether a failure to initialize
1587 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001588 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001589psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1590
1591/**@}*/
1592
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001593/** \defgroup cipher Symmetric ciphers
1594 * @{
1595 */
1596
Gilles Peskine69647a42019-01-14 20:18:12 +01001597/** Encrypt a message using a symmetric cipher.
1598 *
1599 * This function encrypts a message with a random IV (initialization
1600 * vector).
1601 *
1602 * \param handle Handle to the key to use for the operation.
1603 * It must remain valid until the operation
1604 * terminates.
1605 * \param alg The cipher algorithm to compute
1606 * (\c PSA_ALG_XXX value such that
1607 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1608 * \param[in] input Buffer containing the message to encrypt.
1609 * \param input_length Size of the \p input buffer in bytes.
1610 * \param[out] output Buffer where the output is to be written.
1611 * The output contains the IV followed by
1612 * the ciphertext proper.
1613 * \param output_size Size of the \p output buffer in bytes.
1614 * \param[out] output_length On success, the number of bytes
1615 * that make up the output.
1616 *
1617 * \retval #PSA_SUCCESS
1618 * Success.
1619 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001620 * \retval #PSA_ERROR_NOT_PERMITTED
1621 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001622 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001623 * \retval #PSA_ERROR_NOT_SUPPORTED
1624 * \p alg is not supported or is not a cipher algorithm.
1625 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1626 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1627 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1628 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001629 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001630 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001631 * \retval #PSA_ERROR_BAD_STATE
1632 * The library has not been previously initialized by psa_crypto_init().
1633 * It is implementation-dependent whether a failure to initialize
1634 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001635 */
1636psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1637 psa_algorithm_t alg,
1638 const uint8_t *input,
1639 size_t input_length,
1640 uint8_t *output,
1641 size_t output_size,
1642 size_t *output_length);
1643
1644/** Decrypt a message using a symmetric cipher.
1645 *
1646 * This function decrypts a message encrypted with a symmetric cipher.
1647 *
1648 * \param handle Handle to the key to use for the operation.
1649 * It must remain valid until the operation
1650 * terminates.
1651 * \param alg The cipher algorithm to compute
1652 * (\c PSA_ALG_XXX value such that
1653 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1654 * \param[in] input Buffer containing the message to decrypt.
1655 * This consists of the IV followed by the
1656 * ciphertext proper.
1657 * \param input_length Size of the \p input buffer in bytes.
1658 * \param[out] output Buffer where the plaintext is to be written.
1659 * \param output_size Size of the \p output buffer in bytes.
1660 * \param[out] output_length On success, the number of bytes
1661 * that make up the output.
1662 *
1663 * \retval #PSA_SUCCESS
1664 * Success.
1665 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001666 * \retval #PSA_ERROR_NOT_PERMITTED
1667 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001668 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001669 * \retval #PSA_ERROR_NOT_SUPPORTED
1670 * \p alg is not supported or is not a cipher algorithm.
1671 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1672 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1673 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1674 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001675 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001676 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001677 * \retval #PSA_ERROR_BAD_STATE
1678 * The library has not been previously initialized by psa_crypto_init().
1679 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001680 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001681 */
1682psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1683 psa_algorithm_t alg,
1684 const uint8_t *input,
1685 size_t input_length,
1686 uint8_t *output,
1687 size_t output_size,
1688 size_t *output_length);
1689
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001690/** The type of the state data structure for multipart cipher operations.
1691 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001692 * Before calling any function on a cipher operation object, the application
1693 * must initialize it by any of the following means:
1694 * - Set the structure to all-bits-zero, for example:
1695 * \code
1696 * psa_cipher_operation_t operation;
1697 * memset(&operation, 0, sizeof(operation));
1698 * \endcode
1699 * - Initialize the structure to logical zero values, for example:
1700 * \code
1701 * psa_cipher_operation_t operation = {0};
1702 * \endcode
1703 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1704 * for example:
1705 * \code
1706 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1707 * \endcode
1708 * - Assign the result of the function psa_cipher_operation_init()
1709 * to the structure, for example:
1710 * \code
1711 * psa_cipher_operation_t operation;
1712 * operation = psa_cipher_operation_init();
1713 * \endcode
1714 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001715 * This is an implementation-defined \c struct. Applications should not
1716 * make any assumptions about the content of this structure except
1717 * as directed by the documentation of a specific implementation. */
1718typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1719
Jaeden Amero5bae2272019-01-04 11:48:27 +00001720/** \def PSA_CIPHER_OPERATION_INIT
1721 *
1722 * This macro returns a suitable initializer for a cipher operation object of
1723 * type #psa_cipher_operation_t.
1724 */
1725#ifdef __DOXYGEN_ONLY__
1726/* This is an example definition for documentation purposes.
1727 * Implementations should define a suitable value in `crypto_struct.h`.
1728 */
1729#define PSA_CIPHER_OPERATION_INIT {0}
1730#endif
1731
1732/** Return an initial value for a cipher operation object.
1733 */
1734static psa_cipher_operation_t psa_cipher_operation_init(void);
1735
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001736/** Set the key for a multipart symmetric encryption operation.
1737 *
1738 * The sequence of operations to encrypt a message with a symmetric cipher
1739 * is as follows:
1740 * -# Allocate an operation object which will be passed to all the functions
1741 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001742 * -# Initialize the operation object with one of the methods described in the
1743 * documentation for #psa_cipher_operation_t, e.g.
1744 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001745 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001746 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001747 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001748 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001749 * requires a specific IV value.
1750 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1751 * of the message each time.
1752 * -# Call psa_cipher_finish().
1753 *
1754 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001755 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001756 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001757 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001758 * eventually terminate the operation. The following events terminate an
1759 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001760 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001761 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001762 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001763 * \param[in,out] operation The operation object to set up. It must have
1764 * been initialized as per the documentation for
1765 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001766 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001767 * It must remain valid until the operation
1768 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001769 * \param alg The cipher algorithm to compute
1770 * (\c PSA_ALG_XXX value such that
1771 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001772 *
Gilles Peskine28538492018-07-11 17:34:00 +02001773 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001774 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001775 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001776 * \retval #PSA_ERROR_NOT_PERMITTED
1777 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001778 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001779 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001780 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001781 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1782 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1783 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001784 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001785 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001786 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001787 * The operation state is not valid (already set up and not
1788 * subsequently completed).
1789 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001790 * The library has not been previously initialized by psa_crypto_init().
1791 * It is implementation-dependent whether a failure to initialize
1792 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001793 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001794psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001795 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001796 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001797
1798/** Set the key for a multipart symmetric decryption operation.
1799 *
1800 * The sequence of operations to decrypt a message with a symmetric cipher
1801 * is as follows:
1802 * -# Allocate an operation object which will be passed to all the functions
1803 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001804 * -# Initialize the operation object with one of the methods described in the
1805 * documentation for #psa_cipher_operation_t, e.g.
1806 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001807 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001808 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001809 * decryption. If the IV is prepended to the ciphertext, you can call
1810 * psa_cipher_update() on a buffer containing the IV followed by the
1811 * beginning of the message.
1812 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1813 * of the message each time.
1814 * -# Call psa_cipher_finish().
1815 *
1816 * The 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:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001822 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001823 * - A call to psa_cipher_finish() or 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
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001849 * The operation state is not valid (already set up and not
1850 * subsequently completed).
1851 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001852 * The library has not been previously initialized by psa_crypto_init().
1853 * It is implementation-dependent whether a failure to initialize
1854 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001855 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001856psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001857 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001858 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001859
Gilles Peskinedcd14942018-07-12 00:30:52 +02001860/** Generate an IV for a symmetric encryption operation.
1861 *
1862 * This function generates a random IV (initialization vector), nonce
1863 * or initial counter value for the encryption operation as appropriate
1864 * for the chosen algorithm, key type and key size.
1865 *
1866 * The application must call psa_cipher_encrypt_setup() before
1867 * calling this function.
1868 *
1869 * If this function returns an error status, the operation becomes inactive.
1870 *
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
Gilles Peskinef45adda2019-01-14 18:29:18 +01001880 * The operation state is not valid (not set up, or IV already 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 *
1906 * If this function returns an error status, the operation becomes inactive.
1907 *
1908 * \note When encrypting, applications should use psa_cipher_generate_iv()
1909 * instead of this function, unless implementing a protocol that requires
1910 * a non-random IV.
1911 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001912 * \param[in,out] operation Active cipher operation.
1913 * \param[in] iv Buffer containing the IV to use.
1914 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001915 *
1916 * \retval #PSA_SUCCESS
1917 * Success.
1918 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001919 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001920 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001921 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001922 * or the chosen algorithm does not use an IV.
1923 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1924 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1925 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001926 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001927 * \retval #PSA_ERROR_BAD_STATE
1928 * The library has not been previously initialized by psa_crypto_init().
1929 * It is implementation-dependent whether a failure to initialize
1930 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001931 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001932psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001933 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001934 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001935
Gilles Peskinedcd14942018-07-12 00:30:52 +02001936/** Encrypt or decrypt a message fragment in an active cipher operation.
1937 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001938 * Before calling this function, you must:
1939 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1940 * The choice of setup function determines whether this function
1941 * encrypts or decrypts its input.
1942 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1943 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001944 *
1945 * If this function returns an error status, the operation becomes inactive.
1946 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001947 * \param[in,out] operation Active cipher operation.
1948 * \param[in] input Buffer containing the message fragment to
1949 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001950 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001951 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001952 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001953 * \param[out] output_length On success, the number of bytes
1954 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001955 *
1956 * \retval #PSA_SUCCESS
1957 * Success.
1958 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001959 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001960 * not set, or already completed).
1961 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1962 * The size of the \p output buffer is too small.
1963 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1964 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1965 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001966 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001967 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001968 * \retval #PSA_ERROR_BAD_STATE
1969 * The library has not been previously initialized by psa_crypto_init().
1970 * It is implementation-dependent whether a failure to initialize
1971 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001972 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001973psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1974 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001975 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001976 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001977 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001978 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001979
Gilles Peskinedcd14942018-07-12 00:30:52 +02001980/** Finish encrypting or decrypting a message in a cipher operation.
1981 *
1982 * The application must call psa_cipher_encrypt_setup() or
1983 * psa_cipher_decrypt_setup() before calling this function. The choice
1984 * of setup function determines whether this function encrypts or
1985 * decrypts its input.
1986 *
1987 * This function finishes the encryption or decryption of the message
1988 * formed by concatenating the inputs passed to preceding calls to
1989 * psa_cipher_update().
1990 *
1991 * When this function returns, the operation becomes inactive.
1992 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001993 * \param[in,out] operation Active cipher operation.
1994 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001995 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001996 * \param[out] output_length On success, the number of bytes
1997 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001998 *
1999 * \retval #PSA_SUCCESS
2000 * Success.
2001 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002002 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02002003 * not set, or already completed).
2004 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2005 * The size of the \p output buffer is too small.
2006 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2007 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2008 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002009 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002010 * \retval #PSA_ERROR_BAD_STATE
2011 * The library has not been previously initialized by psa_crypto_init().
2012 * It is implementation-dependent whether a failure to initialize
2013 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002014 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002015psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002016 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002017 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002018 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002019
Gilles Peskinedcd14942018-07-12 00:30:52 +02002020/** Abort a cipher operation.
2021 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002022 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002023 * \p operation structure itself. Once aborted, the operation object
2024 * can be reused for another operation by calling
2025 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002026 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002027 * You may call this function any time after the operation object has
2028 * been initialized by any of the following methods:
2029 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2030 * whether it succeeds or not.
2031 * - Initializing the \c struct to all-bits-zero.
2032 * - Initializing the \c struct to logical zeros, e.g.
2033 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002034 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002035 * In particular, calling psa_cipher_abort() after the operation has been
2036 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2037 * is safe and has no effect.
2038 *
2039 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002040 *
2041 * \retval #PSA_SUCCESS
2042 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002043 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002044 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2045 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002046 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002047 * \retval #PSA_ERROR_BAD_STATE
2048 * The library has not been previously initialized by psa_crypto_init().
2049 * It is implementation-dependent whether a failure to initialize
2050 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002051 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002052psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2053
2054/**@}*/
2055
Gilles Peskine3b555712018-03-03 21:27:57 +01002056/** \defgroup aead Authenticated encryption with associated data (AEAD)
2057 * @{
2058 */
2059
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002060/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002061 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002062 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002063 * \param alg The AEAD algorithm to compute
2064 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002065 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002066 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002067 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002068 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002069 * but not encrypted.
2070 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002071 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002072 * encrypted.
2073 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002074 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002075 * encrypted data. The additional data is not
2076 * part of this output. For algorithms where the
2077 * encrypted data and the authentication tag
2078 * are defined as separate outputs, the
2079 * authentication tag is appended to the
2080 * encrypted data.
2081 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2082 * This must be at least
2083 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2084 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002085 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002086 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002087 *
Gilles Peskine28538492018-07-11 17:34:00 +02002088 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002089 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002090 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002091 * \retval #PSA_ERROR_NOT_PERMITTED
2092 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002093 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002094 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002095 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002096 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002097 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2098 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002099 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2100 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002101 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002102 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002103 * The library has not been previously initialized by psa_crypto_init().
2104 * It is implementation-dependent whether a failure to initialize
2105 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002106 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002107psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002108 psa_algorithm_t alg,
2109 const uint8_t *nonce,
2110 size_t nonce_length,
2111 const uint8_t *additional_data,
2112 size_t additional_data_length,
2113 const uint8_t *plaintext,
2114 size_t plaintext_length,
2115 uint8_t *ciphertext,
2116 size_t ciphertext_size,
2117 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002118
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002119/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002120 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002121 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002122 * \param alg The AEAD algorithm to compute
2123 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002124 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002125 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002126 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002127 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002128 * but not encrypted.
2129 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002130 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002131 * encrypted. For algorithms where the
2132 * encrypted data and the authentication tag
2133 * are defined as separate inputs, the buffer
2134 * must contain the encrypted data followed
2135 * by the authentication tag.
2136 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002137 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002138 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2139 * This must be at least
2140 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2141 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002142 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002143 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002144 *
Gilles Peskine28538492018-07-11 17:34:00 +02002145 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002146 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002147 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002148 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002149 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002150 * \retval #PSA_ERROR_NOT_PERMITTED
2151 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002152 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002153 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002154 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002155 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002156 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2157 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002158 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2159 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002160 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002161 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002162 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002163 * The library has not been previously initialized by psa_crypto_init().
2164 * It is implementation-dependent whether a failure to initialize
2165 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002166 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002167psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002168 psa_algorithm_t alg,
2169 const uint8_t *nonce,
2170 size_t nonce_length,
2171 const uint8_t *additional_data,
2172 size_t additional_data_length,
2173 const uint8_t *ciphertext,
2174 size_t ciphertext_length,
2175 uint8_t *plaintext,
2176 size_t plaintext_size,
2177 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002178
Gilles Peskine30a9e412019-01-14 18:36:12 +01002179/** The type of the state data structure for multipart AEAD operations.
2180 *
2181 * Before calling any function on an AEAD operation object, the application
2182 * must initialize it by any of the following means:
2183 * - Set the structure to all-bits-zero, for example:
2184 * \code
2185 * psa_aead_operation_t operation;
2186 * memset(&operation, 0, sizeof(operation));
2187 * \endcode
2188 * - Initialize the structure to logical zero values, for example:
2189 * \code
2190 * psa_aead_operation_t operation = {0};
2191 * \endcode
2192 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2193 * for example:
2194 * \code
2195 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2196 * \endcode
2197 * - Assign the result of the function psa_aead_operation_init()
2198 * to the structure, for example:
2199 * \code
2200 * psa_aead_operation_t operation;
2201 * operation = psa_aead_operation_init();
2202 * \endcode
2203 *
2204 * This is an implementation-defined \c struct. Applications should not
2205 * make any assumptions about the content of this structure except
2206 * as directed by the documentation of a specific implementation. */
2207typedef struct psa_aead_operation_s psa_aead_operation_t;
2208
2209/** \def PSA_AEAD_OPERATION_INIT
2210 *
2211 * This macro returns a suitable initializer for an AEAD operation object of
2212 * type #psa_aead_operation_t.
2213 */
2214#ifdef __DOXYGEN_ONLY__
2215/* This is an example definition for documentation purposes.
2216 * Implementations should define a suitable value in `crypto_struct.h`.
2217 */
2218#define PSA_AEAD_OPERATION_INIT {0}
2219#endif
2220
2221/** Return an initial value for an AEAD operation object.
2222 */
2223static psa_aead_operation_t psa_aead_operation_init(void);
2224
2225/** Set the key for a multipart authenticated encryption operation.
2226 *
2227 * The sequence of operations to encrypt a message with authentication
2228 * is as follows:
2229 * -# Allocate an operation object which will be passed to all the functions
2230 * listed here.
2231 * -# Initialize the operation object with one of the methods described in the
2232 * documentation for #psa_aead_operation_t, e.g.
2233 * PSA_AEAD_OPERATION_INIT.
2234 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002235 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2236 * inputs to the subsequent calls to psa_aead_update_ad() and
2237 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2238 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002239 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2240 * generate or set the nonce. You should use
2241 * psa_aead_generate_nonce() unless the protocol you are implementing
2242 * requires a specific nonce value.
2243 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2244 * of the non-encrypted additional authenticated data each time.
2245 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002246 * of the message to encrypt each time.
Adrian L. Shaw67257572019-08-15 10:53:47 +01002247 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002248 *
2249 * The application may call psa_aead_abort() at any time after the operation
2250 * has been initialized.
2251 *
2252 * After a successful call to psa_aead_encrypt_setup(), the application must
2253 * eventually terminate the operation. The following events terminate an
2254 * operation:
2255 * - A failed call to any of the \c psa_aead_xxx functions.
2256 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2257 *
2258 * \param[in,out] operation The operation object to set up. It must have
2259 * been initialized as per the documentation for
2260 * #psa_aead_operation_t and not yet in use.
2261 * \param handle Handle to the key to use for the operation.
2262 * It must remain valid until the operation
2263 * terminates.
2264 * \param alg The AEAD algorithm to compute
2265 * (\c PSA_ALG_XXX value such that
2266 * #PSA_ALG_IS_AEAD(\p alg) is true).
2267 *
2268 * \retval #PSA_SUCCESS
2269 * Success.
2270 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002271 * \retval #PSA_ERROR_NOT_PERMITTED
2272 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002273 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002274 * \retval #PSA_ERROR_NOT_SUPPORTED
2275 * \p alg is not supported or is not an AEAD algorithm.
2276 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2277 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2278 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002279 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002280 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002281 * \retval #PSA_ERROR_BAD_STATE
2282 * The library has not been previously initialized by psa_crypto_init().
2283 * It is implementation-dependent whether a failure to initialize
2284 * results in this error code.
2285 */
2286psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2287 psa_key_handle_t handle,
2288 psa_algorithm_t alg);
2289
2290/** Set the key for a multipart authenticated decryption operation.
2291 *
2292 * The sequence of operations to decrypt a message with authentication
2293 * is as follows:
2294 * -# Allocate an operation object which will be passed to all the functions
2295 * listed here.
2296 * -# Initialize the operation object with one of the methods described in the
2297 * documentation for #psa_aead_operation_t, e.g.
2298 * PSA_AEAD_OPERATION_INIT.
2299 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002300 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2301 * inputs to the subsequent calls to psa_aead_update_ad() and
2302 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2303 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002304 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2305 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2306 * of the non-encrypted additional authenticated data each time.
2307 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002308 * of the ciphertext to decrypt each time.
2309 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002310 *
2311 * The application may call psa_aead_abort() at any time after the operation
2312 * has been initialized.
2313 *
2314 * After a successful call to psa_aead_decrypt_setup(), the application must
2315 * eventually terminate the operation. The following events terminate an
2316 * operation:
2317 * - A failed call to any of the \c psa_aead_xxx functions.
2318 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2319 *
2320 * \param[in,out] operation The operation object to set up. It must have
2321 * been initialized as per the documentation for
2322 * #psa_aead_operation_t and not yet in use.
2323 * \param handle Handle to the key to use for the operation.
2324 * It must remain valid until the operation
2325 * terminates.
2326 * \param alg The AEAD algorithm to compute
2327 * (\c PSA_ALG_XXX value such that
2328 * #PSA_ALG_IS_AEAD(\p alg) is true).
2329 *
2330 * \retval #PSA_SUCCESS
2331 * Success.
2332 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002333 * \retval #PSA_ERROR_NOT_PERMITTED
2334 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002335 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002336 * \retval #PSA_ERROR_NOT_SUPPORTED
2337 * \p alg is not supported or is not an AEAD algorithm.
2338 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2339 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2340 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002341 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002342 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002343 * \retval #PSA_ERROR_BAD_STATE
2344 * The library has not been previously initialized by psa_crypto_init().
2345 * It is implementation-dependent whether a failure to initialize
2346 * results in this error code.
2347 */
2348psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2349 psa_key_handle_t handle,
2350 psa_algorithm_t alg);
2351
2352/** Generate a random nonce for an authenticated encryption operation.
2353 *
2354 * This function generates a random nonce for the authenticated encryption
2355 * operation with an appropriate size for the chosen algorithm, key type
2356 * and key size.
2357 *
2358 * The application must call psa_aead_encrypt_setup() before
2359 * calling this function.
2360 *
2361 * If this function returns an error status, the operation becomes inactive.
2362 *
2363 * \param[in,out] operation Active AEAD operation.
2364 * \param[out] nonce Buffer where the generated nonce is to be
2365 * written.
2366 * \param nonce_size Size of the \p nonce buffer in bytes.
2367 * \param[out] nonce_length On success, the number of bytes of the
2368 * generated nonce.
2369 *
2370 * \retval #PSA_SUCCESS
2371 * Success.
2372 * \retval #PSA_ERROR_BAD_STATE
2373 * The operation state is not valid (not set up, or nonce already set).
2374 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2375 * The size of the \p nonce buffer is too small.
2376 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2377 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2378 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002379 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002380 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002381 * \retval #PSA_ERROR_BAD_STATE
2382 * The library has not been previously initialized by psa_crypto_init().
2383 * It is implementation-dependent whether a failure to initialize
2384 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002385 */
2386psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002387 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002388 size_t nonce_size,
2389 size_t *nonce_length);
2390
2391/** Set the nonce for an authenticated encryption or decryption operation.
2392 *
2393 * This function sets the nonce for the authenticated
2394 * encryption or decryption operation.
2395 *
2396 * The application must call psa_aead_encrypt_setup() before
2397 * calling this function.
2398 *
2399 * If this function returns an error status, the operation becomes inactive.
2400 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002401 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002402 * instead of this function, unless implementing a protocol that requires
2403 * a non-random IV.
2404 *
2405 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002406 * \param[in] nonce Buffer containing the nonce to use.
2407 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002408 *
2409 * \retval #PSA_SUCCESS
2410 * Success.
2411 * \retval #PSA_ERROR_BAD_STATE
2412 * The operation state is not valid (not set up, or nonce already set).
2413 * \retval #PSA_ERROR_INVALID_ARGUMENT
2414 * The size of \p nonce is not acceptable for the chosen algorithm.
2415 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2416 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2417 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002418 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002419 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002420 * \retval #PSA_ERROR_BAD_STATE
2421 * The library has not been previously initialized by psa_crypto_init().
2422 * It is implementation-dependent whether a failure to initialize
2423 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002424 */
2425psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002426 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002427 size_t nonce_length);
2428
Gilles Peskinebc59c852019-01-17 15:26:08 +01002429/** Declare the lengths of the message and additional data for AEAD.
2430 *
2431 * The application must call this function before calling
2432 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2433 * the operation requires it. If the algorithm does not require it,
2434 * calling this function is optional, but if this function is called
2435 * then the implementation must enforce the lengths.
2436 *
2437 * You may call this function before or after setting the nonce with
2438 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2439 *
2440 * - For #PSA_ALG_CCM, calling this function is required.
2441 * - For the other AEAD algorithms defined in this specification, calling
2442 * this function is not required.
2443 * - For vendor-defined algorithm, refer to the vendor documentation.
2444 *
2445 * \param[in,out] operation Active AEAD operation.
2446 * \param ad_length Size of the non-encrypted additional
2447 * authenticated data in bytes.
2448 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2449 *
2450 * \retval #PSA_SUCCESS
2451 * Success.
2452 * \retval #PSA_ERROR_BAD_STATE
2453 * The operation state is not valid (not set up, already completed,
2454 * or psa_aead_update_ad() or psa_aead_update() already called).
2455 * \retval #PSA_ERROR_INVALID_ARGUMENT
2456 * At least one of the lengths is not acceptable for the chosen
2457 * algorithm.
2458 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2459 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2460 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002461 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002462 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002463 * \retval #PSA_ERROR_BAD_STATE
2464 * The library has not been previously initialized by psa_crypto_init().
2465 * It is implementation-dependent whether a failure to initialize
2466 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002467 */
2468psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2469 size_t ad_length,
2470 size_t plaintext_length);
2471
Gilles Peskine30a9e412019-01-14 18:36:12 +01002472/** Pass additional data to an active AEAD operation.
2473 *
2474 * Additional data is authenticated, but not encrypted.
2475 *
2476 * You may call this function multiple times to pass successive fragments
2477 * of the additional data. You may not call this function after passing
2478 * data to encrypt or decrypt with psa_aead_update().
2479 *
2480 * Before calling this function, you must:
2481 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2482 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2483 *
2484 * If this function returns an error status, the operation becomes inactive.
2485 *
2486 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2487 * there is no guarantee that the input is valid. Therefore, until
2488 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2489 * treat the input as untrusted and prepare to undo any action that
2490 * depends on the input if psa_aead_verify() returns an error status.
2491 *
2492 * \param[in,out] operation Active AEAD operation.
2493 * \param[in] input Buffer containing the fragment of
2494 * additional data.
2495 * \param input_length Size of the \p input buffer in bytes.
2496 *
2497 * \retval #PSA_SUCCESS
2498 * Success.
2499 * \retval #PSA_ERROR_BAD_STATE
2500 * The operation state is not valid (not set up, nonce not set,
2501 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002502 * \retval #PSA_ERROR_INVALID_ARGUMENT
2503 * The total input length overflows the additional data length that
2504 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002505 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2506 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2507 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002508 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002509 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002510 * \retval #PSA_ERROR_BAD_STATE
2511 * The library has not been previously initialized by psa_crypto_init().
2512 * It is implementation-dependent whether a failure to initialize
2513 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002514 */
2515psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2516 const uint8_t *input,
2517 size_t input_length);
2518
2519/** Encrypt or decrypt a message fragment in an active AEAD operation.
2520 *
2521 * Before calling this function, you must:
2522 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2523 * The choice of setup function determines whether this function
2524 * encrypts or decrypts its input.
2525 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2526 * 3. Call psa_aead_update_ad() to pass all the additional data.
2527 *
2528 * If this function returns an error status, the operation becomes inactive.
2529 *
2530 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2531 * there is no guarantee that the input is valid. Therefore, until
2532 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2533 * - Do not use the output in any way other than storing it in a
2534 * confidential location. If you take any action that depends
2535 * on the tentative decrypted data, this action will need to be
2536 * undone if the input turns out not to be valid. Furthermore,
2537 * if an adversary can observe that this action took place
2538 * (for example through timing), they may be able to use this
2539 * fact as an oracle to decrypt any message encrypted with the
2540 * same key.
2541 * - In particular, do not copy the output anywhere but to a
2542 * memory or storage space that you have exclusive access to.
2543 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002544 * This function does not require the input to be aligned to any
2545 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002546 * a whole block at a time, it must consume all the input provided, but
2547 * it may delay the end of the corresponding output until a subsequent
2548 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2549 * provides sufficient input. The amount of data that can be delayed
2550 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002551 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002552 * \param[in,out] operation Active AEAD operation.
2553 * \param[in] input Buffer containing the message fragment to
2554 * encrypt or decrypt.
2555 * \param input_length Size of the \p input buffer in bytes.
2556 * \param[out] output Buffer where the output is to be written.
2557 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002558 * This must be at least
2559 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2560 * \p input_length) where \c alg is the
2561 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002562 * \param[out] output_length On success, the number of bytes
2563 * that make up the returned output.
2564 *
2565 * \retval #PSA_SUCCESS
2566 * Success.
2567 * \retval #PSA_ERROR_BAD_STATE
2568 * The operation state is not valid (not set up, nonce not set
2569 * or already completed).
2570 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2571 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002572 * You can determine a sufficient buffer size by calling
2573 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2574 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002575 * \retval #PSA_ERROR_INVALID_ARGUMENT
2576 * The total length of input to psa_aead_update_ad() so far is
2577 * less than the additional data length that was previously
2578 * specified with psa_aead_set_lengths().
2579 * \retval #PSA_ERROR_INVALID_ARGUMENT
2580 * The total input length overflows the plaintext length that
2581 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002582 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2583 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2584 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002585 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002586 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002587 * \retval #PSA_ERROR_BAD_STATE
2588 * The library has not been previously initialized by psa_crypto_init().
2589 * It is implementation-dependent whether a failure to initialize
2590 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002591 */
2592psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2593 const uint8_t *input,
2594 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002595 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002596 size_t output_size,
2597 size_t *output_length);
2598
2599/** Finish encrypting a message in an AEAD operation.
2600 *
2601 * The operation must have been set up with psa_aead_encrypt_setup().
2602 *
2603 * This function finishes the authentication of the additional data
2604 * formed by concatenating the inputs passed to preceding calls to
2605 * psa_aead_update_ad() with the plaintext formed by concatenating the
2606 * inputs passed to preceding calls to psa_aead_update().
2607 *
2608 * This function has two output buffers:
2609 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002610 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002611 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002612 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002613 * that the operation performs.
2614 *
2615 * When this function returns, the operation becomes inactive.
2616 *
2617 * \param[in,out] operation Active AEAD operation.
2618 * \param[out] ciphertext Buffer where the last part of the ciphertext
2619 * is to be written.
2620 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002621 * This must be at least
2622 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2623 * \c alg is the algorithm that is being
2624 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002625 * \param[out] ciphertext_length On success, the number of bytes of
2626 * returned ciphertext.
2627 * \param[out] tag Buffer where the authentication tag is
2628 * to be written.
2629 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002630 * This must be at least
2631 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2632 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002633 * \param[out] tag_length On success, the number of bytes
2634 * that make up the returned tag.
2635 *
2636 * \retval #PSA_SUCCESS
2637 * Success.
2638 * \retval #PSA_ERROR_BAD_STATE
2639 * The operation state is not valid (not set up, nonce not set,
2640 * decryption, or already completed).
2641 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002642 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002643 * You can determine a sufficient buffer size for \p ciphertext by
2644 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2645 * where \c alg is the algorithm that is being calculated.
2646 * You can determine a sufficient buffer size for \p tag by
2647 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002648 * \retval #PSA_ERROR_INVALID_ARGUMENT
2649 * The total length of input to psa_aead_update_ad() so far is
2650 * less than the additional data length that was previously
2651 * specified with psa_aead_set_lengths().
2652 * \retval #PSA_ERROR_INVALID_ARGUMENT
2653 * The total length of input to psa_aead_update() so far is
2654 * less than the plaintext length that was previously
2655 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002656 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2657 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2658 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002659 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002660 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002661 * \retval #PSA_ERROR_BAD_STATE
2662 * The library has not been previously initialized by psa_crypto_init().
2663 * It is implementation-dependent whether a failure to initialize
2664 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002665 */
2666psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002667 uint8_t *ciphertext,
2668 size_t ciphertext_size,
2669 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002670 uint8_t *tag,
2671 size_t tag_size,
2672 size_t *tag_length);
2673
2674/** Finish authenticating and decrypting a message in an AEAD operation.
2675 *
2676 * The operation must have been set up with psa_aead_decrypt_setup().
2677 *
2678 * This function finishes the authentication of the additional data
2679 * formed by concatenating the inputs passed to preceding calls to
2680 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2681 * inputs passed to preceding calls to psa_aead_update().
2682 *
2683 * When this function returns, the operation becomes inactive.
2684 *
2685 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002686 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002687 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002688 * from previous calls to psa_aead_update()
2689 * that could not be processed until the end
2690 * of the input.
2691 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002692 * This must be at least
2693 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2694 * \c alg is the algorithm that is being
2695 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002696 * \param[out] plaintext_length On success, the number of bytes of
2697 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002698 * \param[in] tag Buffer containing the authentication tag.
2699 * \param tag_length Size of the \p tag buffer in bytes.
2700 *
2701 * \retval #PSA_SUCCESS
2702 * Success.
2703 * \retval #PSA_ERROR_BAD_STATE
2704 * The operation state is not valid (not set up, nonce not set,
2705 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002706 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2707 * The size of the \p plaintext buffer is too small.
2708 * You can determine a sufficient buffer size for \p plaintext by
2709 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2710 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002711 * \retval #PSA_ERROR_INVALID_ARGUMENT
2712 * The total length of input to psa_aead_update_ad() so far is
2713 * less than the additional data length that was previously
2714 * specified with psa_aead_set_lengths().
2715 * \retval #PSA_ERROR_INVALID_ARGUMENT
2716 * The total length of input to psa_aead_update() so far is
2717 * less than the plaintext length that was previously
2718 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002719 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2720 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2721 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002722 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002723 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002724 * \retval #PSA_ERROR_BAD_STATE
2725 * The library has not been previously initialized by psa_crypto_init().
2726 * It is implementation-dependent whether a failure to initialize
2727 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002728 */
2729psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002730 uint8_t *plaintext,
2731 size_t plaintext_size,
2732 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002733 const uint8_t *tag,
2734 size_t tag_length);
2735
2736/** Abort an AEAD operation.
2737 *
2738 * Aborting an operation frees all associated resources except for the
2739 * \p operation structure itself. Once aborted, the operation object
2740 * can be reused for another operation by calling
2741 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2742 *
2743 * You may call this function any time after the operation object has
2744 * been initialized by any of the following methods:
2745 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2746 * whether it succeeds or not.
2747 * - Initializing the \c struct to all-bits-zero.
2748 * - Initializing the \c struct to logical zeros, e.g.
2749 * `psa_aead_operation_t operation = {0}`.
2750 *
2751 * In particular, calling psa_aead_abort() after the operation has been
2752 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2753 * is safe and has no effect.
2754 *
2755 * \param[in,out] operation Initialized AEAD operation.
2756 *
2757 * \retval #PSA_SUCCESS
2758 * \retval #PSA_ERROR_BAD_STATE
2759 * \p operation is not an active AEAD operation.
2760 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2761 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002762 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002763 * \retval #PSA_ERROR_BAD_STATE
2764 * The library has not been previously initialized by psa_crypto_init().
2765 * It is implementation-dependent whether a failure to initialize
2766 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002767 */
2768psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2769
Gilles Peskine3b555712018-03-03 21:27:57 +01002770/**@}*/
2771
Gilles Peskine20035e32018-02-03 22:44:14 +01002772/** \defgroup asymmetric Asymmetric cryptography
2773 * @{
2774 */
2775
2776/**
2777 * \brief Sign a hash or short message with a private key.
2778 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002779 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002780 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002781 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2782 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2783 * to determine the hash algorithm to use.
2784 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002785 * \param handle Handle to the key to use for the operation.
2786 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002787 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002788 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002789 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002790 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002791 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002792 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002793 * \param[out] signature_length On success, the number of bytes
2794 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002795 *
Gilles Peskine28538492018-07-11 17:34:00 +02002796 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002797 * \retval #PSA_ERROR_INVALID_HANDLE
2798 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002799 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002800 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002801 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002802 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002803 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002804 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002805 * \retval #PSA_ERROR_NOT_SUPPORTED
2806 * \retval #PSA_ERROR_INVALID_ARGUMENT
2807 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2808 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2809 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002810 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002811 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002812 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002813 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002814 * The library has not been previously initialized by psa_crypto_init().
2815 * It is implementation-dependent whether a failure to initialize
2816 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002817 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002818psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002819 psa_algorithm_t alg,
2820 const uint8_t *hash,
2821 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002822 uint8_t *signature,
2823 size_t signature_size,
2824 size_t *signature_length);
2825
2826/**
2827 * \brief Verify the signature a hash or short message using a public key.
2828 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002829 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002830 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002831 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2832 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2833 * to determine the hash algorithm to use.
2834 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002835 * \param handle Handle to the key to use for the operation.
2836 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002837 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002838 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002839 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002840 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002841 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002842 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002843 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002844 *
Gilles Peskine28538492018-07-11 17:34:00 +02002845 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002846 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002847 * \retval #PSA_ERROR_INVALID_HANDLE
2848 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002849 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002850 * The calculation was perfomed successfully, but the passed
2851 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002852 * \retval #PSA_ERROR_NOT_SUPPORTED
2853 * \retval #PSA_ERROR_INVALID_ARGUMENT
2854 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2855 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2856 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002857 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002858 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002859 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002860 * The library has not been previously initialized by psa_crypto_init().
2861 * It is implementation-dependent whether a failure to initialize
2862 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002863 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002864psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002865 psa_algorithm_t alg,
2866 const uint8_t *hash,
2867 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002868 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002869 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002870
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002871/**
2872 * \brief Encrypt a short message with a public key.
2873 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002874 * \param handle Handle to the key to use for the operation.
2875 * It must be a public key or an asymmetric
2876 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002877 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002878 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002879 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002880 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002881 * \param[in] salt A salt or label, if supported by the
2882 * encryption algorithm.
2883 * If the algorithm does not support a
2884 * salt, pass \c NULL.
2885 * If the algorithm supports an optional
2886 * salt and you do not want to pass a salt,
2887 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002888 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002889 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2890 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002891 * \param salt_length Size of the \p salt buffer in bytes.
2892 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002893 * \param[out] output Buffer where the encrypted message is to
2894 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002895 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002896 * \param[out] output_length On success, the number of bytes
2897 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002898 *
Gilles Peskine28538492018-07-11 17:34:00 +02002899 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002900 * \retval #PSA_ERROR_INVALID_HANDLE
2901 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002902 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002903 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002904 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002905 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002906 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002907 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002908 * \retval #PSA_ERROR_NOT_SUPPORTED
2909 * \retval #PSA_ERROR_INVALID_ARGUMENT
2910 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2911 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2912 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002913 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002914 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002915 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002916 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002917 * The library has not been previously initialized by psa_crypto_init().
2918 * It is implementation-dependent whether a failure to initialize
2919 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002920 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002921psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002922 psa_algorithm_t alg,
2923 const uint8_t *input,
2924 size_t input_length,
2925 const uint8_t *salt,
2926 size_t salt_length,
2927 uint8_t *output,
2928 size_t output_size,
2929 size_t *output_length);
2930
2931/**
2932 * \brief Decrypt a short message with a private key.
2933 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002934 * \param handle Handle to the key to use for the operation.
2935 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002936 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002937 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002938 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002939 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002940 * \param[in] salt A salt or label, if supported by the
2941 * encryption algorithm.
2942 * If the algorithm does not support a
2943 * salt, pass \c NULL.
2944 * If the algorithm supports an optional
2945 * salt and you do not want to pass a salt,
2946 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002947 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002948 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2949 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002950 * \param salt_length Size of the \p salt buffer in bytes.
2951 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002952 * \param[out] output Buffer where the decrypted message is to
2953 * be written.
2954 * \param output_size Size of the \c output buffer in bytes.
2955 * \param[out] output_length On success, the number of bytes
2956 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002957 *
Gilles Peskine28538492018-07-11 17:34:00 +02002958 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002959 * \retval #PSA_ERROR_INVALID_HANDLE
2960 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002961 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002962 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002963 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002964 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002965 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002966 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002967 * \retval #PSA_ERROR_NOT_SUPPORTED
2968 * \retval #PSA_ERROR_INVALID_ARGUMENT
2969 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2970 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2971 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002972 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002973 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002974 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2975 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002976 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002977 * The library has not been previously initialized by psa_crypto_init().
2978 * It is implementation-dependent whether a failure to initialize
2979 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002980 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002981psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002982 psa_algorithm_t alg,
2983 const uint8_t *input,
2984 size_t input_length,
2985 const uint8_t *salt,
2986 size_t salt_length,
2987 uint8_t *output,
2988 size_t output_size,
2989 size_t *output_length);
2990
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002991/**@}*/
2992
Gilles Peskine35675b62019-05-16 17:26:11 +02002993/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002994 * @{
2995 */
2996
Gilles Peskine35675b62019-05-16 17:26:11 +02002997/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002998 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002999 * Before calling any function on a key derivation operation object, the
3000 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003001 * - Set the structure to all-bits-zero, for example:
3002 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003003 * psa_key_derivation_operation_t operation;
3004 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003005 * \endcode
3006 * - Initialize the structure to logical zero values, for example:
3007 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003008 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003009 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003010 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003011 * for example:
3012 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003013 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003014 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003015 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003016 * to the structure, for example:
3017 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003018 * psa_key_derivation_operation_t operation;
3019 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003020 * \endcode
3021 *
3022 * This is an implementation-defined \c struct. Applications should not
3023 * make any assumptions about the content of this structure except
3024 * as directed by the documentation of a specific implementation.
3025 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003026typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003027
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003028/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003029 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003030 * This macro returns a suitable initializer for a key derivation operation
3031 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003032 */
3033#ifdef __DOXYGEN_ONLY__
3034/* This is an example definition for documentation purposes.
3035 * Implementations should define a suitable value in `crypto_struct.h`.
3036 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003037#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003038#endif
3039
Gilles Peskine35675b62019-05-16 17:26:11 +02003040/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003041 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003042static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003043
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003044/** Set up a key derivation operation.
3045 *
3046 * A key derivation algorithm takes some inputs and uses them to generate
3047 * a byte stream in a deterministic way.
3048 * This byte stream can be used to produce keys and other
3049 * cryptographic material.
3050 *
3051 * To derive a key:
3052 * - Start with an initialized object of type #psa_key_derivation_operation_t.
3053 * - Call psa_key_derivation_setup() to select the algorithm.
3054 * - Provide the inputs for the key derivation by calling
3055 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3056 * as appropriate. Which inputs are needed, in what order, and whether
3057 * they may be keys and if so of what type depends on the algorithm.
3058 * - Optionally set the operation's maximum capacity with
3059 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3060 * of or after providing inputs. For some algorithms, this step is mandatory
3061 * because the output depends on the maximum capacity.
3062 * - To derive a key, call psa_key_derivation_output_key().
3063 * To derive a byte string for a different purpose, call
3064 * - psa_key_derivation_output_bytes().
3065 * Successive calls to these functions use successive output bytes
3066 * calculated by the key derivation algorithm.
3067 * - Clean up the key derivation operation object with
3068 * psa_key_derivation_abort().
3069 *
3070 * \param[in,out] operation The key derivation operation object
3071 * to set up. It must
3072 * have been initialized but not set up yet.
3073 * \param alg The key derivation algorithm to compute
3074 * (\c PSA_ALG_XXX value such that
3075 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3076 *
3077 * \retval #PSA_SUCCESS
3078 * Success.
3079 * \retval #PSA_ERROR_INVALID_ARGUMENT
3080 * \c alg is not a key derivation algorithm.
3081 * \retval #PSA_ERROR_NOT_SUPPORTED
3082 * \c alg is not supported or is not a key derivation algorithm.
3083 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3084 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3085 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003086 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003087 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01003088 * The operation state is either not initialized or has already been setup.
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003089 * \retval #PSA_ERROR_BAD_STATE
3090 * The library has not been previously initialized by psa_crypto_init().
3091 * It is implementation-dependent whether a failure to initialize
3092 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003093 */
3094psa_status_t psa_key_derivation_setup(
3095 psa_key_derivation_operation_t *operation,
3096 psa_algorithm_t alg);
3097
Gilles Peskine35675b62019-05-16 17:26:11 +02003098/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003099 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003100 * The capacity of a key derivation is the maximum number of bytes that it can
3101 * return. When you get *N* bytes of output from a key derivation operation,
3102 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003103 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003104 * \param[in] operation The operation to query.
3105 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003106 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003107 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003108 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003109 * \retval #PSA_ERROR_BAD_STATE
3110 * The operation state is not valid.
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003111 * \retval #PSA_ERROR_HARDWARE_FAILURE
3112 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003113 * \retval #PSA_ERROR_BAD_STATE
3114 * The library has not been previously initialized by psa_crypto_init().
3115 * It is implementation-dependent whether a failure to initialize
3116 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003117 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003118psa_status_t psa_key_derivation_get_capacity(
3119 const psa_key_derivation_operation_t *operation,
3120 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003121
Gilles Peskine35675b62019-05-16 17:26:11 +02003122/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003123 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003124 * The capacity of a key derivation operation is the maximum number of bytes
3125 * that the key derivation operation can return from this point onwards.
3126 *
3127 * \param[in,out] operation The key derivation operation object to modify.
3128 * \param capacity The new capacity of the operation.
3129 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003130 * current capacity.
3131 *
3132 * \retval #PSA_SUCCESS
3133 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003134 * \p capacity is larger than the operation's current capacity.
3135 * In this case, the operation object remains valid and its capacity
3136 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003137 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003138 * The operation state is not valid.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003139 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003140 * \retval #PSA_ERROR_HARDWARE_FAILURE
3141 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003142 * \retval #PSA_ERROR_BAD_STATE
3143 * The library has not been previously initialized by psa_crypto_init().
3144 * It is implementation-dependent whether a failure to initialize
3145 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003146 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003147psa_status_t psa_key_derivation_set_capacity(
3148 psa_key_derivation_operation_t *operation,
3149 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003150
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003151/** Use the maximum possible capacity for a key derivation operation.
3152 *
3153 * Use this value as the capacity argument when setting up a key derivation
3154 * to indicate that the operation should have the maximum possible capacity.
3155 * The value of the maximum possible capacity depends on the key derivation
3156 * algorithm.
3157 */
3158#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3159
3160/** Provide an input for key derivation or key agreement.
3161 *
3162 * Which inputs are required and in what order depends on the algorithm.
3163 * Refer to the documentation of each key derivation or key agreement
3164 * algorithm for information.
3165 *
3166 * This function passes direct inputs. Some inputs must be passed as keys
3167 * using psa_key_derivation_input_key() instead of this function. Refer to
3168 * the documentation of individual step types for information.
3169 *
3170 * \param[in,out] operation The key derivation operation object to use.
3171 * It must have been set up with
3172 * psa_key_derivation_setup() and must not
3173 * have produced any output yet.
3174 * \param step Which step the input data is for.
3175 * \param[in] data Input data to use.
3176 * \param data_length Size of the \p data buffer in bytes.
3177 *
3178 * \retval #PSA_SUCCESS
3179 * Success.
3180 * \retval #PSA_ERROR_INVALID_ARGUMENT
3181 * \c step is not compatible with the operation's algorithm.
3182 * \retval #PSA_ERROR_INVALID_ARGUMENT
3183 * \c step does not allow direct inputs.
3184 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3185 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3186 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003187 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003188 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003189 * \retval #PSA_ERROR_BAD_STATE
3190 * The value of \p step is not valid given the state of \p operation.
3191 * \retval #PSA_ERROR_BAD_STATE
3192 * The library has not been previously initialized by psa_crypto_init().
3193 * It is implementation-dependent whether a failure to initialize
3194 * results in this error code.
3195 */
3196psa_status_t psa_key_derivation_input_bytes(
3197 psa_key_derivation_operation_t *operation,
3198 psa_key_derivation_step_t step,
3199 const uint8_t *data,
3200 size_t data_length);
3201
3202/** Provide an input for key derivation in the form of a key.
3203 *
3204 * Which inputs are required and in what order depends on the algorithm.
3205 * Refer to the documentation of each key derivation or key agreement
3206 * algorithm for information.
3207 *
3208 * This function passes key inputs. Some inputs must be passed as keys
3209 * of the appropriate type using this function, while others must be
3210 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3211 * the documentation of individual step types for information.
3212 *
3213 * \param[in,out] operation The key derivation operation object to use.
3214 * It must have been set up with
3215 * psa_key_derivation_setup() and must not
3216 * have produced any output yet.
3217 * \param step Which step the input data is for.
3218 * \param handle Handle to the key. It must have an
3219 * appropriate type for \p step and must
3220 * allow the usage #PSA_KEY_USAGE_DERIVE.
3221 *
3222 * \retval #PSA_SUCCESS
3223 * Success.
3224 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003225 * \retval #PSA_ERROR_NOT_PERMITTED
3226 * \retval #PSA_ERROR_INVALID_ARGUMENT
3227 * \c step is not compatible with the operation's algorithm.
3228 * \retval #PSA_ERROR_INVALID_ARGUMENT
3229 * \c step does not allow key inputs.
3230 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3231 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3232 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003233 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003234 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003235 * \retval #PSA_ERROR_BAD_STATE
3236 * The value of \p step is not valid given the state of \p operation.
3237 * \retval #PSA_ERROR_BAD_STATE
3238 * The library has not been previously initialized by psa_crypto_init().
3239 * It is implementation-dependent whether a failure to initialize
3240 * results in this error code.
3241 */
3242psa_status_t psa_key_derivation_input_key(
3243 psa_key_derivation_operation_t *operation,
3244 psa_key_derivation_step_t step,
3245 psa_key_handle_t handle);
3246
3247/** Perform a key agreement and use the shared secret as input to a key
3248 * derivation.
3249 *
3250 * A key agreement algorithm takes two inputs: a private key \p private_key
3251 * a public key \p peer_key.
3252 * The result of this function is passed as input to a key derivation.
3253 * The output of this key derivation can be extracted by reading from the
3254 * resulting operation to produce keys and other cryptographic material.
3255 *
3256 * \param[in,out] operation The key derivation operation object to use.
3257 * It must have been set up with
3258 * psa_key_derivation_setup() with a
3259 * key agreement and derivation algorithm
3260 * \c alg (\c PSA_ALG_XXX value such that
3261 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3262 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3263 * is false).
3264 * The operation must be ready for an
3265 * input of the type given by \p step.
3266 * \param step Which step the input data is for.
3267 * \param private_key Handle to the private key to use.
3268 * \param[in] peer_key Public key of the peer. The peer key must be in the
3269 * same format that psa_import_key() accepts for the
3270 * public key type corresponding to the type of
3271 * private_key. That is, this function performs the
3272 * equivalent of
3273 * #psa_import_key(...,
3274 * `peer_key`, `peer_key_length`) where
3275 * with key attributes indicating the public key
3276 * type corresponding to the type of `private_key`.
3277 * For example, for EC keys, this means that peer_key
3278 * is interpreted as a point on the curve that the
3279 * private key is on. The standard formats for public
3280 * keys are documented in the documentation of
3281 * psa_export_public_key().
3282 * \param peer_key_length Size of \p peer_key in bytes.
3283 *
3284 * \retval #PSA_SUCCESS
3285 * Success.
3286 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003287 * \retval #PSA_ERROR_NOT_PERMITTED
3288 * \retval #PSA_ERROR_INVALID_ARGUMENT
3289 * \c private_key is not compatible with \c alg,
3290 * or \p peer_key is not valid for \c alg or not compatible with
3291 * \c private_key.
3292 * \retval #PSA_ERROR_NOT_SUPPORTED
3293 * \c alg is not supported or is not a key derivation algorithm.
3294 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3295 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3296 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003297 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003298 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003299 * \retval #PSA_ERROR_BAD_STATE
3300 * The library has not been previously initialized by psa_crypto_init().
3301 * It is implementation-dependent whether a failure to initialize
3302 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003303 */
3304psa_status_t psa_key_derivation_key_agreement(
3305 psa_key_derivation_operation_t *operation,
3306 psa_key_derivation_step_t step,
3307 psa_key_handle_t private_key,
3308 const uint8_t *peer_key,
3309 size_t peer_key_length);
3310
Gilles Peskine35675b62019-05-16 17:26:11 +02003311/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003312 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003313 * This function calculates output bytes from a key derivation algorithm and
3314 * return those bytes.
3315 * If you view the key derivation's output as a stream of bytes, this
3316 * function destructively reads the requested number of bytes from the
3317 * stream.
3318 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003319 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003320 * \param[in,out] operation The key derivation operation object to read from.
3321 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003322 * \param output_length Number of bytes to output.
3323 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003324 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003325 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003326 * The operation's capacity was less than
3327 * \p output_length bytes. Note that in this case,
3328 * no output is written to the output buffer.
3329 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003330 * subsequent calls to this function will not
3331 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003332 * \retval #PSA_ERROR_BAD_STATE
3333 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3334 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3335 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003336 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003337 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003338 * \retval #PSA_ERROR_BAD_STATE
3339 * The library has not been previously initialized by psa_crypto_init().
3340 * It is implementation-dependent whether a failure to initialize
3341 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003342 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003343psa_status_t psa_key_derivation_output_bytes(
3344 psa_key_derivation_operation_t *operation,
3345 uint8_t *output,
3346 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003347
Gilles Peskine35675b62019-05-16 17:26:11 +02003348/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003349 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003350 * This function calculates output bytes from a key derivation algorithm
3351 * and uses those bytes to generate a key deterministically.
3352 * If you view the key derivation's output as a stream of bytes, this
3353 * function destructively reads as many bytes as required from the
3354 * stream.
3355 * The operation's capacity decreases by the number of bytes read.
3356 *
3357 * How much output is produced and consumed from the operation, and how
3358 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003359 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003360 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003361 * of a given size, this function is functionally equivalent to
3362 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003363 * and passing the resulting output to #psa_import_key.
3364 * However, this function has a security benefit:
3365 * if the implementation provides an isolation boundary then
3366 * the key material is not exposed outside the isolation boundary.
3367 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003368 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003369 * The following key types defined in this specification follow this scheme:
3370 *
3371 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003372 * - #PSA_KEY_TYPE_ARC4;
3373 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003374 * - #PSA_KEY_TYPE_DERIVE;
3375 * - #PSA_KEY_TYPE_HMAC.
3376 *
3377 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003378 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003379 * Montgomery curve), this function always draws a byte string whose
3380 * length is determined by the curve, and sets the mandatory bits
3381 * accordingly. That is:
3382 *
3383 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3384 * and process it as specified in RFC 7748 &sect;5.
3385 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3386 * and process it as specified in RFC 7748 &sect;5.
3387 *
3388 * - For key types for which the key is represented by a single sequence of
3389 * \p bits bits with constraints as to which bit sequences are acceptable,
3390 * this function draws a byte string of length (\p bits / 8) bytes rounded
3391 * up to the nearest whole number of bytes. If the resulting byte string
3392 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3393 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003394 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003395 * for the output produced by psa_export_key().
3396 * The following key types defined in this specification follow this scheme:
3397 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003398 * - #PSA_KEY_TYPE_DES.
3399 * Force-set the parity bits, but discard forbidden weak keys.
3400 * For 2-key and 3-key triple-DES, the three keys are generated
3401 * successively (for example, for 3-key triple-DES,
3402 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3403 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003404 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003405 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003406 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003407 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003408 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003409 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003410 * Weierstrass curve).
3411 * For these key types, interpret the byte string as integer
3412 * in big-endian order. Discard it if it is not in the range
3413 * [0, *N* - 2] where *N* is the boundary of the private key domain
3414 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003415 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003416 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003417 * This method allows compliance to NIST standards, specifically
3418 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003419 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3420 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3421 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3422 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003423 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003424 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003425 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003426 * implementation-defined.
3427 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003428 * In all cases, the data that is read is discarded from the operation.
3429 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003430 *
Gilles Peskine20628592019-04-19 19:29:50 +02003431 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003432 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003433 * \param[out] handle On success, a handle to the newly created key.
3434 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003435 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003436 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003437 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003438 * If the key is persistent, the key material and the key's metadata
3439 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003440 * \retval #PSA_ERROR_ALREADY_EXISTS
3441 * This is an attempt to create a persistent key, and there is
3442 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003443 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003444 * There was not enough data to create the desired key.
3445 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003446 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003447 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003448 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003449 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003450 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003451 * \retval #PSA_ERROR_INVALID_ARGUMENT
3452 * The provided key attributes are not valid for the operation.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003453 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003454 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3455 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3456 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3457 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003458 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003459 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003460 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003461 * The library has not been previously initialized by psa_crypto_init().
3462 * It is implementation-dependent whether a failure to initialize
3463 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003464 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003465psa_status_t psa_key_derivation_output_key(
3466 const psa_key_attributes_t *attributes,
3467 psa_key_derivation_operation_t *operation,
3468 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003469
Gilles Peskine35675b62019-05-16 17:26:11 +02003470/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003471 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003472 * Once a key derivation operation has been aborted, its capacity is zero.
3473 * Aborting an operation frees all associated resources except for the
3474 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003475 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003476 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003477 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003478 * psa_key_derivation_operation_init() or a zero value. In particular,
3479 * it is valid to call psa_key_derivation_abort() twice, or to call
3480 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003481 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003482 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003483 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003484 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003485 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003486 * \retval #PSA_SUCCESS
3487 * \retval #PSA_ERROR_BAD_STATE
3488 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3489 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003490 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003491 * \retval #PSA_ERROR_BAD_STATE
3492 * The library has not been previously initialized by psa_crypto_init().
3493 * It is implementation-dependent whether a failure to initialize
3494 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003495 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003496psa_status_t psa_key_derivation_abort(
3497 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003498
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003499/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003500 *
3501 * \warning The raw result of a key agreement algorithm such as finite-field
3502 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3503 * not be used directly as key material. It should instead be passed as
3504 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003505 * a key derivation, use psa_key_derivation_key_agreement() and other
3506 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003507 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003508 * \param alg The key agreement algorithm to compute
3509 * (\c PSA_ALG_XXX value such that
3510 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3511 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003512 * \param private_key Handle to the private key to use.
3513 * \param[in] peer_key Public key of the peer. It must be
3514 * in the same format that psa_import_key()
3515 * accepts. The standard formats for public
3516 * keys are documented in the documentation
3517 * of psa_export_public_key().
3518 * \param peer_key_length Size of \p peer_key in bytes.
3519 * \param[out] output Buffer where the decrypted message is to
3520 * be written.
3521 * \param output_size Size of the \c output buffer in bytes.
3522 * \param[out] output_length On success, the number of bytes
3523 * that make up the returned output.
3524 *
3525 * \retval #PSA_SUCCESS
3526 * Success.
3527 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003528 * \retval #PSA_ERROR_NOT_PERMITTED
3529 * \retval #PSA_ERROR_INVALID_ARGUMENT
3530 * \p alg is not a key agreement algorithm
3531 * \retval #PSA_ERROR_INVALID_ARGUMENT
3532 * \p private_key is not compatible with \p alg,
3533 * or \p peer_key is not valid for \p alg or not compatible with
3534 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003535 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3536 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003537 * \retval #PSA_ERROR_NOT_SUPPORTED
3538 * \p alg is not a supported key agreement algorithm.
3539 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3540 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3541 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003542 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003543 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003544 * \retval #PSA_ERROR_BAD_STATE
3545 * The library has not been previously initialized by psa_crypto_init().
3546 * It is implementation-dependent whether a failure to initialize
3547 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003548 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003549psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3550 psa_key_handle_t private_key,
3551 const uint8_t *peer_key,
3552 size_t peer_key_length,
3553 uint8_t *output,
3554 size_t output_size,
3555 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003556
Gilles Peskineea0fb492018-07-12 17:17:20 +02003557/**@}*/
3558
Gilles Peskineedd76872018-07-20 17:42:05 +02003559/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003560 * @{
3561 */
3562
3563/**
3564 * \brief Generate random bytes.
3565 *
3566 * \warning This function **can** fail! Callers MUST check the return status
3567 * and MUST NOT use the content of the output buffer if the return
3568 * status is not #PSA_SUCCESS.
3569 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003570 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003571 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003572 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003573 * \param output_size Number of bytes to generate and output.
3574 *
Gilles Peskine28538492018-07-11 17:34:00 +02003575 * \retval #PSA_SUCCESS
3576 * \retval #PSA_ERROR_NOT_SUPPORTED
3577 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003578 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003579 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3580 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003581 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw23649242019-08-08 10:58:08 +01003582 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3583 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir0adf0fc2018-09-06 16:24:41 +03003584 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003585 * The library has not been previously initialized by psa_crypto_init().
3586 * It is implementation-dependent whether a failure to initialize
3587 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003588 */
3589psa_status_t psa_generate_random(uint8_t *output,
3590 size_t output_size);
3591
3592/**
3593 * \brief Generate a key or key pair.
3594 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003595 * The key is generated randomly.
3596 * Its location, policy, type and size are taken from \p attributes.
3597 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003598 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003599 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003600 * the public exponent is 65537.
3601 * The modulus is a product of two probabilistic primes
3602 * between 2^{n-1} and 2^n where n is the bit size specified in the
3603 * attributes.
3604 *
Gilles Peskine20628592019-04-19 19:29:50 +02003605 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003606 * \param[out] handle On success, a handle to the newly created key.
3607 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003608 *
Gilles Peskine28538492018-07-11 17:34:00 +02003609 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003610 * Success.
3611 * If the key is persistent, the key material and the key's metadata
3612 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003613 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003614 * This is an attempt to create a persistent key, and there is
3615 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003616 * \retval #PSA_ERROR_NOT_SUPPORTED
3617 * \retval #PSA_ERROR_INVALID_ARGUMENT
3618 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3619 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3620 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3621 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003622 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003623 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3624 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003625 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003626 * The library has not been previously initialized by psa_crypto_init().
3627 * It is implementation-dependent whether a failure to initialize
3628 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003629 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003630psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003631 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003632
3633/**@}*/
3634
Gilles Peskinee59236f2018-01-27 23:32:46 +01003635#ifdef __cplusplus
3636}
3637#endif
3638
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003639/* The file "crypto_sizes.h" contains definitions for size calculation
3640 * macros whose definitions are implementation-specific. */
3641#include "crypto_sizes.h"
3642
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003643/* The file "crypto_struct.h" contains definitions for
3644 * implementation-specific structs that are declared above. */
3645#include "crypto_struct.h"
3646
3647/* The file "crypto_extra.h" contains vendor-specific definitions. This
3648 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003649#include "crypto_extra.h"
3650
3651#endif /* PSA_CRYPTO_H */