blob: c5f2971e3256495002d25200692598f7aae85018 [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
itayzafrir18617092018-09-16 12:22:41 +0300506 * The library has not been previously initialized by psa_crypto_init().
507 * It is implementation-dependent whether a failure to initialize
508 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100509 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200510psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100511 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200512 size_t data_length,
513 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100514
515/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100516 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200517 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100518 * This function destroys a key from both volatile
Gilles Peskine154bd952018-04-19 08:38:16 +0200519 * memory and, if applicable, non-volatile storage. Implementations shall
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100520 * make a best effort to ensure that that the key material cannot be recovered.
Gilles Peskine154bd952018-04-19 08:38:16 +0200521 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100522 * This function also erases any metadata such as policies and frees all
523 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200524 *
Andrew Thoelke07f16b72019-08-21 22:48:47 +0100525 * Destroying a key will invalidate all existing handles to the key.
526 *
527 * If the key is currently in use in a multipart operation, then destroying the
528 * key will abort the multipart operation.
529 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100530 * \param handle Handle to the key to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100531 *
Gilles Peskine28538492018-07-11 17:34:00 +0200532 * \retval #PSA_SUCCESS
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100533 * The key material has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200534 * \retval #PSA_ERROR_NOT_PERMITTED
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100535 * The key cannot be erased because it is
Gilles Peskine65eb8582018-04-19 08:28:58 +0200536 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100537 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200538 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200539 * There was an failure in communication with the cryptoprocessor.
540 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200541 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200542 * The storage is corrupted. Implementations shall make a best effort
543 * to erase key material even in this stage, however applications
544 * should be aware that it may be impossible to guarantee that the
545 * key material is not recoverable in such cases.
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200546 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200547 * An unexpected condition which is not a storage corruption or
548 * a communication failure occurred. The cryptoprocessor may have
549 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300550 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300551 * The library has not been previously initialized by psa_crypto_init().
552 * It is implementation-dependent whether a failure to initialize
553 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100555psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100556
557/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100558 * \brief Export a key in binary format.
559 *
560 * The output of this function can be passed to psa_import_key() to
561 * create an equivalent object.
562 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100563 * If the implementation of psa_import_key() supports other formats
564 * beyond the format specified here, the output from psa_export_key()
565 * must use the representation specified here, not the original
566 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100567 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100568 * For standard key types, the output format is as follows:
569 *
570 * - For symmetric keys (including MAC keys), the format is the
571 * raw bytes of the key.
572 * - For DES, the key data consists of 8 bytes. The parity bits must be
573 * correct.
574 * - For Triple-DES, the format is the concatenation of the
575 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200576 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200577 * is the non-encrypted DER encoding of the representation defined by
578 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
579 * ```
580 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200581 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200582 * modulus INTEGER, -- n
583 * publicExponent INTEGER, -- e
584 * privateExponent INTEGER, -- d
585 * prime1 INTEGER, -- p
586 * prime2 INTEGER, -- q
587 * exponent1 INTEGER, -- d mod (p-1)
588 * exponent2 INTEGER, -- d mod (q-1)
589 * coefficient INTEGER, -- (inverse of q) mod p
590 * }
591 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200592 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200593 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100594 * a representation of the private value as a `ceiling(m/8)`-byte string
595 * where `m` is the bit size associated with the curve, i.e. the bit size
596 * of the order of the curve's coordinate field. This byte string is
597 * in little-endian order for Montgomery curves (curve types
598 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
599 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
600 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100601 * This is the content of the `privateKey` field of the `ECPrivateKey`
602 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200603 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200604 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000605 * format is the representation of the private key `x` as a big-endian byte
606 * string. The length of the byte string is the private key size in bytes
607 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200608 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
609 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100610 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200611 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
612 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100613 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200614 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200615 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200616 * \param[out] data_length On success, the number of bytes
617 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100618 *
Gilles Peskine28538492018-07-11 17:34:00 +0200619 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100620 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200621 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200622 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100623 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200624 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
625 * The size of the \p data buffer is too small. You can determine a
626 * sufficient buffer size by calling
627 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
628 * where \c type is the key type
629 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200630 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
631 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200632 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100633 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100634 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300635 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300636 * The library has not been previously initialized by psa_crypto_init().
637 * It is implementation-dependent whether a failure to initialize
638 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100639 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100640psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100641 uint8_t *data,
642 size_t data_size,
643 size_t *data_length);
644
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100645/**
646 * \brief Export a public key or the public part of a key pair in binary format.
647 *
648 * The output of this function can be passed to psa_import_key() to
649 * create an object that is equivalent to the public key.
650 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000651 * This specification supports a single format for each key type.
652 * Implementations may support other formats as long as the standard
653 * format is supported. Implementations that support other formats
654 * should ensure that the formats are clearly unambiguous so as to
655 * minimize the risk that an invalid input is accidentally interpreted
656 * according to a different format.
657 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000658 * For standard key types, the output format is as follows:
659 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
660 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
661 * ```
662 * RSAPublicKey ::= SEQUENCE {
663 * modulus INTEGER, -- n
664 * publicExponent INTEGER } -- e
665 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000666 * - For elliptic curve public keys (key types for which
667 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
668 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
669 * Let `m` be the bit size associated with the curve, i.e. the bit size of
670 * `q` for a curve over `F_q`. The representation consists of:
671 * - The byte 0x04;
672 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
673 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200674 * - For Diffie-Hellman key exchange public keys (key types for which
675 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000676 * the format is the representation of the public key `y = g^x mod p` as a
677 * big-endian byte string. The length of the byte string is the length of the
678 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100679 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200680 * Exporting a public key object or the public part of a key pair is
681 * always permitted, regardless of the key's usage flags.
682 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100683 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200684 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200685 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200686 * \param[out] data_length On success, the number of bytes
687 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100688 *
Gilles Peskine28538492018-07-11 17:34:00 +0200689 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100690 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200691 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200692 * The key is neither a public key nor a key pair.
693 * \retval #PSA_ERROR_NOT_SUPPORTED
694 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
695 * The size of the \p data buffer is too small. You can determine a
696 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200697 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200698 * where \c type is the key type
699 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200700 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
701 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200702 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100703 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100704 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300705 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300706 * The library has not been previously initialized by psa_crypto_init().
707 * It is implementation-dependent whether a failure to initialize
708 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100709 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100710psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100711 uint8_t *data,
712 size_t data_size,
713 size_t *data_length);
714
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100715/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100716 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100717 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000718 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100719 * This function is primarily useful to copy a key from one location
720 * to another, since it populates a key using the material from
721 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200722 *
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100723 * This function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100724 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100725 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200726 * The policy on the source key must have the usage flag
727 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200728 * This flag is sufficient to permit the copy if the key has the lifetime
729 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
730 * Some secure elements do not provide a way to copy a key without
731 * making it extractable from the secure element. If a key is located
732 * in such a secure element, then the key must have both usage flags
733 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
734 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200735 *
Gilles Peskine20628592019-04-19 19:29:50 +0200736 * The resulting key may only be used in a way that conforms to
737 * both the policy of the original key and the policy specified in
738 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100739 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200740 * usage flags on the source policy and the usage flags in \p attributes.
741 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100742 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200743 * - If either of the policies allows an algorithm and the other policy
744 * allows a wildcard-based algorithm policy that includes this algorithm,
745 * the resulting key allows the same algorithm.
746 * - If the policies do not allow any algorithm in common, this function
747 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200748 *
Gilles Peskine20628592019-04-19 19:29:50 +0200749 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100750 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200751 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100752 * \param source_handle The key to copy. It must be a valid key handle.
Gilles Peskine20628592019-04-19 19:29:50 +0200753 * \param[in] attributes The attributes for the new key.
754 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200755 * - The key type and size may be 0. If either is
756 * nonzero, it must match the corresponding
757 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200758 * - The key location (the lifetime and, for
759 * persistent keys, the key identifier) is
760 * used directly.
761 * - The policy constraints (usage flags and
762 * algorithm policy) are combined from
763 * the source key and \p attributes so that
764 * both sets of restrictions apply, as
765 * described in the documentation of this function.
766 * \param[out] target_handle On success, a handle to the newly created key.
767 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200768 *
769 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100770 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200771 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200772 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200773 * This is an attempt to create a persistent key, and there is
774 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200775 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200776 * The lifetime or identifier in \p attributes are invalid.
777 * \retval #PSA_ERROR_INVALID_ARGUMENT
778 * The policy constraints on the source and specified in
779 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200780 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200781 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200782 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100783 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200784 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
785 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100786 * The source key is not exportable and its lifetime does not
787 * allow copying it to the target's lifetime.
788 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
789 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200790 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
791 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shaw60b03202019-08-06 17:26:16 +0100792 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200793 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100794 * \retval #PSA_ERROR_BAD_STATE
795 * The library has not been previously initialized by psa_crypto_init().
796 * It is implementation-dependent whether a failure to initialize
797 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100798 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100799psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200800 const psa_key_attributes_t *attributes,
801 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100802
803/**@}*/
804
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100805/** \defgroup hash Message digests
806 * @{
807 */
808
Gilles Peskine69647a42019-01-14 20:18:12 +0100809/** Calculate the hash (digest) of a message.
810 *
811 * \note To verify the hash of a message against an
812 * expected value, use psa_hash_compare() instead.
813 *
814 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
815 * such that #PSA_ALG_IS_HASH(\p alg) is true).
816 * \param[in] input Buffer containing the message to hash.
817 * \param input_length Size of the \p input buffer in bytes.
818 * \param[out] hash Buffer where the hash is to be written.
819 * \param hash_size Size of the \p hash buffer in bytes.
820 * \param[out] hash_length On success, the number of bytes
821 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100822 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100823 *
824 * \retval #PSA_SUCCESS
825 * Success.
826 * \retval #PSA_ERROR_NOT_SUPPORTED
827 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100828 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
829 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100830 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
831 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
832 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200833 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100834 * \retval #PSA_ERROR_BAD_STATE
835 * The library has not been previously initialized by psa_crypto_init().
836 * It is implementation-dependent whether a failure to initialize
837 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100838 */
839psa_status_t psa_hash_compute(psa_algorithm_t alg,
840 const uint8_t *input,
841 size_t input_length,
842 uint8_t *hash,
843 size_t hash_size,
844 size_t *hash_length);
845
846/** Calculate the hash (digest) of a message and compare it with a
847 * reference value.
848 *
849 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
850 * such that #PSA_ALG_IS_HASH(\p alg) is true).
851 * \param[in] input Buffer containing the message to hash.
852 * \param input_length Size of the \p input buffer in bytes.
853 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100854 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100855 *
856 * \retval #PSA_SUCCESS
857 * The expected hash is identical to the actual hash of the input.
858 * \retval #PSA_ERROR_INVALID_SIGNATURE
859 * The hash of the message was calculated successfully, but it
860 * differs from the expected hash.
861 * \retval #PSA_ERROR_NOT_SUPPORTED
862 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100863 * \retval #PSA_ERROR_INVALID_ARGUMENT
864 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100865 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
866 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
867 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200868 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100869 * \retval #PSA_ERROR_BAD_STATE
870 * The library has not been previously initialized by psa_crypto_init().
871 * It is implementation-dependent whether a failure to initialize
872 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100873 */
874psa_status_t psa_hash_compare(psa_algorithm_t alg,
875 const uint8_t *input,
876 size_t input_length,
877 const uint8_t *hash,
878 const size_t hash_length);
879
Gilles Peskine308b91d2018-02-08 09:47:44 +0100880/** The type of the state data structure for multipart hash operations.
881 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000882 * Before calling any function on a hash operation object, the application must
883 * initialize it by any of the following means:
884 * - Set the structure to all-bits-zero, for example:
885 * \code
886 * psa_hash_operation_t operation;
887 * memset(&operation, 0, sizeof(operation));
888 * \endcode
889 * - Initialize the structure to logical zero values, for example:
890 * \code
891 * psa_hash_operation_t operation = {0};
892 * \endcode
893 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
894 * for example:
895 * \code
896 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
897 * \endcode
898 * - Assign the result of the function psa_hash_operation_init()
899 * to the structure, for example:
900 * \code
901 * psa_hash_operation_t operation;
902 * operation = psa_hash_operation_init();
903 * \endcode
904 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100905 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100906 * make any assumptions about the content of this structure except
907 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100908typedef struct psa_hash_operation_s psa_hash_operation_t;
909
Jaeden Amero6a25b412019-01-04 11:47:44 +0000910/** \def PSA_HASH_OPERATION_INIT
911 *
912 * This macro returns a suitable initializer for a hash operation object
913 * of type #psa_hash_operation_t.
914 */
915#ifdef __DOXYGEN_ONLY__
916/* This is an example definition for documentation purposes.
917 * Implementations should define a suitable value in `crypto_struct.h`.
918 */
919#define PSA_HASH_OPERATION_INIT {0}
920#endif
921
922/** Return an initial value for a hash operation object.
923 */
924static psa_hash_operation_t psa_hash_operation_init(void);
925
Gilles Peskinef45adda2019-01-14 18:29:18 +0100926/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100927 *
928 * The sequence of operations to calculate a hash (message digest)
929 * is as follows:
930 * -# Allocate an operation object which will be passed to all the functions
931 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000932 * -# Initialize the operation object with one of the methods described in the
933 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200934 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100935 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100936 * of the message each time. The hash that is calculated is the hash
937 * of the concatenation of these messages in order.
938 * -# To calculate the hash, call psa_hash_finish().
939 * To compare the hash with an expected value, call psa_hash_verify().
940 *
941 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000942 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100943 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200944 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100945 * eventually terminate the operation. The following events terminate an
946 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100947 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100948 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100949 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000950 * \param[in,out] operation The operation object to set up. It must have
951 * been initialized as per the documentation for
952 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200953 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
954 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100955 *
Gilles Peskine28538492018-07-11 17:34:00 +0200956 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100957 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200958 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200959 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100960 * \retval #PSA_ERROR_BAD_STATE
961 * The operation state is not valid (already set up and not
962 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200963 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
964 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
965 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200966 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100967 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawe970d652019-08-08 14:40:04 +0100968 * \p operation is either not initialized or is in use
969 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100970 * The library has not been previously initialized by psa_crypto_init().
971 * It is implementation-dependent whether a failure to initialize
972 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100973 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200974psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100975 psa_algorithm_t alg);
976
Gilles Peskine308b91d2018-02-08 09:47:44 +0100977/** Add a message fragment to a multipart hash operation.
978 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200979 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100980 *
981 * If this function returns an error status, the operation becomes inactive.
982 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200983 * \param[in,out] operation Active hash operation.
984 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200985 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100986 *
Gilles Peskine28538492018-07-11 17:34:00 +0200987 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100988 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200989 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100990 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200991 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
992 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
993 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200994 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100995 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +0100996 * The operation state is not valid.
997 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100998 * The library has not been previously initialized by psa_crypto_init().
999 * It is implementation-dependent whether a failure to initialize
1000 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001001 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001002psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1003 const uint8_t *input,
1004 size_t input_length);
1005
Gilles Peskine308b91d2018-02-08 09:47:44 +01001006/** Finish the calculation of the hash of a message.
1007 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001008 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001009 * This function calculates the hash of the message formed by concatenating
1010 * the inputs passed to preceding calls to psa_hash_update().
1011 *
1012 * When this function returns, the operation becomes inactive.
1013 *
1014 * \warning Applications should not call this function if they expect
1015 * a specific value for the hash. Call psa_hash_verify() instead.
1016 * Beware that comparing integrity or authenticity data such as
1017 * hash values with a function such as \c memcmp is risky
1018 * because the time taken by the comparison may leak information
1019 * about the hashed data which could allow an attacker to guess
1020 * a valid hash and thereby bypass security controls.
1021 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001022 * \param[in,out] operation Active hash operation.
1023 * \param[out] hash Buffer where the hash is to be written.
1024 * \param hash_size Size of the \p hash buffer in bytes.
1025 * \param[out] hash_length On success, the number of bytes
1026 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001027 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001028 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001029 *
Gilles Peskine28538492018-07-11 17:34:00 +02001030 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001031 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001032 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001033 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001034 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001035 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001036 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001037 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001038 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1039 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1040 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001041 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001042 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001043 * The operation state is not valid.
1044 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001045 * The library has not been previously initialized by psa_crypto_init().
1046 * It is implementation-dependent whether a failure to initialize
1047 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001048 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001049psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1050 uint8_t *hash,
1051 size_t hash_size,
1052 size_t *hash_length);
1053
Gilles Peskine308b91d2018-02-08 09:47:44 +01001054/** Finish the calculation of the hash of a message and compare it with
1055 * an expected value.
1056 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001057 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001058 * This function calculates the hash of the message formed by concatenating
1059 * the inputs passed to preceding calls to psa_hash_update(). It then
1060 * compares the calculated hash with the expected hash passed as a
1061 * parameter to this function.
1062 *
1063 * When this function returns, the operation becomes inactive.
1064 *
Gilles Peskine19067982018-03-20 17:54:53 +01001065 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001066 * comparison between the actual hash and the expected hash is performed
1067 * in constant time.
1068 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001069 * \param[in,out] operation Active hash operation.
1070 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001071 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001072 *
Gilles Peskine28538492018-07-11 17:34:00 +02001073 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001075 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001076 * The hash of the message was calculated successfully, but it
1077 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001078 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001079 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001080 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1081 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1082 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001083 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001084 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001085 * The operation state is not valid.
1086 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001087 * The library has not been previously initialized by psa_crypto_init().
1088 * It is implementation-dependent whether a failure to initialize
1089 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001090 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001091psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1092 const uint8_t *hash,
1093 size_t hash_length);
1094
Gilles Peskine308b91d2018-02-08 09:47:44 +01001095/** Abort a hash operation.
1096 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001097 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001098 * \p operation structure itself. Once aborted, the operation object
1099 * can be reused for another operation by calling
1100 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001101 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001102 * You may call this function any time after the operation object has
1103 * been initialized by any of the following methods:
1104 * - A call to psa_hash_setup(), whether it succeeds or not.
1105 * - Initializing the \c struct to all-bits-zero.
1106 * - Initializing the \c struct to logical zeros, e.g.
1107 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001108 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001109 * In particular, calling psa_hash_abort() after the operation has been
1110 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1111 * psa_hash_verify() is safe and has no effect.
1112 *
1113 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001114 *
Gilles Peskine28538492018-07-11 17:34:00 +02001115 * \retval #PSA_SUCCESS
1116 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001117 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001118 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1119 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001120 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001121 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001122 * The operation state is not valid.
1123 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001124 * The library has not been previously initialized by psa_crypto_init().
1125 * It is implementation-dependent whether a failure to initialize
1126 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001127 */
1128psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001129
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001130/** Clone a hash operation.
1131 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001132 * This function copies the state of an ongoing hash operation to
1133 * a new operation object. In other words, this function is equivalent
1134 * to calling psa_hash_setup() on \p target_operation with the same
1135 * algorithm that \p source_operation was set up for, then
1136 * psa_hash_update() on \p target_operation with the same input that
1137 * that was passed to \p source_operation. After this function returns, the
1138 * two objects are independent, i.e. subsequent calls involving one of
1139 * the objects do not affect the other object.
1140 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001141 * \param[in] source_operation The active hash operation to clone.
1142 * \param[in,out] target_operation The operation object to set up.
1143 * It must be initialized but not active.
1144 *
1145 * \retval #PSA_SUCCESS
1146 * \retval #PSA_ERROR_BAD_STATE
1147 * \p source_operation is not an active hash operation.
1148 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001149 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001150 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1151 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001152 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001153 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001154 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001155 * The operation state is either not initialized or has already been setup.
1156 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001157 * The library has not been previously initialized by psa_crypto_init().
1158 * It is implementation-dependent whether a failure to initialize
1159 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001160 */
1161psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1162 psa_hash_operation_t *target_operation);
1163
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001164/**@}*/
1165
Gilles Peskine8c9def32018-02-08 10:02:12 +01001166/** \defgroup MAC Message authentication codes
1167 * @{
1168 */
1169
Gilles Peskine69647a42019-01-14 20:18:12 +01001170/** Calculate the MAC (message authentication code) of a message.
1171 *
1172 * \note To verify the MAC of a message against an
1173 * expected value, use psa_mac_verify() instead.
1174 * Beware that comparing integrity or authenticity data such as
1175 * MAC values with a function such as \c memcmp is risky
1176 * because the time taken by the comparison may leak information
1177 * about the MAC value which could allow an attacker to guess
1178 * a valid MAC and thereby bypass security controls.
1179 *
1180 * \param handle Handle to the key to use for the operation.
1181 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001182 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001183 * \param[in] input Buffer containing the input message.
1184 * \param input_length Size of the \p input buffer in bytes.
1185 * \param[out] mac Buffer where the MAC value is to be written.
1186 * \param mac_size Size of the \p mac buffer in bytes.
1187 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001188 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001189 *
1190 * \retval #PSA_SUCCESS
1191 * Success.
1192 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001193 * \retval #PSA_ERROR_NOT_PERMITTED
1194 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001195 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001196 * \retval #PSA_ERROR_NOT_SUPPORTED
1197 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001198 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1199 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001200 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1201 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1202 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001203 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001204 * \retval #PSA_ERROR_STORAGE_FAILURE
1205 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001206 * \retval #PSA_ERROR_BAD_STATE
1207 * The library has not been previously initialized by psa_crypto_init().
1208 * It is implementation-dependent whether a failure to initialize
1209 * results in this error code.
1210 */
1211psa_status_t psa_mac_compute(psa_key_handle_t handle,
1212 psa_algorithm_t alg,
1213 const uint8_t *input,
1214 size_t input_length,
1215 uint8_t *mac,
1216 size_t mac_size,
1217 size_t *mac_length);
1218
1219/** Calculate the MAC of a message and compare it with a reference value.
1220 *
1221 * \param handle Handle to the key to use for the operation.
1222 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001223 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001224 * \param[in] input Buffer containing the input message.
1225 * \param input_length Size of the \p input buffer in bytes.
1226 * \param[out] mac Buffer containing the expected MAC value.
1227 * \param mac_length Size of the \p mac buffer in bytes.
1228 *
1229 * \retval #PSA_SUCCESS
1230 * The expected MAC is identical to the actual MAC of the input.
1231 * \retval #PSA_ERROR_INVALID_SIGNATURE
1232 * The MAC of the message was calculated successfully, but it
1233 * differs from the expected value.
1234 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001235 * \retval #PSA_ERROR_NOT_PERMITTED
1236 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001237 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001238 * \retval #PSA_ERROR_NOT_SUPPORTED
1239 * \p alg is not supported or is not a MAC algorithm.
1240 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1241 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1242 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001243 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001244 * \retval #PSA_ERROR_STORAGE_FAILURE
1245 * The key could not be retrieved from storage.
1246 * \retval #PSA_ERROR_BAD_STATE
1247 * The library has not been previously initialized by psa_crypto_init().
1248 * It is implementation-dependent whether a failure to initialize
1249 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001250 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001251psa_status_t psa_mac_verify(psa_key_handle_t handle,
1252 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001253 const uint8_t *input,
1254 size_t input_length,
1255 const uint8_t *mac,
1256 const size_t mac_length);
1257
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001258/** The type of the state data structure for multipart MAC operations.
1259 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001260 * Before calling any function on a MAC operation object, the application must
1261 * initialize it by any of the following means:
1262 * - Set the structure to all-bits-zero, for example:
1263 * \code
1264 * psa_mac_operation_t operation;
1265 * memset(&operation, 0, sizeof(operation));
1266 * \endcode
1267 * - Initialize the structure to logical zero values, for example:
1268 * \code
1269 * psa_mac_operation_t operation = {0};
1270 * \endcode
1271 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1272 * for example:
1273 * \code
1274 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1275 * \endcode
1276 * - Assign the result of the function psa_mac_operation_init()
1277 * to the structure, for example:
1278 * \code
1279 * psa_mac_operation_t operation;
1280 * operation = psa_mac_operation_init();
1281 * \endcode
1282 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001283 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001284 * make any assumptions about the content of this structure except
1285 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001286typedef struct psa_mac_operation_s psa_mac_operation_t;
1287
Jaeden Amero769ce272019-01-04 11:48:03 +00001288/** \def PSA_MAC_OPERATION_INIT
1289 *
1290 * This macro returns a suitable initializer for a MAC operation object of type
1291 * #psa_mac_operation_t.
1292 */
1293#ifdef __DOXYGEN_ONLY__
1294/* This is an example definition for documentation purposes.
1295 * Implementations should define a suitable value in `crypto_struct.h`.
1296 */
1297#define PSA_MAC_OPERATION_INIT {0}
1298#endif
1299
1300/** Return an initial value for a MAC operation object.
1301 */
1302static psa_mac_operation_t psa_mac_operation_init(void);
1303
Gilles Peskinef45adda2019-01-14 18:29:18 +01001304/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001305 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001306 * This function sets up the calculation of the MAC
1307 * (message authentication code) of a byte string.
1308 * To verify the MAC of a message against an
1309 * expected value, use psa_mac_verify_setup() instead.
1310 *
1311 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001312 * -# Allocate an operation object which will be passed to all the functions
1313 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001314 * -# Initialize the operation object with one of the methods described in the
1315 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001316 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001317 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1318 * of the message each time. The MAC that is calculated is the MAC
1319 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001320 * -# At the end of the message, call psa_mac_sign_finish() to finish
1321 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001322 *
1323 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001324 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001325 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001326 * After a successful call to psa_mac_sign_setup(), the application must
1327 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001328 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001329 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001330 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001331 * \param[in,out] operation The operation object to set up. It must have
1332 * been initialized as per the documentation for
1333 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001334 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001335 * It must remain valid until the operation
1336 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001337 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001338 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001339 *
Gilles Peskine28538492018-07-11 17:34:00 +02001340 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001341 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001342 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001343 * \retval #PSA_ERROR_NOT_PERMITTED
1344 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001345 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001346 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001347 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001348 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1349 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1350 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001351 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001352 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001353 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001354 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001355 * The operation state is not valid (already set up and not
1356 * subsequently completed).
1357 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001358 * The library has not been previously initialized by psa_crypto_init().
1359 * It is implementation-dependent whether a failure to initialize
1360 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001361 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001362psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001363 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001364 psa_algorithm_t alg);
1365
Gilles Peskinef45adda2019-01-14 18:29:18 +01001366/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001367 *
1368 * This function sets up the verification of the MAC
1369 * (message authentication code) of a byte string against an expected value.
1370 *
1371 * The sequence of operations to verify a MAC is as follows:
1372 * -# Allocate an operation object which will be passed to all the functions
1373 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001374 * -# Initialize the operation object with one of the methods described in the
1375 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001376 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001377 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1378 * of the message each time. The MAC that is calculated is the MAC
1379 * of the concatenation of these messages in order.
1380 * -# At the end of the message, call psa_mac_verify_finish() to finish
1381 * calculating the actual MAC of the message and verify it against
1382 * the expected value.
1383 *
1384 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001385 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001386 *
1387 * After a successful call to psa_mac_verify_setup(), the application must
1388 * eventually terminate the operation through one of the following methods:
1389 * - A failed call to psa_mac_update().
1390 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1391 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001392 * \param[in,out] operation The operation object to set up. It must have
1393 * been initialized as per the documentation for
1394 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001395 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001396 * It must remain valid until the operation
1397 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001398 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1399 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001400 *
Gilles Peskine28538492018-07-11 17:34:00 +02001401 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001402 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001403 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001404 * \retval #PSA_ERROR_NOT_PERMITTED
1405 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001406 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001407 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001408 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001409 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1410 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1411 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001412 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001413 * \retval #PSA_ERROR_STORAGE_FAILURE
1414 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001415 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001416 * The operation state is not valid (already set up and not
1417 * subsequently completed).
1418 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001419 * The library has not been previously initialized by psa_crypto_init().
1420 * It is implementation-dependent whether a failure to initialize
1421 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001422 */
1423psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001424 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001425 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001426
Gilles Peskinedcd14942018-07-12 00:30:52 +02001427/** Add a message fragment to a multipart MAC operation.
1428 *
1429 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1430 * before calling this function.
1431 *
1432 * If this function returns an error status, the operation becomes inactive.
1433 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001434 * \param[in,out] operation Active MAC operation.
1435 * \param[in] input Buffer containing the message fragment to add to
1436 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001437 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001438 *
1439 * \retval #PSA_SUCCESS
1440 * Success.
1441 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001442 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001443 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1444 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1445 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001446 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001447 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001448 * \retval #PSA_ERROR_BAD_STATE
1449 * The library has not been previously initialized by psa_crypto_init().
1450 * It is implementation-dependent whether a failure to initialize
1451 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001452 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001453psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1454 const uint8_t *input,
1455 size_t input_length);
1456
Gilles Peskinedcd14942018-07-12 00:30:52 +02001457/** Finish the calculation of the MAC of a message.
1458 *
1459 * The application must call psa_mac_sign_setup() before calling this function.
1460 * This function calculates the MAC of the message formed by concatenating
1461 * the inputs passed to preceding calls to psa_mac_update().
1462 *
1463 * When this function returns, the operation becomes inactive.
1464 *
1465 * \warning Applications should not call this function if they expect
1466 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1467 * Beware that comparing integrity or authenticity data such as
1468 * MAC values with a function such as \c memcmp is risky
1469 * because the time taken by the comparison may leak information
1470 * about the MAC value which could allow an attacker to guess
1471 * a valid MAC and thereby bypass security controls.
1472 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001473 * \param[in,out] operation Active MAC operation.
1474 * \param[out] mac Buffer where the MAC value is to be written.
1475 * \param mac_size Size of the \p mac buffer in bytes.
1476 * \param[out] mac_length On success, the number of bytes
1477 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001478 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001479 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001480 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001481 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001482 *
1483 * \retval #PSA_SUCCESS
1484 * Success.
1485 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001486 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001487 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001488 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001489 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1490 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1491 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1492 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001493 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001494 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001495 * \retval #PSA_ERROR_BAD_STATE
1496 * The library has not been previously initialized by psa_crypto_init().
1497 * It is implementation-dependent whether a failure to initialize
1498 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001499 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001500psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1501 uint8_t *mac,
1502 size_t mac_size,
1503 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001504
Gilles Peskinedcd14942018-07-12 00:30:52 +02001505/** Finish the calculation of the MAC of a message and compare it with
1506 * an expected value.
1507 *
1508 * The application must call psa_mac_verify_setup() before calling this function.
1509 * This function calculates the MAC of the message formed by concatenating
1510 * the inputs passed to preceding calls to psa_mac_update(). It then
1511 * compares the calculated MAC with the expected MAC passed as a
1512 * parameter to this function.
1513 *
1514 * When this function returns, the operation becomes inactive.
1515 *
1516 * \note Implementations shall make the best effort to ensure that the
1517 * comparison between the actual MAC and the expected MAC is performed
1518 * in constant time.
1519 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001520 * \param[in,out] operation Active MAC operation.
1521 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001522 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001523 *
1524 * \retval #PSA_SUCCESS
1525 * The expected MAC is identical to the actual MAC of the message.
1526 * \retval #PSA_ERROR_INVALID_SIGNATURE
1527 * The MAC of the message was calculated successfully, but it
1528 * differs from the expected MAC.
1529 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001530 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001531 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1532 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1533 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001534 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001535 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001536 * \retval #PSA_ERROR_BAD_STATE
1537 * The library has not been previously initialized by psa_crypto_init().
1538 * It is implementation-dependent whether a failure to initialize
1539 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001540 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001541psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1542 const uint8_t *mac,
1543 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001544
Gilles Peskinedcd14942018-07-12 00:30:52 +02001545/** Abort a MAC operation.
1546 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001547 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001548 * \p operation structure itself. Once aborted, the operation object
1549 * can be reused for another operation by calling
1550 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001551 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001552 * You may call this function any time after the operation object has
1553 * been initialized by any of the following methods:
1554 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1555 * it succeeds or not.
1556 * - Initializing the \c struct to all-bits-zero.
1557 * - Initializing the \c struct to logical zeros, e.g.
1558 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001559 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001560 * In particular, calling psa_mac_abort() after the operation has been
1561 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1562 * psa_mac_verify_finish() is safe and has no effect.
1563 *
1564 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001565 *
1566 * \retval #PSA_SUCCESS
1567 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001568 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001569 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1570 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001571 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001572 * \retval #PSA_ERROR_BAD_STATE
1573 * The library has not been previously initialized by psa_crypto_init().
1574 * It is implementation-dependent whether a failure to initialize
1575 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001576 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001577psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1578
1579/**@}*/
1580
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001581/** \defgroup cipher Symmetric ciphers
1582 * @{
1583 */
1584
Gilles Peskine69647a42019-01-14 20:18:12 +01001585/** Encrypt a message using a symmetric cipher.
1586 *
1587 * This function encrypts a message with a random IV (initialization
1588 * vector).
1589 *
1590 * \param handle Handle to the key to use for the operation.
1591 * It must remain valid until the operation
1592 * terminates.
1593 * \param alg The cipher algorithm to compute
1594 * (\c PSA_ALG_XXX value such that
1595 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1596 * \param[in] input Buffer containing the message to encrypt.
1597 * \param input_length Size of the \p input buffer in bytes.
1598 * \param[out] output Buffer where the output is to be written.
1599 * The output contains the IV followed by
1600 * the ciphertext proper.
1601 * \param output_size Size of the \p output buffer in bytes.
1602 * \param[out] output_length On success, the number of bytes
1603 * that make up the output.
1604 *
1605 * \retval #PSA_SUCCESS
1606 * Success.
1607 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001608 * \retval #PSA_ERROR_NOT_PERMITTED
1609 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001610 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001611 * \retval #PSA_ERROR_NOT_SUPPORTED
1612 * \p alg is not supported or is not a cipher algorithm.
1613 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1614 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1615 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1616 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001617 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001618 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001619 * \retval #PSA_ERROR_BAD_STATE
1620 * The library has not been previously initialized by psa_crypto_init().
1621 * It is implementation-dependent whether a failure to initialize
1622 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001623 */
1624psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1625 psa_algorithm_t alg,
1626 const uint8_t *input,
1627 size_t input_length,
1628 uint8_t *output,
1629 size_t output_size,
1630 size_t *output_length);
1631
1632/** Decrypt a message using a symmetric cipher.
1633 *
1634 * This function decrypts a message encrypted with a symmetric cipher.
1635 *
1636 * \param handle Handle to the key to use for the operation.
1637 * It must remain valid until the operation
1638 * terminates.
1639 * \param alg The cipher algorithm to compute
1640 * (\c PSA_ALG_XXX value such that
1641 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1642 * \param[in] input Buffer containing the message to decrypt.
1643 * This consists of the IV followed by the
1644 * ciphertext proper.
1645 * \param input_length Size of the \p input buffer in bytes.
1646 * \param[out] output Buffer where the plaintext is to be written.
1647 * \param output_size Size of the \p output buffer in bytes.
1648 * \param[out] output_length On success, the number of bytes
1649 * that make up the output.
1650 *
1651 * \retval #PSA_SUCCESS
1652 * Success.
1653 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001654 * \retval #PSA_ERROR_NOT_PERMITTED
1655 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001656 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001657 * \retval #PSA_ERROR_NOT_SUPPORTED
1658 * \p alg is not supported or is not a cipher algorithm.
1659 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1660 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1661 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1662 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001663 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001664 * \retval #PSA_ERROR_BAD_STATE
1665 * The library has not been previously initialized by psa_crypto_init().
1666 * It is implementation-dependent whether a failure to initialize
1667 * results in this error code. * \retval #PSA_ERROR_CORRUPTION_DETECTED
1668
Gilles Peskine69647a42019-01-14 20:18:12 +01001669 */
1670psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1671 psa_algorithm_t alg,
1672 const uint8_t *input,
1673 size_t input_length,
1674 uint8_t *output,
1675 size_t output_size,
1676 size_t *output_length);
1677
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001678/** The type of the state data structure for multipart cipher operations.
1679 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001680 * Before calling any function on a cipher operation object, the application
1681 * must initialize it by any of the following means:
1682 * - Set the structure to all-bits-zero, for example:
1683 * \code
1684 * psa_cipher_operation_t operation;
1685 * memset(&operation, 0, sizeof(operation));
1686 * \endcode
1687 * - Initialize the structure to logical zero values, for example:
1688 * \code
1689 * psa_cipher_operation_t operation = {0};
1690 * \endcode
1691 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1692 * for example:
1693 * \code
1694 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1695 * \endcode
1696 * - Assign the result of the function psa_cipher_operation_init()
1697 * to the structure, for example:
1698 * \code
1699 * psa_cipher_operation_t operation;
1700 * operation = psa_cipher_operation_init();
1701 * \endcode
1702 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001703 * This is an implementation-defined \c struct. Applications should not
1704 * make any assumptions about the content of this structure except
1705 * as directed by the documentation of a specific implementation. */
1706typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1707
Jaeden Amero5bae2272019-01-04 11:48:27 +00001708/** \def PSA_CIPHER_OPERATION_INIT
1709 *
1710 * This macro returns a suitable initializer for a cipher operation object of
1711 * type #psa_cipher_operation_t.
1712 */
1713#ifdef __DOXYGEN_ONLY__
1714/* This is an example definition for documentation purposes.
1715 * Implementations should define a suitable value in `crypto_struct.h`.
1716 */
1717#define PSA_CIPHER_OPERATION_INIT {0}
1718#endif
1719
1720/** Return an initial value for a cipher operation object.
1721 */
1722static psa_cipher_operation_t psa_cipher_operation_init(void);
1723
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001724/** Set the key for a multipart symmetric encryption operation.
1725 *
1726 * The sequence of operations to encrypt a message with a symmetric cipher
1727 * is as follows:
1728 * -# Allocate an operation object which will be passed to all the functions
1729 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001730 * -# Initialize the operation object with one of the methods described in the
1731 * documentation for #psa_cipher_operation_t, e.g.
1732 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001733 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001734 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001735 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001736 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001737 * requires a specific IV value.
1738 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1739 * of the message each time.
1740 * -# Call psa_cipher_finish().
1741 *
1742 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001743 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001744 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001745 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001746 * eventually terminate the operation. The following events terminate an
1747 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001748 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001749 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001750 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001751 * \param[in,out] operation The operation object to set up. It must have
1752 * been initialized as per the documentation for
1753 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001754 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001755 * It must remain valid until the operation
1756 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001757 * \param alg The cipher algorithm to compute
1758 * (\c PSA_ALG_XXX value such that
1759 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001760 *
Gilles Peskine28538492018-07-11 17:34:00 +02001761 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001762 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001763 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001764 * \retval #PSA_ERROR_NOT_PERMITTED
1765 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001766 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001767 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001768 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001769 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1770 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1771 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001772 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001773 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001774 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001775 * The operation state is not valid (already set up and not
1776 * subsequently completed).
1777 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001778 * The library has not been previously initialized by psa_crypto_init().
1779 * It is implementation-dependent whether a failure to initialize
1780 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001781 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001782psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001783 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001784 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001785
1786/** Set the key for a multipart symmetric decryption operation.
1787 *
1788 * The sequence of operations to decrypt a message with a symmetric cipher
1789 * is as follows:
1790 * -# Allocate an operation object which will be passed to all the functions
1791 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001792 * -# Initialize the operation object with one of the methods described in the
1793 * documentation for #psa_cipher_operation_t, e.g.
1794 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001795 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001796 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001797 * decryption. If the IV is prepended to the ciphertext, you can call
1798 * psa_cipher_update() on a buffer containing the IV followed by the
1799 * beginning of the message.
1800 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1801 * of the message each time.
1802 * -# Call psa_cipher_finish().
1803 *
1804 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001805 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001806 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001807 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001808 * eventually terminate the operation. The following events terminate an
1809 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001810 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001811 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001812 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001813 * \param[in,out] operation The operation object to set up. It must have
1814 * been initialized as per the documentation for
1815 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001816 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001817 * It must remain valid until the operation
1818 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001819 * \param alg The cipher algorithm to compute
1820 * (\c PSA_ALG_XXX value such that
1821 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001822 *
Gilles Peskine28538492018-07-11 17:34:00 +02001823 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001824 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001825 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001826 * \retval #PSA_ERROR_NOT_PERMITTED
1827 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001828 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001829 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001830 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001831 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1832 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1833 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001834 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001835 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001836 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001837 * The operation state is not valid (already set up and not
1838 * subsequently completed).
1839 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001840 * The library has not been previously initialized by psa_crypto_init().
1841 * It is implementation-dependent whether a failure to initialize
1842 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001843 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001844psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001845 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001846 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001847
Gilles Peskinedcd14942018-07-12 00:30:52 +02001848/** Generate an IV for a symmetric encryption operation.
1849 *
1850 * This function generates a random IV (initialization vector), nonce
1851 * or initial counter value for the encryption operation as appropriate
1852 * for the chosen algorithm, key type and key size.
1853 *
1854 * The application must call psa_cipher_encrypt_setup() before
1855 * calling this function.
1856 *
1857 * If this function returns an error status, the operation becomes inactive.
1858 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001859 * \param[in,out] operation Active cipher operation.
1860 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001861 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001862 * \param[out] iv_length On success, the number of bytes of the
1863 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001864 *
1865 * \retval #PSA_SUCCESS
1866 * Success.
1867 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001868 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001869 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001870 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001871 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1872 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1873 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001874 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001875 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001876 * \retval #PSA_ERROR_BAD_STATE
1877 * The library has not been previously initialized by psa_crypto_init().
1878 * It is implementation-dependent whether a failure to initialize
1879 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001880 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001881psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001882 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001883 size_t iv_size,
1884 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001885
Gilles Peskinedcd14942018-07-12 00:30:52 +02001886/** Set the IV for a symmetric encryption or decryption operation.
1887 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001888 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001889 * or initial counter value for the encryption or decryption operation.
1890 *
1891 * The application must call psa_cipher_encrypt_setup() before
1892 * calling this function.
1893 *
1894 * If this function returns an error status, the operation becomes inactive.
1895 *
1896 * \note When encrypting, applications should use psa_cipher_generate_iv()
1897 * instead of this function, unless implementing a protocol that requires
1898 * a non-random IV.
1899 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001900 * \param[in,out] operation Active cipher operation.
1901 * \param[in] iv Buffer containing the IV to use.
1902 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001903 *
1904 * \retval #PSA_SUCCESS
1905 * Success.
1906 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001907 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001908 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001909 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001910 * or the chosen algorithm does not use an IV.
1911 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1912 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1913 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001914 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001915 * \retval #PSA_ERROR_BAD_STATE
1916 * The library has not been previously initialized by psa_crypto_init().
1917 * It is implementation-dependent whether a failure to initialize
1918 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001919 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001920psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001921 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001922 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001923
Gilles Peskinedcd14942018-07-12 00:30:52 +02001924/** Encrypt or decrypt a message fragment in an active cipher operation.
1925 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001926 * Before calling this function, you must:
1927 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1928 * The choice of setup function determines whether this function
1929 * encrypts or decrypts its input.
1930 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1931 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001932 *
1933 * If this function returns an error status, the operation becomes inactive.
1934 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001935 * \param[in,out] operation Active cipher operation.
1936 * \param[in] input Buffer containing the message fragment to
1937 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001938 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001939 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001940 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001941 * \param[out] output_length On success, the number of bytes
1942 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001943 *
1944 * \retval #PSA_SUCCESS
1945 * Success.
1946 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001947 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001948 * not set, or already completed).
1949 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1950 * The size of the \p output buffer is too small.
1951 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1952 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1953 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001954 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001955 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001956 * \retval #PSA_ERROR_BAD_STATE
1957 * The library has not been previously initialized by psa_crypto_init().
1958 * It is implementation-dependent whether a failure to initialize
1959 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001960 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001961psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1962 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001963 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001964 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001965 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001966 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001967
Gilles Peskinedcd14942018-07-12 00:30:52 +02001968/** Finish encrypting or decrypting a message in a cipher operation.
1969 *
1970 * The application must call psa_cipher_encrypt_setup() or
1971 * psa_cipher_decrypt_setup() before calling this function. The choice
1972 * of setup function determines whether this function encrypts or
1973 * decrypts its input.
1974 *
1975 * This function finishes the encryption or decryption of the message
1976 * formed by concatenating the inputs passed to preceding calls to
1977 * psa_cipher_update().
1978 *
1979 * When this function returns, the operation becomes inactive.
1980 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001981 * \param[in,out] operation Active cipher operation.
1982 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001983 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001984 * \param[out] output_length On success, the number of bytes
1985 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001986 *
1987 * \retval #PSA_SUCCESS
1988 * Success.
1989 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001990 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001991 * not set, or already completed).
1992 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1993 * The size of the \p output buffer is too small.
1994 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1995 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1996 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001997 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001998 * \retval #PSA_ERROR_BAD_STATE
1999 * The library has not been previously initialized by psa_crypto_init().
2000 * It is implementation-dependent whether a failure to initialize
2001 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002002 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002003psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002004 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002005 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002006 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002007
Gilles Peskinedcd14942018-07-12 00:30:52 +02002008/** Abort a cipher operation.
2009 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002010 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002011 * \p operation structure itself. Once aborted, the operation object
2012 * can be reused for another operation by calling
2013 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002014 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002015 * You may call this function any time after the operation object has
2016 * been initialized by any of the following methods:
2017 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2018 * whether it succeeds or not.
2019 * - Initializing the \c struct to all-bits-zero.
2020 * - Initializing the \c struct to logical zeros, e.g.
2021 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002022 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002023 * In particular, calling psa_cipher_abort() after the operation has been
2024 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2025 * is safe and has no effect.
2026 *
2027 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002028 *
2029 * \retval #PSA_SUCCESS
2030 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002031 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002032 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2033 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002034 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002035 * \retval #PSA_ERROR_BAD_STATE
2036 * The library has not been previously initialized by psa_crypto_init().
2037 * It is implementation-dependent whether a failure to initialize
2038 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002039 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002040psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2041
2042/**@}*/
2043
Gilles Peskine3b555712018-03-03 21:27:57 +01002044/** \defgroup aead Authenticated encryption with associated data (AEAD)
2045 * @{
2046 */
2047
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002048/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002049 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002050 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002051 * \param alg The AEAD algorithm to compute
2052 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002053 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002054 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002055 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002056 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002057 * but not encrypted.
2058 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002059 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002060 * encrypted.
2061 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002062 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002063 * encrypted data. The additional data is not
2064 * part of this output. For algorithms where the
2065 * encrypted data and the authentication tag
2066 * are defined as separate outputs, the
2067 * authentication tag is appended to the
2068 * encrypted data.
2069 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2070 * This must be at least
2071 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2072 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002073 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002074 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002075 *
Gilles Peskine28538492018-07-11 17:34:00 +02002076 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002077 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002078 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002079 * \retval #PSA_ERROR_NOT_PERMITTED
2080 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002081 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002082 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002083 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002084 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002085 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2086 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002087 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2088 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002089 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002090 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002091 * The library has not been previously initialized by psa_crypto_init().
2092 * It is implementation-dependent whether a failure to initialize
2093 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002094 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002095psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002096 psa_algorithm_t alg,
2097 const uint8_t *nonce,
2098 size_t nonce_length,
2099 const uint8_t *additional_data,
2100 size_t additional_data_length,
2101 const uint8_t *plaintext,
2102 size_t plaintext_length,
2103 uint8_t *ciphertext,
2104 size_t ciphertext_size,
2105 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002106
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002107/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002108 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002109 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002110 * \param alg The AEAD algorithm to compute
2111 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002112 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002113 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002114 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002115 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002116 * but not encrypted.
2117 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002118 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002119 * encrypted. For algorithms where the
2120 * encrypted data and the authentication tag
2121 * are defined as separate inputs, the buffer
2122 * must contain the encrypted data followed
2123 * by the authentication tag.
2124 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002125 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002126 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2127 * This must be at least
2128 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2129 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002130 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002131 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002132 *
Gilles Peskine28538492018-07-11 17:34:00 +02002133 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002134 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002135 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002136 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002137 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002138 * \retval #PSA_ERROR_NOT_PERMITTED
2139 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002140 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002141 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002142 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002143 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002144 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2145 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002146 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2147 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002148 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002149 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002150 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002151 * The library has not been previously initialized by psa_crypto_init().
2152 * It is implementation-dependent whether a failure to initialize
2153 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002154 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002155psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002156 psa_algorithm_t alg,
2157 const uint8_t *nonce,
2158 size_t nonce_length,
2159 const uint8_t *additional_data,
2160 size_t additional_data_length,
2161 const uint8_t *ciphertext,
2162 size_t ciphertext_length,
2163 uint8_t *plaintext,
2164 size_t plaintext_size,
2165 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002166
Gilles Peskine30a9e412019-01-14 18:36:12 +01002167/** The type of the state data structure for multipart AEAD operations.
2168 *
2169 * Before calling any function on an AEAD operation object, the application
2170 * must initialize it by any of the following means:
2171 * - Set the structure to all-bits-zero, for example:
2172 * \code
2173 * psa_aead_operation_t operation;
2174 * memset(&operation, 0, sizeof(operation));
2175 * \endcode
2176 * - Initialize the structure to logical zero values, for example:
2177 * \code
2178 * psa_aead_operation_t operation = {0};
2179 * \endcode
2180 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2181 * for example:
2182 * \code
2183 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2184 * \endcode
2185 * - Assign the result of the function psa_aead_operation_init()
2186 * to the structure, for example:
2187 * \code
2188 * psa_aead_operation_t operation;
2189 * operation = psa_aead_operation_init();
2190 * \endcode
2191 *
2192 * This is an implementation-defined \c struct. Applications should not
2193 * make any assumptions about the content of this structure except
2194 * as directed by the documentation of a specific implementation. */
2195typedef struct psa_aead_operation_s psa_aead_operation_t;
2196
2197/** \def PSA_AEAD_OPERATION_INIT
2198 *
2199 * This macro returns a suitable initializer for an AEAD operation object of
2200 * type #psa_aead_operation_t.
2201 */
2202#ifdef __DOXYGEN_ONLY__
2203/* This is an example definition for documentation purposes.
2204 * Implementations should define a suitable value in `crypto_struct.h`.
2205 */
2206#define PSA_AEAD_OPERATION_INIT {0}
2207#endif
2208
2209/** Return an initial value for an AEAD operation object.
2210 */
2211static psa_aead_operation_t psa_aead_operation_init(void);
2212
2213/** Set the key for a multipart authenticated encryption operation.
2214 *
2215 * The sequence of operations to encrypt a message with authentication
2216 * is as follows:
2217 * -# Allocate an operation object which will be passed to all the functions
2218 * listed here.
2219 * -# Initialize the operation object with one of the methods described in the
2220 * documentation for #psa_aead_operation_t, e.g.
2221 * PSA_AEAD_OPERATION_INIT.
2222 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002223 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2224 * inputs to the subsequent calls to psa_aead_update_ad() and
2225 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2226 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002227 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2228 * generate or set the nonce. You should use
2229 * psa_aead_generate_nonce() unless the protocol you are implementing
2230 * requires a specific nonce value.
2231 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2232 * of the non-encrypted additional authenticated data each time.
2233 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002234 * of the message to encrypt each time.
Adrian L. Shaw67257572019-08-15 10:53:47 +01002235 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002236 *
2237 * The application may call psa_aead_abort() at any time after the operation
2238 * has been initialized.
2239 *
2240 * After a successful call to psa_aead_encrypt_setup(), the application must
2241 * eventually terminate the operation. The following events terminate an
2242 * operation:
2243 * - A failed call to any of the \c psa_aead_xxx functions.
2244 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2245 *
2246 * \param[in,out] operation The operation object to set up. It must have
2247 * been initialized as per the documentation for
2248 * #psa_aead_operation_t and not yet in use.
2249 * \param handle Handle to the key to use for the operation.
2250 * It must remain valid until the operation
2251 * terminates.
2252 * \param alg The AEAD algorithm to compute
2253 * (\c PSA_ALG_XXX value such that
2254 * #PSA_ALG_IS_AEAD(\p alg) is true).
2255 *
2256 * \retval #PSA_SUCCESS
2257 * Success.
2258 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002259 * \retval #PSA_ERROR_NOT_PERMITTED
2260 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002261 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002262 * \retval #PSA_ERROR_NOT_SUPPORTED
2263 * \p alg is not supported or is not an AEAD algorithm.
2264 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2265 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2266 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002267 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002268 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002269 * \retval #PSA_ERROR_BAD_STATE
2270 * The library has not been previously initialized by psa_crypto_init().
2271 * It is implementation-dependent whether a failure to initialize
2272 * results in this error code.
2273 */
2274psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2275 psa_key_handle_t handle,
2276 psa_algorithm_t alg);
2277
2278/** Set the key for a multipart authenticated decryption operation.
2279 *
2280 * The sequence of operations to decrypt a message with authentication
2281 * is as follows:
2282 * -# Allocate an operation object which will be passed to all the functions
2283 * listed here.
2284 * -# Initialize the operation object with one of the methods described in the
2285 * documentation for #psa_aead_operation_t, e.g.
2286 * PSA_AEAD_OPERATION_INIT.
2287 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002288 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2289 * inputs to the subsequent calls to psa_aead_update_ad() and
2290 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2291 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002292 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2293 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2294 * of the non-encrypted additional authenticated data each time.
2295 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002296 * of the ciphertext to decrypt each time.
2297 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002298 *
2299 * The application may call psa_aead_abort() at any time after the operation
2300 * has been initialized.
2301 *
2302 * After a successful call to psa_aead_decrypt_setup(), the application must
2303 * eventually terminate the operation. The following events terminate an
2304 * operation:
2305 * - A failed call to any of the \c psa_aead_xxx functions.
2306 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2307 *
2308 * \param[in,out] operation The operation object to set up. It must have
2309 * been initialized as per the documentation for
2310 * #psa_aead_operation_t and not yet in use.
2311 * \param handle Handle to the key to use for the operation.
2312 * It must remain valid until the operation
2313 * terminates.
2314 * \param alg The AEAD algorithm to compute
2315 * (\c PSA_ALG_XXX value such that
2316 * #PSA_ALG_IS_AEAD(\p alg) is true).
2317 *
2318 * \retval #PSA_SUCCESS
2319 * Success.
2320 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002321 * \retval #PSA_ERROR_NOT_PERMITTED
2322 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002323 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002324 * \retval #PSA_ERROR_NOT_SUPPORTED
2325 * \p alg is not supported or is not an AEAD algorithm.
2326 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2327 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2328 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002329 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002330 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002331 * \retval #PSA_ERROR_BAD_STATE
2332 * The library has not been previously initialized by psa_crypto_init().
2333 * It is implementation-dependent whether a failure to initialize
2334 * results in this error code.
2335 */
2336psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2337 psa_key_handle_t handle,
2338 psa_algorithm_t alg);
2339
2340/** Generate a random nonce for an authenticated encryption operation.
2341 *
2342 * This function generates a random nonce for the authenticated encryption
2343 * operation with an appropriate size for the chosen algorithm, key type
2344 * and key size.
2345 *
2346 * The application must call psa_aead_encrypt_setup() before
2347 * calling this function.
2348 *
2349 * If this function returns an error status, the operation becomes inactive.
2350 *
2351 * \param[in,out] operation Active AEAD operation.
2352 * \param[out] nonce Buffer where the generated nonce is to be
2353 * written.
2354 * \param nonce_size Size of the \p nonce buffer in bytes.
2355 * \param[out] nonce_length On success, the number of bytes of the
2356 * generated nonce.
2357 *
2358 * \retval #PSA_SUCCESS
2359 * Success.
2360 * \retval #PSA_ERROR_BAD_STATE
2361 * The operation state is not valid (not set up, or nonce already set).
2362 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2363 * The size of the \p nonce buffer is too small.
2364 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2365 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2366 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002367 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002368 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002369 * \retval #PSA_ERROR_BAD_STATE
2370 * The library has not been previously initialized by psa_crypto_init().
2371 * It is implementation-dependent whether a failure to initialize
2372 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002373 */
2374psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002375 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002376 size_t nonce_size,
2377 size_t *nonce_length);
2378
2379/** Set the nonce for an authenticated encryption or decryption operation.
2380 *
2381 * This function sets the nonce for the authenticated
2382 * encryption or decryption operation.
2383 *
2384 * The application must call psa_aead_encrypt_setup() before
2385 * calling this function.
2386 *
2387 * If this function returns an error status, the operation becomes inactive.
2388 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002389 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002390 * instead of this function, unless implementing a protocol that requires
2391 * a non-random IV.
2392 *
2393 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002394 * \param[in] nonce Buffer containing the nonce to use.
2395 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002396 *
2397 * \retval #PSA_SUCCESS
2398 * Success.
2399 * \retval #PSA_ERROR_BAD_STATE
2400 * The operation state is not valid (not set up, or nonce already set).
2401 * \retval #PSA_ERROR_INVALID_ARGUMENT
2402 * The size of \p nonce is not acceptable for the chosen algorithm.
2403 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2404 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2405 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002406 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002407 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002408 * \retval #PSA_ERROR_BAD_STATE
2409 * The library has not been previously initialized by psa_crypto_init().
2410 * It is implementation-dependent whether a failure to initialize
2411 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002412 */
2413psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002414 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002415 size_t nonce_length);
2416
Gilles Peskinebc59c852019-01-17 15:26:08 +01002417/** Declare the lengths of the message and additional data for AEAD.
2418 *
2419 * The application must call this function before calling
2420 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2421 * the operation requires it. If the algorithm does not require it,
2422 * calling this function is optional, but if this function is called
2423 * then the implementation must enforce the lengths.
2424 *
2425 * You may call this function before or after setting the nonce with
2426 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2427 *
2428 * - For #PSA_ALG_CCM, calling this function is required.
2429 * - For the other AEAD algorithms defined in this specification, calling
2430 * this function is not required.
2431 * - For vendor-defined algorithm, refer to the vendor documentation.
2432 *
2433 * \param[in,out] operation Active AEAD operation.
2434 * \param ad_length Size of the non-encrypted additional
2435 * authenticated data in bytes.
2436 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2437 *
2438 * \retval #PSA_SUCCESS
2439 * Success.
2440 * \retval #PSA_ERROR_BAD_STATE
2441 * The operation state is not valid (not set up, already completed,
2442 * or psa_aead_update_ad() or psa_aead_update() already called).
2443 * \retval #PSA_ERROR_INVALID_ARGUMENT
2444 * At least one of the lengths is not acceptable for the chosen
2445 * algorithm.
2446 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2447 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2448 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002449 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002450 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002451 * \retval #PSA_ERROR_BAD_STATE
2452 * The library has not been previously initialized by psa_crypto_init().
2453 * It is implementation-dependent whether a failure to initialize
2454 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002455 */
2456psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2457 size_t ad_length,
2458 size_t plaintext_length);
2459
Gilles Peskine30a9e412019-01-14 18:36:12 +01002460/** Pass additional data to an active AEAD operation.
2461 *
2462 * Additional data is authenticated, but not encrypted.
2463 *
2464 * You may call this function multiple times to pass successive fragments
2465 * of the additional data. You may not call this function after passing
2466 * data to encrypt or decrypt with psa_aead_update().
2467 *
2468 * Before calling this function, you must:
2469 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2470 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2471 *
2472 * If this function returns an error status, the operation becomes inactive.
2473 *
2474 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2475 * there is no guarantee that the input is valid. Therefore, until
2476 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2477 * treat the input as untrusted and prepare to undo any action that
2478 * depends on the input if psa_aead_verify() returns an error status.
2479 *
2480 * \param[in,out] operation Active AEAD operation.
2481 * \param[in] input Buffer containing the fragment of
2482 * additional data.
2483 * \param input_length Size of the \p input buffer in bytes.
2484 *
2485 * \retval #PSA_SUCCESS
2486 * Success.
2487 * \retval #PSA_ERROR_BAD_STATE
2488 * The operation state is not valid (not set up, nonce not set,
2489 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002490 * \retval #PSA_ERROR_INVALID_ARGUMENT
2491 * The total input length overflows the additional data length that
2492 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002493 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2494 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2495 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002496 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002497 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002498 * \retval #PSA_ERROR_BAD_STATE
2499 * The library has not been previously initialized by psa_crypto_init().
2500 * It is implementation-dependent whether a failure to initialize
2501 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002502 */
2503psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2504 const uint8_t *input,
2505 size_t input_length);
2506
2507/** Encrypt or decrypt a message fragment in an active AEAD operation.
2508 *
2509 * Before calling this function, you must:
2510 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2511 * The choice of setup function determines whether this function
2512 * encrypts or decrypts its input.
2513 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2514 * 3. Call psa_aead_update_ad() to pass all the additional data.
2515 *
2516 * If this function returns an error status, the operation becomes inactive.
2517 *
2518 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2519 * there is no guarantee that the input is valid. Therefore, until
2520 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2521 * - Do not use the output in any way other than storing it in a
2522 * confidential location. If you take any action that depends
2523 * on the tentative decrypted data, this action will need to be
2524 * undone if the input turns out not to be valid. Furthermore,
2525 * if an adversary can observe that this action took place
2526 * (for example through timing), they may be able to use this
2527 * fact as an oracle to decrypt any message encrypted with the
2528 * same key.
2529 * - In particular, do not copy the output anywhere but to a
2530 * memory or storage space that you have exclusive access to.
2531 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002532 * This function does not require the input to be aligned to any
2533 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002534 * a whole block at a time, it must consume all the input provided, but
2535 * it may delay the end of the corresponding output until a subsequent
2536 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2537 * provides sufficient input. The amount of data that can be delayed
2538 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002539 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002540 * \param[in,out] operation Active AEAD operation.
2541 * \param[in] input Buffer containing the message fragment to
2542 * encrypt or decrypt.
2543 * \param input_length Size of the \p input buffer in bytes.
2544 * \param[out] output Buffer where the output is to be written.
2545 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002546 * This must be at least
2547 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2548 * \p input_length) where \c alg is the
2549 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002550 * \param[out] output_length On success, the number of bytes
2551 * that make up the returned output.
2552 *
2553 * \retval #PSA_SUCCESS
2554 * Success.
2555 * \retval #PSA_ERROR_BAD_STATE
2556 * The operation state is not valid (not set up, nonce not set
2557 * or already completed).
2558 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2559 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002560 * You can determine a sufficient buffer size by calling
2561 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2562 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002563 * \retval #PSA_ERROR_INVALID_ARGUMENT
2564 * The total length of input to psa_aead_update_ad() so far is
2565 * less than the additional data length that was previously
2566 * specified with psa_aead_set_lengths().
2567 * \retval #PSA_ERROR_INVALID_ARGUMENT
2568 * The total input length overflows the plaintext length that
2569 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002570 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2571 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2572 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002573 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002574 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002575 * \retval #PSA_ERROR_BAD_STATE
2576 * The library has not been previously initialized by psa_crypto_init().
2577 * It is implementation-dependent whether a failure to initialize
2578 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002579 */
2580psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2581 const uint8_t *input,
2582 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002583 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002584 size_t output_size,
2585 size_t *output_length);
2586
2587/** Finish encrypting a message in an AEAD operation.
2588 *
2589 * The operation must have been set up with psa_aead_encrypt_setup().
2590 *
2591 * This function finishes the authentication of the additional data
2592 * formed by concatenating the inputs passed to preceding calls to
2593 * psa_aead_update_ad() with the plaintext formed by concatenating the
2594 * inputs passed to preceding calls to psa_aead_update().
2595 *
2596 * This function has two output buffers:
2597 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002598 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002599 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002600 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002601 * that the operation performs.
2602 *
2603 * When this function returns, the operation becomes inactive.
2604 *
2605 * \param[in,out] operation Active AEAD operation.
2606 * \param[out] ciphertext Buffer where the last part of the ciphertext
2607 * is to be written.
2608 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002609 * This must be at least
2610 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2611 * \c alg is the algorithm that is being
2612 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002613 * \param[out] ciphertext_length On success, the number of bytes of
2614 * returned ciphertext.
2615 * \param[out] tag Buffer where the authentication tag is
2616 * to be written.
2617 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002618 * This must be at least
2619 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2620 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002621 * \param[out] tag_length On success, the number of bytes
2622 * that make up the returned tag.
2623 *
2624 * \retval #PSA_SUCCESS
2625 * Success.
2626 * \retval #PSA_ERROR_BAD_STATE
2627 * The operation state is not valid (not set up, nonce not set,
2628 * decryption, or already completed).
2629 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002630 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002631 * You can determine a sufficient buffer size for \p ciphertext by
2632 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2633 * where \c alg is the algorithm that is being calculated.
2634 * You can determine a sufficient buffer size for \p tag by
2635 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002636 * \retval #PSA_ERROR_INVALID_ARGUMENT
2637 * The total length of input to psa_aead_update_ad() so far is
2638 * less than the additional data length that was previously
2639 * specified with psa_aead_set_lengths().
2640 * \retval #PSA_ERROR_INVALID_ARGUMENT
2641 * The total length of input to psa_aead_update() so far is
2642 * less than the plaintext length that was previously
2643 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002644 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2645 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2646 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002647 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002648 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002649 * \retval #PSA_ERROR_BAD_STATE
2650 * The library has not been previously initialized by psa_crypto_init().
2651 * It is implementation-dependent whether a failure to initialize
2652 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002653 */
2654psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002655 uint8_t *ciphertext,
2656 size_t ciphertext_size,
2657 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002658 uint8_t *tag,
2659 size_t tag_size,
2660 size_t *tag_length);
2661
2662/** Finish authenticating and decrypting a message in an AEAD operation.
2663 *
2664 * The operation must have been set up with psa_aead_decrypt_setup().
2665 *
2666 * This function finishes the authentication of the additional data
2667 * formed by concatenating the inputs passed to preceding calls to
2668 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2669 * inputs passed to preceding calls to psa_aead_update().
2670 *
2671 * When this function returns, the operation becomes inactive.
2672 *
2673 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002674 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002675 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002676 * from previous calls to psa_aead_update()
2677 * that could not be processed until the end
2678 * of the input.
2679 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002680 * This must be at least
2681 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2682 * \c alg is the algorithm that is being
2683 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002684 * \param[out] plaintext_length On success, the number of bytes of
2685 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002686 * \param[in] tag Buffer containing the authentication tag.
2687 * \param tag_length Size of the \p tag buffer in bytes.
2688 *
2689 * \retval #PSA_SUCCESS
2690 * Success.
2691 * \retval #PSA_ERROR_BAD_STATE
2692 * The operation state is not valid (not set up, nonce not set,
2693 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002694 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2695 * The size of the \p plaintext buffer is too small.
2696 * You can determine a sufficient buffer size for \p plaintext by
2697 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2698 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002699 * \retval #PSA_ERROR_INVALID_ARGUMENT
2700 * The total length of input to psa_aead_update_ad() so far is
2701 * less than the additional data length that was previously
2702 * specified with psa_aead_set_lengths().
2703 * \retval #PSA_ERROR_INVALID_ARGUMENT
2704 * The total length of input to psa_aead_update() so far is
2705 * less than the plaintext length that was previously
2706 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002707 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2708 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2709 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002710 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002711 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002712 * \retval #PSA_ERROR_BAD_STATE
2713 * The library has not been previously initialized by psa_crypto_init().
2714 * It is implementation-dependent whether a failure to initialize
2715 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002716 */
2717psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002718 uint8_t *plaintext,
2719 size_t plaintext_size,
2720 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002721 const uint8_t *tag,
2722 size_t tag_length);
2723
2724/** Abort an AEAD operation.
2725 *
2726 * Aborting an operation frees all associated resources except for the
2727 * \p operation structure itself. Once aborted, the operation object
2728 * can be reused for another operation by calling
2729 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2730 *
2731 * You may call this function any time after the operation object has
2732 * been initialized by any of the following methods:
2733 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2734 * whether it succeeds or not.
2735 * - Initializing the \c struct to all-bits-zero.
2736 * - Initializing the \c struct to logical zeros, e.g.
2737 * `psa_aead_operation_t operation = {0}`.
2738 *
2739 * In particular, calling psa_aead_abort() after the operation has been
2740 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2741 * is safe and has no effect.
2742 *
2743 * \param[in,out] operation Initialized AEAD operation.
2744 *
2745 * \retval #PSA_SUCCESS
2746 * \retval #PSA_ERROR_BAD_STATE
2747 * \p operation is not an active AEAD operation.
2748 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2749 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002750 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002751 * \retval #PSA_ERROR_BAD_STATE
2752 * The library has not been previously initialized by psa_crypto_init().
2753 * It is implementation-dependent whether a failure to initialize
2754 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002755 */
2756psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2757
Gilles Peskine3b555712018-03-03 21:27:57 +01002758/**@}*/
2759
Gilles Peskine20035e32018-02-03 22:44:14 +01002760/** \defgroup asymmetric Asymmetric cryptography
2761 * @{
2762 */
2763
2764/**
2765 * \brief Sign a hash or short message with a private key.
2766 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002767 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002768 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002769 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2770 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2771 * to determine the hash algorithm to use.
2772 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002773 * \param handle Handle to the key to use for the operation.
2774 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002775 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002776 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002777 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002778 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002779 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002780 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002781 * \param[out] signature_length On success, the number of bytes
2782 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002783 *
Gilles Peskine28538492018-07-11 17:34:00 +02002784 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002785 * \retval #PSA_ERROR_INVALID_HANDLE
2786 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002787 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002788 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002789 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002790 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002791 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002792 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002793 * \retval #PSA_ERROR_NOT_SUPPORTED
2794 * \retval #PSA_ERROR_INVALID_ARGUMENT
2795 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2796 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2797 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002798 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002799 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002800 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002801 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002802 * The library has not been previously initialized by psa_crypto_init().
2803 * It is implementation-dependent whether a failure to initialize
2804 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002805 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002806psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002807 psa_algorithm_t alg,
2808 const uint8_t *hash,
2809 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002810 uint8_t *signature,
2811 size_t signature_size,
2812 size_t *signature_length);
2813
2814/**
2815 * \brief Verify the signature a hash or short message using a public key.
2816 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002817 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002818 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002819 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2820 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2821 * to determine the hash algorithm to use.
2822 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002823 * \param handle Handle to the key to use for the operation.
2824 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002825 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002826 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002827 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002828 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002829 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002830 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002831 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002832 *
Gilles Peskine28538492018-07-11 17:34:00 +02002833 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002834 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002835 * \retval #PSA_ERROR_INVALID_HANDLE
2836 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002837 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002838 * The calculation was perfomed successfully, but the passed
2839 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002840 * \retval #PSA_ERROR_NOT_SUPPORTED
2841 * \retval #PSA_ERROR_INVALID_ARGUMENT
2842 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2843 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2844 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002845 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002846 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002847 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002848 * The library has not been previously initialized by psa_crypto_init().
2849 * It is implementation-dependent whether a failure to initialize
2850 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002851 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002852psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002853 psa_algorithm_t alg,
2854 const uint8_t *hash,
2855 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002856 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002857 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002858
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002859/**
2860 * \brief Encrypt a short message with a public key.
2861 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002862 * \param handle Handle to the key to use for the operation.
2863 * It must be a public key or an asymmetric
2864 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002865 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002866 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002867 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002868 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002869 * \param[in] salt A salt or label, if supported by the
2870 * encryption algorithm.
2871 * If the algorithm does not support a
2872 * salt, pass \c NULL.
2873 * If the algorithm supports an optional
2874 * salt and you do not want to pass a salt,
2875 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002876 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002877 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2878 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002879 * \param salt_length Size of the \p salt buffer in bytes.
2880 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002881 * \param[out] output Buffer where the encrypted message is to
2882 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002883 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002884 * \param[out] output_length On success, the number of bytes
2885 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002886 *
Gilles Peskine28538492018-07-11 17:34:00 +02002887 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002888 * \retval #PSA_ERROR_INVALID_HANDLE
2889 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002890 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002891 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002892 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002893 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002894 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002895 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002896 * \retval #PSA_ERROR_NOT_SUPPORTED
2897 * \retval #PSA_ERROR_INVALID_ARGUMENT
2898 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2899 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2900 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002901 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002902 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002903 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002904 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002905 * The library has not been previously initialized by psa_crypto_init().
2906 * It is implementation-dependent whether a failure to initialize
2907 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002908 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002909psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002910 psa_algorithm_t alg,
2911 const uint8_t *input,
2912 size_t input_length,
2913 const uint8_t *salt,
2914 size_t salt_length,
2915 uint8_t *output,
2916 size_t output_size,
2917 size_t *output_length);
2918
2919/**
2920 * \brief Decrypt a short message with a private key.
2921 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002922 * \param handle Handle to the key to use for the operation.
2923 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002924 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002925 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002926 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002927 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002928 * \param[in] salt A salt or label, if supported by the
2929 * encryption algorithm.
2930 * If the algorithm does not support a
2931 * salt, pass \c NULL.
2932 * If the algorithm supports an optional
2933 * salt and you do not want to pass a salt,
2934 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002935 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002936 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2937 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002938 * \param salt_length Size of the \p salt buffer in bytes.
2939 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002940 * \param[out] output Buffer where the decrypted message is to
2941 * be written.
2942 * \param output_size Size of the \c output buffer in bytes.
2943 * \param[out] output_length On success, the number of bytes
2944 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002945 *
Gilles Peskine28538492018-07-11 17:34:00 +02002946 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002947 * \retval #PSA_ERROR_INVALID_HANDLE
2948 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002949 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002950 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002951 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002952 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002953 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002954 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002955 * \retval #PSA_ERROR_NOT_SUPPORTED
2956 * \retval #PSA_ERROR_INVALID_ARGUMENT
2957 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2958 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2959 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002960 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002961 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002962 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2963 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002964 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002965 * The library has not been previously initialized by psa_crypto_init().
2966 * It is implementation-dependent whether a failure to initialize
2967 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002968 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002969psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002970 psa_algorithm_t alg,
2971 const uint8_t *input,
2972 size_t input_length,
2973 const uint8_t *salt,
2974 size_t salt_length,
2975 uint8_t *output,
2976 size_t output_size,
2977 size_t *output_length);
2978
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002979/**@}*/
2980
Gilles Peskine35675b62019-05-16 17:26:11 +02002981/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002982 * @{
2983 */
2984
Gilles Peskine35675b62019-05-16 17:26:11 +02002985/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002986 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002987 * Before calling any function on a key derivation operation object, the
2988 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002989 * - Set the structure to all-bits-zero, for example:
2990 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002991 * psa_key_derivation_operation_t operation;
2992 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002993 * \endcode
2994 * - Initialize the structure to logical zero values, for example:
2995 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002996 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002997 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002998 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002999 * for example:
3000 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003001 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003002 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003003 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003004 * to the structure, for example:
3005 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003006 * psa_key_derivation_operation_t operation;
3007 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003008 * \endcode
3009 *
3010 * This is an implementation-defined \c struct. Applications should not
3011 * make any assumptions about the content of this structure except
3012 * as directed by the documentation of a specific implementation.
3013 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003014typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003015
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003016/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003017 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003018 * This macro returns a suitable initializer for a key derivation operation
3019 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003020 */
3021#ifdef __DOXYGEN_ONLY__
3022/* This is an example definition for documentation purposes.
3023 * Implementations should define a suitable value in `crypto_struct.h`.
3024 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003025#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003026#endif
3027
Gilles Peskine35675b62019-05-16 17:26:11 +02003028/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003029 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003030static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003031
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003032/** Set up a key derivation operation.
3033 *
3034 * A key derivation algorithm takes some inputs and uses them to generate
3035 * a byte stream in a deterministic way.
3036 * This byte stream can be used to produce keys and other
3037 * cryptographic material.
3038 *
3039 * To derive a key:
3040 * - Start with an initialized object of type #psa_key_derivation_operation_t.
3041 * - Call psa_key_derivation_setup() to select the algorithm.
3042 * - Provide the inputs for the key derivation by calling
3043 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3044 * as appropriate. Which inputs are needed, in what order, and whether
3045 * they may be keys and if so of what type depends on the algorithm.
3046 * - Optionally set the operation's maximum capacity with
3047 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3048 * of or after providing inputs. For some algorithms, this step is mandatory
3049 * because the output depends on the maximum capacity.
3050 * - To derive a key, call psa_key_derivation_output_key().
3051 * To derive a byte string for a different purpose, call
3052 * - psa_key_derivation_output_bytes().
3053 * Successive calls to these functions use successive output bytes
3054 * calculated by the key derivation algorithm.
3055 * - Clean up the key derivation operation object with
3056 * psa_key_derivation_abort().
3057 *
3058 * \param[in,out] operation The key derivation operation object
3059 * to set up. It must
3060 * have been initialized but not set up yet.
3061 * \param alg The key derivation algorithm to compute
3062 * (\c PSA_ALG_XXX value such that
3063 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3064 *
3065 * \retval #PSA_SUCCESS
3066 * Success.
3067 * \retval #PSA_ERROR_INVALID_ARGUMENT
3068 * \c alg is not a key derivation algorithm.
3069 * \retval #PSA_ERROR_NOT_SUPPORTED
3070 * \c alg is not supported or is not a key derivation algorithm.
3071 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3072 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3073 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003074 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003075 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01003076 * The operation state is either not initialized or has already been setup.
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003077 * \retval #PSA_ERROR_BAD_STATE
3078 * The library has not been previously initialized by psa_crypto_init().
3079 * It is implementation-dependent whether a failure to initialize
3080 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003081 */
3082psa_status_t psa_key_derivation_setup(
3083 psa_key_derivation_operation_t *operation,
3084 psa_algorithm_t alg);
3085
Gilles Peskine35675b62019-05-16 17:26:11 +02003086/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003087 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003088 * The capacity of a key derivation is the maximum number of bytes that it can
3089 * return. When you get *N* bytes of output from a key derivation operation,
3090 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003091 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003092 * \param[in] operation The operation to query.
3093 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003094 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003095 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003096 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003097 * \retval #PSA_ERROR_BAD_STATE
3098 * The operation state is not valid.
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003099 * \retval #PSA_ERROR_HARDWARE_FAILURE
3100 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003101 * \retval #PSA_ERROR_BAD_STATE
3102 * The library has not been previously initialized by psa_crypto_init().
3103 * It is implementation-dependent whether a failure to initialize
3104 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003105 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003106psa_status_t psa_key_derivation_get_capacity(
3107 const psa_key_derivation_operation_t *operation,
3108 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003109
Gilles Peskine35675b62019-05-16 17:26:11 +02003110/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003111 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003112 * The capacity of a key derivation operation is the maximum number of bytes
3113 * that the key derivation operation can return from this point onwards.
3114 *
3115 * \param[in,out] operation The key derivation operation object to modify.
3116 * \param capacity The new capacity of the operation.
3117 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003118 * current capacity.
3119 *
3120 * \retval #PSA_SUCCESS
3121 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003122 * \p capacity is larger than the operation's current capacity.
3123 * In this case, the operation object remains valid and its capacity
3124 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003125 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003126 * The operation state is not valid.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003127 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003128 * \retval #PSA_ERROR_HARDWARE_FAILURE
3129 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003130 * \retval #PSA_ERROR_BAD_STATE
3131 * The library has not been previously initialized by psa_crypto_init().
3132 * It is implementation-dependent whether a failure to initialize
3133 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003134 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003135psa_status_t psa_key_derivation_set_capacity(
3136 psa_key_derivation_operation_t *operation,
3137 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003138
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003139/** Use the maximum possible capacity for a key derivation operation.
3140 *
3141 * Use this value as the capacity argument when setting up a key derivation
3142 * to indicate that the operation should have the maximum possible capacity.
3143 * The value of the maximum possible capacity depends on the key derivation
3144 * algorithm.
3145 */
3146#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3147
3148/** Provide an input for key derivation or key agreement.
3149 *
3150 * Which inputs are required and in what order depends on the algorithm.
3151 * Refer to the documentation of each key derivation or key agreement
3152 * algorithm for information.
3153 *
3154 * This function passes direct inputs. Some inputs must be passed as keys
3155 * using psa_key_derivation_input_key() instead of this function. Refer to
3156 * the documentation of individual step types for information.
3157 *
3158 * \param[in,out] operation The key derivation operation object to use.
3159 * It must have been set up with
3160 * psa_key_derivation_setup() and must not
3161 * have produced any output yet.
3162 * \param step Which step the input data is for.
3163 * \param[in] data Input data to use.
3164 * \param data_length Size of the \p data buffer in bytes.
3165 *
3166 * \retval #PSA_SUCCESS
3167 * Success.
3168 * \retval #PSA_ERROR_INVALID_ARGUMENT
3169 * \c step is not compatible with the operation's algorithm.
3170 * \retval #PSA_ERROR_INVALID_ARGUMENT
3171 * \c step does not allow direct inputs.
3172 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3173 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3174 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003175 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003176 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003177 * \retval #PSA_ERROR_BAD_STATE
3178 * The value of \p step is not valid given the state of \p operation.
3179 * \retval #PSA_ERROR_BAD_STATE
3180 * The library has not been previously initialized by psa_crypto_init().
3181 * It is implementation-dependent whether a failure to initialize
3182 * results in this error code.
3183 */
3184psa_status_t psa_key_derivation_input_bytes(
3185 psa_key_derivation_operation_t *operation,
3186 psa_key_derivation_step_t step,
3187 const uint8_t *data,
3188 size_t data_length);
3189
3190/** Provide an input for key derivation in the form of a key.
3191 *
3192 * Which inputs are required and in what order depends on the algorithm.
3193 * Refer to the documentation of each key derivation or key agreement
3194 * algorithm for information.
3195 *
3196 * This function passes key inputs. Some inputs must be passed as keys
3197 * of the appropriate type using this function, while others must be
3198 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3199 * the documentation of individual step types for information.
3200 *
3201 * \param[in,out] operation The key derivation operation object to use.
3202 * It must have been set up with
3203 * psa_key_derivation_setup() and must not
3204 * have produced any output yet.
3205 * \param step Which step the input data is for.
3206 * \param handle Handle to the key. It must have an
3207 * appropriate type for \p step and must
3208 * allow the usage #PSA_KEY_USAGE_DERIVE.
3209 *
3210 * \retval #PSA_SUCCESS
3211 * Success.
3212 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003213 * \retval #PSA_ERROR_NOT_PERMITTED
3214 * \retval #PSA_ERROR_INVALID_ARGUMENT
3215 * \c step is not compatible with the operation's algorithm.
3216 * \retval #PSA_ERROR_INVALID_ARGUMENT
3217 * \c step does not allow key inputs.
3218 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3219 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3220 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003221 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003222 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003223 * \retval #PSA_ERROR_BAD_STATE
3224 * The value of \p step is not valid given the state of \p operation.
3225 * \retval #PSA_ERROR_BAD_STATE
3226 * The library has not been previously initialized by psa_crypto_init().
3227 * It is implementation-dependent whether a failure to initialize
3228 * results in this error code.
3229 */
3230psa_status_t psa_key_derivation_input_key(
3231 psa_key_derivation_operation_t *operation,
3232 psa_key_derivation_step_t step,
3233 psa_key_handle_t handle);
3234
3235/** Perform a key agreement and use the shared secret as input to a key
3236 * derivation.
3237 *
3238 * A key agreement algorithm takes two inputs: a private key \p private_key
3239 * a public key \p peer_key.
3240 * The result of this function is passed as input to a key derivation.
3241 * The output of this key derivation can be extracted by reading from the
3242 * resulting operation to produce keys and other cryptographic material.
3243 *
3244 * \param[in,out] operation The key derivation operation object to use.
3245 * It must have been set up with
3246 * psa_key_derivation_setup() with a
3247 * key agreement and derivation algorithm
3248 * \c alg (\c PSA_ALG_XXX value such that
3249 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3250 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3251 * is false).
3252 * The operation must be ready for an
3253 * input of the type given by \p step.
3254 * \param step Which step the input data is for.
3255 * \param private_key Handle to the private key to use.
3256 * \param[in] peer_key Public key of the peer. The peer key must be in the
3257 * same format that psa_import_key() accepts for the
3258 * public key type corresponding to the type of
3259 * private_key. That is, this function performs the
3260 * equivalent of
3261 * #psa_import_key(...,
3262 * `peer_key`, `peer_key_length`) where
3263 * with key attributes indicating the public key
3264 * type corresponding to the type of `private_key`.
3265 * For example, for EC keys, this means that peer_key
3266 * is interpreted as a point on the curve that the
3267 * private key is on. The standard formats for public
3268 * keys are documented in the documentation of
3269 * psa_export_public_key().
3270 * \param peer_key_length Size of \p peer_key in bytes.
3271 *
3272 * \retval #PSA_SUCCESS
3273 * Success.
3274 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003275 * \retval #PSA_ERROR_NOT_PERMITTED
3276 * \retval #PSA_ERROR_INVALID_ARGUMENT
3277 * \c private_key is not compatible with \c alg,
3278 * or \p peer_key is not valid for \c alg or not compatible with
3279 * \c private_key.
3280 * \retval #PSA_ERROR_NOT_SUPPORTED
3281 * \c alg is not supported or is not a key derivation algorithm.
3282 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3283 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3284 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003285 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003286 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003287 * \retval #PSA_ERROR_BAD_STATE
3288 * The library has not been previously initialized by psa_crypto_init().
3289 * It is implementation-dependent whether a failure to initialize
3290 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003291 */
3292psa_status_t psa_key_derivation_key_agreement(
3293 psa_key_derivation_operation_t *operation,
3294 psa_key_derivation_step_t step,
3295 psa_key_handle_t private_key,
3296 const uint8_t *peer_key,
3297 size_t peer_key_length);
3298
Gilles Peskine35675b62019-05-16 17:26:11 +02003299/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003300 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003301 * This function calculates output bytes from a key derivation algorithm and
3302 * return those bytes.
3303 * If you view the key derivation's output as a stream of bytes, this
3304 * function destructively reads the requested number of bytes from the
3305 * stream.
3306 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003307 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003308 * \param[in,out] operation The key derivation operation object to read from.
3309 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003310 * \param output_length Number of bytes to output.
3311 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003312 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003313 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003314 * The operation's capacity was less than
3315 * \p output_length bytes. Note that in this case,
3316 * no output is written to the output buffer.
3317 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003318 * subsequent calls to this function will not
3319 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003320 * \retval #PSA_ERROR_BAD_STATE
3321 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3322 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3323 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003324 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003325 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003326 * \retval #PSA_ERROR_BAD_STATE
3327 * The library has not been previously initialized by psa_crypto_init().
3328 * It is implementation-dependent whether a failure to initialize
3329 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003330 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003331psa_status_t psa_key_derivation_output_bytes(
3332 psa_key_derivation_operation_t *operation,
3333 uint8_t *output,
3334 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003335
Gilles Peskine35675b62019-05-16 17:26:11 +02003336/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003337 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003338 * This function calculates output bytes from a key derivation algorithm
3339 * and uses those bytes to generate a key deterministically.
3340 * If you view the key derivation's output as a stream of bytes, this
3341 * function destructively reads as many bytes as required from the
3342 * stream.
3343 * The operation's capacity decreases by the number of bytes read.
3344 *
3345 * How much output is produced and consumed from the operation, and how
3346 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003347 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003348 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003349 * of a given size, this function is functionally equivalent to
3350 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003351 * and passing the resulting output to #psa_import_key.
3352 * However, this function has a security benefit:
3353 * if the implementation provides an isolation boundary then
3354 * the key material is not exposed outside the isolation boundary.
3355 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003356 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003357 * The following key types defined in this specification follow this scheme:
3358 *
3359 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003360 * - #PSA_KEY_TYPE_ARC4;
3361 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003362 * - #PSA_KEY_TYPE_DERIVE;
3363 * - #PSA_KEY_TYPE_HMAC.
3364 *
3365 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003366 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003367 * Montgomery curve), this function always draws a byte string whose
3368 * length is determined by the curve, and sets the mandatory bits
3369 * accordingly. That is:
3370 *
3371 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3372 * and process it as specified in RFC 7748 &sect;5.
3373 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3374 * and process it as specified in RFC 7748 &sect;5.
3375 *
3376 * - For key types for which the key is represented by a single sequence of
3377 * \p bits bits with constraints as to which bit sequences are acceptable,
3378 * this function draws a byte string of length (\p bits / 8) bytes rounded
3379 * up to the nearest whole number of bytes. If the resulting byte string
3380 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3381 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003382 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003383 * for the output produced by psa_export_key().
3384 * The following key types defined in this specification follow this scheme:
3385 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003386 * - #PSA_KEY_TYPE_DES.
3387 * Force-set the parity bits, but discard forbidden weak keys.
3388 * For 2-key and 3-key triple-DES, the three keys are generated
3389 * successively (for example, for 3-key triple-DES,
3390 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3391 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003392 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003393 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003394 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003395 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003396 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003397 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003398 * Weierstrass curve).
3399 * For these key types, interpret the byte string as integer
3400 * in big-endian order. Discard it if it is not in the range
3401 * [0, *N* - 2] where *N* is the boundary of the private key domain
3402 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003403 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003404 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003405 * This method allows compliance to NIST standards, specifically
3406 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003407 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3408 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3409 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3410 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003411 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003412 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003413 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003414 * implementation-defined.
3415 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003416 * In all cases, the data that is read is discarded from the operation.
3417 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003418 *
Gilles Peskine20628592019-04-19 19:29:50 +02003419 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003420 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003421 * \param[out] handle On success, a handle to the newly created key.
3422 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003423 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003424 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003425 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003426 * If the key is persistent, the key material and the key's metadata
3427 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003428 * \retval #PSA_ERROR_ALREADY_EXISTS
3429 * This is an attempt to create a persistent key, and there is
3430 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003431 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003432 * There was not enough data to create the desired key.
3433 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003434 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003435 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003436 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003437 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003438 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003439 * \retval #PSA_ERROR_INVALID_ARGUMENT
3440 * The provided key attributes are not valid for the operation.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003441 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003442 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3443 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3444 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3445 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003446 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003447 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003448 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003449 * The library has not been previously initialized by psa_crypto_init().
3450 * It is implementation-dependent whether a failure to initialize
3451 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003452 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003453psa_status_t psa_key_derivation_output_key(
3454 const psa_key_attributes_t *attributes,
3455 psa_key_derivation_operation_t *operation,
3456 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003457
Gilles Peskine35675b62019-05-16 17:26:11 +02003458/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003459 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003460 * Once a key derivation operation has been aborted, its capacity is zero.
3461 * Aborting an operation frees all associated resources except for the
3462 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003463 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003464 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003465 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003466 * psa_key_derivation_operation_init() or a zero value. In particular,
3467 * it is valid to call psa_key_derivation_abort() twice, or to call
3468 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003469 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003470 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003471 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003472 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003473 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003474 * \retval #PSA_SUCCESS
3475 * \retval #PSA_ERROR_BAD_STATE
3476 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3477 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003478 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003479 * \retval #PSA_ERROR_BAD_STATE
3480 * The library has not been previously initialized by psa_crypto_init().
3481 * It is implementation-dependent whether a failure to initialize
3482 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003483 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003484psa_status_t psa_key_derivation_abort(
3485 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003486
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003487/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003488 *
3489 * \warning The raw result of a key agreement algorithm such as finite-field
3490 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3491 * not be used directly as key material. It should instead be passed as
3492 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003493 * a key derivation, use psa_key_derivation_key_agreement() and other
3494 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003495 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003496 * \param alg The key agreement algorithm to compute
3497 * (\c PSA_ALG_XXX value such that
3498 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3499 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003500 * \param private_key Handle to the private key to use.
3501 * \param[in] peer_key Public key of the peer. It must be
3502 * in the same format that psa_import_key()
3503 * accepts. The standard formats for public
3504 * keys are documented in the documentation
3505 * of psa_export_public_key().
3506 * \param peer_key_length Size of \p peer_key in bytes.
3507 * \param[out] output Buffer where the decrypted message is to
3508 * be written.
3509 * \param output_size Size of the \c output buffer in bytes.
3510 * \param[out] output_length On success, the number of bytes
3511 * that make up the returned output.
3512 *
3513 * \retval #PSA_SUCCESS
3514 * Success.
3515 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003516 * \retval #PSA_ERROR_NOT_PERMITTED
3517 * \retval #PSA_ERROR_INVALID_ARGUMENT
3518 * \p alg is not a key agreement algorithm
3519 * \retval #PSA_ERROR_INVALID_ARGUMENT
3520 * \p private_key is not compatible with \p alg,
3521 * or \p peer_key is not valid for \p alg or not compatible with
3522 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003523 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3524 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003525 * \retval #PSA_ERROR_NOT_SUPPORTED
3526 * \p alg is not a supported key agreement algorithm.
3527 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3528 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3529 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003530 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003531 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003532 * \retval #PSA_ERROR_BAD_STATE
3533 * The library has not been previously initialized by psa_crypto_init().
3534 * It is implementation-dependent whether a failure to initialize
3535 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003536 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003537psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3538 psa_key_handle_t private_key,
3539 const uint8_t *peer_key,
3540 size_t peer_key_length,
3541 uint8_t *output,
3542 size_t output_size,
3543 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003544
Gilles Peskineea0fb492018-07-12 17:17:20 +02003545/**@}*/
3546
Gilles Peskineedd76872018-07-20 17:42:05 +02003547/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003548 * @{
3549 */
3550
3551/**
3552 * \brief Generate random bytes.
3553 *
3554 * \warning This function **can** fail! Callers MUST check the return status
3555 * and MUST NOT use the content of the output buffer if the return
3556 * status is not #PSA_SUCCESS.
3557 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003558 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003559 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003560 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003561 * \param output_size Number of bytes to generate and output.
3562 *
Gilles Peskine28538492018-07-11 17:34:00 +02003563 * \retval #PSA_SUCCESS
3564 * \retval #PSA_ERROR_NOT_SUPPORTED
3565 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003566 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003567 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3568 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003569 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003570 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003571 * The library has not been previously initialized by psa_crypto_init().
3572 * It is implementation-dependent whether a failure to initialize
3573 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003574 */
3575psa_status_t psa_generate_random(uint8_t *output,
3576 size_t output_size);
3577
3578/**
3579 * \brief Generate a key or key pair.
3580 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003581 * The key is generated randomly.
3582 * Its location, policy, type and size are taken from \p attributes.
3583 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003584 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003585 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003586 * the public exponent is 65537.
3587 * The modulus is a product of two probabilistic primes
3588 * between 2^{n-1} and 2^n where n is the bit size specified in the
3589 * attributes.
3590 *
Gilles Peskine20628592019-04-19 19:29:50 +02003591 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003592 * \param[out] handle On success, a handle to the newly created key.
3593 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003594 *
Gilles Peskine28538492018-07-11 17:34:00 +02003595 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003596 * Success.
3597 * If the key is persistent, the key material and the key's metadata
3598 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003599 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003600 * This is an attempt to create a persistent key, and there is
3601 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003602 * \retval #PSA_ERROR_NOT_SUPPORTED
3603 * \retval #PSA_ERROR_INVALID_ARGUMENT
3604 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3605 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3606 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3607 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003608 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003609 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3610 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003611 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003612 * The library has not been previously initialized by psa_crypto_init().
3613 * It is implementation-dependent whether a failure to initialize
3614 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003615 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003616psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003617 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003618
3619/**@}*/
3620
Gilles Peskinee59236f2018-01-27 23:32:46 +01003621#ifdef __cplusplus
3622}
3623#endif
3624
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003625/* The file "crypto_sizes.h" contains definitions for size calculation
3626 * macros whose definitions are implementation-specific. */
3627#include "crypto_sizes.h"
3628
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003629/* The file "crypto_struct.h" contains definitions for
3630 * implementation-specific structs that are declared above. */
3631#include "crypto_struct.h"
3632
3633/* The file "crypto_extra.h" contains vendor-specific definitions. This
3634 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003635#include "crypto_extra.h"
3636
3637#endif /* PSA_CRYPTO_H */