blob: cf2c1ef31ad45c4b8a5ed6c54c058ea45474c458 [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
Adrian L. Shawd89338a2019-09-19 13:32:57 +010060/** \defgroup API version
61 * @{
62 */
63
64/**
65 * The major version of this implementation of the PSA Crypto API
66 */
67#define PSA_CRYPTO_API_VERSION_MAJOR 1
68
69/**
70 * The minor version of this implementation of the PSA Crypto API
71 */
72#define PSA_CRYPTO_API_VERSION_MINOR 0
73
74/**@}*/
75
Gilles Peskinef3b731e2018-12-12 13:38:31 +010076/* The file "crypto_values.h" declares macros to build and analyze values
77 * of integral types defined in "crypto_types.h". */
78#include "crypto_values.h"
79
80/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010081 * @{
82 */
83
84/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010085 * \brief Library initialization.
86 *
87 * Applications must call this function before calling any other
88 * function in this module.
89 *
90 * Applications may call this function more than once. Once a call
91 * succeeds, subsequent calls are guaranteed to succeed.
92 *
itayzafrir18617092018-09-16 12:22:41 +030093 * If the application calls other functions before calling psa_crypto_init(),
94 * the behavior is undefined. Implementations are encouraged to either perform
95 * the operation as if the library had been initialized or to return
96 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
97 * implementations should not return a success status if the lack of
98 * initialization may have security implications, for example due to improper
99 * seeding of the random number generator.
100 *
Gilles Peskine28538492018-07-11 17:34:00 +0200101 * \retval #PSA_SUCCESS
102 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
103 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
104 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200105 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +0200106 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +0100107 */
108psa_status_t psa_crypto_init(void);
109
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100110/**@}*/
111
Gilles Peskine105f67f2019-07-23 18:16:05 +0200112/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +0200113 * @{
114 */
115
Gilles Peskinea0c06552019-05-21 15:54:54 +0200116/** \def PSA_KEY_ATTRIBUTES_INIT
117 *
118 * This macro returns a suitable initializer for a key attribute structure
119 * of type #psa_key_attributes_t.
120 */
121#ifdef __DOXYGEN_ONLY__
122/* This is an example definition for documentation purposes.
123 * Implementations should define a suitable value in `crypto_struct.h`.
124 */
125#define PSA_KEY_ATTRIBUTES_INIT {0}
126#endif
127
128/** Return an initial value for a key attributes structure.
129 */
130static psa_key_attributes_t psa_key_attributes_init(void);
131
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200132/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200133 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200134 * If the attribute structure currently declares the key as volatile (which
135 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200136 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200137 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200138 * This function does not access storage, it merely stores the given
139 * value in the structure.
140 * The persistent key will be written to storage when the attribute
141 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200142 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200143 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200144 *
Gilles Peskine20628592019-04-19 19:29:50 +0200145 * This function may be declared as `static` (i.e. without external
146 * linkage). This function may be provided as a function-like macro,
147 * but in this case it must evaluate each of its arguments exactly once.
148 *
149 * \param[out] attributes The attribute structure to write to.
150 * \param id The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200151 */
152static void psa_set_key_id(psa_key_attributes_t *attributes,
153 psa_key_id_t id);
154
155/** Set the location of a persistent key.
156 *
157 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200158 * with psa_set_key_id(). By default, a key that has a persistent identifier
159 * is stored in the default storage area identifier by
160 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
161 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200162 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200163 * This function does not access storage, it merely stores the given
164 * value in the structure.
165 * The persistent key will be written to storage when the attribute
166 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200167 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200168 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200169 *
170 * This function may be declared as `static` (i.e. without external
171 * linkage). This function may be provided as a function-like macro,
172 * but in this case it must evaluate each of its arguments exactly once.
173 *
174 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200175 * \param lifetime The lifetime for the key.
176 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200177 * key will be volatile, and the key identifier
178 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200179 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200180static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
181 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200182
Gilles Peskine20628592019-04-19 19:29:50 +0200183/** Retrieve the key identifier from key attributes.
184 *
185 * This function may be declared as `static` (i.e. without external
186 * linkage). This function may be provided as a function-like macro,
187 * but in this case it must evaluate its argument exactly once.
188 *
189 * \param[in] attributes The key attribute structure to query.
190 *
191 * \return The persistent identifier stored in the attribute structure.
192 * This value is unspecified if the attribute structure declares
193 * the key as volatile.
194 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200195static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
196
Gilles Peskine20628592019-04-19 19:29:50 +0200197/** Retrieve the lifetime from key attributes.
198 *
199 * This function may be declared as `static` (i.e. without external
200 * linkage). This function may be provided as a function-like macro,
201 * but in this case it must evaluate its argument exactly once.
202 *
203 * \param[in] attributes The key attribute structure to query.
204 *
205 * \return The lifetime value stored in the attribute structure.
206 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200207static psa_key_lifetime_t psa_get_key_lifetime(
208 const psa_key_attributes_t *attributes);
209
Gilles Peskine20628592019-04-19 19:29:50 +0200210/** Declare usage flags for a key.
211 *
212 * Usage flags are part of a key's usage policy. They encode what
213 * kind of operations are permitted on the key. For more details,
214 * refer to the documentation of the type #psa_key_usage_t.
215 *
216 * This function overwrites any usage flags
217 * previously set in \p attributes.
218 *
219 * This function may be declared as `static` (i.e. without external
220 * linkage). This function may be provided as a function-like macro,
221 * but in this case it must evaluate each of its arguments exactly once.
222 *
223 * \param[out] attributes The attribute structure to write to.
224 * \param usage_flags The usage flags to write.
225 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200226static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
227 psa_key_usage_t usage_flags);
228
Gilles Peskine20628592019-04-19 19:29:50 +0200229/** Retrieve the usage flags from key attributes.
230 *
231 * This function may be declared as `static` (i.e. without external
232 * linkage). This function may be provided as a function-like macro,
233 * but in this case it must evaluate its argument exactly once.
234 *
235 * \param[in] attributes The key attribute structure to query.
236 *
237 * \return The usage flags stored in the attribute structure.
238 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200239static psa_key_usage_t psa_get_key_usage_flags(
240 const psa_key_attributes_t *attributes);
241
Gilles Peskine20628592019-04-19 19:29:50 +0200242/** Declare the permitted algorithm policy for a key.
243 *
244 * The permitted algorithm policy of a key encodes which algorithm or
Gilles Peskinea170d922019-09-12 16:59:37 +0200245 * algorithms are permitted to be used with this key. The following
246 * algorithm policies are supported:
247 * - 0 does not allow any cryptographic operation with the key. The key
248 * may be used for non-cryptographic actions such as exporting (if
249 * permitted by the usage flags).
250 * - An algorithm value permits this particular algorithm.
251 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
252 * signature scheme with any hash algorithm.
Gilles Peskine20628592019-04-19 19:29:50 +0200253 *
254 * This function overwrites any algorithm policy
255 * previously set in \p attributes.
256 *
257 * This function may be declared as `static` (i.e. without external
258 * linkage). This function may be provided as a function-like macro,
259 * but in this case it must evaluate each of its arguments exactly once.
260 *
261 * \param[out] attributes The attribute structure to write to.
262 * \param alg The permitted algorithm policy to write.
263 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200264static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
265 psa_algorithm_t alg);
266
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100267
Gilles Peskine20628592019-04-19 19:29:50 +0200268/** Retrieve the algorithm policy from key attributes.
269 *
270 * This function may be declared as `static` (i.e. without external
271 * linkage). This function may be provided as a function-like macro,
272 * but in this case it must evaluate its argument exactly once.
273 *
274 * \param[in] attributes The key attribute structure to query.
275 *
276 * \return The algorithm stored in the attribute structure.
277 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200278static psa_algorithm_t psa_get_key_algorithm(
279 const psa_key_attributes_t *attributes);
280
Gilles Peskine20628592019-04-19 19:29:50 +0200281/** Declare the type of a key.
282 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200283 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200284 * previously set in \p attributes.
285 *
286 * This function may be declared as `static` (i.e. without external
287 * linkage). This function may be provided as a function-like macro,
288 * but in this case it must evaluate each of its arguments exactly once.
289 *
290 * \param[out] attributes The attribute structure to write to.
291 * \param type The key type to write.
Gilles Peskinea170d922019-09-12 16:59:37 +0200292 * If this is 0, the key type in \p attributes
293 * becomes unspecified.
Gilles Peskine20628592019-04-19 19:29:50 +0200294 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200295static void psa_set_key_type(psa_key_attributes_t *attributes,
296 psa_key_type_t type);
297
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100298
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200299/** Declare the size of a key.
300 *
301 * This function overwrites any key size previously set in \p attributes.
302 *
303 * This function may be declared as `static` (i.e. without external
304 * linkage). This function may be provided as a function-like macro,
305 * but in this case it must evaluate each of its arguments exactly once.
306 *
307 * \param[out] attributes The attribute structure to write to.
308 * \param bits The key size in bits.
Gilles Peskinea170d922019-09-12 16:59:37 +0200309 * If this is 0, the key size in \p attributes
Gilles Peskine05c900b2019-09-12 18:29:43 +0200310 * becomes unspecified. Keys of size 0 are
311 * not supported.
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200312 */
313static void psa_set_key_bits(psa_key_attributes_t *attributes,
314 size_t bits);
315
Gilles Peskine20628592019-04-19 19:29:50 +0200316/** Retrieve the key type from key attributes.
317 *
318 * This function may be declared as `static` (i.e. without external
319 * linkage). This function may be provided as a function-like macro,
320 * but in this case it must evaluate its argument exactly once.
321 *
322 * \param[in] attributes The key attribute structure to query.
323 *
324 * \return The key type stored in the attribute structure.
325 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200326static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
327
Gilles Peskine20628592019-04-19 19:29:50 +0200328/** Retrieve the key size from key attributes.
329 *
330 * This function may be declared as `static` (i.e. without external
331 * linkage). This function may be provided as a function-like macro,
332 * but in this case it must evaluate its argument exactly once.
333 *
334 * \param[in] attributes The key attribute structure to query.
335 *
336 * \return The key size stored in the attribute structure, in bits.
337 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200338static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
339
Gilles Peskine20628592019-04-19 19:29:50 +0200340/** Retrieve the attributes of a key.
341 *
342 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200343 * psa_reset_key_attributes(). It then copies the attributes of
344 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200345 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200346 * \note This function may allocate memory or other resources.
347 * Once you have called this function on an attribute structure,
348 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200349 *
Gilles Peskine20628592019-04-19 19:29:50 +0200350 * \param[in] handle Handle to the key to query.
351 * \param[in,out] attributes On success, the attributes of the key.
352 * On failure, equivalent to a
353 * freshly-initialized structure.
354 *
355 * \retval #PSA_SUCCESS
356 * \retval #PSA_ERROR_INVALID_HANDLE
357 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
358 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100359 * \retval #PSA_ERROR_CORRUPTION_DETECTED
360 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100361 * \retval #PSA_ERROR_BAD_STATE
362 * The library has not been previously initialized by psa_crypto_init().
363 * It is implementation-dependent whether a failure to initialize
364 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200365 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200366psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
367 psa_key_attributes_t *attributes);
368
Gilles Peskine20628592019-04-19 19:29:50 +0200369/** Reset a key attribute structure to a freshly initialized state.
370 *
371 * You must initialize the attribute structure as described in the
372 * documentation of the type #psa_key_attributes_t before calling this
373 * function. Once the structure has been initialized, you may call this
374 * function at any time.
375 *
376 * This function frees any auxiliary resources that the structure
377 * may contain.
378 *
379 * \param[in,out] attributes The attribute structure to reset.
380 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200381void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200382
Gilles Peskine87a5e562019-04-17 12:28:25 +0200383/**@}*/
384
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100385/** \defgroup key_management Key management
386 * @{
387 */
388
Gilles Peskinef535eb22018-11-30 14:08:36 +0100389/** Open a handle to an existing persistent key.
390 *
Gilles Peskine4754cde2019-05-21 15:56:29 +0200391 * Open a handle to a persistent key. A key is persistent if it was created
392 * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
393 * always has a nonzero key identifier, set with psa_set_key_id() when
394 * creating the key. Implementations may provide additional pre-provisioned
Andrew Thoelke203491c2019-08-21 17:55:30 +0100395 * keys that can be opened with psa_open_key(). Such keys have a key identifier
396 * in the vendor range, as documented in the description of #psa_key_id_t.
Gilles Peskine4754cde2019-05-21 15:56:29 +0200397 *
Andrew Thoelkede183412019-09-05 09:38:06 +0100398 * The application must eventually close the handle with psa_close_key() or
399 * psa_destroy_key() to release associated resources. If the application dies
400 * without calling one of these functions, the implementation should perform
401 * the equivalent of a call to psa_close_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100402 *
Andrew Thoelke9741b112019-08-21 18:20:41 +0100403 * Some implementations permit an application to open the same key multiple
Andrew Thoelkede183412019-09-05 09:38:06 +0100404 * times. If this is successful, each call to psa_open_key() will return a
405 * different key handle.
406 *
407 * \note Applications that rely on opening a key multiple times will not be
408 * portable to implementations that only permit a single key handle to be
409 * opened. See also :ref:\`key-handles\`.
Andrew Thoelke9741b112019-08-21 18:20:41 +0100410 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100411 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100412 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100413 *
414 * \retval #PSA_SUCCESS
415 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100416 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100417 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Andrew Thoelke9741b112019-08-21 18:20:41 +0100418 * The implementation does not have sufficient resources to open the
419 * key. This can be due to reaching an implementation limit on the
420 * number of open keys, the number of open key handles, or available
421 * memory.
David Saadab4ecc272019-02-14 13:48:10 +0200422 * \retval #PSA_ERROR_DOES_NOT_EXIST
Andrew Thoelke9741b112019-08-21 18:20:41 +0100423 * There is no persistent key with key identifier \p id.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100424 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrew Thoelke9741b112019-08-21 18:20:41 +0100425 * \p id is not a valid persistent key identifier.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100426 * \retval #PSA_ERROR_NOT_PERMITTED
427 * The specified key exists, but the application does not have the
428 * permission to access it. Note that this specification does not
429 * define any way to create such a key, but it may be possible
430 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200431 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawd789dc12019-08-12 15:06:48 +0100432 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine225010f2019-05-06 18:44:55 +0200433 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100434 * \retval #PSA_ERROR_BAD_STATE
435 * The library has not been previously initialized by psa_crypto_init().
436 * It is implementation-dependent whether a failure to initialize
437 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100438 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200439psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100440 psa_key_handle_t *handle);
441
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100442
Gilles Peskinef535eb22018-11-30 14:08:36 +0100443/** Close a key handle.
444 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100445 * If the handle designates a volatile key, this will destroy the key material
446 * and free all associated resources, just like psa_destroy_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100447 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100448 * If this is the last open handle to a persistent key, then closing the handle
449 * will free all resources associated with the key in volatile memory. The key
450 * data in persistent storage is not affected and can be opened again later
451 * with a call to psa_open_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100452 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100453 * Closing the key handle makes the handle invalid, and the key handle
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100454 * must not be used again by the application.
Andrew Thoelke3daba812019-08-21 22:46:56 +0100455 *
Andrew Thoelke970629f2019-09-09 09:56:34 +0100456 * \note If the key handle was used to set up an active
Andrew Thoelkede183412019-09-05 09:38:06 +0100457 * :ref:\`multipart operation <multipart-operations>\`, then closing the
458 * key handle can cause the multipart operation to fail. Applications should
459 * maintain the key handle until after the multipart operation has finished.
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100460 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100461 * \param handle The key handle to close.
462 *
463 * \retval #PSA_SUCCESS
464 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100465 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawf97c8522019-08-15 13:27:12 +0100466 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100467 * \retval #PSA_ERROR_BAD_STATE
468 * The library has not been previously initialized by psa_crypto_init().
469 * It is implementation-dependent whether a failure to initialize
470 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100471 */
472psa_status_t psa_close_key(psa_key_handle_t handle);
473
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100474/** Make a copy of a key.
475 *
476 * Copy key material from one location to another.
477 *
478 * This function is primarily useful to copy a key from one location
479 * to another, since it populates a key using the material from
480 * another key which may have a different lifetime.
481 *
482 * This function may be used to share a key with a different party,
483 * subject to implementation-defined restrictions on key sharing.
484 *
485 * The policy on the source key must have the usage flag
486 * #PSA_KEY_USAGE_COPY set.
487 * This flag is sufficient to permit the copy if the key has the lifetime
488 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
489 * Some secure elements do not provide a way to copy a key without
490 * making it extractable from the secure element. If a key is located
491 * in such a secure element, then the key must have both usage flags
492 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
493 * a copy of the key outside the secure element.
494 *
495 * The resulting key may only be used in a way that conforms to
496 * both the policy of the original key and the policy specified in
497 * the \p attributes parameter:
498 * - The usage flags on the resulting key are the bitwise-and of the
499 * usage flags on the source policy and the usage flags in \p attributes.
500 * - If both allow the same algorithm or wildcard-based
501 * algorithm policy, the resulting key has the same algorithm policy.
502 * - If either of the policies allows an algorithm and the other policy
503 * allows a wildcard-based algorithm policy that includes this algorithm,
504 * the resulting key allows the same algorithm.
505 * - If the policies do not allow any algorithm in common, this function
506 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
507 *
508 * The effect of this function on implementation-defined attributes is
509 * implementation-defined.
510 *
511 * \param source_handle The key to copy. It must be a valid key handle.
512 * \param[in] attributes The attributes for the new key.
513 * They are used as follows:
514 * - The key type and size may be 0. If either is
515 * nonzero, it must match the corresponding
516 * attribute of the source key.
517 * - The key location (the lifetime and, for
518 * persistent keys, the key identifier) is
519 * used directly.
520 * - The policy constraints (usage flags and
521 * algorithm policy) are combined from
522 * the source key and \p attributes so that
523 * both sets of restrictions apply, as
524 * described in the documentation of this function.
525 * \param[out] target_handle On success, a handle to the newly created key.
526 * \c 0 on failure.
527 *
528 * \retval #PSA_SUCCESS
529 * \retval #PSA_ERROR_INVALID_HANDLE
530 * \p source_handle is invalid.
531 * \retval #PSA_ERROR_ALREADY_EXISTS
532 * This is an attempt to create a persistent key, and there is
533 * already a persistent key with the given identifier.
534 * \retval #PSA_ERROR_INVALID_ARGUMENT
535 * The lifetime or identifier in \p attributes are invalid.
536 * \retval #PSA_ERROR_INVALID_ARGUMENT
537 * The policy constraints on the source and specified in
538 * \p attributes are incompatible.
539 * \retval #PSA_ERROR_INVALID_ARGUMENT
540 * \p attributes specifies a key type or key size
541 * which does not match the attributes of the source key.
542 * \retval #PSA_ERROR_NOT_PERMITTED
543 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
544 * \retval #PSA_ERROR_NOT_PERMITTED
545 * The source key is not exportable and its lifetime does not
546 * allow copying it to the target's lifetime.
547 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
548 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
549 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
550 * \retval #PSA_ERROR_HARDWARE_FAILURE
551 * \retval #PSA_ERROR_STORAGE_FAILURE
552 * \retval #PSA_ERROR_CORRUPTION_DETECTED
553 * \retval #PSA_ERROR_BAD_STATE
554 * The library has not been previously initialized by psa_crypto_init().
555 * It is implementation-dependent whether a failure to initialize
556 * results in this error code.
557 */
558psa_status_t psa_copy_key(psa_key_handle_t source_handle,
559 const psa_key_attributes_t *attributes,
560 psa_key_handle_t *target_handle);
561
562
563/**
564 * \brief Destroy a key.
565 *
566 * This function destroys a key from both volatile
567 * memory and, if applicable, non-volatile storage. Implementations shall
568 * make a best effort to ensure that that the key material cannot be recovered.
569 *
570 * This function also erases any metadata such as policies and frees
571 * resources associated with the key. To free all resources associated with
572 * the key, all handles to the key must be closed or destroyed.
573 *
574 * Destroying the key makes the handle invalid, and the key handle
575 * must not be used again by the application. Using other open handles to the
576 * destroyed key in a cryptographic operation will result in an error.
577 *
578 * If a key is currently in use in a multipart operation, then destroying the
579 * key will cause the multipart operation to fail.
580 *
581 * \param handle Handle to the key to erase.
582 *
583 * \retval #PSA_SUCCESS
584 * The key material has been erased.
585 * \retval #PSA_ERROR_NOT_PERMITTED
586 * The key cannot be erased because it is
587 * read-only, either due to a policy or due to physical restrictions.
588 * \retval #PSA_ERROR_INVALID_HANDLE
589 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
590 * There was an failure in communication with the cryptoprocessor.
591 * The key material may still be present in the cryptoprocessor.
592 * \retval #PSA_ERROR_STORAGE_FAILURE
593 * The storage is corrupted. Implementations shall make a best effort
594 * to erase key material even in this stage, however applications
595 * should be aware that it may be impossible to guarantee that the
596 * key material is not recoverable in such cases.
597 * \retval #PSA_ERROR_CORRUPTION_DETECTED
598 * An unexpected condition which is not a storage corruption or
599 * a communication failure occurred. The cryptoprocessor may have
600 * been compromised.
601 * \retval #PSA_ERROR_BAD_STATE
602 * The library has not been previously initialized by psa_crypto_init().
603 * It is implementation-dependent whether a failure to initialize
604 * results in this error code.
605 */
606psa_status_t psa_destroy_key(psa_key_handle_t handle);
607
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100608/**@}*/
609
610/** \defgroup import_export Key import and export
611 * @{
612 */
613
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100614/**
615 * \brief Import a key in binary format.
616 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100617 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100618 * documentation of psa_export_public_key() for the format of public keys
619 * and to the documentation of psa_export_key() for the format for
620 * other key types.
621 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200622 * The key data determines the key size. The attributes may optionally
623 * specify a key size; in this case it must match the size determined
624 * from the key data. A key size of 0 in \p attributes indicates that
625 * the key size is solely determined by the key data.
626 *
627 * Implementations must reject an attempt to import a key of size 0.
628 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100629 * This specification supports a single format for each key type.
630 * Implementations may support other formats as long as the standard
631 * format is supported. Implementations that support other formats
632 * should ensure that the formats are clearly unambiguous so as to
633 * minimize the risk that an invalid input is accidentally interpreted
634 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100635 *
Gilles Peskine20628592019-04-19 19:29:50 +0200636 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200637 * The key size is always determined from the
638 * \p data buffer.
639 * If the key size in \p attributes is nonzero,
640 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200641 * \param[out] handle On success, a handle to the newly created key.
642 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100643 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200644 * buffer is interpreted according to the type declared
645 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200646 * All implementations must support at least the format
647 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100648 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200649 * the chosen type. Implementations may allow other
650 * formats, but should be conservative: implementations
651 * should err on the side of rejecting content if it
652 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200653 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100654 *
Gilles Peskine28538492018-07-11 17:34:00 +0200655 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100656 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100657 * If the key is persistent, the key material and the key's metadata
658 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200659 * \retval #PSA_ERROR_ALREADY_EXISTS
660 * This is an attempt to create a persistent key, and there is
661 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200662 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200663 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200664 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200665 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200666 * The key attributes, as a whole, are invalid.
667 * \retval #PSA_ERROR_INVALID_ARGUMENT
668 * The key data is not correctly formatted.
669 * \retval #PSA_ERROR_INVALID_ARGUMENT
670 * The size in \p attributes is nonzero and does not match the size
671 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200672 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
673 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
674 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100675 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200676 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200677 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300678 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw97d3bc32019-08-08 14:40:04 +0100679 * \p operation is either not initialized or is in use
680 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300681 * The library has not been previously initialized by psa_crypto_init().
682 * It is implementation-dependent whether a failure to initialize
683 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100684 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200685psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100686 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200687 size_t data_length,
688 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100689
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100690
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100691
692/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100693 * \brief Export a key in binary format.
694 *
695 * The output of this function can be passed to psa_import_key() to
696 * create an equivalent object.
697 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100698 * If the implementation of psa_import_key() supports other formats
699 * beyond the format specified here, the output from psa_export_key()
700 * must use the representation specified here, not the original
701 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100702 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100703 * For standard key types, the output format is as follows:
704 *
705 * - For symmetric keys (including MAC keys), the format is the
706 * raw bytes of the key.
707 * - For DES, the key data consists of 8 bytes. The parity bits must be
708 * correct.
709 * - For Triple-DES, the format is the concatenation of the
710 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200711 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200712 * is the non-encrypted DER encoding of the representation defined by
713 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
714 * ```
715 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200716 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200717 * modulus INTEGER, -- n
718 * publicExponent INTEGER, -- e
719 * privateExponent INTEGER, -- d
720 * prime1 INTEGER, -- p
721 * prime2 INTEGER, -- q
722 * exponent1 INTEGER, -- d mod (p-1)
723 * exponent2 INTEGER, -- d mod (q-1)
724 * coefficient INTEGER, -- (inverse of q) mod p
725 * }
726 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200727 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200728 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100729 * a representation of the private value as a `ceiling(m/8)`-byte string
730 * where `m` is the bit size associated with the curve, i.e. the bit size
731 * of the order of the curve's coordinate field. This byte string is
732 * in little-endian order for Montgomery curves (curve types
733 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
734 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
735 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100736 * This is the content of the `privateKey` field of the `ECPrivateKey`
737 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200738 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200739 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000740 * format is the representation of the private key `x` as a big-endian byte
741 * string. The length of the byte string is the private key size in bytes
742 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200743 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
744 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100745 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200746 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
747 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100748 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200749 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200750 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200751 * \param[out] data_length On success, the number of bytes
752 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100753 *
Gilles Peskine28538492018-07-11 17:34:00 +0200754 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100755 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200756 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200757 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100758 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200759 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
760 * The size of the \p data buffer is too small. You can determine a
761 * sufficient buffer size by calling
762 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
763 * where \c type is the key type
764 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200765 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
766 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200767 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100768 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100769 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300770 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300771 * The library has not been previously initialized by psa_crypto_init().
772 * It is implementation-dependent whether a failure to initialize
773 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100774 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100775psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100776 uint8_t *data,
777 size_t data_size,
778 size_t *data_length);
779
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100780/**
781 * \brief Export a public key or the public part of a key pair in binary format.
782 *
783 * The output of this function can be passed to psa_import_key() to
784 * create an object that is equivalent to the public key.
785 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000786 * This specification supports a single format for each key type.
787 * Implementations may support other formats as long as the standard
788 * format is supported. Implementations that support other formats
789 * should ensure that the formats are clearly unambiguous so as to
790 * minimize the risk that an invalid input is accidentally interpreted
791 * according to a different format.
792 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000793 * For standard key types, the output format is as follows:
794 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
795 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
796 * ```
797 * RSAPublicKey ::= SEQUENCE {
798 * modulus INTEGER, -- n
799 * publicExponent INTEGER } -- e
800 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000801 * - For elliptic curve public keys (key types for which
802 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
803 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
804 * Let `m` be the bit size associated with the curve, i.e. the bit size of
805 * `q` for a curve over `F_q`. The representation consists of:
806 * - The byte 0x04;
807 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
808 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200809 * - For Diffie-Hellman key exchange public keys (key types for which
810 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000811 * the format is the representation of the public key `y = g^x mod p` as a
812 * big-endian byte string. The length of the byte string is the length of the
813 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100814 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200815 * Exporting a public key object or the public part of a key pair is
816 * always permitted, regardless of the key's usage flags.
817 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100818 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200819 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200820 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200821 * \param[out] data_length On success, the number of bytes
822 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100823 *
Gilles Peskine28538492018-07-11 17:34:00 +0200824 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100825 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200826 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200827 * The key is neither a public key nor a key pair.
828 * \retval #PSA_ERROR_NOT_SUPPORTED
829 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
830 * The size of the \p data buffer is too small. You can determine a
831 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200832 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200833 * where \c type is the key type
834 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200835 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
836 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200837 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100838 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100839 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300840 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300841 * The library has not been previously initialized by psa_crypto_init().
842 * It is implementation-dependent whether a failure to initialize
843 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100844 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100845psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100846 uint8_t *data,
847 size_t data_size,
848 size_t *data_length);
849
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100850
Gilles Peskine20035e32018-02-03 22:44:14 +0100851
852/**@}*/
853
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100854/** \defgroup hash Message digests
855 * @{
856 */
857
Gilles Peskine69647a42019-01-14 20:18:12 +0100858/** Calculate the hash (digest) of a message.
859 *
860 * \note To verify the hash of a message against an
861 * expected value, use psa_hash_compare() instead.
862 *
863 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
864 * such that #PSA_ALG_IS_HASH(\p alg) is true).
865 * \param[in] input Buffer containing the message to hash.
866 * \param input_length Size of the \p input buffer in bytes.
867 * \param[out] hash Buffer where the hash is to be written.
868 * \param hash_size Size of the \p hash buffer in bytes.
869 * \param[out] hash_length On success, the number of bytes
870 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100871 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100872 *
873 * \retval #PSA_SUCCESS
874 * Success.
875 * \retval #PSA_ERROR_NOT_SUPPORTED
876 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100877 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100878 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
879 * \p hash_size is too small
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. Shaw7f1863c2019-08-06 16:34:44 +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_compute(psa_algorithm_t alg,
891 const uint8_t *input,
892 size_t input_length,
893 uint8_t *hash,
894 size_t hash_size,
895 size_t *hash_length);
896
897/** Calculate the hash (digest) of a message and compare it with a
898 * reference value.
899 *
900 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
901 * such that #PSA_ALG_IS_HASH(\p alg) is true).
902 * \param[in] input Buffer containing the message to hash.
903 * \param input_length Size of the \p input buffer in bytes.
904 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100905 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100906 *
907 * \retval #PSA_SUCCESS
908 * The expected hash is identical to the actual hash of the input.
909 * \retval #PSA_ERROR_INVALID_SIGNATURE
910 * The hash of the message was calculated successfully, but it
911 * differs from the expected hash.
912 * \retval #PSA_ERROR_NOT_SUPPORTED
913 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100914 * \retval #PSA_ERROR_INVALID_ARGUMENT
915 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100916 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
917 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
918 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200919 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100920 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100921 * \retval #PSA_ERROR_BAD_STATE
922 * The library has not been previously initialized by psa_crypto_init().
923 * It is implementation-dependent whether a failure to initialize
924 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100925 */
926psa_status_t psa_hash_compare(psa_algorithm_t alg,
927 const uint8_t *input,
928 size_t input_length,
929 const uint8_t *hash,
930 const size_t hash_length);
931
Gilles Peskine308b91d2018-02-08 09:47:44 +0100932/** The type of the state data structure for multipart hash operations.
933 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000934 * Before calling any function on a hash operation object, the application must
935 * initialize it by any of the following means:
936 * - Set the structure to all-bits-zero, for example:
937 * \code
938 * psa_hash_operation_t operation;
939 * memset(&operation, 0, sizeof(operation));
940 * \endcode
941 * - Initialize the structure to logical zero values, for example:
942 * \code
943 * psa_hash_operation_t operation = {0};
944 * \endcode
945 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
946 * for example:
947 * \code
948 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
949 * \endcode
950 * - Assign the result of the function psa_hash_operation_init()
951 * to the structure, for example:
952 * \code
953 * psa_hash_operation_t operation;
954 * operation = psa_hash_operation_init();
955 * \endcode
956 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100957 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100958 * make any assumptions about the content of this structure except
959 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100960typedef struct psa_hash_operation_s psa_hash_operation_t;
961
Jaeden Amero6a25b412019-01-04 11:47:44 +0000962/** \def PSA_HASH_OPERATION_INIT
963 *
964 * This macro returns a suitable initializer for a hash operation object
965 * of type #psa_hash_operation_t.
966 */
967#ifdef __DOXYGEN_ONLY__
968/* This is an example definition for documentation purposes.
969 * Implementations should define a suitable value in `crypto_struct.h`.
970 */
971#define PSA_HASH_OPERATION_INIT {0}
972#endif
973
974/** Return an initial value for a hash operation object.
975 */
976static psa_hash_operation_t psa_hash_operation_init(void);
977
Gilles Peskinef45adda2019-01-14 18:29:18 +0100978/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100979 *
980 * The sequence of operations to calculate a hash (message digest)
981 * is as follows:
982 * -# Allocate an operation object which will be passed to all the functions
983 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000984 * -# Initialize the operation object with one of the methods described in the
985 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200986 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100987 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100988 * of the message each time. The hash that is calculated is the hash
989 * of the concatenation of these messages in order.
990 * -# To calculate the hash, call psa_hash_finish().
991 * To compare the hash with an expected value, call psa_hash_verify().
992 *
993 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000994 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100995 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200996 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100997 * eventually terminate the operation. The following events terminate an
998 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100999 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001000 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001001 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001002 * \param[in,out] operation The operation object to set up. It must have
1003 * been initialized as per the documentation for
1004 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001005 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1006 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001007 *
Gilles Peskine28538492018-07-11 17:34:00 +02001008 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001009 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001010 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +01001011 * \p alg is not a supported hash algorithm.
1012 * \retval #PSA_ERROR_INVALID_ARGUMENT
1013 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001014 * \retval #PSA_ERROR_BAD_STATE
1015 * The operation state is not valid (already set up and not
1016 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001017 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1018 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1019 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001020 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001021 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawe970d652019-08-08 14:40:04 +01001022 * \p operation is either not initialized or is in use
1023 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001024 * The library has not been previously initialized by psa_crypto_init().
1025 * It is implementation-dependent whether a failure to initialize
1026 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001027 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001028psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001029 psa_algorithm_t alg);
1030
Gilles Peskine308b91d2018-02-08 09:47:44 +01001031/** Add a message fragment to a multipart hash operation.
1032 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001033 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001034 *
1035 * If this function returns an error status, the operation becomes inactive.
1036 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001037 * \param[in,out] operation Active hash operation.
1038 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001039 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001040 *
Gilles Peskine28538492018-07-11 17:34:00 +02001041 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001042 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001043 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001044 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001045 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1046 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1047 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001048 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001049 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001050 * The operation state is not valid.
1051 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001052 * The library has not been previously initialized by psa_crypto_init().
1053 * It is implementation-dependent whether a failure to initialize
1054 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001055 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001056psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1057 const uint8_t *input,
1058 size_t input_length);
1059
Gilles Peskine308b91d2018-02-08 09:47:44 +01001060/** Finish the calculation of the hash of a message.
1061 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001062 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001063 * This function calculates the hash of the message formed by concatenating
1064 * the inputs passed to preceding calls to psa_hash_update().
1065 *
1066 * When this function returns, the operation becomes inactive.
1067 *
1068 * \warning Applications should not call this function if they expect
1069 * a specific value for the hash. Call psa_hash_verify() instead.
1070 * Beware that comparing integrity or authenticity data such as
1071 * hash values with a function such as \c memcmp is risky
1072 * because the time taken by the comparison may leak information
1073 * about the hashed data which could allow an attacker to guess
1074 * a valid hash and thereby bypass security controls.
1075 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001076 * \param[in,out] operation Active hash operation.
1077 * \param[out] hash Buffer where the hash is to be written.
1078 * \param hash_size Size of the \p hash buffer in bytes.
1079 * \param[out] hash_length On success, the number of bytes
1080 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001081 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001082 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001083 *
Gilles Peskine28538492018-07-11 17:34:00 +02001084 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001085 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001086 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001087 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001088 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001089 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001090 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001091 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001092 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1093 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1094 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001095 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001096 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001097 * The operation state is not valid.
1098 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001099 * The library has not been previously initialized by psa_crypto_init().
1100 * It is implementation-dependent whether a failure to initialize
1101 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001102 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001103psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1104 uint8_t *hash,
1105 size_t hash_size,
1106 size_t *hash_length);
1107
Gilles Peskine308b91d2018-02-08 09:47:44 +01001108/** Finish the calculation of the hash of a message and compare it with
1109 * an expected value.
1110 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001111 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001112 * This function calculates the hash of the message formed by concatenating
1113 * the inputs passed to preceding calls to psa_hash_update(). It then
1114 * compares the calculated hash with the expected hash passed as a
1115 * parameter to this function.
1116 *
1117 * When this function returns, the operation becomes inactive.
1118 *
Gilles Peskine19067982018-03-20 17:54:53 +01001119 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001120 * comparison between the actual hash and the expected hash is performed
1121 * in constant time.
1122 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001123 * \param[in,out] operation Active hash operation.
1124 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001125 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001126 *
Gilles Peskine28538492018-07-11 17:34:00 +02001127 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001128 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001129 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001130 * The hash of the message was calculated successfully, but it
1131 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001132 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001133 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001134 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1135 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1136 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001137 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001138 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001139 * The operation state is not valid.
1140 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001141 * The library has not been previously initialized by psa_crypto_init().
1142 * It is implementation-dependent whether a failure to initialize
1143 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001144 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001145psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1146 const uint8_t *hash,
1147 size_t hash_length);
1148
Gilles Peskine308b91d2018-02-08 09:47:44 +01001149/** Abort a hash operation.
1150 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001151 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001152 * \p operation structure itself. Once aborted, the operation object
1153 * can be reused for another operation by calling
1154 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001155 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001156 * You may call this function any time after the operation object has
1157 * been initialized by any of the following methods:
1158 * - A call to psa_hash_setup(), whether it succeeds or not.
1159 * - Initializing the \c struct to all-bits-zero.
1160 * - Initializing the \c struct to logical zeros, e.g.
1161 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001162 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001163 * In particular, calling psa_hash_abort() after the operation has been
1164 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1165 * psa_hash_verify() is safe and has no effect.
1166 *
1167 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001168 *
Gilles Peskine28538492018-07-11 17:34:00 +02001169 * \retval #PSA_SUCCESS
1170 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001171 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001172 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1173 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001174 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001175 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001176 * The operation state is not valid.
1177 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001178 * The library has not been previously initialized by psa_crypto_init().
1179 * It is implementation-dependent whether a failure to initialize
1180 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001181 */
1182psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001183
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001184/** Clone a hash operation.
1185 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001186 * This function copies the state of an ongoing hash operation to
1187 * a new operation object. In other words, this function is equivalent
1188 * to calling psa_hash_setup() on \p target_operation with the same
1189 * algorithm that \p source_operation was set up for, then
1190 * psa_hash_update() on \p target_operation with the same input that
1191 * that was passed to \p source_operation. After this function returns, the
1192 * two objects are independent, i.e. subsequent calls involving one of
1193 * the objects do not affect the other object.
1194 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001195 * \param[in] source_operation The active hash operation to clone.
1196 * \param[in,out] target_operation The operation object to set up.
1197 * It must be initialized but not active.
1198 *
1199 * \retval #PSA_SUCCESS
1200 * \retval #PSA_ERROR_BAD_STATE
1201 * \p source_operation is not an active hash operation.
1202 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001203 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001204 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1205 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001206 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001207 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001208 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001209 * The operation state is either not initialized or has already been setup.
1210 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw8f7cd1e2019-08-08 14:49:01 +01001211 * The operation state is either not initialized or has already been setup.
1212 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001213 * 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.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001216 */
1217psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1218 psa_hash_operation_t *target_operation);
1219
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001220/**@}*/
1221
Gilles Peskine8c9def32018-02-08 10:02:12 +01001222/** \defgroup MAC Message authentication codes
1223 * @{
1224 */
1225
Gilles Peskine69647a42019-01-14 20:18:12 +01001226/** Calculate the MAC (message authentication code) of a message.
1227 *
1228 * \note To verify the MAC of a message against an
1229 * expected value, use psa_mac_verify() instead.
1230 * Beware that comparing integrity or authenticity data such as
1231 * MAC values with a function such as \c memcmp is risky
1232 * because the time taken by the comparison may leak information
1233 * about the MAC value which could allow an attacker to guess
1234 * a valid MAC and thereby bypass security controls.
1235 *
1236 * \param handle Handle to the key to use for the operation.
1237 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001238 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001239 * \param[in] input Buffer containing the input message.
1240 * \param input_length Size of the \p input buffer in bytes.
1241 * \param[out] mac Buffer where the MAC value is to be written.
1242 * \param mac_size Size of the \p mac buffer in bytes.
1243 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001244 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001245 *
1246 * \retval #PSA_SUCCESS
1247 * Success.
1248 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001249 * \retval #PSA_ERROR_NOT_PERMITTED
1250 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001251 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001252 * \retval #PSA_ERROR_NOT_SUPPORTED
1253 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001254 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1255 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001256 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1257 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1258 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001259 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001260 * \retval #PSA_ERROR_STORAGE_FAILURE
1261 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001262 * \retval #PSA_ERROR_BAD_STATE
1263 * The library has not been previously initialized by psa_crypto_init().
1264 * It is implementation-dependent whether a failure to initialize
1265 * results in this error code.
1266 */
1267psa_status_t psa_mac_compute(psa_key_handle_t handle,
1268 psa_algorithm_t alg,
1269 const uint8_t *input,
1270 size_t input_length,
1271 uint8_t *mac,
1272 size_t mac_size,
1273 size_t *mac_length);
1274
1275/** Calculate the MAC of a message and compare it with a reference value.
1276 *
1277 * \param handle Handle to the key to use for the operation.
1278 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001279 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001280 * \param[in] input Buffer containing the input message.
1281 * \param input_length Size of the \p input buffer in bytes.
1282 * \param[out] mac Buffer containing the expected MAC value.
1283 * \param mac_length Size of the \p mac buffer in bytes.
1284 *
1285 * \retval #PSA_SUCCESS
1286 * The expected MAC is identical to the actual MAC of the input.
1287 * \retval #PSA_ERROR_INVALID_SIGNATURE
1288 * The MAC of the message was calculated successfully, but it
1289 * differs from the expected value.
1290 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001291 * \retval #PSA_ERROR_NOT_PERMITTED
1292 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001293 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001294 * \retval #PSA_ERROR_NOT_SUPPORTED
1295 * \p alg is not supported or is not a MAC algorithm.
1296 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1297 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1298 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001299 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001300 * \retval #PSA_ERROR_STORAGE_FAILURE
1301 * The key could not be retrieved from storage.
1302 * \retval #PSA_ERROR_BAD_STATE
1303 * The library has not been previously initialized by psa_crypto_init().
1304 * It is implementation-dependent whether a failure to initialize
1305 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001306 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001307psa_status_t psa_mac_verify(psa_key_handle_t handle,
1308 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001309 const uint8_t *input,
1310 size_t input_length,
1311 const uint8_t *mac,
1312 const size_t mac_length);
1313
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001314/** The type of the state data structure for multipart MAC operations.
1315 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001316 * Before calling any function on a MAC operation object, the application must
1317 * initialize it by any of the following means:
1318 * - Set the structure to all-bits-zero, for example:
1319 * \code
1320 * psa_mac_operation_t operation;
1321 * memset(&operation, 0, sizeof(operation));
1322 * \endcode
1323 * - Initialize the structure to logical zero values, for example:
1324 * \code
1325 * psa_mac_operation_t operation = {0};
1326 * \endcode
1327 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1328 * for example:
1329 * \code
1330 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1331 * \endcode
1332 * - Assign the result of the function psa_mac_operation_init()
1333 * to the structure, for example:
1334 * \code
1335 * psa_mac_operation_t operation;
1336 * operation = psa_mac_operation_init();
1337 * \endcode
1338 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001339 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001340 * make any assumptions about the content of this structure except
1341 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001342typedef struct psa_mac_operation_s psa_mac_operation_t;
1343
Jaeden Amero769ce272019-01-04 11:48:03 +00001344/** \def PSA_MAC_OPERATION_INIT
1345 *
1346 * This macro returns a suitable initializer for a MAC operation object of type
1347 * #psa_mac_operation_t.
1348 */
1349#ifdef __DOXYGEN_ONLY__
1350/* This is an example definition for documentation purposes.
1351 * Implementations should define a suitable value in `crypto_struct.h`.
1352 */
1353#define PSA_MAC_OPERATION_INIT {0}
1354#endif
1355
1356/** Return an initial value for a MAC operation object.
1357 */
1358static psa_mac_operation_t psa_mac_operation_init(void);
1359
Gilles Peskinef45adda2019-01-14 18:29:18 +01001360/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001361 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001362 * This function sets up the calculation of the MAC
1363 * (message authentication code) of a byte string.
1364 * To verify the MAC of a message against an
1365 * expected value, use psa_mac_verify_setup() instead.
1366 *
1367 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001368 * -# Allocate an operation object which will be passed to all the functions
1369 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001370 * -# Initialize the operation object with one of the methods described in the
1371 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001372 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001373 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1374 * of the message each time. The MAC that is calculated is the MAC
1375 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001376 * -# At the end of the message, call psa_mac_sign_finish() to finish
1377 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001378 *
1379 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001380 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001381 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001382 * After a successful call to psa_mac_sign_setup(), the application must
1383 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001384 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001385 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001386 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001387 * \param[in,out] operation The operation object to set up. It must have
1388 * been initialized as per the documentation for
1389 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001390 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001391 * It must remain valid until the operation
1392 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001393 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001394 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001395 *
Gilles Peskine28538492018-07-11 17:34:00 +02001396 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001397 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001398 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001399 * \retval #PSA_ERROR_NOT_PERMITTED
1400 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001401 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001402 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001403 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001404 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1405 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1406 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001407 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001408 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001409 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001410 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001411 * The operation state is not valid (already set up and not
1412 * subsequently completed).
1413 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001414 * The library has not been previously initialized by psa_crypto_init().
1415 * It is implementation-dependent whether a failure to initialize
1416 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001417 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001418psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001419 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001420 psa_algorithm_t alg);
1421
Gilles Peskinef45adda2019-01-14 18:29:18 +01001422/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001423 *
1424 * This function sets up the verification of the MAC
1425 * (message authentication code) of a byte string against an expected value.
1426 *
1427 * The sequence of operations to verify a MAC is as follows:
1428 * -# Allocate an operation object which will be passed to all the functions
1429 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001430 * -# Initialize the operation object with one of the methods described in the
1431 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001432 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001433 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1434 * of the message each time. The MAC that is calculated is the MAC
1435 * of the concatenation of these messages in order.
1436 * -# At the end of the message, call psa_mac_verify_finish() to finish
1437 * calculating the actual MAC of the message and verify it against
1438 * the expected value.
1439 *
1440 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001441 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001442 *
1443 * After a successful call to psa_mac_verify_setup(), the application must
1444 * eventually terminate the operation through one of the following methods:
1445 * - A failed call to psa_mac_update().
1446 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1447 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001448 * \param[in,out] operation The operation object to set up. It must have
1449 * been initialized as per the documentation for
1450 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001451 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001452 * It must remain valid until the operation
1453 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001454 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1455 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001456 *
Gilles Peskine28538492018-07-11 17:34:00 +02001457 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001458 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001459 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001460 * \retval #PSA_ERROR_NOT_PERMITTED
1461 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001462 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001463 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001464 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001465 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1466 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1467 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001468 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001469 * \retval #PSA_ERROR_STORAGE_FAILURE
1470 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001471 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001472 * The operation state is not valid (already set up and not
1473 * subsequently completed).
1474 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001475 * The library has not been previously initialized by psa_crypto_init().
1476 * It is implementation-dependent whether a failure to initialize
1477 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001478 */
1479psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001480 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001481 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001482
Gilles Peskinedcd14942018-07-12 00:30:52 +02001483/** Add a message fragment to a multipart MAC operation.
1484 *
1485 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1486 * before calling this function.
1487 *
1488 * If this function returns an error status, the operation becomes inactive.
1489 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001490 * \param[in,out] operation Active MAC operation.
1491 * \param[in] input Buffer containing the message fragment to add to
1492 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001493 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001494 *
1495 * \retval #PSA_SUCCESS
1496 * Success.
1497 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001498 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001499 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1500 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1501 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001502 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001503 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001504 * \retval #PSA_ERROR_BAD_STATE
1505 * The library has not been previously initialized by psa_crypto_init().
1506 * It is implementation-dependent whether a failure to initialize
1507 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001508 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001509psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1510 const uint8_t *input,
1511 size_t input_length);
1512
Gilles Peskinedcd14942018-07-12 00:30:52 +02001513/** Finish the calculation of the MAC of a message.
1514 *
1515 * The application must call psa_mac_sign_setup() before calling this function.
1516 * This function calculates the MAC of the message formed by concatenating
1517 * the inputs passed to preceding calls to psa_mac_update().
1518 *
1519 * When this function returns, the operation becomes inactive.
1520 *
1521 * \warning Applications should not call this function if they expect
1522 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1523 * Beware that comparing integrity or authenticity data such as
1524 * MAC values with a function such as \c memcmp is risky
1525 * because the time taken by the comparison may leak information
1526 * about the MAC value which could allow an attacker to guess
1527 * a valid MAC and thereby bypass security controls.
1528 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001529 * \param[in,out] operation Active MAC operation.
1530 * \param[out] mac Buffer where the MAC value is to be written.
1531 * \param mac_size Size of the \p mac buffer in bytes.
1532 * \param[out] mac_length On success, the number of bytes
1533 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001534 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001535 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001536 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001537 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001538 *
1539 * \retval #PSA_SUCCESS
1540 * Success.
1541 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001542 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001543 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001544 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001545 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1546 * \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. Shaw26322362019-08-13 11:43:40 +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_sign_finish(psa_mac_operation_t *operation,
1557 uint8_t *mac,
1558 size_t mac_size,
1559 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001560
Gilles Peskinedcd14942018-07-12 00:30:52 +02001561/** Finish the calculation of the MAC of a message and compare it with
1562 * an expected value.
1563 *
1564 * The application must call psa_mac_verify_setup() before calling this function.
1565 * This function calculates the MAC of the message formed by concatenating
1566 * the inputs passed to preceding calls to psa_mac_update(). It then
1567 * compares the calculated MAC with the expected MAC passed as a
1568 * parameter to this function.
1569 *
1570 * When this function returns, the operation becomes inactive.
1571 *
1572 * \note Implementations shall make the best effort to ensure that the
1573 * comparison between the actual MAC and the expected MAC is performed
1574 * in constant time.
1575 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001576 * \param[in,out] operation Active MAC operation.
1577 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001578 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001579 *
1580 * \retval #PSA_SUCCESS
1581 * The expected MAC is identical to the actual MAC of the message.
1582 * \retval #PSA_ERROR_INVALID_SIGNATURE
1583 * The MAC of the message was calculated successfully, but it
1584 * differs from the expected MAC.
1585 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001586 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001587 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1588 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1589 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001590 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001591 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001592 * \retval #PSA_ERROR_BAD_STATE
1593 * The library has not been previously initialized by psa_crypto_init().
1594 * It is implementation-dependent whether a failure to initialize
1595 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001596 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001597psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1598 const uint8_t *mac,
1599 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001600
Gilles Peskinedcd14942018-07-12 00:30:52 +02001601/** Abort a MAC operation.
1602 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001603 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001604 * \p operation structure itself. Once aborted, the operation object
1605 * can be reused for another operation by calling
1606 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001607 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001608 * You may call this function any time after the operation object has
1609 * been initialized by any of the following methods:
1610 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1611 * it succeeds or not.
1612 * - Initializing the \c struct to all-bits-zero.
1613 * - Initializing the \c struct to logical zeros, e.g.
1614 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001615 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001616 * In particular, calling psa_mac_abort() after the operation has been
1617 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1618 * psa_mac_verify_finish() is safe and has no effect.
1619 *
1620 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001621 *
1622 * \retval #PSA_SUCCESS
1623 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001624 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001625 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1626 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001627 * \retval #PSA_ERROR_CORRUPTION_DETECTED
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 Peskinedcd14942018-07-12 00:30:52 +02001632 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001633psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1634
1635/**@}*/
1636
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001637/** \defgroup cipher Symmetric ciphers
1638 * @{
1639 */
1640
Gilles Peskine69647a42019-01-14 20:18:12 +01001641/** Encrypt a message using a symmetric cipher.
1642 *
1643 * This function encrypts a message with a random IV (initialization
1644 * vector).
1645 *
1646 * \param handle Handle to the key to use for the operation.
1647 * It must remain valid until the operation
1648 * terminates.
1649 * \param alg The cipher algorithm to compute
1650 * (\c PSA_ALG_XXX value such that
1651 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1652 * \param[in] input Buffer containing the message to encrypt.
1653 * \param input_length Size of the \p input buffer in bytes.
1654 * \param[out] output Buffer where the output is to be written.
1655 * The output contains the IV followed by
1656 * the ciphertext proper.
1657 * \param output_size Size of the \p output buffer in bytes.
1658 * \param[out] output_length On success, the number of bytes
1659 * that make up the output.
1660 *
1661 * \retval #PSA_SUCCESS
1662 * Success.
1663 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001664 * \retval #PSA_ERROR_NOT_PERMITTED
1665 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001666 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001667 * \retval #PSA_ERROR_NOT_SUPPORTED
1668 * \p alg is not supported or is not a cipher algorithm.
1669 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1670 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1671 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1672 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001673 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001674 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001675 * \retval #PSA_ERROR_BAD_STATE
1676 * The library has not been previously initialized by psa_crypto_init().
1677 * It is implementation-dependent whether a failure to initialize
1678 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001679 */
1680psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1681 psa_algorithm_t alg,
1682 const uint8_t *input,
1683 size_t input_length,
1684 uint8_t *output,
1685 size_t output_size,
1686 size_t *output_length);
1687
1688/** Decrypt a message using a symmetric cipher.
1689 *
1690 * This function decrypts a message encrypted with a symmetric cipher.
1691 *
1692 * \param handle Handle to the key to use for the operation.
1693 * It must remain valid until the operation
1694 * terminates.
1695 * \param alg The cipher algorithm to compute
1696 * (\c PSA_ALG_XXX value such that
1697 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1698 * \param[in] input Buffer containing the message to decrypt.
1699 * This consists of the IV followed by the
1700 * ciphertext proper.
1701 * \param input_length Size of the \p input buffer in bytes.
1702 * \param[out] output Buffer where the plaintext is to be written.
1703 * \param output_size Size of the \p output buffer in bytes.
1704 * \param[out] output_length On success, the number of bytes
1705 * that make up the output.
1706 *
1707 * \retval #PSA_SUCCESS
1708 * Success.
1709 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001710 * \retval #PSA_ERROR_NOT_PERMITTED
1711 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001712 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001713 * \retval #PSA_ERROR_NOT_SUPPORTED
1714 * \p alg is not supported or is not a cipher algorithm.
1715 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1716 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1717 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1718 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001719 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001720 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001721 * \retval #PSA_ERROR_BAD_STATE
1722 * The library has not been previously initialized by psa_crypto_init().
1723 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001724 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001725 */
1726psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1727 psa_algorithm_t alg,
1728 const uint8_t *input,
1729 size_t input_length,
1730 uint8_t *output,
1731 size_t output_size,
1732 size_t *output_length);
1733
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001734/** The type of the state data structure for multipart cipher operations.
1735 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001736 * Before calling any function on a cipher operation object, the application
1737 * must initialize it by any of the following means:
1738 * - Set the structure to all-bits-zero, for example:
1739 * \code
1740 * psa_cipher_operation_t operation;
1741 * memset(&operation, 0, sizeof(operation));
1742 * \endcode
1743 * - Initialize the structure to logical zero values, for example:
1744 * \code
1745 * psa_cipher_operation_t operation = {0};
1746 * \endcode
1747 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1748 * for example:
1749 * \code
1750 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1751 * \endcode
1752 * - Assign the result of the function psa_cipher_operation_init()
1753 * to the structure, for example:
1754 * \code
1755 * psa_cipher_operation_t operation;
1756 * operation = psa_cipher_operation_init();
1757 * \endcode
1758 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001759 * This is an implementation-defined \c struct. Applications should not
1760 * make any assumptions about the content of this structure except
1761 * as directed by the documentation of a specific implementation. */
1762typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1763
Jaeden Amero5bae2272019-01-04 11:48:27 +00001764/** \def PSA_CIPHER_OPERATION_INIT
1765 *
1766 * This macro returns a suitable initializer for a cipher operation object of
1767 * type #psa_cipher_operation_t.
1768 */
1769#ifdef __DOXYGEN_ONLY__
1770/* This is an example definition for documentation purposes.
1771 * Implementations should define a suitable value in `crypto_struct.h`.
1772 */
1773#define PSA_CIPHER_OPERATION_INIT {0}
1774#endif
1775
1776/** Return an initial value for a cipher operation object.
1777 */
1778static psa_cipher_operation_t psa_cipher_operation_init(void);
1779
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001780/** Set the key for a multipart symmetric encryption operation.
1781 *
1782 * The sequence of operations to encrypt a message with a symmetric cipher
1783 * is as follows:
1784 * -# Allocate an operation object which will be passed to all the functions
1785 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001786 * -# Initialize the operation object with one of the methods described in the
1787 * documentation for #psa_cipher_operation_t, e.g.
1788 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001789 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001790 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001791 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001792 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001793 * requires a specific IV value.
1794 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1795 * of the message each time.
1796 * -# Call psa_cipher_finish().
1797 *
1798 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001799 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001800 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001801 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001802 * eventually terminate the operation. The following events terminate an
1803 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001804 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001805 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001806 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001807 * \param[in,out] operation The operation object to set up. It must have
1808 * been initialized as per the documentation for
1809 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001810 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001811 * It must remain valid until the operation
1812 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001813 * \param alg The cipher algorithm to compute
1814 * (\c PSA_ALG_XXX value such that
1815 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001816 *
Gilles Peskine28538492018-07-11 17:34:00 +02001817 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001818 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001819 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001820 * \retval #PSA_ERROR_NOT_PERMITTED
1821 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001822 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001823 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001824 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001825 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1826 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1827 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001828 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001829 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001830 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001831 * The operation state is not valid (already set up and not
1832 * subsequently completed).
1833 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001834 * The library has not been previously initialized by psa_crypto_init().
1835 * It is implementation-dependent whether a failure to initialize
1836 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001837 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001838psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001839 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001840 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001841
1842/** Set the key for a multipart symmetric decryption operation.
1843 *
1844 * The sequence of operations to decrypt a message with a symmetric cipher
1845 * is as follows:
1846 * -# Allocate an operation object which will be passed to all the functions
1847 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001848 * -# Initialize the operation object with one of the methods described in the
1849 * documentation for #psa_cipher_operation_t, e.g.
1850 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001851 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001852 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001853 * decryption. If the IV is prepended to the ciphertext, you can call
1854 * psa_cipher_update() on a buffer containing the IV followed by the
1855 * beginning of the message.
1856 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1857 * of the message each time.
1858 * -# Call psa_cipher_finish().
1859 *
1860 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001861 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001862 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001863 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001864 * eventually terminate the operation. The following events terminate an
1865 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001866 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001867 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001868 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001869 * \param[in,out] operation The operation object to set up. It must have
1870 * been initialized as per the documentation for
1871 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001872 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001873 * It must remain valid until the operation
1874 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001875 * \param alg The cipher algorithm to compute
1876 * (\c PSA_ALG_XXX value such that
1877 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001878 *
Gilles Peskine28538492018-07-11 17:34:00 +02001879 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001880 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001881 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001882 * \retval #PSA_ERROR_NOT_PERMITTED
1883 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001884 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001885 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001886 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001887 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1888 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1889 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001890 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001891 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001892 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001893 * The operation state is not valid (already set up and not
1894 * subsequently completed).
1895 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001896 * The library has not been previously initialized by psa_crypto_init().
1897 * It is implementation-dependent whether a failure to initialize
1898 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001899 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001900psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001901 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001902 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001903
Gilles Peskinedcd14942018-07-12 00:30:52 +02001904/** Generate an IV for a symmetric encryption operation.
1905 *
1906 * This function generates a random IV (initialization vector), nonce
1907 * or initial counter value for the encryption operation as appropriate
1908 * for the chosen algorithm, key type and key size.
1909 *
1910 * The application must call psa_cipher_encrypt_setup() before
1911 * calling this function.
1912 *
1913 * If this function returns an error status, the operation becomes inactive.
1914 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001915 * \param[in,out] operation Active cipher operation.
1916 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001917 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001918 * \param[out] iv_length On success, the number of bytes of the
1919 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001920 *
1921 * \retval #PSA_SUCCESS
1922 * Success.
1923 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001924 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001925 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001926 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001927 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1928 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1929 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001930 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001931 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001932 * \retval #PSA_ERROR_BAD_STATE
1933 * The library has not been previously initialized by psa_crypto_init().
1934 * It is implementation-dependent whether a failure to initialize
1935 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001936 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001937psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001938 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001939 size_t iv_size,
1940 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001941
Gilles Peskinedcd14942018-07-12 00:30:52 +02001942/** Set the IV for a symmetric encryption or decryption operation.
1943 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001944 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001945 * or initial counter value for the encryption or decryption operation.
1946 *
1947 * The application must call psa_cipher_encrypt_setup() before
1948 * calling this function.
1949 *
1950 * If this function returns an error status, the operation becomes inactive.
1951 *
1952 * \note When encrypting, applications should use psa_cipher_generate_iv()
1953 * instead of this function, unless implementing a protocol that requires
1954 * a non-random IV.
1955 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001956 * \param[in,out] operation Active cipher operation.
1957 * \param[in] iv Buffer containing the IV to use.
1958 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001959 *
1960 * \retval #PSA_SUCCESS
1961 * Success.
1962 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001963 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001964 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001965 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001966 * or the chosen algorithm does not use an IV.
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. Shaw56b32b12019-08-13 11:43:40 +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 Peskinefe119512018-07-08 21:39:34 +02001977psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001978 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001979 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001980
Gilles Peskinedcd14942018-07-12 00:30:52 +02001981/** Encrypt or decrypt a message fragment in an active cipher operation.
1982 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001983 * Before calling this function, you must:
1984 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1985 * The choice of setup function determines whether this function
1986 * encrypts or decrypts its input.
1987 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1988 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001989 *
1990 * If this function returns an error status, the operation becomes inactive.
1991 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001992 * \param[in,out] operation Active cipher operation.
1993 * \param[in] input Buffer containing the message fragment to
1994 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001995 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001996 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001997 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001998 * \param[out] output_length On success, the number of bytes
1999 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002000 *
2001 * \retval #PSA_SUCCESS
2002 * Success.
2003 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002004 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02002005 * not set, or already completed).
2006 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2007 * The size of the \p output buffer is too small.
2008 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2009 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2010 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002011 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002012 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002013 * \retval #PSA_ERROR_BAD_STATE
2014 * The library has not been previously initialized by psa_crypto_init().
2015 * It is implementation-dependent whether a failure to initialize
2016 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002017 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002018psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2019 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002020 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00002021 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02002022 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002023 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002024
Gilles Peskinedcd14942018-07-12 00:30:52 +02002025/** Finish encrypting or decrypting a message in a cipher operation.
2026 *
2027 * The application must call psa_cipher_encrypt_setup() or
2028 * psa_cipher_decrypt_setup() before calling this function. The choice
2029 * of setup function determines whether this function encrypts or
2030 * decrypts its input.
2031 *
2032 * This function finishes the encryption or decryption of the message
2033 * formed by concatenating the inputs passed to preceding calls to
2034 * psa_cipher_update().
2035 *
2036 * When this function returns, the operation becomes inactive.
2037 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002038 * \param[in,out] operation Active cipher operation.
2039 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002040 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002041 * \param[out] output_length On success, the number of bytes
2042 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002043 *
2044 * \retval #PSA_SUCCESS
2045 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02002046 * \retval #PSA_ERROR_INVALID_ARGUMENT
2047 * The total input size passed to this operation is not valid for
2048 * this particular algorithm. For example, the algorithm is a based
2049 * on block cipher and requires a whole number of blocks, but the
2050 * total input size is not a multiple of the block size.
2051 * \retval #PSA_ERROR_INVALID_PADDING
2052 * This is a decryption operation for an algorithm that includes
2053 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002054 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002055 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02002056 * not set, or already completed).
2057 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2058 * The size of the \p output buffer is too small.
2059 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2060 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2061 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002062 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002063 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002064 * \retval #PSA_ERROR_BAD_STATE
2065 * The library has not been previously initialized by psa_crypto_init().
2066 * It is implementation-dependent whether a failure to initialize
2067 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002068 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002069psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002070 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002071 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002072 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002073
Gilles Peskinedcd14942018-07-12 00:30:52 +02002074/** Abort a cipher operation.
2075 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002076 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002077 * \p operation structure itself. Once aborted, the operation object
2078 * can be reused for another operation by calling
2079 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002080 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002081 * You may call this function any time after the operation object has
2082 * been initialized by any of the following methods:
2083 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2084 * whether it succeeds or not.
2085 * - Initializing the \c struct to all-bits-zero.
2086 * - Initializing the \c struct to logical zeros, e.g.
2087 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002088 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002089 * In particular, calling psa_cipher_abort() after the operation has been
2090 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2091 * is safe and has no effect.
2092 *
2093 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002094 *
2095 * \retval #PSA_SUCCESS
2096 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002097 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002098 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2099 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002100 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002101 * \retval #PSA_ERROR_BAD_STATE
2102 * The library has not been previously initialized by psa_crypto_init().
2103 * It is implementation-dependent whether a failure to initialize
2104 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002105 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002106psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2107
2108/**@}*/
2109
Gilles Peskine3b555712018-03-03 21:27:57 +01002110/** \defgroup aead Authenticated encryption with associated data (AEAD)
2111 * @{
2112 */
2113
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002114/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002115 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002116 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002117 * \param alg The AEAD algorithm to compute
2118 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002119 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002120 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002121 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002122 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002123 * but not encrypted.
2124 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002125 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002126 * encrypted.
2127 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002128 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002129 * encrypted data. The additional data is not
2130 * part of this output. For algorithms where the
2131 * encrypted data and the authentication tag
2132 * are defined as separate outputs, the
2133 * authentication tag is appended to the
2134 * encrypted data.
2135 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2136 * This must be at least
2137 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2138 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002139 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002140 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002141 *
Gilles Peskine28538492018-07-11 17:34:00 +02002142 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002143 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002144 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002145 * \retval #PSA_ERROR_NOT_PERMITTED
2146 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002147 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002148 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002149 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002150 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002151 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2152 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002153 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2154 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002155 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002156 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002157 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002158 * The library has not been previously initialized by psa_crypto_init().
2159 * It is implementation-dependent whether a failure to initialize
2160 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002161 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002162psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002163 psa_algorithm_t alg,
2164 const uint8_t *nonce,
2165 size_t nonce_length,
2166 const uint8_t *additional_data,
2167 size_t additional_data_length,
2168 const uint8_t *plaintext,
2169 size_t plaintext_length,
2170 uint8_t *ciphertext,
2171 size_t ciphertext_size,
2172 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002173
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002174/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002175 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002176 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002177 * \param alg The AEAD algorithm to compute
2178 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002179 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002180 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002181 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002182 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002183 * but not encrypted.
2184 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002185 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002186 * encrypted. For algorithms where the
2187 * encrypted data and the authentication tag
2188 * are defined as separate inputs, the buffer
2189 * must contain the encrypted data followed
2190 * by the authentication tag.
2191 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002192 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002193 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2194 * This must be at least
2195 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2196 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002197 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002198 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002199 *
Gilles Peskine28538492018-07-11 17:34:00 +02002200 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002201 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002202 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002203 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002204 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002205 * \retval #PSA_ERROR_NOT_PERMITTED
2206 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002207 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002208 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002209 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002210 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002211 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2212 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002213 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2214 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002215 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002216 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002217 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002218 * The library has not been previously initialized by psa_crypto_init().
2219 * It is implementation-dependent whether a failure to initialize
2220 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002221 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002222psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002223 psa_algorithm_t alg,
2224 const uint8_t *nonce,
2225 size_t nonce_length,
2226 const uint8_t *additional_data,
2227 size_t additional_data_length,
2228 const uint8_t *ciphertext,
2229 size_t ciphertext_length,
2230 uint8_t *plaintext,
2231 size_t plaintext_size,
2232 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002233
Gilles Peskine30a9e412019-01-14 18:36:12 +01002234/** The type of the state data structure for multipart AEAD operations.
2235 *
2236 * Before calling any function on an AEAD operation object, the application
2237 * must initialize it by any of the following means:
2238 * - Set the structure to all-bits-zero, for example:
2239 * \code
2240 * psa_aead_operation_t operation;
2241 * memset(&operation, 0, sizeof(operation));
2242 * \endcode
2243 * - Initialize the structure to logical zero values, for example:
2244 * \code
2245 * psa_aead_operation_t operation = {0};
2246 * \endcode
2247 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2248 * for example:
2249 * \code
2250 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2251 * \endcode
2252 * - Assign the result of the function psa_aead_operation_init()
2253 * to the structure, for example:
2254 * \code
2255 * psa_aead_operation_t operation;
2256 * operation = psa_aead_operation_init();
2257 * \endcode
2258 *
2259 * This is an implementation-defined \c struct. Applications should not
2260 * make any assumptions about the content of this structure except
2261 * as directed by the documentation of a specific implementation. */
2262typedef struct psa_aead_operation_s psa_aead_operation_t;
2263
2264/** \def PSA_AEAD_OPERATION_INIT
2265 *
2266 * This macro returns a suitable initializer for an AEAD operation object of
2267 * type #psa_aead_operation_t.
2268 */
2269#ifdef __DOXYGEN_ONLY__
2270/* This is an example definition for documentation purposes.
2271 * Implementations should define a suitable value in `crypto_struct.h`.
2272 */
2273#define PSA_AEAD_OPERATION_INIT {0}
2274#endif
2275
2276/** Return an initial value for an AEAD operation object.
2277 */
2278static psa_aead_operation_t psa_aead_operation_init(void);
2279
2280/** Set the key for a multipart authenticated encryption operation.
2281 *
2282 * The sequence of operations to encrypt a message with authentication
2283 * is as follows:
2284 * -# Allocate an operation object which will be passed to all the functions
2285 * listed here.
2286 * -# Initialize the operation object with one of the methods described in the
2287 * documentation for #psa_aead_operation_t, e.g.
2288 * PSA_AEAD_OPERATION_INIT.
2289 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002290 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2291 * inputs to the subsequent calls to psa_aead_update_ad() and
2292 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2293 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002294 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2295 * generate or set the nonce. You should use
2296 * psa_aead_generate_nonce() unless the protocol you are implementing
2297 * requires a specific nonce value.
2298 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2299 * of the non-encrypted additional authenticated data each time.
2300 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002301 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002302 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002303 *
2304 * The application may call psa_aead_abort() at any time after the operation
2305 * has been initialized.
2306 *
2307 * After a successful call to psa_aead_encrypt_setup(), the application must
2308 * eventually terminate the operation. The following events terminate an
2309 * operation:
2310 * - A failed call to any of the \c psa_aead_xxx functions.
2311 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2312 *
2313 * \param[in,out] operation The operation object to set up. It must have
2314 * been initialized as per the documentation for
2315 * #psa_aead_operation_t and not yet in use.
2316 * \param handle Handle to the key to use for the operation.
2317 * It must remain valid until the operation
2318 * terminates.
2319 * \param alg The AEAD algorithm to compute
2320 * (\c PSA_ALG_XXX value such that
2321 * #PSA_ALG_IS_AEAD(\p alg) is true).
2322 *
2323 * \retval #PSA_SUCCESS
2324 * Success.
2325 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002326 * \retval #PSA_ERROR_NOT_PERMITTED
2327 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002328 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002329 * \retval #PSA_ERROR_NOT_SUPPORTED
2330 * \p alg is not supported or is not an AEAD algorithm.
2331 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2332 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2333 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002334 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002335 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002336 * \retval #PSA_ERROR_BAD_STATE
2337 * The library has not been previously initialized by psa_crypto_init().
2338 * It is implementation-dependent whether a failure to initialize
2339 * results in this error code.
2340 */
2341psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2342 psa_key_handle_t handle,
2343 psa_algorithm_t alg);
2344
2345/** Set the key for a multipart authenticated decryption operation.
2346 *
2347 * The sequence of operations to decrypt a message with authentication
2348 * is as follows:
2349 * -# Allocate an operation object which will be passed to all the functions
2350 * listed here.
2351 * -# Initialize the operation object with one of the methods described in the
2352 * documentation for #psa_aead_operation_t, e.g.
2353 * PSA_AEAD_OPERATION_INIT.
2354 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002355 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2356 * inputs to the subsequent calls to psa_aead_update_ad() and
2357 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2358 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002359 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2360 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2361 * of the non-encrypted additional authenticated data each time.
2362 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002363 * of the ciphertext to decrypt each time.
2364 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002365 *
2366 * The application may call psa_aead_abort() at any time after the operation
2367 * has been initialized.
2368 *
2369 * After a successful call to psa_aead_decrypt_setup(), the application must
2370 * eventually terminate the operation. The following events terminate an
2371 * operation:
2372 * - A failed call to any of the \c psa_aead_xxx functions.
2373 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2374 *
2375 * \param[in,out] operation The operation object to set up. It must have
2376 * been initialized as per the documentation for
2377 * #psa_aead_operation_t and not yet in use.
2378 * \param handle Handle to the key to use for the operation.
2379 * It must remain valid until the operation
2380 * terminates.
2381 * \param alg The AEAD algorithm to compute
2382 * (\c PSA_ALG_XXX value such that
2383 * #PSA_ALG_IS_AEAD(\p alg) is true).
2384 *
2385 * \retval #PSA_SUCCESS
2386 * Success.
2387 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002388 * \retval #PSA_ERROR_NOT_PERMITTED
2389 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002390 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002391 * \retval #PSA_ERROR_NOT_SUPPORTED
2392 * \p alg is not supported or is not an AEAD algorithm.
2393 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2394 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2395 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002396 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002397 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002398 * \retval #PSA_ERROR_BAD_STATE
2399 * The library has not been previously initialized by psa_crypto_init().
2400 * It is implementation-dependent whether a failure to initialize
2401 * results in this error code.
2402 */
2403psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2404 psa_key_handle_t handle,
2405 psa_algorithm_t alg);
2406
2407/** Generate a random nonce for an authenticated encryption operation.
2408 *
2409 * This function generates a random nonce for the authenticated encryption
2410 * operation with an appropriate size for the chosen algorithm, key type
2411 * and key size.
2412 *
2413 * The application must call psa_aead_encrypt_setup() before
2414 * calling this function.
2415 *
2416 * If this function returns an error status, the operation becomes inactive.
2417 *
2418 * \param[in,out] operation Active AEAD operation.
2419 * \param[out] nonce Buffer where the generated nonce is to be
2420 * written.
2421 * \param nonce_size Size of the \p nonce buffer in bytes.
2422 * \param[out] nonce_length On success, the number of bytes of the
2423 * generated nonce.
2424 *
2425 * \retval #PSA_SUCCESS
2426 * Success.
2427 * \retval #PSA_ERROR_BAD_STATE
2428 * The operation state is not valid (not set up, or nonce already set).
2429 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2430 * The size of the \p nonce buffer is too small.
2431 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2432 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2433 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002434 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002435 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002436 * \retval #PSA_ERROR_BAD_STATE
2437 * The library has not been previously initialized by psa_crypto_init().
2438 * It is implementation-dependent whether a failure to initialize
2439 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002440 */
2441psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002442 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002443 size_t nonce_size,
2444 size_t *nonce_length);
2445
2446/** Set the nonce for an authenticated encryption or decryption operation.
2447 *
2448 * This function sets the nonce for the authenticated
2449 * encryption or decryption operation.
2450 *
2451 * The application must call psa_aead_encrypt_setup() before
2452 * calling this function.
2453 *
2454 * If this function returns an error status, the operation becomes inactive.
2455 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002456 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002457 * instead of this function, unless implementing a protocol that requires
2458 * a non-random IV.
2459 *
2460 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002461 * \param[in] nonce Buffer containing the nonce to use.
2462 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002463 *
2464 * \retval #PSA_SUCCESS
2465 * Success.
2466 * \retval #PSA_ERROR_BAD_STATE
2467 * The operation state is not valid (not set up, or nonce already set).
2468 * \retval #PSA_ERROR_INVALID_ARGUMENT
2469 * The size of \p nonce is not acceptable for the chosen algorithm.
2470 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2471 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2472 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002473 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002474 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002475 * \retval #PSA_ERROR_BAD_STATE
2476 * The library has not been previously initialized by psa_crypto_init().
2477 * It is implementation-dependent whether a failure to initialize
2478 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002479 */
2480psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002481 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002482 size_t nonce_length);
2483
Gilles Peskinebc59c852019-01-17 15:26:08 +01002484/** Declare the lengths of the message and additional data for AEAD.
2485 *
2486 * The application must call this function before calling
2487 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2488 * the operation requires it. If the algorithm does not require it,
2489 * calling this function is optional, but if this function is called
2490 * then the implementation must enforce the lengths.
2491 *
2492 * You may call this function before or after setting the nonce with
2493 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2494 *
2495 * - For #PSA_ALG_CCM, calling this function is required.
2496 * - For the other AEAD algorithms defined in this specification, calling
2497 * this function is not required.
2498 * - For vendor-defined algorithm, refer to the vendor documentation.
2499 *
2500 * \param[in,out] operation Active AEAD operation.
2501 * \param ad_length Size of the non-encrypted additional
2502 * authenticated data in bytes.
2503 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2504 *
2505 * \retval #PSA_SUCCESS
2506 * Success.
2507 * \retval #PSA_ERROR_BAD_STATE
2508 * The operation state is not valid (not set up, already completed,
2509 * or psa_aead_update_ad() or psa_aead_update() already called).
2510 * \retval #PSA_ERROR_INVALID_ARGUMENT
2511 * At least one of the lengths is not acceptable for the chosen
2512 * algorithm.
2513 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2514 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2515 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002516 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002517 * \retval #PSA_ERROR_BAD_STATE
2518 * The library has not been previously initialized by psa_crypto_init().
2519 * It is implementation-dependent whether a failure to initialize
2520 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002521 */
2522psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2523 size_t ad_length,
2524 size_t plaintext_length);
2525
Gilles Peskine30a9e412019-01-14 18:36:12 +01002526/** Pass additional data to an active AEAD operation.
2527 *
2528 * Additional data is authenticated, but not encrypted.
2529 *
2530 * You may call this function multiple times to pass successive fragments
2531 * of the additional data. You may not call this function after passing
2532 * data to encrypt or decrypt with psa_aead_update().
2533 *
2534 * Before calling this function, you must:
2535 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2536 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2537 *
2538 * If this function returns an error status, the operation becomes inactive.
2539 *
2540 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2541 * there is no guarantee that the input is valid. Therefore, until
2542 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2543 * treat the input as untrusted and prepare to undo any action that
2544 * depends on the input if psa_aead_verify() returns an error status.
2545 *
2546 * \param[in,out] operation Active AEAD operation.
2547 * \param[in] input Buffer containing the fragment of
2548 * additional data.
2549 * \param input_length Size of the \p input buffer in bytes.
2550 *
2551 * \retval #PSA_SUCCESS
2552 * Success.
2553 * \retval #PSA_ERROR_BAD_STATE
2554 * The operation state is not valid (not set up, nonce not set,
2555 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002556 * \retval #PSA_ERROR_INVALID_ARGUMENT
2557 * The total input length overflows the additional data length that
2558 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002559 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2560 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2561 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002562 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002563 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002564 * \retval #PSA_ERROR_BAD_STATE
2565 * The library has not been previously initialized by psa_crypto_init().
2566 * It is implementation-dependent whether a failure to initialize
2567 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002568 */
2569psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2570 const uint8_t *input,
2571 size_t input_length);
2572
2573/** Encrypt or decrypt a message fragment in an active AEAD operation.
2574 *
2575 * Before calling this function, you must:
2576 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2577 * The choice of setup function determines whether this function
2578 * encrypts or decrypts its input.
2579 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2580 * 3. Call psa_aead_update_ad() to pass all the additional data.
2581 *
2582 * If this function returns an error status, the operation becomes inactive.
2583 *
2584 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2585 * there is no guarantee that the input is valid. Therefore, until
2586 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2587 * - Do not use the output in any way other than storing it in a
2588 * confidential location. If you take any action that depends
2589 * on the tentative decrypted data, this action will need to be
2590 * undone if the input turns out not to be valid. Furthermore,
2591 * if an adversary can observe that this action took place
2592 * (for example through timing), they may be able to use this
2593 * fact as an oracle to decrypt any message encrypted with the
2594 * same key.
2595 * - In particular, do not copy the output anywhere but to a
2596 * memory or storage space that you have exclusive access to.
2597 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002598 * This function does not require the input to be aligned to any
2599 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002600 * a whole block at a time, it must consume all the input provided, but
2601 * it may delay the end of the corresponding output until a subsequent
2602 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2603 * provides sufficient input. The amount of data that can be delayed
2604 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002605 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002606 * \param[in,out] operation Active AEAD operation.
2607 * \param[in] input Buffer containing the message fragment to
2608 * encrypt or decrypt.
2609 * \param input_length Size of the \p input buffer in bytes.
2610 * \param[out] output Buffer where the output is to be written.
2611 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002612 * This must be at least
2613 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2614 * \p input_length) where \c alg is the
2615 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002616 * \param[out] output_length On success, the number of bytes
2617 * that make up the returned output.
2618 *
2619 * \retval #PSA_SUCCESS
2620 * Success.
2621 * \retval #PSA_ERROR_BAD_STATE
2622 * The operation state is not valid (not set up, nonce not set
2623 * or already completed).
2624 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2625 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002626 * You can determine a sufficient buffer size by calling
2627 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2628 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002629 * \retval #PSA_ERROR_INVALID_ARGUMENT
2630 * The total length of input to psa_aead_update_ad() so far is
2631 * less than the additional data length that was previously
2632 * specified with psa_aead_set_lengths().
2633 * \retval #PSA_ERROR_INVALID_ARGUMENT
2634 * The total input length overflows the plaintext length that
2635 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002636 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2637 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2638 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002639 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002640 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002641 * \retval #PSA_ERROR_BAD_STATE
2642 * The library has not been previously initialized by psa_crypto_init().
2643 * It is implementation-dependent whether a failure to initialize
2644 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002645 */
2646psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2647 const uint8_t *input,
2648 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002649 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002650 size_t output_size,
2651 size_t *output_length);
2652
2653/** Finish encrypting a message in an AEAD operation.
2654 *
2655 * The operation must have been set up with psa_aead_encrypt_setup().
2656 *
2657 * This function finishes the authentication of the additional data
2658 * formed by concatenating the inputs passed to preceding calls to
2659 * psa_aead_update_ad() with the plaintext formed by concatenating the
2660 * inputs passed to preceding calls to psa_aead_update().
2661 *
2662 * This function has two output buffers:
2663 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002664 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002665 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002666 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002667 * that the operation performs.
2668 *
2669 * When this function returns, the operation becomes inactive.
2670 *
2671 * \param[in,out] operation Active AEAD operation.
2672 * \param[out] ciphertext Buffer where the last part of the ciphertext
2673 * is to be written.
2674 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002675 * This must be at least
2676 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2677 * \c alg is the algorithm that is being
2678 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002679 * \param[out] ciphertext_length On success, the number of bytes of
2680 * returned ciphertext.
2681 * \param[out] tag Buffer where the authentication tag is
2682 * to be written.
2683 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002684 * This must be at least
2685 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2686 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002687 * \param[out] tag_length On success, the number of bytes
2688 * that make up the returned tag.
2689 *
2690 * \retval #PSA_SUCCESS
2691 * Success.
2692 * \retval #PSA_ERROR_BAD_STATE
2693 * The operation state is not valid (not set up, nonce not set,
2694 * decryption, or already completed).
2695 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002696 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002697 * You can determine a sufficient buffer size for \p ciphertext by
2698 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2699 * where \c alg is the algorithm that is being calculated.
2700 * You can determine a sufficient buffer size for \p tag by
2701 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002702 * \retval #PSA_ERROR_INVALID_ARGUMENT
2703 * The total length of input to psa_aead_update_ad() so far is
2704 * less than the additional data length that was previously
2705 * specified with psa_aead_set_lengths().
2706 * \retval #PSA_ERROR_INVALID_ARGUMENT
2707 * The total length of input to psa_aead_update() so far is
2708 * less than the plaintext length that was previously
2709 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002710 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2711 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2712 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002713 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002714 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002715 * \retval #PSA_ERROR_BAD_STATE
2716 * The library has not been previously initialized by psa_crypto_init().
2717 * It is implementation-dependent whether a failure to initialize
2718 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002719 */
2720psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002721 uint8_t *ciphertext,
2722 size_t ciphertext_size,
2723 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002724 uint8_t *tag,
2725 size_t tag_size,
2726 size_t *tag_length);
2727
2728/** Finish authenticating and decrypting a message in an AEAD operation.
2729 *
2730 * The operation must have been set up with psa_aead_decrypt_setup().
2731 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002732 * This function finishes the authenticated decryption of the message
2733 * components:
2734 *
2735 * - The additional data consisting of the concatenation of the inputs
2736 * passed to preceding calls to psa_aead_update_ad().
2737 * - The ciphertext consisting of the concatenation of the inputs passed to
2738 * preceding calls to psa_aead_update().
2739 * - The tag passed to this function call.
2740 *
2741 * If the authentication tag is correct, this function outputs any remaining
2742 * plaintext and reports success. If the authentication tag is not correct,
2743 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002744 *
2745 * When this function returns, the operation becomes inactive.
2746 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002747 * \note Implementations shall make the best effort to ensure that the
2748 * comparison between the actual tag and the expected tag is performed
2749 * in constant time.
2750 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002751 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002752 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002753 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002754 * from previous calls to psa_aead_update()
2755 * that could not be processed until the end
2756 * of the input.
2757 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002758 * This must be at least
2759 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2760 * \c alg is the algorithm that is being
2761 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002762 * \param[out] plaintext_length On success, the number of bytes of
2763 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002764 * \param[in] tag Buffer containing the authentication tag.
2765 * \param tag_length Size of the \p tag buffer in bytes.
2766 *
2767 * \retval #PSA_SUCCESS
2768 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002769 * \retval #PSA_ERROR_INVALID_SIGNATURE
2770 * The calculations were successful, but the authentication tag is
2771 * not correct.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002772 * \retval #PSA_ERROR_BAD_STATE
2773 * The operation state is not valid (not set up, nonce not set,
2774 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002775 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2776 * The size of the \p plaintext buffer is too small.
2777 * You can determine a sufficient buffer size for \p plaintext by
2778 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2779 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002780 * \retval #PSA_ERROR_INVALID_ARGUMENT
2781 * The total length of input to psa_aead_update_ad() so far is
2782 * less than the additional data length that was previously
2783 * specified with psa_aead_set_lengths().
2784 * \retval #PSA_ERROR_INVALID_ARGUMENT
2785 * The total length of input to psa_aead_update() so far is
2786 * less than the plaintext length that was previously
2787 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002788 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2789 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2790 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002791 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002792 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002793 * \retval #PSA_ERROR_BAD_STATE
2794 * The library has not been previously initialized by psa_crypto_init().
2795 * It is implementation-dependent whether a failure to initialize
2796 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002797 */
2798psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002799 uint8_t *plaintext,
2800 size_t plaintext_size,
2801 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002802 const uint8_t *tag,
2803 size_t tag_length);
2804
2805/** Abort an AEAD operation.
2806 *
2807 * Aborting an operation frees all associated resources except for the
2808 * \p operation structure itself. Once aborted, the operation object
2809 * can be reused for another operation by calling
2810 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2811 *
2812 * You may call this function any time after the operation object has
2813 * been initialized by any of the following methods:
2814 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2815 * whether it succeeds or not.
2816 * - Initializing the \c struct to all-bits-zero.
2817 * - Initializing the \c struct to logical zeros, e.g.
2818 * `psa_aead_operation_t operation = {0}`.
2819 *
2820 * In particular, calling psa_aead_abort() after the operation has been
2821 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2822 * is safe and has no effect.
2823 *
2824 * \param[in,out] operation Initialized AEAD operation.
2825 *
2826 * \retval #PSA_SUCCESS
2827 * \retval #PSA_ERROR_BAD_STATE
2828 * \p operation is not an active AEAD operation.
2829 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2830 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002831 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002832 * \retval #PSA_ERROR_BAD_STATE
2833 * The library has not been previously initialized by psa_crypto_init().
2834 * It is implementation-dependent whether a failure to initialize
2835 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002836 */
2837psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2838
Gilles Peskine3b555712018-03-03 21:27:57 +01002839/**@}*/
2840
Gilles Peskine20035e32018-02-03 22:44:14 +01002841/** \defgroup asymmetric Asymmetric cryptography
2842 * @{
2843 */
2844
2845/**
2846 * \brief Sign a hash or short message with a private key.
2847 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002848 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002849 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002850 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2851 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2852 * to determine the hash algorithm to use.
2853 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002854 * \param handle Handle to the key to use for the operation.
2855 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002856 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002857 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002858 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002859 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002860 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002861 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002862 * \param[out] signature_length On success, the number of bytes
2863 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002864 *
Gilles Peskine28538492018-07-11 17:34:00 +02002865 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002866 * \retval #PSA_ERROR_INVALID_HANDLE
2867 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002868 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002869 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002870 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002871 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002872 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002873 * respectively of \p handle.
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. Shaw27c12152019-08-08 11:10:32 +01002880 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002881 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002882 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002883 * The library has not been previously initialized by psa_crypto_init().
2884 * It is implementation-dependent whether a failure to initialize
2885 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002886 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002887psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002888 psa_algorithm_t alg,
2889 const uint8_t *hash,
2890 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002891 uint8_t *signature,
2892 size_t signature_size,
2893 size_t *signature_length);
2894
2895/**
2896 * \brief Verify the signature a hash or short message using a public key.
2897 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002898 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002899 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002900 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2901 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2902 * to determine the hash algorithm to use.
2903 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002904 * \param handle Handle to the key to use for the operation.
2905 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002906 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002907 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002908 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002909 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002910 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002911 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002912 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002913 *
Gilles Peskine28538492018-07-11 17:34:00 +02002914 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002915 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002916 * \retval #PSA_ERROR_INVALID_HANDLE
2917 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002918 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002919 * The calculation was perfomed successfully, but the passed
2920 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002921 * \retval #PSA_ERROR_NOT_SUPPORTED
2922 * \retval #PSA_ERROR_INVALID_ARGUMENT
2923 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2924 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2925 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002926 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002927 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002928 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002929 * The library has not been previously initialized by psa_crypto_init().
2930 * It is implementation-dependent whether a failure to initialize
2931 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002932 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002933psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002934 psa_algorithm_t alg,
2935 const uint8_t *hash,
2936 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002937 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002938 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002939
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002940/**
2941 * \brief Encrypt a short message with a public key.
2942 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002943 * \param handle Handle to the key to use for the operation.
2944 * It must be a public key or an asymmetric
2945 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002946 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002947 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002948 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002949 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002950 * \param[in] salt A salt or label, if supported by the
2951 * encryption algorithm.
2952 * If the algorithm does not support a
2953 * salt, pass \c NULL.
2954 * If the algorithm supports an optional
2955 * salt and you do not want to pass a salt,
2956 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002957 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002958 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2959 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002960 * \param salt_length Size of the \p salt buffer in bytes.
2961 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002962 * \param[out] output Buffer where the encrypted message is to
2963 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002964 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002965 * \param[out] output_length On success, the number of bytes
2966 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002967 *
Gilles Peskine28538492018-07-11 17:34:00 +02002968 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002969 * \retval #PSA_ERROR_INVALID_HANDLE
2970 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002971 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002972 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002973 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002974 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002975 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002976 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002977 * \retval #PSA_ERROR_NOT_SUPPORTED
2978 * \retval #PSA_ERROR_INVALID_ARGUMENT
2979 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2980 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2981 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002982 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002983 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002984 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002985 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002986 * The library has not been previously initialized by psa_crypto_init().
2987 * It is implementation-dependent whether a failure to initialize
2988 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002989 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002990psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002991 psa_algorithm_t alg,
2992 const uint8_t *input,
2993 size_t input_length,
2994 const uint8_t *salt,
2995 size_t salt_length,
2996 uint8_t *output,
2997 size_t output_size,
2998 size_t *output_length);
2999
3000/**
3001 * \brief Decrypt a short message with a private key.
3002 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003003 * \param handle Handle to the key to use for the operation.
3004 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003005 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003006 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003007 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003008 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003009 * \param[in] salt A salt or label, if supported by the
3010 * encryption algorithm.
3011 * If the algorithm does not support a
3012 * salt, pass \c NULL.
3013 * If the algorithm supports an optional
3014 * salt and you do not want to pass a salt,
3015 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003016 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003017 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3018 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003019 * \param salt_length Size of the \p salt buffer in bytes.
3020 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003021 * \param[out] output Buffer where the decrypted message is to
3022 * be written.
3023 * \param output_size Size of the \c output buffer in bytes.
3024 * \param[out] output_length On success, the number of bytes
3025 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003026 *
Gilles Peskine28538492018-07-11 17:34:00 +02003027 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003028 * \retval #PSA_ERROR_INVALID_HANDLE
3029 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003030 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003031 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003032 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003033 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003034 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003035 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02003036 * \retval #PSA_ERROR_NOT_SUPPORTED
3037 * \retval #PSA_ERROR_INVALID_ARGUMENT
3038 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3039 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3040 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003041 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003042 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003043 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3044 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003045 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003046 * The library has not been previously initialized by psa_crypto_init().
3047 * It is implementation-dependent whether a failure to initialize
3048 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003049 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003050psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003051 psa_algorithm_t alg,
3052 const uint8_t *input,
3053 size_t input_length,
3054 const uint8_t *salt,
3055 size_t salt_length,
3056 uint8_t *output,
3057 size_t output_size,
3058 size_t *output_length);
3059
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003060/**@}*/
3061
Gilles Peskine35675b62019-05-16 17:26:11 +02003062/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003063 * @{
3064 */
3065
Gilles Peskine35675b62019-05-16 17:26:11 +02003066/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003067 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003068 * Before calling any function on a key derivation operation object, the
3069 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003070 * - Set the structure to all-bits-zero, for example:
3071 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003072 * psa_key_derivation_operation_t operation;
3073 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003074 * \endcode
3075 * - Initialize the structure to logical zero values, for example:
3076 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003077 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003078 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003079 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003080 * for example:
3081 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003082 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003083 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003084 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003085 * to the structure, for example:
3086 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003087 * psa_key_derivation_operation_t operation;
3088 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003089 * \endcode
3090 *
3091 * This is an implementation-defined \c struct. Applications should not
3092 * make any assumptions about the content of this structure except
3093 * as directed by the documentation of a specific implementation.
3094 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003095typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003096
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003097/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003098 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003099 * This macro returns a suitable initializer for a key derivation operation
3100 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003101 */
3102#ifdef __DOXYGEN_ONLY__
3103/* This is an example definition for documentation purposes.
3104 * Implementations should define a suitable value in `crypto_struct.h`.
3105 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003106#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003107#endif
3108
Gilles Peskine35675b62019-05-16 17:26:11 +02003109/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003110 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003111static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003112
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003113/** Set up a key derivation operation.
3114 *
3115 * A key derivation algorithm takes some inputs and uses them to generate
3116 * a byte stream in a deterministic way.
3117 * This byte stream can be used to produce keys and other
3118 * cryptographic material.
3119 *
3120 * To derive a key:
3121 * - Start with an initialized object of type #psa_key_derivation_operation_t.
3122 * - Call psa_key_derivation_setup() to select the algorithm.
3123 * - Provide the inputs for the key derivation by calling
3124 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3125 * as appropriate. Which inputs are needed, in what order, and whether
3126 * they may be keys and if so of what type depends on the algorithm.
3127 * - Optionally set the operation's maximum capacity with
3128 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3129 * of or after providing inputs. For some algorithms, this step is mandatory
3130 * because the output depends on the maximum capacity.
3131 * - To derive a key, call psa_key_derivation_output_key().
3132 * To derive a byte string for a different purpose, call
3133 * - psa_key_derivation_output_bytes().
3134 * Successive calls to these functions use successive output bytes
3135 * calculated by the key derivation algorithm.
3136 * - Clean up the key derivation operation object with
3137 * psa_key_derivation_abort().
3138 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003139 * Implementations must reject an attempt to derive a key of size 0.
3140 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003141 * \param[in,out] operation The key derivation operation object
3142 * to set up. It must
3143 * have been initialized but not set up yet.
3144 * \param alg The key derivation algorithm to compute
3145 * (\c PSA_ALG_XXX value such that
3146 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3147 *
3148 * \retval #PSA_SUCCESS
3149 * Success.
3150 * \retval #PSA_ERROR_INVALID_ARGUMENT
3151 * \c alg is not a key derivation algorithm.
3152 * \retval #PSA_ERROR_NOT_SUPPORTED
3153 * \c alg is not supported or is not a key derivation algorithm.
3154 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3155 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3156 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003157 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003158 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003159 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01003160 * The operation state is either not initialized or has already been setup.
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003161 * \retval #PSA_ERROR_BAD_STATE
3162 * The library has not been previously initialized by psa_crypto_init().
3163 * It is implementation-dependent whether a failure to initialize
3164 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003165 */
3166psa_status_t psa_key_derivation_setup(
3167 psa_key_derivation_operation_t *operation,
3168 psa_algorithm_t alg);
3169
Gilles Peskine35675b62019-05-16 17:26:11 +02003170/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003171 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003172 * The capacity of a key derivation is the maximum number of bytes that it can
3173 * return. When you get *N* bytes of output from a key derivation operation,
3174 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003175 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003176 * \param[in] operation The operation to query.
3177 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003178 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003179 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003180 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003181 * \retval #PSA_ERROR_BAD_STATE
3182 * The operation state is not valid.
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003183 * \retval #PSA_ERROR_HARDWARE_FAILURE
3184 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003185 * \retval #PSA_ERROR_BAD_STATE
3186 * The library has not been previously initialized by psa_crypto_init().
3187 * It is implementation-dependent whether a failure to initialize
3188 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003189 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003190psa_status_t psa_key_derivation_get_capacity(
3191 const psa_key_derivation_operation_t *operation,
3192 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003193
Gilles Peskine35675b62019-05-16 17:26:11 +02003194/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003195 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003196 * The capacity of a key derivation operation is the maximum number of bytes
3197 * that the key derivation operation can return from this point onwards.
3198 *
3199 * \param[in,out] operation The key derivation operation object to modify.
3200 * \param capacity The new capacity of the operation.
3201 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003202 * current capacity.
3203 *
3204 * \retval #PSA_SUCCESS
3205 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003206 * \p capacity is larger than the operation's current capacity.
3207 * In this case, the operation object remains valid and its capacity
3208 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003209 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003210 * The operation state is not valid.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003211 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003212 * \retval #PSA_ERROR_HARDWARE_FAILURE
3213 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003214 * \retval #PSA_ERROR_BAD_STATE
3215 * The library has not been previously initialized by psa_crypto_init().
3216 * It is implementation-dependent whether a failure to initialize
3217 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003218 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003219psa_status_t psa_key_derivation_set_capacity(
3220 psa_key_derivation_operation_t *operation,
3221 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003222
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003223/** Use the maximum possible capacity for a key derivation operation.
3224 *
3225 * Use this value as the capacity argument when setting up a key derivation
3226 * to indicate that the operation should have the maximum possible capacity.
3227 * The value of the maximum possible capacity depends on the key derivation
3228 * algorithm.
3229 */
3230#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3231
3232/** Provide an input for key derivation or key agreement.
3233 *
3234 * Which inputs are required and in what order depends on the algorithm.
3235 * Refer to the documentation of each key derivation or key agreement
3236 * algorithm for information.
3237 *
3238 * This function passes direct inputs. Some inputs must be passed as keys
3239 * using psa_key_derivation_input_key() instead of this function. Refer to
3240 * the documentation of individual step types for information.
3241 *
3242 * \param[in,out] operation The key derivation operation object to use.
3243 * It must have been set up with
3244 * psa_key_derivation_setup() and must not
3245 * have produced any output yet.
3246 * \param step Which step the input data is for.
3247 * \param[in] data Input data to use.
3248 * \param data_length Size of the \p data buffer in bytes.
3249 *
3250 * \retval #PSA_SUCCESS
3251 * Success.
3252 * \retval #PSA_ERROR_INVALID_ARGUMENT
3253 * \c step is not compatible with the operation's algorithm.
3254 * \retval #PSA_ERROR_INVALID_ARGUMENT
3255 * \c step does not allow direct inputs.
3256 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3257 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3258 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003259 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003260 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003261 * \retval #PSA_ERROR_BAD_STATE
3262 * The value of \p step is not valid given the state of \p operation.
3263 * \retval #PSA_ERROR_BAD_STATE
3264 * The library has not been previously initialized by psa_crypto_init().
3265 * It is implementation-dependent whether a failure to initialize
3266 * results in this error code.
3267 */
3268psa_status_t psa_key_derivation_input_bytes(
3269 psa_key_derivation_operation_t *operation,
3270 psa_key_derivation_step_t step,
3271 const uint8_t *data,
3272 size_t data_length);
3273
3274/** Provide an input for key derivation in the form of a key.
3275 *
3276 * Which inputs are required and in what order depends on the algorithm.
3277 * Refer to the documentation of each key derivation or key agreement
3278 * algorithm for information.
3279 *
3280 * This function passes key inputs. Some inputs must be passed as keys
3281 * of the appropriate type using this function, while others must be
3282 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3283 * the documentation of individual step types for information.
3284 *
3285 * \param[in,out] operation The key derivation operation object to use.
3286 * It must have been set up with
3287 * psa_key_derivation_setup() and must not
3288 * have produced any output yet.
3289 * \param step Which step the input data is for.
3290 * \param handle Handle to the key. It must have an
3291 * appropriate type for \p step and must
3292 * allow the usage #PSA_KEY_USAGE_DERIVE.
3293 *
3294 * \retval #PSA_SUCCESS
3295 * Success.
3296 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003297 * \retval #PSA_ERROR_NOT_PERMITTED
3298 * \retval #PSA_ERROR_INVALID_ARGUMENT
3299 * \c step is not compatible with the operation's algorithm.
3300 * \retval #PSA_ERROR_INVALID_ARGUMENT
3301 * \c step does not allow key inputs.
3302 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3303 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3304 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003305 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003306 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003307 * \retval #PSA_ERROR_BAD_STATE
3308 * The value of \p step is not valid given the state of \p operation.
3309 * \retval #PSA_ERROR_BAD_STATE
3310 * The library has not been previously initialized by psa_crypto_init().
3311 * It is implementation-dependent whether a failure to initialize
3312 * results in this error code.
3313 */
3314psa_status_t psa_key_derivation_input_key(
3315 psa_key_derivation_operation_t *operation,
3316 psa_key_derivation_step_t step,
3317 psa_key_handle_t handle);
3318
3319/** Perform a key agreement and use the shared secret as input to a key
3320 * derivation.
3321 *
3322 * A key agreement algorithm takes two inputs: a private key \p private_key
3323 * a public key \p peer_key.
3324 * The result of this function is passed as input to a key derivation.
3325 * The output of this key derivation can be extracted by reading from the
3326 * resulting operation to produce keys and other cryptographic material.
3327 *
3328 * \param[in,out] operation The key derivation operation object to use.
3329 * It must have been set up with
3330 * psa_key_derivation_setup() with a
3331 * key agreement and derivation algorithm
3332 * \c alg (\c PSA_ALG_XXX value such that
3333 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3334 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3335 * is false).
3336 * The operation must be ready for an
3337 * input of the type given by \p step.
3338 * \param step Which step the input data is for.
3339 * \param private_key Handle to the private key to use.
3340 * \param[in] peer_key Public key of the peer. The peer key must be in the
3341 * same format that psa_import_key() accepts for the
3342 * public key type corresponding to the type of
3343 * private_key. That is, this function performs the
3344 * equivalent of
3345 * #psa_import_key(...,
3346 * `peer_key`, `peer_key_length`) where
3347 * with key attributes indicating the public key
3348 * type corresponding to the type of `private_key`.
3349 * For example, for EC keys, this means that peer_key
3350 * is interpreted as a point on the curve that the
3351 * private key is on. The standard formats for public
3352 * keys are documented in the documentation of
3353 * psa_export_public_key().
3354 * \param peer_key_length Size of \p peer_key in bytes.
3355 *
3356 * \retval #PSA_SUCCESS
3357 * Success.
3358 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003359 * \retval #PSA_ERROR_NOT_PERMITTED
3360 * \retval #PSA_ERROR_INVALID_ARGUMENT
3361 * \c private_key is not compatible with \c alg,
3362 * or \p peer_key is not valid for \c alg or not compatible with
3363 * \c private_key.
3364 * \retval #PSA_ERROR_NOT_SUPPORTED
3365 * \c alg is not supported or is not a key derivation algorithm.
3366 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3367 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3368 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003369 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003370 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003371 * \retval #PSA_ERROR_BAD_STATE
3372 * The library has not been previously initialized by psa_crypto_init().
3373 * It is implementation-dependent whether a failure to initialize
3374 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003375 */
3376psa_status_t psa_key_derivation_key_agreement(
3377 psa_key_derivation_operation_t *operation,
3378 psa_key_derivation_step_t step,
3379 psa_key_handle_t private_key,
3380 const uint8_t *peer_key,
3381 size_t peer_key_length);
3382
Gilles Peskine35675b62019-05-16 17:26:11 +02003383/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003384 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003385 * This function calculates output bytes from a key derivation algorithm and
3386 * return those bytes.
3387 * If you view the key derivation's output as a stream of bytes, this
3388 * function destructively reads the requested number of bytes from the
3389 * stream.
3390 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003391 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003392 * \param[in,out] operation The key derivation operation object to read from.
3393 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003394 * \param output_length Number of bytes to output.
3395 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003396 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003397 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003398 * The operation's capacity was less than
3399 * \p output_length bytes. Note that in this case,
3400 * no output is written to the output buffer.
3401 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003402 * subsequent calls to this function will not
3403 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003404 * \retval #PSA_ERROR_BAD_STATE
3405 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3406 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3407 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003408 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003409 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003410 * \retval #PSA_ERROR_BAD_STATE
3411 * The library has not been previously initialized by psa_crypto_init().
3412 * It is implementation-dependent whether a failure to initialize
3413 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003414 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003415psa_status_t psa_key_derivation_output_bytes(
3416 psa_key_derivation_operation_t *operation,
3417 uint8_t *output,
3418 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003419
Gilles Peskine35675b62019-05-16 17:26:11 +02003420/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003421 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003422 * This function calculates output bytes from a key derivation algorithm
3423 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003424 * The key's location, usage policy, type and size are taken from
3425 * \p attributes.
3426 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003427 * If you view the key derivation's output as a stream of bytes, this
3428 * function destructively reads as many bytes as required from the
3429 * stream.
3430 * The operation's capacity decreases by the number of bytes read.
3431 *
3432 * How much output is produced and consumed from the operation, and how
3433 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003434 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003435 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003436 * of a given size, this function is functionally equivalent to
3437 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003438 * and passing the resulting output to #psa_import_key.
3439 * However, this function has a security benefit:
3440 * if the implementation provides an isolation boundary then
3441 * the key material is not exposed outside the isolation boundary.
3442 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003443 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003444 * The following key types defined in this specification follow this scheme:
3445 *
3446 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003447 * - #PSA_KEY_TYPE_ARC4;
3448 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003449 * - #PSA_KEY_TYPE_DERIVE;
3450 * - #PSA_KEY_TYPE_HMAC.
3451 *
3452 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003453 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003454 * Montgomery curve), this function always draws a byte string whose
3455 * length is determined by the curve, and sets the mandatory bits
3456 * accordingly. That is:
3457 *
3458 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3459 * and process it as specified in RFC 7748 &sect;5.
3460 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3461 * and process it as specified in RFC 7748 &sect;5.
3462 *
3463 * - For key types for which the key is represented by a single sequence of
3464 * \p bits bits with constraints as to which bit sequences are acceptable,
3465 * this function draws a byte string of length (\p bits / 8) bytes rounded
3466 * up to the nearest whole number of bytes. If the resulting byte string
3467 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3468 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003469 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003470 * for the output produced by psa_export_key().
3471 * The following key types defined in this specification follow this scheme:
3472 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003473 * - #PSA_KEY_TYPE_DES.
3474 * Force-set the parity bits, but discard forbidden weak keys.
3475 * For 2-key and 3-key triple-DES, the three keys are generated
3476 * successively (for example, for 3-key triple-DES,
3477 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3478 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003479 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003480 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003481 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003482 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003483 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003484 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003485 * Weierstrass curve).
3486 * For these key types, interpret the byte string as integer
3487 * in big-endian order. Discard it if it is not in the range
3488 * [0, *N* - 2] where *N* is the boundary of the private key domain
3489 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003490 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003491 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003492 * This method allows compliance to NIST standards, specifically
3493 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003494 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3495 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3496 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3497 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003498 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003499 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003500 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003501 * implementation-defined.
3502 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003503 * In all cases, the data that is read is discarded from the operation.
3504 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003505 *
Gilles Peskine20628592019-04-19 19:29:50 +02003506 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003507 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003508 * \param[out] handle On success, a handle to the newly created key.
3509 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003510 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003511 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003512 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003513 * If the key is persistent, the key material and the key's metadata
3514 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003515 * \retval #PSA_ERROR_ALREADY_EXISTS
3516 * This is an attempt to create a persistent key, and there is
3517 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003518 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003519 * There was not enough data to create the desired key.
3520 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003521 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003522 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003523 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003524 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003525 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003526 * \retval #PSA_ERROR_INVALID_ARGUMENT
3527 * The provided key attributes are not valid for the operation.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003528 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003529 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3530 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3531 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3532 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003533 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003534 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003535 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003536 * The library has not been previously initialized by psa_crypto_init().
3537 * It is implementation-dependent whether a failure to initialize
3538 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003539 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003540psa_status_t psa_key_derivation_output_key(
3541 const psa_key_attributes_t *attributes,
3542 psa_key_derivation_operation_t *operation,
3543 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003544
Gilles Peskine35675b62019-05-16 17:26:11 +02003545/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003546 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003547 * Once a key derivation operation has been aborted, its capacity is zero.
3548 * Aborting an operation frees all associated resources except for the
3549 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003550 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003551 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003552 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003553 * psa_key_derivation_operation_init() or a zero value. In particular,
3554 * it is valid to call psa_key_derivation_abort() twice, or to call
3555 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003556 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003557 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003558 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003559 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003560 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003561 * \retval #PSA_SUCCESS
3562 * \retval #PSA_ERROR_BAD_STATE
3563 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3564 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003565 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003566 * \retval #PSA_ERROR_BAD_STATE
3567 * The library has not been previously initialized by psa_crypto_init().
3568 * It is implementation-dependent whether a failure to initialize
3569 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003570 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003571psa_status_t psa_key_derivation_abort(
3572 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003573
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003574/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003575 *
3576 * \warning The raw result of a key agreement algorithm such as finite-field
3577 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3578 * not be used directly as key material. It should instead be passed as
3579 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003580 * a key derivation, use psa_key_derivation_key_agreement() and other
3581 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003582 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003583 * \param alg The key agreement algorithm to compute
3584 * (\c PSA_ALG_XXX value such that
3585 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3586 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003587 * \param private_key Handle to the private key to use.
3588 * \param[in] peer_key Public key of the peer. It must be
3589 * in the same format that psa_import_key()
3590 * accepts. The standard formats for public
3591 * keys are documented in the documentation
3592 * of psa_export_public_key().
3593 * \param peer_key_length Size of \p peer_key in bytes.
3594 * \param[out] output Buffer where the decrypted message is to
3595 * be written.
3596 * \param output_size Size of the \c output buffer in bytes.
3597 * \param[out] output_length On success, the number of bytes
3598 * that make up the returned output.
3599 *
3600 * \retval #PSA_SUCCESS
3601 * Success.
3602 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003603 * \retval #PSA_ERROR_NOT_PERMITTED
3604 * \retval #PSA_ERROR_INVALID_ARGUMENT
3605 * \p alg is not a key agreement algorithm
3606 * \retval #PSA_ERROR_INVALID_ARGUMENT
3607 * \p private_key is not compatible with \p alg,
3608 * or \p peer_key is not valid for \p alg or not compatible with
3609 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003610 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3611 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003612 * \retval #PSA_ERROR_NOT_SUPPORTED
3613 * \p alg is not a supported key agreement algorithm.
3614 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3615 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3616 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003617 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003618 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003619 * \retval #PSA_ERROR_BAD_STATE
3620 * The library has not been previously initialized by psa_crypto_init().
3621 * It is implementation-dependent whether a failure to initialize
3622 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003623 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003624psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3625 psa_key_handle_t private_key,
3626 const uint8_t *peer_key,
3627 size_t peer_key_length,
3628 uint8_t *output,
3629 size_t output_size,
3630 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003631
Gilles Peskineea0fb492018-07-12 17:17:20 +02003632/**@}*/
3633
Gilles Peskineedd76872018-07-20 17:42:05 +02003634/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003635 * @{
3636 */
3637
3638/**
3639 * \brief Generate random bytes.
3640 *
3641 * \warning This function **can** fail! Callers MUST check the return status
3642 * and MUST NOT use the content of the output buffer if the return
3643 * status is not #PSA_SUCCESS.
3644 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003645 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003646 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003647 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003648 * \param output_size Number of bytes to generate and output.
3649 *
Gilles Peskine28538492018-07-11 17:34:00 +02003650 * \retval #PSA_SUCCESS
3651 * \retval #PSA_ERROR_NOT_SUPPORTED
3652 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003653 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003654 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3655 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003656 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003657 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003658 * The library has not been previously initialized by psa_crypto_init().
3659 * It is implementation-dependent whether a failure to initialize
3660 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003661 */
3662psa_status_t psa_generate_random(uint8_t *output,
3663 size_t output_size);
3664
3665/**
3666 * \brief Generate a key or key pair.
3667 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003668 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02003669 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003670 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003671 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003672 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003673 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003674 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003675 * the public exponent is 65537.
3676 * The modulus is a product of two probabilistic primes
3677 * between 2^{n-1} and 2^n where n is the bit size specified in the
3678 * attributes.
3679 *
Gilles Peskine20628592019-04-19 19:29:50 +02003680 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003681 * \param[out] handle On success, a handle to the newly created key.
3682 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003683 *
Gilles Peskine28538492018-07-11 17:34:00 +02003684 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003685 * Success.
3686 * If the key is persistent, the key material and the key's metadata
3687 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003688 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003689 * This is an attempt to create a persistent key, and there is
3690 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003691 * \retval #PSA_ERROR_NOT_SUPPORTED
3692 * \retval #PSA_ERROR_INVALID_ARGUMENT
3693 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3694 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3695 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3696 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003697 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003698 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3699 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003700 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003701 * The library has not been previously initialized by psa_crypto_init().
3702 * It is implementation-dependent whether a failure to initialize
3703 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003704 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003705psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003706 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003707
3708/**@}*/
3709
Gilles Peskinee59236f2018-01-27 23:32:46 +01003710#ifdef __cplusplus
3711}
3712#endif
3713
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003714/* The file "crypto_sizes.h" contains definitions for size calculation
3715 * macros whose definitions are implementation-specific. */
3716#include "crypto_sizes.h"
3717
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003718/* The file "crypto_struct.h" contains definitions for
3719 * implementation-specific structs that are declared above. */
3720#include "crypto_struct.h"
3721
3722/* The file "crypto_extra.h" contains vendor-specific definitions. This
3723 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003724#include "crypto_extra.h"
3725
3726#endif /* PSA_CRYPTO_H */