blob: d2788caab1fe69cf8a5b7995005308874900046f [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +020089 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +020090 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
Gilles Peskine105f67f2019-07-23 18:16:05 +020096/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +020097 * @{
98 */
99
Gilles Peskinea0c06552019-05-21 15:54:54 +0200100/** \def PSA_KEY_ATTRIBUTES_INIT
101 *
102 * This macro returns a suitable initializer for a key attribute structure
103 * of type #psa_key_attributes_t.
104 */
105#ifdef __DOXYGEN_ONLY__
106/* This is an example definition for documentation purposes.
107 * Implementations should define a suitable value in `crypto_struct.h`.
108 */
109#define PSA_KEY_ATTRIBUTES_INIT {0}
110#endif
111
112/** Return an initial value for a key attributes structure.
113 */
114static psa_key_attributes_t psa_key_attributes_init(void);
115
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200116/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200117 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200118 * If the attribute structure currently declares the key as volatile (which
119 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200120 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200121 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200122 * This function does not access storage, it merely stores the given
123 * value in the structure.
124 * The persistent key will be written to storage when the attribute
125 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200126 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200127 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200128 *
Gilles Peskine20628592019-04-19 19:29:50 +0200129 * This function may be declared as `static` (i.e. without external
130 * linkage). This function may be provided as a function-like macro,
131 * but in this case it must evaluate each of its arguments exactly once.
132 *
133 * \param[out] attributes The attribute structure to write to.
134 * \param id The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200135 */
136static void psa_set_key_id(psa_key_attributes_t *attributes,
137 psa_key_id_t id);
138
139/** Set the location of a persistent key.
140 *
141 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200142 * with psa_set_key_id(). By default, a key that has a persistent identifier
143 * is stored in the default storage area identifier by
144 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
145 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200146 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200147 * This function does not access storage, it merely stores the given
148 * value in the structure.
149 * The persistent key will be written to storage when the attribute
150 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200151 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200152 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200153 *
154 * This function may be declared as `static` (i.e. without external
155 * linkage). This function may be provided as a function-like macro,
156 * but in this case it must evaluate each of its arguments exactly once.
157 *
158 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200159 * \param lifetime The lifetime for the key.
160 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200161 * key will be volatile, and the key identifier
162 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200163 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200164static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
165 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200166
Gilles Peskine20628592019-04-19 19:29:50 +0200167/** Retrieve the key identifier from key attributes.
168 *
169 * This function may be declared as `static` (i.e. without external
170 * linkage). This function may be provided as a function-like macro,
171 * but in this case it must evaluate its argument exactly once.
172 *
173 * \param[in] attributes The key attribute structure to query.
174 *
175 * \return The persistent identifier stored in the attribute structure.
176 * This value is unspecified if the attribute structure declares
177 * the key as volatile.
178 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200179static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
180
Gilles Peskine20628592019-04-19 19:29:50 +0200181/** Retrieve the lifetime from key attributes.
182 *
183 * This function may be declared as `static` (i.e. without external
184 * linkage). This function may be provided as a function-like macro,
185 * but in this case it must evaluate its argument exactly once.
186 *
187 * \param[in] attributes The key attribute structure to query.
188 *
189 * \return The lifetime value stored in the attribute structure.
190 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200191static psa_key_lifetime_t psa_get_key_lifetime(
192 const psa_key_attributes_t *attributes);
193
Gilles Peskine20628592019-04-19 19:29:50 +0200194/** Declare usage flags for a key.
195 *
196 * Usage flags are part of a key's usage policy. They encode what
197 * kind of operations are permitted on the key. For more details,
198 * refer to the documentation of the type #psa_key_usage_t.
199 *
200 * This function overwrites any usage flags
201 * previously set in \p attributes.
202 *
203 * This function may be declared as `static` (i.e. without external
204 * linkage). This function may be provided as a function-like macro,
205 * but in this case it must evaluate each of its arguments exactly once.
206 *
207 * \param[out] attributes The attribute structure to write to.
208 * \param usage_flags The usage flags to write.
209 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200210static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
211 psa_key_usage_t usage_flags);
212
Gilles Peskine20628592019-04-19 19:29:50 +0200213/** Retrieve the usage flags from key attributes.
214 *
215 * This function may be declared as `static` (i.e. without external
216 * linkage). This function may be provided as a function-like macro,
217 * but in this case it must evaluate its argument exactly once.
218 *
219 * \param[in] attributes The key attribute structure to query.
220 *
221 * \return The usage flags stored in the attribute structure.
222 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200223static psa_key_usage_t psa_get_key_usage_flags(
224 const psa_key_attributes_t *attributes);
225
Gilles Peskine20628592019-04-19 19:29:50 +0200226/** Declare the permitted algorithm policy for a key.
227 *
228 * The permitted algorithm policy of a key encodes which algorithm or
229 * algorithms are permitted to be used with this key.
230 *
231 * This function overwrites any algorithm policy
232 * previously set in \p attributes.
233 *
234 * This function may be declared as `static` (i.e. without external
235 * linkage). This function may be provided as a function-like macro,
236 * but in this case it must evaluate each of its arguments exactly once.
237 *
238 * \param[out] attributes The attribute structure to write to.
239 * \param alg The permitted algorithm policy to write.
240 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200241static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
242 psa_algorithm_t alg);
243
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100244
Gilles Peskine20628592019-04-19 19:29:50 +0200245/** Retrieve the algorithm policy from key attributes.
246 *
247 * This function may be declared as `static` (i.e. without external
248 * linkage). This function may be provided as a function-like macro,
249 * but in this case it must evaluate its argument exactly once.
250 *
251 * \param[in] attributes The key attribute structure to query.
252 *
253 * \return The algorithm stored in the attribute structure.
254 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200255static psa_algorithm_t psa_get_key_algorithm(
256 const psa_key_attributes_t *attributes);
257
Gilles Peskine20628592019-04-19 19:29:50 +0200258/** Declare the type of a key.
259 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200260 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200261 * previously set in \p attributes.
262 *
263 * This function may be declared as `static` (i.e. without external
264 * linkage). This function may be provided as a function-like macro,
265 * but in this case it must evaluate each of its arguments exactly once.
266 *
267 * \param[out] attributes The attribute structure to write to.
268 * \param type The key type to write.
269 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200270static void psa_set_key_type(psa_key_attributes_t *attributes,
271 psa_key_type_t type);
272
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100273
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200274/** Declare the size of a key.
275 *
276 * This function overwrites any key size previously set in \p attributes.
277 *
278 * This function may be declared as `static` (i.e. without external
279 * linkage). This function may be provided as a function-like macro,
280 * but in this case it must evaluate each of its arguments exactly once.
281 *
282 * \param[out] attributes The attribute structure to write to.
283 * \param bits The key size in bits.
284 */
285static void psa_set_key_bits(psa_key_attributes_t *attributes,
286 size_t bits);
287
Gilles Peskine20628592019-04-19 19:29:50 +0200288/** Retrieve the key type from key attributes.
289 *
290 * This function may be declared as `static` (i.e. without external
291 * linkage). This function may be provided as a function-like macro,
292 * but in this case it must evaluate its argument exactly once.
293 *
294 * \param[in] attributes The key attribute structure to query.
295 *
296 * \return The key type stored in the attribute structure.
297 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200298static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
299
Gilles Peskine20628592019-04-19 19:29:50 +0200300/** Retrieve the key size from key attributes.
301 *
302 * This function may be declared as `static` (i.e. without external
303 * linkage). This function may be provided as a function-like macro,
304 * but in this case it must evaluate its argument exactly once.
305 *
306 * \param[in] attributes The key attribute structure to query.
307 *
308 * \return The key size stored in the attribute structure, in bits.
309 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200310static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
311
Gilles Peskine20628592019-04-19 19:29:50 +0200312/** Retrieve the attributes of a key.
313 *
314 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200315 * psa_reset_key_attributes(). It then copies the attributes of
316 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200317 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200318 * \note This function may allocate memory or other resources.
319 * Once you have called this function on an attribute structure,
320 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200321 *
Gilles Peskine20628592019-04-19 19:29:50 +0200322 * \param[in] handle Handle to the key to query.
323 * \param[in,out] attributes On success, the attributes of the key.
324 * On failure, equivalent to a
325 * freshly-initialized structure.
326 *
327 * \retval #PSA_SUCCESS
328 * \retval #PSA_ERROR_INVALID_HANDLE
329 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
330 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100331 * \retval #PSA_ERROR_CORRUPTION_DETECTED
332 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100333 * \retval #PSA_ERROR_BAD_STATE
334 * The library has not been previously initialized by psa_crypto_init().
335 * It is implementation-dependent whether a failure to initialize
336 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200337 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200338psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
339 psa_key_attributes_t *attributes);
340
Gilles Peskine20628592019-04-19 19:29:50 +0200341/** Reset a key attribute structure to a freshly initialized state.
342 *
343 * You must initialize the attribute structure as described in the
344 * documentation of the type #psa_key_attributes_t before calling this
345 * function. Once the structure has been initialized, you may call this
346 * function at any time.
347 *
348 * This function frees any auxiliary resources that the structure
349 * may contain.
350 *
351 * \param[in,out] attributes The attribute structure to reset.
352 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200353void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200354
Gilles Peskine87a5e562019-04-17 12:28:25 +0200355/**@}*/
356
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357/** \defgroup key_management Key management
358 * @{
359 */
360
Gilles Peskinef535eb22018-11-30 14:08:36 +0100361/** Open a handle to an existing persistent key.
362 *
Gilles Peskine4754cde2019-05-21 15:56:29 +0200363 * Open a handle to a persistent key. A key is persistent if it was created
364 * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
365 * always has a nonzero key identifier, set with psa_set_key_id() when
366 * creating the key. Implementations may provide additional pre-provisioned
Andrew Thoelke203491c2019-08-21 17:55:30 +0100367 * keys that can be opened with psa_open_key(). Such keys have a key identifier
368 * in the vendor range, as documented in the description of #psa_key_id_t.
Gilles Peskine4754cde2019-05-21 15:56:29 +0200369 *
Andrew Thoelkede183412019-09-05 09:38:06 +0100370 * The application must eventually close the handle with psa_close_key() or
371 * psa_destroy_key() to release associated resources. If the application dies
372 * without calling one of these functions, the implementation should perform
373 * the equivalent of a call to psa_close_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100374 *
Andrew Thoelke9741b112019-08-21 18:20:41 +0100375 * Some implementations permit an application to open the same key multiple
Andrew Thoelkede183412019-09-05 09:38:06 +0100376 * times. If this is successful, each call to psa_open_key() will return a
377 * different key handle.
378 *
379 * \note Applications that rely on opening a key multiple times will not be
380 * portable to implementations that only permit a single key handle to be
381 * opened. See also :ref:\`key-handles\`.
Andrew Thoelke9741b112019-08-21 18:20:41 +0100382 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100383 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100384 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100385 *
386 * \retval #PSA_SUCCESS
387 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100388 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100389 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Andrew Thoelke9741b112019-08-21 18:20:41 +0100390 * The implementation does not have sufficient resources to open the
391 * key. This can be due to reaching an implementation limit on the
392 * number of open keys, the number of open key handles, or available
393 * memory.
David Saadab4ecc272019-02-14 13:48:10 +0200394 * \retval #PSA_ERROR_DOES_NOT_EXIST
Andrew Thoelke9741b112019-08-21 18:20:41 +0100395 * There is no persistent key with key identifier \p id.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100396 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrew Thoelke9741b112019-08-21 18:20:41 +0100397 * \p id is not a valid persistent key identifier.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100398 * \retval #PSA_ERROR_NOT_PERMITTED
399 * The specified key exists, but the application does not have the
400 * permission to access it. Note that this specification does not
401 * define any way to create such a key, but it may be possible
402 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200403 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawd789dc12019-08-12 15:06:48 +0100404 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine225010f2019-05-06 18:44:55 +0200405 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100406 * \retval #PSA_ERROR_BAD_STATE
407 * The library has not been previously initialized by psa_crypto_init().
408 * It is implementation-dependent whether a failure to initialize
409 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100410 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200411psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100412 psa_key_handle_t *handle);
413
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100414
Gilles Peskinef535eb22018-11-30 14:08:36 +0100415/** Close a key handle.
416 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100417 * If the handle designates a volatile key, this will destroy the key material
418 * and free all associated resources, just like psa_destroy_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100419 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100420 * If this is the last open handle to a persistent key, then closing the handle
421 * will free all resources associated with the key in volatile memory. The key
422 * data in persistent storage is not affected and can be opened again later
423 * with a call to psa_open_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100424 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100425 * Closing the key handle makes the handle invalid, and the key handle
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100426 * must not be used again by the application.
Andrew Thoelke3daba812019-08-21 22:46:56 +0100427 *
Andrew Thoelke970629f2019-09-09 09:56:34 +0100428 * \note If the key handle was used to set up an active
Andrew Thoelkede183412019-09-05 09:38:06 +0100429 * :ref:\`multipart operation <multipart-operations>\`, then closing the
430 * key handle can cause the multipart operation to fail. Applications should
431 * maintain the key handle until after the multipart operation has finished.
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100432 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100433 * \param handle The key handle to close.
434 *
435 * \retval #PSA_SUCCESS
436 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100437 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawf97c8522019-08-15 13:27:12 +0100438 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100439 * \retval #PSA_ERROR_BAD_STATE
440 * The library has not been previously initialized by psa_crypto_init().
441 * It is implementation-dependent whether a failure to initialize
442 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100443 */
444psa_status_t psa_close_key(psa_key_handle_t handle);
445
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100446/** Make a copy of a key.
447 *
448 * Copy key material from one location to another.
449 *
450 * This function is primarily useful to copy a key from one location
451 * to another, since it populates a key using the material from
452 * another key which may have a different lifetime.
453 *
454 * This function may be used to share a key with a different party,
455 * subject to implementation-defined restrictions on key sharing.
456 *
457 * The policy on the source key must have the usage flag
458 * #PSA_KEY_USAGE_COPY set.
459 * This flag is sufficient to permit the copy if the key has the lifetime
460 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
461 * Some secure elements do not provide a way to copy a key without
462 * making it extractable from the secure element. If a key is located
463 * in such a secure element, then the key must have both usage flags
464 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
465 * a copy of the key outside the secure element.
466 *
467 * The resulting key may only be used in a way that conforms to
468 * both the policy of the original key and the policy specified in
469 * the \p attributes parameter:
470 * - The usage flags on the resulting key are the bitwise-and of the
471 * usage flags on the source policy and the usage flags in \p attributes.
472 * - If both allow the same algorithm or wildcard-based
473 * algorithm policy, the resulting key has the same algorithm policy.
474 * - If either of the policies allows an algorithm and the other policy
475 * allows a wildcard-based algorithm policy that includes this algorithm,
476 * the resulting key allows the same algorithm.
477 * - If the policies do not allow any algorithm in common, this function
478 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
479 *
480 * The effect of this function on implementation-defined attributes is
481 * implementation-defined.
482 *
483 * \param source_handle The key to copy. It must be a valid key handle.
484 * \param[in] attributes The attributes for the new key.
485 * They are used as follows:
486 * - The key type and size may be 0. If either is
487 * nonzero, it must match the corresponding
488 * attribute of the source key.
489 * - The key location (the lifetime and, for
490 * persistent keys, the key identifier) is
491 * used directly.
492 * - The policy constraints (usage flags and
493 * algorithm policy) are combined from
494 * the source key and \p attributes so that
495 * both sets of restrictions apply, as
496 * described in the documentation of this function.
497 * \param[out] target_handle On success, a handle to the newly created key.
498 * \c 0 on failure.
499 *
500 * \retval #PSA_SUCCESS
501 * \retval #PSA_ERROR_INVALID_HANDLE
502 * \p source_handle is invalid.
503 * \retval #PSA_ERROR_ALREADY_EXISTS
504 * This is an attempt to create a persistent key, and there is
505 * already a persistent key with the given identifier.
506 * \retval #PSA_ERROR_INVALID_ARGUMENT
507 * The lifetime or identifier in \p attributes are invalid.
508 * \retval #PSA_ERROR_INVALID_ARGUMENT
509 * The policy constraints on the source and specified in
510 * \p attributes are incompatible.
511 * \retval #PSA_ERROR_INVALID_ARGUMENT
512 * \p attributes specifies a key type or key size
513 * which does not match the attributes of the source key.
514 * \retval #PSA_ERROR_NOT_PERMITTED
515 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
516 * \retval #PSA_ERROR_NOT_PERMITTED
517 * The source key is not exportable and its lifetime does not
518 * allow copying it to the target's lifetime.
519 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
520 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
521 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
522 * \retval #PSA_ERROR_HARDWARE_FAILURE
523 * \retval #PSA_ERROR_STORAGE_FAILURE
524 * \retval #PSA_ERROR_CORRUPTION_DETECTED
525 * \retval #PSA_ERROR_BAD_STATE
526 * The library has not been previously initialized by psa_crypto_init().
527 * It is implementation-dependent whether a failure to initialize
528 * results in this error code.
529 */
530psa_status_t psa_copy_key(psa_key_handle_t source_handle,
531 const psa_key_attributes_t *attributes,
532 psa_key_handle_t *target_handle);
533
534
535/**
536 * \brief Destroy a key.
537 *
538 * This function destroys a key from both volatile
539 * memory and, if applicable, non-volatile storage. Implementations shall
540 * make a best effort to ensure that that the key material cannot be recovered.
541 *
542 * This function also erases any metadata such as policies and frees
543 * resources associated with the key. To free all resources associated with
544 * the key, all handles to the key must be closed or destroyed.
545 *
546 * Destroying the key makes the handle invalid, and the key handle
547 * must not be used again by the application. Using other open handles to the
548 * destroyed key in a cryptographic operation will result in an error.
549 *
550 * If a key is currently in use in a multipart operation, then destroying the
551 * key will cause the multipart operation to fail.
552 *
553 * \param handle Handle to the key to erase.
554 *
555 * \retval #PSA_SUCCESS
556 * The key material has been erased.
557 * \retval #PSA_ERROR_NOT_PERMITTED
558 * The key cannot be erased because it is
559 * read-only, either due to a policy or due to physical restrictions.
560 * \retval #PSA_ERROR_INVALID_HANDLE
561 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
562 * There was an failure in communication with the cryptoprocessor.
563 * The key material may still be present in the cryptoprocessor.
564 * \retval #PSA_ERROR_STORAGE_FAILURE
565 * The storage is corrupted. Implementations shall make a best effort
566 * to erase key material even in this stage, however applications
567 * should be aware that it may be impossible to guarantee that the
568 * key material is not recoverable in such cases.
569 * \retval #PSA_ERROR_CORRUPTION_DETECTED
570 * An unexpected condition which is not a storage corruption or
571 * a communication failure occurred. The cryptoprocessor may have
572 * been compromised.
573 * \retval #PSA_ERROR_BAD_STATE
574 * The library has not been previously initialized by psa_crypto_init().
575 * It is implementation-dependent whether a failure to initialize
576 * results in this error code.
577 */
578psa_status_t psa_destroy_key(psa_key_handle_t handle);
579
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100580/**@}*/
581
582/** \defgroup import_export Key import and export
583 * @{
584 */
585
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100586/**
587 * \brief Import a key in binary format.
588 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100589 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100590 * documentation of psa_export_public_key() for the format of public keys
591 * and to the documentation of psa_export_key() for the format for
592 * other key types.
593 *
594 * This specification supports a single format for each key type.
595 * Implementations may support other formats as long as the standard
596 * format is supported. Implementations that support other formats
597 * should ensure that the formats are clearly unambiguous so as to
598 * minimize the risk that an invalid input is accidentally interpreted
599 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100600 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100601
Gilles Peskine20628592019-04-19 19:29:50 +0200602 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200603 * The key size is always determined from the
604 * \p data buffer.
605 * If the key size in \p attributes is nonzero,
606 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200607 * \param[out] handle On success, a handle to the newly created key.
608 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100609 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200610 * buffer is interpreted according to the type declared
611 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200612 * All implementations must support at least the format
613 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100614 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200615 * the chosen type. Implementations may allow other
616 * formats, but should be conservative: implementations
617 * should err on the side of rejecting content if it
618 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200619 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100620 *
Gilles Peskine28538492018-07-11 17:34:00 +0200621 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100622 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100623 * If the key is persistent, the key material and the key's metadata
624 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200625 * \retval #PSA_ERROR_ALREADY_EXISTS
626 * This is an attempt to create a persistent key, and there is
627 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200628 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200629 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200630 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200631 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200632 * The key attributes, as a whole, are invalid.
633 * \retval #PSA_ERROR_INVALID_ARGUMENT
634 * The key data is not correctly formatted.
635 * \retval #PSA_ERROR_INVALID_ARGUMENT
636 * The size in \p attributes is nonzero and does not match the size
637 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200638 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
639 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
640 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100641 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200642 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200643 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300644 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300645 * The library has not been previously initialized by psa_crypto_init().
646 * It is implementation-dependent whether a failure to initialize
647 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100648 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200649psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100650 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200651 size_t data_length,
652 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100653
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100654
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100655
656/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100657 * \brief Export a key in binary format.
658 *
659 * The output of this function can be passed to psa_import_key() to
660 * create an equivalent object.
661 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100662 * If the implementation of psa_import_key() supports other formats
663 * beyond the format specified here, the output from psa_export_key()
664 * must use the representation specified here, not the original
665 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100666 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100667 * For standard key types, the output format is as follows:
668 *
669 * - For symmetric keys (including MAC keys), the format is the
670 * raw bytes of the key.
671 * - For DES, the key data consists of 8 bytes. The parity bits must be
672 * correct.
673 * - For Triple-DES, the format is the concatenation of the
674 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200675 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200676 * is the non-encrypted DER encoding of the representation defined by
677 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
678 * ```
679 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200680 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200681 * modulus INTEGER, -- n
682 * publicExponent INTEGER, -- e
683 * privateExponent INTEGER, -- d
684 * prime1 INTEGER, -- p
685 * prime2 INTEGER, -- q
686 * exponent1 INTEGER, -- d mod (p-1)
687 * exponent2 INTEGER, -- d mod (q-1)
688 * coefficient INTEGER, -- (inverse of q) mod p
689 * }
690 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200691 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200692 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100693 * a representation of the private value as a `ceiling(m/8)`-byte string
694 * where `m` is the bit size associated with the curve, i.e. the bit size
695 * of the order of the curve's coordinate field. This byte string is
696 * in little-endian order for Montgomery curves (curve types
697 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
698 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
699 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100700 * This is the content of the `privateKey` field of the `ECPrivateKey`
701 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200702 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200703 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000704 * format is the representation of the private key `x` as a big-endian byte
705 * string. The length of the byte string is the private key size in bytes
706 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200707 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
708 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100709 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200710 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
711 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100712 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200713 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200714 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200715 * \param[out] data_length On success, the number of bytes
716 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100717 *
Gilles Peskine28538492018-07-11 17:34:00 +0200718 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100719 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200720 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200721 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100722 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200723 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
724 * The size of the \p data buffer is too small. You can determine a
725 * sufficient buffer size by calling
726 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
727 * where \c type is the key type
728 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200729 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
730 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200731 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100732 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100733 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300734 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300735 * The library has not been previously initialized by psa_crypto_init().
736 * It is implementation-dependent whether a failure to initialize
737 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100739psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 uint8_t *data,
741 size_t data_size,
742 size_t *data_length);
743
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100744/**
745 * \brief Export a public key or the public part of a key pair in binary format.
746 *
747 * The output of this function can be passed to psa_import_key() to
748 * create an object that is equivalent to the public key.
749 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000750 * This specification supports a single format for each key type.
751 * Implementations may support other formats as long as the standard
752 * format is supported. Implementations that support other formats
753 * should ensure that the formats are clearly unambiguous so as to
754 * minimize the risk that an invalid input is accidentally interpreted
755 * according to a different format.
756 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000757 * For standard key types, the output format is as follows:
758 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
759 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
760 * ```
761 * RSAPublicKey ::= SEQUENCE {
762 * modulus INTEGER, -- n
763 * publicExponent INTEGER } -- e
764 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000765 * - For elliptic curve public keys (key types for which
766 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
767 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
768 * Let `m` be the bit size associated with the curve, i.e. the bit size of
769 * `q` for a curve over `F_q`. The representation consists of:
770 * - The byte 0x04;
771 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
772 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200773 * - For Diffie-Hellman key exchange public keys (key types for which
774 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000775 * the format is the representation of the public key `y = g^x mod p` as a
776 * big-endian byte string. The length of the byte string is the length of the
777 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100778 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200779 * Exporting a public key object or the public part of a key pair is
780 * always permitted, regardless of the key's usage flags.
781 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100782 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200783 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200784 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200785 * \param[out] data_length On success, the number of bytes
786 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100787 *
Gilles Peskine28538492018-07-11 17:34:00 +0200788 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100789 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200790 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200791 * The key is neither a public key nor a key pair.
792 * \retval #PSA_ERROR_NOT_SUPPORTED
793 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
794 * The size of the \p data buffer is too small. You can determine a
795 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200796 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200797 * where \c type is the key type
798 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200799 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
800 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200801 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100802 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100803 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300804 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300805 * The library has not been previously initialized by psa_crypto_init().
806 * It is implementation-dependent whether a failure to initialize
807 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100808 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100809psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100810 uint8_t *data,
811 size_t data_size,
812 size_t *data_length);
813
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100814
Gilles Peskine20035e32018-02-03 22:44:14 +0100815
816/**@}*/
817
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100818/** \defgroup hash Message digests
819 * @{
820 */
821
Gilles Peskine69647a42019-01-14 20:18:12 +0100822/** Calculate the hash (digest) of a message.
823 *
824 * \note To verify the hash of a message against an
825 * expected value, use psa_hash_compare() instead.
826 *
827 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
828 * such that #PSA_ALG_IS_HASH(\p alg) is true).
829 * \param[in] input Buffer containing the message to hash.
830 * \param input_length Size of the \p input buffer in bytes.
831 * \param[out] hash Buffer where the hash is to be written.
832 * \param hash_size Size of the \p hash buffer in bytes.
833 * \param[out] hash_length On success, the number of bytes
834 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100835 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100836 *
837 * \retval #PSA_SUCCESS
838 * Success.
839 * \retval #PSA_ERROR_NOT_SUPPORTED
840 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100841 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100842 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
843 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100844 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
845 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
846 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200847 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100848 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100849 * \retval #PSA_ERROR_BAD_STATE
850 * The library has not been previously initialized by psa_crypto_init().
851 * It is implementation-dependent whether a failure to initialize
852 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100853 */
854psa_status_t psa_hash_compute(psa_algorithm_t alg,
855 const uint8_t *input,
856 size_t input_length,
857 uint8_t *hash,
858 size_t hash_size,
859 size_t *hash_length);
860
861/** Calculate the hash (digest) of a message and compare it with a
862 * reference value.
863 *
864 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
865 * such that #PSA_ALG_IS_HASH(\p alg) is true).
866 * \param[in] input Buffer containing the message to hash.
867 * \param input_length Size of the \p input buffer in bytes.
868 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100869 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100870 *
871 * \retval #PSA_SUCCESS
872 * The expected hash is identical to the actual hash of the input.
873 * \retval #PSA_ERROR_INVALID_SIGNATURE
874 * The hash of the message was calculated successfully, but it
875 * differs from the expected hash.
876 * \retval #PSA_ERROR_NOT_SUPPORTED
877 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100878 * \retval #PSA_ERROR_INVALID_ARGUMENT
879 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100880 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
881 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
882 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200883 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100884 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100885 * \retval #PSA_ERROR_BAD_STATE
886 * The library has not been previously initialized by psa_crypto_init().
887 * It is implementation-dependent whether a failure to initialize
888 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100889 */
890psa_status_t psa_hash_compare(psa_algorithm_t alg,
891 const uint8_t *input,
892 size_t input_length,
893 const uint8_t *hash,
894 const size_t hash_length);
895
Gilles Peskine308b91d2018-02-08 09:47:44 +0100896/** The type of the state data structure for multipart hash operations.
897 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000898 * Before calling any function on a hash operation object, the application must
899 * initialize it by any of the following means:
900 * - Set the structure to all-bits-zero, for example:
901 * \code
902 * psa_hash_operation_t operation;
903 * memset(&operation, 0, sizeof(operation));
904 * \endcode
905 * - Initialize the structure to logical zero values, for example:
906 * \code
907 * psa_hash_operation_t operation = {0};
908 * \endcode
909 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
910 * for example:
911 * \code
912 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
913 * \endcode
914 * - Assign the result of the function psa_hash_operation_init()
915 * to the structure, for example:
916 * \code
917 * psa_hash_operation_t operation;
918 * operation = psa_hash_operation_init();
919 * \endcode
920 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100921 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100922 * make any assumptions about the content of this structure except
923 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100924typedef struct psa_hash_operation_s psa_hash_operation_t;
925
Jaeden Amero6a25b412019-01-04 11:47:44 +0000926/** \def PSA_HASH_OPERATION_INIT
927 *
928 * This macro returns a suitable initializer for a hash operation object
929 * of type #psa_hash_operation_t.
930 */
931#ifdef __DOXYGEN_ONLY__
932/* This is an example definition for documentation purposes.
933 * Implementations should define a suitable value in `crypto_struct.h`.
934 */
935#define PSA_HASH_OPERATION_INIT {0}
936#endif
937
938/** Return an initial value for a hash operation object.
939 */
940static psa_hash_operation_t psa_hash_operation_init(void);
941
Gilles Peskinef45adda2019-01-14 18:29:18 +0100942/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100943 *
944 * The sequence of operations to calculate a hash (message digest)
945 * is as follows:
946 * -# Allocate an operation object which will be passed to all the functions
947 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000948 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100949 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200950 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100951 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100952 * of the message each time. The hash that is calculated is the hash
953 * of the concatenation of these messages in order.
954 * -# To calculate the hash, call psa_hash_finish().
955 * To compare the hash with an expected value, call psa_hash_verify().
956 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100957 * If an error occurs at any step after a call to psa_hash_setup(), the
958 * operation will need to be reset by a call to psa_hash_abort(). The
959 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000960 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100961 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200962 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100963 * eventually terminate the operation. The following events terminate an
964 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100965 * - A successful call to psa_hash_finish() or psa_hash_verify().
966 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100967 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000968 * \param[in,out] operation The operation object to set up. It must have
969 * been initialized as per the documentation for
970 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200971 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
972 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100973 *
Gilles Peskine28538492018-07-11 17:34:00 +0200974 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100975 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200976 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100977 * \p alg is not a supported hash algorithm.
978 * \retval #PSA_ERROR_INVALID_ARGUMENT
979 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100980 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100981 * The operation state is not valid (it must be inactive).
Gilles Peskine28538492018-07-11 17:34:00 +0200982 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
983 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
984 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200985 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100986 * \retval #PSA_ERROR_BAD_STATE
987 * The library has not been previously initialized by psa_crypto_init().
988 * It is implementation-dependent whether a failure to initialize
989 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100990 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200991psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100992 psa_algorithm_t alg);
993
Gilles Peskine308b91d2018-02-08 09:47:44 +0100994/** Add a message fragment to a multipart hash operation.
995 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200996 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100997 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100998 * If this function returns an error status, the operation enters an error
999 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001000 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001001 * \param[in,out] operation Active hash operation.
1002 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001003 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001004 *
Gilles Peskine28538492018-07-11 17:34:00 +02001005 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001006 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001007 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001008 * The operation state is not valid (it muct be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001009 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1010 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1011 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001012 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001013 * \retval #PSA_ERROR_BAD_STATE
1014 * The library has not been previously initialized by psa_crypto_init().
1015 * It is implementation-dependent whether a failure to initialize
1016 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001017 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001018psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1019 const uint8_t *input,
1020 size_t input_length);
1021
Gilles Peskine308b91d2018-02-08 09:47:44 +01001022/** Finish the calculation of the hash of a message.
1023 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001024 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001025 * This function calculates the hash of the message formed by concatenating
1026 * the inputs passed to preceding calls to psa_hash_update().
1027 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001028 * When this function returns successfuly, the operation becomes inactive.
1029 * If this function returns an error status, the operation enters an error
1030 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001031 *
1032 * \warning Applications should not call this function if they expect
1033 * a specific value for the hash. Call psa_hash_verify() instead.
1034 * Beware that comparing integrity or authenticity data such as
1035 * hash values with a function such as \c memcmp is risky
1036 * because the time taken by the comparison may leak information
1037 * about the hashed data which could allow an attacker to guess
1038 * a valid hash and thereby bypass security controls.
1039 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001040 * \param[in,out] operation Active hash operation.
1041 * \param[out] hash Buffer where the hash is to be written.
1042 * \param hash_size Size of the \p hash buffer in bytes.
1043 * \param[out] hash_length On success, the number of bytes
1044 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001045 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001046 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001047 *
Gilles Peskine28538492018-07-11 17:34:00 +02001048 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001049 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001050 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001051 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001052 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001053 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001054 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001055 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001056 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1057 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1058 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001059 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001060 * \retval #PSA_ERROR_BAD_STATE
1061 * The library has not been previously initialized by psa_crypto_init().
1062 * It is implementation-dependent whether a failure to initialize
1063 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001064 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001065psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1066 uint8_t *hash,
1067 size_t hash_size,
1068 size_t *hash_length);
1069
Gilles Peskine308b91d2018-02-08 09:47:44 +01001070/** Finish the calculation of the hash of a message and compare it with
1071 * an expected value.
1072 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001073 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074 * This function calculates the hash of the message formed by concatenating
1075 * the inputs passed to preceding calls to psa_hash_update(). It then
1076 * compares the calculated hash with the expected hash passed as a
1077 * parameter to this function.
1078 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001079 * When this function returns successfuly, the operation becomes inactive.
1080 * If this function returns an error status, the operation enters an error
1081 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001082 *
Gilles Peskine19067982018-03-20 17:54:53 +01001083 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001084 * comparison between the actual hash and the expected hash is performed
1085 * in constant time.
1086 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001087 * \param[in,out] operation Active hash operation.
1088 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001089 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001090 *
Gilles Peskine28538492018-07-11 17:34:00 +02001091 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001092 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001093 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001094 * The hash of the message was calculated successfully, but it
1095 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001096 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001097 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001098 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1099 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1100 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001101 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001102 * \retval #PSA_ERROR_BAD_STATE
1103 * The library has not been previously initialized by psa_crypto_init().
1104 * It is implementation-dependent whether a failure to initialize
1105 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001106 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001107psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1108 const uint8_t *hash,
1109 size_t hash_length);
1110
Gilles Peskine308b91d2018-02-08 09:47:44 +01001111/** Abort a hash operation.
1112 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001113 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001114 * \p operation structure itself. Once aborted, the operation object
1115 * can be reused for another operation by calling
1116 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001117 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001118 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001119 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001120 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001121 * In particular, calling psa_hash_abort() after the operation has been
1122 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1123 * psa_hash_verify() is safe and has no effect.
1124 *
1125 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001126 *
Gilles Peskine28538492018-07-11 17:34:00 +02001127 * \retval #PSA_SUCCESS
Gilles Peskine28538492018-07-11 17:34:00 +02001128 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1129 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001130 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001131 * \retval #PSA_ERROR_BAD_STATE
1132 * The library has not been previously initialized by psa_crypto_init().
1133 * It is implementation-dependent whether a failure to initialize
1134 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001135 */
1136psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001137
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001138/** Clone a hash operation.
1139 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001140 * This function copies the state of an ongoing hash operation to
1141 * a new operation object. In other words, this function is equivalent
1142 * to calling psa_hash_setup() on \p target_operation with the same
1143 * algorithm that \p source_operation was set up for, then
1144 * psa_hash_update() on \p target_operation with the same input that
1145 * that was passed to \p source_operation. After this function returns, the
1146 * two objects are independent, i.e. subsequent calls involving one of
1147 * the objects do not affect the other object.
1148 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001149 * \param[in] source_operation The active hash operation to clone.
1150 * \param[in,out] target_operation The operation object to set up.
1151 * It must be initialized but not active.
1152 *
1153 * \retval #PSA_SUCCESS
1154 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001155 * The \p source_operation state is not valid (it must be active).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001156 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001157 * The \p target_operation state is not valid (it must be inactive).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001158 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1159 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001160 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001161 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001162 * \retval #PSA_ERROR_BAD_STATE
1163 * The library has not been previously initialized by psa_crypto_init().
1164 * It is implementation-dependent whether a failure to initialize
1165 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001166 */
1167psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1168 psa_hash_operation_t *target_operation);
1169
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001170/**@}*/
1171
Gilles Peskine8c9def32018-02-08 10:02:12 +01001172/** \defgroup MAC Message authentication codes
1173 * @{
1174 */
1175
Gilles Peskine69647a42019-01-14 20:18:12 +01001176/** Calculate the MAC (message authentication code) of a message.
1177 *
1178 * \note To verify the MAC of a message against an
1179 * expected value, use psa_mac_verify() instead.
1180 * Beware that comparing integrity or authenticity data such as
1181 * MAC values with a function such as \c memcmp is risky
1182 * because the time taken by the comparison may leak information
1183 * about the MAC value which could allow an attacker to guess
1184 * a valid MAC and thereby bypass security controls.
1185 *
1186 * \param handle Handle to the key to use for the operation.
1187 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001188 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001189 * \param[in] input Buffer containing the input message.
1190 * \param input_length Size of the \p input buffer in bytes.
1191 * \param[out] mac Buffer where the MAC value is to be written.
1192 * \param mac_size Size of the \p mac buffer in bytes.
1193 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001194 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001195 *
1196 * \retval #PSA_SUCCESS
1197 * Success.
1198 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001199 * \retval #PSA_ERROR_NOT_PERMITTED
1200 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001201 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001202 * \retval #PSA_ERROR_NOT_SUPPORTED
1203 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001204 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1205 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001206 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1207 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1208 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001209 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001210 * \retval #PSA_ERROR_STORAGE_FAILURE
1211 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001212 * \retval #PSA_ERROR_BAD_STATE
1213 * The library has not been previously initialized by psa_crypto_init().
1214 * It is implementation-dependent whether a failure to initialize
1215 * results in this error code.
1216 */
1217psa_status_t psa_mac_compute(psa_key_handle_t handle,
1218 psa_algorithm_t alg,
1219 const uint8_t *input,
1220 size_t input_length,
1221 uint8_t *mac,
1222 size_t mac_size,
1223 size_t *mac_length);
1224
1225/** Calculate the MAC of a message and compare it with a reference value.
1226 *
1227 * \param handle Handle to the key to use for the operation.
1228 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001229 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001230 * \param[in] input Buffer containing the input message.
1231 * \param input_length Size of the \p input buffer in bytes.
1232 * \param[out] mac Buffer containing the expected MAC value.
1233 * \param mac_length Size of the \p mac buffer in bytes.
1234 *
1235 * \retval #PSA_SUCCESS
1236 * The expected MAC is identical to the actual MAC of the input.
1237 * \retval #PSA_ERROR_INVALID_SIGNATURE
1238 * The MAC of the message was calculated successfully, but it
1239 * differs from the expected value.
1240 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001241 * \retval #PSA_ERROR_NOT_PERMITTED
1242 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001243 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001244 * \retval #PSA_ERROR_NOT_SUPPORTED
1245 * \p alg is not supported or is not a MAC algorithm.
1246 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1247 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1248 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001249 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001250 * \retval #PSA_ERROR_STORAGE_FAILURE
1251 * The key could not be retrieved from storage.
1252 * \retval #PSA_ERROR_BAD_STATE
1253 * The library has not been previously initialized by psa_crypto_init().
1254 * It is implementation-dependent whether a failure to initialize
1255 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001256 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001257psa_status_t psa_mac_verify(psa_key_handle_t handle,
1258 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001259 const uint8_t *input,
1260 size_t input_length,
1261 const uint8_t *mac,
1262 const size_t mac_length);
1263
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001264/** The type of the state data structure for multipart MAC operations.
1265 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001266 * Before calling any function on a MAC operation object, the application must
1267 * initialize it by any of the following means:
1268 * - Set the structure to all-bits-zero, for example:
1269 * \code
1270 * psa_mac_operation_t operation;
1271 * memset(&operation, 0, sizeof(operation));
1272 * \endcode
1273 * - Initialize the structure to logical zero values, for example:
1274 * \code
1275 * psa_mac_operation_t operation = {0};
1276 * \endcode
1277 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1278 * for example:
1279 * \code
1280 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1281 * \endcode
1282 * - Assign the result of the function psa_mac_operation_init()
1283 * to the structure, for example:
1284 * \code
1285 * psa_mac_operation_t operation;
1286 * operation = psa_mac_operation_init();
1287 * \endcode
1288 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001289 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001290 * make any assumptions about the content of this structure except
1291 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001292typedef struct psa_mac_operation_s psa_mac_operation_t;
1293
Jaeden Amero769ce272019-01-04 11:48:03 +00001294/** \def PSA_MAC_OPERATION_INIT
1295 *
1296 * This macro returns a suitable initializer for a MAC operation object of type
1297 * #psa_mac_operation_t.
1298 */
1299#ifdef __DOXYGEN_ONLY__
1300/* This is an example definition for documentation purposes.
1301 * Implementations should define a suitable value in `crypto_struct.h`.
1302 */
1303#define PSA_MAC_OPERATION_INIT {0}
1304#endif
1305
1306/** Return an initial value for a MAC operation object.
1307 */
1308static psa_mac_operation_t psa_mac_operation_init(void);
1309
Gilles Peskinef45adda2019-01-14 18:29:18 +01001310/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001311 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001312 * This function sets up the calculation of the MAC
1313 * (message authentication code) of a byte string.
1314 * To verify the MAC of a message against an
1315 * expected value, use psa_mac_verify_setup() instead.
1316 *
1317 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001318 * -# Allocate an operation object which will be passed to all the functions
1319 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001320 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001321 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001322 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001323 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1324 * of the message each time. The MAC that is calculated is the MAC
1325 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001326 * -# At the end of the message, call psa_mac_sign_finish() to finish
1327 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001328 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001329 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1330 * operation will need to be reset by a call to psa_mac_abort(). The
1331 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001332 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001333 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001334 * After a successful call to psa_mac_sign_setup(), the application must
1335 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001336 * - A successful call to psa_mac_sign_finish().
1337 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001338 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001339 * \param[in,out] operation The operation object to set up. It must have
1340 * been initialized as per the documentation for
1341 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001342 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001343 * It must remain valid until the operation
1344 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001345 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001346 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001347 *
Gilles Peskine28538492018-07-11 17:34:00 +02001348 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001349 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001350 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001351 * \retval #PSA_ERROR_NOT_PERMITTED
1352 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001353 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001354 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001355 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001356 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1357 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1358 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001359 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001360 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001361 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001362 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001363 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001364 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001365 * The library has not been previously initialized by psa_crypto_init().
1366 * It is implementation-dependent whether a failure to initialize
1367 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001368 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001369psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001370 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001371 psa_algorithm_t alg);
1372
Gilles Peskinef45adda2019-01-14 18:29:18 +01001373/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001374 *
1375 * This function sets up the verification of the MAC
1376 * (message authentication code) of a byte string against an expected value.
1377 *
1378 * The sequence of operations to verify a MAC is as follows:
1379 * -# Allocate an operation object which will be passed to all the functions
1380 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001381 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001382 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001383 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001384 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1385 * of the message each time. The MAC that is calculated is the MAC
1386 * of the concatenation of these messages in order.
1387 * -# At the end of the message, call psa_mac_verify_finish() to finish
1388 * calculating the actual MAC of the message and verify it against
1389 * the expected value.
1390 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001391 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1392 * operation will need to be reset by a call to psa_mac_abort(). The
1393 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001394 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001395 *
1396 * After a successful call to psa_mac_verify_setup(), the application must
1397 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001398 * - A successful call to psa_mac_verify_finish().
1399 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001400 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001401 * \param[in,out] operation The operation object to set up. It must have
1402 * been initialized as per the documentation for
1403 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001404 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001405 * It must remain valid until the operation
1406 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001407 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1408 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001409 *
Gilles Peskine28538492018-07-11 17:34:00 +02001410 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001411 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001412 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001413 * \retval #PSA_ERROR_NOT_PERMITTED
1414 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001415 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001416 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001417 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001418 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1419 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1420 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001421 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001422 * \retval #PSA_ERROR_STORAGE_FAILURE
1423 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001424 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001425 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001426 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001427 * The library has not been previously initialized by psa_crypto_init().
1428 * It is implementation-dependent whether a failure to initialize
1429 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001430 */
1431psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001432 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001433 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001434
Gilles Peskinedcd14942018-07-12 00:30:52 +02001435/** Add a message fragment to a multipart MAC operation.
1436 *
1437 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1438 * before calling this function.
1439 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001440 * If this function returns an error status, the operation enters an error
1441 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001442 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001443 * \param[in,out] operation Active MAC operation.
1444 * \param[in] input Buffer containing the message fragment to add to
1445 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001446 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001447 *
1448 * \retval #PSA_SUCCESS
1449 * Success.
1450 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001451 * The operation state is not valid (it must be active).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001452 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1453 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1454 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001455 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001456 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001457 * \retval #PSA_ERROR_BAD_STATE
1458 * The library has not been previously initialized by psa_crypto_init().
1459 * It is implementation-dependent whether a failure to initialize
1460 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001461 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001462psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1463 const uint8_t *input,
1464 size_t input_length);
1465
Gilles Peskinedcd14942018-07-12 00:30:52 +02001466/** Finish the calculation of the MAC of a message.
1467 *
1468 * The application must call psa_mac_sign_setup() before calling this function.
1469 * This function calculates the MAC of the message formed by concatenating
1470 * the inputs passed to preceding calls to psa_mac_update().
1471 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001472 * When this function returns successfuly, the operation becomes inactive.
1473 * If this function returns an error status, the operation enters an error
1474 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001475 *
1476 * \warning Applications should not call this function if they expect
1477 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1478 * Beware that comparing integrity or authenticity data such as
1479 * MAC values with a function such as \c memcmp is risky
1480 * because the time taken by the comparison may leak information
1481 * about the MAC value which could allow an attacker to guess
1482 * a valid MAC and thereby bypass security controls.
1483 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001484 * \param[in,out] operation Active MAC operation.
1485 * \param[out] mac Buffer where the MAC value is to be written.
1486 * \param mac_size Size of the \p mac buffer in bytes.
1487 * \param[out] mac_length On success, the number of bytes
1488 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001489 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001490 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001491 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001492 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001493 *
1494 * \retval #PSA_SUCCESS
1495 * Success.
1496 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001497 * The operation state is not valid (it must be an active mac sign
1498 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001499 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001500 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001501 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1502 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1503 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1504 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001505 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001506 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001507 * \retval #PSA_ERROR_BAD_STATE
1508 * The library has not been previously initialized by psa_crypto_init().
1509 * It is implementation-dependent whether a failure to initialize
1510 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001511 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001512psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1513 uint8_t *mac,
1514 size_t mac_size,
1515 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001516
Gilles Peskinedcd14942018-07-12 00:30:52 +02001517/** Finish the calculation of the MAC of a message and compare it with
1518 * an expected value.
1519 *
1520 * The application must call psa_mac_verify_setup() before calling this function.
1521 * This function calculates the MAC of the message formed by concatenating
1522 * the inputs passed to preceding calls to psa_mac_update(). It then
1523 * compares the calculated MAC with the expected MAC passed as a
1524 * parameter to this function.
1525 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001526 * When this function returns successfuly, the operation becomes inactive.
1527 * If this function returns an error status, the operation enters an error
1528 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001529 *
1530 * \note Implementations shall make the best effort to ensure that the
1531 * comparison between the actual MAC and the expected MAC is performed
1532 * in constant time.
1533 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001534 * \param[in,out] operation Active MAC operation.
1535 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001536 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001537 *
1538 * \retval #PSA_SUCCESS
1539 * The expected MAC is identical to the actual MAC of the message.
1540 * \retval #PSA_ERROR_INVALID_SIGNATURE
1541 * The MAC of the message was calculated successfully, but it
1542 * differs from the expected MAC.
1543 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001544 * The operation state is not valid (it must be an active mac verify
1545 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001546 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1547 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1548 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001549 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001550 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001551 * \retval #PSA_ERROR_BAD_STATE
1552 * The library has not been previously initialized by psa_crypto_init().
1553 * It is implementation-dependent whether a failure to initialize
1554 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001555 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001556psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1557 const uint8_t *mac,
1558 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001559
Gilles Peskinedcd14942018-07-12 00:30:52 +02001560/** Abort a MAC operation.
1561 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001562 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001563 * \p operation structure itself. Once aborted, the operation object
1564 * can be reused for another operation by calling
1565 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001566 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001567 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001568 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001569 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001570 * In particular, calling psa_mac_abort() after the operation has been
1571 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1572 * psa_mac_verify_finish() is safe and has no effect.
1573 *
1574 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001575 *
1576 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02001577 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1578 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001579 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001580 * \retval #PSA_ERROR_BAD_STATE
1581 * The library has not been previously initialized by psa_crypto_init().
1582 * It is implementation-dependent whether a failure to initialize
1583 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001584 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001585psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1586
1587/**@}*/
1588
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001589/** \defgroup cipher Symmetric ciphers
1590 * @{
1591 */
1592
Gilles Peskine69647a42019-01-14 20:18:12 +01001593/** Encrypt a message using a symmetric cipher.
1594 *
1595 * This function encrypts a message with a random IV (initialization
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001596 * vector). Use the multipart #psa_cipher_operation_t object to provide
1597 * other foms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001598 *
1599 * \param handle Handle to the key to use for the operation.
1600 * It must remain valid until the operation
1601 * terminates.
1602 * \param alg The cipher algorithm to compute
1603 * (\c PSA_ALG_XXX value such that
1604 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1605 * \param[in] input Buffer containing the message to encrypt.
1606 * \param input_length Size of the \p input buffer in bytes.
1607 * \param[out] output Buffer where the output is to be written.
1608 * The output contains the IV followed by
1609 * the ciphertext proper.
1610 * \param output_size Size of the \p output buffer in bytes.
1611 * \param[out] output_length On success, the number of bytes
1612 * that make up the output.
1613 *
1614 * \retval #PSA_SUCCESS
1615 * Success.
1616 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001617 * \retval #PSA_ERROR_NOT_PERMITTED
1618 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001619 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001620 * \retval #PSA_ERROR_NOT_SUPPORTED
1621 * \p alg is not supported or is not a cipher algorithm.
1622 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1623 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1624 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1625 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001626 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001627 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001628 * \retval #PSA_ERROR_BAD_STATE
1629 * The library has not been previously initialized by psa_crypto_init().
1630 * It is implementation-dependent whether a failure to initialize
1631 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001632 */
1633psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1634 psa_algorithm_t alg,
1635 const uint8_t *input,
1636 size_t input_length,
1637 uint8_t *output,
1638 size_t output_size,
1639 size_t *output_length);
1640
1641/** Decrypt a message using a symmetric cipher.
1642 *
1643 * This function decrypts a message encrypted with a symmetric cipher.
1644 *
1645 * \param handle Handle to the key to use for the operation.
1646 * It must remain valid until the operation
1647 * terminates.
1648 * \param alg The cipher algorithm to compute
1649 * (\c PSA_ALG_XXX value such that
1650 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1651 * \param[in] input Buffer containing the message to decrypt.
1652 * This consists of the IV followed by the
1653 * ciphertext proper.
1654 * \param input_length Size of the \p input buffer in bytes.
1655 * \param[out] output Buffer where the plaintext is to be written.
1656 * \param output_size Size of the \p output buffer in bytes.
1657 * \param[out] output_length On success, the number of bytes
1658 * that make up the output.
1659 *
1660 * \retval #PSA_SUCCESS
1661 * Success.
1662 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001663 * \retval #PSA_ERROR_NOT_PERMITTED
1664 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001665 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001666 * \retval #PSA_ERROR_NOT_SUPPORTED
1667 * \p alg is not supported or is not a cipher algorithm.
1668 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1669 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1670 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1671 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001672 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001673 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001674 * \retval #PSA_ERROR_BAD_STATE
1675 * The library has not been previously initialized by psa_crypto_init().
1676 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001677 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001678 */
1679psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1680 psa_algorithm_t alg,
1681 const uint8_t *input,
1682 size_t input_length,
1683 uint8_t *output,
1684 size_t output_size,
1685 size_t *output_length);
1686
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001687/** The type of the state data structure for multipart cipher operations.
1688 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001689 * Before calling any function on a cipher operation object, the application
1690 * must initialize it by any of the following means:
1691 * - Set the structure to all-bits-zero, for example:
1692 * \code
1693 * psa_cipher_operation_t operation;
1694 * memset(&operation, 0, sizeof(operation));
1695 * \endcode
1696 * - Initialize the structure to logical zero values, for example:
1697 * \code
1698 * psa_cipher_operation_t operation = {0};
1699 * \endcode
1700 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1701 * for example:
1702 * \code
1703 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1704 * \endcode
1705 * - Assign the result of the function psa_cipher_operation_init()
1706 * to the structure, for example:
1707 * \code
1708 * psa_cipher_operation_t operation;
1709 * operation = psa_cipher_operation_init();
1710 * \endcode
1711 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001712 * This is an implementation-defined \c struct. Applications should not
1713 * make any assumptions about the content of this structure except
1714 * as directed by the documentation of a specific implementation. */
1715typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1716
Jaeden Amero5bae2272019-01-04 11:48:27 +00001717/** \def PSA_CIPHER_OPERATION_INIT
1718 *
1719 * This macro returns a suitable initializer for a cipher operation object of
1720 * type #psa_cipher_operation_t.
1721 */
1722#ifdef __DOXYGEN_ONLY__
1723/* This is an example definition for documentation purposes.
1724 * Implementations should define a suitable value in `crypto_struct.h`.
1725 */
1726#define PSA_CIPHER_OPERATION_INIT {0}
1727#endif
1728
1729/** Return an initial value for a cipher operation object.
1730 */
1731static psa_cipher_operation_t psa_cipher_operation_init(void);
1732
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001733/** Set the key for a multipart symmetric encryption operation.
1734 *
1735 * The sequence of operations to encrypt a message with a symmetric cipher
1736 * is as follows:
1737 * -# Allocate an operation object which will be passed to all the functions
1738 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001739 * -# Initialize the operation object with one of the methods described in the
1740 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001741 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001742 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001743 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001744 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001745 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001746 * requires a specific IV value.
1747 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1748 * of the message each time.
1749 * -# Call psa_cipher_finish().
1750 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001751 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1752 * the operation will need to be reset by a call to psa_cipher_abort(). The
1753 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001754 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001755 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001756 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001757 * eventually terminate the operation. The following events terminate an
1758 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001759 * - A successful call to psa_cipher_finish().
1760 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001761 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001762 * \param[in,out] operation The operation object to set up. It must have
1763 * been initialized as per the documentation for
1764 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001765 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001766 * It must remain valid until the operation
1767 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001768 * \param alg The cipher algorithm to compute
1769 * (\c PSA_ALG_XXX value such that
1770 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001771 *
Gilles Peskine28538492018-07-11 17:34:00 +02001772 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001773 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001774 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001775 * \retval #PSA_ERROR_NOT_PERMITTED
1776 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001777 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001778 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001779 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001780 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1781 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1782 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001783 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001784 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001785 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001786 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001787 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001788 * The library has not been previously initialized by psa_crypto_init().
1789 * It is implementation-dependent whether a failure to initialize
1790 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001791 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001792psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001793 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001794 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001795
1796/** Set the key for a multipart symmetric decryption operation.
1797 *
1798 * The sequence of operations to decrypt a message with a symmetric cipher
1799 * is as follows:
1800 * -# Allocate an operation object which will be passed to all the functions
1801 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001802 * -# Initialize the operation object with one of the methods described in the
1803 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001804 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001805 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001806 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001807 * decryption. If the IV is prepended to the ciphertext, you can call
1808 * psa_cipher_update() on a buffer containing the IV followed by the
1809 * beginning of the message.
1810 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1811 * of the message each time.
1812 * -# Call psa_cipher_finish().
1813 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001814 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1815 * the operation will need to be reset by a call to psa_cipher_abort(). The
1816 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001817 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001818 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001819 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001820 * eventually terminate the operation. The following events terminate an
1821 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001822 * - A successful call to psa_cipher_finish().
1823 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001824 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001825 * \param[in,out] operation The operation object to set up. It must have
1826 * been initialized as per the documentation for
1827 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001828 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001829 * It must remain valid until the operation
1830 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001831 * \param alg The cipher algorithm to compute
1832 * (\c PSA_ALG_XXX value such that
1833 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001834 *
Gilles Peskine28538492018-07-11 17:34:00 +02001835 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001836 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001837 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001838 * \retval #PSA_ERROR_NOT_PERMITTED
1839 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001840 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001841 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001842 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001843 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1844 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1845 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001846 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001847 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001848 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001849 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001850 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001851 * The library has not been previously initialized by psa_crypto_init().
1852 * It is implementation-dependent whether a failure to initialize
1853 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001854 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001855psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001856 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001857 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001858
Gilles Peskinedcd14942018-07-12 00:30:52 +02001859/** Generate an IV for a symmetric encryption operation.
1860 *
1861 * This function generates a random IV (initialization vector), nonce
1862 * or initial counter value for the encryption operation as appropriate
1863 * for the chosen algorithm, key type and key size.
1864 *
1865 * The application must call psa_cipher_encrypt_setup() before
1866 * calling this function.
1867 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001868 * If this function returns an error status, the operation enters an error
1869 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001870 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001871 * \param[in,out] operation Active cipher operation.
1872 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001873 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001874 * \param[out] iv_length On success, the number of bytes of the
1875 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001876 *
1877 * \retval #PSA_SUCCESS
1878 * Success.
1879 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001880 * The operation state is not valid (it must be active, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001881 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001882 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001883 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1884 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1885 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001886 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001887 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001888 * \retval #PSA_ERROR_BAD_STATE
1889 * The library has not been previously initialized by psa_crypto_init().
1890 * It is implementation-dependent whether a failure to initialize
1891 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001892 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001893psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001894 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001895 size_t iv_size,
1896 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001897
Gilles Peskinedcd14942018-07-12 00:30:52 +02001898/** Set the IV for a symmetric encryption or decryption operation.
1899 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001900 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001901 * or initial counter value for the encryption or decryption operation.
1902 *
1903 * The application must call psa_cipher_encrypt_setup() before
1904 * calling this function.
1905 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001906 * If this function returns an error status, the operation enters an error
1907 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001908 *
1909 * \note When encrypting, applications should use psa_cipher_generate_iv()
1910 * instead of this function, unless implementing a protocol that requires
1911 * a non-random IV.
1912 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001913 * \param[in,out] operation Active cipher operation.
1914 * \param[in] iv Buffer containing the IV to use.
1915 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001916 *
1917 * \retval #PSA_SUCCESS
1918 * Success.
1919 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001920 * The operation state is not valid (it must be an active cipher
1921 * encrypt operation, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001922 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001923 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001924 * or the chosen algorithm does not use an IV.
1925 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1926 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1927 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001928 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001929 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001930 * \retval #PSA_ERROR_BAD_STATE
1931 * The library has not been previously initialized by psa_crypto_init().
1932 * It is implementation-dependent whether a failure to initialize
1933 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001934 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001935psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001936 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001937 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001938
Gilles Peskinedcd14942018-07-12 00:30:52 +02001939/** Encrypt or decrypt a message fragment in an active cipher operation.
1940 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001941 * Before calling this function, you must:
1942 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1943 * The choice of setup function determines whether this function
1944 * encrypts or decrypts its input.
1945 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1946 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001947 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001948 * If this function returns an error status, the operation enters an error
1949 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001950 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001951 * \param[in,out] operation Active cipher operation.
1952 * \param[in] input Buffer containing the message fragment to
1953 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001954 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001955 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001956 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001957 * \param[out] output_length On success, the number of bytes
1958 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001959 *
1960 * \retval #PSA_SUCCESS
1961 * Success.
1962 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001963 * The operation state is not valid (it must be active, with an IV set
1964 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001965 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1966 * The size of the \p output buffer is too small.
1967 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1968 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1969 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001970 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001971 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001972 * \retval #PSA_ERROR_BAD_STATE
1973 * The library has not been previously initialized by psa_crypto_init().
1974 * It is implementation-dependent whether a failure to initialize
1975 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001976 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001977psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1978 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001979 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001980 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001981 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001982 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001983
Gilles Peskinedcd14942018-07-12 00:30:52 +02001984/** Finish encrypting or decrypting a message in a cipher operation.
1985 *
1986 * The application must call psa_cipher_encrypt_setup() or
1987 * psa_cipher_decrypt_setup() before calling this function. The choice
1988 * of setup function determines whether this function encrypts or
1989 * decrypts its input.
1990 *
1991 * This function finishes the encryption or decryption of the message
1992 * formed by concatenating the inputs passed to preceding calls to
1993 * psa_cipher_update().
1994 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001995 * When this function returns successfuly, the operation becomes inactive.
1996 * If this function returns an error status, the operation enters an error
1997 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001998 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001999 * \param[in,out] operation Active cipher operation.
2000 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002001 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002002 * \param[out] output_length On success, the number of bytes
2003 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002004 *
2005 * \retval #PSA_SUCCESS
2006 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02002007 * \retval #PSA_ERROR_INVALID_ARGUMENT
2008 * The total input size passed to this operation is not valid for
2009 * this particular algorithm. For example, the algorithm is a based
2010 * on block cipher and requires a whole number of blocks, but the
2011 * total input size is not a multiple of the block size.
2012 * \retval #PSA_ERROR_INVALID_PADDING
2013 * This is a decryption operation for an algorithm that includes
2014 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002015 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002016 * The operation state is not valid (it must be active, with an IV set
2017 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02002018 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2019 * The size of the \p output buffer is too small.
2020 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2021 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2022 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002023 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002024 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002025 * \retval #PSA_ERROR_BAD_STATE
2026 * The library has not been previously initialized by psa_crypto_init().
2027 * It is implementation-dependent whether a failure to initialize
2028 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002029 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002030psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002031 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002032 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002033 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002034
Gilles Peskinedcd14942018-07-12 00:30:52 +02002035/** Abort a cipher operation.
2036 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002037 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002038 * \p operation structure itself. Once aborted, the operation object
2039 * can be reused for another operation by calling
2040 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002041 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002042 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002043 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002044 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002045 * In particular, calling psa_cipher_abort() after the operation has been
2046 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2047 * is safe and has no effect.
2048 *
2049 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002050 *
2051 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02002052 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2053 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002054 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002055 * \retval #PSA_ERROR_BAD_STATE
2056 * The library has not been previously initialized by psa_crypto_init().
2057 * It is implementation-dependent whether a failure to initialize
2058 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002059 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002060psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2061
2062/**@}*/
2063
Gilles Peskine3b555712018-03-03 21:27:57 +01002064/** \defgroup aead Authenticated encryption with associated data (AEAD)
2065 * @{
2066 */
2067
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002068/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002069 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002070 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002071 * \param alg The AEAD algorithm to compute
2072 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002073 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002074 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002075 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002076 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002077 * but not encrypted.
2078 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002079 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002080 * encrypted.
2081 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002082 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002083 * encrypted data. The additional data is not
2084 * part of this output. For algorithms where the
2085 * encrypted data and the authentication tag
2086 * are defined as separate outputs, the
2087 * authentication tag is appended to the
2088 * encrypted data.
2089 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2090 * This must be at least
2091 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2092 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002093 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002094 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002095 *
Gilles Peskine28538492018-07-11 17:34:00 +02002096 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002097 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002098 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002099 * \retval #PSA_ERROR_NOT_PERMITTED
2100 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002101 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002102 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002103 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002104 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002105 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2106 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002107 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2108 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002109 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002110 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002111 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002112 * The library has not been previously initialized by psa_crypto_init().
2113 * It is implementation-dependent whether a failure to initialize
2114 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002115 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002116psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002117 psa_algorithm_t alg,
2118 const uint8_t *nonce,
2119 size_t nonce_length,
2120 const uint8_t *additional_data,
2121 size_t additional_data_length,
2122 const uint8_t *plaintext,
2123 size_t plaintext_length,
2124 uint8_t *ciphertext,
2125 size_t ciphertext_size,
2126 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002127
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002128/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002129 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002130 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002131 * \param alg The AEAD algorithm to compute
2132 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002133 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002134 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002135 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002136 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002137 * but not encrypted.
2138 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002139 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002140 * encrypted. For algorithms where the
2141 * encrypted data and the authentication tag
2142 * are defined as separate inputs, the buffer
2143 * must contain the encrypted data followed
2144 * by the authentication tag.
2145 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002146 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002147 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2148 * This must be at least
2149 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2150 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002151 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002152 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002153 *
Gilles Peskine28538492018-07-11 17:34:00 +02002154 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002155 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002156 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002157 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002158 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002159 * \retval #PSA_ERROR_NOT_PERMITTED
2160 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002161 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002162 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002163 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002164 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002165 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2166 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002167 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2168 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002169 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002170 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002171 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002172 * The library has not been previously initialized by psa_crypto_init().
2173 * It is implementation-dependent whether a failure to initialize
2174 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002175 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002176psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002177 psa_algorithm_t alg,
2178 const uint8_t *nonce,
2179 size_t nonce_length,
2180 const uint8_t *additional_data,
2181 size_t additional_data_length,
2182 const uint8_t *ciphertext,
2183 size_t ciphertext_length,
2184 uint8_t *plaintext,
2185 size_t plaintext_size,
2186 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002187
Gilles Peskine30a9e412019-01-14 18:36:12 +01002188/** The type of the state data structure for multipart AEAD operations.
2189 *
2190 * Before calling any function on an AEAD operation object, the application
2191 * must initialize it by any of the following means:
2192 * - Set the structure to all-bits-zero, for example:
2193 * \code
2194 * psa_aead_operation_t operation;
2195 * memset(&operation, 0, sizeof(operation));
2196 * \endcode
2197 * - Initialize the structure to logical zero values, for example:
2198 * \code
2199 * psa_aead_operation_t operation = {0};
2200 * \endcode
2201 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2202 * for example:
2203 * \code
2204 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2205 * \endcode
2206 * - Assign the result of the function psa_aead_operation_init()
2207 * to the structure, for example:
2208 * \code
2209 * psa_aead_operation_t operation;
2210 * operation = psa_aead_operation_init();
2211 * \endcode
2212 *
2213 * This is an implementation-defined \c struct. Applications should not
2214 * make any assumptions about the content of this structure except
2215 * as directed by the documentation of a specific implementation. */
2216typedef struct psa_aead_operation_s psa_aead_operation_t;
2217
2218/** \def PSA_AEAD_OPERATION_INIT
2219 *
2220 * This macro returns a suitable initializer for an AEAD operation object of
2221 * type #psa_aead_operation_t.
2222 */
2223#ifdef __DOXYGEN_ONLY__
2224/* This is an example definition for documentation purposes.
2225 * Implementations should define a suitable value in `crypto_struct.h`.
2226 */
2227#define PSA_AEAD_OPERATION_INIT {0}
2228#endif
2229
2230/** Return an initial value for an AEAD operation object.
2231 */
2232static psa_aead_operation_t psa_aead_operation_init(void);
2233
2234/** Set the key for a multipart authenticated encryption operation.
2235 *
2236 * The sequence of operations to encrypt a message with authentication
2237 * is as follows:
2238 * -# Allocate an operation object which will be passed to all the functions
2239 * listed here.
2240 * -# Initialize the operation object with one of the methods described in the
2241 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002242 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002243 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002244 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2245 * inputs to the subsequent calls to psa_aead_update_ad() and
2246 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2247 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002248 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2249 * generate or set the nonce. You should use
2250 * psa_aead_generate_nonce() unless the protocol you are implementing
2251 * requires a specific nonce value.
2252 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2253 * of the non-encrypted additional authenticated data each time.
2254 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002255 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002256 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002257 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002258 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2259 * the operation will need to be reset by a call to psa_aead_abort(). The
2260 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002261 * has been initialized.
2262 *
2263 * After a successful call to psa_aead_encrypt_setup(), the application must
2264 * eventually terminate the operation. The following events terminate an
2265 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002266 * - A successful call to psa_aead_finish().
2267 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002268 *
2269 * \param[in,out] operation The operation object to set up. It must have
2270 * been initialized as per the documentation for
2271 * #psa_aead_operation_t and not yet in use.
2272 * \param handle Handle to the key to use for the operation.
2273 * It must remain valid until the operation
2274 * terminates.
2275 * \param alg The AEAD algorithm to compute
2276 * (\c PSA_ALG_XXX value such that
2277 * #PSA_ALG_IS_AEAD(\p alg) is true).
2278 *
2279 * \retval #PSA_SUCCESS
2280 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002281 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002282 * The operation state is not valid (it must be inactive).
Andrew Thoelke340984b2019-09-11 21:33:41 +01002283 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002284 * \retval #PSA_ERROR_NOT_PERMITTED
2285 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002286 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002287 * \retval #PSA_ERROR_NOT_SUPPORTED
2288 * \p alg is not supported or is not an AEAD algorithm.
2289 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2290 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2291 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002292 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002293 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002294 * \retval #PSA_ERROR_BAD_STATE
2295 * The library has not been previously initialized by psa_crypto_init().
2296 * It is implementation-dependent whether a failure to initialize
2297 * results in this error code.
2298 */
2299psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2300 psa_key_handle_t handle,
2301 psa_algorithm_t alg);
2302
2303/** Set the key for a multipart authenticated decryption operation.
2304 *
2305 * The sequence of operations to decrypt a message with authentication
2306 * is as follows:
2307 * -# Allocate an operation object which will be passed to all the functions
2308 * listed here.
2309 * -# Initialize the operation object with one of the methods described in the
2310 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002311 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002312 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002313 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2314 * inputs to the subsequent calls to psa_aead_update_ad() and
2315 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2316 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002317 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2318 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2319 * of the non-encrypted additional authenticated data each time.
2320 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002321 * of the ciphertext to decrypt each time.
2322 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002323 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002324 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2325 * the operation will need to be reset by a call to psa_aead_abort(). The
2326 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002327 * has been initialized.
2328 *
2329 * After a successful call to psa_aead_decrypt_setup(), the application must
2330 * eventually terminate the operation. The following events terminate an
2331 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002332 * - A successful call to psa_aead_verify().
2333 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002334 *
2335 * \param[in,out] operation The operation object to set up. It must have
2336 * been initialized as per the documentation for
2337 * #psa_aead_operation_t and not yet in use.
2338 * \param handle Handle to the key to use for the operation.
2339 * It must remain valid until the operation
2340 * terminates.
2341 * \param alg The AEAD algorithm to compute
2342 * (\c PSA_ALG_XXX value such that
2343 * #PSA_ALG_IS_AEAD(\p alg) is true).
2344 *
2345 * \retval #PSA_SUCCESS
2346 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002347 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002348 * The operation state is not valid (it must be inactive).
Andrew Thoelke340984b2019-09-11 21:33:41 +01002349 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002350 * \retval #PSA_ERROR_NOT_PERMITTED
2351 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002352 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002353 * \retval #PSA_ERROR_NOT_SUPPORTED
2354 * \p alg is not supported or is not an AEAD algorithm.
2355 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2356 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2357 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002358 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002359 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002360 * \retval #PSA_ERROR_BAD_STATE
2361 * The library has not been previously initialized by psa_crypto_init().
2362 * It is implementation-dependent whether a failure to initialize
2363 * results in this error code.
2364 */
2365psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2366 psa_key_handle_t handle,
2367 psa_algorithm_t alg);
2368
2369/** Generate a random nonce for an authenticated encryption operation.
2370 *
2371 * This function generates a random nonce for the authenticated encryption
2372 * operation with an appropriate size for the chosen algorithm, key type
2373 * and key size.
2374 *
2375 * The application must call psa_aead_encrypt_setup() before
2376 * calling this function.
2377 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002378 * If this function returns an error status, the operation enters an error
2379 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002380 *
2381 * \param[in,out] operation Active AEAD operation.
2382 * \param[out] nonce Buffer where the generated nonce is to be
2383 * written.
2384 * \param nonce_size Size of the \p nonce buffer in bytes.
2385 * \param[out] nonce_length On success, the number of bytes of the
2386 * generated nonce.
2387 *
2388 * \retval #PSA_SUCCESS
2389 * Success.
2390 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002391 * The operation state is not valid (it must be an active aead encrypt
2392 operation, with no nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002393 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2394 * The size of the \p nonce buffer is too small.
2395 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2396 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2397 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002398 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002399 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002400 * \retval #PSA_ERROR_BAD_STATE
2401 * The library has not been previously initialized by psa_crypto_init().
2402 * It is implementation-dependent whether a failure to initialize
2403 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002404 */
2405psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002406 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002407 size_t nonce_size,
2408 size_t *nonce_length);
2409
2410/** Set the nonce for an authenticated encryption or decryption operation.
2411 *
2412 * This function sets the nonce for the authenticated
2413 * encryption or decryption operation.
2414 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002415 * The application must call psa_aead_encrypt_setup() or
2416 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002417 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002418 * If this function returns an error status, the operation enters an error
2419 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002420 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002421 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002422 * instead of this function, unless implementing a protocol that requires
2423 * a non-random IV.
2424 *
2425 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002426 * \param[in] nonce Buffer containing the nonce to use.
2427 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002428 *
2429 * \retval #PSA_SUCCESS
2430 * Success.
2431 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002432 * The operation state is not valid (it must be active, with no nonce
2433 * set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002434 * \retval #PSA_ERROR_INVALID_ARGUMENT
2435 * The size of \p nonce is not acceptable for the chosen algorithm.
2436 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2437 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2438 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002439 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002440 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002441 * \retval #PSA_ERROR_BAD_STATE
2442 * The library has not been previously initialized by psa_crypto_init().
2443 * It is implementation-dependent whether a failure to initialize
2444 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002445 */
2446psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002447 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002448 size_t nonce_length);
2449
Gilles Peskinebc59c852019-01-17 15:26:08 +01002450/** Declare the lengths of the message and additional data for AEAD.
2451 *
2452 * The application must call this function before calling
2453 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2454 * the operation requires it. If the algorithm does not require it,
2455 * calling this function is optional, but if this function is called
2456 * then the implementation must enforce the lengths.
2457 *
2458 * You may call this function before or after setting the nonce with
2459 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2460 *
2461 * - For #PSA_ALG_CCM, calling this function is required.
2462 * - For the other AEAD algorithms defined in this specification, calling
2463 * this function is not required.
2464 * - For vendor-defined algorithm, refer to the vendor documentation.
2465 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002466 * If this function returns an error status, the operation enters an error
2467 * state and must be aborted by calling psa_aead_abort().
2468 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002469 * \param[in,out] operation Active AEAD operation.
2470 * \param ad_length Size of the non-encrypted additional
2471 * authenticated data in bytes.
2472 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2473 *
2474 * \retval #PSA_SUCCESS
2475 * Success.
2476 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002477 * The operation state is not valid (it must be active, but before
2478 * psa_aead_update_ad() or psa_aead_update() are called).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002479 * \retval #PSA_ERROR_INVALID_ARGUMENT
2480 * At least one of the lengths is not acceptable for the chosen
2481 * algorithm.
2482 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2483 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2484 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002485 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002486 * \retval #PSA_ERROR_BAD_STATE
2487 * The library has not been previously initialized by psa_crypto_init().
2488 * It is implementation-dependent whether a failure to initialize
2489 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002490 */
2491psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2492 size_t ad_length,
2493 size_t plaintext_length);
2494
Gilles Peskine30a9e412019-01-14 18:36:12 +01002495/** Pass additional data to an active AEAD operation.
2496 *
2497 * Additional data is authenticated, but not encrypted.
2498 *
2499 * You may call this function multiple times to pass successive fragments
2500 * of the additional data. You may not call this function after passing
2501 * data to encrypt or decrypt with psa_aead_update().
2502 *
2503 * Before calling this function, you must:
2504 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2505 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2506 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002507 * If this function returns an error status, the operation enters an error
2508 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002509 *
2510 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2511 * there is no guarantee that the input is valid. Therefore, until
2512 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2513 * treat the input as untrusted and prepare to undo any action that
2514 * depends on the input if psa_aead_verify() returns an error status.
2515 *
2516 * \param[in,out] operation Active AEAD operation.
2517 * \param[in] input Buffer containing the fragment of
2518 * additional data.
2519 * \param input_length Size of the \p input buffer in bytes.
2520 *
2521 * \retval #PSA_SUCCESS
2522 * Success.
2523 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002524 * The operation state is not valid (it must be active and ready to
2525 * receive additional data).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002526 * \retval #PSA_ERROR_INVALID_ARGUMENT
2527 * The total input length overflows the additional data length that
2528 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002529 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2530 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2531 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002532 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002533 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002534 * \retval #PSA_ERROR_BAD_STATE
2535 * The library has not been previously initialized by psa_crypto_init().
2536 * It is implementation-dependent whether a failure to initialize
2537 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002538 */
2539psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2540 const uint8_t *input,
2541 size_t input_length);
2542
2543/** Encrypt or decrypt a message fragment in an active AEAD operation.
2544 *
2545 * Before calling this function, you must:
2546 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2547 * The choice of setup function determines whether this function
2548 * encrypts or decrypts its input.
2549 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2550 * 3. Call psa_aead_update_ad() to pass all the additional data.
2551 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002552 * If this function returns an error status, the operation enters an error
2553 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002554 *
2555 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2556 * there is no guarantee that the input is valid. Therefore, until
2557 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2558 * - Do not use the output in any way other than storing it in a
2559 * confidential location. If you take any action that depends
2560 * on the tentative decrypted data, this action will need to be
2561 * undone if the input turns out not to be valid. Furthermore,
2562 * if an adversary can observe that this action took place
2563 * (for example through timing), they may be able to use this
2564 * fact as an oracle to decrypt any message encrypted with the
2565 * same key.
2566 * - In particular, do not copy the output anywhere but to a
2567 * memory or storage space that you have exclusive access to.
2568 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002569 * This function does not require the input to be aligned to any
2570 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002571 * a whole block at a time, it must consume all the input provided, but
2572 * it may delay the end of the corresponding output until a subsequent
2573 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2574 * provides sufficient input. The amount of data that can be delayed
2575 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002576 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002577 * \param[in,out] operation Active AEAD operation.
2578 * \param[in] input Buffer containing the message fragment to
2579 * encrypt or decrypt.
2580 * \param input_length Size of the \p input buffer in bytes.
2581 * \param[out] output Buffer where the output is to be written.
2582 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002583 * This must be at least
2584 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2585 * \p input_length) where \c alg is the
2586 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002587 * \param[out] output_length On success, the number of bytes
2588 * that make up the returned output.
2589 *
2590 * \retval #PSA_SUCCESS
2591 * Success.
2592 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002593 * The operation state is not valid (it must be active and ready to
2594 * recieve message data).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002595 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2596 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002597 * You can determine a sufficient buffer size by calling
2598 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2599 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002600 * \retval #PSA_ERROR_INVALID_ARGUMENT
2601 * The total length of input to psa_aead_update_ad() so far is
2602 * less than the additional data length that was previously
2603 * specified with psa_aead_set_lengths().
2604 * \retval #PSA_ERROR_INVALID_ARGUMENT
2605 * The total input length overflows the plaintext length that
2606 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002607 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2608 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2609 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002610 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002611 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002612 * \retval #PSA_ERROR_BAD_STATE
2613 * The library has not been previously initialized by psa_crypto_init().
2614 * It is implementation-dependent whether a failure to initialize
2615 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002616 */
2617psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2618 const uint8_t *input,
2619 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002620 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002621 size_t output_size,
2622 size_t *output_length);
2623
2624/** Finish encrypting a message in an AEAD operation.
2625 *
2626 * The operation must have been set up with psa_aead_encrypt_setup().
2627 *
2628 * This function finishes the authentication of the additional data
2629 * formed by concatenating the inputs passed to preceding calls to
2630 * psa_aead_update_ad() with the plaintext formed by concatenating the
2631 * inputs passed to preceding calls to psa_aead_update().
2632 *
2633 * This function has two output buffers:
2634 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002635 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002636 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002637 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002638 * that the operation performs.
2639 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002640 * When this function returns successfuly, the operation becomes inactive.
2641 * If this function returns an error status, the operation enters an error
2642 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002643 *
2644 * \param[in,out] operation Active AEAD operation.
2645 * \param[out] ciphertext Buffer where the last part of the ciphertext
2646 * is to be written.
2647 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002648 * This must be at least
2649 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2650 * \c alg is the algorithm that is being
2651 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002652 * \param[out] ciphertext_length On success, the number of bytes of
2653 * returned ciphertext.
2654 * \param[out] tag Buffer where the authentication tag is
2655 * to be written.
2656 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002657 * This must be at least
2658 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2659 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002660 * \param[out] tag_length On success, the number of bytes
2661 * that make up the returned tag.
2662 *
2663 * \retval #PSA_SUCCESS
2664 * Success.
2665 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002666 * The operation state is not valid (it must be an active aead encrypt
2667 * operation, with all data provided).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002668 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002669 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002670 * You can determine a sufficient buffer size for \p ciphertext by
2671 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2672 * where \c alg is the algorithm that is being calculated.
2673 * You can determine a sufficient buffer size for \p tag by
2674 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002675 * \retval #PSA_ERROR_INVALID_ARGUMENT
2676 * The total length of input to psa_aead_update_ad() so far is
2677 * less than the additional data length that was previously
2678 * specified with psa_aead_set_lengths().
2679 * \retval #PSA_ERROR_INVALID_ARGUMENT
2680 * The total length of input to psa_aead_update() so far is
2681 * less than the plaintext length that was previously
2682 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002683 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2684 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2685 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002686 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002687 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002688 * \retval #PSA_ERROR_BAD_STATE
2689 * The library has not been previously initialized by psa_crypto_init().
2690 * It is implementation-dependent whether a failure to initialize
2691 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002692 */
2693psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002694 uint8_t *ciphertext,
2695 size_t ciphertext_size,
2696 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002697 uint8_t *tag,
2698 size_t tag_size,
2699 size_t *tag_length);
2700
2701/** Finish authenticating and decrypting a message in an AEAD operation.
2702 *
2703 * The operation must have been set up with psa_aead_decrypt_setup().
2704 *
2705 * This function finishes the authentication of the additional data
2706 * formed by concatenating the inputs passed to preceding calls to
2707 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2708 * inputs passed to preceding calls to psa_aead_update().
2709 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002710 * When this function returns successfuly, the operation becomes inactive.
2711 * If this function returns an error status, the operation enters an error
2712 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002713 *
2714 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002715 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002716 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002717 * from previous calls to psa_aead_update()
2718 * that could not be processed until the end
2719 * of the input.
2720 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002721 * This must be at least
2722 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2723 * \c alg is the algorithm that is being
2724 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002725 * \param[out] plaintext_length On success, the number of bytes of
2726 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002727 * \param[in] tag Buffer containing the authentication tag.
2728 * \param tag_length Size of the \p tag buffer in bytes.
2729 *
2730 * \retval #PSA_SUCCESS
2731 * Success.
2732 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002733 * The operation state is not valid (it must be an active aead decrypt
2734 * operation, with all data provided).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002735 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2736 * The size of the \p plaintext buffer is too small.
2737 * You can determine a sufficient buffer size for \p plaintext by
2738 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2739 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002740 * \retval #PSA_ERROR_INVALID_ARGUMENT
2741 * The total length of input to psa_aead_update_ad() so far is
2742 * less than the additional data length that was previously
2743 * specified with psa_aead_set_lengths().
2744 * \retval #PSA_ERROR_INVALID_ARGUMENT
2745 * The total length of input to psa_aead_update() so far is
2746 * less than the plaintext length that was previously
2747 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002748 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2749 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2750 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002751 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002752 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002753 * \retval #PSA_ERROR_BAD_STATE
2754 * The library has not been previously initialized by psa_crypto_init().
2755 * It is implementation-dependent whether a failure to initialize
2756 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002757 */
2758psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002759 uint8_t *plaintext,
2760 size_t plaintext_size,
2761 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002762 const uint8_t *tag,
2763 size_t tag_length);
2764
2765/** Abort an AEAD operation.
2766 *
2767 * Aborting an operation frees all associated resources except for the
2768 * \p operation structure itself. Once aborted, the operation object
2769 * can be reused for another operation by calling
2770 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2771 *
2772 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002773 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002774 *
2775 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002776 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2777 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002778 *
2779 * \param[in,out] operation Initialized AEAD operation.
2780 *
2781 * \retval #PSA_SUCCESS
Gilles Peskine30a9e412019-01-14 18:36:12 +01002782 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2783 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002784 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002785 * \retval #PSA_ERROR_BAD_STATE
2786 * The library has not been previously initialized by psa_crypto_init().
2787 * It is implementation-dependent whether a failure to initialize
2788 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002789 */
2790psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2791
Gilles Peskine3b555712018-03-03 21:27:57 +01002792/**@}*/
2793
Gilles Peskine20035e32018-02-03 22:44:14 +01002794/** \defgroup asymmetric Asymmetric cryptography
2795 * @{
2796 */
2797
2798/**
2799 * \brief Sign a hash or short message with a private key.
2800 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002801 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002802 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002803 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2804 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2805 * to determine the hash algorithm to use.
2806 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002807 * \param handle Handle to the key to use for the operation.
2808 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002809 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002810 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002811 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002812 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002813 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002814 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002815 * \param[out] signature_length On success, the number of bytes
2816 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002817 *
Gilles Peskine28538492018-07-11 17:34:00 +02002818 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002819 * \retval #PSA_ERROR_INVALID_HANDLE
2820 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002821 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002822 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002823 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002824 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002825 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002826 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002827 * \retval #PSA_ERROR_NOT_SUPPORTED
2828 * \retval #PSA_ERROR_INVALID_ARGUMENT
2829 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2830 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2831 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002832 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002833 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002834 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002835 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002836 * The library has not been previously initialized by psa_crypto_init().
2837 * It is implementation-dependent whether a failure to initialize
2838 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002839 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002840psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002841 psa_algorithm_t alg,
2842 const uint8_t *hash,
2843 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002844 uint8_t *signature,
2845 size_t signature_size,
2846 size_t *signature_length);
2847
2848/**
2849 * \brief Verify the signature a hash or short message using a public key.
2850 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002851 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002852 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002853 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2854 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2855 * to determine the hash algorithm to use.
2856 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002857 * \param handle Handle to the key to use for the operation.
2858 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002859 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002860 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002861 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002862 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002863 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002864 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002865 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002866 *
Gilles Peskine28538492018-07-11 17:34:00 +02002867 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002868 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002869 * \retval #PSA_ERROR_INVALID_HANDLE
2870 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002871 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002872 * The calculation was perfomed successfully, but the passed
2873 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002874 * \retval #PSA_ERROR_NOT_SUPPORTED
2875 * \retval #PSA_ERROR_INVALID_ARGUMENT
2876 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2877 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2878 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002879 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002880 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002881 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002882 * The library has not been previously initialized by psa_crypto_init().
2883 * It is implementation-dependent whether a failure to initialize
2884 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002885 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002886psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002887 psa_algorithm_t alg,
2888 const uint8_t *hash,
2889 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002890 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002891 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002892
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002893/**
2894 * \brief Encrypt a short message with a public key.
2895 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002896 * \param handle Handle to the key to use for the operation.
2897 * It must be a public key or an asymmetric
2898 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002899 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002900 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002901 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002902 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002903 * \param[in] salt A salt or label, if supported by the
2904 * encryption algorithm.
2905 * If the algorithm does not support a
2906 * salt, pass \c NULL.
2907 * If the algorithm supports an optional
2908 * salt and you do not want to pass a salt,
2909 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002910 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002911 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2912 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002913 * \param salt_length Size of the \p salt buffer in bytes.
2914 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002915 * \param[out] output Buffer where the encrypted message is to
2916 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002917 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002918 * \param[out] output_length On success, the number of bytes
2919 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002920 *
Gilles Peskine28538492018-07-11 17:34:00 +02002921 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002922 * \retval #PSA_ERROR_INVALID_HANDLE
2923 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002924 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002925 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002926 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002927 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002928 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002929 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002930 * \retval #PSA_ERROR_NOT_SUPPORTED
2931 * \retval #PSA_ERROR_INVALID_ARGUMENT
2932 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2933 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2934 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002935 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002936 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002937 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002938 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002939 * The library has not been previously initialized by psa_crypto_init().
2940 * It is implementation-dependent whether a failure to initialize
2941 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002942 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002943psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002944 psa_algorithm_t alg,
2945 const uint8_t *input,
2946 size_t input_length,
2947 const uint8_t *salt,
2948 size_t salt_length,
2949 uint8_t *output,
2950 size_t output_size,
2951 size_t *output_length);
2952
2953/**
2954 * \brief Decrypt a short message with a private key.
2955 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002956 * \param handle Handle to the key to use for the operation.
2957 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002958 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002959 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002960 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002961 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002962 * \param[in] salt A salt or label, if supported by the
2963 * encryption algorithm.
2964 * If the algorithm does not support a
2965 * salt, pass \c NULL.
2966 * If the algorithm supports an optional
2967 * salt and you do not want to pass a salt,
2968 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002969 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002970 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2971 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002972 * \param salt_length Size of the \p salt buffer in bytes.
2973 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002974 * \param[out] output Buffer where the decrypted message is to
2975 * be written.
2976 * \param output_size Size of the \c output buffer in bytes.
2977 * \param[out] output_length On success, the number of bytes
2978 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002979 *
Gilles Peskine28538492018-07-11 17:34:00 +02002980 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002981 * \retval #PSA_ERROR_INVALID_HANDLE
2982 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002983 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002984 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002985 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002986 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002987 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002988 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002989 * \retval #PSA_ERROR_NOT_SUPPORTED
2990 * \retval #PSA_ERROR_INVALID_ARGUMENT
2991 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2992 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2993 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002994 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002995 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002996 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2997 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002998 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002999 * The library has not been previously initialized by psa_crypto_init().
3000 * It is implementation-dependent whether a failure to initialize
3001 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003002 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003003psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003004 psa_algorithm_t alg,
3005 const uint8_t *input,
3006 size_t input_length,
3007 const uint8_t *salt,
3008 size_t salt_length,
3009 uint8_t *output,
3010 size_t output_size,
3011 size_t *output_length);
3012
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003013/**@}*/
3014
Gilles Peskine35675b62019-05-16 17:26:11 +02003015/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003016 * @{
3017 */
3018
Gilles Peskine35675b62019-05-16 17:26:11 +02003019/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003020 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003021 * Before calling any function on a key derivation operation object, the
3022 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003023 * - Set the structure to all-bits-zero, for example:
3024 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003025 * psa_key_derivation_operation_t operation;
3026 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003027 * \endcode
3028 * - Initialize the structure to logical zero values, for example:
3029 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003030 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003031 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003032 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003033 * for example:
3034 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003035 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003036 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003037 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003038 * to the structure, for example:
3039 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003040 * psa_key_derivation_operation_t operation;
3041 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003042 * \endcode
3043 *
3044 * This is an implementation-defined \c struct. Applications should not
3045 * make any assumptions about the content of this structure except
3046 * as directed by the documentation of a specific implementation.
3047 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003048typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003049
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003050/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003051 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003052 * This macro returns a suitable initializer for a key derivation operation
3053 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003054 */
3055#ifdef __DOXYGEN_ONLY__
3056/* This is an example definition for documentation purposes.
3057 * Implementations should define a suitable value in `crypto_struct.h`.
3058 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003059#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003060#endif
3061
Gilles Peskine35675b62019-05-16 17:26:11 +02003062/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003063 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003064static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003065
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003066/** Set up a key derivation operation.
3067 *
3068 * A key derivation algorithm takes some inputs and uses them to generate
3069 * a byte stream in a deterministic way.
3070 * This byte stream can be used to produce keys and other
3071 * cryptographic material.
3072 *
3073 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003074 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3075 * -# Call psa_key_derivation_setup() to select the algorithm.
3076 * -# Provide the inputs for the key derivation by calling
3077 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3078 * as appropriate. Which inputs are needed, in what order, and whether
3079 * they may be keys and if so of what type depends on the algorithm.
3080 * -# Optionally set the operation's maximum capacity with
3081 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3082 * of or after providing inputs. For some algorithms, this step is mandatory
3083 * because the output depends on the maximum capacity.
3084 * -# To derive a key, call psa_key_derivation_output_key().
3085 * To derive a byte string for a different purpose, call
3086 * psa_key_derivation_output_bytes().
3087 * Successive calls to these functions use successive output bytes
3088 * calculated by the key derivation algorithm.
3089 * -# Clean up the key derivation operation object with
3090 * psa_key_derivation_abort().
3091 *
3092 * If this function returns an error, the key derivation operation object is
3093 * not changed.
3094 *
3095 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3096 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003097 *
3098 * \param[in,out] operation The key derivation operation object
3099 * to set up. It must
3100 * have been initialized but not set up yet.
3101 * \param alg The key derivation algorithm to compute
3102 * (\c PSA_ALG_XXX value such that
3103 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3104 *
3105 * \retval #PSA_SUCCESS
3106 * Success.
3107 * \retval #PSA_ERROR_INVALID_ARGUMENT
3108 * \c alg is not a key derivation algorithm.
3109 * \retval #PSA_ERROR_NOT_SUPPORTED
3110 * \c alg is not supported or is not a key derivation algorithm.
3111 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3112 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3113 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003114 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003115 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003116 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003117 * The operation state is not valid (it must be inactive).
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003118 * \retval #PSA_ERROR_BAD_STATE
3119 * The library has not been previously initialized by psa_crypto_init().
3120 * It is implementation-dependent whether a failure to initialize
3121 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003122 */
3123psa_status_t psa_key_derivation_setup(
3124 psa_key_derivation_operation_t *operation,
3125 psa_algorithm_t alg);
3126
Gilles Peskine35675b62019-05-16 17:26:11 +02003127/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003128 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003129 * The capacity of a key derivation is the maximum number of bytes that it can
3130 * return. When you get *N* bytes of output from a key derivation operation,
3131 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003132 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003133 * \param[in] operation The operation to query.
3134 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003135 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003136 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003137 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003138 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003139 * The operation state is not valid (it must be active).
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003140 * \retval #PSA_ERROR_HARDWARE_FAILURE
3141 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003142 * \retval #PSA_ERROR_BAD_STATE
3143 * The library has not been previously initialized by psa_crypto_init().
3144 * It is implementation-dependent whether a failure to initialize
3145 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003146 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003147psa_status_t psa_key_derivation_get_capacity(
3148 const psa_key_derivation_operation_t *operation,
3149 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003150
Gilles Peskine35675b62019-05-16 17:26:11 +02003151/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003152 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003153 * The capacity of a key derivation operation is the maximum number of bytes
3154 * that the key derivation operation can return from this point onwards.
3155 *
3156 * \param[in,out] operation The key derivation operation object to modify.
3157 * \param capacity The new capacity of the operation.
3158 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003159 * current capacity.
3160 *
3161 * \retval #PSA_SUCCESS
3162 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003163 * \p capacity is larger than the operation's current capacity.
3164 * In this case, the operation object remains valid and its capacity
3165 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003166 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003167 * The operation state is not valid (it must be active).
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003168 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003169 * \retval #PSA_ERROR_HARDWARE_FAILURE
3170 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003171 * \retval #PSA_ERROR_BAD_STATE
3172 * The library has not been previously initialized by psa_crypto_init().
3173 * It is implementation-dependent whether a failure to initialize
3174 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003175 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003176psa_status_t psa_key_derivation_set_capacity(
3177 psa_key_derivation_operation_t *operation,
3178 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003179
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003180/** Use the maximum possible capacity for a key derivation operation.
3181 *
3182 * Use this value as the capacity argument when setting up a key derivation
3183 * to indicate that the operation should have the maximum possible capacity.
3184 * The value of the maximum possible capacity depends on the key derivation
3185 * algorithm.
3186 */
3187#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3188
3189/** Provide an input for key derivation or key agreement.
3190 *
3191 * Which inputs are required and in what order depends on the algorithm.
3192 * Refer to the documentation of each key derivation or key agreement
3193 * algorithm for information.
3194 *
3195 * This function passes direct inputs. Some inputs must be passed as keys
3196 * using psa_key_derivation_input_key() instead of this function. Refer to
3197 * the documentation of individual step types for information.
3198 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003199 * If this function returns an error status, the operation enters an error
3200 * state and must be aborted by calling psa_key_derivation_abort().
3201 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003202 * \param[in,out] operation The key derivation operation object to use.
3203 * It must have been set up with
3204 * psa_key_derivation_setup() and must not
3205 * have produced any output yet.
3206 * \param step Which step the input data is for.
3207 * \param[in] data Input data to use.
3208 * \param data_length Size of the \p data buffer in bytes.
3209 *
3210 * \retval #PSA_SUCCESS
3211 * Success.
3212 * \retval #PSA_ERROR_INVALID_ARGUMENT
3213 * \c step is not compatible with the operation's algorithm.
3214 * \retval #PSA_ERROR_INVALID_ARGUMENT
3215 * \c step does not allow direct inputs.
3216 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3217 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3218 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003219 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003220 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003221 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003222 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003223 * \retval #PSA_ERROR_BAD_STATE
3224 * The library has not been previously initialized by psa_crypto_init().
3225 * It is implementation-dependent whether a failure to initialize
3226 * results in this error code.
3227 */
3228psa_status_t psa_key_derivation_input_bytes(
3229 psa_key_derivation_operation_t *operation,
3230 psa_key_derivation_step_t step,
3231 const uint8_t *data,
3232 size_t data_length);
3233
3234/** Provide an input for key derivation in the form of a key.
3235 *
3236 * Which inputs are required and in what order depends on the algorithm.
3237 * Refer to the documentation of each key derivation or key agreement
3238 * algorithm for information.
3239 *
3240 * This function passes key inputs. Some inputs must be passed as keys
3241 * of the appropriate type using this function, while others must be
3242 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3243 * the documentation of individual step types for information.
3244 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003245 * If this function returns an error status, the operation enters an error
3246 * state and must be aborted by calling psa_key_derivation_abort().
3247 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003248 * \param[in,out] operation The key derivation operation object to use.
3249 * It must have been set up with
3250 * psa_key_derivation_setup() and must not
3251 * have produced any output yet.
3252 * \param step Which step the input data is for.
3253 * \param handle Handle to the key. It must have an
3254 * appropriate type for \p step and must
3255 * allow the usage #PSA_KEY_USAGE_DERIVE.
3256 *
3257 * \retval #PSA_SUCCESS
3258 * Success.
3259 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003260 * \retval #PSA_ERROR_NOT_PERMITTED
3261 * \retval #PSA_ERROR_INVALID_ARGUMENT
3262 * \c step is not compatible with the operation's algorithm.
3263 * \retval #PSA_ERROR_INVALID_ARGUMENT
3264 * \c step does not allow key inputs.
3265 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3266 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3267 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003268 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003269 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003270 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003271 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003272 * \retval #PSA_ERROR_BAD_STATE
3273 * The library has not been previously initialized by psa_crypto_init().
3274 * It is implementation-dependent whether a failure to initialize
3275 * results in this error code.
3276 */
3277psa_status_t psa_key_derivation_input_key(
3278 psa_key_derivation_operation_t *operation,
3279 psa_key_derivation_step_t step,
3280 psa_key_handle_t handle);
3281
3282/** Perform a key agreement and use the shared secret as input to a key
3283 * derivation.
3284 *
3285 * A key agreement algorithm takes two inputs: a private key \p private_key
3286 * a public key \p peer_key.
3287 * The result of this function is passed as input to a key derivation.
3288 * The output of this key derivation can be extracted by reading from the
3289 * resulting operation to produce keys and other cryptographic material.
3290 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003291 * If this function returns an error status, the operation enters an error
3292 * state and must be aborted by calling psa_key_derivation_abort().
3293 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003294 * \param[in,out] operation The key derivation operation object to use.
3295 * It must have been set up with
3296 * psa_key_derivation_setup() with a
3297 * key agreement and derivation algorithm
3298 * \c alg (\c PSA_ALG_XXX value such that
3299 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3300 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3301 * is false).
3302 * The operation must be ready for an
3303 * input of the type given by \p step.
3304 * \param step Which step the input data is for.
3305 * \param private_key Handle to the private key to use.
3306 * \param[in] peer_key Public key of the peer. The peer key must be in the
3307 * same format that psa_import_key() accepts for the
3308 * public key type corresponding to the type of
3309 * private_key. That is, this function performs the
3310 * equivalent of
3311 * #psa_import_key(...,
3312 * `peer_key`, `peer_key_length`) where
3313 * with key attributes indicating the public key
3314 * type corresponding to the type of `private_key`.
3315 * For example, for EC keys, this means that peer_key
3316 * is interpreted as a point on the curve that the
3317 * private key is on. The standard formats for public
3318 * keys are documented in the documentation of
3319 * psa_export_public_key().
3320 * \param peer_key_length Size of \p peer_key in bytes.
3321 *
3322 * \retval #PSA_SUCCESS
3323 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01003324 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003325 * The operation state is not valid for this key agreement \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003326 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003327 * \retval #PSA_ERROR_NOT_PERMITTED
3328 * \retval #PSA_ERROR_INVALID_ARGUMENT
3329 * \c private_key is not compatible with \c alg,
3330 * or \p peer_key is not valid for \c alg or not compatible with
3331 * \c private_key.
3332 * \retval #PSA_ERROR_NOT_SUPPORTED
3333 * \c alg is not supported or is not a key derivation algorithm.
3334 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3335 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3336 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003337 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003338 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003339 * \retval #PSA_ERROR_BAD_STATE
3340 * The library has not been previously initialized by psa_crypto_init().
3341 * It is implementation-dependent whether a failure to initialize
3342 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003343 */
3344psa_status_t psa_key_derivation_key_agreement(
3345 psa_key_derivation_operation_t *operation,
3346 psa_key_derivation_step_t step,
3347 psa_key_handle_t private_key,
3348 const uint8_t *peer_key,
3349 size_t peer_key_length);
3350
Gilles Peskine35675b62019-05-16 17:26:11 +02003351/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003352 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003353 * This function calculates output bytes from a key derivation algorithm and
3354 * return those bytes.
3355 * If you view the key derivation's output as a stream of bytes, this
3356 * function destructively reads the requested number of bytes from the
3357 * stream.
3358 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003359 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003360 * If this function returns an error status, the operation enters an error
3361 * state and must be aborted by calling psa_key_derivation_abort().
3362 *
3363* \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine35675b62019-05-16 17:26:11 +02003364 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003365 * \param output_length Number of bytes to output.
3366 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003367 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003368 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003369 * The operation's capacity was less than
3370 * \p output_length bytes. Note that in this case,
3371 * no output is written to the output buffer.
3372 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003373 * subsequent calls to this function will not
3374 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003375 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003376 * The operation state is not valid (it must be active and completed
3377 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003378 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3379 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3380 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003381 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003382 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003383 * \retval #PSA_ERROR_BAD_STATE
3384 * The library has not been previously initialized by psa_crypto_init().
3385 * It is implementation-dependent whether a failure to initialize
3386 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003387 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003388psa_status_t psa_key_derivation_output_bytes(
3389 psa_key_derivation_operation_t *operation,
3390 uint8_t *output,
3391 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003392
Gilles Peskine35675b62019-05-16 17:26:11 +02003393/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003394 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003395 * This function calculates output bytes from a key derivation algorithm
3396 * and uses those bytes to generate a key deterministically.
3397 * If you view the key derivation's output as a stream of bytes, this
3398 * function destructively reads as many bytes as required from the
3399 * stream.
3400 * The operation's capacity decreases by the number of bytes read.
3401 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003402 * If this function returns an error status, the operation enters an error
3403 * state and must be aborted by calling psa_key_derivation_abort().
3404 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003405 * How much output is produced and consumed from the operation, and how
3406 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003407 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003408 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003409 * of a given size, this function is functionally equivalent to
3410 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003411 * and passing the resulting output to #psa_import_key.
3412 * However, this function has a security benefit:
3413 * if the implementation provides an isolation boundary then
3414 * the key material is not exposed outside the isolation boundary.
3415 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003416 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003417 * The following key types defined in this specification follow this scheme:
3418 *
3419 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003420 * - #PSA_KEY_TYPE_ARC4;
3421 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003422 * - #PSA_KEY_TYPE_DERIVE;
3423 * - #PSA_KEY_TYPE_HMAC.
3424 *
3425 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003426 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003427 * Montgomery curve), this function always draws a byte string whose
3428 * length is determined by the curve, and sets the mandatory bits
3429 * accordingly. That is:
3430 *
3431 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3432 * and process it as specified in RFC 7748 &sect;5.
3433 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3434 * and process it as specified in RFC 7748 &sect;5.
3435 *
3436 * - For key types for which the key is represented by a single sequence of
3437 * \p bits bits with constraints as to which bit sequences are acceptable,
3438 * this function draws a byte string of length (\p bits / 8) bytes rounded
3439 * up to the nearest whole number of bytes. If the resulting byte string
3440 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3441 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003442 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003443 * for the output produced by psa_export_key().
3444 * The following key types defined in this specification follow this scheme:
3445 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003446 * - #PSA_KEY_TYPE_DES.
3447 * Force-set the parity bits, but discard forbidden weak keys.
3448 * For 2-key and 3-key triple-DES, the three keys are generated
3449 * successively (for example, for 3-key triple-DES,
3450 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3451 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003452 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003453 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003454 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003455 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003456 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003457 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003458 * Weierstrass curve).
3459 * For these key types, interpret the byte string as integer
3460 * in big-endian order. Discard it if it is not in the range
3461 * [0, *N* - 2] where *N* is the boundary of the private key domain
3462 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003463 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003464 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003465 * This method allows compliance to NIST standards, specifically
3466 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003467 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3468 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3469 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3470 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003471 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003472 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003473 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003474 * implementation-defined.
3475 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003476 * In all cases, the data that is read is discarded from the operation.
3477 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003478 *
Gilles Peskine20628592019-04-19 19:29:50 +02003479 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003480 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003481 * \param[out] handle On success, a handle to the newly created key.
3482 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003483 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003484 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003485 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003486 * If the key is persistent, the key material and the key's metadata
3487 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003488 * \retval #PSA_ERROR_ALREADY_EXISTS
3489 * This is an attempt to create a persistent key, and there is
3490 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003491 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003492 * There was not enough data to create the desired key.
3493 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003494 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003495 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003496 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003497 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003498 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003499 * \retval #PSA_ERROR_INVALID_ARGUMENT
3500 * The provided key attributes are not valid for the operation.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003501 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003502 * The operation state is not valid (it must be active and completed
3503 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003504 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3505 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3506 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3507 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003508 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003509 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003510 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003511 * The library has not been previously initialized by psa_crypto_init().
3512 * It is implementation-dependent whether a failure to initialize
3513 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003514 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003515psa_status_t psa_key_derivation_output_key(
3516 const psa_key_attributes_t *attributes,
3517 psa_key_derivation_operation_t *operation,
3518 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003519
Gilles Peskine35675b62019-05-16 17:26:11 +02003520/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003521 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003522 * Once a key derivation operation has been aborted, its capacity is zero.
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003523 * Aborting an operation frees all associated resources except for the \c
3524 * operation structure itself. Once aborted, the operation object can be reused
3525 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003526 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003527 * This function may be called at any time after the operation
3528 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003529 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003530 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3531 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003532 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003533 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003534 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003535 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003536 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3537 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003538 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003539 * \retval #PSA_ERROR_BAD_STATE
3540 * The library has not been previously initialized by psa_crypto_init().
3541 * It is implementation-dependent whether a failure to initialize
3542 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003543 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003544psa_status_t psa_key_derivation_abort(
3545 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003546
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003547/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003548 *
3549 * \warning The raw result of a key agreement algorithm such as finite-field
3550 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3551 * not be used directly as key material. It should instead be passed as
3552 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003553 * a key derivation, use psa_key_derivation_key_agreement() and other
3554 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003555 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003556 * \param alg The key agreement algorithm to compute
3557 * (\c PSA_ALG_XXX value such that
3558 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3559 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003560 * \param private_key Handle to the private key to use.
3561 * \param[in] peer_key Public key of the peer. It must be
3562 * in the same format that psa_import_key()
3563 * accepts. The standard formats for public
3564 * keys are documented in the documentation
3565 * of psa_export_public_key().
3566 * \param peer_key_length Size of \p peer_key in bytes.
3567 * \param[out] output Buffer where the decrypted message is to
3568 * be written.
3569 * \param output_size Size of the \c output buffer in bytes.
3570 * \param[out] output_length On success, the number of bytes
3571 * that make up the returned output.
3572 *
3573 * \retval #PSA_SUCCESS
3574 * Success.
3575 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003576 * \retval #PSA_ERROR_NOT_PERMITTED
3577 * \retval #PSA_ERROR_INVALID_ARGUMENT
3578 * \p alg is not a key agreement algorithm
3579 * \retval #PSA_ERROR_INVALID_ARGUMENT
3580 * \p private_key is not compatible with \p alg,
3581 * or \p peer_key is not valid for \p alg or not compatible with
3582 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003583 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3584 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003585 * \retval #PSA_ERROR_NOT_SUPPORTED
3586 * \p alg is not a supported key agreement algorithm.
3587 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3588 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3589 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003590 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003591 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003592 * \retval #PSA_ERROR_BAD_STATE
3593 * The library has not been previously initialized by psa_crypto_init().
3594 * It is implementation-dependent whether a failure to initialize
3595 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003596 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003597psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3598 psa_key_handle_t private_key,
3599 const uint8_t *peer_key,
3600 size_t peer_key_length,
3601 uint8_t *output,
3602 size_t output_size,
3603 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003604
Gilles Peskineea0fb492018-07-12 17:17:20 +02003605/**@}*/
3606
Gilles Peskineedd76872018-07-20 17:42:05 +02003607/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003608 * @{
3609 */
3610
3611/**
3612 * \brief Generate random bytes.
3613 *
3614 * \warning This function **can** fail! Callers MUST check the return status
3615 * and MUST NOT use the content of the output buffer if the return
3616 * status is not #PSA_SUCCESS.
3617 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003618 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003619 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003620 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003621 * \param output_size Number of bytes to generate and output.
3622 *
Gilles Peskine28538492018-07-11 17:34:00 +02003623 * \retval #PSA_SUCCESS
3624 * \retval #PSA_ERROR_NOT_SUPPORTED
3625 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003626 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003627 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3628 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003629 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003630 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003631 * The library has not been previously initialized by psa_crypto_init().
3632 * It is implementation-dependent whether a failure to initialize
3633 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003634 */
3635psa_status_t psa_generate_random(uint8_t *output,
3636 size_t output_size);
3637
3638/**
3639 * \brief Generate a key or key pair.
3640 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003641 * The key is generated randomly.
3642 * Its location, policy, type and size are taken from \p attributes.
3643 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003644 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003645 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003646 * the public exponent is 65537.
3647 * The modulus is a product of two probabilistic primes
3648 * between 2^{n-1} and 2^n where n is the bit size specified in the
3649 * attributes.
3650 *
Gilles Peskine20628592019-04-19 19:29:50 +02003651 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003652 * \param[out] handle On success, a handle to the newly created key.
3653 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003654 *
Gilles Peskine28538492018-07-11 17:34:00 +02003655 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003656 * Success.
3657 * If the key is persistent, the key material and the key's metadata
3658 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003659 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003660 * This is an attempt to create a persistent key, and there is
3661 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003662 * \retval #PSA_ERROR_NOT_SUPPORTED
3663 * \retval #PSA_ERROR_INVALID_ARGUMENT
3664 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3665 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3666 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3667 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003668 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003669 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3670 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003671 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003672 * The library has not been previously initialized by psa_crypto_init().
3673 * It is implementation-dependent whether a failure to initialize
3674 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003675 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003676psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003677 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003678
3679/**@}*/
3680
Gilles Peskinee59236f2018-01-27 23:32:46 +01003681#ifdef __cplusplus
3682}
3683#endif
3684
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003685/* The file "crypto_sizes.h" contains definitions for size calculation
3686 * macros whose definitions are implementation-specific. */
3687#include "crypto_sizes.h"
3688
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003689/* The file "crypto_struct.h" contains definitions for
3690 * implementation-specific structs that are declared above. */
3691#include "crypto_struct.h"
3692
3693/* The file "crypto_extra.h" contains vendor-specific definitions. This
3694 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003695#include "crypto_extra.h"
3696
3697#endif /* PSA_CRYPTO_H */