blob: 71bad3b7a28dd7de37049e449ff9020d07eda211 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +020089 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +020090 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
Gilles Peskine105f67f2019-07-23 18:16:05 +020096/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +020097 * @{
98 */
99
Gilles Peskinea0c06552019-05-21 15:54:54 +0200100/** \def PSA_KEY_ATTRIBUTES_INIT
101 *
102 * This macro returns a suitable initializer for a key attribute structure
103 * of type #psa_key_attributes_t.
104 */
105#ifdef __DOXYGEN_ONLY__
106/* This is an example definition for documentation purposes.
107 * Implementations should define a suitable value in `crypto_struct.h`.
108 */
109#define PSA_KEY_ATTRIBUTES_INIT {0}
110#endif
111
112/** Return an initial value for a key attributes structure.
113 */
114static psa_key_attributes_t psa_key_attributes_init(void);
115
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200116/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200117 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200118 * If the attribute structure currently declares the key as volatile (which
119 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200120 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200121 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200122 * This function does not access storage, it merely stores the given
123 * value in the structure.
124 * The persistent key will be written to storage when the attribute
125 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200126 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200127 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200128 *
Gilles Peskine20628592019-04-19 19:29:50 +0200129 * This function may be declared as `static` (i.e. without external
130 * linkage). This function may be provided as a function-like macro,
131 * but in this case it must evaluate each of its arguments exactly once.
132 *
133 * \param[out] attributes The attribute structure to write to.
134 * \param id The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200135 */
136static void psa_set_key_id(psa_key_attributes_t *attributes,
137 psa_key_id_t id);
138
139/** Set the location of a persistent key.
140 *
141 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200142 * with psa_set_key_id(). By default, a key that has a persistent identifier
143 * is stored in the default storage area identifier by
144 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
145 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200146 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200147 * This function does not access storage, it merely stores the given
148 * value in the structure.
149 * The persistent key will be written to storage when the attribute
150 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200151 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200152 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200153 *
154 * This function may be declared as `static` (i.e. without external
155 * linkage). This function may be provided as a function-like macro,
156 * but in this case it must evaluate each of its arguments exactly once.
157 *
158 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200159 * \param lifetime The lifetime for the key.
160 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200161 * key will be volatile, and the key identifier
162 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200163 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200164static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
165 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200166
Gilles Peskine20628592019-04-19 19:29:50 +0200167/** Retrieve the key identifier from key attributes.
168 *
169 * This function may be declared as `static` (i.e. without external
170 * linkage). This function may be provided as a function-like macro,
171 * but in this case it must evaluate its argument exactly once.
172 *
173 * \param[in] attributes The key attribute structure to query.
174 *
175 * \return The persistent identifier stored in the attribute structure.
176 * This value is unspecified if the attribute structure declares
177 * the key as volatile.
178 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200179static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
180
Gilles Peskine20628592019-04-19 19:29:50 +0200181/** Retrieve the lifetime from key attributes.
182 *
183 * This function may be declared as `static` (i.e. without external
184 * linkage). This function may be provided as a function-like macro,
185 * but in this case it must evaluate its argument exactly once.
186 *
187 * \param[in] attributes The key attribute structure to query.
188 *
189 * \return The lifetime value stored in the attribute structure.
190 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200191static psa_key_lifetime_t psa_get_key_lifetime(
192 const psa_key_attributes_t *attributes);
193
Gilles Peskine20628592019-04-19 19:29:50 +0200194/** Declare usage flags for a key.
195 *
196 * Usage flags are part of a key's usage policy. They encode what
197 * kind of operations are permitted on the key. For more details,
198 * refer to the documentation of the type #psa_key_usage_t.
199 *
200 * This function overwrites any usage flags
201 * previously set in \p attributes.
202 *
203 * This function may be declared as `static` (i.e. without external
204 * linkage). This function may be provided as a function-like macro,
205 * but in this case it must evaluate each of its arguments exactly once.
206 *
207 * \param[out] attributes The attribute structure to write to.
208 * \param usage_flags The usage flags to write.
209 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200210static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
211 psa_key_usage_t usage_flags);
212
Gilles Peskine20628592019-04-19 19:29:50 +0200213/** Retrieve the usage flags from key attributes.
214 *
215 * This function may be declared as `static` (i.e. without external
216 * linkage). This function may be provided as a function-like macro,
217 * but in this case it must evaluate its argument exactly once.
218 *
219 * \param[in] attributes The key attribute structure to query.
220 *
221 * \return The usage flags stored in the attribute structure.
222 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200223static psa_key_usage_t psa_get_key_usage_flags(
224 const psa_key_attributes_t *attributes);
225
Gilles Peskine20628592019-04-19 19:29:50 +0200226/** Declare the permitted algorithm policy for a key.
227 *
228 * The permitted algorithm policy of a key encodes which algorithm or
229 * algorithms are permitted to be used with this key.
230 *
231 * This function overwrites any algorithm policy
232 * previously set in \p attributes.
233 *
234 * This function may be declared as `static` (i.e. without external
235 * linkage). This function may be provided as a function-like macro,
236 * but in this case it must evaluate each of its arguments exactly once.
237 *
238 * \param[out] attributes The attribute structure to write to.
239 * \param alg The permitted algorithm policy to write.
240 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200241static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
242 psa_algorithm_t alg);
243
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100244
Gilles Peskine20628592019-04-19 19:29:50 +0200245/** Retrieve the algorithm policy from key attributes.
246 *
247 * This function may be declared as `static` (i.e. without external
248 * linkage). This function may be provided as a function-like macro,
249 * but in this case it must evaluate its argument exactly once.
250 *
251 * \param[in] attributes The key attribute structure to query.
252 *
253 * \return The algorithm stored in the attribute structure.
254 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200255static psa_algorithm_t psa_get_key_algorithm(
256 const psa_key_attributes_t *attributes);
257
Gilles Peskine20628592019-04-19 19:29:50 +0200258/** Declare the type of a key.
259 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200260 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200261 * previously set in \p attributes.
262 *
263 * This function may be declared as `static` (i.e. without external
264 * linkage). This function may be provided as a function-like macro,
265 * but in this case it must evaluate each of its arguments exactly once.
266 *
267 * \param[out] attributes The attribute structure to write to.
268 * \param type The key type to write.
269 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200270static void psa_set_key_type(psa_key_attributes_t *attributes,
271 psa_key_type_t type);
272
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100273
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200274/** Declare the size of a key.
275 *
276 * This function overwrites any key size previously set in \p attributes.
277 *
278 * This function may be declared as `static` (i.e. without external
279 * linkage). This function may be provided as a function-like macro,
280 * but in this case it must evaluate each of its arguments exactly once.
281 *
282 * \param[out] attributes The attribute structure to write to.
283 * \param bits The key size in bits.
284 */
285static void psa_set_key_bits(psa_key_attributes_t *attributes,
286 size_t bits);
287
Gilles Peskine20628592019-04-19 19:29:50 +0200288/** Retrieve the key type from key attributes.
289 *
290 * This function may be declared as `static` (i.e. without external
291 * linkage). This function may be provided as a function-like macro,
292 * but in this case it must evaluate its argument exactly once.
293 *
294 * \param[in] attributes The key attribute structure to query.
295 *
296 * \return The key type stored in the attribute structure.
297 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200298static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
299
Gilles Peskine20628592019-04-19 19:29:50 +0200300/** Retrieve the key size from key attributes.
301 *
302 * This function may be declared as `static` (i.e. without external
303 * linkage). This function may be provided as a function-like macro,
304 * but in this case it must evaluate its argument exactly once.
305 *
306 * \param[in] attributes The key attribute structure to query.
307 *
308 * \return The key size stored in the attribute structure, in bits.
309 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200310static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
311
Gilles Peskine20628592019-04-19 19:29:50 +0200312/** Retrieve the attributes of a key.
313 *
314 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200315 * psa_reset_key_attributes(). It then copies the attributes of
316 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200317 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200318 * \note This function may allocate memory or other resources.
319 * Once you have called this function on an attribute structure,
320 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200321 *
Gilles Peskine20628592019-04-19 19:29:50 +0200322 * \param[in] handle Handle to the key to query.
323 * \param[in,out] attributes On success, the attributes of the key.
324 * On failure, equivalent to a
325 * freshly-initialized structure.
326 *
327 * \retval #PSA_SUCCESS
328 * \retval #PSA_ERROR_INVALID_HANDLE
329 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
330 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100331 * \retval #PSA_ERROR_CORRUPTION_DETECTED
332 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100333 * \retval #PSA_ERROR_BAD_STATE
334 * The library has not been previously initialized by psa_crypto_init().
335 * It is implementation-dependent whether a failure to initialize
336 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200337 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200338psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
339 psa_key_attributes_t *attributes);
340
Gilles Peskine20628592019-04-19 19:29:50 +0200341/** Reset a key attribute structure to a freshly initialized state.
342 *
343 * You must initialize the attribute structure as described in the
344 * documentation of the type #psa_key_attributes_t before calling this
345 * function. Once the structure has been initialized, you may call this
346 * function at any time.
347 *
348 * This function frees any auxiliary resources that the structure
349 * may contain.
350 *
351 * \param[in,out] attributes The attribute structure to reset.
352 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200353void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200354
Gilles Peskine87a5e562019-04-17 12:28:25 +0200355/**@}*/
356
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357/** \defgroup key_management Key management
358 * @{
359 */
360
Gilles Peskinef535eb22018-11-30 14:08:36 +0100361/** Open a handle to an existing persistent key.
362 *
Gilles Peskine4754cde2019-05-21 15:56:29 +0200363 * Open a handle to a persistent key. A key is persistent if it was created
364 * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
365 * always has a nonzero key identifier, set with psa_set_key_id() when
366 * creating the key. Implementations may provide additional pre-provisioned
Andrew Thoelke203491c2019-08-21 17:55:30 +0100367 * keys that can be opened with psa_open_key(). Such keys have a key identifier
368 * in the vendor range, as documented in the description of #psa_key_id_t.
Gilles Peskine4754cde2019-05-21 15:56:29 +0200369 *
Andrew Thoelkede183412019-09-05 09:38:06 +0100370 * The application must eventually close the handle with psa_close_key() or
371 * psa_destroy_key() to release associated resources. If the application dies
372 * without calling one of these functions, the implementation should perform
373 * the equivalent of a call to psa_close_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100374 *
Andrew Thoelke9741b112019-08-21 18:20:41 +0100375 * Some implementations permit an application to open the same key multiple
Andrew Thoelkede183412019-09-05 09:38:06 +0100376 * times. If this is successful, each call to psa_open_key() will return a
377 * different key handle.
378 *
379 * \note Applications that rely on opening a key multiple times will not be
380 * portable to implementations that only permit a single key handle to be
381 * opened. See also :ref:\`key-handles\`.
Andrew Thoelke9741b112019-08-21 18:20:41 +0100382 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100383 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100384 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100385 *
386 * \retval #PSA_SUCCESS
387 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100388 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100389 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Andrew Thoelke9741b112019-08-21 18:20:41 +0100390 * The implementation does not have sufficient resources to open the
391 * key. This can be due to reaching an implementation limit on the
392 * number of open keys, the number of open key handles, or available
393 * memory.
David Saadab4ecc272019-02-14 13:48:10 +0200394 * \retval #PSA_ERROR_DOES_NOT_EXIST
Andrew Thoelke9741b112019-08-21 18:20:41 +0100395 * There is no persistent key with key identifier \p id.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100396 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrew Thoelke9741b112019-08-21 18:20:41 +0100397 * \p id is not a valid persistent key identifier.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100398 * \retval #PSA_ERROR_NOT_PERMITTED
399 * The specified key exists, but the application does not have the
400 * permission to access it. Note that this specification does not
401 * define any way to create such a key, but it may be possible
402 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200403 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawd789dc12019-08-12 15:06:48 +0100404 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine225010f2019-05-06 18:44:55 +0200405 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100406 * \retval #PSA_ERROR_BAD_STATE
407 * The library has not been previously initialized by psa_crypto_init().
408 * It is implementation-dependent whether a failure to initialize
409 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100410 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200411psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100412 psa_key_handle_t *handle);
413
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100414
Gilles Peskinef535eb22018-11-30 14:08:36 +0100415/** Close a key handle.
416 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100417 * If the handle designates a volatile key, this will destroy the key material
418 * and free all associated resources, just like psa_destroy_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100419 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100420 * If this is the last open handle to a persistent key, then closing the handle
421 * will free all resources associated with the key in volatile memory. The key
422 * data in persistent storage is not affected and can be opened again later
423 * with a call to psa_open_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100424 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100425 * Closing the key handle makes the handle invalid, and the key handle
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100426 * must not be used again by the application.
Andrew Thoelke3daba812019-08-21 22:46:56 +0100427 *
Andrew Thoelke970629f2019-09-09 09:56:34 +0100428 * \note If the key handle was used to set up an active
Andrew Thoelkede183412019-09-05 09:38:06 +0100429 * :ref:\`multipart operation <multipart-operations>\`, then closing the
430 * key handle can cause the multipart operation to fail. Applications should
431 * maintain the key handle until after the multipart operation has finished.
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100432 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100433 * \param handle The key handle to close.
434 *
435 * \retval #PSA_SUCCESS
436 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100437 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawf97c8522019-08-15 13:27:12 +0100438 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100439 * \retval #PSA_ERROR_BAD_STATE
440 * The library has not been previously initialized by psa_crypto_init().
441 * It is implementation-dependent whether a failure to initialize
442 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100443 */
444psa_status_t psa_close_key(psa_key_handle_t handle);
445
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100446/**@}*/
447
448/** \defgroup import_export Key import and export
449 * @{
450 */
451
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100452/**
453 * \brief Import a key in binary format.
454 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100455 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100456 * documentation of psa_export_public_key() for the format of public keys
457 * and to the documentation of psa_export_key() for the format for
458 * other key types.
459 *
460 * This specification supports a single format for each key type.
461 * Implementations may support other formats as long as the standard
462 * format is supported. Implementations that support other formats
463 * should ensure that the formats are clearly unambiguous so as to
464 * minimize the risk that an invalid input is accidentally interpreted
465 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100466 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100467
Gilles Peskine20628592019-04-19 19:29:50 +0200468 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200469 * The key size is always determined from the
470 * \p data buffer.
471 * If the key size in \p attributes is nonzero,
472 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200473 * \param[out] handle On success, a handle to the newly created key.
474 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100475 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200476 * buffer is interpreted according to the type declared
477 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200478 * All implementations must support at least the format
479 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100480 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200481 * the chosen type. Implementations may allow other
482 * formats, but should be conservative: implementations
483 * should err on the side of rejecting content if it
484 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200485 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100486 *
Gilles Peskine28538492018-07-11 17:34:00 +0200487 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100488 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100489 * If the key is persistent, the key material and the key's metadata
490 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200491 * \retval #PSA_ERROR_ALREADY_EXISTS
492 * This is an attempt to create a persistent key, and there is
493 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200494 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200495 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200496 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200497 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200498 * The key attributes, as a whole, are invalid.
499 * \retval #PSA_ERROR_INVALID_ARGUMENT
500 * The key data is not correctly formatted.
501 * \retval #PSA_ERROR_INVALID_ARGUMENT
502 * The size in \p attributes is nonzero and does not match the size
503 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200504 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
505 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
506 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100507 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200508 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200509 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300510 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw97d3bc32019-08-08 14:40:04 +0100511 * \p operation is either not initialized or is in use
512 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300513 * The library has not been previously initialized by psa_crypto_init().
514 * It is implementation-dependent whether a failure to initialize
515 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100516 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200517psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100518 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200519 size_t data_length,
520 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100521
522/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100523 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200524 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100525 * This function destroys a key from both volatile
Gilles Peskine154bd952018-04-19 08:38:16 +0200526 * memory and, if applicable, non-volatile storage. Implementations shall
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100527 * make a best effort to ensure that that the key material cannot be recovered.
Gilles Peskine154bd952018-04-19 08:38:16 +0200528 *
Andrew Thoelkede183412019-09-05 09:38:06 +0100529 * This function also erases any metadata such as policies and frees
530 * resources associated with the key. To free all resources associated with
531 * the key, all handles to the key must be closed or destroyed.
Gilles Peskine154bd952018-04-19 08:38:16 +0200532 *
Andrew Thoelkede183412019-09-05 09:38:06 +0100533 * Destroying the key makes the handle invalid, and the key handle
534 * must not be used again by the application. Using other open handles to the
535 * destroyed key in a cryptographic operation will result in an error.
Andrew Thoelke07f16b72019-08-21 22:48:47 +0100536 *
Andrew Thoelkede183412019-09-05 09:38:06 +0100537 * If a key is currently in use in a multipart operation, then destroying the
538 * key will cause the multipart operation to fail.
Andrew Thoelke07f16b72019-08-21 22:48:47 +0100539 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100540 * \param handle Handle to the key to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100541 *
Gilles Peskine28538492018-07-11 17:34:00 +0200542 * \retval #PSA_SUCCESS
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100543 * The key material has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200544 * \retval #PSA_ERROR_NOT_PERMITTED
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100545 * The key cannot be erased because it is
Gilles Peskine65eb8582018-04-19 08:28:58 +0200546 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100547 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200548 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200549 * There was an failure in communication with the cryptoprocessor.
550 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200551 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200552 * The storage is corrupted. Implementations shall make a best effort
553 * to erase key material even in this stage, however applications
554 * should be aware that it may be impossible to guarantee that the
555 * key material is not recoverable in such cases.
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200556 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200557 * An unexpected condition which is not a storage corruption or
558 * a communication failure occurred. The cryptoprocessor may have
559 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300560 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300561 * The library has not been previously initialized by psa_crypto_init().
562 * It is implementation-dependent whether a failure to initialize
563 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100564 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100565psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100566
567/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568 * \brief Export a key in binary format.
569 *
570 * The output of this function can be passed to psa_import_key() to
571 * create an equivalent object.
572 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100573 * If the implementation of psa_import_key() supports other formats
574 * beyond the format specified here, the output from psa_export_key()
575 * must use the representation specified here, not the original
576 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100577 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100578 * For standard key types, the output format is as follows:
579 *
580 * - For symmetric keys (including MAC keys), the format is the
581 * raw bytes of the key.
582 * - For DES, the key data consists of 8 bytes. The parity bits must be
583 * correct.
584 * - For Triple-DES, the format is the concatenation of the
585 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200586 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200587 * is the non-encrypted DER encoding of the representation defined by
588 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
589 * ```
590 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200591 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200592 * modulus INTEGER, -- n
593 * publicExponent INTEGER, -- e
594 * privateExponent INTEGER, -- d
595 * prime1 INTEGER, -- p
596 * prime2 INTEGER, -- q
597 * exponent1 INTEGER, -- d mod (p-1)
598 * exponent2 INTEGER, -- d mod (q-1)
599 * coefficient INTEGER, -- (inverse of q) mod p
600 * }
601 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200602 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200603 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100604 * a representation of the private value as a `ceiling(m/8)`-byte string
605 * where `m` is the bit size associated with the curve, i.e. the bit size
606 * of the order of the curve's coordinate field. This byte string is
607 * in little-endian order for Montgomery curves (curve types
608 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
609 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
610 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100611 * This is the content of the `privateKey` field of the `ECPrivateKey`
612 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200613 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200614 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000615 * format is the representation of the private key `x` as a big-endian byte
616 * string. The length of the byte string is the private key size in bytes
617 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200618 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
619 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100620 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200621 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
622 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100623 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200624 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200625 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200626 * \param[out] data_length On success, the number of bytes
627 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100628 *
Gilles Peskine28538492018-07-11 17:34:00 +0200629 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100630 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200631 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200632 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100633 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200634 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
635 * The size of the \p data buffer is too small. You can determine a
636 * sufficient buffer size by calling
637 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
638 * where \c type is the key type
639 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200640 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
641 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200642 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100643 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100644 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300645 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300646 * The library has not been previously initialized by psa_crypto_init().
647 * It is implementation-dependent whether a failure to initialize
648 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100649 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100650psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100651 uint8_t *data,
652 size_t data_size,
653 size_t *data_length);
654
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100655/**
656 * \brief Export a public key or the public part of a key pair in binary format.
657 *
658 * The output of this function can be passed to psa_import_key() to
659 * create an object that is equivalent to the public key.
660 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000661 * This specification supports a single format for each key type.
662 * Implementations may support other formats as long as the standard
663 * format is supported. Implementations that support other formats
664 * should ensure that the formats are clearly unambiguous so as to
665 * minimize the risk that an invalid input is accidentally interpreted
666 * according to a different format.
667 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000668 * For standard key types, the output format is as follows:
669 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
670 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
671 * ```
672 * RSAPublicKey ::= SEQUENCE {
673 * modulus INTEGER, -- n
674 * publicExponent INTEGER } -- e
675 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000676 * - For elliptic curve public keys (key types for which
677 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
678 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
679 * Let `m` be the bit size associated with the curve, i.e. the bit size of
680 * `q` for a curve over `F_q`. The representation consists of:
681 * - The byte 0x04;
682 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
683 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200684 * - For Diffie-Hellman key exchange public keys (key types for which
685 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000686 * the format is the representation of the public key `y = g^x mod p` as a
687 * big-endian byte string. The length of the byte string is the length of the
688 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100689 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200690 * Exporting a public key object or the public part of a key pair is
691 * always permitted, regardless of the key's usage flags.
692 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100693 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200694 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200695 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200696 * \param[out] data_length On success, the number of bytes
697 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100698 *
Gilles Peskine28538492018-07-11 17:34:00 +0200699 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100700 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200701 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200702 * The key is neither a public key nor a key pair.
703 * \retval #PSA_ERROR_NOT_SUPPORTED
704 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
705 * The size of the \p data buffer is too small. You can determine a
706 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200707 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200708 * where \c type is the key type
709 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200710 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
711 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200712 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100713 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100714 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300715 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300716 * The library has not been previously initialized by psa_crypto_init().
717 * It is implementation-dependent whether a failure to initialize
718 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100719 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100720psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100721 uint8_t *data,
722 size_t data_size,
723 size_t *data_length);
724
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100725/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100726 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100727 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000728 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100729 * This function is primarily useful to copy a key from one location
730 * to another, since it populates a key using the material from
731 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200732 *
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100733 * This function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100734 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100735 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200736 * The policy on the source key must have the usage flag
737 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200738 * This flag is sufficient to permit the copy if the key has the lifetime
739 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
740 * Some secure elements do not provide a way to copy a key without
741 * making it extractable from the secure element. If a key is located
742 * in such a secure element, then the key must have both usage flags
743 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
744 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200745 *
Gilles Peskine20628592019-04-19 19:29:50 +0200746 * The resulting key may only be used in a way that conforms to
747 * both the policy of the original key and the policy specified in
748 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100749 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200750 * usage flags on the source policy and the usage flags in \p attributes.
751 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100752 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200753 * - If either of the policies allows an algorithm and the other policy
754 * allows a wildcard-based algorithm policy that includes this algorithm,
755 * the resulting key allows the same algorithm.
756 * - If the policies do not allow any algorithm in common, this function
757 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200758 *
Gilles Peskine20628592019-04-19 19:29:50 +0200759 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100760 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200761 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100762 * \param source_handle The key to copy. It must be a valid key handle.
Gilles Peskine20628592019-04-19 19:29:50 +0200763 * \param[in] attributes The attributes for the new key.
764 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200765 * - The key type and size may be 0. If either is
766 * nonzero, it must match the corresponding
767 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200768 * - The key location (the lifetime and, for
769 * persistent keys, the key identifier) is
770 * used directly.
771 * - The policy constraints (usage flags and
772 * algorithm policy) are combined from
773 * the source key and \p attributes so that
774 * both sets of restrictions apply, as
775 * described in the documentation of this function.
776 * \param[out] target_handle On success, a handle to the newly created key.
777 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200778 *
779 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100780 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200781 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200782 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200783 * This is an attempt to create a persistent key, and there is
784 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200785 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200786 * The lifetime or identifier in \p attributes are invalid.
787 * \retval #PSA_ERROR_INVALID_ARGUMENT
788 * The policy constraints on the source and specified in
789 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200790 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200791 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200792 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100793 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200794 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
795 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100796 * The source key is not exportable and its lifetime does not
797 * allow copying it to the target's lifetime.
798 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
799 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200800 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
801 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shaw60b03202019-08-06 17:26:16 +0100802 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200803 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100804 * \retval #PSA_ERROR_BAD_STATE
805 * The library has not been previously initialized by psa_crypto_init().
806 * It is implementation-dependent whether a failure to initialize
807 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100808 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100809psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200810 const psa_key_attributes_t *attributes,
811 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100812
813/**@}*/
814
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100815/** \defgroup hash Message digests
816 * @{
817 */
818
Gilles Peskine69647a42019-01-14 20:18:12 +0100819/** Calculate the hash (digest) of a message.
820 *
821 * \note To verify the hash of a message against an
822 * expected value, use psa_hash_compare() instead.
823 *
824 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
825 * such that #PSA_ALG_IS_HASH(\p alg) is true).
826 * \param[in] input Buffer containing the message to hash.
827 * \param input_length Size of the \p input buffer in bytes.
828 * \param[out] hash Buffer where the hash is to be written.
829 * \param hash_size Size of the \p hash buffer in bytes.
830 * \param[out] hash_length On success, the number of bytes
831 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100832 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100833 *
834 * \retval #PSA_SUCCESS
835 * Success.
836 * \retval #PSA_ERROR_NOT_SUPPORTED
837 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100838 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100839 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
840 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100841 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
842 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
843 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200844 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100845 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100846 * \retval #PSA_ERROR_BAD_STATE
847 * The library has not been previously initialized by psa_crypto_init().
848 * It is implementation-dependent whether a failure to initialize
849 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100850 */
851psa_status_t psa_hash_compute(psa_algorithm_t alg,
852 const uint8_t *input,
853 size_t input_length,
854 uint8_t *hash,
855 size_t hash_size,
856 size_t *hash_length);
857
858/** Calculate the hash (digest) of a message and compare it with a
859 * reference value.
860 *
861 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
862 * such that #PSA_ALG_IS_HASH(\p alg) is true).
863 * \param[in] input Buffer containing the message to hash.
864 * \param input_length Size of the \p input buffer in bytes.
865 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100866 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100867 *
868 * \retval #PSA_SUCCESS
869 * The expected hash is identical to the actual hash of the input.
870 * \retval #PSA_ERROR_INVALID_SIGNATURE
871 * The hash of the message was calculated successfully, but it
872 * differs from the expected hash.
873 * \retval #PSA_ERROR_NOT_SUPPORTED
874 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100875 * \retval #PSA_ERROR_INVALID_ARGUMENT
876 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100877 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
878 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
879 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200880 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100881 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100882 * \retval #PSA_ERROR_BAD_STATE
883 * The library has not been previously initialized by psa_crypto_init().
884 * It is implementation-dependent whether a failure to initialize
885 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100886 */
887psa_status_t psa_hash_compare(psa_algorithm_t alg,
888 const uint8_t *input,
889 size_t input_length,
890 const uint8_t *hash,
891 const size_t hash_length);
892
Gilles Peskine308b91d2018-02-08 09:47:44 +0100893/** The type of the state data structure for multipart hash operations.
894 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000895 * Before calling any function on a hash operation object, the application must
896 * initialize it by any of the following means:
897 * - Set the structure to all-bits-zero, for example:
898 * \code
899 * psa_hash_operation_t operation;
900 * memset(&operation, 0, sizeof(operation));
901 * \endcode
902 * - Initialize the structure to logical zero values, for example:
903 * \code
904 * psa_hash_operation_t operation = {0};
905 * \endcode
906 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
907 * for example:
908 * \code
909 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
910 * \endcode
911 * - Assign the result of the function psa_hash_operation_init()
912 * to the structure, for example:
913 * \code
914 * psa_hash_operation_t operation;
915 * operation = psa_hash_operation_init();
916 * \endcode
917 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100918 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100919 * make any assumptions about the content of this structure except
920 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100921typedef struct psa_hash_operation_s psa_hash_operation_t;
922
Jaeden Amero6a25b412019-01-04 11:47:44 +0000923/** \def PSA_HASH_OPERATION_INIT
924 *
925 * This macro returns a suitable initializer for a hash operation object
926 * of type #psa_hash_operation_t.
927 */
928#ifdef __DOXYGEN_ONLY__
929/* This is an example definition for documentation purposes.
930 * Implementations should define a suitable value in `crypto_struct.h`.
931 */
932#define PSA_HASH_OPERATION_INIT {0}
933#endif
934
935/** Return an initial value for a hash operation object.
936 */
937static psa_hash_operation_t psa_hash_operation_init(void);
938
Gilles Peskinef45adda2019-01-14 18:29:18 +0100939/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100940 *
941 * The sequence of operations to calculate a hash (message digest)
942 * is as follows:
943 * -# Allocate an operation object which will be passed to all the functions
944 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000945 * -# Initialize the operation object with one of the methods described in the
946 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200947 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100948 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100949 * of the message each time. The hash that is calculated is the hash
950 * of the concatenation of these messages in order.
951 * -# To calculate the hash, call psa_hash_finish().
952 * To compare the hash with an expected value, call psa_hash_verify().
953 *
954 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000955 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100956 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200957 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100958 * eventually terminate the operation. The following events terminate an
959 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100960 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100961 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100962 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000963 * \param[in,out] operation The operation object to set up. It must have
964 * been initialized as per the documentation for
965 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200966 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
967 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100968 *
Gilles Peskine28538492018-07-11 17:34:00 +0200969 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100970 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200971 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100972 * \p alg is not a supported hash algorithm.
973 * \retval #PSA_ERROR_INVALID_ARGUMENT
974 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100975 * \retval #PSA_ERROR_BAD_STATE
976 * The operation state is not valid (already set up and not
977 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200978 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
979 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
980 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200981 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100982 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawe970d652019-08-08 14:40:04 +0100983 * \p operation is either not initialized or is in use
984 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100985 * The library has not been previously initialized by psa_crypto_init().
986 * It is implementation-dependent whether a failure to initialize
987 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100988 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200989psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100990 psa_algorithm_t alg);
991
Gilles Peskine308b91d2018-02-08 09:47:44 +0100992/** Add a message fragment to a multipart hash operation.
993 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200994 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100995 *
996 * If this function returns an error status, the operation becomes inactive.
997 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200998 * \param[in,out] operation Active hash operation.
999 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001000 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001001 *
Gilles Peskine28538492018-07-11 17:34:00 +02001002 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001003 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001004 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001005 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001006 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1007 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1008 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001009 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001010 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001011 * The operation state is not valid.
1012 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001013 * The library has not been previously initialized by psa_crypto_init().
1014 * It is implementation-dependent whether a failure to initialize
1015 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001016 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001017psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1018 const uint8_t *input,
1019 size_t input_length);
1020
Gilles Peskine308b91d2018-02-08 09:47:44 +01001021/** Finish the calculation of the hash of a message.
1022 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001023 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001024 * This function calculates the hash of the message formed by concatenating
1025 * the inputs passed to preceding calls to psa_hash_update().
1026 *
1027 * When this function returns, the operation becomes inactive.
1028 *
1029 * \warning Applications should not call this function if they expect
1030 * a specific value for the hash. Call psa_hash_verify() instead.
1031 * Beware that comparing integrity or authenticity data such as
1032 * hash values with a function such as \c memcmp is risky
1033 * because the time taken by the comparison may leak information
1034 * about the hashed data which could allow an attacker to guess
1035 * a valid hash and thereby bypass security controls.
1036 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001037 * \param[in,out] operation Active hash operation.
1038 * \param[out] hash Buffer where the hash is to be written.
1039 * \param hash_size Size of the \p hash buffer in bytes.
1040 * \param[out] hash_length On success, the number of bytes
1041 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001042 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001043 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001044 *
Gilles Peskine28538492018-07-11 17:34:00 +02001045 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001046 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001047 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001048 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001049 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001050 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001051 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001052 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001053 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1054 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1055 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001056 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001057 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001058 * The operation state is not valid.
1059 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001060 * The library has not been previously initialized by psa_crypto_init().
1061 * It is implementation-dependent whether a failure to initialize
1062 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001063 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001064psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1065 uint8_t *hash,
1066 size_t hash_size,
1067 size_t *hash_length);
1068
Gilles Peskine308b91d2018-02-08 09:47:44 +01001069/** Finish the calculation of the hash of a message and compare it with
1070 * an expected value.
1071 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001072 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001073 * This function calculates the hash of the message formed by concatenating
1074 * the inputs passed to preceding calls to psa_hash_update(). It then
1075 * compares the calculated hash with the expected hash passed as a
1076 * parameter to this function.
1077 *
1078 * When this function returns, the operation becomes inactive.
1079 *
Gilles Peskine19067982018-03-20 17:54:53 +01001080 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001081 * comparison between the actual hash and the expected hash is performed
1082 * in constant time.
1083 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001084 * \param[in,out] operation Active hash operation.
1085 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001086 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001087 *
Gilles Peskine28538492018-07-11 17:34:00 +02001088 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001089 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001090 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001091 * The hash of the message was calculated successfully, but it
1092 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001093 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001094 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001095 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1096 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1097 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001098 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001099 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001100 * The operation state is not valid.
1101 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001102 * The library has not been previously initialized by psa_crypto_init().
1103 * It is implementation-dependent whether a failure to initialize
1104 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001105 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001106psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1107 const uint8_t *hash,
1108 size_t hash_length);
1109
Gilles Peskine308b91d2018-02-08 09:47:44 +01001110/** Abort a hash operation.
1111 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001112 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001113 * \p operation structure itself. Once aborted, the operation object
1114 * can be reused for another operation by calling
1115 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001116 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001117 * You may call this function any time after the operation object has
1118 * been initialized by any of the following methods:
1119 * - A call to psa_hash_setup(), whether it succeeds or not.
1120 * - Initializing the \c struct to all-bits-zero.
1121 * - Initializing the \c struct to logical zeros, e.g.
1122 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001123 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001124 * In particular, calling psa_hash_abort() after the operation has been
1125 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1126 * psa_hash_verify() is safe and has no effect.
1127 *
1128 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001129 *
Gilles Peskine28538492018-07-11 17:34:00 +02001130 * \retval #PSA_SUCCESS
1131 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001132 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001133 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1134 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001135 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001136 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001137 * The operation state is not valid.
1138 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001139 * The library has not been previously initialized by psa_crypto_init().
1140 * It is implementation-dependent whether a failure to initialize
1141 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001142 */
1143psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001144
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001145/** Clone a hash operation.
1146 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001147 * This function copies the state of an ongoing hash operation to
1148 * a new operation object. In other words, this function is equivalent
1149 * to calling psa_hash_setup() on \p target_operation with the same
1150 * algorithm that \p source_operation was set up for, then
1151 * psa_hash_update() on \p target_operation with the same input that
1152 * that was passed to \p source_operation. After this function returns, the
1153 * two objects are independent, i.e. subsequent calls involving one of
1154 * the objects do not affect the other object.
1155 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001156 * \param[in] source_operation The active hash operation to clone.
1157 * \param[in,out] target_operation The operation object to set up.
1158 * It must be initialized but not active.
1159 *
1160 * \retval #PSA_SUCCESS
1161 * \retval #PSA_ERROR_BAD_STATE
1162 * \p source_operation is not an active hash operation.
1163 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001164 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001165 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1166 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001167 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001168 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001169 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001170 * The operation state is either not initialized or has already been setup.
1171 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw8f7cd1e2019-08-08 14:49:01 +01001172 * The operation state is either not initialized or has already been setup.
1173 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001174 * The library has not been previously initialized by psa_crypto_init().
1175 * It is implementation-dependent whether a failure to initialize
1176 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001177 */
1178psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1179 psa_hash_operation_t *target_operation);
1180
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001181/**@}*/
1182
Gilles Peskine8c9def32018-02-08 10:02:12 +01001183/** \defgroup MAC Message authentication codes
1184 * @{
1185 */
1186
Gilles Peskine69647a42019-01-14 20:18:12 +01001187/** Calculate the MAC (message authentication code) of a message.
1188 *
1189 * \note To verify the MAC of a message against an
1190 * expected value, use psa_mac_verify() instead.
1191 * Beware that comparing integrity or authenticity data such as
1192 * MAC values with a function such as \c memcmp is risky
1193 * because the time taken by the comparison may leak information
1194 * about the MAC value which could allow an attacker to guess
1195 * a valid MAC and thereby bypass security controls.
1196 *
1197 * \param handle Handle to the key to use for the operation.
1198 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001199 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001200 * \param[in] input Buffer containing the input message.
1201 * \param input_length Size of the \p input buffer in bytes.
1202 * \param[out] mac Buffer where the MAC value is to be written.
1203 * \param mac_size Size of the \p mac buffer in bytes.
1204 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001205 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001206 *
1207 * \retval #PSA_SUCCESS
1208 * Success.
1209 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001210 * \retval #PSA_ERROR_NOT_PERMITTED
1211 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001212 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001213 * \retval #PSA_ERROR_NOT_SUPPORTED
1214 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001215 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1216 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001217 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1218 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1219 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001220 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001221 * \retval #PSA_ERROR_STORAGE_FAILURE
1222 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001223 * \retval #PSA_ERROR_BAD_STATE
1224 * The library has not been previously initialized by psa_crypto_init().
1225 * It is implementation-dependent whether a failure to initialize
1226 * results in this error code.
1227 */
1228psa_status_t psa_mac_compute(psa_key_handle_t handle,
1229 psa_algorithm_t alg,
1230 const uint8_t *input,
1231 size_t input_length,
1232 uint8_t *mac,
1233 size_t mac_size,
1234 size_t *mac_length);
1235
1236/** Calculate the MAC of a message and compare it with a reference value.
1237 *
1238 * \param handle Handle to the key to use for the operation.
1239 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001240 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001241 * \param[in] input Buffer containing the input message.
1242 * \param input_length Size of the \p input buffer in bytes.
1243 * \param[out] mac Buffer containing the expected MAC value.
1244 * \param mac_length Size of the \p mac buffer in bytes.
1245 *
1246 * \retval #PSA_SUCCESS
1247 * The expected MAC is identical to the actual MAC of the input.
1248 * \retval #PSA_ERROR_INVALID_SIGNATURE
1249 * The MAC of the message was calculated successfully, but it
1250 * differs from the expected value.
1251 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001252 * \retval #PSA_ERROR_NOT_PERMITTED
1253 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001254 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001255 * \retval #PSA_ERROR_NOT_SUPPORTED
1256 * \p alg is not supported or is not a MAC algorithm.
1257 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1258 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1259 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001260 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001261 * \retval #PSA_ERROR_STORAGE_FAILURE
1262 * The key could not be retrieved from storage.
1263 * \retval #PSA_ERROR_BAD_STATE
1264 * The library has not been previously initialized by psa_crypto_init().
1265 * It is implementation-dependent whether a failure to initialize
1266 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001267 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001268psa_status_t psa_mac_verify(psa_key_handle_t handle,
1269 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001270 const uint8_t *input,
1271 size_t input_length,
1272 const uint8_t *mac,
1273 const size_t mac_length);
1274
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001275/** The type of the state data structure for multipart MAC operations.
1276 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001277 * Before calling any function on a MAC operation object, the application must
1278 * initialize it by any of the following means:
1279 * - Set the structure to all-bits-zero, for example:
1280 * \code
1281 * psa_mac_operation_t operation;
1282 * memset(&operation, 0, sizeof(operation));
1283 * \endcode
1284 * - Initialize the structure to logical zero values, for example:
1285 * \code
1286 * psa_mac_operation_t operation = {0};
1287 * \endcode
1288 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1289 * for example:
1290 * \code
1291 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1292 * \endcode
1293 * - Assign the result of the function psa_mac_operation_init()
1294 * to the structure, for example:
1295 * \code
1296 * psa_mac_operation_t operation;
1297 * operation = psa_mac_operation_init();
1298 * \endcode
1299 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001300 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001301 * make any assumptions about the content of this structure except
1302 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001303typedef struct psa_mac_operation_s psa_mac_operation_t;
1304
Jaeden Amero769ce272019-01-04 11:48:03 +00001305/** \def PSA_MAC_OPERATION_INIT
1306 *
1307 * This macro returns a suitable initializer for a MAC operation object of type
1308 * #psa_mac_operation_t.
1309 */
1310#ifdef __DOXYGEN_ONLY__
1311/* This is an example definition for documentation purposes.
1312 * Implementations should define a suitable value in `crypto_struct.h`.
1313 */
1314#define PSA_MAC_OPERATION_INIT {0}
1315#endif
1316
1317/** Return an initial value for a MAC operation object.
1318 */
1319static psa_mac_operation_t psa_mac_operation_init(void);
1320
Gilles Peskinef45adda2019-01-14 18:29:18 +01001321/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001322 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001323 * This function sets up the calculation of the MAC
1324 * (message authentication code) of a byte string.
1325 * To verify the MAC of a message against an
1326 * expected value, use psa_mac_verify_setup() instead.
1327 *
1328 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001329 * -# Allocate an operation object which will be passed to all the functions
1330 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001331 * -# Initialize the operation object with one of the methods described in the
1332 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001333 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001334 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1335 * of the message each time. The MAC that is calculated is the MAC
1336 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001337 * -# At the end of the message, call psa_mac_sign_finish() to finish
1338 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001339 *
1340 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001341 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001342 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001343 * After a successful call to psa_mac_sign_setup(), the application must
1344 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001345 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001346 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001347 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001348 * \param[in,out] operation The operation object to set up. It must have
1349 * been initialized as per the documentation for
1350 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001351 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001352 * It must remain valid until the operation
1353 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001354 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001355 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001356 *
Gilles Peskine28538492018-07-11 17:34:00 +02001357 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001358 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001359 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001360 * \retval #PSA_ERROR_NOT_PERMITTED
1361 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001362 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001363 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001364 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001365 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1366 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1367 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001368 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001369 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001370 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001371 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001372 * The operation state is not valid (already set up and not
1373 * subsequently completed).
1374 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001375 * The library has not been previously initialized by psa_crypto_init().
1376 * It is implementation-dependent whether a failure to initialize
1377 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001378 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001379psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001380 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001381 psa_algorithm_t alg);
1382
Gilles Peskinef45adda2019-01-14 18:29:18 +01001383/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001384 *
1385 * This function sets up the verification of the MAC
1386 * (message authentication code) of a byte string against an expected value.
1387 *
1388 * The sequence of operations to verify a MAC is as follows:
1389 * -# Allocate an operation object which will be passed to all the functions
1390 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001391 * -# Initialize the operation object with one of the methods described in the
1392 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001393 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001394 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1395 * of the message each time. The MAC that is calculated is the MAC
1396 * of the concatenation of these messages in order.
1397 * -# At the end of the message, call psa_mac_verify_finish() to finish
1398 * calculating the actual MAC of the message and verify it against
1399 * the expected value.
1400 *
1401 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001402 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001403 *
1404 * After a successful call to psa_mac_verify_setup(), the application must
1405 * eventually terminate the operation through one of the following methods:
1406 * - A failed call to psa_mac_update().
1407 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1408 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001409 * \param[in,out] operation The operation object to set up. It must have
1410 * been initialized as per the documentation for
1411 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001412 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001413 * It must remain valid until the operation
1414 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001415 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1416 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001417 *
Gilles Peskine28538492018-07-11 17:34:00 +02001418 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001419 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001420 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001421 * \retval #PSA_ERROR_NOT_PERMITTED
1422 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001423 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001424 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001425 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001426 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1427 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1428 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001429 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001430 * \retval #PSA_ERROR_STORAGE_FAILURE
1431 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001432 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001433 * The operation state is not valid (already set up and not
1434 * subsequently completed).
1435 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001436 * The library has not been previously initialized by psa_crypto_init().
1437 * It is implementation-dependent whether a failure to initialize
1438 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001439 */
1440psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001441 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001442 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001443
Gilles Peskinedcd14942018-07-12 00:30:52 +02001444/** Add a message fragment to a multipart MAC operation.
1445 *
1446 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1447 * before calling this function.
1448 *
1449 * If this function returns an error status, the operation becomes inactive.
1450 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001451 * \param[in,out] operation Active MAC operation.
1452 * \param[in] input Buffer containing the message fragment to add to
1453 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001454 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001455 *
1456 * \retval #PSA_SUCCESS
1457 * Success.
1458 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001459 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001460 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1461 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1462 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001463 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001464 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001465 * \retval #PSA_ERROR_BAD_STATE
1466 * The library has not been previously initialized by psa_crypto_init().
1467 * It is implementation-dependent whether a failure to initialize
1468 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001469 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001470psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1471 const uint8_t *input,
1472 size_t input_length);
1473
Gilles Peskinedcd14942018-07-12 00:30:52 +02001474/** Finish the calculation of the MAC of a message.
1475 *
1476 * The application must call psa_mac_sign_setup() before calling this function.
1477 * This function calculates the MAC of the message formed by concatenating
1478 * the inputs passed to preceding calls to psa_mac_update().
1479 *
1480 * When this function returns, the operation becomes inactive.
1481 *
1482 * \warning Applications should not call this function if they expect
1483 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1484 * Beware that comparing integrity or authenticity data such as
1485 * MAC values with a function such as \c memcmp is risky
1486 * because the time taken by the comparison may leak information
1487 * about the MAC value which could allow an attacker to guess
1488 * a valid MAC and thereby bypass security controls.
1489 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001490 * \param[in,out] operation Active MAC operation.
1491 * \param[out] mac Buffer where the MAC value is to be written.
1492 * \param mac_size Size of the \p mac buffer in bytes.
1493 * \param[out] mac_length On success, the number of bytes
1494 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001495 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001496 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001497 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001498 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001499 *
1500 * \retval #PSA_SUCCESS
1501 * Success.
1502 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001503 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001504 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001505 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001506 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1507 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1508 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1509 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001510 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001511 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001512 * \retval #PSA_ERROR_BAD_STATE
1513 * The library has not been previously initialized by psa_crypto_init().
1514 * It is implementation-dependent whether a failure to initialize
1515 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001516 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001517psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1518 uint8_t *mac,
1519 size_t mac_size,
1520 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001521
Gilles Peskinedcd14942018-07-12 00:30:52 +02001522/** Finish the calculation of the MAC of a message and compare it with
1523 * an expected value.
1524 *
1525 * The application must call psa_mac_verify_setup() before calling this function.
1526 * This function calculates the MAC of the message formed by concatenating
1527 * the inputs passed to preceding calls to psa_mac_update(). It then
1528 * compares the calculated MAC with the expected MAC passed as a
1529 * parameter to this function.
1530 *
1531 * When this function returns, the operation becomes inactive.
1532 *
1533 * \note Implementations shall make the best effort to ensure that the
1534 * comparison between the actual MAC and the expected MAC is performed
1535 * in constant time.
1536 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001537 * \param[in,out] operation Active MAC operation.
1538 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001539 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001540 *
1541 * \retval #PSA_SUCCESS
1542 * The expected MAC is identical to the actual MAC of the message.
1543 * \retval #PSA_ERROR_INVALID_SIGNATURE
1544 * The MAC of the message was calculated successfully, but it
1545 * differs from the expected MAC.
1546 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001547 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001548 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1549 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1550 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001551 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001552 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001553 * \retval #PSA_ERROR_BAD_STATE
1554 * The library has not been previously initialized by psa_crypto_init().
1555 * It is implementation-dependent whether a failure to initialize
1556 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001557 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001558psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1559 const uint8_t *mac,
1560 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001561
Gilles Peskinedcd14942018-07-12 00:30:52 +02001562/** Abort a MAC operation.
1563 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001564 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001565 * \p operation structure itself. Once aborted, the operation object
1566 * can be reused for another operation by calling
1567 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001568 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001569 * You may call this function any time after the operation object has
1570 * been initialized by any of the following methods:
1571 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1572 * it succeeds or not.
1573 * - Initializing the \c struct to all-bits-zero.
1574 * - Initializing the \c struct to logical zeros, e.g.
1575 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001576 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001577 * In particular, calling psa_mac_abort() after the operation has been
1578 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1579 * psa_mac_verify_finish() is safe and has no effect.
1580 *
1581 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001582 *
1583 * \retval #PSA_SUCCESS
1584 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001585 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001586 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1587 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001588 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001589 * \retval #PSA_ERROR_BAD_STATE
1590 * The library has not been previously initialized by psa_crypto_init().
1591 * It is implementation-dependent whether a failure to initialize
1592 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001593 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001594psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1595
1596/**@}*/
1597
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001598/** \defgroup cipher Symmetric ciphers
1599 * @{
1600 */
1601
Gilles Peskine69647a42019-01-14 20:18:12 +01001602/** Encrypt a message using a symmetric cipher.
1603 *
1604 * This function encrypts a message with a random IV (initialization
1605 * vector).
1606 *
1607 * \param handle Handle to the key to use for the operation.
1608 * It must remain valid until the operation
1609 * terminates.
1610 * \param alg The cipher algorithm to compute
1611 * (\c PSA_ALG_XXX value such that
1612 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1613 * \param[in] input Buffer containing the message to encrypt.
1614 * \param input_length Size of the \p input buffer in bytes.
1615 * \param[out] output Buffer where the output is to be written.
1616 * The output contains the IV followed by
1617 * the ciphertext proper.
1618 * \param output_size Size of the \p output buffer in bytes.
1619 * \param[out] output_length On success, the number of bytes
1620 * that make up the output.
1621 *
1622 * \retval #PSA_SUCCESS
1623 * Success.
1624 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001625 * \retval #PSA_ERROR_NOT_PERMITTED
1626 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001627 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001628 * \retval #PSA_ERROR_NOT_SUPPORTED
1629 * \p alg is not supported or is not a cipher algorithm.
1630 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1631 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1632 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1633 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001634 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001635 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001636 * \retval #PSA_ERROR_BAD_STATE
1637 * The library has not been previously initialized by psa_crypto_init().
1638 * It is implementation-dependent whether a failure to initialize
1639 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001640 */
1641psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1642 psa_algorithm_t alg,
1643 const uint8_t *input,
1644 size_t input_length,
1645 uint8_t *output,
1646 size_t output_size,
1647 size_t *output_length);
1648
1649/** Decrypt a message using a symmetric cipher.
1650 *
1651 * This function decrypts a message encrypted with a symmetric cipher.
1652 *
1653 * \param handle Handle to the key to use for the operation.
1654 * It must remain valid until the operation
1655 * terminates.
1656 * \param alg The cipher algorithm to compute
1657 * (\c PSA_ALG_XXX value such that
1658 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1659 * \param[in] input Buffer containing the message to decrypt.
1660 * This consists of the IV followed by the
1661 * ciphertext proper.
1662 * \param input_length Size of the \p input buffer in bytes.
1663 * \param[out] output Buffer where the plaintext is to be written.
1664 * \param output_size Size of the \p output buffer in bytes.
1665 * \param[out] output_length On success, the number of bytes
1666 * that make up the output.
1667 *
1668 * \retval #PSA_SUCCESS
1669 * Success.
1670 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001671 * \retval #PSA_ERROR_NOT_PERMITTED
1672 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001673 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001674 * \retval #PSA_ERROR_NOT_SUPPORTED
1675 * \p alg is not supported or is not a cipher algorithm.
1676 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1677 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1678 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1679 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001680 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001681 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001682 * \retval #PSA_ERROR_BAD_STATE
1683 * The library has not been previously initialized by psa_crypto_init().
1684 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001685 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001686 */
1687psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1688 psa_algorithm_t alg,
1689 const uint8_t *input,
1690 size_t input_length,
1691 uint8_t *output,
1692 size_t output_size,
1693 size_t *output_length);
1694
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001695/** The type of the state data structure for multipart cipher operations.
1696 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001697 * Before calling any function on a cipher operation object, the application
1698 * must initialize it by any of the following means:
1699 * - Set the structure to all-bits-zero, for example:
1700 * \code
1701 * psa_cipher_operation_t operation;
1702 * memset(&operation, 0, sizeof(operation));
1703 * \endcode
1704 * - Initialize the structure to logical zero values, for example:
1705 * \code
1706 * psa_cipher_operation_t operation = {0};
1707 * \endcode
1708 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1709 * for example:
1710 * \code
1711 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1712 * \endcode
1713 * - Assign the result of the function psa_cipher_operation_init()
1714 * to the structure, for example:
1715 * \code
1716 * psa_cipher_operation_t operation;
1717 * operation = psa_cipher_operation_init();
1718 * \endcode
1719 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001720 * This is an implementation-defined \c struct. Applications should not
1721 * make any assumptions about the content of this structure except
1722 * as directed by the documentation of a specific implementation. */
1723typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1724
Jaeden Amero5bae2272019-01-04 11:48:27 +00001725/** \def PSA_CIPHER_OPERATION_INIT
1726 *
1727 * This macro returns a suitable initializer for a cipher operation object of
1728 * type #psa_cipher_operation_t.
1729 */
1730#ifdef __DOXYGEN_ONLY__
1731/* This is an example definition for documentation purposes.
1732 * Implementations should define a suitable value in `crypto_struct.h`.
1733 */
1734#define PSA_CIPHER_OPERATION_INIT {0}
1735#endif
1736
1737/** Return an initial value for a cipher operation object.
1738 */
1739static psa_cipher_operation_t psa_cipher_operation_init(void);
1740
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001741/** Set the key for a multipart symmetric encryption operation.
1742 *
1743 * The sequence of operations to encrypt a message with a symmetric cipher
1744 * is as follows:
1745 * -# Allocate an operation object which will be passed to all the functions
1746 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001747 * -# Initialize the operation object with one of the methods described in the
1748 * documentation for #psa_cipher_operation_t, e.g.
1749 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001750 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001751 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001752 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001753 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001754 * requires a specific IV value.
1755 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1756 * of the message each time.
1757 * -# Call psa_cipher_finish().
1758 *
1759 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001760 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001761 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001762 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001763 * eventually terminate the operation. The following events terminate an
1764 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001765 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001766 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001767 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001768 * \param[in,out] operation The operation object to set up. It must have
1769 * been initialized as per the documentation for
1770 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001771 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001772 * It must remain valid until the operation
1773 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001774 * \param alg The cipher algorithm to compute
1775 * (\c PSA_ALG_XXX value such that
1776 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001777 *
Gilles Peskine28538492018-07-11 17:34:00 +02001778 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001779 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001780 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001781 * \retval #PSA_ERROR_NOT_PERMITTED
1782 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001783 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001784 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001785 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001786 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1787 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1788 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001789 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001790 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001791 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001792 * The operation state is not valid (already set up and not
1793 * subsequently completed).
1794 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001795 * The library has not been previously initialized by psa_crypto_init().
1796 * It is implementation-dependent whether a failure to initialize
1797 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001798 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001799psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001800 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001801 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001802
1803/** Set the key for a multipart symmetric decryption operation.
1804 *
1805 * The sequence of operations to decrypt a message with a symmetric cipher
1806 * is as follows:
1807 * -# Allocate an operation object which will be passed to all the functions
1808 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001809 * -# Initialize the operation object with one of the methods described in the
1810 * documentation for #psa_cipher_operation_t, e.g.
1811 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001812 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001813 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001814 * decryption. If the IV is prepended to the ciphertext, you can call
1815 * psa_cipher_update() on a buffer containing the IV followed by the
1816 * beginning of the message.
1817 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1818 * of the message each time.
1819 * -# Call psa_cipher_finish().
1820 *
1821 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001822 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001823 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001824 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001825 * eventually terminate the operation. The following events terminate an
1826 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001827 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001828 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001829 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001830 * \param[in,out] operation The operation object to set up. It must have
1831 * been initialized as per the documentation for
1832 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001833 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001834 * It must remain valid until the operation
1835 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001836 * \param alg The cipher algorithm to compute
1837 * (\c PSA_ALG_XXX value such that
1838 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001839 *
Gilles Peskine28538492018-07-11 17:34:00 +02001840 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001841 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001842 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001843 * \retval #PSA_ERROR_NOT_PERMITTED
1844 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001845 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001846 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001847 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001848 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1849 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1850 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001851 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001852 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001853 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001854 * The operation state is not valid (already set up and not
1855 * subsequently completed).
1856 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001857 * The library has not been previously initialized by psa_crypto_init().
1858 * It is implementation-dependent whether a failure to initialize
1859 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001860 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001861psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001862 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001863 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001864
Gilles Peskinedcd14942018-07-12 00:30:52 +02001865/** Generate an IV for a symmetric encryption operation.
1866 *
1867 * This function generates a random IV (initialization vector), nonce
1868 * or initial counter value for the encryption operation as appropriate
1869 * for the chosen algorithm, key type and key size.
1870 *
1871 * The application must call psa_cipher_encrypt_setup() before
1872 * calling this function.
1873 *
1874 * If this function returns an error status, the operation becomes inactive.
1875 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001876 * \param[in,out] operation Active cipher operation.
1877 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001878 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001879 * \param[out] iv_length On success, the number of bytes of the
1880 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001881 *
1882 * \retval #PSA_SUCCESS
1883 * Success.
1884 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001885 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001886 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001887 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001888 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1889 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1890 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001891 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001892 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001893 * \retval #PSA_ERROR_BAD_STATE
1894 * The library has not been previously initialized by psa_crypto_init().
1895 * It is implementation-dependent whether a failure to initialize
1896 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001897 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001898psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001899 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001900 size_t iv_size,
1901 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001902
Gilles Peskinedcd14942018-07-12 00:30:52 +02001903/** Set the IV for a symmetric encryption or decryption operation.
1904 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001905 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001906 * or initial counter value for the encryption or decryption operation.
1907 *
1908 * The application must call psa_cipher_encrypt_setup() before
1909 * calling this function.
1910 *
1911 * If this function returns an error status, the operation becomes inactive.
1912 *
1913 * \note When encrypting, applications should use psa_cipher_generate_iv()
1914 * instead of this function, unless implementing a protocol that requires
1915 * a non-random IV.
1916 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001917 * \param[in,out] operation Active cipher operation.
1918 * \param[in] iv Buffer containing the IV to use.
1919 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001920 *
1921 * \retval #PSA_SUCCESS
1922 * Success.
1923 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001924 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001925 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001926 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001927 * or the chosen algorithm does not use an IV.
1928 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1929 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1930 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001931 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001932 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001933 * \retval #PSA_ERROR_BAD_STATE
1934 * The library has not been previously initialized by psa_crypto_init().
1935 * It is implementation-dependent whether a failure to initialize
1936 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001937 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001938psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001939 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001940 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001941
Gilles Peskinedcd14942018-07-12 00:30:52 +02001942/** Encrypt or decrypt a message fragment in an active cipher operation.
1943 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001944 * Before calling this function, you must:
1945 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1946 * The choice of setup function determines whether this function
1947 * encrypts or decrypts its input.
1948 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1949 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001950 *
1951 * If this function returns an error status, the operation becomes inactive.
1952 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001953 * \param[in,out] operation Active cipher operation.
1954 * \param[in] input Buffer containing the message fragment to
1955 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001956 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001957 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001958 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001959 * \param[out] output_length On success, the number of bytes
1960 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001961 *
1962 * \retval #PSA_SUCCESS
1963 * Success.
1964 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001965 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001966 * not set, or already completed).
1967 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1968 * The size of the \p output buffer is too small.
1969 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1970 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1971 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001972 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001973 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001974 * \retval #PSA_ERROR_BAD_STATE
1975 * The library has not been previously initialized by psa_crypto_init().
1976 * It is implementation-dependent whether a failure to initialize
1977 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001978 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001979psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1980 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001981 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001982 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001983 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001984 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001985
Gilles Peskinedcd14942018-07-12 00:30:52 +02001986/** Finish encrypting or decrypting a message in a cipher operation.
1987 *
1988 * The application must call psa_cipher_encrypt_setup() or
1989 * psa_cipher_decrypt_setup() before calling this function. The choice
1990 * of setup function determines whether this function encrypts or
1991 * decrypts its input.
1992 *
1993 * This function finishes the encryption or decryption of the message
1994 * formed by concatenating the inputs passed to preceding calls to
1995 * psa_cipher_update().
1996 *
1997 * When this function returns, the operation becomes inactive.
1998 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001999 * \param[in,out] operation Active cipher operation.
2000 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002001 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002002 * \param[out] output_length On success, the number of bytes
2003 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002004 *
2005 * \retval #PSA_SUCCESS
2006 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02002007 * \retval #PSA_ERROR_INVALID_ARGUMENT
2008 * The total input size passed to this operation is not valid for
2009 * this particular algorithm. For example, the algorithm is a based
2010 * on block cipher and requires a whole number of blocks, but the
2011 * total input size is not a multiple of the block size.
2012 * \retval #PSA_ERROR_INVALID_PADDING
2013 * This is a decryption operation for an algorithm that includes
2014 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002015 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002016 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02002017 * not set, or already completed).
2018 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2019 * The size of the \p output buffer is too small.
2020 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2021 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2022 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002023 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002024 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002025 * \retval #PSA_ERROR_BAD_STATE
2026 * The library has not been previously initialized by psa_crypto_init().
2027 * It is implementation-dependent whether a failure to initialize
2028 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002029 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002030psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002031 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002032 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002033 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002034
Gilles Peskinedcd14942018-07-12 00:30:52 +02002035/** Abort a cipher operation.
2036 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002037 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002038 * \p operation structure itself. Once aborted, the operation object
2039 * can be reused for another operation by calling
2040 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002041 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002042 * You may call this function any time after the operation object has
2043 * been initialized by any of the following methods:
2044 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2045 * whether it succeeds or not.
2046 * - Initializing the \c struct to all-bits-zero.
2047 * - Initializing the \c struct to logical zeros, e.g.
2048 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002049 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002050 * In particular, calling psa_cipher_abort() after the operation has been
2051 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2052 * is safe and has no effect.
2053 *
2054 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002055 *
2056 * \retval #PSA_SUCCESS
2057 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002058 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002059 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2060 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002061 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002062 * \retval #PSA_ERROR_BAD_STATE
2063 * The library has not been previously initialized by psa_crypto_init().
2064 * It is implementation-dependent whether a failure to initialize
2065 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002066 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002067psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2068
2069/**@}*/
2070
Gilles Peskine3b555712018-03-03 21:27:57 +01002071/** \defgroup aead Authenticated encryption with associated data (AEAD)
2072 * @{
2073 */
2074
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002075/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002076 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002077 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002078 * \param alg The AEAD algorithm to compute
2079 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002080 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002081 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002082 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002083 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002084 * but not encrypted.
2085 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002086 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002087 * encrypted.
2088 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002089 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002090 * encrypted data. The additional data is not
2091 * part of this output. For algorithms where the
2092 * encrypted data and the authentication tag
2093 * are defined as separate outputs, the
2094 * authentication tag is appended to the
2095 * encrypted data.
2096 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2097 * This must be at least
2098 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2099 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002100 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002101 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002102 *
Gilles Peskine28538492018-07-11 17:34:00 +02002103 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002104 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002105 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002106 * \retval #PSA_ERROR_NOT_PERMITTED
2107 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002108 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002109 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002110 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002111 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002112 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2113 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002114 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2115 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002116 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002117 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002118 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002119 * The library has not been previously initialized by psa_crypto_init().
2120 * It is implementation-dependent whether a failure to initialize
2121 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002122 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002123psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002124 psa_algorithm_t alg,
2125 const uint8_t *nonce,
2126 size_t nonce_length,
2127 const uint8_t *additional_data,
2128 size_t additional_data_length,
2129 const uint8_t *plaintext,
2130 size_t plaintext_length,
2131 uint8_t *ciphertext,
2132 size_t ciphertext_size,
2133 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002134
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002135/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002136 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002137 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002138 * \param alg The AEAD algorithm to compute
2139 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002140 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002141 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002142 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002143 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002144 * but not encrypted.
2145 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002146 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002147 * encrypted. For algorithms where the
2148 * encrypted data and the authentication tag
2149 * are defined as separate inputs, the buffer
2150 * must contain the encrypted data followed
2151 * by the authentication tag.
2152 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002153 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002154 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2155 * This must be at least
2156 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2157 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002158 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002159 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002160 *
Gilles Peskine28538492018-07-11 17:34:00 +02002161 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002162 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002163 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002164 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002165 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002166 * \retval #PSA_ERROR_NOT_PERMITTED
2167 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002168 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002169 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002170 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002171 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002172 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2173 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002174 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2175 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002176 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002177 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002178 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002179 * The library has not been previously initialized by psa_crypto_init().
2180 * It is implementation-dependent whether a failure to initialize
2181 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002182 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002183psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002184 psa_algorithm_t alg,
2185 const uint8_t *nonce,
2186 size_t nonce_length,
2187 const uint8_t *additional_data,
2188 size_t additional_data_length,
2189 const uint8_t *ciphertext,
2190 size_t ciphertext_length,
2191 uint8_t *plaintext,
2192 size_t plaintext_size,
2193 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002194
Gilles Peskine30a9e412019-01-14 18:36:12 +01002195/** The type of the state data structure for multipart AEAD operations.
2196 *
2197 * Before calling any function on an AEAD operation object, the application
2198 * must initialize it by any of the following means:
2199 * - Set the structure to all-bits-zero, for example:
2200 * \code
2201 * psa_aead_operation_t operation;
2202 * memset(&operation, 0, sizeof(operation));
2203 * \endcode
2204 * - Initialize the structure to logical zero values, for example:
2205 * \code
2206 * psa_aead_operation_t operation = {0};
2207 * \endcode
2208 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2209 * for example:
2210 * \code
2211 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2212 * \endcode
2213 * - Assign the result of the function psa_aead_operation_init()
2214 * to the structure, for example:
2215 * \code
2216 * psa_aead_operation_t operation;
2217 * operation = psa_aead_operation_init();
2218 * \endcode
2219 *
2220 * This is an implementation-defined \c struct. Applications should not
2221 * make any assumptions about the content of this structure except
2222 * as directed by the documentation of a specific implementation. */
2223typedef struct psa_aead_operation_s psa_aead_operation_t;
2224
2225/** \def PSA_AEAD_OPERATION_INIT
2226 *
2227 * This macro returns a suitable initializer for an AEAD operation object of
2228 * type #psa_aead_operation_t.
2229 */
2230#ifdef __DOXYGEN_ONLY__
2231/* This is an example definition for documentation purposes.
2232 * Implementations should define a suitable value in `crypto_struct.h`.
2233 */
2234#define PSA_AEAD_OPERATION_INIT {0}
2235#endif
2236
2237/** Return an initial value for an AEAD operation object.
2238 */
2239static psa_aead_operation_t psa_aead_operation_init(void);
2240
2241/** Set the key for a multipart authenticated encryption operation.
2242 *
2243 * The sequence of operations to encrypt a message with authentication
2244 * is as follows:
2245 * -# Allocate an operation object which will be passed to all the functions
2246 * listed here.
2247 * -# Initialize the operation object with one of the methods described in the
2248 * documentation for #psa_aead_operation_t, e.g.
2249 * PSA_AEAD_OPERATION_INIT.
2250 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002251 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2252 * inputs to the subsequent calls to psa_aead_update_ad() and
2253 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2254 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002255 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2256 * generate or set the nonce. You should use
2257 * psa_aead_generate_nonce() unless the protocol you are implementing
2258 * requires a specific nonce value.
2259 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2260 * of the non-encrypted additional authenticated data each time.
2261 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002262 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002263 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002264 *
2265 * The application may call psa_aead_abort() at any time after the operation
2266 * has been initialized.
2267 *
2268 * After a successful call to psa_aead_encrypt_setup(), the application must
2269 * eventually terminate the operation. The following events terminate an
2270 * operation:
2271 * - A failed call to any of the \c psa_aead_xxx functions.
2272 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2273 *
2274 * \param[in,out] operation The operation object to set up. It must have
2275 * been initialized as per the documentation for
2276 * #psa_aead_operation_t and not yet in use.
2277 * \param handle Handle to the key to use for the operation.
2278 * It must remain valid until the operation
2279 * terminates.
2280 * \param alg The AEAD algorithm to compute
2281 * (\c PSA_ALG_XXX value such that
2282 * #PSA_ALG_IS_AEAD(\p alg) is true).
2283 *
2284 * \retval #PSA_SUCCESS
2285 * Success.
2286 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002287 * \retval #PSA_ERROR_NOT_PERMITTED
2288 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002289 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002290 * \retval #PSA_ERROR_NOT_SUPPORTED
2291 * \p alg is not supported or is not an AEAD algorithm.
2292 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2293 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2294 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002295 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002296 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002297 * \retval #PSA_ERROR_BAD_STATE
2298 * The library has not been previously initialized by psa_crypto_init().
2299 * It is implementation-dependent whether a failure to initialize
2300 * results in this error code.
2301 */
2302psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2303 psa_key_handle_t handle,
2304 psa_algorithm_t alg);
2305
2306/** Set the key for a multipart authenticated decryption operation.
2307 *
2308 * The sequence of operations to decrypt a message with authentication
2309 * is as follows:
2310 * -# Allocate an operation object which will be passed to all the functions
2311 * listed here.
2312 * -# Initialize the operation object with one of the methods described in the
2313 * documentation for #psa_aead_operation_t, e.g.
2314 * PSA_AEAD_OPERATION_INIT.
2315 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002316 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2317 * inputs to the subsequent calls to psa_aead_update_ad() and
2318 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2319 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002320 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2321 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2322 * of the non-encrypted additional authenticated data each time.
2323 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002324 * of the ciphertext to decrypt each time.
2325 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002326 *
2327 * The application may call psa_aead_abort() at any time after the operation
2328 * has been initialized.
2329 *
2330 * After a successful call to psa_aead_decrypt_setup(), the application must
2331 * eventually terminate the operation. The following events terminate an
2332 * operation:
2333 * - A failed call to any of the \c psa_aead_xxx functions.
2334 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2335 *
2336 * \param[in,out] operation The operation object to set up. It must have
2337 * been initialized as per the documentation for
2338 * #psa_aead_operation_t and not yet in use.
2339 * \param handle Handle to the key to use for the operation.
2340 * It must remain valid until the operation
2341 * terminates.
2342 * \param alg The AEAD algorithm to compute
2343 * (\c PSA_ALG_XXX value such that
2344 * #PSA_ALG_IS_AEAD(\p alg) is true).
2345 *
2346 * \retval #PSA_SUCCESS
2347 * Success.
2348 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002349 * \retval #PSA_ERROR_NOT_PERMITTED
2350 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002351 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002352 * \retval #PSA_ERROR_NOT_SUPPORTED
2353 * \p alg is not supported or is not an AEAD algorithm.
2354 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2355 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2356 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002357 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002358 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002359 * \retval #PSA_ERROR_BAD_STATE
2360 * The library has not been previously initialized by psa_crypto_init().
2361 * It is implementation-dependent whether a failure to initialize
2362 * results in this error code.
2363 */
2364psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2365 psa_key_handle_t handle,
2366 psa_algorithm_t alg);
2367
2368/** Generate a random nonce for an authenticated encryption operation.
2369 *
2370 * This function generates a random nonce for the authenticated encryption
2371 * operation with an appropriate size for the chosen algorithm, key type
2372 * and key size.
2373 *
2374 * The application must call psa_aead_encrypt_setup() before
2375 * calling this function.
2376 *
2377 * If this function returns an error status, the operation becomes inactive.
2378 *
2379 * \param[in,out] operation Active AEAD operation.
2380 * \param[out] nonce Buffer where the generated nonce is to be
2381 * written.
2382 * \param nonce_size Size of the \p nonce buffer in bytes.
2383 * \param[out] nonce_length On success, the number of bytes of the
2384 * generated nonce.
2385 *
2386 * \retval #PSA_SUCCESS
2387 * Success.
2388 * \retval #PSA_ERROR_BAD_STATE
2389 * The operation state is not valid (not set up, or nonce already set).
2390 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2391 * The size of the \p nonce buffer is too small.
2392 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2393 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2394 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002395 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002396 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002397 * \retval #PSA_ERROR_BAD_STATE
2398 * The library has not been previously initialized by psa_crypto_init().
2399 * It is implementation-dependent whether a failure to initialize
2400 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002401 */
2402psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002403 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002404 size_t nonce_size,
2405 size_t *nonce_length);
2406
2407/** Set the nonce for an authenticated encryption or decryption operation.
2408 *
2409 * This function sets the nonce for the authenticated
2410 * encryption or decryption operation.
2411 *
2412 * The application must call psa_aead_encrypt_setup() before
2413 * calling this function.
2414 *
2415 * If this function returns an error status, the operation becomes inactive.
2416 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002417 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002418 * instead of this function, unless implementing a protocol that requires
2419 * a non-random IV.
2420 *
2421 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002422 * \param[in] nonce Buffer containing the nonce to use.
2423 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002424 *
2425 * \retval #PSA_SUCCESS
2426 * Success.
2427 * \retval #PSA_ERROR_BAD_STATE
2428 * The operation state is not valid (not set up, or nonce already set).
2429 * \retval #PSA_ERROR_INVALID_ARGUMENT
2430 * The size of \p nonce is not acceptable for the chosen algorithm.
2431 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2432 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2433 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002434 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002435 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002436 * \retval #PSA_ERROR_BAD_STATE
2437 * The library has not been previously initialized by psa_crypto_init().
2438 * It is implementation-dependent whether a failure to initialize
2439 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002440 */
2441psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002442 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002443 size_t nonce_length);
2444
Gilles Peskinebc59c852019-01-17 15:26:08 +01002445/** Declare the lengths of the message and additional data for AEAD.
2446 *
2447 * The application must call this function before calling
2448 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2449 * the operation requires it. If the algorithm does not require it,
2450 * calling this function is optional, but if this function is called
2451 * then the implementation must enforce the lengths.
2452 *
2453 * You may call this function before or after setting the nonce with
2454 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2455 *
2456 * - For #PSA_ALG_CCM, calling this function is required.
2457 * - For the other AEAD algorithms defined in this specification, calling
2458 * this function is not required.
2459 * - For vendor-defined algorithm, refer to the vendor documentation.
2460 *
2461 * \param[in,out] operation Active AEAD operation.
2462 * \param ad_length Size of the non-encrypted additional
2463 * authenticated data in bytes.
2464 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2465 *
2466 * \retval #PSA_SUCCESS
2467 * Success.
2468 * \retval #PSA_ERROR_BAD_STATE
2469 * The operation state is not valid (not set up, already completed,
2470 * or psa_aead_update_ad() or psa_aead_update() already called).
2471 * \retval #PSA_ERROR_INVALID_ARGUMENT
2472 * At least one of the lengths is not acceptable for the chosen
2473 * algorithm.
2474 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2475 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2476 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002477 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002478 * \retval #PSA_ERROR_BAD_STATE
2479 * The library has not been previously initialized by psa_crypto_init().
2480 * It is implementation-dependent whether a failure to initialize
2481 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002482 */
2483psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2484 size_t ad_length,
2485 size_t plaintext_length);
2486
Gilles Peskine30a9e412019-01-14 18:36:12 +01002487/** Pass additional data to an active AEAD operation.
2488 *
2489 * Additional data is authenticated, but not encrypted.
2490 *
2491 * You may call this function multiple times to pass successive fragments
2492 * of the additional data. You may not call this function after passing
2493 * data to encrypt or decrypt with psa_aead_update().
2494 *
2495 * Before calling this function, you must:
2496 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2497 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2498 *
2499 * If this function returns an error status, the operation becomes inactive.
2500 *
2501 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2502 * there is no guarantee that the input is valid. Therefore, until
2503 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2504 * treat the input as untrusted and prepare to undo any action that
2505 * depends on the input if psa_aead_verify() returns an error status.
2506 *
2507 * \param[in,out] operation Active AEAD operation.
2508 * \param[in] input Buffer containing the fragment of
2509 * additional data.
2510 * \param input_length Size of the \p input buffer in bytes.
2511 *
2512 * \retval #PSA_SUCCESS
2513 * Success.
2514 * \retval #PSA_ERROR_BAD_STATE
2515 * The operation state is not valid (not set up, nonce not set,
2516 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002517 * \retval #PSA_ERROR_INVALID_ARGUMENT
2518 * The total input length overflows the additional data length that
2519 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002520 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2521 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2522 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002523 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002524 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002525 * \retval #PSA_ERROR_BAD_STATE
2526 * The library has not been previously initialized by psa_crypto_init().
2527 * It is implementation-dependent whether a failure to initialize
2528 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002529 */
2530psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2531 const uint8_t *input,
2532 size_t input_length);
2533
2534/** Encrypt or decrypt a message fragment in an active AEAD operation.
2535 *
2536 * Before calling this function, you must:
2537 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2538 * The choice of setup function determines whether this function
2539 * encrypts or decrypts its input.
2540 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2541 * 3. Call psa_aead_update_ad() to pass all the additional data.
2542 *
2543 * If this function returns an error status, the operation becomes inactive.
2544 *
2545 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2546 * there is no guarantee that the input is valid. Therefore, until
2547 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2548 * - Do not use the output in any way other than storing it in a
2549 * confidential location. If you take any action that depends
2550 * on the tentative decrypted data, this action will need to be
2551 * undone if the input turns out not to be valid. Furthermore,
2552 * if an adversary can observe that this action took place
2553 * (for example through timing), they may be able to use this
2554 * fact as an oracle to decrypt any message encrypted with the
2555 * same key.
2556 * - In particular, do not copy the output anywhere but to a
2557 * memory or storage space that you have exclusive access to.
2558 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002559 * This function does not require the input to be aligned to any
2560 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002561 * a whole block at a time, it must consume all the input provided, but
2562 * it may delay the end of the corresponding output until a subsequent
2563 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2564 * provides sufficient input. The amount of data that can be delayed
2565 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002566 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002567 * \param[in,out] operation Active AEAD operation.
2568 * \param[in] input Buffer containing the message fragment to
2569 * encrypt or decrypt.
2570 * \param input_length Size of the \p input buffer in bytes.
2571 * \param[out] output Buffer where the output is to be written.
2572 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002573 * This must be at least
2574 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2575 * \p input_length) where \c alg is the
2576 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002577 * \param[out] output_length On success, the number of bytes
2578 * that make up the returned output.
2579 *
2580 * \retval #PSA_SUCCESS
2581 * Success.
2582 * \retval #PSA_ERROR_BAD_STATE
2583 * The operation state is not valid (not set up, nonce not set
2584 * or already completed).
2585 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2586 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002587 * You can determine a sufficient buffer size by calling
2588 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2589 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002590 * \retval #PSA_ERROR_INVALID_ARGUMENT
2591 * The total length of input to psa_aead_update_ad() so far is
2592 * less than the additional data length that was previously
2593 * specified with psa_aead_set_lengths().
2594 * \retval #PSA_ERROR_INVALID_ARGUMENT
2595 * The total input length overflows the plaintext length that
2596 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002597 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2598 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2599 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002600 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002601 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002602 * \retval #PSA_ERROR_BAD_STATE
2603 * The library has not been previously initialized by psa_crypto_init().
2604 * It is implementation-dependent whether a failure to initialize
2605 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002606 */
2607psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2608 const uint8_t *input,
2609 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002610 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002611 size_t output_size,
2612 size_t *output_length);
2613
2614/** Finish encrypting a message in an AEAD operation.
2615 *
2616 * The operation must have been set up with psa_aead_encrypt_setup().
2617 *
2618 * This function finishes the authentication of the additional data
2619 * formed by concatenating the inputs passed to preceding calls to
2620 * psa_aead_update_ad() with the plaintext formed by concatenating the
2621 * inputs passed to preceding calls to psa_aead_update().
2622 *
2623 * This function has two output buffers:
2624 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002625 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002626 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002627 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002628 * that the operation performs.
2629 *
2630 * When this function returns, the operation becomes inactive.
2631 *
2632 * \param[in,out] operation Active AEAD operation.
2633 * \param[out] ciphertext Buffer where the last part of the ciphertext
2634 * is to be written.
2635 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002636 * This must be at least
2637 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2638 * \c alg is the algorithm that is being
2639 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002640 * \param[out] ciphertext_length On success, the number of bytes of
2641 * returned ciphertext.
2642 * \param[out] tag Buffer where the authentication tag is
2643 * to be written.
2644 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002645 * This must be at least
2646 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2647 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002648 * \param[out] tag_length On success, the number of bytes
2649 * that make up the returned tag.
2650 *
2651 * \retval #PSA_SUCCESS
2652 * Success.
2653 * \retval #PSA_ERROR_BAD_STATE
2654 * The operation state is not valid (not set up, nonce not set,
2655 * decryption, or already completed).
2656 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002657 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002658 * You can determine a sufficient buffer size for \p ciphertext by
2659 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2660 * where \c alg is the algorithm that is being calculated.
2661 * You can determine a sufficient buffer size for \p tag by
2662 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002663 * \retval #PSA_ERROR_INVALID_ARGUMENT
2664 * The total length of input to psa_aead_update_ad() so far is
2665 * less than the additional data length that was previously
2666 * specified with psa_aead_set_lengths().
2667 * \retval #PSA_ERROR_INVALID_ARGUMENT
2668 * The total length of input to psa_aead_update() so far is
2669 * less than the plaintext length that was previously
2670 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002671 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2672 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2673 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002674 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002675 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002676 * \retval #PSA_ERROR_BAD_STATE
2677 * The library has not been previously initialized by psa_crypto_init().
2678 * It is implementation-dependent whether a failure to initialize
2679 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002680 */
2681psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002682 uint8_t *ciphertext,
2683 size_t ciphertext_size,
2684 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002685 uint8_t *tag,
2686 size_t tag_size,
2687 size_t *tag_length);
2688
2689/** Finish authenticating and decrypting a message in an AEAD operation.
2690 *
2691 * The operation must have been set up with psa_aead_decrypt_setup().
2692 *
2693 * This function finishes the authentication of the additional data
2694 * formed by concatenating the inputs passed to preceding calls to
2695 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2696 * inputs passed to preceding calls to psa_aead_update().
2697 *
2698 * When this function returns, the operation becomes inactive.
2699 *
2700 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002701 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002702 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002703 * from previous calls to psa_aead_update()
2704 * that could not be processed until the end
2705 * of the input.
2706 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002707 * This must be at least
2708 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2709 * \c alg is the algorithm that is being
2710 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002711 * \param[out] plaintext_length On success, the number of bytes of
2712 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002713 * \param[in] tag Buffer containing the authentication tag.
2714 * \param tag_length Size of the \p tag buffer in bytes.
2715 *
2716 * \retval #PSA_SUCCESS
2717 * Success.
2718 * \retval #PSA_ERROR_BAD_STATE
2719 * The operation state is not valid (not set up, nonce not set,
2720 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002721 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2722 * The size of the \p plaintext buffer is too small.
2723 * You can determine a sufficient buffer size for \p plaintext by
2724 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2725 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002726 * \retval #PSA_ERROR_INVALID_ARGUMENT
2727 * The total length of input to psa_aead_update_ad() so far is
2728 * less than the additional data length that was previously
2729 * specified with psa_aead_set_lengths().
2730 * \retval #PSA_ERROR_INVALID_ARGUMENT
2731 * The total length of input to psa_aead_update() so far is
2732 * less than the plaintext length that was previously
2733 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002734 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2735 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2736 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002737 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002738 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002739 * \retval #PSA_ERROR_BAD_STATE
2740 * The library has not been previously initialized by psa_crypto_init().
2741 * It is implementation-dependent whether a failure to initialize
2742 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002743 */
2744psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002745 uint8_t *plaintext,
2746 size_t plaintext_size,
2747 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002748 const uint8_t *tag,
2749 size_t tag_length);
2750
2751/** Abort an AEAD operation.
2752 *
2753 * Aborting an operation frees all associated resources except for the
2754 * \p operation structure itself. Once aborted, the operation object
2755 * can be reused for another operation by calling
2756 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2757 *
2758 * You may call this function any time after the operation object has
2759 * been initialized by any of the following methods:
2760 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2761 * whether it succeeds or not.
2762 * - Initializing the \c struct to all-bits-zero.
2763 * - Initializing the \c struct to logical zeros, e.g.
2764 * `psa_aead_operation_t operation = {0}`.
2765 *
2766 * In particular, calling psa_aead_abort() after the operation has been
2767 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2768 * is safe and has no effect.
2769 *
2770 * \param[in,out] operation Initialized AEAD operation.
2771 *
2772 * \retval #PSA_SUCCESS
2773 * \retval #PSA_ERROR_BAD_STATE
2774 * \p operation is not an active AEAD operation.
2775 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2776 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002777 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002778 * \retval #PSA_ERROR_BAD_STATE
2779 * The library has not been previously initialized by psa_crypto_init().
2780 * It is implementation-dependent whether a failure to initialize
2781 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002782 */
2783psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2784
Gilles Peskine3b555712018-03-03 21:27:57 +01002785/**@}*/
2786
Gilles Peskine20035e32018-02-03 22:44:14 +01002787/** \defgroup asymmetric Asymmetric cryptography
2788 * @{
2789 */
2790
2791/**
2792 * \brief Sign a hash or short message with a private key.
2793 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002794 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002795 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002796 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2797 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2798 * to determine the hash algorithm to use.
2799 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002800 * \param handle Handle to the key to use for the operation.
2801 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002802 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002803 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002804 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002805 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002806 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002807 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002808 * \param[out] signature_length On success, the number of bytes
2809 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002810 *
Gilles Peskine28538492018-07-11 17:34:00 +02002811 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002812 * \retval #PSA_ERROR_INVALID_HANDLE
2813 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002814 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002815 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002816 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002817 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002818 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002819 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002820 * \retval #PSA_ERROR_NOT_SUPPORTED
2821 * \retval #PSA_ERROR_INVALID_ARGUMENT
2822 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2823 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2824 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002825 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002826 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002827 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002828 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002829 * The library has not been previously initialized by psa_crypto_init().
2830 * It is implementation-dependent whether a failure to initialize
2831 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002832 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002833psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002834 psa_algorithm_t alg,
2835 const uint8_t *hash,
2836 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002837 uint8_t *signature,
2838 size_t signature_size,
2839 size_t *signature_length);
2840
2841/**
2842 * \brief Verify the signature a hash or short message using a public key.
2843 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002844 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002845 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002846 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2847 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2848 * to determine the hash algorithm to use.
2849 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002850 * \param handle Handle to the key to use for the operation.
2851 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002852 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002853 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002854 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002855 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002856 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002857 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002858 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002859 *
Gilles Peskine28538492018-07-11 17:34:00 +02002860 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002861 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002862 * \retval #PSA_ERROR_INVALID_HANDLE
2863 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002864 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002865 * The calculation was perfomed successfully, but the passed
2866 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002867 * \retval #PSA_ERROR_NOT_SUPPORTED
2868 * \retval #PSA_ERROR_INVALID_ARGUMENT
2869 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2870 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2871 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002872 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002873 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002874 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002875 * The library has not been previously initialized by psa_crypto_init().
2876 * It is implementation-dependent whether a failure to initialize
2877 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002878 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002879psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002880 psa_algorithm_t alg,
2881 const uint8_t *hash,
2882 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002883 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002884 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002885
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002886/**
2887 * \brief Encrypt a short message with a public key.
2888 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002889 * \param handle Handle to the key to use for the operation.
2890 * It must be a public key or an asymmetric
2891 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002892 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002893 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002894 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002895 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002896 * \param[in] salt A salt or label, if supported by the
2897 * encryption algorithm.
2898 * If the algorithm does not support a
2899 * salt, pass \c NULL.
2900 * If the algorithm supports an optional
2901 * salt and you do not want to pass a salt,
2902 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002903 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002904 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2905 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002906 * \param salt_length Size of the \p salt buffer in bytes.
2907 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002908 * \param[out] output Buffer where the encrypted message is to
2909 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002910 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002911 * \param[out] output_length On success, the number of bytes
2912 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002913 *
Gilles Peskine28538492018-07-11 17:34:00 +02002914 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002915 * \retval #PSA_ERROR_INVALID_HANDLE
2916 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002917 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002918 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002919 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002920 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002921 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002922 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002923 * \retval #PSA_ERROR_NOT_SUPPORTED
2924 * \retval #PSA_ERROR_INVALID_ARGUMENT
2925 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2926 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2927 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002928 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002929 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002930 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002931 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002932 * The library has not been previously initialized by psa_crypto_init().
2933 * It is implementation-dependent whether a failure to initialize
2934 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002935 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002936psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002937 psa_algorithm_t alg,
2938 const uint8_t *input,
2939 size_t input_length,
2940 const uint8_t *salt,
2941 size_t salt_length,
2942 uint8_t *output,
2943 size_t output_size,
2944 size_t *output_length);
2945
2946/**
2947 * \brief Decrypt a short message with a private key.
2948 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002949 * \param handle Handle to the key to use for the operation.
2950 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002951 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002952 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002953 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002954 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002955 * \param[in] salt A salt or label, if supported by the
2956 * encryption algorithm.
2957 * If the algorithm does not support a
2958 * salt, pass \c NULL.
2959 * If the algorithm supports an optional
2960 * salt and you do not want to pass a salt,
2961 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002962 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002963 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2964 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002965 * \param salt_length Size of the \p salt buffer in bytes.
2966 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002967 * \param[out] output Buffer where the decrypted message is to
2968 * be written.
2969 * \param output_size Size of the \c output buffer in bytes.
2970 * \param[out] output_length On success, the number of bytes
2971 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002972 *
Gilles Peskine28538492018-07-11 17:34:00 +02002973 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002974 * \retval #PSA_ERROR_INVALID_HANDLE
2975 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002976 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002977 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002978 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002979 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002980 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002981 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002982 * \retval #PSA_ERROR_NOT_SUPPORTED
2983 * \retval #PSA_ERROR_INVALID_ARGUMENT
2984 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2985 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2986 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002987 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002988 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002989 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2990 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002991 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002992 * The library has not been previously initialized by psa_crypto_init().
2993 * It is implementation-dependent whether a failure to initialize
2994 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002995 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002996psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002997 psa_algorithm_t alg,
2998 const uint8_t *input,
2999 size_t input_length,
3000 const uint8_t *salt,
3001 size_t salt_length,
3002 uint8_t *output,
3003 size_t output_size,
3004 size_t *output_length);
3005
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003006/**@}*/
3007
Gilles Peskine35675b62019-05-16 17:26:11 +02003008/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003009 * @{
3010 */
3011
Gilles Peskine35675b62019-05-16 17:26:11 +02003012/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003013 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003014 * Before calling any function on a key derivation operation object, the
3015 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003016 * - Set the structure to all-bits-zero, for example:
3017 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003018 * psa_key_derivation_operation_t operation;
3019 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003020 * \endcode
3021 * - Initialize the structure to logical zero values, for example:
3022 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003023 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003024 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003025 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003026 * for example:
3027 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003028 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003029 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003030 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003031 * to the structure, for example:
3032 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003033 * psa_key_derivation_operation_t operation;
3034 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003035 * \endcode
3036 *
3037 * This is an implementation-defined \c struct. Applications should not
3038 * make any assumptions about the content of this structure except
3039 * as directed by the documentation of a specific implementation.
3040 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003041typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003042
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003043/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003044 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003045 * This macro returns a suitable initializer for a key derivation operation
3046 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003047 */
3048#ifdef __DOXYGEN_ONLY__
3049/* This is an example definition for documentation purposes.
3050 * Implementations should define a suitable value in `crypto_struct.h`.
3051 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003052#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003053#endif
3054
Gilles Peskine35675b62019-05-16 17:26:11 +02003055/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003056 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003057static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003058
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003059/** Set up a key derivation operation.
3060 *
3061 * A key derivation algorithm takes some inputs and uses them to generate
3062 * a byte stream in a deterministic way.
3063 * This byte stream can be used to produce keys and other
3064 * cryptographic material.
3065 *
3066 * To derive a key:
3067 * - Start with an initialized object of type #psa_key_derivation_operation_t.
3068 * - Call psa_key_derivation_setup() to select the algorithm.
3069 * - Provide the inputs for the key derivation by calling
3070 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3071 * as appropriate. Which inputs are needed, in what order, and whether
3072 * they may be keys and if so of what type depends on the algorithm.
3073 * - Optionally set the operation's maximum capacity with
3074 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3075 * of or after providing inputs. For some algorithms, this step is mandatory
3076 * because the output depends on the maximum capacity.
3077 * - To derive a key, call psa_key_derivation_output_key().
3078 * To derive a byte string for a different purpose, call
3079 * - psa_key_derivation_output_bytes().
3080 * Successive calls to these functions use successive output bytes
3081 * calculated by the key derivation algorithm.
3082 * - Clean up the key derivation operation object with
3083 * psa_key_derivation_abort().
3084 *
3085 * \param[in,out] operation The key derivation operation object
3086 * to set up. It must
3087 * have been initialized but not set up yet.
3088 * \param alg The key derivation algorithm to compute
3089 * (\c PSA_ALG_XXX value such that
3090 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3091 *
3092 * \retval #PSA_SUCCESS
3093 * Success.
3094 * \retval #PSA_ERROR_INVALID_ARGUMENT
3095 * \c alg is not a key derivation algorithm.
3096 * \retval #PSA_ERROR_NOT_SUPPORTED
3097 * \c alg is not supported or is not a key derivation algorithm.
3098 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3099 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3100 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003101 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003102 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003103 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01003104 * The operation state is either not initialized or has already been setup.
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003105 * \retval #PSA_ERROR_BAD_STATE
3106 * The library has not been previously initialized by psa_crypto_init().
3107 * It is implementation-dependent whether a failure to initialize
3108 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003109 */
3110psa_status_t psa_key_derivation_setup(
3111 psa_key_derivation_operation_t *operation,
3112 psa_algorithm_t alg);
3113
Gilles Peskine35675b62019-05-16 17:26:11 +02003114/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003115 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003116 * The capacity of a key derivation is the maximum number of bytes that it can
3117 * return. When you get *N* bytes of output from a key derivation operation,
3118 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003119 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003120 * \param[in] operation The operation to query.
3121 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003122 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003123 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003124 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003125 * \retval #PSA_ERROR_BAD_STATE
3126 * The operation state is not valid.
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003127 * \retval #PSA_ERROR_HARDWARE_FAILURE
3128 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003129 * \retval #PSA_ERROR_BAD_STATE
3130 * The library has not been previously initialized by psa_crypto_init().
3131 * It is implementation-dependent whether a failure to initialize
3132 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003133 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003134psa_status_t psa_key_derivation_get_capacity(
3135 const psa_key_derivation_operation_t *operation,
3136 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003137
Gilles Peskine35675b62019-05-16 17:26:11 +02003138/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003139 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003140 * The capacity of a key derivation operation is the maximum number of bytes
3141 * that the key derivation operation can return from this point onwards.
3142 *
3143 * \param[in,out] operation The key derivation operation object to modify.
3144 * \param capacity The new capacity of the operation.
3145 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003146 * current capacity.
3147 *
3148 * \retval #PSA_SUCCESS
3149 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003150 * \p capacity is larger than the operation's current capacity.
3151 * In this case, the operation object remains valid and its capacity
3152 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003153 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003154 * The operation state is not valid.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003155 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003156 * \retval #PSA_ERROR_HARDWARE_FAILURE
3157 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003158 * \retval #PSA_ERROR_BAD_STATE
3159 * The library has not been previously initialized by psa_crypto_init().
3160 * It is implementation-dependent whether a failure to initialize
3161 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003162 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003163psa_status_t psa_key_derivation_set_capacity(
3164 psa_key_derivation_operation_t *operation,
3165 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003166
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003167/** Use the maximum possible capacity for a key derivation operation.
3168 *
3169 * Use this value as the capacity argument when setting up a key derivation
3170 * to indicate that the operation should have the maximum possible capacity.
3171 * The value of the maximum possible capacity depends on the key derivation
3172 * algorithm.
3173 */
3174#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3175
3176/** Provide an input for key derivation or key agreement.
3177 *
3178 * Which inputs are required and in what order depends on the algorithm.
3179 * Refer to the documentation of each key derivation or key agreement
3180 * algorithm for information.
3181 *
3182 * This function passes direct inputs. Some inputs must be passed as keys
3183 * using psa_key_derivation_input_key() instead of this function. Refer to
3184 * the documentation of individual step types for information.
3185 *
3186 * \param[in,out] operation The key derivation operation object to use.
3187 * It must have been set up with
3188 * psa_key_derivation_setup() and must not
3189 * have produced any output yet.
3190 * \param step Which step the input data is for.
3191 * \param[in] data Input data to use.
3192 * \param data_length Size of the \p data buffer in bytes.
3193 *
3194 * \retval #PSA_SUCCESS
3195 * Success.
3196 * \retval #PSA_ERROR_INVALID_ARGUMENT
3197 * \c step is not compatible with the operation's algorithm.
3198 * \retval #PSA_ERROR_INVALID_ARGUMENT
3199 * \c step does not allow direct inputs.
3200 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3201 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3202 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003203 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003204 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003205 * \retval #PSA_ERROR_BAD_STATE
3206 * The value of \p step is not valid given the state of \p operation.
3207 * \retval #PSA_ERROR_BAD_STATE
3208 * The library has not been previously initialized by psa_crypto_init().
3209 * It is implementation-dependent whether a failure to initialize
3210 * results in this error code.
3211 */
3212psa_status_t psa_key_derivation_input_bytes(
3213 psa_key_derivation_operation_t *operation,
3214 psa_key_derivation_step_t step,
3215 const uint8_t *data,
3216 size_t data_length);
3217
3218/** Provide an input for key derivation in the form of a key.
3219 *
3220 * Which inputs are required and in what order depends on the algorithm.
3221 * Refer to the documentation of each key derivation or key agreement
3222 * algorithm for information.
3223 *
3224 * This function passes key inputs. Some inputs must be passed as keys
3225 * of the appropriate type using this function, while others must be
3226 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3227 * the documentation of individual step types for information.
3228 *
3229 * \param[in,out] operation The key derivation operation object to use.
3230 * It must have been set up with
3231 * psa_key_derivation_setup() and must not
3232 * have produced any output yet.
3233 * \param step Which step the input data is for.
3234 * \param handle Handle to the key. It must have an
3235 * appropriate type for \p step and must
3236 * allow the usage #PSA_KEY_USAGE_DERIVE.
3237 *
3238 * \retval #PSA_SUCCESS
3239 * Success.
3240 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003241 * \retval #PSA_ERROR_NOT_PERMITTED
3242 * \retval #PSA_ERROR_INVALID_ARGUMENT
3243 * \c step is not compatible with the operation's algorithm.
3244 * \retval #PSA_ERROR_INVALID_ARGUMENT
3245 * \c step does not allow key inputs.
3246 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3247 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3248 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003249 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003250 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003251 * \retval #PSA_ERROR_BAD_STATE
3252 * The value of \p step is not valid given the state of \p operation.
3253 * \retval #PSA_ERROR_BAD_STATE
3254 * The library has not been previously initialized by psa_crypto_init().
3255 * It is implementation-dependent whether a failure to initialize
3256 * results in this error code.
3257 */
3258psa_status_t psa_key_derivation_input_key(
3259 psa_key_derivation_operation_t *operation,
3260 psa_key_derivation_step_t step,
3261 psa_key_handle_t handle);
3262
3263/** Perform a key agreement and use the shared secret as input to a key
3264 * derivation.
3265 *
3266 * A key agreement algorithm takes two inputs: a private key \p private_key
3267 * a public key \p peer_key.
3268 * The result of this function is passed as input to a key derivation.
3269 * The output of this key derivation can be extracted by reading from the
3270 * resulting operation to produce keys and other cryptographic material.
3271 *
3272 * \param[in,out] operation The key derivation operation object to use.
3273 * It must have been set up with
3274 * psa_key_derivation_setup() with a
3275 * key agreement and derivation algorithm
3276 * \c alg (\c PSA_ALG_XXX value such that
3277 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3278 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3279 * is false).
3280 * The operation must be ready for an
3281 * input of the type given by \p step.
3282 * \param step Which step the input data is for.
3283 * \param private_key Handle to the private key to use.
3284 * \param[in] peer_key Public key of the peer. The peer key must be in the
3285 * same format that psa_import_key() accepts for the
3286 * public key type corresponding to the type of
3287 * private_key. That is, this function performs the
3288 * equivalent of
3289 * #psa_import_key(...,
3290 * `peer_key`, `peer_key_length`) where
3291 * with key attributes indicating the public key
3292 * type corresponding to the type of `private_key`.
3293 * For example, for EC keys, this means that peer_key
3294 * is interpreted as a point on the curve that the
3295 * private key is on. The standard formats for public
3296 * keys are documented in the documentation of
3297 * psa_export_public_key().
3298 * \param peer_key_length Size of \p peer_key in bytes.
3299 *
3300 * \retval #PSA_SUCCESS
3301 * Success.
3302 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003303 * \retval #PSA_ERROR_NOT_PERMITTED
3304 * \retval #PSA_ERROR_INVALID_ARGUMENT
3305 * \c private_key is not compatible with \c alg,
3306 * or \p peer_key is not valid for \c alg or not compatible with
3307 * \c private_key.
3308 * \retval #PSA_ERROR_NOT_SUPPORTED
3309 * \c alg is not supported or is not a key derivation algorithm.
3310 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3311 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3312 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003313 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003314 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003315 * \retval #PSA_ERROR_BAD_STATE
3316 * The library has not been previously initialized by psa_crypto_init().
3317 * It is implementation-dependent whether a failure to initialize
3318 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003319 */
3320psa_status_t psa_key_derivation_key_agreement(
3321 psa_key_derivation_operation_t *operation,
3322 psa_key_derivation_step_t step,
3323 psa_key_handle_t private_key,
3324 const uint8_t *peer_key,
3325 size_t peer_key_length);
3326
Gilles Peskine35675b62019-05-16 17:26:11 +02003327/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003328 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003329 * This function calculates output bytes from a key derivation algorithm and
3330 * return those bytes.
3331 * If you view the key derivation's output as a stream of bytes, this
3332 * function destructively reads the requested number of bytes from the
3333 * stream.
3334 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003335 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003336 * \param[in,out] operation The key derivation operation object to read from.
3337 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003338 * \param output_length Number of bytes to output.
3339 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003340 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003341 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003342 * The operation's capacity was less than
3343 * \p output_length bytes. Note that in this case,
3344 * no output is written to the output buffer.
3345 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003346 * subsequent calls to this function will not
3347 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003348 * \retval #PSA_ERROR_BAD_STATE
3349 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3350 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3351 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003352 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003353 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003354 * \retval #PSA_ERROR_BAD_STATE
3355 * The library has not been previously initialized by psa_crypto_init().
3356 * It is implementation-dependent whether a failure to initialize
3357 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003358 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003359psa_status_t psa_key_derivation_output_bytes(
3360 psa_key_derivation_operation_t *operation,
3361 uint8_t *output,
3362 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003363
Gilles Peskine35675b62019-05-16 17:26:11 +02003364/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003365 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003366 * This function calculates output bytes from a key derivation algorithm
3367 * and uses those bytes to generate a key deterministically.
3368 * If you view the key derivation's output as a stream of bytes, this
3369 * function destructively reads as many bytes as required from the
3370 * stream.
3371 * The operation's capacity decreases by the number of bytes read.
3372 *
3373 * How much output is produced and consumed from the operation, and how
3374 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003375 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003376 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003377 * of a given size, this function is functionally equivalent to
3378 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003379 * and passing the resulting output to #psa_import_key.
3380 * However, this function has a security benefit:
3381 * if the implementation provides an isolation boundary then
3382 * the key material is not exposed outside the isolation boundary.
3383 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003384 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003385 * The following key types defined in this specification follow this scheme:
3386 *
3387 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003388 * - #PSA_KEY_TYPE_ARC4;
3389 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003390 * - #PSA_KEY_TYPE_DERIVE;
3391 * - #PSA_KEY_TYPE_HMAC.
3392 *
3393 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003394 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003395 * Montgomery curve), this function always draws a byte string whose
3396 * length is determined by the curve, and sets the mandatory bits
3397 * accordingly. That is:
3398 *
3399 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3400 * and process it as specified in RFC 7748 &sect;5.
3401 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3402 * and process it as specified in RFC 7748 &sect;5.
3403 *
3404 * - For key types for which the key is represented by a single sequence of
3405 * \p bits bits with constraints as to which bit sequences are acceptable,
3406 * this function draws a byte string of length (\p bits / 8) bytes rounded
3407 * up to the nearest whole number of bytes. If the resulting byte string
3408 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3409 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003410 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003411 * for the output produced by psa_export_key().
3412 * The following key types defined in this specification follow this scheme:
3413 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003414 * - #PSA_KEY_TYPE_DES.
3415 * Force-set the parity bits, but discard forbidden weak keys.
3416 * For 2-key and 3-key triple-DES, the three keys are generated
3417 * successively (for example, for 3-key triple-DES,
3418 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3419 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003420 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003421 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003422 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003423 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003424 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003425 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003426 * Weierstrass curve).
3427 * For these key types, interpret the byte string as integer
3428 * in big-endian order. Discard it if it is not in the range
3429 * [0, *N* - 2] where *N* is the boundary of the private key domain
3430 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003431 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003432 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003433 * This method allows compliance to NIST standards, specifically
3434 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003435 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3436 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3437 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3438 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003439 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003440 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003441 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003442 * implementation-defined.
3443 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003444 * In all cases, the data that is read is discarded from the operation.
3445 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003446 *
Gilles Peskine20628592019-04-19 19:29:50 +02003447 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003448 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003449 * \param[out] handle On success, a handle to the newly created key.
3450 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003451 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003452 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003453 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003454 * If the key is persistent, the key material and the key's metadata
3455 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003456 * \retval #PSA_ERROR_ALREADY_EXISTS
3457 * This is an attempt to create a persistent key, and there is
3458 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003459 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003460 * There was not enough data to create the desired key.
3461 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003462 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003463 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003464 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003465 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003466 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003467 * \retval #PSA_ERROR_INVALID_ARGUMENT
3468 * The provided key attributes are not valid for the operation.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003469 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003470 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3471 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3472 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3473 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003474 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003475 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003476 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003477 * The library has not been previously initialized by psa_crypto_init().
3478 * It is implementation-dependent whether a failure to initialize
3479 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003480 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003481psa_status_t psa_key_derivation_output_key(
3482 const psa_key_attributes_t *attributes,
3483 psa_key_derivation_operation_t *operation,
3484 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003485
Gilles Peskine35675b62019-05-16 17:26:11 +02003486/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003487 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003488 * Once a key derivation operation has been aborted, its capacity is zero.
3489 * Aborting an operation frees all associated resources except for the
3490 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003491 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003492 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003493 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003494 * psa_key_derivation_operation_init() or a zero value. In particular,
3495 * it is valid to call psa_key_derivation_abort() twice, or to call
3496 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003497 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003498 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003499 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003500 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003501 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003502 * \retval #PSA_SUCCESS
3503 * \retval #PSA_ERROR_BAD_STATE
3504 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3505 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003506 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003507 * \retval #PSA_ERROR_BAD_STATE
3508 * The library has not been previously initialized by psa_crypto_init().
3509 * It is implementation-dependent whether a failure to initialize
3510 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003511 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003512psa_status_t psa_key_derivation_abort(
3513 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003514
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003515/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003516 *
3517 * \warning The raw result of a key agreement algorithm such as finite-field
3518 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3519 * not be used directly as key material. It should instead be passed as
3520 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003521 * a key derivation, use psa_key_derivation_key_agreement() and other
3522 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003523 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003524 * \param alg The key agreement algorithm to compute
3525 * (\c PSA_ALG_XXX value such that
3526 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3527 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003528 * \param private_key Handle to the private key to use.
3529 * \param[in] peer_key Public key of the peer. It must be
3530 * in the same format that psa_import_key()
3531 * accepts. The standard formats for public
3532 * keys are documented in the documentation
3533 * of psa_export_public_key().
3534 * \param peer_key_length Size of \p peer_key in bytes.
3535 * \param[out] output Buffer where the decrypted message is to
3536 * be written.
3537 * \param output_size Size of the \c output buffer in bytes.
3538 * \param[out] output_length On success, the number of bytes
3539 * that make up the returned output.
3540 *
3541 * \retval #PSA_SUCCESS
3542 * Success.
3543 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003544 * \retval #PSA_ERROR_NOT_PERMITTED
3545 * \retval #PSA_ERROR_INVALID_ARGUMENT
3546 * \p alg is not a key agreement algorithm
3547 * \retval #PSA_ERROR_INVALID_ARGUMENT
3548 * \p private_key is not compatible with \p alg,
3549 * or \p peer_key is not valid for \p alg or not compatible with
3550 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003551 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3552 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003553 * \retval #PSA_ERROR_NOT_SUPPORTED
3554 * \p alg is not a supported key agreement algorithm.
3555 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3556 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3557 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003558 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003559 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003560 * \retval #PSA_ERROR_BAD_STATE
3561 * The library has not been previously initialized by psa_crypto_init().
3562 * It is implementation-dependent whether a failure to initialize
3563 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003564 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003565psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3566 psa_key_handle_t private_key,
3567 const uint8_t *peer_key,
3568 size_t peer_key_length,
3569 uint8_t *output,
3570 size_t output_size,
3571 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003572
Gilles Peskineea0fb492018-07-12 17:17:20 +02003573/**@}*/
3574
Gilles Peskineedd76872018-07-20 17:42:05 +02003575/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003576 * @{
3577 */
3578
3579/**
3580 * \brief Generate random bytes.
3581 *
3582 * \warning This function **can** fail! Callers MUST check the return status
3583 * and MUST NOT use the content of the output buffer if the return
3584 * status is not #PSA_SUCCESS.
3585 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003586 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003587 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003588 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003589 * \param output_size Number of bytes to generate and output.
3590 *
Gilles Peskine28538492018-07-11 17:34:00 +02003591 * \retval #PSA_SUCCESS
3592 * \retval #PSA_ERROR_NOT_SUPPORTED
3593 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003594 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003595 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3596 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003597 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003598 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003599 * The library has not been previously initialized by psa_crypto_init().
3600 * It is implementation-dependent whether a failure to initialize
3601 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003602 */
3603psa_status_t psa_generate_random(uint8_t *output,
3604 size_t output_size);
3605
3606/**
3607 * \brief Generate a key or key pair.
3608 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003609 * The key is generated randomly.
3610 * Its location, policy, type and size are taken from \p attributes.
3611 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003612 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003613 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003614 * the public exponent is 65537.
3615 * The modulus is a product of two probabilistic primes
3616 * between 2^{n-1} and 2^n where n is the bit size specified in the
3617 * attributes.
3618 *
Gilles Peskine20628592019-04-19 19:29:50 +02003619 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003620 * \param[out] handle On success, a handle to the newly created key.
3621 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003622 *
Gilles Peskine28538492018-07-11 17:34:00 +02003623 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003624 * Success.
3625 * If the key is persistent, the key material and the key's metadata
3626 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003627 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003628 * This is an attempt to create a persistent key, and there is
3629 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003630 * \retval #PSA_ERROR_NOT_SUPPORTED
3631 * \retval #PSA_ERROR_INVALID_ARGUMENT
3632 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3633 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3634 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3635 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003636 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003637 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3638 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003639 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003640 * The library has not been previously initialized by psa_crypto_init().
3641 * It is implementation-dependent whether a failure to initialize
3642 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003643 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003644psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003645 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003646
3647/**@}*/
3648
Gilles Peskinee59236f2018-01-27 23:32:46 +01003649#ifdef __cplusplus
3650}
3651#endif
3652
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003653/* The file "crypto_sizes.h" contains definitions for size calculation
3654 * macros whose definitions are implementation-specific. */
3655#include "crypto_sizes.h"
3656
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003657/* The file "crypto_struct.h" contains definitions for
3658 * implementation-specific structs that are declared above. */
3659#include "crypto_struct.h"
3660
3661/* The file "crypto_extra.h" contains vendor-specific definitions. This
3662 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003663#include "crypto_extra.h"
3664
3665#endif /* PSA_CRYPTO_H */