blob: c9b3c15bae27619d02c73bcb8138ac2f363ba77c [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
Andrew Thoelke02b372b2019-10-02 09:32:21 +010060/** \defgroup version API version
Adrian L. Shawd89338a2019-09-19 13:32:57 +010061 * @{
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.
Gilles Peskine24934012019-10-08 15:43:13 +0200462 * If this is \c 0, do nothing and return \c PSA_SUCCESS.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100463 *
464 * \retval #PSA_SUCCESS
Gilles Peskine24934012019-10-08 15:43:13 +0200465 * \p handle was a valid handle or \c 0. It is now closed.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100466 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine24934012019-10-08 15:43:13 +0200467 * \p handle is not a valid handle nor \c 0.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100468 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawf97c8522019-08-15 13:27:12 +0100469 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100470 * \retval #PSA_ERROR_BAD_STATE
471 * The library has not been previously initialized by psa_crypto_init().
472 * It is implementation-dependent whether a failure to initialize
473 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100474 */
475psa_status_t psa_close_key(psa_key_handle_t handle);
476
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100477/** Make a copy of a key.
478 *
479 * Copy key material from one location to another.
480 *
481 * This function is primarily useful to copy a key from one location
482 * to another, since it populates a key using the material from
483 * another key which may have a different lifetime.
484 *
485 * This function may be used to share a key with a different party,
486 * subject to implementation-defined restrictions on key sharing.
487 *
488 * The policy on the source key must have the usage flag
489 * #PSA_KEY_USAGE_COPY set.
490 * This flag is sufficient to permit the copy if the key has the lifetime
491 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
492 * Some secure elements do not provide a way to copy a key without
493 * making it extractable from the secure element. If a key is located
494 * in such a secure element, then the key must have both usage flags
495 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
496 * a copy of the key outside the secure element.
497 *
498 * The resulting key may only be used in a way that conforms to
499 * both the policy of the original key and the policy specified in
500 * the \p attributes parameter:
501 * - The usage flags on the resulting key are the bitwise-and of the
502 * usage flags on the source policy and the usage flags in \p attributes.
503 * - If both allow the same algorithm or wildcard-based
504 * algorithm policy, the resulting key has the same algorithm policy.
505 * - If either of the policies allows an algorithm and the other policy
506 * allows a wildcard-based algorithm policy that includes this algorithm,
507 * the resulting key allows the same algorithm.
508 * - If the policies do not allow any algorithm in common, this function
509 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
510 *
511 * The effect of this function on implementation-defined attributes is
512 * implementation-defined.
513 *
514 * \param source_handle The key to copy. It must be a valid key handle.
515 * \param[in] attributes The attributes for the new key.
516 * They are used as follows:
517 * - The key type and size may be 0. If either is
518 * nonzero, it must match the corresponding
519 * attribute of the source key.
520 * - The key location (the lifetime and, for
521 * persistent keys, the key identifier) is
522 * used directly.
523 * - The policy constraints (usage flags and
524 * algorithm policy) are combined from
525 * the source key and \p attributes so that
526 * both sets of restrictions apply, as
527 * described in the documentation of this function.
528 * \param[out] target_handle On success, a handle to the newly created key.
529 * \c 0 on failure.
530 *
531 * \retval #PSA_SUCCESS
532 * \retval #PSA_ERROR_INVALID_HANDLE
533 * \p source_handle is invalid.
534 * \retval #PSA_ERROR_ALREADY_EXISTS
535 * This is an attempt to create a persistent key, and there is
536 * already a persistent key with the given identifier.
537 * \retval #PSA_ERROR_INVALID_ARGUMENT
538 * The lifetime or identifier in \p attributes are invalid.
539 * \retval #PSA_ERROR_INVALID_ARGUMENT
540 * The policy constraints on the source and specified in
541 * \p attributes are incompatible.
542 * \retval #PSA_ERROR_INVALID_ARGUMENT
543 * \p attributes specifies a key type or key size
544 * which does not match the attributes of the source key.
545 * \retval #PSA_ERROR_NOT_PERMITTED
546 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
547 * \retval #PSA_ERROR_NOT_PERMITTED
548 * The source key is not exportable and its lifetime does not
549 * allow copying it to the target's lifetime.
550 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
551 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
552 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
553 * \retval #PSA_ERROR_HARDWARE_FAILURE
554 * \retval #PSA_ERROR_STORAGE_FAILURE
555 * \retval #PSA_ERROR_CORRUPTION_DETECTED
556 * \retval #PSA_ERROR_BAD_STATE
557 * The library has not been previously initialized by psa_crypto_init().
558 * It is implementation-dependent whether a failure to initialize
559 * results in this error code.
560 */
561psa_status_t psa_copy_key(psa_key_handle_t source_handle,
562 const psa_key_attributes_t *attributes,
563 psa_key_handle_t *target_handle);
564
565
566/**
567 * \brief Destroy a key.
568 *
569 * This function destroys a key from both volatile
570 * memory and, if applicable, non-volatile storage. Implementations shall
571 * make a best effort to ensure that that the key material cannot be recovered.
572 *
573 * This function also erases any metadata such as policies and frees
574 * resources associated with the key. To free all resources associated with
575 * the key, all handles to the key must be closed or destroyed.
576 *
577 * Destroying the key makes the handle invalid, and the key handle
578 * must not be used again by the application. Using other open handles to the
579 * destroyed key in a cryptographic operation will result in an error.
580 *
581 * If a key is currently in use in a multipart operation, then destroying the
582 * key will cause the multipart operation to fail.
583 *
584 * \param handle Handle to the key to erase.
Gilles Peskine24934012019-10-08 15:43:13 +0200585 * If this is \c 0, do nothing and return \c PSA_SUCCESS.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100586 *
587 * \retval #PSA_SUCCESS
Gilles Peskine24934012019-10-08 15:43:13 +0200588 * \p handle was a valid handle and the key material that it
589 * referred to has been erased.
590 * Alternatively, \p handle is \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100591 * \retval #PSA_ERROR_NOT_PERMITTED
592 * The key cannot be erased because it is
593 * read-only, either due to a policy or due to physical restrictions.
594 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine24934012019-10-08 15:43:13 +0200595 * \p handle is not a valid handle nor \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100596 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
597 * There was an failure in communication with the cryptoprocessor.
598 * The key material may still be present in the cryptoprocessor.
599 * \retval #PSA_ERROR_STORAGE_FAILURE
600 * The storage is corrupted. Implementations shall make a best effort
601 * to erase key material even in this stage, however applications
602 * should be aware that it may be impossible to guarantee that the
603 * key material is not recoverable in such cases.
604 * \retval #PSA_ERROR_CORRUPTION_DETECTED
605 * An unexpected condition which is not a storage corruption or
606 * a communication failure occurred. The cryptoprocessor may have
607 * been compromised.
608 * \retval #PSA_ERROR_BAD_STATE
609 * The library has not been previously initialized by psa_crypto_init().
610 * It is implementation-dependent whether a failure to initialize
611 * results in this error code.
612 */
613psa_status_t psa_destroy_key(psa_key_handle_t handle);
614
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100615/**@}*/
616
617/** \defgroup import_export Key import and export
618 * @{
619 */
620
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100621/**
622 * \brief Import a key in binary format.
623 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100624 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100625 * documentation of psa_export_public_key() for the format of public keys
626 * and to the documentation of psa_export_key() for the format for
627 * other key types.
628 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200629 * The key data determines the key size. The attributes may optionally
630 * specify a key size; in this case it must match the size determined
631 * from the key data. A key size of 0 in \p attributes indicates that
632 * the key size is solely determined by the key data.
633 *
634 * Implementations must reject an attempt to import a key of size 0.
635 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100636 * This specification supports a single format for each key type.
637 * Implementations may support other formats as long as the standard
638 * format is supported. Implementations that support other formats
639 * should ensure that the formats are clearly unambiguous so as to
640 * minimize the risk that an invalid input is accidentally interpreted
641 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100642 *
Gilles Peskine20628592019-04-19 19:29:50 +0200643 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200644 * The key size is always determined from the
645 * \p data buffer.
646 * If the key size in \p attributes is nonzero,
647 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200648 * \param[out] handle On success, a handle to the newly created key.
649 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100650 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200651 * buffer is interpreted according to the type declared
652 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200653 * All implementations must support at least the format
654 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100655 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200656 * the chosen type. Implementations may allow other
657 * formats, but should be conservative: implementations
658 * should err on the side of rejecting content if it
659 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200660 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100661 *
Gilles Peskine28538492018-07-11 17:34:00 +0200662 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100663 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100664 * If the key is persistent, the key material and the key's metadata
665 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200666 * \retval #PSA_ERROR_ALREADY_EXISTS
667 * This is an attempt to create a persistent key, and there is
668 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200669 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200670 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200671 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200672 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200673 * The key attributes, as a whole, are invalid.
674 * \retval #PSA_ERROR_INVALID_ARGUMENT
675 * The key data is not correctly formatted.
676 * \retval #PSA_ERROR_INVALID_ARGUMENT
677 * The size in \p attributes is nonzero and does not match the size
678 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200679 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
680 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
681 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100682 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200683 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200684 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300685 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300686 * The library has not been previously initialized by psa_crypto_init().
687 * It is implementation-dependent whether a failure to initialize
688 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100689 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200690psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100691 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200692 size_t data_length,
693 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100694
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100695
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100696
697/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100698 * \brief Export a key in binary format.
699 *
700 * The output of this function can be passed to psa_import_key() to
701 * create an equivalent object.
702 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100703 * If the implementation of psa_import_key() supports other formats
704 * beyond the format specified here, the output from psa_export_key()
705 * must use the representation specified here, not the original
706 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100707 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100708 * For standard key types, the output format is as follows:
709 *
710 * - For symmetric keys (including MAC keys), the format is the
711 * raw bytes of the key.
712 * - For DES, the key data consists of 8 bytes. The parity bits must be
713 * correct.
714 * - For Triple-DES, the format is the concatenation of the
715 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200716 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200717 * is the non-encrypted DER encoding of the representation defined by
718 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
719 * ```
720 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200721 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200722 * modulus INTEGER, -- n
723 * publicExponent INTEGER, -- e
724 * privateExponent INTEGER, -- d
725 * prime1 INTEGER, -- p
726 * prime2 INTEGER, -- q
727 * exponent1 INTEGER, -- d mod (p-1)
728 * exponent2 INTEGER, -- d mod (q-1)
729 * coefficient INTEGER, -- (inverse of q) mod p
730 * }
731 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200732 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200733 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100734 * a representation of the private value as a `ceiling(m/8)`-byte string
735 * where `m` is the bit size associated with the curve, i.e. the bit size
736 * of the order of the curve's coordinate field. This byte string is
737 * in little-endian order for Montgomery curves (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +0100738 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
739 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
740 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100741 * This is the content of the `privateKey` field of the `ECPrivateKey`
742 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200743 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200744 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000745 * format is the representation of the private key `x` as a big-endian byte
746 * string. The length of the byte string is the private key size in bytes
747 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200748 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
749 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100750 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200751 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
752 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100753 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200754 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200755 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200756 * \param[out] data_length On success, the number of bytes
757 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100758 *
Gilles Peskine28538492018-07-11 17:34:00 +0200759 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100760 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200761 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200762 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100763 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200764 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
765 * The size of the \p data buffer is too small. You can determine a
766 * sufficient buffer size by calling
767 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
768 * where \c type is the key type
769 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200770 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
771 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200772 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100773 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100774 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300775 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300776 * The library has not been previously initialized by psa_crypto_init().
777 * It is implementation-dependent whether a failure to initialize
778 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100779 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100780psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100781 uint8_t *data,
782 size_t data_size,
783 size_t *data_length);
784
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100785/**
786 * \brief Export a public key or the public part of a key pair in binary format.
787 *
788 * The output of this function can be passed to psa_import_key() to
789 * create an object that is equivalent to the public key.
790 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000791 * This specification supports a single format for each key type.
792 * Implementations may support other formats as long as the standard
793 * format is supported. Implementations that support other formats
794 * should ensure that the formats are clearly unambiguous so as to
795 * minimize the risk that an invalid input is accidentally interpreted
796 * according to a different format.
797 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000798 * For standard key types, the output format is as follows:
799 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
800 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
801 * ```
802 * RSAPublicKey ::= SEQUENCE {
803 * modulus INTEGER, -- n
804 * publicExponent INTEGER } -- e
805 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000806 * - For elliptic curve public keys (key types for which
807 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
808 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
809 * Let `m` be the bit size associated with the curve, i.e. the bit size of
810 * `q` for a curve over `F_q`. The representation consists of:
811 * - The byte 0x04;
812 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
813 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200814 * - For Diffie-Hellman key exchange public keys (key types for which
815 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000816 * the format is the representation of the public key `y = g^x mod p` as a
817 * big-endian byte string. The length of the byte string is the length of the
818 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100819 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200820 * Exporting a public key object or the public part of a key pair is
821 * always permitted, regardless of the key's usage flags.
822 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100823 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200824 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200825 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200826 * \param[out] data_length On success, the number of bytes
827 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100828 *
Gilles Peskine28538492018-07-11 17:34:00 +0200829 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100830 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200831 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200832 * The key is neither a public key nor a key pair.
833 * \retval #PSA_ERROR_NOT_SUPPORTED
834 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
835 * The size of the \p data buffer is too small. You can determine a
836 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200837 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200838 * where \c type is the key type
839 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200840 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
841 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200842 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100843 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100844 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300845 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300846 * The library has not been previously initialized by psa_crypto_init().
847 * It is implementation-dependent whether a failure to initialize
848 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100849 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100850psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100851 uint8_t *data,
852 size_t data_size,
853 size_t *data_length);
854
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100855
Gilles Peskine20035e32018-02-03 22:44:14 +0100856
857/**@}*/
858
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100859/** \defgroup hash Message digests
860 * @{
861 */
862
Gilles Peskine69647a42019-01-14 20:18:12 +0100863/** Calculate the hash (digest) of a message.
864 *
865 * \note To verify the hash of a message against an
866 * expected value, use psa_hash_compare() instead.
867 *
868 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
869 * such that #PSA_ALG_IS_HASH(\p alg) is true).
870 * \param[in] input Buffer containing the message to hash.
871 * \param input_length Size of the \p input buffer in bytes.
872 * \param[out] hash Buffer where the hash is to be written.
873 * \param hash_size Size of the \p hash buffer in bytes.
874 * \param[out] hash_length On success, the number of bytes
875 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100876 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100877 *
878 * \retval #PSA_SUCCESS
879 * Success.
880 * \retval #PSA_ERROR_NOT_SUPPORTED
881 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100882 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100883 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
884 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100885 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
886 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
887 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200888 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100889 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100890 * \retval #PSA_ERROR_BAD_STATE
891 * The library has not been previously initialized by psa_crypto_init().
892 * It is implementation-dependent whether a failure to initialize
893 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100894 */
895psa_status_t psa_hash_compute(psa_algorithm_t alg,
896 const uint8_t *input,
897 size_t input_length,
898 uint8_t *hash,
899 size_t hash_size,
900 size_t *hash_length);
901
902/** Calculate the hash (digest) of a message and compare it with a
903 * reference value.
904 *
905 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
906 * such that #PSA_ALG_IS_HASH(\p alg) is true).
907 * \param[in] input Buffer containing the message to hash.
908 * \param input_length Size of the \p input buffer in bytes.
909 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100910 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100911 *
912 * \retval #PSA_SUCCESS
913 * The expected hash is identical to the actual hash of the input.
914 * \retval #PSA_ERROR_INVALID_SIGNATURE
915 * The hash of the message was calculated successfully, but it
916 * differs from the expected hash.
917 * \retval #PSA_ERROR_NOT_SUPPORTED
918 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100919 * \retval #PSA_ERROR_INVALID_ARGUMENT
920 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100921 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
922 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
923 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200924 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100925 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100926 * \retval #PSA_ERROR_BAD_STATE
927 * The library has not been previously initialized by psa_crypto_init().
928 * It is implementation-dependent whether a failure to initialize
929 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100930 */
931psa_status_t psa_hash_compare(psa_algorithm_t alg,
932 const uint8_t *input,
933 size_t input_length,
934 const uint8_t *hash,
Gilles Peskinefa710f52019-12-02 14:31:48 +0100935 size_t hash_length);
Gilles Peskine69647a42019-01-14 20:18:12 +0100936
Gilles Peskine308b91d2018-02-08 09:47:44 +0100937/** The type of the state data structure for multipart hash operations.
938 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000939 * Before calling any function on a hash operation object, the application must
940 * initialize it by any of the following means:
941 * - Set the structure to all-bits-zero, for example:
942 * \code
943 * psa_hash_operation_t operation;
944 * memset(&operation, 0, sizeof(operation));
945 * \endcode
946 * - Initialize the structure to logical zero values, for example:
947 * \code
948 * psa_hash_operation_t operation = {0};
949 * \endcode
950 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
951 * for example:
952 * \code
953 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
954 * \endcode
955 * - Assign the result of the function psa_hash_operation_init()
956 * to the structure, for example:
957 * \code
958 * psa_hash_operation_t operation;
959 * operation = psa_hash_operation_init();
960 * \endcode
961 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100962 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100963 * make any assumptions about the content of this structure except
964 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100965typedef struct psa_hash_operation_s psa_hash_operation_t;
966
Jaeden Amero6a25b412019-01-04 11:47:44 +0000967/** \def PSA_HASH_OPERATION_INIT
968 *
969 * This macro returns a suitable initializer for a hash operation object
970 * of type #psa_hash_operation_t.
971 */
972#ifdef __DOXYGEN_ONLY__
973/* This is an example definition for documentation purposes.
974 * Implementations should define a suitable value in `crypto_struct.h`.
975 */
976#define PSA_HASH_OPERATION_INIT {0}
977#endif
978
979/** Return an initial value for a hash operation object.
980 */
981static psa_hash_operation_t psa_hash_operation_init(void);
982
Gilles Peskinef45adda2019-01-14 18:29:18 +0100983/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100984 *
985 * The sequence of operations to calculate a hash (message digest)
986 * is as follows:
987 * -# Allocate an operation object which will be passed to all the functions
988 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000989 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100990 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200991 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100992 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100993 * of the message each time. The hash that is calculated is the hash
994 * of the concatenation of these messages in order.
995 * -# To calculate the hash, call psa_hash_finish().
996 * To compare the hash with an expected value, call psa_hash_verify().
997 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100998 * If an error occurs at any step after a call to psa_hash_setup(), the
999 * operation will need to be reset by a call to psa_hash_abort(). The
1000 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +00001001 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001002 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001003 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001004 * eventually terminate the operation. The following events terminate an
1005 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001006 * - A successful call to psa_hash_finish() or psa_hash_verify().
1007 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001008 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001009 * \param[in,out] operation The operation object to set up. It must have
1010 * been initialized as per the documentation for
1011 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001012 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1013 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001014 *
Gilles Peskine28538492018-07-11 17:34:00 +02001015 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001016 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001017 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +01001018 * \p alg is not a supported hash algorithm.
1019 * \retval #PSA_ERROR_INVALID_ARGUMENT
1020 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001021 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001022 * The operation state is not valid (it must be inactive).
Gilles Peskine28538492018-07-11 17:34:00 +02001023 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1024 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1025 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001026 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001027 * \retval #PSA_ERROR_BAD_STATE
1028 * The library has not been previously initialized by psa_crypto_init().
1029 * It is implementation-dependent whether a failure to initialize
1030 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001031 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001032psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001033 psa_algorithm_t alg);
1034
Gilles Peskine308b91d2018-02-08 09:47:44 +01001035/** Add a message fragment to a multipart hash operation.
1036 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001037 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001038 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001039 * If this function returns an error status, the operation enters an error
1040 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001041 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001042 * \param[in,out] operation Active hash operation.
1043 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001044 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001045 *
Gilles Peskine28538492018-07-11 17:34:00 +02001046 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001047 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001048 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001049 * The operation state is not valid (it muct be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001050 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1051 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1052 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001053 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001054 * \retval #PSA_ERROR_BAD_STATE
1055 * The library has not been previously initialized by psa_crypto_init().
1056 * It is implementation-dependent whether a failure to initialize
1057 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001058 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001059psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1060 const uint8_t *input,
1061 size_t input_length);
1062
Gilles Peskine308b91d2018-02-08 09:47:44 +01001063/** Finish the calculation of the hash of a message.
1064 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001065 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001066 * This function calculates the hash of the message formed by concatenating
1067 * the inputs passed to preceding calls to psa_hash_update().
1068 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001069 * When this function returns successfuly, the operation becomes inactive.
1070 * If this function returns an error status, the operation enters an error
1071 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001072 *
1073 * \warning Applications should not call this function if they expect
1074 * a specific value for the hash. Call psa_hash_verify() instead.
1075 * Beware that comparing integrity or authenticity data such as
1076 * hash values with a function such as \c memcmp is risky
1077 * because the time taken by the comparison may leak information
1078 * about the hashed data which could allow an attacker to guess
1079 * a valid hash and thereby bypass security controls.
1080 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001081 * \param[in,out] operation Active hash operation.
1082 * \param[out] hash Buffer where the hash is to be written.
1083 * \param hash_size Size of the \p hash buffer in bytes.
1084 * \param[out] hash_length On success, the number of bytes
1085 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001086 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001087 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001088 *
Gilles Peskine28538492018-07-11 17:34:00 +02001089 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001090 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001091 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001092 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001093 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001094 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001095 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001096 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001097 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1098 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1099 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001100 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001101 * \retval #PSA_ERROR_BAD_STATE
1102 * The library has not been previously initialized by psa_crypto_init().
1103 * It is implementation-dependent whether a failure to initialize
1104 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001105 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001106psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1107 uint8_t *hash,
1108 size_t hash_size,
1109 size_t *hash_length);
1110
Gilles Peskine308b91d2018-02-08 09:47:44 +01001111/** Finish the calculation of the hash of a message and compare it with
1112 * an expected value.
1113 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001114 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001115 * This function calculates the hash of the message formed by concatenating
1116 * the inputs passed to preceding calls to psa_hash_update(). It then
1117 * compares the calculated hash with the expected hash passed as a
1118 * parameter to this function.
1119 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001120 * When this function returns successfuly, the operation becomes inactive.
1121 * If this function returns an error status, the operation enters an error
1122 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001123 *
Gilles Peskine19067982018-03-20 17:54:53 +01001124 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001125 * comparison between the actual hash and the expected hash is performed
1126 * in constant time.
1127 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001128 * \param[in,out] operation Active hash operation.
1129 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001130 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001131 *
Gilles Peskine28538492018-07-11 17:34:00 +02001132 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001133 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001134 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001135 * The hash of the message was calculated successfully, but it
1136 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001137 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001138 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001139 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1140 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1141 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001142 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001143 * \retval #PSA_ERROR_BAD_STATE
1144 * The library has not been previously initialized by psa_crypto_init().
1145 * It is implementation-dependent whether a failure to initialize
1146 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001147 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001148psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1149 const uint8_t *hash,
1150 size_t hash_length);
1151
Gilles Peskine308b91d2018-02-08 09:47:44 +01001152/** Abort a hash operation.
1153 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001154 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001155 * \p operation structure itself. Once aborted, the operation object
1156 * can be reused for another operation by calling
1157 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001158 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001159 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001160 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001161 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001162 * In particular, calling psa_hash_abort() after the operation has been
1163 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1164 * psa_hash_verify() is safe and has no effect.
1165 *
1166 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001167 *
Gilles Peskine28538492018-07-11 17:34:00 +02001168 * \retval #PSA_SUCCESS
Gilles Peskine28538492018-07-11 17:34:00 +02001169 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1170 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001171 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001172 * \retval #PSA_ERROR_BAD_STATE
1173 * The library has not been previously initialized by psa_crypto_init().
1174 * It is implementation-dependent whether a failure to initialize
1175 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001176 */
1177psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001178
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001179/** Clone a hash operation.
1180 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001181 * This function copies the state of an ongoing hash operation to
1182 * a new operation object. In other words, this function is equivalent
1183 * to calling psa_hash_setup() on \p target_operation with the same
1184 * algorithm that \p source_operation was set up for, then
1185 * psa_hash_update() on \p target_operation with the same input that
1186 * that was passed to \p source_operation. After this function returns, the
1187 * two objects are independent, i.e. subsequent calls involving one of
1188 * the objects do not affect the other object.
1189 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001190 * \param[in] source_operation The active hash operation to clone.
1191 * \param[in,out] target_operation The operation object to set up.
1192 * It must be initialized but not active.
1193 *
1194 * \retval #PSA_SUCCESS
1195 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001196 * The \p source_operation state is not valid (it must be active).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001197 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001198 * The \p target_operation state is not valid (it must be inactive).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001199 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1200 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001201 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001202 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001203 * \retval #PSA_ERROR_BAD_STATE
1204 * The library has not been previously initialized by psa_crypto_init().
1205 * It is implementation-dependent whether a failure to initialize
1206 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001207 */
1208psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1209 psa_hash_operation_t *target_operation);
1210
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001211/**@}*/
1212
Gilles Peskine8c9def32018-02-08 10:02:12 +01001213/** \defgroup MAC Message authentication codes
1214 * @{
1215 */
1216
Gilles Peskine69647a42019-01-14 20:18:12 +01001217/** Calculate the MAC (message authentication code) of a message.
1218 *
1219 * \note To verify the MAC of a message against an
1220 * expected value, use psa_mac_verify() instead.
1221 * Beware that comparing integrity or authenticity data such as
1222 * MAC values with a function such as \c memcmp is risky
1223 * because the time taken by the comparison may leak information
1224 * about the MAC value which could allow an attacker to guess
1225 * a valid MAC and thereby bypass security controls.
1226 *
1227 * \param handle Handle to the key to use for the operation.
1228 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001229 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001230 * \param[in] input Buffer containing the input message.
1231 * \param input_length Size of the \p input buffer in bytes.
1232 * \param[out] mac Buffer where the MAC value is to be written.
1233 * \param mac_size Size of the \p mac buffer in bytes.
1234 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001235 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001236 *
1237 * \retval #PSA_SUCCESS
1238 * Success.
1239 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001240 * \retval #PSA_ERROR_NOT_PERMITTED
1241 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001242 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001243 * \retval #PSA_ERROR_NOT_SUPPORTED
1244 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001245 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1246 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001247 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1248 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1249 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001250 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001251 * \retval #PSA_ERROR_STORAGE_FAILURE
1252 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001253 * \retval #PSA_ERROR_BAD_STATE
1254 * The library has not been previously initialized by psa_crypto_init().
1255 * It is implementation-dependent whether a failure to initialize
1256 * results in this error code.
1257 */
1258psa_status_t psa_mac_compute(psa_key_handle_t handle,
1259 psa_algorithm_t alg,
1260 const uint8_t *input,
1261 size_t input_length,
1262 uint8_t *mac,
1263 size_t mac_size,
1264 size_t *mac_length);
1265
1266/** Calculate the MAC of a message and compare it with a reference value.
1267 *
1268 * \param handle Handle to the key to use for the operation.
1269 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001270 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001271 * \param[in] input Buffer containing the input message.
1272 * \param input_length Size of the \p input buffer in bytes.
1273 * \param[out] mac Buffer containing the expected MAC value.
1274 * \param mac_length Size of the \p mac buffer in bytes.
1275 *
1276 * \retval #PSA_SUCCESS
1277 * The expected MAC is identical to the actual MAC of the input.
1278 * \retval #PSA_ERROR_INVALID_SIGNATURE
1279 * The MAC of the message was calculated successfully, but it
1280 * differs from the expected value.
1281 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001282 * \retval #PSA_ERROR_NOT_PERMITTED
1283 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001284 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001285 * \retval #PSA_ERROR_NOT_SUPPORTED
1286 * \p alg is not supported or is not a MAC algorithm.
1287 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1288 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1289 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001290 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001291 * \retval #PSA_ERROR_STORAGE_FAILURE
1292 * The key could not be retrieved from storage.
1293 * \retval #PSA_ERROR_BAD_STATE
1294 * The library has not been previously initialized by psa_crypto_init().
1295 * It is implementation-dependent whether a failure to initialize
1296 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001297 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001298psa_status_t psa_mac_verify(psa_key_handle_t handle,
1299 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001300 const uint8_t *input,
1301 size_t input_length,
1302 const uint8_t *mac,
Gilles Peskine13faa2d2020-01-30 16:32:21 +01001303 size_t mac_length);
Gilles Peskine69647a42019-01-14 20:18:12 +01001304
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001305/** The type of the state data structure for multipart MAC operations.
1306 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001307 * Before calling any function on a MAC operation object, the application must
1308 * initialize it by any of the following means:
1309 * - Set the structure to all-bits-zero, for example:
1310 * \code
1311 * psa_mac_operation_t operation;
1312 * memset(&operation, 0, sizeof(operation));
1313 * \endcode
1314 * - Initialize the structure to logical zero values, for example:
1315 * \code
1316 * psa_mac_operation_t operation = {0};
1317 * \endcode
1318 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1319 * for example:
1320 * \code
1321 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1322 * \endcode
1323 * - Assign the result of the function psa_mac_operation_init()
1324 * to the structure, for example:
1325 * \code
1326 * psa_mac_operation_t operation;
1327 * operation = psa_mac_operation_init();
1328 * \endcode
1329 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001330 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001331 * make any assumptions about the content of this structure except
1332 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001333typedef struct psa_mac_operation_s psa_mac_operation_t;
1334
Jaeden Amero769ce272019-01-04 11:48:03 +00001335/** \def PSA_MAC_OPERATION_INIT
1336 *
1337 * This macro returns a suitable initializer for a MAC operation object of type
1338 * #psa_mac_operation_t.
1339 */
1340#ifdef __DOXYGEN_ONLY__
1341/* This is an example definition for documentation purposes.
1342 * Implementations should define a suitable value in `crypto_struct.h`.
1343 */
1344#define PSA_MAC_OPERATION_INIT {0}
1345#endif
1346
1347/** Return an initial value for a MAC operation object.
1348 */
1349static psa_mac_operation_t psa_mac_operation_init(void);
1350
Gilles Peskinef45adda2019-01-14 18:29:18 +01001351/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001352 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001353 * This function sets up the calculation of the MAC
1354 * (message authentication code) of a byte string.
1355 * To verify the MAC of a message against an
1356 * expected value, use psa_mac_verify_setup() instead.
1357 *
1358 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001359 * -# Allocate an operation object which will be passed to all the functions
1360 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001361 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001362 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001363 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001364 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1365 * of the message each time. The MAC that is calculated is the MAC
1366 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001367 * -# At the end of the message, call psa_mac_sign_finish() to finish
1368 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001369 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001370 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1371 * operation will need to be reset by a call to psa_mac_abort(). The
1372 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001373 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001374 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001375 * After a successful call to psa_mac_sign_setup(), the application must
1376 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001377 * - A successful call to psa_mac_sign_finish().
1378 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001379 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001380 * \param[in,out] operation The operation object to set up. It must have
1381 * been initialized as per the documentation for
1382 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001383 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001384 * It must remain valid until the operation
1385 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001386 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001387 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001388 *
Gilles Peskine28538492018-07-11 17:34:00 +02001389 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001390 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001391 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001392 * \retval #PSA_ERROR_NOT_PERMITTED
1393 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001394 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001395 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001396 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001397 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1398 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1399 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001400 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001401 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001402 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001403 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001404 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001405 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001406 * The library has not been previously initialized by psa_crypto_init().
1407 * It is implementation-dependent whether a failure to initialize
1408 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001409 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001410psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001411 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001412 psa_algorithm_t alg);
1413
Gilles Peskinef45adda2019-01-14 18:29:18 +01001414/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001415 *
1416 * This function sets up the verification of the MAC
1417 * (message authentication code) of a byte string against an expected value.
1418 *
1419 * The sequence of operations to verify a MAC is as follows:
1420 * -# Allocate an operation object which will be passed to all the functions
1421 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001422 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001423 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001424 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001425 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1426 * of the message each time. The MAC that is calculated is the MAC
1427 * of the concatenation of these messages in order.
1428 * -# At the end of the message, call psa_mac_verify_finish() to finish
1429 * calculating the actual MAC of the message and verify it against
1430 * the expected value.
1431 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001432 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1433 * operation will need to be reset by a call to psa_mac_abort(). The
1434 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001435 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001436 *
1437 * After a successful call to psa_mac_verify_setup(), the application must
1438 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001439 * - A successful call to psa_mac_verify_finish().
1440 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001441 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001442 * \param[in,out] operation The operation object to set up. It must have
1443 * been initialized as per the documentation for
1444 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001445 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001446 * It must remain valid until the operation
1447 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001448 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1449 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001450 *
Gilles Peskine28538492018-07-11 17:34:00 +02001451 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001452 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001453 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001454 * \retval #PSA_ERROR_NOT_PERMITTED
1455 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001456 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001457 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001458 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001459 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1460 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1461 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001462 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001463 * \retval #PSA_ERROR_STORAGE_FAILURE
1464 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001465 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001466 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001467 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001468 * The library has not been previously initialized by psa_crypto_init().
1469 * It is implementation-dependent whether a failure to initialize
1470 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001471 */
1472psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001473 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001474 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001475
Gilles Peskinedcd14942018-07-12 00:30:52 +02001476/** Add a message fragment to a multipart MAC operation.
1477 *
1478 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1479 * before calling this function.
1480 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001481 * If this function returns an error status, the operation enters an error
1482 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001483 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001484 * \param[in,out] operation Active MAC operation.
1485 * \param[in] input Buffer containing the message fragment to add to
1486 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001487 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001488 *
1489 * \retval #PSA_SUCCESS
1490 * Success.
1491 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001492 * The operation state is not valid (it must be active).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001493 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1494 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1495 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001496 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001497 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001498 * \retval #PSA_ERROR_BAD_STATE
1499 * The library has not been previously initialized by psa_crypto_init().
1500 * It is implementation-dependent whether a failure to initialize
1501 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001502 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001503psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1504 const uint8_t *input,
1505 size_t input_length);
1506
Gilles Peskinedcd14942018-07-12 00:30:52 +02001507/** Finish the calculation of the MAC of a message.
1508 *
1509 * The application must call psa_mac_sign_setup() before calling this function.
1510 * This function calculates the MAC of the message formed by concatenating
1511 * the inputs passed to preceding calls to psa_mac_update().
1512 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001513 * When this function returns successfuly, the operation becomes inactive.
1514 * If this function returns an error status, the operation enters an error
1515 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001516 *
1517 * \warning Applications should not call this function if they expect
1518 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1519 * Beware that comparing integrity or authenticity data such as
1520 * MAC values with a function such as \c memcmp is risky
1521 * because the time taken by the comparison may leak information
1522 * about the MAC value which could allow an attacker to guess
1523 * a valid MAC and thereby bypass security controls.
1524 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001525 * \param[in,out] operation Active MAC operation.
1526 * \param[out] mac Buffer where the MAC value is to be written.
1527 * \param mac_size Size of the \p mac buffer in bytes.
1528 * \param[out] mac_length On success, the number of bytes
1529 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001530 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001531 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001532 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001533 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001534 *
1535 * \retval #PSA_SUCCESS
1536 * Success.
1537 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001538 * The operation state is not valid (it must be an active mac sign
1539 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001540 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001541 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001542 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1543 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1544 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1545 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001546 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001547 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001548 * \retval #PSA_ERROR_BAD_STATE
1549 * The library has not been previously initialized by psa_crypto_init().
1550 * It is implementation-dependent whether a failure to initialize
1551 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001552 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001553psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1554 uint8_t *mac,
1555 size_t mac_size,
1556 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001557
Gilles Peskinedcd14942018-07-12 00:30:52 +02001558/** Finish the calculation of the MAC of a message and compare it with
1559 * an expected value.
1560 *
1561 * The application must call psa_mac_verify_setup() before calling this function.
1562 * This function calculates the MAC of the message formed by concatenating
1563 * the inputs passed to preceding calls to psa_mac_update(). It then
1564 * compares the calculated MAC with the expected MAC passed as a
1565 * parameter to this function.
1566 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001567 * When this function returns successfuly, the operation becomes inactive.
1568 * If this function returns an error status, the operation enters an error
1569 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001570 *
1571 * \note Implementations shall make the best effort to ensure that the
1572 * comparison between the actual MAC and the expected MAC is performed
1573 * in constant time.
1574 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001575 * \param[in,out] operation Active MAC operation.
1576 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001577 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001578 *
1579 * \retval #PSA_SUCCESS
1580 * The expected MAC is identical to the actual MAC of the message.
1581 * \retval #PSA_ERROR_INVALID_SIGNATURE
1582 * The MAC of the message was calculated successfully, but it
1583 * differs from the expected MAC.
1584 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001585 * The operation state is not valid (it must be an active mac verify
1586 * operation).
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
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001609 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001610 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001611 * In particular, calling psa_mac_abort() after the operation has been
1612 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1613 * psa_mac_verify_finish() is safe and has no effect.
1614 *
1615 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001616 *
1617 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02001618 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1619 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001620 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001621 * \retval #PSA_ERROR_BAD_STATE
1622 * The library has not been previously initialized by psa_crypto_init().
1623 * It is implementation-dependent whether a failure to initialize
1624 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001625 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001626psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1627
1628/**@}*/
1629
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001630/** \defgroup cipher Symmetric ciphers
1631 * @{
1632 */
1633
Gilles Peskine69647a42019-01-14 20:18:12 +01001634/** Encrypt a message using a symmetric cipher.
1635 *
1636 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001637 * vector). Use the multipart operation interface with a
1638 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001639 *
1640 * \param handle Handle to the key to use for the operation.
1641 * It must remain valid until the operation
1642 * terminates.
1643 * \param alg The cipher algorithm to compute
1644 * (\c PSA_ALG_XXX value such that
1645 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1646 * \param[in] input Buffer containing the message to encrypt.
1647 * \param input_length Size of the \p input buffer in bytes.
1648 * \param[out] output Buffer where the output is to be written.
1649 * The output contains the IV followed by
1650 * the ciphertext proper.
1651 * \param output_size Size of the \p output buffer in bytes.
1652 * \param[out] output_length On success, the number of bytes
1653 * that make up the output.
1654 *
1655 * \retval #PSA_SUCCESS
1656 * Success.
1657 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001658 * \retval #PSA_ERROR_NOT_PERMITTED
1659 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001660 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001661 * \retval #PSA_ERROR_NOT_SUPPORTED
1662 * \p alg is not supported or is not a cipher algorithm.
1663 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1664 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1665 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1666 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001667 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001668 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001669 * \retval #PSA_ERROR_BAD_STATE
1670 * The library has not been previously initialized by psa_crypto_init().
1671 * It is implementation-dependent whether a failure to initialize
1672 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001673 */
1674psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1675 psa_algorithm_t alg,
1676 const uint8_t *input,
1677 size_t input_length,
1678 uint8_t *output,
1679 size_t output_size,
1680 size_t *output_length);
1681
1682/** Decrypt a message using a symmetric cipher.
1683 *
1684 * This function decrypts a message encrypted with a symmetric cipher.
1685 *
1686 * \param handle Handle to the key to use for the operation.
1687 * It must remain valid until the operation
1688 * terminates.
1689 * \param alg The cipher algorithm to compute
1690 * (\c PSA_ALG_XXX value such that
1691 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1692 * \param[in] input Buffer containing the message to decrypt.
1693 * This consists of the IV followed by the
1694 * ciphertext proper.
1695 * \param input_length Size of the \p input buffer in bytes.
1696 * \param[out] output Buffer where the plaintext is to be written.
1697 * \param output_size Size of the \p output buffer in bytes.
1698 * \param[out] output_length On success, the number of bytes
1699 * that make up the output.
1700 *
1701 * \retval #PSA_SUCCESS
1702 * Success.
1703 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001704 * \retval #PSA_ERROR_NOT_PERMITTED
1705 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001706 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001707 * \retval #PSA_ERROR_NOT_SUPPORTED
1708 * \p alg is not supported or is not a cipher algorithm.
1709 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1710 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1711 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1712 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001713 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001714 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001715 * \retval #PSA_ERROR_BAD_STATE
1716 * The library has not been previously initialized by psa_crypto_init().
1717 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001718 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001719 */
1720psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1721 psa_algorithm_t alg,
1722 const uint8_t *input,
1723 size_t input_length,
1724 uint8_t *output,
1725 size_t output_size,
1726 size_t *output_length);
1727
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001728/** The type of the state data structure for multipart cipher operations.
1729 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001730 * Before calling any function on a cipher operation object, the application
1731 * must initialize it by any of the following means:
1732 * - Set the structure to all-bits-zero, for example:
1733 * \code
1734 * psa_cipher_operation_t operation;
1735 * memset(&operation, 0, sizeof(operation));
1736 * \endcode
1737 * - Initialize the structure to logical zero values, for example:
1738 * \code
1739 * psa_cipher_operation_t operation = {0};
1740 * \endcode
1741 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1742 * for example:
1743 * \code
1744 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1745 * \endcode
1746 * - Assign the result of the function psa_cipher_operation_init()
1747 * to the structure, for example:
1748 * \code
1749 * psa_cipher_operation_t operation;
1750 * operation = psa_cipher_operation_init();
1751 * \endcode
1752 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001753 * This is an implementation-defined \c struct. Applications should not
1754 * make any assumptions about the content of this structure except
1755 * as directed by the documentation of a specific implementation. */
1756typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1757
Jaeden Amero5bae2272019-01-04 11:48:27 +00001758/** \def PSA_CIPHER_OPERATION_INIT
1759 *
1760 * This macro returns a suitable initializer for a cipher operation object of
1761 * type #psa_cipher_operation_t.
1762 */
1763#ifdef __DOXYGEN_ONLY__
1764/* This is an example definition for documentation purposes.
1765 * Implementations should define a suitable value in `crypto_struct.h`.
1766 */
1767#define PSA_CIPHER_OPERATION_INIT {0}
1768#endif
1769
1770/** Return an initial value for a cipher operation object.
1771 */
1772static psa_cipher_operation_t psa_cipher_operation_init(void);
1773
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001774/** Set the key for a multipart symmetric encryption operation.
1775 *
1776 * The sequence of operations to encrypt a message with a symmetric cipher
1777 * is as follows:
1778 * -# Allocate an operation object which will be passed to all the functions
1779 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001780 * -# Initialize the operation object with one of the methods described in the
1781 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001782 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001783 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001784 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001785 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001786 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001787 * requires a specific IV value.
1788 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1789 * of the message each time.
1790 * -# Call psa_cipher_finish().
1791 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001792 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1793 * the operation will need to be reset by a call to psa_cipher_abort(). The
1794 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001795 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001796 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001797 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001798 * eventually terminate the operation. The following events terminate an
1799 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001800 * - A successful call to psa_cipher_finish().
1801 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001802 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001803 * \param[in,out] operation The operation object to set up. It must have
1804 * been initialized as per the documentation for
1805 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001806 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001807 * It must remain valid until the operation
1808 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001809 * \param alg The cipher algorithm to compute
1810 * (\c PSA_ALG_XXX value such that
1811 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001812 *
Gilles Peskine28538492018-07-11 17:34:00 +02001813 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001814 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001815 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001816 * \retval #PSA_ERROR_NOT_PERMITTED
1817 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001818 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001819 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001820 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001821 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1822 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1823 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001824 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001825 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001826 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001827 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001828 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001829 * The library has not been previously initialized by psa_crypto_init().
1830 * It is implementation-dependent whether a failure to initialize
1831 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001832 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001833psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001834 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001835 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001836
1837/** Set the key for a multipart symmetric decryption operation.
1838 *
1839 * The sequence of operations to decrypt a message with a symmetric cipher
1840 * is as follows:
1841 * -# Allocate an operation object which will be passed to all the functions
1842 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001843 * -# Initialize the operation object with one of the methods described in the
1844 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001845 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001846 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001847 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001848 * decryption. If the IV is prepended to the ciphertext, you can call
1849 * psa_cipher_update() on a buffer containing the IV followed by the
1850 * beginning of the message.
1851 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1852 * of the message each time.
1853 * -# Call psa_cipher_finish().
1854 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001855 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1856 * the operation will need to be reset by a call to psa_cipher_abort(). The
1857 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001858 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001859 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001860 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001861 * eventually terminate the operation. The following events terminate an
1862 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001863 * - A successful call to psa_cipher_finish().
1864 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001865 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001866 * \param[in,out] operation The operation object to set up. It must have
1867 * been initialized as per the documentation for
1868 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001869 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001870 * It must remain valid until the operation
1871 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001872 * \param alg The cipher algorithm to compute
1873 * (\c PSA_ALG_XXX value such that
1874 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001875 *
Gilles Peskine28538492018-07-11 17:34:00 +02001876 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001877 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001878 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001879 * \retval #PSA_ERROR_NOT_PERMITTED
1880 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001881 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001882 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001883 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001884 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1885 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1886 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001887 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001888 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001889 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001890 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001891 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001892 * The library has not been previously initialized by psa_crypto_init().
1893 * It is implementation-dependent whether a failure to initialize
1894 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001895 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001896psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001897 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001898 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001899
Gilles Peskinedcd14942018-07-12 00:30:52 +02001900/** Generate an IV for a symmetric encryption operation.
1901 *
1902 * This function generates a random IV (initialization vector), nonce
1903 * or initial counter value for the encryption operation as appropriate
1904 * for the chosen algorithm, key type and key size.
1905 *
1906 * The application must call psa_cipher_encrypt_setup() before
1907 * calling this function.
1908 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001909 * If this function returns an error status, the operation enters an error
1910 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001911 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001912 * \param[in,out] operation Active cipher operation.
1913 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001914 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001915 * \param[out] iv_length On success, the number of bytes of the
1916 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001917 *
1918 * \retval #PSA_SUCCESS
1919 * Success.
1920 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001921 * The operation state is not valid (it must be active, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001922 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001923 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001924 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1925 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1926 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001927 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001928 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001929 * \retval #PSA_ERROR_BAD_STATE
1930 * The library has not been previously initialized by psa_crypto_init().
1931 * It is implementation-dependent whether a failure to initialize
1932 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001933 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001934psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001935 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001936 size_t iv_size,
1937 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001938
Gilles Peskinedcd14942018-07-12 00:30:52 +02001939/** Set the IV for a symmetric encryption or decryption operation.
1940 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001941 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001942 * or initial counter value for the encryption or decryption operation.
1943 *
1944 * The application must call psa_cipher_encrypt_setup() before
1945 * calling this function.
1946 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001947 * If this function returns an error status, the operation enters an error
1948 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001949 *
1950 * \note When encrypting, applications should use psa_cipher_generate_iv()
1951 * instead of this function, unless implementing a protocol that requires
1952 * a non-random IV.
1953 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001954 * \param[in,out] operation Active cipher operation.
1955 * \param[in] iv Buffer containing the IV to use.
1956 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001957 *
1958 * \retval #PSA_SUCCESS
1959 * Success.
1960 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001961 * The operation state is not valid (it must be an active cipher
1962 * encrypt operation, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001963 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001964 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001965 * or the chosen algorithm does not use an IV.
1966 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1967 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1968 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001969 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001970 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001971 * \retval #PSA_ERROR_BAD_STATE
1972 * The library has not been previously initialized by psa_crypto_init().
1973 * It is implementation-dependent whether a failure to initialize
1974 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001975 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001976psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001977 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001978 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001979
Gilles Peskinedcd14942018-07-12 00:30:52 +02001980/** Encrypt or decrypt a message fragment in an active cipher operation.
1981 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001982 * Before calling this function, you must:
1983 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1984 * The choice of setup function determines whether this function
1985 * encrypts or decrypts its input.
1986 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1987 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001988 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001989 * If this function returns an error status, the operation enters an error
1990 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001991 *
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
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002004 * The operation state is not valid (it must be active, with an IV set
2005 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02002006 * \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 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002036 * When this function returns successfuly, the operation becomes inactive.
2037 * If this function returns an error status, the operation enters an error
2038 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002039 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002040 * \param[in,out] operation Active cipher operation.
2041 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002042 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002043 * \param[out] output_length On success, the number of bytes
2044 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002045 *
2046 * \retval #PSA_SUCCESS
2047 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02002048 * \retval #PSA_ERROR_INVALID_ARGUMENT
2049 * The total input size passed to this operation is not valid for
2050 * this particular algorithm. For example, the algorithm is a based
2051 * on block cipher and requires a whole number of blocks, but the
2052 * total input size is not a multiple of the block size.
2053 * \retval #PSA_ERROR_INVALID_PADDING
2054 * This is a decryption operation for an algorithm that includes
2055 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002056 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002057 * The operation state is not valid (it must be active, with an IV set
2058 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02002059 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2060 * The size of the \p output buffer is too small.
2061 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2062 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2063 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002064 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002065 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002066 * \retval #PSA_ERROR_BAD_STATE
2067 * The library has not been previously initialized by psa_crypto_init().
2068 * It is implementation-dependent whether a failure to initialize
2069 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002070 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002071psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002072 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002073 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002074 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002075
Gilles Peskinedcd14942018-07-12 00:30:52 +02002076/** Abort a cipher operation.
2077 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002078 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002079 * \p operation structure itself. Once aborted, the operation object
2080 * can be reused for another operation by calling
2081 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002082 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002083 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002084 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002085 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002086 * In particular, calling psa_cipher_abort() after the operation has been
2087 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2088 * is safe and has no effect.
2089 *
2090 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002091 *
2092 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02002093 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2094 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002095 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002096 * \retval #PSA_ERROR_BAD_STATE
2097 * The library has not been previously initialized by psa_crypto_init().
2098 * It is implementation-dependent whether a failure to initialize
2099 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002100 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002101psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2102
2103/**@}*/
2104
Gilles Peskine3b555712018-03-03 21:27:57 +01002105/** \defgroup aead Authenticated encryption with associated data (AEAD)
2106 * @{
2107 */
2108
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002109/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002110 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002111 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002112 * \param alg The AEAD algorithm to compute
2113 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002114 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002115 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002116 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002117 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002118 * but not encrypted.
2119 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002120 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002121 * encrypted.
2122 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002123 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002124 * encrypted data. The additional data is not
2125 * part of this output. For algorithms where the
2126 * encrypted data and the authentication tag
2127 * are defined as separate outputs, the
2128 * authentication tag is appended to the
2129 * encrypted data.
2130 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2131 * This must be at least
2132 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2133 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002134 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002135 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002136 *
Gilles Peskine28538492018-07-11 17:34:00 +02002137 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002138 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002139 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002140 * \retval #PSA_ERROR_NOT_PERMITTED
2141 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002142 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002143 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002144 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002145 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002146 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2147 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002148 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2149 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002150 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002151 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002152 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002153 * The library has not been previously initialized by psa_crypto_init().
2154 * It is implementation-dependent whether a failure to initialize
2155 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002156 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002157psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002158 psa_algorithm_t alg,
2159 const uint8_t *nonce,
2160 size_t nonce_length,
2161 const uint8_t *additional_data,
2162 size_t additional_data_length,
2163 const uint8_t *plaintext,
2164 size_t plaintext_length,
2165 uint8_t *ciphertext,
2166 size_t ciphertext_size,
2167 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002168
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002169/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002170 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002171 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002172 * \param alg The AEAD algorithm to compute
2173 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002174 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002175 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002176 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002177 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002178 * but not encrypted.
2179 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002180 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002181 * encrypted. For algorithms where the
2182 * encrypted data and the authentication tag
2183 * are defined as separate inputs, the buffer
2184 * must contain the encrypted data followed
2185 * by the authentication tag.
2186 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002187 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002188 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2189 * This must be at least
2190 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2191 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002192 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002193 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002194 *
Gilles Peskine28538492018-07-11 17:34:00 +02002195 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002196 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002197 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002198 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002199 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002200 * \retval #PSA_ERROR_NOT_PERMITTED
2201 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002202 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002203 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002204 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002205 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002206 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2207 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002208 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2209 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002210 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002211 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002212 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002213 * The library has not been previously initialized by psa_crypto_init().
2214 * It is implementation-dependent whether a failure to initialize
2215 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002216 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002217psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002218 psa_algorithm_t alg,
2219 const uint8_t *nonce,
2220 size_t nonce_length,
2221 const uint8_t *additional_data,
2222 size_t additional_data_length,
2223 const uint8_t *ciphertext,
2224 size_t ciphertext_length,
2225 uint8_t *plaintext,
2226 size_t plaintext_size,
2227 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002228
Gilles Peskine30a9e412019-01-14 18:36:12 +01002229/** The type of the state data structure for multipart AEAD operations.
2230 *
2231 * Before calling any function on an AEAD operation object, the application
2232 * must initialize it by any of the following means:
2233 * - Set the structure to all-bits-zero, for example:
2234 * \code
2235 * psa_aead_operation_t operation;
2236 * memset(&operation, 0, sizeof(operation));
2237 * \endcode
2238 * - Initialize the structure to logical zero values, for example:
2239 * \code
2240 * psa_aead_operation_t operation = {0};
2241 * \endcode
2242 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2243 * for example:
2244 * \code
2245 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2246 * \endcode
2247 * - Assign the result of the function psa_aead_operation_init()
2248 * to the structure, for example:
2249 * \code
2250 * psa_aead_operation_t operation;
2251 * operation = psa_aead_operation_init();
2252 * \endcode
2253 *
2254 * This is an implementation-defined \c struct. Applications should not
2255 * make any assumptions about the content of this structure except
2256 * as directed by the documentation of a specific implementation. */
2257typedef struct psa_aead_operation_s psa_aead_operation_t;
2258
2259/** \def PSA_AEAD_OPERATION_INIT
2260 *
2261 * This macro returns a suitable initializer for an AEAD operation object of
2262 * type #psa_aead_operation_t.
2263 */
2264#ifdef __DOXYGEN_ONLY__
2265/* This is an example definition for documentation purposes.
2266 * Implementations should define a suitable value in `crypto_struct.h`.
2267 */
2268#define PSA_AEAD_OPERATION_INIT {0}
2269#endif
2270
2271/** Return an initial value for an AEAD operation object.
2272 */
2273static psa_aead_operation_t psa_aead_operation_init(void);
2274
2275/** Set the key for a multipart authenticated encryption operation.
2276 *
2277 * The sequence of operations to encrypt a message with authentication
2278 * is as follows:
2279 * -# Allocate an operation object which will be passed to all the functions
2280 * listed here.
2281 * -# Initialize the operation object with one of the methods described in the
2282 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002283 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002284 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002285 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2286 * inputs to the subsequent calls to psa_aead_update_ad() and
2287 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2288 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002289 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2290 * generate or set the nonce. You should use
2291 * psa_aead_generate_nonce() unless the protocol you are implementing
2292 * requires a specific nonce value.
2293 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2294 * of the non-encrypted additional authenticated data each time.
2295 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002296 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002297 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002298 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002299 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2300 * the operation will need to be reset by a call to psa_aead_abort(). The
2301 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002302 * has been initialized.
2303 *
2304 * After a successful call to psa_aead_encrypt_setup(), the application must
2305 * eventually terminate the operation. The following events terminate an
2306 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002307 * - A successful call to psa_aead_finish().
2308 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002309 *
2310 * \param[in,out] operation The operation object to set up. It must have
2311 * been initialized as per the documentation for
2312 * #psa_aead_operation_t and not yet in use.
2313 * \param handle Handle to the key to use for the operation.
2314 * It must remain valid until the operation
2315 * terminates.
2316 * \param alg The AEAD algorithm to compute
2317 * (\c PSA_ALG_XXX value such that
2318 * #PSA_ALG_IS_AEAD(\p alg) is true).
2319 *
2320 * \retval #PSA_SUCCESS
2321 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002322 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002323 * The operation state is not valid (it must be inactive).
Andrew Thoelke340984b2019-09-11 21:33:41 +01002324 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002325 * \retval #PSA_ERROR_NOT_PERMITTED
2326 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002327 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002328 * \retval #PSA_ERROR_NOT_SUPPORTED
2329 * \p alg is not supported or is not an AEAD algorithm.
2330 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2331 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2332 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002333 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002334 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002335 * \retval #PSA_ERROR_BAD_STATE
2336 * The library has not been previously initialized by psa_crypto_init().
2337 * It is implementation-dependent whether a failure to initialize
2338 * results in this error code.
2339 */
2340psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2341 psa_key_handle_t handle,
2342 psa_algorithm_t alg);
2343
2344/** Set the key for a multipart authenticated decryption operation.
2345 *
2346 * The sequence of operations to decrypt a message with authentication
2347 * is as follows:
2348 * -# Allocate an operation object which will be passed to all the functions
2349 * listed here.
2350 * -# Initialize the operation object with one of the methods described in the
2351 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002352 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002353 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002354 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2355 * inputs to the subsequent calls to psa_aead_update_ad() and
2356 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2357 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002358 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2359 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2360 * of the non-encrypted additional authenticated data each time.
2361 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002362 * of the ciphertext to decrypt each time.
2363 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002364 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002365 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2366 * the operation will need to be reset by a call to psa_aead_abort(). The
2367 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002368 * has been initialized.
2369 *
2370 * After a successful call to psa_aead_decrypt_setup(), the application must
2371 * eventually terminate the operation. The following events terminate an
2372 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002373 * - A successful call to psa_aead_verify().
2374 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002375 *
2376 * \param[in,out] operation The operation object to set up. It must have
2377 * been initialized as per the documentation for
2378 * #psa_aead_operation_t and not yet in use.
2379 * \param handle Handle to the key to use for the operation.
2380 * It must remain valid until the operation
2381 * terminates.
2382 * \param alg The AEAD algorithm to compute
2383 * (\c PSA_ALG_XXX value such that
2384 * #PSA_ALG_IS_AEAD(\p alg) is true).
2385 *
2386 * \retval #PSA_SUCCESS
2387 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002388 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002389 * The operation state is not valid (it must be inactive).
Andrew Thoelke340984b2019-09-11 21:33:41 +01002390 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002391 * \retval #PSA_ERROR_NOT_PERMITTED
2392 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002393 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002394 * \retval #PSA_ERROR_NOT_SUPPORTED
2395 * \p alg is not supported or is not an AEAD algorithm.
2396 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2397 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2398 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002399 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002400 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002401 * \retval #PSA_ERROR_BAD_STATE
2402 * The library has not been previously initialized by psa_crypto_init().
2403 * It is implementation-dependent whether a failure to initialize
2404 * results in this error code.
2405 */
2406psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2407 psa_key_handle_t handle,
2408 psa_algorithm_t alg);
2409
2410/** Generate a random nonce for an authenticated encryption operation.
2411 *
2412 * This function generates a random nonce for the authenticated encryption
2413 * operation with an appropriate size for the chosen algorithm, key type
2414 * and key size.
2415 *
2416 * The application must call psa_aead_encrypt_setup() before
2417 * calling this function.
2418 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002419 * If this function returns an error status, the operation enters an error
2420 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002421 *
2422 * \param[in,out] operation Active AEAD operation.
2423 * \param[out] nonce Buffer where the generated nonce is to be
2424 * written.
2425 * \param nonce_size Size of the \p nonce buffer in bytes.
2426 * \param[out] nonce_length On success, the number of bytes of the
2427 * generated nonce.
2428 *
2429 * \retval #PSA_SUCCESS
2430 * Success.
2431 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002432 * The operation state is not valid (it must be an active aead encrypt
2433 operation, with no nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002434 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2435 * The size of the \p nonce buffer is too small.
2436 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2437 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2438 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002439 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002440 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002441 * \retval #PSA_ERROR_BAD_STATE
2442 * The library has not been previously initialized by psa_crypto_init().
2443 * It is implementation-dependent whether a failure to initialize
2444 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002445 */
2446psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002447 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002448 size_t nonce_size,
2449 size_t *nonce_length);
2450
2451/** Set the nonce for an authenticated encryption or decryption operation.
2452 *
2453 * This function sets the nonce for the authenticated
2454 * encryption or decryption operation.
2455 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002456 * The application must call psa_aead_encrypt_setup() or
2457 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002458 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002459 * If this function returns an error status, the operation enters an error
2460 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002461 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002462 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002463 * instead of this function, unless implementing a protocol that requires
2464 * a non-random IV.
2465 *
2466 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002467 * \param[in] nonce Buffer containing the nonce to use.
2468 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002469 *
2470 * \retval #PSA_SUCCESS
2471 * Success.
2472 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002473 * The operation state is not valid (it must be active, with no nonce
2474 * set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002475 * \retval #PSA_ERROR_INVALID_ARGUMENT
2476 * The size of \p nonce is not acceptable for the chosen algorithm.
2477 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2478 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2479 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002480 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002481 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002482 * \retval #PSA_ERROR_BAD_STATE
2483 * The library has not been previously initialized by psa_crypto_init().
2484 * It is implementation-dependent whether a failure to initialize
2485 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002486 */
2487psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002488 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002489 size_t nonce_length);
2490
Gilles Peskinebc59c852019-01-17 15:26:08 +01002491/** Declare the lengths of the message and additional data for AEAD.
2492 *
2493 * The application must call this function before calling
2494 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2495 * the operation requires it. If the algorithm does not require it,
2496 * calling this function is optional, but if this function is called
2497 * then the implementation must enforce the lengths.
2498 *
2499 * You may call this function before or after setting the nonce with
2500 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2501 *
2502 * - For #PSA_ALG_CCM, calling this function is required.
2503 * - For the other AEAD algorithms defined in this specification, calling
2504 * this function is not required.
2505 * - For vendor-defined algorithm, refer to the vendor documentation.
2506 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002507 * If this function returns an error status, the operation enters an error
2508 * state and must be aborted by calling psa_aead_abort().
2509 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002510 * \param[in,out] operation Active AEAD operation.
2511 * \param ad_length Size of the non-encrypted additional
2512 * authenticated data in bytes.
2513 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2514 *
2515 * \retval #PSA_SUCCESS
2516 * Success.
2517 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002518 * The operation state is not valid (it must be active, and
2519 * psa_aead_update_ad() and psa_aead_update() must not have been
2520 * called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002521 * \retval #PSA_ERROR_INVALID_ARGUMENT
2522 * At least one of the lengths is not acceptable for the chosen
2523 * algorithm.
2524 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2525 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2526 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002527 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002528 * \retval #PSA_ERROR_BAD_STATE
2529 * The library has not been previously initialized by psa_crypto_init().
2530 * It is implementation-dependent whether a failure to initialize
2531 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002532 */
2533psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2534 size_t ad_length,
2535 size_t plaintext_length);
2536
Gilles Peskine30a9e412019-01-14 18:36:12 +01002537/** Pass additional data to an active AEAD operation.
2538 *
2539 * Additional data is authenticated, but not encrypted.
2540 *
2541 * You may call this function multiple times to pass successive fragments
2542 * of the additional data. You may not call this function after passing
2543 * data to encrypt or decrypt with psa_aead_update().
2544 *
2545 * Before calling this function, you must:
2546 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2547 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2548 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002549 * If this function returns an error status, the operation enters an error
2550 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002551 *
2552 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2553 * there is no guarantee that the input is valid. Therefore, until
2554 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2555 * treat the input as untrusted and prepare to undo any action that
2556 * depends on the input if psa_aead_verify() returns an error status.
2557 *
2558 * \param[in,out] operation Active AEAD operation.
2559 * \param[in] input Buffer containing the fragment of
2560 * additional data.
2561 * \param input_length Size of the \p input buffer in bytes.
2562 *
2563 * \retval #PSA_SUCCESS
2564 * Success.
2565 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002566 * The operation state is not valid (it must be active, have a nonce
2567 * set, have lengths set if required by the algorithm, and
2568 * psa_aead_update() must not have been called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002569 * \retval #PSA_ERROR_INVALID_ARGUMENT
2570 * The total input length overflows the additional data length that
2571 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002572 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2573 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2574 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002575 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002576 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002577 * \retval #PSA_ERROR_BAD_STATE
2578 * The library has not been previously initialized by psa_crypto_init().
2579 * It is implementation-dependent whether a failure to initialize
2580 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002581 */
2582psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2583 const uint8_t *input,
2584 size_t input_length);
2585
2586/** Encrypt or decrypt a message fragment in an active AEAD operation.
2587 *
2588 * Before calling this function, you must:
2589 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2590 * The choice of setup function determines whether this function
2591 * encrypts or decrypts its input.
2592 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2593 * 3. Call psa_aead_update_ad() to pass all the additional data.
2594 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002595 * If this function returns an error status, the operation enters an error
2596 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002597 *
2598 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2599 * there is no guarantee that the input is valid. Therefore, until
2600 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2601 * - Do not use the output in any way other than storing it in a
2602 * confidential location. If you take any action that depends
2603 * on the tentative decrypted data, this action will need to be
2604 * undone if the input turns out not to be valid. Furthermore,
2605 * if an adversary can observe that this action took place
2606 * (for example through timing), they may be able to use this
2607 * fact as an oracle to decrypt any message encrypted with the
2608 * same key.
2609 * - In particular, do not copy the output anywhere but to a
2610 * memory or storage space that you have exclusive access to.
2611 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002612 * This function does not require the input to be aligned to any
2613 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002614 * a whole block at a time, it must consume all the input provided, but
2615 * it may delay the end of the corresponding output until a subsequent
2616 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2617 * provides sufficient input. The amount of data that can be delayed
2618 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002619 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002620 * \param[in,out] operation Active AEAD operation.
2621 * \param[in] input Buffer containing the message fragment to
2622 * encrypt or decrypt.
2623 * \param input_length Size of the \p input buffer in bytes.
2624 * \param[out] output Buffer where the output is to be written.
2625 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002626 * This must be at least
2627 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2628 * \p input_length) where \c alg is the
2629 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002630 * \param[out] output_length On success, the number of bytes
2631 * that make up the returned output.
2632 *
2633 * \retval #PSA_SUCCESS
2634 * Success.
2635 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002636 * The operation state is not valid (it must be active, have a nonce
2637 * set, and have lengths set if required by the algorithm).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002638 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2639 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002640 * You can determine a sufficient buffer size by calling
2641 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2642 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002643 * \retval #PSA_ERROR_INVALID_ARGUMENT
2644 * The total length of input to psa_aead_update_ad() so far is
2645 * less than the additional data length that was previously
2646 * specified with psa_aead_set_lengths().
2647 * \retval #PSA_ERROR_INVALID_ARGUMENT
2648 * The total input length overflows the plaintext length that
2649 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002650 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2651 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2652 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002653 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002654 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002655 * \retval #PSA_ERROR_BAD_STATE
2656 * The library has not been previously initialized by psa_crypto_init().
2657 * It is implementation-dependent whether a failure to initialize
2658 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002659 */
2660psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2661 const uint8_t *input,
2662 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002663 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002664 size_t output_size,
2665 size_t *output_length);
2666
2667/** Finish encrypting a message in an AEAD operation.
2668 *
2669 * The operation must have been set up with psa_aead_encrypt_setup().
2670 *
2671 * This function finishes the authentication of the additional data
2672 * formed by concatenating the inputs passed to preceding calls to
2673 * psa_aead_update_ad() with the plaintext formed by concatenating the
2674 * inputs passed to preceding calls to psa_aead_update().
2675 *
2676 * This function has two output buffers:
2677 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002678 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002679 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002680 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002681 * that the operation performs.
2682 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002683 * When this function returns successfuly, the operation becomes inactive.
2684 * If this function returns an error status, the operation enters an error
2685 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002686 *
2687 * \param[in,out] operation Active AEAD operation.
2688 * \param[out] ciphertext Buffer where the last part of the ciphertext
2689 * is to be written.
2690 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002691 * This must be at least
2692 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2693 * \c alg is the algorithm that is being
2694 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002695 * \param[out] ciphertext_length On success, the number of bytes of
2696 * returned ciphertext.
2697 * \param[out] tag Buffer where the authentication tag is
2698 * to be written.
2699 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002700 * This must be at least
2701 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2702 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002703 * \param[out] tag_length On success, the number of bytes
2704 * that make up the returned tag.
2705 *
2706 * \retval #PSA_SUCCESS
2707 * Success.
2708 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002709 * The operation state is not valid (it must be an active encryption
2710 * operation with a nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002711 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002712 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002713 * You can determine a sufficient buffer size for \p ciphertext by
2714 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2715 * where \c alg is the algorithm that is being calculated.
2716 * You can determine a sufficient buffer size for \p tag by
2717 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002718 * \retval #PSA_ERROR_INVALID_ARGUMENT
2719 * The total length of input to psa_aead_update_ad() so far is
2720 * less than the additional data length that was previously
2721 * specified with psa_aead_set_lengths().
2722 * \retval #PSA_ERROR_INVALID_ARGUMENT
2723 * The total length of input to psa_aead_update() so far is
2724 * less than the plaintext length that was previously
2725 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002726 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2727 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2728 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002729 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002730 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002731 * \retval #PSA_ERROR_BAD_STATE
2732 * The library has not been previously initialized by psa_crypto_init().
2733 * It is implementation-dependent whether a failure to initialize
2734 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002735 */
2736psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002737 uint8_t *ciphertext,
2738 size_t ciphertext_size,
2739 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002740 uint8_t *tag,
2741 size_t tag_size,
2742 size_t *tag_length);
2743
2744/** Finish authenticating and decrypting a message in an AEAD operation.
2745 *
2746 * The operation must have been set up with psa_aead_decrypt_setup().
2747 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002748 * This function finishes the authenticated decryption of the message
2749 * components:
2750 *
2751 * - The additional data consisting of the concatenation of the inputs
2752 * passed to preceding calls to psa_aead_update_ad().
2753 * - The ciphertext consisting of the concatenation of the inputs passed to
2754 * preceding calls to psa_aead_update().
2755 * - The tag passed to this function call.
2756 *
2757 * If the authentication tag is correct, this function outputs any remaining
2758 * plaintext and reports success. If the authentication tag is not correct,
2759 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002760 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002761 * When this function returns successfuly, the operation becomes inactive.
2762 * If this function returns an error status, the operation enters an error
2763 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002764 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002765 * \note Implementations shall make the best effort to ensure that the
2766 * comparison between the actual tag and the expected tag is performed
2767 * in constant time.
2768 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002769 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002770 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002771 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002772 * from previous calls to psa_aead_update()
2773 * that could not be processed until the end
2774 * of the input.
2775 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002776 * This must be at least
2777 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2778 * \c alg is the algorithm that is being
2779 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002780 * \param[out] plaintext_length On success, the number of bytes of
2781 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002782 * \param[in] tag Buffer containing the authentication tag.
2783 * \param tag_length Size of the \p tag buffer in bytes.
2784 *
2785 * \retval #PSA_SUCCESS
2786 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002787 * \retval #PSA_ERROR_INVALID_SIGNATURE
2788 * The calculations were successful, but the authentication tag is
2789 * not correct.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002790 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002791 * The operation state is not valid (it must be an active decryption
2792 * operation with a nonce set).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002793 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2794 * The size of the \p plaintext buffer is too small.
2795 * You can determine a sufficient buffer size for \p plaintext by
2796 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2797 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002798 * \retval #PSA_ERROR_INVALID_ARGUMENT
2799 * The total length of input to psa_aead_update_ad() so far is
2800 * less than the additional data length that was previously
2801 * specified with psa_aead_set_lengths().
2802 * \retval #PSA_ERROR_INVALID_ARGUMENT
2803 * The total length of input to psa_aead_update() so far is
2804 * less than the plaintext length that was previously
2805 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002806 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2807 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2808 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002809 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002810 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002811 * \retval #PSA_ERROR_BAD_STATE
2812 * The library has not been previously initialized by psa_crypto_init().
2813 * It is implementation-dependent whether a failure to initialize
2814 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002815 */
2816psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002817 uint8_t *plaintext,
2818 size_t plaintext_size,
2819 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002820 const uint8_t *tag,
2821 size_t tag_length);
2822
2823/** Abort an AEAD operation.
2824 *
2825 * Aborting an operation frees all associated resources except for the
2826 * \p operation structure itself. Once aborted, the operation object
2827 * can be reused for another operation by calling
2828 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2829 *
2830 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002831 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002832 *
2833 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002834 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2835 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002836 *
2837 * \param[in,out] operation Initialized AEAD operation.
2838 *
2839 * \retval #PSA_SUCCESS
Gilles Peskine30a9e412019-01-14 18:36:12 +01002840 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2841 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002842 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002843 * \retval #PSA_ERROR_BAD_STATE
2844 * The library has not been previously initialized by psa_crypto_init().
2845 * It is implementation-dependent whether a failure to initialize
2846 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002847 */
2848psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2849
Gilles Peskine3b555712018-03-03 21:27:57 +01002850/**@}*/
2851
Gilles Peskine20035e32018-02-03 22:44:14 +01002852/** \defgroup asymmetric Asymmetric cryptography
2853 * @{
2854 */
2855
2856/**
2857 * \brief Sign a hash or short message with a private key.
2858 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002859 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002860 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002861 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2862 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2863 * to determine the hash algorithm to use.
2864 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002865 * \param handle Handle to the key to use for the operation.
2866 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002867 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002868 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002869 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002870 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002871 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002872 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002873 * \param[out] signature_length On success, the number of bytes
2874 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002875 *
Gilles Peskine28538492018-07-11 17:34:00 +02002876 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002877 * \retval #PSA_ERROR_INVALID_HANDLE
2878 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002879 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002880 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002881 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002882 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002883 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002884 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002885 * \retval #PSA_ERROR_NOT_SUPPORTED
2886 * \retval #PSA_ERROR_INVALID_ARGUMENT
2887 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2888 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2889 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002890 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002891 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002892 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002893 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002894 * The library has not been previously initialized by psa_crypto_init().
2895 * It is implementation-dependent whether a failure to initialize
2896 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002897 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002898psa_status_t psa_sign_hash(psa_key_handle_t handle,
2899 psa_algorithm_t alg,
2900 const uint8_t *hash,
2901 size_t hash_length,
2902 uint8_t *signature,
2903 size_t signature_size,
2904 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002905
2906/**
2907 * \brief Verify the signature a hash or short message using a public key.
2908 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002909 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002910 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002911 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2912 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2913 * to determine the hash algorithm to use.
2914 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002915 * \param handle Handle to the key to use for the operation.
2916 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002917 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002918 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002919 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002920 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002921 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002922 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002923 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002924 *
Gilles Peskine28538492018-07-11 17:34:00 +02002925 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002926 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002927 * \retval #PSA_ERROR_INVALID_HANDLE
2928 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002929 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002930 * The calculation was perfomed successfully, but the passed
2931 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002932 * \retval #PSA_ERROR_NOT_SUPPORTED
2933 * \retval #PSA_ERROR_INVALID_ARGUMENT
2934 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2935 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2936 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002937 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002938 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002939 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002940 * The library has not been previously initialized by psa_crypto_init().
2941 * It is implementation-dependent whether a failure to initialize
2942 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002943 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002944psa_status_t psa_verify_hash(psa_key_handle_t handle,
2945 psa_algorithm_t alg,
2946 const uint8_t *hash,
2947 size_t hash_length,
2948 const uint8_t *signature,
2949 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002950
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002951/**
2952 * \brief Encrypt a short message with a public key.
2953 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002954 * \param handle Handle to the key to use for the operation.
2955 * It must be a public key or an asymmetric
2956 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002957 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002958 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002959 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002960 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002961 * \param[in] salt A salt or label, if supported by the
2962 * encryption algorithm.
2963 * If the algorithm does not support a
2964 * salt, pass \c NULL.
2965 * If the algorithm supports an optional
2966 * salt and you do not want to pass a salt,
2967 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002968 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002969 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2970 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002971 * \param salt_length Size of the \p salt buffer in bytes.
2972 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002973 * \param[out] output Buffer where the encrypted message is to
2974 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002975 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002976 * \param[out] output_length On success, the number of bytes
2977 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002978 *
Gilles Peskine28538492018-07-11 17:34:00 +02002979 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002980 * \retval #PSA_ERROR_INVALID_HANDLE
2981 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002982 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002983 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002984 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002985 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002986 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002987 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002988 * \retval #PSA_ERROR_NOT_SUPPORTED
2989 * \retval #PSA_ERROR_INVALID_ARGUMENT
2990 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2991 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2992 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002993 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002994 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002995 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002996 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002997 * The library has not been previously initialized by psa_crypto_init().
2998 * It is implementation-dependent whether a failure to initialize
2999 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003000 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003001psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003002 psa_algorithm_t alg,
3003 const uint8_t *input,
3004 size_t input_length,
3005 const uint8_t *salt,
3006 size_t salt_length,
3007 uint8_t *output,
3008 size_t output_size,
3009 size_t *output_length);
3010
3011/**
3012 * \brief Decrypt a short message with a private key.
3013 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003014 * \param handle Handle to the key to use for the operation.
3015 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003016 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003017 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003018 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003019 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003020 * \param[in] salt A salt or label, if supported by the
3021 * encryption algorithm.
3022 * If the algorithm does not support a
3023 * salt, pass \c NULL.
3024 * If the algorithm supports an optional
3025 * salt and you do not want to pass a salt,
3026 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003027 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003028 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3029 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003030 * \param salt_length Size of the \p salt buffer in bytes.
3031 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003032 * \param[out] output Buffer where the decrypted message is to
3033 * be written.
3034 * \param output_size Size of the \c output buffer in bytes.
3035 * \param[out] output_length On success, the number of bytes
3036 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003037 *
Gilles Peskine28538492018-07-11 17:34:00 +02003038 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003039 * \retval #PSA_ERROR_INVALID_HANDLE
3040 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003041 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003042 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003043 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003044 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003045 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003046 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02003047 * \retval #PSA_ERROR_NOT_SUPPORTED
3048 * \retval #PSA_ERROR_INVALID_ARGUMENT
3049 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3050 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3051 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003052 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003053 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003054 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3055 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003056 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003057 * The library has not been previously initialized by psa_crypto_init().
3058 * It is implementation-dependent whether a failure to initialize
3059 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003060 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003061psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003062 psa_algorithm_t alg,
3063 const uint8_t *input,
3064 size_t input_length,
3065 const uint8_t *salt,
3066 size_t salt_length,
3067 uint8_t *output,
3068 size_t output_size,
3069 size_t *output_length);
3070
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003071/**@}*/
3072
Gilles Peskine35675b62019-05-16 17:26:11 +02003073/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003074 * @{
3075 */
3076
Gilles Peskine35675b62019-05-16 17:26:11 +02003077/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003078 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003079 * Before calling any function on a key derivation operation object, the
3080 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003081 * - Set the structure to all-bits-zero, for example:
3082 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003083 * psa_key_derivation_operation_t operation;
3084 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003085 * \endcode
3086 * - Initialize the structure to logical zero values, for example:
3087 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003088 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003089 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003090 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003091 * for example:
3092 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003093 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003094 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003095 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003096 * to the structure, for example:
3097 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003098 * psa_key_derivation_operation_t operation;
3099 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003100 * \endcode
3101 *
3102 * This is an implementation-defined \c struct. Applications should not
3103 * make any assumptions about the content of this structure except
3104 * as directed by the documentation of a specific implementation.
3105 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003106typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003107
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003108/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003109 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003110 * This macro returns a suitable initializer for a key derivation operation
3111 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003112 */
3113#ifdef __DOXYGEN_ONLY__
3114/* This is an example definition for documentation purposes.
3115 * Implementations should define a suitable value in `crypto_struct.h`.
3116 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003117#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003118#endif
3119
Gilles Peskine35675b62019-05-16 17:26:11 +02003120/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003121 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003122static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003123
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003124/** Set up a key derivation operation.
3125 *
3126 * A key derivation algorithm takes some inputs and uses them to generate
3127 * a byte stream in a deterministic way.
3128 * This byte stream can be used to produce keys and other
3129 * cryptographic material.
3130 *
3131 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003132 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3133 * -# Call psa_key_derivation_setup() to select the algorithm.
3134 * -# Provide the inputs for the key derivation by calling
3135 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3136 * as appropriate. Which inputs are needed, in what order, and whether
3137 * they may be keys and if so of what type depends on the algorithm.
3138 * -# Optionally set the operation's maximum capacity with
3139 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3140 * of or after providing inputs. For some algorithms, this step is mandatory
3141 * because the output depends on the maximum capacity.
3142 * -# To derive a key, call psa_key_derivation_output_key().
3143 * To derive a byte string for a different purpose, call
3144 * psa_key_derivation_output_bytes().
3145 * Successive calls to these functions use successive output bytes
3146 * calculated by the key derivation algorithm.
3147 * -# Clean up the key derivation operation object with
3148 * psa_key_derivation_abort().
3149 *
3150 * If this function returns an error, the key derivation operation object is
3151 * not changed.
3152 *
3153 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3154 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003155 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003156 * Implementations must reject an attempt to derive a key of size 0.
3157 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003158 * \param[in,out] operation The key derivation operation object
3159 * to set up. It must
3160 * have been initialized but not set up yet.
3161 * \param alg The key derivation algorithm to compute
3162 * (\c PSA_ALG_XXX value such that
3163 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3164 *
3165 * \retval #PSA_SUCCESS
3166 * Success.
3167 * \retval #PSA_ERROR_INVALID_ARGUMENT
3168 * \c alg is not a key derivation algorithm.
3169 * \retval #PSA_ERROR_NOT_SUPPORTED
3170 * \c alg is not supported or is not a key derivation algorithm.
3171 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3172 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3173 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003174 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003175 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003176 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003177 * The operation state is not valid (it must be inactive).
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003178 * \retval #PSA_ERROR_BAD_STATE
3179 * The library has not been previously initialized by psa_crypto_init().
3180 * It is implementation-dependent whether a failure to initialize
3181 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003182 */
3183psa_status_t psa_key_derivation_setup(
3184 psa_key_derivation_operation_t *operation,
3185 psa_algorithm_t alg);
3186
Gilles Peskine35675b62019-05-16 17:26:11 +02003187/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003188 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003189 * The capacity of a key derivation is the maximum number of bytes that it can
3190 * return. When you get *N* bytes of output from a key derivation operation,
3191 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003192 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003193 * \param[in] operation The operation to query.
3194 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003195 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003196 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003197 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003198 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003199 * The operation state is not valid (it must be active).
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003200 * \retval #PSA_ERROR_HARDWARE_FAILURE
3201 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003202 * \retval #PSA_ERROR_BAD_STATE
3203 * The library has not been previously initialized by psa_crypto_init().
3204 * It is implementation-dependent whether a failure to initialize
3205 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003206 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003207psa_status_t psa_key_derivation_get_capacity(
3208 const psa_key_derivation_operation_t *operation,
3209 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003210
Gilles Peskine35675b62019-05-16 17:26:11 +02003211/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003212 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003213 * The capacity of a key derivation operation is the maximum number of bytes
3214 * that the key derivation operation can return from this point onwards.
3215 *
3216 * \param[in,out] operation The key derivation operation object to modify.
3217 * \param capacity The new capacity of the operation.
3218 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003219 * current capacity.
3220 *
3221 * \retval #PSA_SUCCESS
3222 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003223 * \p capacity is larger than the operation's current capacity.
3224 * In this case, the operation object remains valid and its capacity
3225 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003226 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003227 * The operation state is not valid (it must be active).
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003228 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003229 * \retval #PSA_ERROR_HARDWARE_FAILURE
3230 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003231 * \retval #PSA_ERROR_BAD_STATE
3232 * The library has not been previously initialized by psa_crypto_init().
3233 * It is implementation-dependent whether a failure to initialize
3234 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003235 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003236psa_status_t psa_key_derivation_set_capacity(
3237 psa_key_derivation_operation_t *operation,
3238 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003239
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003240/** Use the maximum possible capacity for a key derivation operation.
3241 *
3242 * Use this value as the capacity argument when setting up a key derivation
3243 * to indicate that the operation should have the maximum possible capacity.
3244 * The value of the maximum possible capacity depends on the key derivation
3245 * algorithm.
3246 */
3247#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3248
3249/** Provide an input for key derivation or key agreement.
3250 *
3251 * Which inputs are required and in what order depends on the algorithm.
3252 * Refer to the documentation of each key derivation or key agreement
3253 * algorithm for information.
3254 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003255 * This function passes direct inputs, which is usually correct for
3256 * non-secret inputs. To pass a secret input, which should be in a key
3257 * object, call psa_key_derivation_input_key() instead of this function.
3258 * Refer to the documentation of individual step types
3259 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3260 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003261 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003262 * If this function returns an error status, the operation enters an error
3263 * state and must be aborted by calling psa_key_derivation_abort().
3264 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003265 * \param[in,out] operation The key derivation operation object to use.
3266 * It must have been set up with
3267 * psa_key_derivation_setup() and must not
3268 * have produced any output yet.
3269 * \param step Which step the input data is for.
3270 * \param[in] data Input data to use.
3271 * \param data_length Size of the \p data buffer in bytes.
3272 *
3273 * \retval #PSA_SUCCESS
3274 * Success.
3275 * \retval #PSA_ERROR_INVALID_ARGUMENT
3276 * \c step is not compatible with the operation's algorithm.
3277 * \retval #PSA_ERROR_INVALID_ARGUMENT
3278 * \c step does not allow direct inputs.
3279 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3280 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3281 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003282 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003283 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003284 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003285 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003286 * \retval #PSA_ERROR_BAD_STATE
3287 * The library has not been previously initialized by psa_crypto_init().
3288 * It is implementation-dependent whether a failure to initialize
3289 * results in this error code.
3290 */
3291psa_status_t psa_key_derivation_input_bytes(
3292 psa_key_derivation_operation_t *operation,
3293 psa_key_derivation_step_t step,
3294 const uint8_t *data,
3295 size_t data_length);
3296
3297/** Provide an input for key derivation in the form of a key.
3298 *
3299 * Which inputs are required and in what order depends on the algorithm.
3300 * Refer to the documentation of each key derivation or key agreement
3301 * algorithm for information.
3302 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003303 * This function obtains input from a key object, which is usually correct for
3304 * secret inputs or for non-secret personalization strings kept in the key
3305 * store. To pass a non-secret parameter which is not in the key store,
3306 * call psa_key_derivation_input_bytes() instead of this function.
3307 * Refer to the documentation of individual step types
3308 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3309 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003310 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003311 * If this function returns an error status, the operation enters an error
3312 * state and must be aborted by calling psa_key_derivation_abort().
3313 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003314 * \param[in,out] operation The key derivation operation object to use.
3315 * It must have been set up with
3316 * psa_key_derivation_setup() and must not
3317 * have produced any output yet.
3318 * \param step Which step the input data is for.
3319 * \param handle Handle to the key. It must have an
3320 * appropriate type for \p step and must
3321 * allow the usage #PSA_KEY_USAGE_DERIVE.
3322 *
3323 * \retval #PSA_SUCCESS
3324 * Success.
3325 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003326 * \retval #PSA_ERROR_NOT_PERMITTED
3327 * \retval #PSA_ERROR_INVALID_ARGUMENT
3328 * \c step is not compatible with the operation's algorithm.
3329 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine224b0d62019-09-23 18:13:17 +02003330 * \c step does not allow key inputs of the given type
3331 * or does not allow key inputs at all.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003332 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3333 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3334 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003335 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003336 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003337 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003338 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003339 * \retval #PSA_ERROR_BAD_STATE
3340 * The library has not been previously initialized by psa_crypto_init().
3341 * It is implementation-dependent whether a failure to initialize
3342 * results in this error code.
3343 */
3344psa_status_t psa_key_derivation_input_key(
3345 psa_key_derivation_operation_t *operation,
3346 psa_key_derivation_step_t step,
3347 psa_key_handle_t handle);
3348
3349/** Perform a key agreement and use the shared secret as input to a key
3350 * derivation.
3351 *
3352 * A key agreement algorithm takes two inputs: a private key \p private_key
3353 * a public key \p peer_key.
3354 * The result of this function is passed as input to a key derivation.
3355 * The output of this key derivation can be extracted by reading from the
3356 * resulting operation to produce keys and other cryptographic material.
3357 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003358 * If this function returns an error status, the operation enters an error
3359 * state and must be aborted by calling psa_key_derivation_abort().
3360 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003361 * \param[in,out] operation The key derivation operation object to use.
3362 * It must have been set up with
3363 * psa_key_derivation_setup() with a
3364 * key agreement and derivation algorithm
3365 * \c alg (\c PSA_ALG_XXX value such that
3366 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3367 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3368 * is false).
3369 * The operation must be ready for an
3370 * input of the type given by \p step.
3371 * \param step Which step the input data is for.
3372 * \param private_key Handle to the private key to use.
3373 * \param[in] peer_key Public key of the peer. The peer key must be in the
3374 * same format that psa_import_key() accepts for the
3375 * public key type corresponding to the type of
3376 * private_key. That is, this function performs the
3377 * equivalent of
3378 * #psa_import_key(...,
3379 * `peer_key`, `peer_key_length`) where
3380 * with key attributes indicating the public key
3381 * type corresponding to the type of `private_key`.
3382 * For example, for EC keys, this means that peer_key
3383 * is interpreted as a point on the curve that the
3384 * private key is on. The standard formats for public
3385 * keys are documented in the documentation of
3386 * psa_export_public_key().
3387 * \param peer_key_length Size of \p peer_key in bytes.
3388 *
3389 * \retval #PSA_SUCCESS
3390 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01003391 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003392 * The operation state is not valid for this key agreement \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003393 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003394 * \retval #PSA_ERROR_NOT_PERMITTED
3395 * \retval #PSA_ERROR_INVALID_ARGUMENT
3396 * \c private_key is not compatible with \c alg,
3397 * or \p peer_key is not valid for \c alg or not compatible with
3398 * \c private_key.
3399 * \retval #PSA_ERROR_NOT_SUPPORTED
3400 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskine224b0d62019-09-23 18:13:17 +02003401 * \retval #PSA_ERROR_INVALID_ARGUMENT
3402 * \c step does not allow an input resulting from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003403 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3404 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3405 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003406 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003407 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003408 * \retval #PSA_ERROR_BAD_STATE
3409 * The library has not been previously initialized by psa_crypto_init().
3410 * It is implementation-dependent whether a failure to initialize
3411 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003412 */
3413psa_status_t psa_key_derivation_key_agreement(
3414 psa_key_derivation_operation_t *operation,
3415 psa_key_derivation_step_t step,
3416 psa_key_handle_t private_key,
3417 const uint8_t *peer_key,
3418 size_t peer_key_length);
3419
Gilles Peskine35675b62019-05-16 17:26:11 +02003420/** Read some data from a 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 and
3423 * return those bytes.
3424 * If you view the key derivation's output as a stream of bytes, this
3425 * function destructively reads the requested number of bytes from the
3426 * stream.
3427 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003428 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003429 * If this function returns an error status other than
3430 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003431 * state and must be aborted by calling psa_key_derivation_abort().
3432 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003433 * \param[in,out] operation The key derivation operation object to read from.
3434 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003435 * \param output_length Number of bytes to output.
3436 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003437 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003438 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003439 * The operation's capacity was less than
3440 * \p output_length bytes. Note that in this case,
3441 * no output is written to the output buffer.
3442 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003443 * subsequent calls to this function will not
3444 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003445 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003446 * The operation state is not valid (it must be active and completed
3447 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003448 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3449 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3450 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003451 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003452 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003453 * \retval #PSA_ERROR_BAD_STATE
3454 * The library has not been previously initialized by psa_crypto_init().
3455 * It is implementation-dependent whether a failure to initialize
3456 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003457 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003458psa_status_t psa_key_derivation_output_bytes(
3459 psa_key_derivation_operation_t *operation,
3460 uint8_t *output,
3461 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003462
Gilles Peskine35675b62019-05-16 17:26:11 +02003463/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003464 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003465 * This function calculates output bytes from a key derivation algorithm
3466 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003467 * The key's location, usage policy, type and size are taken from
3468 * \p attributes.
3469 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003470 * If you view the key derivation's output as a stream of bytes, this
3471 * function destructively reads as many bytes as required from the
3472 * stream.
3473 * The operation's capacity decreases by the number of bytes read.
3474 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003475 * If this function returns an error status other than
3476 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003477 * state and must be aborted by calling psa_key_derivation_abort().
3478 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003479 * How much output is produced and consumed from the operation, and how
3480 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003481 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003482 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003483 * of a given size, this function is functionally equivalent to
3484 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003485 * and passing the resulting output to #psa_import_key.
3486 * However, this function has a security benefit:
3487 * if the implementation provides an isolation boundary then
3488 * the key material is not exposed outside the isolation boundary.
3489 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003490 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003491 * The following key types defined in this specification follow this scheme:
3492 *
3493 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003494 * - #PSA_KEY_TYPE_ARC4;
3495 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003496 * - #PSA_KEY_TYPE_DERIVE;
3497 * - #PSA_KEY_TYPE_HMAC.
3498 *
3499 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003500 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003501 * Montgomery curve), this function always draws a byte string whose
3502 * length is determined by the curve, and sets the mandatory bits
3503 * accordingly. That is:
3504 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01003505 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003506 * string and process it as specified in RFC 7748 &sect;5.
Paul Elliott8ff510a2020-06-02 17:19:28 +01003507 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003508 * string and process it as specified in RFC 7748 &sect;5.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003509 *
3510 * - For key types for which the key is represented by a single sequence of
3511 * \p bits bits with constraints as to which bit sequences are acceptable,
3512 * this function draws a byte string of length (\p bits / 8) bytes rounded
3513 * up to the nearest whole number of bytes. If the resulting byte string
3514 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3515 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003516 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003517 * for the output produced by psa_export_key().
3518 * The following key types defined in this specification follow this scheme:
3519 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003520 * - #PSA_KEY_TYPE_DES.
3521 * Force-set the parity bits, but discard forbidden weak keys.
3522 * For 2-key and 3-key triple-DES, the three keys are generated
3523 * successively (for example, for 3-key triple-DES,
3524 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3525 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003526 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003527 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003528 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003529 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003530 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003531 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003532 * Weierstrass curve).
3533 * For these key types, interpret the byte string as integer
3534 * in big-endian order. Discard it if it is not in the range
3535 * [0, *N* - 2] where *N* is the boundary of the private key domain
3536 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003537 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003538 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003539 * This method allows compliance to NIST standards, specifically
3540 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003541 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3542 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3543 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3544 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003545 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003546 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003547 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003548 * implementation-defined.
3549 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003550 * In all cases, the data that is read is discarded from the operation.
3551 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003552 *
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003553 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3554 * the input to that step must be provided with psa_key_derivation_input_key().
3555 * Future versions of this specification may include additional restrictions
3556 * on the derived key based on the attributes and strength of the secret key.
3557 *
Gilles Peskine20628592019-04-19 19:29:50 +02003558 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003559 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003560 * \param[out] handle On success, a handle to the newly created key.
3561 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003562 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003563 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003564 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003565 * If the key is persistent, the key material and the key's metadata
3566 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003567 * \retval #PSA_ERROR_ALREADY_EXISTS
3568 * This is an attempt to create a persistent key, and there is
3569 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003570 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003571 * There was not enough data to create the desired key.
3572 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003573 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003574 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003575 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003576 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003577 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003578 * \retval #PSA_ERROR_INVALID_ARGUMENT
3579 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003580 * \retval #PSA_ERROR_NOT_PERMITTED
3581 * The #PSA_KEY_DERIVATION_INPUT_SECRET input was not provided through
3582 * a key.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003583 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003584 * The operation state is not valid (it must be active and completed
3585 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003586 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3587 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3588 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3589 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003590 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003591 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003592 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003593 * The library has not been previously initialized by psa_crypto_init().
3594 * It is implementation-dependent whether a failure to initialize
3595 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003596 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003597psa_status_t psa_key_derivation_output_key(
3598 const psa_key_attributes_t *attributes,
3599 psa_key_derivation_operation_t *operation,
3600 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003601
Gilles Peskine35675b62019-05-16 17:26:11 +02003602/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003603 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003604 * Aborting an operation frees all associated resources except for the \c
3605 * operation structure itself. Once aborted, the operation object can be reused
3606 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003607 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003608 * This function may be called at any time after the operation
3609 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003610 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003611 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3612 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003613 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003614 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003615 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003616 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003617 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3618 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003619 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003620 * \retval #PSA_ERROR_BAD_STATE
3621 * The library has not been previously initialized by psa_crypto_init().
3622 * It is implementation-dependent whether a failure to initialize
3623 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003624 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003625psa_status_t psa_key_derivation_abort(
3626 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003627
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003628/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003629 *
3630 * \warning The raw result of a key agreement algorithm such as finite-field
3631 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3632 * not be used directly as key material. It should instead be passed as
3633 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003634 * a key derivation, use psa_key_derivation_key_agreement() and other
3635 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003636 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003637 * \param alg The key agreement algorithm to compute
3638 * (\c PSA_ALG_XXX value such that
3639 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3640 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003641 * \param private_key Handle to the private key to use.
3642 * \param[in] peer_key Public key of the peer. It must be
3643 * in the same format that psa_import_key()
3644 * accepts. The standard formats for public
3645 * keys are documented in the documentation
3646 * of psa_export_public_key().
3647 * \param peer_key_length Size of \p peer_key in bytes.
3648 * \param[out] output Buffer where the decrypted message is to
3649 * be written.
3650 * \param output_size Size of the \c output buffer in bytes.
3651 * \param[out] output_length On success, the number of bytes
3652 * that make up the returned output.
3653 *
3654 * \retval #PSA_SUCCESS
3655 * Success.
3656 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003657 * \retval #PSA_ERROR_NOT_PERMITTED
3658 * \retval #PSA_ERROR_INVALID_ARGUMENT
3659 * \p alg is not a key agreement algorithm
3660 * \retval #PSA_ERROR_INVALID_ARGUMENT
3661 * \p private_key is not compatible with \p alg,
3662 * or \p peer_key is not valid for \p alg or not compatible with
3663 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003664 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3665 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003666 * \retval #PSA_ERROR_NOT_SUPPORTED
3667 * \p alg is not a supported key agreement algorithm.
3668 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3669 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3670 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003671 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003672 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003673 * \retval #PSA_ERROR_BAD_STATE
3674 * The library has not been previously initialized by psa_crypto_init().
3675 * It is implementation-dependent whether a failure to initialize
3676 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003677 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003678psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3679 psa_key_handle_t private_key,
3680 const uint8_t *peer_key,
3681 size_t peer_key_length,
3682 uint8_t *output,
3683 size_t output_size,
3684 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003685
Gilles Peskineea0fb492018-07-12 17:17:20 +02003686/**@}*/
3687
Gilles Peskineedd76872018-07-20 17:42:05 +02003688/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003689 * @{
3690 */
3691
3692/**
3693 * \brief Generate random bytes.
3694 *
3695 * \warning This function **can** fail! Callers MUST check the return status
3696 * and MUST NOT use the content of the output buffer if the return
3697 * status is not #PSA_SUCCESS.
3698 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003699 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003700 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003701 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003702 * \param output_size Number of bytes to generate and output.
3703 *
Gilles Peskine28538492018-07-11 17:34:00 +02003704 * \retval #PSA_SUCCESS
3705 * \retval #PSA_ERROR_NOT_SUPPORTED
3706 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003707 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003708 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3709 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003710 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003711 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003712 * The library has not been previously initialized by psa_crypto_init().
3713 * It is implementation-dependent whether a failure to initialize
3714 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003715 */
3716psa_status_t psa_generate_random(uint8_t *output,
3717 size_t output_size);
3718
3719/**
3720 * \brief Generate a key or key pair.
3721 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003722 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02003723 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003724 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003725 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003726 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003727 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003728 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003729 * the public exponent is 65537.
3730 * The modulus is a product of two probabilistic primes
3731 * between 2^{n-1} and 2^n where n is the bit size specified in the
3732 * attributes.
3733 *
Gilles Peskine20628592019-04-19 19:29:50 +02003734 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003735 * \param[out] handle On success, a handle to the newly created key.
3736 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003737 *
Gilles Peskine28538492018-07-11 17:34:00 +02003738 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003739 * Success.
3740 * If the key is persistent, the key material and the key's metadata
3741 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003742 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003743 * This is an attempt to create a persistent key, and there is
3744 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003745 * \retval #PSA_ERROR_NOT_SUPPORTED
3746 * \retval #PSA_ERROR_INVALID_ARGUMENT
3747 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3748 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3749 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3750 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003751 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003752 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3753 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003754 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003755 * The library has not been previously initialized by psa_crypto_init().
3756 * It is implementation-dependent whether a failure to initialize
3757 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003758 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003759psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003760 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003761
3762/**@}*/
3763
Gilles Peskinee59236f2018-01-27 23:32:46 +01003764#ifdef __cplusplus
3765}
3766#endif
3767
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003768/* The file "crypto_sizes.h" contains definitions for size calculation
3769 * macros whose definitions are implementation-specific. */
3770#include "crypto_sizes.h"
3771
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003772/* The file "crypto_struct.h" contains definitions for
3773 * implementation-specific structs that are declared above. */
3774#include "crypto_struct.h"
3775
3776/* The file "crypto_extra.h" contains vendor-specific definitions. This
3777 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003778#include "crypto_extra.h"
3779
3780#endif /* PSA_CRYPTO_H */