blob: 05d76e1c1e39a5bc8b144376d388ccdfaf058bf2 [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. Shaw15731c12019-08-06 16:21:00 +0100834 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100835 * \retval #PSA_ERROR_BAD_STATE
836 * The library has not been previously initialized by psa_crypto_init().
837 * It is implementation-dependent whether a failure to initialize
838 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100839 */
840psa_status_t psa_hash_compute(psa_algorithm_t alg,
841 const uint8_t *input,
842 size_t input_length,
843 uint8_t *hash,
844 size_t hash_size,
845 size_t *hash_length);
846
847/** Calculate the hash (digest) of a message and compare it with a
848 * reference value.
849 *
850 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
851 * such that #PSA_ALG_IS_HASH(\p alg) is true).
852 * \param[in] input Buffer containing the message to hash.
853 * \param input_length Size of the \p input buffer in bytes.
854 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100855 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100856 *
857 * \retval #PSA_SUCCESS
858 * The expected hash is identical to the actual hash of the input.
859 * \retval #PSA_ERROR_INVALID_SIGNATURE
860 * The hash of the message was calculated successfully, but it
861 * differs from the expected hash.
862 * \retval #PSA_ERROR_NOT_SUPPORTED
863 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100864 * \retval #PSA_ERROR_INVALID_ARGUMENT
865 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100866 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
867 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
868 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200869 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100870 * \retval #PSA_ERROR_BAD_STATE
871 * The library has not been previously initialized by psa_crypto_init().
872 * It is implementation-dependent whether a failure to initialize
873 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100874 */
875psa_status_t psa_hash_compare(psa_algorithm_t alg,
876 const uint8_t *input,
877 size_t input_length,
878 const uint8_t *hash,
879 const size_t hash_length);
880
Gilles Peskine308b91d2018-02-08 09:47:44 +0100881/** The type of the state data structure for multipart hash operations.
882 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000883 * Before calling any function on a hash operation object, the application must
884 * initialize it by any of the following means:
885 * - Set the structure to all-bits-zero, for example:
886 * \code
887 * psa_hash_operation_t operation;
888 * memset(&operation, 0, sizeof(operation));
889 * \endcode
890 * - Initialize the structure to logical zero values, for example:
891 * \code
892 * psa_hash_operation_t operation = {0};
893 * \endcode
894 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
895 * for example:
896 * \code
897 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
898 * \endcode
899 * - Assign the result of the function psa_hash_operation_init()
900 * to the structure, for example:
901 * \code
902 * psa_hash_operation_t operation;
903 * operation = psa_hash_operation_init();
904 * \endcode
905 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100906 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100907 * make any assumptions about the content of this structure except
908 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100909typedef struct psa_hash_operation_s psa_hash_operation_t;
910
Jaeden Amero6a25b412019-01-04 11:47:44 +0000911/** \def PSA_HASH_OPERATION_INIT
912 *
913 * This macro returns a suitable initializer for a hash operation object
914 * of type #psa_hash_operation_t.
915 */
916#ifdef __DOXYGEN_ONLY__
917/* This is an example definition for documentation purposes.
918 * Implementations should define a suitable value in `crypto_struct.h`.
919 */
920#define PSA_HASH_OPERATION_INIT {0}
921#endif
922
923/** Return an initial value for a hash operation object.
924 */
925static psa_hash_operation_t psa_hash_operation_init(void);
926
Gilles Peskinef45adda2019-01-14 18:29:18 +0100927/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100928 *
929 * The sequence of operations to calculate a hash (message digest)
930 * is as follows:
931 * -# Allocate an operation object which will be passed to all the functions
932 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000933 * -# Initialize the operation object with one of the methods described in the
934 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200935 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100936 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100937 * of the message each time. The hash that is calculated is the hash
938 * of the concatenation of these messages in order.
939 * -# To calculate the hash, call psa_hash_finish().
940 * To compare the hash with an expected value, call psa_hash_verify().
941 *
942 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000943 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100944 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200945 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100946 * eventually terminate the operation. The following events terminate an
947 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100948 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100949 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100950 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000951 * \param[in,out] operation The operation object to set up. It must have
952 * been initialized as per the documentation for
953 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200954 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
955 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100956 *
Gilles Peskine28538492018-07-11 17:34:00 +0200957 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100958 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200959 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100960 * \p alg is not a supported hash algorithm.
961 * \retval #PSA_ERROR_INVALID_ARGUMENT
962 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100963 * \retval #PSA_ERROR_BAD_STATE
964 * The operation state is not valid (already set up and not
965 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200966 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
967 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
968 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200969 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100970 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawe970d652019-08-08 14:40:04 +0100971 * \p operation is either not initialized or is in use
972 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100973 * The library has not been previously initialized by psa_crypto_init().
974 * It is implementation-dependent whether a failure to initialize
975 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100976 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200977psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100978 psa_algorithm_t alg);
979
Gilles Peskine308b91d2018-02-08 09:47:44 +0100980/** Add a message fragment to a multipart hash operation.
981 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200982 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100983 *
984 * If this function returns an error status, the operation becomes inactive.
985 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200986 * \param[in,out] operation Active hash operation.
987 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200988 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100989 *
Gilles Peskine28538492018-07-11 17:34:00 +0200990 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100991 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200992 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100993 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200994 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
995 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
996 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200997 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100998 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +0100999 * The operation state is not valid.
1000 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001001 * The library has not been previously initialized by psa_crypto_init().
1002 * It is implementation-dependent whether a failure to initialize
1003 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001004 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001005psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1006 const uint8_t *input,
1007 size_t input_length);
1008
Gilles Peskine308b91d2018-02-08 09:47:44 +01001009/** Finish the calculation of the hash of a message.
1010 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001011 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001012 * This function calculates the hash of the message formed by concatenating
1013 * the inputs passed to preceding calls to psa_hash_update().
1014 *
1015 * When this function returns, the operation becomes inactive.
1016 *
1017 * \warning Applications should not call this function if they expect
1018 * a specific value for the hash. Call psa_hash_verify() instead.
1019 * Beware that comparing integrity or authenticity data such as
1020 * hash values with a function such as \c memcmp is risky
1021 * because the time taken by the comparison may leak information
1022 * about the hashed data which could allow an attacker to guess
1023 * a valid hash and thereby bypass security controls.
1024 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001025 * \param[in,out] operation Active hash operation.
1026 * \param[out] hash Buffer where the hash is to be written.
1027 * \param hash_size Size of the \p hash buffer in bytes.
1028 * \param[out] hash_length On success, the number of bytes
1029 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001030 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001031 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001032 *
Gilles Peskine28538492018-07-11 17:34:00 +02001033 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001034 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001035 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001036 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001037 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001038 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001039 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001040 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001041 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1042 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1043 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001044 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001045 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001046 * The operation state is not valid.
1047 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001048 * The library has not been previously initialized by psa_crypto_init().
1049 * It is implementation-dependent whether a failure to initialize
1050 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001051 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001052psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1053 uint8_t *hash,
1054 size_t hash_size,
1055 size_t *hash_length);
1056
Gilles Peskine308b91d2018-02-08 09:47:44 +01001057/** Finish the calculation of the hash of a message and compare it with
1058 * an expected value.
1059 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001060 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001061 * This function calculates the hash of the message formed by concatenating
1062 * the inputs passed to preceding calls to psa_hash_update(). It then
1063 * compares the calculated hash with the expected hash passed as a
1064 * parameter to this function.
1065 *
1066 * When this function returns, the operation becomes inactive.
1067 *
Gilles Peskine19067982018-03-20 17:54:53 +01001068 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001069 * comparison between the actual hash and the expected hash is performed
1070 * in constant time.
1071 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001072 * \param[in,out] operation Active hash operation.
1073 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001074 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001075 *
Gilles Peskine28538492018-07-11 17:34:00 +02001076 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001077 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001078 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001079 * The hash of the message was calculated successfully, but it
1080 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001081 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001082 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001083 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1084 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1085 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001086 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001087 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001088 * The operation state is not valid.
1089 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001090 * The library has not been previously initialized by psa_crypto_init().
1091 * It is implementation-dependent whether a failure to initialize
1092 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001093 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001094psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1095 const uint8_t *hash,
1096 size_t hash_length);
1097
Gilles Peskine308b91d2018-02-08 09:47:44 +01001098/** Abort a hash operation.
1099 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001100 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001101 * \p operation structure itself. Once aborted, the operation object
1102 * can be reused for another operation by calling
1103 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001104 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001105 * You may call this function any time after the operation object has
1106 * been initialized by any of the following methods:
1107 * - A call to psa_hash_setup(), whether it succeeds or not.
1108 * - Initializing the \c struct to all-bits-zero.
1109 * - Initializing the \c struct to logical zeros, e.g.
1110 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001111 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001112 * In particular, calling psa_hash_abort() after the operation has been
1113 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1114 * psa_hash_verify() is safe and has no effect.
1115 *
1116 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001117 *
Gilles Peskine28538492018-07-11 17:34:00 +02001118 * \retval #PSA_SUCCESS
1119 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001120 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001121 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1122 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001123 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001124 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001125 * The operation state is not valid.
1126 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001127 * The library has not been previously initialized by psa_crypto_init().
1128 * It is implementation-dependent whether a failure to initialize
1129 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001130 */
1131psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001132
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001133/** Clone a hash operation.
1134 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001135 * This function copies the state of an ongoing hash operation to
1136 * a new operation object. In other words, this function is equivalent
1137 * to calling psa_hash_setup() on \p target_operation with the same
1138 * algorithm that \p source_operation was set up for, then
1139 * psa_hash_update() on \p target_operation with the same input that
1140 * that was passed to \p source_operation. After this function returns, the
1141 * two objects are independent, i.e. subsequent calls involving one of
1142 * the objects do not affect the other object.
1143 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001144 * \param[in] source_operation The active hash operation to clone.
1145 * \param[in,out] target_operation The operation object to set up.
1146 * It must be initialized but not active.
1147 *
1148 * \retval #PSA_SUCCESS
1149 * \retval #PSA_ERROR_BAD_STATE
1150 * \p source_operation is not an active hash operation.
1151 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001152 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001153 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1154 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001155 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001156 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001157 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001158 * The operation state is either not initialized or has already been setup.
1159 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001160 * The library has not been previously initialized by psa_crypto_init().
1161 * It is implementation-dependent whether a failure to initialize
1162 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001163 */
1164psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1165 psa_hash_operation_t *target_operation);
1166
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001167/**@}*/
1168
Gilles Peskine8c9def32018-02-08 10:02:12 +01001169/** \defgroup MAC Message authentication codes
1170 * @{
1171 */
1172
Gilles Peskine69647a42019-01-14 20:18:12 +01001173/** Calculate the MAC (message authentication code) of a message.
1174 *
1175 * \note To verify the MAC of a message against an
1176 * expected value, use psa_mac_verify() instead.
1177 * Beware that comparing integrity or authenticity data such as
1178 * MAC values with a function such as \c memcmp is risky
1179 * because the time taken by the comparison may leak information
1180 * about the MAC value which could allow an attacker to guess
1181 * a valid MAC and thereby bypass security controls.
1182 *
1183 * \param handle Handle to the key to use for the operation.
1184 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001185 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001186 * \param[in] input Buffer containing the input message.
1187 * \param input_length Size of the \p input buffer in bytes.
1188 * \param[out] mac Buffer where the MAC value is to be written.
1189 * \param mac_size Size of the \p mac buffer in bytes.
1190 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001191 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001192 *
1193 * \retval #PSA_SUCCESS
1194 * Success.
1195 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001196 * \retval #PSA_ERROR_NOT_PERMITTED
1197 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001198 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001199 * \retval #PSA_ERROR_NOT_SUPPORTED
1200 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001201 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1202 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001203 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1204 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1205 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001206 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001207 * \retval #PSA_ERROR_STORAGE_FAILURE
1208 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001209 * \retval #PSA_ERROR_BAD_STATE
1210 * The library has not been previously initialized by psa_crypto_init().
1211 * It is implementation-dependent whether a failure to initialize
1212 * results in this error code.
1213 */
1214psa_status_t psa_mac_compute(psa_key_handle_t handle,
1215 psa_algorithm_t alg,
1216 const uint8_t *input,
1217 size_t input_length,
1218 uint8_t *mac,
1219 size_t mac_size,
1220 size_t *mac_length);
1221
1222/** Calculate the MAC of a message and compare it with a reference value.
1223 *
1224 * \param handle Handle to the key to use for the operation.
1225 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001226 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001227 * \param[in] input Buffer containing the input message.
1228 * \param input_length Size of the \p input buffer in bytes.
1229 * \param[out] mac Buffer containing the expected MAC value.
1230 * \param mac_length Size of the \p mac buffer in bytes.
1231 *
1232 * \retval #PSA_SUCCESS
1233 * The expected MAC is identical to the actual MAC of the input.
1234 * \retval #PSA_ERROR_INVALID_SIGNATURE
1235 * The MAC of the message was calculated successfully, but it
1236 * differs from the expected value.
1237 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001238 * \retval #PSA_ERROR_NOT_PERMITTED
1239 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001240 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001241 * \retval #PSA_ERROR_NOT_SUPPORTED
1242 * \p alg is not supported or is not a MAC algorithm.
1243 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1244 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1245 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001246 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001247 * \retval #PSA_ERROR_STORAGE_FAILURE
1248 * The key could not be retrieved from storage.
1249 * \retval #PSA_ERROR_BAD_STATE
1250 * The library has not been previously initialized by psa_crypto_init().
1251 * It is implementation-dependent whether a failure to initialize
1252 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001253 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001254psa_status_t psa_mac_verify(psa_key_handle_t handle,
1255 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001256 const uint8_t *input,
1257 size_t input_length,
1258 const uint8_t *mac,
1259 const size_t mac_length);
1260
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001261/** The type of the state data structure for multipart MAC operations.
1262 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001263 * Before calling any function on a MAC operation object, the application must
1264 * initialize it by any of the following means:
1265 * - Set the structure to all-bits-zero, for example:
1266 * \code
1267 * psa_mac_operation_t operation;
1268 * memset(&operation, 0, sizeof(operation));
1269 * \endcode
1270 * - Initialize the structure to logical zero values, for example:
1271 * \code
1272 * psa_mac_operation_t operation = {0};
1273 * \endcode
1274 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1275 * for example:
1276 * \code
1277 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1278 * \endcode
1279 * - Assign the result of the function psa_mac_operation_init()
1280 * to the structure, for example:
1281 * \code
1282 * psa_mac_operation_t operation;
1283 * operation = psa_mac_operation_init();
1284 * \endcode
1285 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001286 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001287 * make any assumptions about the content of this structure except
1288 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001289typedef struct psa_mac_operation_s psa_mac_operation_t;
1290
Jaeden Amero769ce272019-01-04 11:48:03 +00001291/** \def PSA_MAC_OPERATION_INIT
1292 *
1293 * This macro returns a suitable initializer for a MAC operation object of type
1294 * #psa_mac_operation_t.
1295 */
1296#ifdef __DOXYGEN_ONLY__
1297/* This is an example definition for documentation purposes.
1298 * Implementations should define a suitable value in `crypto_struct.h`.
1299 */
1300#define PSA_MAC_OPERATION_INIT {0}
1301#endif
1302
1303/** Return an initial value for a MAC operation object.
1304 */
1305static psa_mac_operation_t psa_mac_operation_init(void);
1306
Gilles Peskinef45adda2019-01-14 18:29:18 +01001307/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001308 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001309 * This function sets up the calculation of the MAC
1310 * (message authentication code) of a byte string.
1311 * To verify the MAC of a message against an
1312 * expected value, use psa_mac_verify_setup() instead.
1313 *
1314 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001315 * -# Allocate an operation object which will be passed to all the functions
1316 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001317 * -# Initialize the operation object with one of the methods described in the
1318 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001319 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001320 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1321 * of the message each time. The MAC that is calculated is the MAC
1322 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001323 * -# At the end of the message, call psa_mac_sign_finish() to finish
1324 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001325 *
1326 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001327 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001328 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001329 * After a successful call to psa_mac_sign_setup(), the application must
1330 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001331 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001332 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001333 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001334 * \param[in,out] operation The operation object to set up. It must have
1335 * been initialized as per the documentation for
1336 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001337 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001338 * It must remain valid until the operation
1339 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001340 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001341 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001342 *
Gilles Peskine28538492018-07-11 17:34:00 +02001343 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001344 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001345 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001346 * \retval #PSA_ERROR_NOT_PERMITTED
1347 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001348 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001349 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001350 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001351 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1352 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1353 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001354 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001355 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001356 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001357 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001358 * The operation state is not valid (already set up and not
1359 * subsequently completed).
1360 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001361 * The library has not been previously initialized by psa_crypto_init().
1362 * It is implementation-dependent whether a failure to initialize
1363 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001364 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001365psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001366 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001367 psa_algorithm_t alg);
1368
Gilles Peskinef45adda2019-01-14 18:29:18 +01001369/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001370 *
1371 * This function sets up the verification of the MAC
1372 * (message authentication code) of a byte string against an expected value.
1373 *
1374 * The sequence of operations to verify a MAC is as follows:
1375 * -# Allocate an operation object which will be passed to all the functions
1376 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001377 * -# Initialize the operation object with one of the methods described in the
1378 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001379 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001380 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1381 * of the message each time. The MAC that is calculated is the MAC
1382 * of the concatenation of these messages in order.
1383 * -# At the end of the message, call psa_mac_verify_finish() to finish
1384 * calculating the actual MAC of the message and verify it against
1385 * the expected value.
1386 *
1387 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001388 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001389 *
1390 * After a successful call to psa_mac_verify_setup(), the application must
1391 * eventually terminate the operation through one of the following methods:
1392 * - A failed call to psa_mac_update().
1393 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1394 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001395 * \param[in,out] operation The operation object to set up. It must have
1396 * been initialized as per the documentation for
1397 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001398 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001399 * It must remain valid until the operation
1400 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001401 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1402 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001403 *
Gilles Peskine28538492018-07-11 17:34:00 +02001404 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001405 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001406 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001407 * \retval #PSA_ERROR_NOT_PERMITTED
1408 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001409 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001410 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001411 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001412 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1413 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1414 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001415 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001416 * \retval #PSA_ERROR_STORAGE_FAILURE
1417 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001418 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001419 * The operation state is not valid (already set up and not
1420 * subsequently completed).
1421 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001422 * The library has not been previously initialized by psa_crypto_init().
1423 * It is implementation-dependent whether a failure to initialize
1424 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001425 */
1426psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001427 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001428 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001429
Gilles Peskinedcd14942018-07-12 00:30:52 +02001430/** Add a message fragment to a multipart MAC operation.
1431 *
1432 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1433 * before calling this function.
1434 *
1435 * If this function returns an error status, the operation becomes inactive.
1436 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001437 * \param[in,out] operation Active MAC operation.
1438 * \param[in] input Buffer containing the message fragment to add to
1439 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001440 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001441 *
1442 * \retval #PSA_SUCCESS
1443 * Success.
1444 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001445 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001446 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1447 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1448 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001449 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001450 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001451 * \retval #PSA_ERROR_BAD_STATE
1452 * The library has not been previously initialized by psa_crypto_init().
1453 * It is implementation-dependent whether a failure to initialize
1454 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001455 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001456psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1457 const uint8_t *input,
1458 size_t input_length);
1459
Gilles Peskinedcd14942018-07-12 00:30:52 +02001460/** Finish the calculation of the MAC of a message.
1461 *
1462 * The application must call psa_mac_sign_setup() before calling this function.
1463 * This function calculates the MAC of the message formed by concatenating
1464 * the inputs passed to preceding calls to psa_mac_update().
1465 *
1466 * When this function returns, the operation becomes inactive.
1467 *
1468 * \warning Applications should not call this function if they expect
1469 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1470 * Beware that comparing integrity or authenticity data such as
1471 * MAC values with a function such as \c memcmp is risky
1472 * because the time taken by the comparison may leak information
1473 * about the MAC value which could allow an attacker to guess
1474 * a valid MAC and thereby bypass security controls.
1475 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001476 * \param[in,out] operation Active MAC operation.
1477 * \param[out] mac Buffer where the MAC value is to be written.
1478 * \param mac_size Size of the \p mac buffer in bytes.
1479 * \param[out] mac_length On success, the number of bytes
1480 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001481 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001482 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001483 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001484 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001485 *
1486 * \retval #PSA_SUCCESS
1487 * Success.
1488 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001489 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001490 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001491 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001492 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1493 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1494 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1495 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001496 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001497 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001498 * \retval #PSA_ERROR_BAD_STATE
1499 * The library has not been previously initialized by psa_crypto_init().
1500 * It is implementation-dependent whether a failure to initialize
1501 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001502 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001503psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1504 uint8_t *mac,
1505 size_t mac_size,
1506 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001507
Gilles Peskinedcd14942018-07-12 00:30:52 +02001508/** Finish the calculation of the MAC of a message and compare it with
1509 * an expected value.
1510 *
1511 * The application must call psa_mac_verify_setup() before calling this function.
1512 * This function calculates the MAC of the message formed by concatenating
1513 * the inputs passed to preceding calls to psa_mac_update(). It then
1514 * compares the calculated MAC with the expected MAC passed as a
1515 * parameter to this function.
1516 *
1517 * When this function returns, the operation becomes inactive.
1518 *
1519 * \note Implementations shall make the best effort to ensure that the
1520 * comparison between the actual MAC and the expected MAC is performed
1521 * in constant time.
1522 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001523 * \param[in,out] operation Active MAC operation.
1524 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001525 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001526 *
1527 * \retval #PSA_SUCCESS
1528 * The expected MAC is identical to the actual MAC of the message.
1529 * \retval #PSA_ERROR_INVALID_SIGNATURE
1530 * The MAC of the message was calculated successfully, but it
1531 * differs from the expected MAC.
1532 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001533 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001534 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1535 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1536 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001537 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001538 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001539 * \retval #PSA_ERROR_BAD_STATE
1540 * The library has not been previously initialized by psa_crypto_init().
1541 * It is implementation-dependent whether a failure to initialize
1542 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001543 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001544psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1545 const uint8_t *mac,
1546 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001547
Gilles Peskinedcd14942018-07-12 00:30:52 +02001548/** Abort a MAC operation.
1549 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001550 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001551 * \p operation structure itself. Once aborted, the operation object
1552 * can be reused for another operation by calling
1553 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001554 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001555 * You may call this function any time after the operation object has
1556 * been initialized by any of the following methods:
1557 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1558 * it succeeds or not.
1559 * - Initializing the \c struct to all-bits-zero.
1560 * - Initializing the \c struct to logical zeros, e.g.
1561 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001562 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001563 * In particular, calling psa_mac_abort() after the operation has been
1564 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1565 * psa_mac_verify_finish() is safe and has no effect.
1566 *
1567 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001568 *
1569 * \retval #PSA_SUCCESS
1570 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001571 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001572 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1573 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001574 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001575 * \retval #PSA_ERROR_BAD_STATE
1576 * The library has not been previously initialized by psa_crypto_init().
1577 * It is implementation-dependent whether a failure to initialize
1578 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001579 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001580psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1581
1582/**@}*/
1583
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001584/** \defgroup cipher Symmetric ciphers
1585 * @{
1586 */
1587
Gilles Peskine69647a42019-01-14 20:18:12 +01001588/** Encrypt a message using a symmetric cipher.
1589 *
1590 * This function encrypts a message with a random IV (initialization
1591 * vector).
1592 *
1593 * \param handle Handle to the key to use for the operation.
1594 * It must remain valid until the operation
1595 * terminates.
1596 * \param alg The cipher algorithm to compute
1597 * (\c PSA_ALG_XXX value such that
1598 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1599 * \param[in] input Buffer containing the message to encrypt.
1600 * \param input_length Size of the \p input buffer in bytes.
1601 * \param[out] output Buffer where the output is to be written.
1602 * The output contains the IV followed by
1603 * the ciphertext proper.
1604 * \param output_size Size of the \p output buffer in bytes.
1605 * \param[out] output_length On success, the number of bytes
1606 * that make up the output.
1607 *
1608 * \retval #PSA_SUCCESS
1609 * Success.
1610 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001611 * \retval #PSA_ERROR_NOT_PERMITTED
1612 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001613 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001614 * \retval #PSA_ERROR_NOT_SUPPORTED
1615 * \p alg is not supported or is not a cipher algorithm.
1616 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1617 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1618 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1619 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001620 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001621 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001622 * \retval #PSA_ERROR_BAD_STATE
1623 * The library has not been previously initialized by psa_crypto_init().
1624 * It is implementation-dependent whether a failure to initialize
1625 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001626 */
1627psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1628 psa_algorithm_t alg,
1629 const uint8_t *input,
1630 size_t input_length,
1631 uint8_t *output,
1632 size_t output_size,
1633 size_t *output_length);
1634
1635/** Decrypt a message using a symmetric cipher.
1636 *
1637 * This function decrypts a message encrypted with a symmetric cipher.
1638 *
1639 * \param handle Handle to the key to use for the operation.
1640 * It must remain valid until the operation
1641 * terminates.
1642 * \param alg The cipher algorithm to compute
1643 * (\c PSA_ALG_XXX value such that
1644 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1645 * \param[in] input Buffer containing the message to decrypt.
1646 * This consists of the IV followed by the
1647 * ciphertext proper.
1648 * \param input_length Size of the \p input buffer in bytes.
1649 * \param[out] output Buffer where the plaintext is to be written.
1650 * \param output_size Size of the \p output buffer in bytes.
1651 * \param[out] output_length On success, the number of bytes
1652 * that make up the output.
1653 *
1654 * \retval #PSA_SUCCESS
1655 * Success.
1656 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001657 * \retval #PSA_ERROR_NOT_PERMITTED
1658 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001659 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001660 * \retval #PSA_ERROR_NOT_SUPPORTED
1661 * \p alg is not supported or is not a cipher algorithm.
1662 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1663 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1664 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1665 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001666 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001667 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001668 * \retval #PSA_ERROR_BAD_STATE
1669 * The library has not been previously initialized by psa_crypto_init().
1670 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001671 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001672 */
1673psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1674 psa_algorithm_t alg,
1675 const uint8_t *input,
1676 size_t input_length,
1677 uint8_t *output,
1678 size_t output_size,
1679 size_t *output_length);
1680
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001681/** The type of the state data structure for multipart cipher operations.
1682 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001683 * Before calling any function on a cipher operation object, the application
1684 * must initialize it by any of the following means:
1685 * - Set the structure to all-bits-zero, for example:
1686 * \code
1687 * psa_cipher_operation_t operation;
1688 * memset(&operation, 0, sizeof(operation));
1689 * \endcode
1690 * - Initialize the structure to logical zero values, for example:
1691 * \code
1692 * psa_cipher_operation_t operation = {0};
1693 * \endcode
1694 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1695 * for example:
1696 * \code
1697 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1698 * \endcode
1699 * - Assign the result of the function psa_cipher_operation_init()
1700 * to the structure, for example:
1701 * \code
1702 * psa_cipher_operation_t operation;
1703 * operation = psa_cipher_operation_init();
1704 * \endcode
1705 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001706 * This is an implementation-defined \c struct. Applications should not
1707 * make any assumptions about the content of this structure except
1708 * as directed by the documentation of a specific implementation. */
1709typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1710
Jaeden Amero5bae2272019-01-04 11:48:27 +00001711/** \def PSA_CIPHER_OPERATION_INIT
1712 *
1713 * This macro returns a suitable initializer for a cipher operation object of
1714 * type #psa_cipher_operation_t.
1715 */
1716#ifdef __DOXYGEN_ONLY__
1717/* This is an example definition for documentation purposes.
1718 * Implementations should define a suitable value in `crypto_struct.h`.
1719 */
1720#define PSA_CIPHER_OPERATION_INIT {0}
1721#endif
1722
1723/** Return an initial value for a cipher operation object.
1724 */
1725static psa_cipher_operation_t psa_cipher_operation_init(void);
1726
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001727/** Set the key for a multipart symmetric encryption operation.
1728 *
1729 * The sequence of operations to encrypt a message with a symmetric cipher
1730 * is as follows:
1731 * -# Allocate an operation object which will be passed to all the functions
1732 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001733 * -# Initialize the operation object with one of the methods described in the
1734 * documentation for #psa_cipher_operation_t, e.g.
1735 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001736 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001737 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001738 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001739 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001740 * requires a specific IV value.
1741 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1742 * of the message each time.
1743 * -# Call psa_cipher_finish().
1744 *
1745 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001746 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001747 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001748 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001749 * eventually terminate the operation. The following events terminate an
1750 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001751 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001752 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001753 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001754 * \param[in,out] operation The operation object to set up. It must have
1755 * been initialized as per the documentation for
1756 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001757 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001758 * It must remain valid until the operation
1759 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001760 * \param alg The cipher algorithm to compute
1761 * (\c PSA_ALG_XXX value such that
1762 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001763 *
Gilles Peskine28538492018-07-11 17:34:00 +02001764 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001765 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001766 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001767 * \retval #PSA_ERROR_NOT_PERMITTED
1768 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001769 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001770 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001771 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001772 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1773 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1774 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001775 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001776 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001777 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001778 * The operation state is not valid (already set up and not
1779 * subsequently completed).
1780 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001781 * The library has not been previously initialized by psa_crypto_init().
1782 * It is implementation-dependent whether a failure to initialize
1783 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001784 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001785psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001786 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001787 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001788
1789/** Set the key for a multipart symmetric decryption operation.
1790 *
1791 * The sequence of operations to decrypt a message with a symmetric cipher
1792 * is as follows:
1793 * -# Allocate an operation object which will be passed to all the functions
1794 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001795 * -# Initialize the operation object with one of the methods described in the
1796 * documentation for #psa_cipher_operation_t, e.g.
1797 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001798 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001799 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001800 * decryption. If the IV is prepended to the ciphertext, you can call
1801 * psa_cipher_update() on a buffer containing the IV followed by the
1802 * beginning of the message.
1803 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1804 * of the message each time.
1805 * -# Call psa_cipher_finish().
1806 *
1807 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001808 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001809 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001810 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001811 * eventually terminate the operation. The following events terminate an
1812 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001813 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001814 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001815 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001816 * \param[in,out] operation The operation object to set up. It must have
1817 * been initialized as per the documentation for
1818 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001819 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001820 * It must remain valid until the operation
1821 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001822 * \param alg The cipher algorithm to compute
1823 * (\c PSA_ALG_XXX value such that
1824 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001825 *
Gilles Peskine28538492018-07-11 17:34:00 +02001826 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001827 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001828 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001829 * \retval #PSA_ERROR_NOT_PERMITTED
1830 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001831 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001832 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001833 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001834 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1835 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1836 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001837 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001838 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001839 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001840 * The operation state is not valid (already set up and not
1841 * subsequently completed).
1842 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001843 * The library has not been previously initialized by psa_crypto_init().
1844 * It is implementation-dependent whether a failure to initialize
1845 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001846 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001847psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001848 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001849 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001850
Gilles Peskinedcd14942018-07-12 00:30:52 +02001851/** Generate an IV for a symmetric encryption operation.
1852 *
1853 * This function generates a random IV (initialization vector), nonce
1854 * or initial counter value for the encryption operation as appropriate
1855 * for the chosen algorithm, key type and key size.
1856 *
1857 * The application must call psa_cipher_encrypt_setup() before
1858 * calling this function.
1859 *
1860 * If this function returns an error status, the operation becomes inactive.
1861 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001862 * \param[in,out] operation Active cipher operation.
1863 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001864 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001865 * \param[out] iv_length On success, the number of bytes of the
1866 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001867 *
1868 * \retval #PSA_SUCCESS
1869 * Success.
1870 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001871 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001872 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001873 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001874 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1875 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1876 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001877 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001878 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001879 * \retval #PSA_ERROR_BAD_STATE
1880 * The library has not been previously initialized by psa_crypto_init().
1881 * It is implementation-dependent whether a failure to initialize
1882 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001883 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001884psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001885 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001886 size_t iv_size,
1887 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001888
Gilles Peskinedcd14942018-07-12 00:30:52 +02001889/** Set the IV for a symmetric encryption or decryption operation.
1890 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001891 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001892 * or initial counter value for the encryption or decryption operation.
1893 *
1894 * The application must call psa_cipher_encrypt_setup() before
1895 * calling this function.
1896 *
1897 * If this function returns an error status, the operation becomes inactive.
1898 *
1899 * \note When encrypting, applications should use psa_cipher_generate_iv()
1900 * instead of this function, unless implementing a protocol that requires
1901 * a non-random IV.
1902 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001903 * \param[in,out] operation Active cipher operation.
1904 * \param[in] iv Buffer containing the IV to use.
1905 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001906 *
1907 * \retval #PSA_SUCCESS
1908 * Success.
1909 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001910 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001911 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001912 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001913 * or the chosen algorithm does not use an IV.
1914 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1915 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1916 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001917 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001918 * \retval #PSA_ERROR_BAD_STATE
1919 * The library has not been previously initialized by psa_crypto_init().
1920 * It is implementation-dependent whether a failure to initialize
1921 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001922 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001923psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001924 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001925 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001926
Gilles Peskinedcd14942018-07-12 00:30:52 +02001927/** Encrypt or decrypt a message fragment in an active cipher operation.
1928 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001929 * Before calling this function, you must:
1930 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1931 * The choice of setup function determines whether this function
1932 * encrypts or decrypts its input.
1933 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1934 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001935 *
1936 * If this function returns an error status, the operation becomes inactive.
1937 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001938 * \param[in,out] operation Active cipher operation.
1939 * \param[in] input Buffer containing the message fragment to
1940 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001941 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001942 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001943 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001944 * \param[out] output_length On success, the number of bytes
1945 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001946 *
1947 * \retval #PSA_SUCCESS
1948 * Success.
1949 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001950 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001951 * not set, or already completed).
1952 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1953 * The size of the \p output buffer is too small.
1954 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1955 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1956 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001957 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001958 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001959 * \retval #PSA_ERROR_BAD_STATE
1960 * The library has not been previously initialized by psa_crypto_init().
1961 * It is implementation-dependent whether a failure to initialize
1962 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001963 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001964psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1965 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001966 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001967 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001968 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001969 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001970
Gilles Peskinedcd14942018-07-12 00:30:52 +02001971/** Finish encrypting or decrypting a message in a cipher operation.
1972 *
1973 * The application must call psa_cipher_encrypt_setup() or
1974 * psa_cipher_decrypt_setup() before calling this function. The choice
1975 * of setup function determines whether this function encrypts or
1976 * decrypts its input.
1977 *
1978 * This function finishes the encryption or decryption of the message
1979 * formed by concatenating the inputs passed to preceding calls to
1980 * psa_cipher_update().
1981 *
1982 * When this function returns, the operation becomes inactive.
1983 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001984 * \param[in,out] operation Active cipher operation.
1985 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001986 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001987 * \param[out] output_length On success, the number of bytes
1988 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001989 *
1990 * \retval #PSA_SUCCESS
1991 * Success.
1992 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001993 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001994 * not set, or already completed).
1995 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1996 * The size of the \p output buffer is too small.
1997 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1998 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1999 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002000 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002001 * \retval #PSA_ERROR_BAD_STATE
2002 * The library has not been previously initialized by psa_crypto_init().
2003 * It is implementation-dependent whether a failure to initialize
2004 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002005 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002006psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002007 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002008 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002009 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002010
Gilles Peskinedcd14942018-07-12 00:30:52 +02002011/** Abort a cipher operation.
2012 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002013 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002014 * \p operation structure itself. Once aborted, the operation object
2015 * can be reused for another operation by calling
2016 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002017 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002018 * You may call this function any time after the operation object has
2019 * been initialized by any of the following methods:
2020 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2021 * whether it succeeds or not.
2022 * - Initializing the \c struct to all-bits-zero.
2023 * - Initializing the \c struct to logical zeros, e.g.
2024 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002025 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002026 * In particular, calling psa_cipher_abort() after the operation has been
2027 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2028 * is safe and has no effect.
2029 *
2030 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002031 *
2032 * \retval #PSA_SUCCESS
2033 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002034 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002035 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2036 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002037 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002038 * \retval #PSA_ERROR_BAD_STATE
2039 * The library has not been previously initialized by psa_crypto_init().
2040 * It is implementation-dependent whether a failure to initialize
2041 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002042 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002043psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2044
2045/**@}*/
2046
Gilles Peskine3b555712018-03-03 21:27:57 +01002047/** \defgroup aead Authenticated encryption with associated data (AEAD)
2048 * @{
2049 */
2050
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002051/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002052 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002053 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002054 * \param alg The AEAD algorithm to compute
2055 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002056 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002057 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002058 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002059 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002060 * but not encrypted.
2061 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002062 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002063 * encrypted.
2064 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002065 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002066 * encrypted data. The additional data is not
2067 * part of this output. For algorithms where the
2068 * encrypted data and the authentication tag
2069 * are defined as separate outputs, the
2070 * authentication tag is appended to the
2071 * encrypted data.
2072 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2073 * This must be at least
2074 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2075 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002076 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002077 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002078 *
Gilles Peskine28538492018-07-11 17:34:00 +02002079 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002080 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002081 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002082 * \retval #PSA_ERROR_NOT_PERMITTED
2083 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002084 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002085 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002086 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002087 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002088 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2089 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002090 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2091 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002092 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002093 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002094 * The library has not been previously initialized by psa_crypto_init().
2095 * It is implementation-dependent whether a failure to initialize
2096 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002097 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002098psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002099 psa_algorithm_t alg,
2100 const uint8_t *nonce,
2101 size_t nonce_length,
2102 const uint8_t *additional_data,
2103 size_t additional_data_length,
2104 const uint8_t *plaintext,
2105 size_t plaintext_length,
2106 uint8_t *ciphertext,
2107 size_t ciphertext_size,
2108 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002109
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002110/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002111 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002112 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002113 * \param alg The AEAD algorithm to compute
2114 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002115 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002116 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002117 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002118 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002119 * but not encrypted.
2120 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002121 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002122 * encrypted. For algorithms where the
2123 * encrypted data and the authentication tag
2124 * are defined as separate inputs, the buffer
2125 * must contain the encrypted data followed
2126 * by the authentication tag.
2127 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002128 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002129 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2130 * This must be at least
2131 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2132 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002133 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002134 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002135 *
Gilles Peskine28538492018-07-11 17:34:00 +02002136 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002137 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002138 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002139 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002140 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002141 * \retval #PSA_ERROR_NOT_PERMITTED
2142 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002143 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002144 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002145 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002146 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002147 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2148 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002149 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2150 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002151 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002152 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002153 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002154 * The library has not been previously initialized by psa_crypto_init().
2155 * It is implementation-dependent whether a failure to initialize
2156 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002157 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002158psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002159 psa_algorithm_t alg,
2160 const uint8_t *nonce,
2161 size_t nonce_length,
2162 const uint8_t *additional_data,
2163 size_t additional_data_length,
2164 const uint8_t *ciphertext,
2165 size_t ciphertext_length,
2166 uint8_t *plaintext,
2167 size_t plaintext_size,
2168 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002169
Gilles Peskine30a9e412019-01-14 18:36:12 +01002170/** The type of the state data structure for multipart AEAD operations.
2171 *
2172 * Before calling any function on an AEAD operation object, the application
2173 * must initialize it by any of the following means:
2174 * - Set the structure to all-bits-zero, for example:
2175 * \code
2176 * psa_aead_operation_t operation;
2177 * memset(&operation, 0, sizeof(operation));
2178 * \endcode
2179 * - Initialize the structure to logical zero values, for example:
2180 * \code
2181 * psa_aead_operation_t operation = {0};
2182 * \endcode
2183 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2184 * for example:
2185 * \code
2186 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2187 * \endcode
2188 * - Assign the result of the function psa_aead_operation_init()
2189 * to the structure, for example:
2190 * \code
2191 * psa_aead_operation_t operation;
2192 * operation = psa_aead_operation_init();
2193 * \endcode
2194 *
2195 * This is an implementation-defined \c struct. Applications should not
2196 * make any assumptions about the content of this structure except
2197 * as directed by the documentation of a specific implementation. */
2198typedef struct psa_aead_operation_s psa_aead_operation_t;
2199
2200/** \def PSA_AEAD_OPERATION_INIT
2201 *
2202 * This macro returns a suitable initializer for an AEAD operation object of
2203 * type #psa_aead_operation_t.
2204 */
2205#ifdef __DOXYGEN_ONLY__
2206/* This is an example definition for documentation purposes.
2207 * Implementations should define a suitable value in `crypto_struct.h`.
2208 */
2209#define PSA_AEAD_OPERATION_INIT {0}
2210#endif
2211
2212/** Return an initial value for an AEAD operation object.
2213 */
2214static psa_aead_operation_t psa_aead_operation_init(void);
2215
2216/** Set the key for a multipart authenticated encryption operation.
2217 *
2218 * The sequence of operations to encrypt a message with authentication
2219 * is as follows:
2220 * -# Allocate an operation object which will be passed to all the functions
2221 * listed here.
2222 * -# Initialize the operation object with one of the methods described in the
2223 * documentation for #psa_aead_operation_t, e.g.
2224 * PSA_AEAD_OPERATION_INIT.
2225 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002226 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2227 * inputs to the subsequent calls to psa_aead_update_ad() and
2228 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2229 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002230 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2231 * generate or set the nonce. You should use
2232 * psa_aead_generate_nonce() unless the protocol you are implementing
2233 * requires a specific nonce value.
2234 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2235 * of the non-encrypted additional authenticated data each time.
2236 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002237 * of the message to encrypt each time.
Adrian L. Shaw67257572019-08-15 10:53:47 +01002238 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002239 *
2240 * The application may call psa_aead_abort() at any time after the operation
2241 * has been initialized.
2242 *
2243 * After a successful call to psa_aead_encrypt_setup(), the application must
2244 * eventually terminate the operation. The following events terminate an
2245 * operation:
2246 * - A failed call to any of the \c psa_aead_xxx functions.
2247 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2248 *
2249 * \param[in,out] operation The operation object to set up. It must have
2250 * been initialized as per the documentation for
2251 * #psa_aead_operation_t and not yet in use.
2252 * \param handle Handle to the key to use for the operation.
2253 * It must remain valid until the operation
2254 * terminates.
2255 * \param alg The AEAD algorithm to compute
2256 * (\c PSA_ALG_XXX value such that
2257 * #PSA_ALG_IS_AEAD(\p alg) is true).
2258 *
2259 * \retval #PSA_SUCCESS
2260 * Success.
2261 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002262 * \retval #PSA_ERROR_NOT_PERMITTED
2263 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002264 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002265 * \retval #PSA_ERROR_NOT_SUPPORTED
2266 * \p alg is not supported or is not an AEAD algorithm.
2267 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2268 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2269 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002270 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002271 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002272 * \retval #PSA_ERROR_BAD_STATE
2273 * The library has not been previously initialized by psa_crypto_init().
2274 * It is implementation-dependent whether a failure to initialize
2275 * results in this error code.
2276 */
2277psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2278 psa_key_handle_t handle,
2279 psa_algorithm_t alg);
2280
2281/** Set the key for a multipart authenticated decryption operation.
2282 *
2283 * The sequence of operations to decrypt a message with authentication
2284 * is as follows:
2285 * -# Allocate an operation object which will be passed to all the functions
2286 * listed here.
2287 * -# Initialize the operation object with one of the methods described in the
2288 * documentation for #psa_aead_operation_t, e.g.
2289 * PSA_AEAD_OPERATION_INIT.
2290 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002291 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2292 * inputs to the subsequent calls to psa_aead_update_ad() and
2293 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2294 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002295 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2296 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2297 * of the non-encrypted additional authenticated data each time.
2298 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002299 * of the ciphertext to decrypt each time.
2300 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002301 *
2302 * The application may call psa_aead_abort() at any time after the operation
2303 * has been initialized.
2304 *
2305 * After a successful call to psa_aead_decrypt_setup(), the application must
2306 * eventually terminate the operation. The following events terminate an
2307 * operation:
2308 * - A failed call to any of the \c psa_aead_xxx functions.
2309 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2310 *
2311 * \param[in,out] operation The operation object to set up. It must have
2312 * been initialized as per the documentation for
2313 * #psa_aead_operation_t and not yet in use.
2314 * \param handle Handle to the key to use for the operation.
2315 * It must remain valid until the operation
2316 * terminates.
2317 * \param alg The AEAD algorithm to compute
2318 * (\c PSA_ALG_XXX value such that
2319 * #PSA_ALG_IS_AEAD(\p alg) is true).
2320 *
2321 * \retval #PSA_SUCCESS
2322 * Success.
2323 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002324 * \retval #PSA_ERROR_NOT_PERMITTED
2325 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002326 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002327 * \retval #PSA_ERROR_NOT_SUPPORTED
2328 * \p alg is not supported or is not an AEAD algorithm.
2329 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2330 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2331 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002332 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002333 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002334 * \retval #PSA_ERROR_BAD_STATE
2335 * The library has not been previously initialized by psa_crypto_init().
2336 * It is implementation-dependent whether a failure to initialize
2337 * results in this error code.
2338 */
2339psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2340 psa_key_handle_t handle,
2341 psa_algorithm_t alg);
2342
2343/** Generate a random nonce for an authenticated encryption operation.
2344 *
2345 * This function generates a random nonce for the authenticated encryption
2346 * operation with an appropriate size for the chosen algorithm, key type
2347 * and key size.
2348 *
2349 * The application must call psa_aead_encrypt_setup() before
2350 * calling this function.
2351 *
2352 * If this function returns an error status, the operation becomes inactive.
2353 *
2354 * \param[in,out] operation Active AEAD operation.
2355 * \param[out] nonce Buffer where the generated nonce is to be
2356 * written.
2357 * \param nonce_size Size of the \p nonce buffer in bytes.
2358 * \param[out] nonce_length On success, the number of bytes of the
2359 * generated nonce.
2360 *
2361 * \retval #PSA_SUCCESS
2362 * Success.
2363 * \retval #PSA_ERROR_BAD_STATE
2364 * The operation state is not valid (not set up, or nonce already set).
2365 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2366 * The size of the \p nonce buffer is too small.
2367 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2368 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2369 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002370 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002371 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002372 * \retval #PSA_ERROR_BAD_STATE
2373 * The library has not been previously initialized by psa_crypto_init().
2374 * It is implementation-dependent whether a failure to initialize
2375 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002376 */
2377psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002378 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002379 size_t nonce_size,
2380 size_t *nonce_length);
2381
2382/** Set the nonce for an authenticated encryption or decryption operation.
2383 *
2384 * This function sets the nonce for the authenticated
2385 * encryption or decryption operation.
2386 *
2387 * The application must call psa_aead_encrypt_setup() before
2388 * calling this function.
2389 *
2390 * If this function returns an error status, the operation becomes inactive.
2391 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002392 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002393 * instead of this function, unless implementing a protocol that requires
2394 * a non-random IV.
2395 *
2396 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002397 * \param[in] nonce Buffer containing the nonce to use.
2398 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002399 *
2400 * \retval #PSA_SUCCESS
2401 * Success.
2402 * \retval #PSA_ERROR_BAD_STATE
2403 * The operation state is not valid (not set up, or nonce already set).
2404 * \retval #PSA_ERROR_INVALID_ARGUMENT
2405 * The size of \p nonce is not acceptable for the chosen algorithm.
2406 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2407 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2408 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002409 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002410 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002411 * \retval #PSA_ERROR_BAD_STATE
2412 * The library has not been previously initialized by psa_crypto_init().
2413 * It is implementation-dependent whether a failure to initialize
2414 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002415 */
2416psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002417 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002418 size_t nonce_length);
2419
Gilles Peskinebc59c852019-01-17 15:26:08 +01002420/** Declare the lengths of the message and additional data for AEAD.
2421 *
2422 * The application must call this function before calling
2423 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2424 * the operation requires it. If the algorithm does not require it,
2425 * calling this function is optional, but if this function is called
2426 * then the implementation must enforce the lengths.
2427 *
2428 * You may call this function before or after setting the nonce with
2429 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2430 *
2431 * - For #PSA_ALG_CCM, calling this function is required.
2432 * - For the other AEAD algorithms defined in this specification, calling
2433 * this function is not required.
2434 * - For vendor-defined algorithm, refer to the vendor documentation.
2435 *
2436 * \param[in,out] operation Active AEAD operation.
2437 * \param ad_length Size of the non-encrypted additional
2438 * authenticated data in bytes.
2439 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2440 *
2441 * \retval #PSA_SUCCESS
2442 * Success.
2443 * \retval #PSA_ERROR_BAD_STATE
2444 * The operation state is not valid (not set up, already completed,
2445 * or psa_aead_update_ad() or psa_aead_update() already called).
2446 * \retval #PSA_ERROR_INVALID_ARGUMENT
2447 * At least one of the lengths is not acceptable for the chosen
2448 * algorithm.
2449 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2450 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2451 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002452 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002453 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002454 * \retval #PSA_ERROR_BAD_STATE
2455 * The library has not been previously initialized by psa_crypto_init().
2456 * It is implementation-dependent whether a failure to initialize
2457 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002458 */
2459psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2460 size_t ad_length,
2461 size_t plaintext_length);
2462
Gilles Peskine30a9e412019-01-14 18:36:12 +01002463/** Pass additional data to an active AEAD operation.
2464 *
2465 * Additional data is authenticated, but not encrypted.
2466 *
2467 * You may call this function multiple times to pass successive fragments
2468 * of the additional data. You may not call this function after passing
2469 * data to encrypt or decrypt with psa_aead_update().
2470 *
2471 * Before calling this function, you must:
2472 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2473 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2474 *
2475 * If this function returns an error status, the operation becomes inactive.
2476 *
2477 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2478 * there is no guarantee that the input is valid. Therefore, until
2479 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2480 * treat the input as untrusted and prepare to undo any action that
2481 * depends on the input if psa_aead_verify() returns an error status.
2482 *
2483 * \param[in,out] operation Active AEAD operation.
2484 * \param[in] input Buffer containing the fragment of
2485 * additional data.
2486 * \param input_length Size of the \p input buffer in bytes.
2487 *
2488 * \retval #PSA_SUCCESS
2489 * Success.
2490 * \retval #PSA_ERROR_BAD_STATE
2491 * The operation state is not valid (not set up, nonce not set,
2492 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002493 * \retval #PSA_ERROR_INVALID_ARGUMENT
2494 * The total input length overflows the additional data length that
2495 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002496 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2497 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2498 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002499 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002500 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002501 * \retval #PSA_ERROR_BAD_STATE
2502 * The library has not been previously initialized by psa_crypto_init().
2503 * It is implementation-dependent whether a failure to initialize
2504 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002505 */
2506psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2507 const uint8_t *input,
2508 size_t input_length);
2509
2510/** Encrypt or decrypt a message fragment in an active AEAD operation.
2511 *
2512 * Before calling this function, you must:
2513 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2514 * The choice of setup function determines whether this function
2515 * encrypts or decrypts its input.
2516 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2517 * 3. Call psa_aead_update_ad() to pass all the additional data.
2518 *
2519 * If this function returns an error status, the operation becomes inactive.
2520 *
2521 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2522 * there is no guarantee that the input is valid. Therefore, until
2523 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2524 * - Do not use the output in any way other than storing it in a
2525 * confidential location. If you take any action that depends
2526 * on the tentative decrypted data, this action will need to be
2527 * undone if the input turns out not to be valid. Furthermore,
2528 * if an adversary can observe that this action took place
2529 * (for example through timing), they may be able to use this
2530 * fact as an oracle to decrypt any message encrypted with the
2531 * same key.
2532 * - In particular, do not copy the output anywhere but to a
2533 * memory or storage space that you have exclusive access to.
2534 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002535 * This function does not require the input to be aligned to any
2536 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002537 * a whole block at a time, it must consume all the input provided, but
2538 * it may delay the end of the corresponding output until a subsequent
2539 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2540 * provides sufficient input. The amount of data that can be delayed
2541 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002542 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002543 * \param[in,out] operation Active AEAD operation.
2544 * \param[in] input Buffer containing the message fragment to
2545 * encrypt or decrypt.
2546 * \param input_length Size of the \p input buffer in bytes.
2547 * \param[out] output Buffer where the output is to be written.
2548 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002549 * This must be at least
2550 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2551 * \p input_length) where \c alg is the
2552 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002553 * \param[out] output_length On success, the number of bytes
2554 * that make up the returned output.
2555 *
2556 * \retval #PSA_SUCCESS
2557 * Success.
2558 * \retval #PSA_ERROR_BAD_STATE
2559 * The operation state is not valid (not set up, nonce not set
2560 * or already completed).
2561 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2562 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002563 * You can determine a sufficient buffer size by calling
2564 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2565 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002566 * \retval #PSA_ERROR_INVALID_ARGUMENT
2567 * The total length of input to psa_aead_update_ad() so far is
2568 * less than the additional data length that was previously
2569 * specified with psa_aead_set_lengths().
2570 * \retval #PSA_ERROR_INVALID_ARGUMENT
2571 * The total input length overflows the plaintext length that
2572 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002573 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2574 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2575 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002576 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002577 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002578 * \retval #PSA_ERROR_BAD_STATE
2579 * The library has not been previously initialized by psa_crypto_init().
2580 * It is implementation-dependent whether a failure to initialize
2581 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002582 */
2583psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2584 const uint8_t *input,
2585 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002586 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002587 size_t output_size,
2588 size_t *output_length);
2589
2590/** Finish encrypting a message in an AEAD operation.
2591 *
2592 * The operation must have been set up with psa_aead_encrypt_setup().
2593 *
2594 * This function finishes the authentication of the additional data
2595 * formed by concatenating the inputs passed to preceding calls to
2596 * psa_aead_update_ad() with the plaintext formed by concatenating the
2597 * inputs passed to preceding calls to psa_aead_update().
2598 *
2599 * This function has two output buffers:
2600 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002601 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002602 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002603 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002604 * that the operation performs.
2605 *
2606 * When this function returns, the operation becomes inactive.
2607 *
2608 * \param[in,out] operation Active AEAD operation.
2609 * \param[out] ciphertext Buffer where the last part of the ciphertext
2610 * is to be written.
2611 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002612 * This must be at least
2613 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2614 * \c alg is the algorithm that is being
2615 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002616 * \param[out] ciphertext_length On success, the number of bytes of
2617 * returned ciphertext.
2618 * \param[out] tag Buffer where the authentication tag is
2619 * to be written.
2620 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002621 * This must be at least
2622 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2623 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002624 * \param[out] tag_length On success, the number of bytes
2625 * that make up the returned tag.
2626 *
2627 * \retval #PSA_SUCCESS
2628 * Success.
2629 * \retval #PSA_ERROR_BAD_STATE
2630 * The operation state is not valid (not set up, nonce not set,
2631 * decryption, or already completed).
2632 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002633 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002634 * You can determine a sufficient buffer size for \p ciphertext by
2635 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2636 * where \c alg is the algorithm that is being calculated.
2637 * You can determine a sufficient buffer size for \p tag by
2638 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002639 * \retval #PSA_ERROR_INVALID_ARGUMENT
2640 * The total length of input to psa_aead_update_ad() so far is
2641 * less than the additional data length that was previously
2642 * specified with psa_aead_set_lengths().
2643 * \retval #PSA_ERROR_INVALID_ARGUMENT
2644 * The total length of input to psa_aead_update() so far is
2645 * less than the plaintext length that was previously
2646 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002647 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2648 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2649 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002650 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002651 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002652 * \retval #PSA_ERROR_BAD_STATE
2653 * The library has not been previously initialized by psa_crypto_init().
2654 * It is implementation-dependent whether a failure to initialize
2655 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002656 */
2657psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002658 uint8_t *ciphertext,
2659 size_t ciphertext_size,
2660 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002661 uint8_t *tag,
2662 size_t tag_size,
2663 size_t *tag_length);
2664
2665/** Finish authenticating and decrypting a message in an AEAD operation.
2666 *
2667 * The operation must have been set up with psa_aead_decrypt_setup().
2668 *
2669 * This function finishes the authentication of the additional data
2670 * formed by concatenating the inputs passed to preceding calls to
2671 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2672 * inputs passed to preceding calls to psa_aead_update().
2673 *
2674 * When this function returns, the operation becomes inactive.
2675 *
2676 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002677 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002678 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002679 * from previous calls to psa_aead_update()
2680 * that could not be processed until the end
2681 * of the input.
2682 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002683 * This must be at least
2684 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2685 * \c alg is the algorithm that is being
2686 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002687 * \param[out] plaintext_length On success, the number of bytes of
2688 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002689 * \param[in] tag Buffer containing the authentication tag.
2690 * \param tag_length Size of the \p tag buffer in bytes.
2691 *
2692 * \retval #PSA_SUCCESS
2693 * Success.
2694 * \retval #PSA_ERROR_BAD_STATE
2695 * The operation state is not valid (not set up, nonce not set,
2696 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002697 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2698 * The size of the \p plaintext buffer is too small.
2699 * You can determine a sufficient buffer size for \p plaintext by
2700 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2701 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002702 * \retval #PSA_ERROR_INVALID_ARGUMENT
2703 * The total length of input to psa_aead_update_ad() so far is
2704 * less than the additional data length that was previously
2705 * specified with psa_aead_set_lengths().
2706 * \retval #PSA_ERROR_INVALID_ARGUMENT
2707 * The total length of input to psa_aead_update() so far is
2708 * less than the plaintext length that was previously
2709 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002710 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2711 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2712 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002713 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002714 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002715 * \retval #PSA_ERROR_BAD_STATE
2716 * The library has not been previously initialized by psa_crypto_init().
2717 * It is implementation-dependent whether a failure to initialize
2718 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002719 */
2720psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002721 uint8_t *plaintext,
2722 size_t plaintext_size,
2723 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002724 const uint8_t *tag,
2725 size_t tag_length);
2726
2727/** Abort an AEAD operation.
2728 *
2729 * Aborting an operation frees all associated resources except for the
2730 * \p operation structure itself. Once aborted, the operation object
2731 * can be reused for another operation by calling
2732 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2733 *
2734 * You may call this function any time after the operation object has
2735 * been initialized by any of the following methods:
2736 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2737 * whether it succeeds or not.
2738 * - Initializing the \c struct to all-bits-zero.
2739 * - Initializing the \c struct to logical zeros, e.g.
2740 * `psa_aead_operation_t operation = {0}`.
2741 *
2742 * In particular, calling psa_aead_abort() after the operation has been
2743 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2744 * is safe and has no effect.
2745 *
2746 * \param[in,out] operation Initialized AEAD operation.
2747 *
2748 * \retval #PSA_SUCCESS
2749 * \retval #PSA_ERROR_BAD_STATE
2750 * \p operation is not an active AEAD operation.
2751 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2752 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002753 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002754 * \retval #PSA_ERROR_BAD_STATE
2755 * The library has not been previously initialized by psa_crypto_init().
2756 * It is implementation-dependent whether a failure to initialize
2757 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002758 */
2759psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2760
Gilles Peskine3b555712018-03-03 21:27:57 +01002761/**@}*/
2762
Gilles Peskine20035e32018-02-03 22:44:14 +01002763/** \defgroup asymmetric Asymmetric cryptography
2764 * @{
2765 */
2766
2767/**
2768 * \brief Sign a hash or short message with a private key.
2769 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002770 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002771 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002772 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2773 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2774 * to determine the hash algorithm to use.
2775 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002776 * \param handle Handle to the key to use for the operation.
2777 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002778 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002779 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002780 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002781 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002782 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002783 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002784 * \param[out] signature_length On success, the number of bytes
2785 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002786 *
Gilles Peskine28538492018-07-11 17:34:00 +02002787 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002788 * \retval #PSA_ERROR_INVALID_HANDLE
2789 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002790 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002791 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002792 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002793 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002794 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002795 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002796 * \retval #PSA_ERROR_NOT_SUPPORTED
2797 * \retval #PSA_ERROR_INVALID_ARGUMENT
2798 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2799 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2800 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002801 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002802 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002803 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002804 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002805 * The library has not been previously initialized by psa_crypto_init().
2806 * It is implementation-dependent whether a failure to initialize
2807 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002808 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002809psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002810 psa_algorithm_t alg,
2811 const uint8_t *hash,
2812 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002813 uint8_t *signature,
2814 size_t signature_size,
2815 size_t *signature_length);
2816
2817/**
2818 * \brief Verify the signature a hash or short message using a public key.
2819 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002820 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002821 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002822 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2823 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2824 * to determine the hash algorithm to use.
2825 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002826 * \param handle Handle to the key to use for the operation.
2827 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002828 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002829 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002830 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002831 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002832 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002833 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002834 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002835 *
Gilles Peskine28538492018-07-11 17:34:00 +02002836 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002837 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002838 * \retval #PSA_ERROR_INVALID_HANDLE
2839 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002840 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002841 * The calculation was perfomed successfully, but the passed
2842 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002843 * \retval #PSA_ERROR_NOT_SUPPORTED
2844 * \retval #PSA_ERROR_INVALID_ARGUMENT
2845 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2846 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2847 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002848 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002849 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002850 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002851 * The library has not been previously initialized by psa_crypto_init().
2852 * It is implementation-dependent whether a failure to initialize
2853 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002854 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002855psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002856 psa_algorithm_t alg,
2857 const uint8_t *hash,
2858 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002859 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002860 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002861
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002862/**
2863 * \brief Encrypt a short message with a public key.
2864 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002865 * \param handle Handle to the key to use for the operation.
2866 * It must be a public key or an asymmetric
2867 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002868 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002869 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002870 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002871 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002872 * \param[in] salt A salt or label, if supported by the
2873 * encryption algorithm.
2874 * If the algorithm does not support a
2875 * salt, pass \c NULL.
2876 * If the algorithm supports an optional
2877 * salt and you do not want to pass a salt,
2878 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002879 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002880 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2881 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002882 * \param salt_length Size of the \p salt buffer in bytes.
2883 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002884 * \param[out] output Buffer where the encrypted message is to
2885 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002886 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002887 * \param[out] output_length On success, the number of bytes
2888 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002889 *
Gilles Peskine28538492018-07-11 17:34:00 +02002890 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002891 * \retval #PSA_ERROR_INVALID_HANDLE
2892 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002893 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002894 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002895 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002896 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002897 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002898 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002899 * \retval #PSA_ERROR_NOT_SUPPORTED
2900 * \retval #PSA_ERROR_INVALID_ARGUMENT
2901 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2902 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2903 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002904 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002905 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002906 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002907 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002908 * The library has not been previously initialized by psa_crypto_init().
2909 * It is implementation-dependent whether a failure to initialize
2910 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002911 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002912psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002913 psa_algorithm_t alg,
2914 const uint8_t *input,
2915 size_t input_length,
2916 const uint8_t *salt,
2917 size_t salt_length,
2918 uint8_t *output,
2919 size_t output_size,
2920 size_t *output_length);
2921
2922/**
2923 * \brief Decrypt a short message with a private key.
2924 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002925 * \param handle Handle to the key to use for the operation.
2926 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002927 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002928 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002929 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002930 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002931 * \param[in] salt A salt or label, if supported by the
2932 * encryption algorithm.
2933 * If the algorithm does not support a
2934 * salt, pass \c NULL.
2935 * If the algorithm supports an optional
2936 * salt and you do not want to pass a salt,
2937 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002938 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002939 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2940 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002941 * \param salt_length Size of the \p salt buffer in bytes.
2942 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002943 * \param[out] output Buffer where the decrypted message is to
2944 * be written.
2945 * \param output_size Size of the \c output buffer in bytes.
2946 * \param[out] output_length On success, the number of bytes
2947 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002948 *
Gilles Peskine28538492018-07-11 17:34:00 +02002949 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002950 * \retval #PSA_ERROR_INVALID_HANDLE
2951 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002952 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002953 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002954 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002955 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002956 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002957 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002958 * \retval #PSA_ERROR_NOT_SUPPORTED
2959 * \retval #PSA_ERROR_INVALID_ARGUMENT
2960 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2961 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2962 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002963 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002964 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002965 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2966 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002967 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002968 * The library has not been previously initialized by psa_crypto_init().
2969 * It is implementation-dependent whether a failure to initialize
2970 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002971 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002972psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002973 psa_algorithm_t alg,
2974 const uint8_t *input,
2975 size_t input_length,
2976 const uint8_t *salt,
2977 size_t salt_length,
2978 uint8_t *output,
2979 size_t output_size,
2980 size_t *output_length);
2981
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002982/**@}*/
2983
Gilles Peskine35675b62019-05-16 17:26:11 +02002984/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002985 * @{
2986 */
2987
Gilles Peskine35675b62019-05-16 17:26:11 +02002988/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002989 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002990 * Before calling any function on a key derivation operation object, the
2991 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002992 * - Set the structure to all-bits-zero, for example:
2993 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002994 * psa_key_derivation_operation_t operation;
2995 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002996 * \endcode
2997 * - Initialize the structure to logical zero values, for example:
2998 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002999 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003000 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003001 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003002 * for example:
3003 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003004 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003005 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003006 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003007 * to the structure, for example:
3008 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003009 * psa_key_derivation_operation_t operation;
3010 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003011 * \endcode
3012 *
3013 * This is an implementation-defined \c struct. Applications should not
3014 * make any assumptions about the content of this structure except
3015 * as directed by the documentation of a specific implementation.
3016 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003017typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003018
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003019/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003020 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003021 * This macro returns a suitable initializer for a key derivation operation
3022 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003023 */
3024#ifdef __DOXYGEN_ONLY__
3025/* This is an example definition for documentation purposes.
3026 * Implementations should define a suitable value in `crypto_struct.h`.
3027 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003028#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003029#endif
3030
Gilles Peskine35675b62019-05-16 17:26:11 +02003031/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003032 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003033static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003034
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003035/** Set up a key derivation operation.
3036 *
3037 * A key derivation algorithm takes some inputs and uses them to generate
3038 * a byte stream in a deterministic way.
3039 * This byte stream can be used to produce keys and other
3040 * cryptographic material.
3041 *
3042 * To derive a key:
3043 * - Start with an initialized object of type #psa_key_derivation_operation_t.
3044 * - Call psa_key_derivation_setup() to select the algorithm.
3045 * - Provide the inputs for the key derivation by calling
3046 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3047 * as appropriate. Which inputs are needed, in what order, and whether
3048 * they may be keys and if so of what type depends on the algorithm.
3049 * - Optionally set the operation's maximum capacity with
3050 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3051 * of or after providing inputs. For some algorithms, this step is mandatory
3052 * because the output depends on the maximum capacity.
3053 * - To derive a key, call psa_key_derivation_output_key().
3054 * To derive a byte string for a different purpose, call
3055 * - psa_key_derivation_output_bytes().
3056 * Successive calls to these functions use successive output bytes
3057 * calculated by the key derivation algorithm.
3058 * - Clean up the key derivation operation object with
3059 * psa_key_derivation_abort().
3060 *
3061 * \param[in,out] operation The key derivation operation object
3062 * to set up. It must
3063 * have been initialized but not set up yet.
3064 * \param alg The key derivation algorithm to compute
3065 * (\c PSA_ALG_XXX value such that
3066 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3067 *
3068 * \retval #PSA_SUCCESS
3069 * Success.
3070 * \retval #PSA_ERROR_INVALID_ARGUMENT
3071 * \c alg is not a key derivation algorithm.
3072 * \retval #PSA_ERROR_NOT_SUPPORTED
3073 * \c alg is not supported or is not a key derivation algorithm.
3074 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3075 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3076 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003077 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003078 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01003079 * The operation state is either not initialized or has already been setup.
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003080 * \retval #PSA_ERROR_BAD_STATE
3081 * The library has not been previously initialized by psa_crypto_init().
3082 * It is implementation-dependent whether a failure to initialize
3083 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003084 */
3085psa_status_t psa_key_derivation_setup(
3086 psa_key_derivation_operation_t *operation,
3087 psa_algorithm_t alg);
3088
Gilles Peskine35675b62019-05-16 17:26:11 +02003089/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003090 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003091 * The capacity of a key derivation is the maximum number of bytes that it can
3092 * return. When you get *N* bytes of output from a key derivation operation,
3093 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003094 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003095 * \param[in] operation The operation to query.
3096 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003097 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003098 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003099 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003100 * \retval #PSA_ERROR_BAD_STATE
3101 * The operation state is not valid.
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003102 * \retval #PSA_ERROR_HARDWARE_FAILURE
3103 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003104 * \retval #PSA_ERROR_BAD_STATE
3105 * The library has not been previously initialized by psa_crypto_init().
3106 * It is implementation-dependent whether a failure to initialize
3107 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003108 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003109psa_status_t psa_key_derivation_get_capacity(
3110 const psa_key_derivation_operation_t *operation,
3111 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003112
Gilles Peskine35675b62019-05-16 17:26:11 +02003113/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003114 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003115 * The capacity of a key derivation operation is the maximum number of bytes
3116 * that the key derivation operation can return from this point onwards.
3117 *
3118 * \param[in,out] operation The key derivation operation object to modify.
3119 * \param capacity The new capacity of the operation.
3120 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003121 * current capacity.
3122 *
3123 * \retval #PSA_SUCCESS
3124 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003125 * \p capacity is larger than the operation's current capacity.
3126 * In this case, the operation object remains valid and its capacity
3127 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003128 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003129 * The operation state is not valid.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003130 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003131 * \retval #PSA_ERROR_HARDWARE_FAILURE
3132 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003133 * \retval #PSA_ERROR_BAD_STATE
3134 * The library has not been previously initialized by psa_crypto_init().
3135 * It is implementation-dependent whether a failure to initialize
3136 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003137 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003138psa_status_t psa_key_derivation_set_capacity(
3139 psa_key_derivation_operation_t *operation,
3140 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003141
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003142/** Use the maximum possible capacity for a key derivation operation.
3143 *
3144 * Use this value as the capacity argument when setting up a key derivation
3145 * to indicate that the operation should have the maximum possible capacity.
3146 * The value of the maximum possible capacity depends on the key derivation
3147 * algorithm.
3148 */
3149#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3150
3151/** Provide an input for key derivation or key agreement.
3152 *
3153 * Which inputs are required and in what order depends on the algorithm.
3154 * Refer to the documentation of each key derivation or key agreement
3155 * algorithm for information.
3156 *
3157 * This function passes direct inputs. Some inputs must be passed as keys
3158 * using psa_key_derivation_input_key() instead of this function. Refer to
3159 * the documentation of individual step types for information.
3160 *
3161 * \param[in,out] operation The key derivation operation object to use.
3162 * It must have been set up with
3163 * psa_key_derivation_setup() and must not
3164 * have produced any output yet.
3165 * \param step Which step the input data is for.
3166 * \param[in] data Input data to use.
3167 * \param data_length Size of the \p data buffer in bytes.
3168 *
3169 * \retval #PSA_SUCCESS
3170 * Success.
3171 * \retval #PSA_ERROR_INVALID_ARGUMENT
3172 * \c step is not compatible with the operation's algorithm.
3173 * \retval #PSA_ERROR_INVALID_ARGUMENT
3174 * \c step does not allow direct inputs.
3175 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3176 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3177 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003178 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003179 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003180 * \retval #PSA_ERROR_BAD_STATE
3181 * The value of \p step is not valid given the state of \p operation.
3182 * \retval #PSA_ERROR_BAD_STATE
3183 * The library has not been previously initialized by psa_crypto_init().
3184 * It is implementation-dependent whether a failure to initialize
3185 * results in this error code.
3186 */
3187psa_status_t psa_key_derivation_input_bytes(
3188 psa_key_derivation_operation_t *operation,
3189 psa_key_derivation_step_t step,
3190 const uint8_t *data,
3191 size_t data_length);
3192
3193/** Provide an input for key derivation in the form of a key.
3194 *
3195 * Which inputs are required and in what order depends on the algorithm.
3196 * Refer to the documentation of each key derivation or key agreement
3197 * algorithm for information.
3198 *
3199 * This function passes key inputs. Some inputs must be passed as keys
3200 * of the appropriate type using this function, while others must be
3201 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3202 * the documentation of individual step types for information.
3203 *
3204 * \param[in,out] operation The key derivation operation object to use.
3205 * It must have been set up with
3206 * psa_key_derivation_setup() and must not
3207 * have produced any output yet.
3208 * \param step Which step the input data is for.
3209 * \param handle Handle to the key. It must have an
3210 * appropriate type for \p step and must
3211 * allow the usage #PSA_KEY_USAGE_DERIVE.
3212 *
3213 * \retval #PSA_SUCCESS
3214 * Success.
3215 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003216 * \retval #PSA_ERROR_NOT_PERMITTED
3217 * \retval #PSA_ERROR_INVALID_ARGUMENT
3218 * \c step is not compatible with the operation's algorithm.
3219 * \retval #PSA_ERROR_INVALID_ARGUMENT
3220 * \c step does not allow key inputs.
3221 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3222 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3223 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003224 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003225 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003226 * \retval #PSA_ERROR_BAD_STATE
3227 * The value of \p step is not valid given the state of \p operation.
3228 * \retval #PSA_ERROR_BAD_STATE
3229 * The library has not been previously initialized by psa_crypto_init().
3230 * It is implementation-dependent whether a failure to initialize
3231 * results in this error code.
3232 */
3233psa_status_t psa_key_derivation_input_key(
3234 psa_key_derivation_operation_t *operation,
3235 psa_key_derivation_step_t step,
3236 psa_key_handle_t handle);
3237
3238/** Perform a key agreement and use the shared secret as input to a key
3239 * derivation.
3240 *
3241 * A key agreement algorithm takes two inputs: a private key \p private_key
3242 * a public key \p peer_key.
3243 * The result of this function is passed as input to a key derivation.
3244 * The output of this key derivation can be extracted by reading from the
3245 * resulting operation to produce keys and other cryptographic material.
3246 *
3247 * \param[in,out] operation The key derivation operation object to use.
3248 * It must have been set up with
3249 * psa_key_derivation_setup() with a
3250 * key agreement and derivation algorithm
3251 * \c alg (\c PSA_ALG_XXX value such that
3252 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3253 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3254 * is false).
3255 * The operation must be ready for an
3256 * input of the type given by \p step.
3257 * \param step Which step the input data is for.
3258 * \param private_key Handle to the private key to use.
3259 * \param[in] peer_key Public key of the peer. The peer key must be in the
3260 * same format that psa_import_key() accepts for the
3261 * public key type corresponding to the type of
3262 * private_key. That is, this function performs the
3263 * equivalent of
3264 * #psa_import_key(...,
3265 * `peer_key`, `peer_key_length`) where
3266 * with key attributes indicating the public key
3267 * type corresponding to the type of `private_key`.
3268 * For example, for EC keys, this means that peer_key
3269 * is interpreted as a point on the curve that the
3270 * private key is on. The standard formats for public
3271 * keys are documented in the documentation of
3272 * psa_export_public_key().
3273 * \param peer_key_length Size of \p peer_key in bytes.
3274 *
3275 * \retval #PSA_SUCCESS
3276 * Success.
3277 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003278 * \retval #PSA_ERROR_NOT_PERMITTED
3279 * \retval #PSA_ERROR_INVALID_ARGUMENT
3280 * \c private_key is not compatible with \c alg,
3281 * or \p peer_key is not valid for \c alg or not compatible with
3282 * \c private_key.
3283 * \retval #PSA_ERROR_NOT_SUPPORTED
3284 * \c alg is not supported or is not a key derivation algorithm.
3285 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3286 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3287 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003288 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003289 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003290 * \retval #PSA_ERROR_BAD_STATE
3291 * The library has not been previously initialized by psa_crypto_init().
3292 * It is implementation-dependent whether a failure to initialize
3293 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003294 */
3295psa_status_t psa_key_derivation_key_agreement(
3296 psa_key_derivation_operation_t *operation,
3297 psa_key_derivation_step_t step,
3298 psa_key_handle_t private_key,
3299 const uint8_t *peer_key,
3300 size_t peer_key_length);
3301
Gilles Peskine35675b62019-05-16 17:26:11 +02003302/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003303 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003304 * This function calculates output bytes from a key derivation algorithm and
3305 * return those bytes.
3306 * If you view the key derivation's output as a stream of bytes, this
3307 * function destructively reads the requested number of bytes from the
3308 * stream.
3309 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003310 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003311 * \param[in,out] operation The key derivation operation object to read from.
3312 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003313 * \param output_length Number of bytes to output.
3314 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003315 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003316 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003317 * The operation's capacity was less than
3318 * \p output_length bytes. Note that in this case,
3319 * no output is written to the output buffer.
3320 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003321 * subsequent calls to this function will not
3322 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003323 * \retval #PSA_ERROR_BAD_STATE
3324 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3325 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3326 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003327 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003328 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003329 * \retval #PSA_ERROR_BAD_STATE
3330 * The library has not been previously initialized by psa_crypto_init().
3331 * It is implementation-dependent whether a failure to initialize
3332 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003333 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003334psa_status_t psa_key_derivation_output_bytes(
3335 psa_key_derivation_operation_t *operation,
3336 uint8_t *output,
3337 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003338
Gilles Peskine35675b62019-05-16 17:26:11 +02003339/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003340 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003341 * This function calculates output bytes from a key derivation algorithm
3342 * and uses those bytes to generate a key deterministically.
3343 * If you view the key derivation's output as a stream of bytes, this
3344 * function destructively reads as many bytes as required from the
3345 * stream.
3346 * The operation's capacity decreases by the number of bytes read.
3347 *
3348 * How much output is produced and consumed from the operation, and how
3349 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003350 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003351 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003352 * of a given size, this function is functionally equivalent to
3353 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003354 * and passing the resulting output to #psa_import_key.
3355 * However, this function has a security benefit:
3356 * if the implementation provides an isolation boundary then
3357 * the key material is not exposed outside the isolation boundary.
3358 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003359 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003360 * The following key types defined in this specification follow this scheme:
3361 *
3362 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003363 * - #PSA_KEY_TYPE_ARC4;
3364 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003365 * - #PSA_KEY_TYPE_DERIVE;
3366 * - #PSA_KEY_TYPE_HMAC.
3367 *
3368 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003369 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003370 * Montgomery curve), this function always draws a byte string whose
3371 * length is determined by the curve, and sets the mandatory bits
3372 * accordingly. That is:
3373 *
3374 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3375 * and process it as specified in RFC 7748 &sect;5.
3376 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3377 * and process it as specified in RFC 7748 &sect;5.
3378 *
3379 * - For key types for which the key is represented by a single sequence of
3380 * \p bits bits with constraints as to which bit sequences are acceptable,
3381 * this function draws a byte string of length (\p bits / 8) bytes rounded
3382 * up to the nearest whole number of bytes. If the resulting byte string
3383 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3384 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003385 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003386 * for the output produced by psa_export_key().
3387 * The following key types defined in this specification follow this scheme:
3388 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003389 * - #PSA_KEY_TYPE_DES.
3390 * Force-set the parity bits, but discard forbidden weak keys.
3391 * For 2-key and 3-key triple-DES, the three keys are generated
3392 * successively (for example, for 3-key triple-DES,
3393 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3394 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003395 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003396 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003397 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003398 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003399 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003400 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003401 * Weierstrass curve).
3402 * For these key types, interpret the byte string as integer
3403 * in big-endian order. Discard it if it is not in the range
3404 * [0, *N* - 2] where *N* is the boundary of the private key domain
3405 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003406 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003407 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003408 * This method allows compliance to NIST standards, specifically
3409 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003410 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3411 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3412 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3413 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003414 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003415 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003416 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003417 * implementation-defined.
3418 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003419 * In all cases, the data that is read is discarded from the operation.
3420 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003421 *
Gilles Peskine20628592019-04-19 19:29:50 +02003422 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003423 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003424 * \param[out] handle On success, a handle to the newly created key.
3425 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003426 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003427 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003428 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003429 * If the key is persistent, the key material and the key's metadata
3430 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003431 * \retval #PSA_ERROR_ALREADY_EXISTS
3432 * This is an attempt to create a persistent key, and there is
3433 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003434 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003435 * There was not enough data to create the desired key.
3436 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003437 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003438 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003439 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003440 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003441 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003442 * \retval #PSA_ERROR_INVALID_ARGUMENT
3443 * The provided key attributes are not valid for the operation.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003444 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003445 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3446 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3447 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3448 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003449 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003450 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003451 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003452 * The library has not been previously initialized by psa_crypto_init().
3453 * It is implementation-dependent whether a failure to initialize
3454 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003455 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003456psa_status_t psa_key_derivation_output_key(
3457 const psa_key_attributes_t *attributes,
3458 psa_key_derivation_operation_t *operation,
3459 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003460
Gilles Peskine35675b62019-05-16 17:26:11 +02003461/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003462 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003463 * Once a key derivation operation has been aborted, its capacity is zero.
3464 * Aborting an operation frees all associated resources except for the
3465 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003466 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003467 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003468 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003469 * psa_key_derivation_operation_init() or a zero value. In particular,
3470 * it is valid to call psa_key_derivation_abort() twice, or to call
3471 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003472 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003473 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003474 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003475 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003476 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003477 * \retval #PSA_SUCCESS
3478 * \retval #PSA_ERROR_BAD_STATE
3479 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3480 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003481 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003482 * \retval #PSA_ERROR_BAD_STATE
3483 * The library has not been previously initialized by psa_crypto_init().
3484 * It is implementation-dependent whether a failure to initialize
3485 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003486 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003487psa_status_t psa_key_derivation_abort(
3488 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003489
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003490/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003491 *
3492 * \warning The raw result of a key agreement algorithm such as finite-field
3493 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3494 * not be used directly as key material. It should instead be passed as
3495 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003496 * a key derivation, use psa_key_derivation_key_agreement() and other
3497 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003498 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003499 * \param alg The key agreement algorithm to compute
3500 * (\c PSA_ALG_XXX value such that
3501 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3502 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003503 * \param private_key Handle to the private key to use.
3504 * \param[in] peer_key Public key of the peer. It must be
3505 * in the same format that psa_import_key()
3506 * accepts. The standard formats for public
3507 * keys are documented in the documentation
3508 * of psa_export_public_key().
3509 * \param peer_key_length Size of \p peer_key in bytes.
3510 * \param[out] output Buffer where the decrypted message is to
3511 * be written.
3512 * \param output_size Size of the \c output buffer in bytes.
3513 * \param[out] output_length On success, the number of bytes
3514 * that make up the returned output.
3515 *
3516 * \retval #PSA_SUCCESS
3517 * Success.
3518 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003519 * \retval #PSA_ERROR_NOT_PERMITTED
3520 * \retval #PSA_ERROR_INVALID_ARGUMENT
3521 * \p alg is not a key agreement algorithm
3522 * \retval #PSA_ERROR_INVALID_ARGUMENT
3523 * \p private_key is not compatible with \p alg,
3524 * or \p peer_key is not valid for \p alg or not compatible with
3525 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003526 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3527 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003528 * \retval #PSA_ERROR_NOT_SUPPORTED
3529 * \p alg is not a supported key agreement algorithm.
3530 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3531 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3532 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003533 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003534 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003535 * \retval #PSA_ERROR_BAD_STATE
3536 * The library has not been previously initialized by psa_crypto_init().
3537 * It is implementation-dependent whether a failure to initialize
3538 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003539 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003540psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3541 psa_key_handle_t private_key,
3542 const uint8_t *peer_key,
3543 size_t peer_key_length,
3544 uint8_t *output,
3545 size_t output_size,
3546 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003547
Gilles Peskineea0fb492018-07-12 17:17:20 +02003548/**@}*/
3549
Gilles Peskineedd76872018-07-20 17:42:05 +02003550/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003551 * @{
3552 */
3553
3554/**
3555 * \brief Generate random bytes.
3556 *
3557 * \warning This function **can** fail! Callers MUST check the return status
3558 * and MUST NOT use the content of the output buffer if the return
3559 * status is not #PSA_SUCCESS.
3560 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003561 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003562 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003563 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003564 * \param output_size Number of bytes to generate and output.
3565 *
Gilles Peskine28538492018-07-11 17:34:00 +02003566 * \retval #PSA_SUCCESS
3567 * \retval #PSA_ERROR_NOT_SUPPORTED
3568 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003569 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003570 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3571 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003572 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003573 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003574 * The library has not been previously initialized by psa_crypto_init().
3575 * It is implementation-dependent whether a failure to initialize
3576 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003577 */
3578psa_status_t psa_generate_random(uint8_t *output,
3579 size_t output_size);
3580
3581/**
3582 * \brief Generate a key or key pair.
3583 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003584 * The key is generated randomly.
3585 * Its location, policy, type and size are taken from \p attributes.
3586 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003587 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003588 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003589 * the public exponent is 65537.
3590 * The modulus is a product of two probabilistic primes
3591 * between 2^{n-1} and 2^n where n is the bit size specified in the
3592 * attributes.
3593 *
Gilles Peskine20628592019-04-19 19:29:50 +02003594 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003595 * \param[out] handle On success, a handle to the newly created key.
3596 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003597 *
Gilles Peskine28538492018-07-11 17:34:00 +02003598 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003599 * Success.
3600 * If the key is persistent, the key material and the key's metadata
3601 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003602 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003603 * This is an attempt to create a persistent key, and there is
3604 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003605 * \retval #PSA_ERROR_NOT_SUPPORTED
3606 * \retval #PSA_ERROR_INVALID_ARGUMENT
3607 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3608 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3609 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3610 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003611 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003612 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3613 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003614 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003615 * The library has not been previously initialized by psa_crypto_init().
3616 * It is implementation-dependent whether a failure to initialize
3617 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003618 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003619psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003620 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003621
3622/**@}*/
3623
Gilles Peskinee59236f2018-01-27 23:32:46 +01003624#ifdef __cplusplus
3625}
3626#endif
3627
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003628/* The file "crypto_sizes.h" contains definitions for size calculation
3629 * macros whose definitions are implementation-specific. */
3630#include "crypto_sizes.h"
3631
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003632/* The file "crypto_struct.h" contains definitions for
3633 * implementation-specific structs that are declared above. */
3634#include "crypto_struct.h"
3635
3636/* The file "crypto_extra.h" contains vendor-specific definitions. This
3637 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003638#include "crypto_extra.h"
3639
3640#endif /* PSA_CRYPTO_H */