blob: 5ba16b987ff35cfa9d4d156fc5e7baa6d9208bd0 [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/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02006 * Copyright The Mbed TLS Contributors
Jaeden Amerocab54942018-07-25 13:26:13 +01007 * 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 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200149 * \param[out] attributes The attribute structure to write to.
150 * \param key The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200151 */
Ronald Cron71016a92020-08-28 19:01:50 +0200152static void psa_set_key_id( psa_key_attributes_t *attributes,
153 mbedtls_svc_key_id_t key );
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200154
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 */
Ronald Cron71016a92020-08-28 19:01:50 +0200195static mbedtls_svc_key_id_t psa_get_key_id(
196 const psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200197
Gilles Peskine20628592019-04-19 19:29:50 +0200198/** Retrieve the lifetime from key attributes.
199 *
200 * This function may be declared as `static` (i.e. without external
201 * linkage). This function may be provided as a function-like macro,
202 * but in this case it must evaluate its argument exactly once.
203 *
204 * \param[in] attributes The key attribute structure to query.
205 *
206 * \return The lifetime value stored in the attribute structure.
207 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200208static psa_key_lifetime_t psa_get_key_lifetime(
209 const psa_key_attributes_t *attributes);
210
Gilles Peskine20628592019-04-19 19:29:50 +0200211/** Declare usage flags for a key.
212 *
213 * Usage flags are part of a key's usage policy. They encode what
214 * kind of operations are permitted on the key. For more details,
215 * refer to the documentation of the type #psa_key_usage_t.
216 *
217 * This function overwrites any usage flags
218 * previously set in \p attributes.
219 *
220 * This function may be declared as `static` (i.e. without external
221 * linkage). This function may be provided as a function-like macro,
222 * but in this case it must evaluate each of its arguments exactly once.
223 *
224 * \param[out] attributes The attribute structure to write to.
225 * \param usage_flags The usage flags to write.
226 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200227static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
228 psa_key_usage_t usage_flags);
229
Gilles Peskine20628592019-04-19 19:29:50 +0200230/** Retrieve the usage flags from key attributes.
231 *
232 * This function may be declared as `static` (i.e. without external
233 * linkage). This function may be provided as a function-like macro,
234 * but in this case it must evaluate its argument exactly once.
235 *
236 * \param[in] attributes The key attribute structure to query.
237 *
238 * \return The usage flags stored in the attribute structure.
239 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200240static psa_key_usage_t psa_get_key_usage_flags(
241 const psa_key_attributes_t *attributes);
242
Gilles Peskine20628592019-04-19 19:29:50 +0200243/** Declare the permitted algorithm policy for a key.
244 *
245 * The permitted algorithm policy of a key encodes which algorithm or
Gilles Peskinea170d922019-09-12 16:59:37 +0200246 * algorithms are permitted to be used with this key. The following
247 * algorithm policies are supported:
248 * - 0 does not allow any cryptographic operation with the key. The key
249 * may be used for non-cryptographic actions such as exporting (if
250 * permitted by the usage flags).
251 * - An algorithm value permits this particular algorithm.
252 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
253 * signature scheme with any hash algorithm.
Gilles Peskine20628592019-04-19 19:29:50 +0200254 *
255 * This function overwrites any algorithm policy
256 * previously set in \p attributes.
257 *
258 * This function may be declared as `static` (i.e. without external
259 * linkage). This function may be provided as a function-like macro,
260 * but in this case it must evaluate each of its arguments exactly once.
261 *
262 * \param[out] attributes The attribute structure to write to.
263 * \param alg The permitted algorithm policy to write.
264 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200265static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
266 psa_algorithm_t alg);
267
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100268
Gilles Peskine20628592019-04-19 19:29:50 +0200269/** Retrieve the algorithm policy from key attributes.
270 *
271 * This function may be declared as `static` (i.e. without external
272 * linkage). This function may be provided as a function-like macro,
273 * but in this case it must evaluate its argument exactly once.
274 *
275 * \param[in] attributes The key attribute structure to query.
276 *
277 * \return The algorithm stored in the attribute structure.
278 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200279static psa_algorithm_t psa_get_key_algorithm(
280 const psa_key_attributes_t *attributes);
281
Gilles Peskine20628592019-04-19 19:29:50 +0200282/** Declare the type of a key.
283 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200284 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200285 * previously set in \p attributes.
286 *
287 * This function may be declared as `static` (i.e. without external
288 * linkage). This function may be provided as a function-like macro,
289 * but in this case it must evaluate each of its arguments exactly once.
290 *
291 * \param[out] attributes The attribute structure to write to.
292 * \param type The key type to write.
Gilles Peskinea170d922019-09-12 16:59:37 +0200293 * If this is 0, the key type in \p attributes
294 * becomes unspecified.
Gilles Peskine20628592019-04-19 19:29:50 +0200295 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200296static void psa_set_key_type(psa_key_attributes_t *attributes,
297 psa_key_type_t type);
298
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100299
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200300/** Declare the size of a key.
301 *
302 * This function overwrites any key size previously set in \p attributes.
303 *
304 * This function may be declared as `static` (i.e. without external
305 * linkage). This function may be provided as a function-like macro,
306 * but in this case it must evaluate each of its arguments exactly once.
307 *
308 * \param[out] attributes The attribute structure to write to.
309 * \param bits The key size in bits.
Gilles Peskinea170d922019-09-12 16:59:37 +0200310 * If this is 0, the key size in \p attributes
Gilles Peskine05c900b2019-09-12 18:29:43 +0200311 * becomes unspecified. Keys of size 0 are
312 * not supported.
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200313 */
314static void psa_set_key_bits(psa_key_attributes_t *attributes,
315 size_t bits);
316
Gilles Peskine20628592019-04-19 19:29:50 +0200317/** Retrieve the key type from key attributes.
318 *
319 * This function may be declared as `static` (i.e. without external
320 * linkage). This function may be provided as a function-like macro,
321 * but in this case it must evaluate its argument exactly once.
322 *
323 * \param[in] attributes The key attribute structure to query.
324 *
325 * \return The key type stored in the attribute structure.
326 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200327static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
328
Gilles Peskine20628592019-04-19 19:29:50 +0200329/** Retrieve the key size from key attributes.
330 *
331 * This function may be declared as `static` (i.e. without external
332 * linkage). This function may be provided as a function-like macro,
333 * but in this case it must evaluate its argument exactly once.
334 *
335 * \param[in] attributes The key attribute structure to query.
336 *
337 * \return The key size stored in the attribute structure, in bits.
338 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200339static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
340
Gilles Peskine20628592019-04-19 19:29:50 +0200341/** Retrieve the attributes of a key.
342 *
343 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200344 * psa_reset_key_attributes(). It then copies the attributes of
345 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200346 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200347 * \note This function may allocate memory or other resources.
348 * Once you have called this function on an attribute structure,
349 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200350 *
Gilles Peskine20628592019-04-19 19:29:50 +0200351 * \param[in] handle Handle to the key to query.
352 * \param[in,out] attributes On success, the attributes of the key.
353 * On failure, equivalent to a
354 * freshly-initialized structure.
355 *
356 * \retval #PSA_SUCCESS
357 * \retval #PSA_ERROR_INVALID_HANDLE
358 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
359 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100360 * \retval #PSA_ERROR_CORRUPTION_DETECTED
361 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100362 * \retval #PSA_ERROR_BAD_STATE
363 * The library has not been previously initialized by psa_crypto_init().
364 * It is implementation-dependent whether a failure to initialize
365 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200366 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200367psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
368 psa_key_attributes_t *attributes);
369
Gilles Peskine20628592019-04-19 19:29:50 +0200370/** Reset a key attribute structure to a freshly initialized state.
371 *
372 * You must initialize the attribute structure as described in the
373 * documentation of the type #psa_key_attributes_t before calling this
374 * function. Once the structure has been initialized, you may call this
375 * function at any time.
376 *
377 * This function frees any auxiliary resources that the structure
378 * may contain.
379 *
380 * \param[in,out] attributes The attribute structure to reset.
381 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200382void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200383
Gilles Peskine87a5e562019-04-17 12:28:25 +0200384/**@}*/
385
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100386/** \defgroup key_management Key management
387 * @{
388 */
389
Gilles Peskinef535eb22018-11-30 14:08:36 +0100390/** Open a handle to an existing persistent key.
391 *
Gilles Peskine4754cde2019-05-21 15:56:29 +0200392 * Open a handle to a persistent key. A key is persistent if it was created
393 * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
394 * always has a nonzero key identifier, set with psa_set_key_id() when
395 * creating the key. Implementations may provide additional pre-provisioned
Ronald Cron27238fc2020-07-23 12:30:41 +0200396 * keys that can be opened with psa_open_key(). Such keys have an application
397 * key identifier in the vendor range, as documented in the description of
398 * #psa_key_id_t.
Gilles Peskine4754cde2019-05-21 15:56:29 +0200399 *
Andrew Thoelkede183412019-09-05 09:38:06 +0100400 * The application must eventually close the handle with psa_close_key() or
401 * psa_destroy_key() to release associated resources. If the application dies
402 * without calling one of these functions, the implementation should perform
403 * the equivalent of a call to psa_close_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100404 *
Andrew Thoelke9741b112019-08-21 18:20:41 +0100405 * Some implementations permit an application to open the same key multiple
Andrew Thoelkede183412019-09-05 09:38:06 +0100406 * times. If this is successful, each call to psa_open_key() will return a
407 * different key handle.
408 *
409 * \note Applications that rely on opening a key multiple times will not be
410 * portable to implementations that only permit a single key handle to be
411 * opened. See also :ref:\`key-handles\`.
Andrew Thoelke9741b112019-08-21 18:20:41 +0100412 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200413 * \param key The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100414 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100415 *
416 * \retval #PSA_SUCCESS
417 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100418 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100419 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Andrew Thoelke9741b112019-08-21 18:20:41 +0100420 * The implementation does not have sufficient resources to open the
421 * key. This can be due to reaching an implementation limit on the
422 * number of open keys, the number of open key handles, or available
423 * memory.
David Saadab4ecc272019-02-14 13:48:10 +0200424 * \retval #PSA_ERROR_DOES_NOT_EXIST
Andrew Thoelke9741b112019-08-21 18:20:41 +0100425 * There is no persistent key with key identifier \p id.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100426 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrew Thoelke9741b112019-08-21 18:20:41 +0100427 * \p id is not a valid persistent key identifier.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100428 * \retval #PSA_ERROR_NOT_PERMITTED
429 * The specified key exists, but the application does not have the
430 * permission to access it. Note that this specification does not
431 * define any way to create such a key, but it may be possible
432 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200433 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawd789dc12019-08-12 15:06:48 +0100434 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine225010f2019-05-06 18:44:55 +0200435 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100436 * \retval #PSA_ERROR_BAD_STATE
437 * The library has not been previously initialized by psa_crypto_init().
438 * It is implementation-dependent whether a failure to initialize
439 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100440 */
Ronald Cron71016a92020-08-28 19:01:50 +0200441psa_status_t psa_open_key( mbedtls_svc_key_id_t key,
442 psa_key_handle_t *handle );
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100443
Gilles Peskinef535eb22018-11-30 14:08:36 +0100444/** Close a key handle.
445 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100446 * If the handle designates a volatile key, this will destroy the key material
447 * and free all associated resources, just like psa_destroy_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100448 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100449 * If this is the last open handle to a persistent key, then closing the handle
450 * will free all resources associated with the key in volatile memory. The key
451 * data in persistent storage is not affected and can be opened again later
452 * with a call to psa_open_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100453 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100454 * Closing the key handle makes the handle invalid, and the key handle
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100455 * must not be used again by the application.
Andrew Thoelke3daba812019-08-21 22:46:56 +0100456 *
Andrew Thoelke970629f2019-09-09 09:56:34 +0100457 * \note If the key handle was used to set up an active
Andrew Thoelkede183412019-09-05 09:38:06 +0100458 * :ref:\`multipart operation <multipart-operations>\`, then closing the
459 * key handle can cause the multipart operation to fail. Applications should
460 * maintain the key handle until after the multipart operation has finished.
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100461 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100462 * \param handle The key handle to close.
Gilles Peskine24934012019-10-08 15:43:13 +0200463 * If this is \c 0, do nothing and return \c PSA_SUCCESS.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100464 *
465 * \retval #PSA_SUCCESS
Gilles Peskine24934012019-10-08 15:43:13 +0200466 * \p handle was a valid handle or \c 0. It is now closed.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100467 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine24934012019-10-08 15:43:13 +0200468 * \p handle is not a valid handle nor \c 0.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100469 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawf97c8522019-08-15 13:27:12 +0100470 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100471 * \retval #PSA_ERROR_BAD_STATE
472 * The library has not been previously initialized by psa_crypto_init().
473 * It is implementation-dependent whether a failure to initialize
474 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100475 */
476psa_status_t psa_close_key(psa_key_handle_t handle);
477
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100478/** Make a copy of a key.
479 *
480 * Copy key material from one location to another.
481 *
482 * This function is primarily useful to copy a key from one location
483 * to another, since it populates a key using the material from
484 * another key which may have a different lifetime.
485 *
486 * This function may be used to share a key with a different party,
487 * subject to implementation-defined restrictions on key sharing.
488 *
489 * The policy on the source key must have the usage flag
490 * #PSA_KEY_USAGE_COPY set.
491 * This flag is sufficient to permit the copy if the key has the lifetime
492 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
493 * Some secure elements do not provide a way to copy a key without
494 * making it extractable from the secure element. If a key is located
495 * in such a secure element, then the key must have both usage flags
496 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
497 * a copy of the key outside the secure element.
498 *
499 * The resulting key may only be used in a way that conforms to
500 * both the policy of the original key and the policy specified in
501 * the \p attributes parameter:
502 * - The usage flags on the resulting key are the bitwise-and of the
503 * usage flags on the source policy and the usage flags in \p attributes.
504 * - If both allow the same algorithm or wildcard-based
505 * algorithm policy, the resulting key has the same algorithm policy.
506 * - If either of the policies allows an algorithm and the other policy
507 * allows a wildcard-based algorithm policy that includes this algorithm,
508 * the resulting key allows the same algorithm.
509 * - If the policies do not allow any algorithm in common, this function
510 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
511 *
512 * The effect of this function on implementation-defined attributes is
513 * implementation-defined.
514 *
515 * \param source_handle The key to copy. It must be a valid key handle.
516 * \param[in] attributes The attributes for the new key.
517 * They are used as follows:
518 * - The key type and size may be 0. If either is
519 * nonzero, it must match the corresponding
520 * attribute of the source key.
521 * - The key location (the lifetime and, for
522 * persistent keys, the key identifier) is
523 * used directly.
524 * - The policy constraints (usage flags and
525 * algorithm policy) are combined from
526 * the source key and \p attributes so that
527 * both sets of restrictions apply, as
528 * described in the documentation of this function.
529 * \param[out] target_handle On success, a handle to the newly created key.
530 * \c 0 on failure.
531 *
532 * \retval #PSA_SUCCESS
533 * \retval #PSA_ERROR_INVALID_HANDLE
534 * \p source_handle is invalid.
535 * \retval #PSA_ERROR_ALREADY_EXISTS
536 * This is an attempt to create a persistent key, and there is
537 * already a persistent key with the given identifier.
538 * \retval #PSA_ERROR_INVALID_ARGUMENT
539 * The lifetime or identifier in \p attributes are invalid.
540 * \retval #PSA_ERROR_INVALID_ARGUMENT
541 * The policy constraints on the source and specified in
542 * \p attributes are incompatible.
543 * \retval #PSA_ERROR_INVALID_ARGUMENT
544 * \p attributes specifies a key type or key size
545 * which does not match the attributes of the source key.
546 * \retval #PSA_ERROR_NOT_PERMITTED
547 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
548 * \retval #PSA_ERROR_NOT_PERMITTED
549 * The source key is not exportable and its lifetime does not
550 * allow copying it to the target's lifetime.
551 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
552 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
553 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
554 * \retval #PSA_ERROR_HARDWARE_FAILURE
555 * \retval #PSA_ERROR_STORAGE_FAILURE
556 * \retval #PSA_ERROR_CORRUPTION_DETECTED
557 * \retval #PSA_ERROR_BAD_STATE
558 * The library has not been previously initialized by psa_crypto_init().
559 * It is implementation-dependent whether a failure to initialize
560 * results in this error code.
561 */
562psa_status_t psa_copy_key(psa_key_handle_t source_handle,
563 const psa_key_attributes_t *attributes,
564 psa_key_handle_t *target_handle);
565
566
567/**
568 * \brief Destroy a key.
569 *
570 * This function destroys a key from both volatile
571 * memory and, if applicable, non-volatile storage. Implementations shall
572 * make a best effort to ensure that that the key material cannot be recovered.
573 *
574 * This function also erases any metadata such as policies and frees
575 * resources associated with the key. To free all resources associated with
576 * the key, all handles to the key must be closed or destroyed.
577 *
578 * Destroying the key makes the handle invalid, and the key handle
579 * must not be used again by the application. Using other open handles to the
580 * destroyed key in a cryptographic operation will result in an error.
581 *
582 * If a key is currently in use in a multipart operation, then destroying the
583 * key will cause the multipart operation to fail.
584 *
585 * \param handle Handle to the key to erase.
Gilles Peskine24934012019-10-08 15:43:13 +0200586 * If this is \c 0, do nothing and return \c PSA_SUCCESS.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100587 *
588 * \retval #PSA_SUCCESS
Gilles Peskine24934012019-10-08 15:43:13 +0200589 * \p handle was a valid handle and the key material that it
590 * referred to has been erased.
591 * Alternatively, \p handle is \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100592 * \retval #PSA_ERROR_NOT_PERMITTED
593 * The key cannot be erased because it is
594 * read-only, either due to a policy or due to physical restrictions.
595 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine24934012019-10-08 15:43:13 +0200596 * \p handle is not a valid handle nor \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100597 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
598 * There was an failure in communication with the cryptoprocessor.
599 * The key material may still be present in the cryptoprocessor.
600 * \retval #PSA_ERROR_STORAGE_FAILURE
601 * The storage is corrupted. Implementations shall make a best effort
602 * to erase key material even in this stage, however applications
603 * should be aware that it may be impossible to guarantee that the
604 * key material is not recoverable in such cases.
605 * \retval #PSA_ERROR_CORRUPTION_DETECTED
606 * An unexpected condition which is not a storage corruption or
607 * a communication failure occurred. The cryptoprocessor may have
608 * been compromised.
609 * \retval #PSA_ERROR_BAD_STATE
610 * The library has not been previously initialized by psa_crypto_init().
611 * It is implementation-dependent whether a failure to initialize
612 * results in this error code.
613 */
614psa_status_t psa_destroy_key(psa_key_handle_t handle);
615
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100616/**@}*/
617
618/** \defgroup import_export Key import and export
619 * @{
620 */
621
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100622/**
623 * \brief Import a key in binary format.
624 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100625 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100626 * documentation of psa_export_public_key() for the format of public keys
627 * and to the documentation of psa_export_key() for the format for
628 * other key types.
629 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200630 * The key data determines the key size. The attributes may optionally
631 * specify a key size; in this case it must match the size determined
632 * from the key data. A key size of 0 in \p attributes indicates that
633 * the key size is solely determined by the key data.
634 *
635 * Implementations must reject an attempt to import a key of size 0.
636 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100637 * This specification supports a single format for each key type.
638 * Implementations may support other formats as long as the standard
639 * format is supported. Implementations that support other formats
640 * should ensure that the formats are clearly unambiguous so as to
641 * minimize the risk that an invalid input is accidentally interpreted
642 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100643 *
Gilles Peskine20628592019-04-19 19:29:50 +0200644 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200645 * The key size is always determined from the
646 * \p data buffer.
647 * If the key size in \p attributes is nonzero,
648 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200649 * \param[out] handle On success, a handle to the newly created key.
650 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100651 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200652 * buffer is interpreted according to the type declared
653 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200654 * All implementations must support at least the format
655 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100656 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200657 * the chosen type. Implementations may allow other
658 * formats, but should be conservative: implementations
659 * should err on the side of rejecting content if it
660 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200661 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100662 *
Gilles Peskine28538492018-07-11 17:34:00 +0200663 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100664 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100665 * If the key is persistent, the key material and the key's metadata
666 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200667 * \retval #PSA_ERROR_ALREADY_EXISTS
668 * This is an attempt to create a persistent key, and there is
669 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200670 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200671 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200672 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200673 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200674 * The key attributes, as a whole, are invalid.
675 * \retval #PSA_ERROR_INVALID_ARGUMENT
676 * The key data is not correctly formatted.
677 * \retval #PSA_ERROR_INVALID_ARGUMENT
678 * The size in \p attributes is nonzero and does not match the size
679 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200680 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
681 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
682 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100683 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200684 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200685 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300686 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300687 * The library has not been previously initialized by psa_crypto_init().
688 * It is implementation-dependent whether a failure to initialize
689 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100690 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200691psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100692 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200693 size_t data_length,
694 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100695
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100696
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100697
698/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100699 * \brief Export a key in binary format.
700 *
701 * The output of this function can be passed to psa_import_key() to
702 * create an equivalent object.
703 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100704 * If the implementation of psa_import_key() supports other formats
705 * beyond the format specified here, the output from psa_export_key()
706 * must use the representation specified here, not the original
707 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100708 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100709 * For standard key types, the output format is as follows:
710 *
711 * - For symmetric keys (including MAC keys), the format is the
712 * raw bytes of the key.
713 * - For DES, the key data consists of 8 bytes. The parity bits must be
714 * correct.
715 * - For Triple-DES, the format is the concatenation of the
716 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200717 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200718 * is the non-encrypted DER encoding of the representation defined by
719 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
720 * ```
721 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200722 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200723 * modulus INTEGER, -- n
724 * publicExponent INTEGER, -- e
725 * privateExponent INTEGER, -- d
726 * prime1 INTEGER, -- p
727 * prime2 INTEGER, -- q
728 * exponent1 INTEGER, -- d mod (p-1)
729 * exponent2 INTEGER, -- d mod (q-1)
730 * coefficient INTEGER, -- (inverse of q) mod p
731 * }
732 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200733 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200734 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100735 * a representation of the private value as a `ceiling(m/8)`-byte string
736 * where `m` is the bit size associated with the curve, i.e. the bit size
737 * of the order of the curve's coordinate field. This byte string is
738 * in little-endian order for Montgomery curves (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +0100739 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
740 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
741 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
Steven Cooreman6f5cc712020-06-11 16:40:41 +0200742 * For Weierstrass curves, this is the content of the `privateKey` field of
743 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
744 * the format is defined by RFC 7748, and output is masked according to §5.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200745 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200746 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000747 * format is the representation of the private key `x` as a big-endian byte
748 * string. The length of the byte string is the private key size in bytes
749 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200750 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
751 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100752 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200753 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
754 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100755 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200756 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200757 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200758 * \param[out] data_length On success, the number of bytes
759 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100760 *
Gilles Peskine28538492018-07-11 17:34:00 +0200761 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100762 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200763 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200764 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100765 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200766 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
767 * The size of the \p data buffer is too small. You can determine a
768 * sufficient buffer size by calling
769 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
770 * where \c type is the key type
771 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200772 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
773 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200774 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100775 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100776 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300777 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300778 * The library has not been previously initialized by psa_crypto_init().
779 * It is implementation-dependent whether a failure to initialize
780 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100781 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100782psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100783 uint8_t *data,
784 size_t data_size,
785 size_t *data_length);
786
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100787/**
788 * \brief Export a public key or the public part of a key pair in binary format.
789 *
790 * The output of this function can be passed to psa_import_key() to
791 * create an object that is equivalent to the public key.
792 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000793 * This specification supports a single format for each key type.
794 * Implementations may support other formats as long as the standard
795 * format is supported. Implementations that support other formats
796 * should ensure that the formats are clearly unambiguous so as to
797 * minimize the risk that an invalid input is accidentally interpreted
798 * according to a different format.
799 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000800 * For standard key types, the output format is as follows:
801 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
802 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
803 * ```
804 * RSAPublicKey ::= SEQUENCE {
805 * modulus INTEGER, -- n
806 * publicExponent INTEGER } -- e
807 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000808 * - For elliptic curve public keys (key types for which
809 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
810 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
811 * Let `m` be the bit size associated with the curve, i.e. the bit size of
812 * `q` for a curve over `F_q`. The representation consists of:
813 * - The byte 0x04;
814 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
815 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200816 * - For Diffie-Hellman key exchange public keys (key types for which
817 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000818 * the format is the representation of the public key `y = g^x mod p` as a
819 * big-endian byte string. The length of the byte string is the length of the
820 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100821 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200822 * Exporting a public key object or the public part of a key pair is
823 * always permitted, regardless of the key's usage flags.
824 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100825 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200826 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200827 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200828 * \param[out] data_length On success, the number of bytes
829 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100830 *
Gilles Peskine28538492018-07-11 17:34:00 +0200831 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100832 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200833 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200834 * The key is neither a public key nor a key pair.
835 * \retval #PSA_ERROR_NOT_SUPPORTED
836 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
837 * The size of the \p data buffer is too small. You can determine a
838 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200839 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200840 * where \c type is the key type
841 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200842 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
843 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200844 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100845 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100846 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300847 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300848 * The library has not been previously initialized by psa_crypto_init().
849 * It is implementation-dependent whether a failure to initialize
850 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100851 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100852psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100853 uint8_t *data,
854 size_t data_size,
855 size_t *data_length);
856
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100857
Gilles Peskine20035e32018-02-03 22:44:14 +0100858
859/**@}*/
860
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100861/** \defgroup hash Message digests
862 * @{
863 */
864
Gilles Peskine69647a42019-01-14 20:18:12 +0100865/** Calculate the hash (digest) of a message.
866 *
867 * \note To verify the hash of a message against an
868 * expected value, use psa_hash_compare() instead.
869 *
870 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
871 * such that #PSA_ALG_IS_HASH(\p alg) is true).
872 * \param[in] input Buffer containing the message to hash.
873 * \param input_length Size of the \p input buffer in bytes.
874 * \param[out] hash Buffer where the hash is to be written.
875 * \param hash_size Size of the \p hash buffer in bytes.
876 * \param[out] hash_length On success, the number of bytes
877 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100878 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100879 *
880 * \retval #PSA_SUCCESS
881 * Success.
882 * \retval #PSA_ERROR_NOT_SUPPORTED
883 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100884 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100885 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
886 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100887 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
888 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
889 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200890 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100891 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100892 * \retval #PSA_ERROR_BAD_STATE
893 * The library has not been previously initialized by psa_crypto_init().
894 * It is implementation-dependent whether a failure to initialize
895 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100896 */
897psa_status_t psa_hash_compute(psa_algorithm_t alg,
898 const uint8_t *input,
899 size_t input_length,
900 uint8_t *hash,
901 size_t hash_size,
902 size_t *hash_length);
903
904/** Calculate the hash (digest) of a message and compare it with a
905 * reference value.
906 *
907 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
908 * such that #PSA_ALG_IS_HASH(\p alg) is true).
909 * \param[in] input Buffer containing the message to hash.
910 * \param input_length Size of the \p input buffer in bytes.
911 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100912 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100913 *
914 * \retval #PSA_SUCCESS
915 * The expected hash is identical to the actual hash of the input.
916 * \retval #PSA_ERROR_INVALID_SIGNATURE
917 * The hash of the message was calculated successfully, but it
918 * differs from the expected hash.
919 * \retval #PSA_ERROR_NOT_SUPPORTED
920 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100921 * \retval #PSA_ERROR_INVALID_ARGUMENT
922 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100923 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
924 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
925 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200926 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100927 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100928 * \retval #PSA_ERROR_BAD_STATE
929 * The library has not been previously initialized by psa_crypto_init().
930 * It is implementation-dependent whether a failure to initialize
931 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100932 */
933psa_status_t psa_hash_compare(psa_algorithm_t alg,
934 const uint8_t *input,
935 size_t input_length,
936 const uint8_t *hash,
Gilles Peskinefa710f52019-12-02 14:31:48 +0100937 size_t hash_length);
Gilles Peskine69647a42019-01-14 20:18:12 +0100938
Gilles Peskine308b91d2018-02-08 09:47:44 +0100939/** The type of the state data structure for multipart hash operations.
940 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000941 * Before calling any function on a hash operation object, the application must
942 * initialize it by any of the following means:
943 * - Set the structure to all-bits-zero, for example:
944 * \code
945 * psa_hash_operation_t operation;
946 * memset(&operation, 0, sizeof(operation));
947 * \endcode
948 * - Initialize the structure to logical zero values, for example:
949 * \code
950 * psa_hash_operation_t operation = {0};
951 * \endcode
952 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
953 * for example:
954 * \code
955 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
956 * \endcode
957 * - Assign the result of the function psa_hash_operation_init()
958 * to the structure, for example:
959 * \code
960 * psa_hash_operation_t operation;
961 * operation = psa_hash_operation_init();
962 * \endcode
963 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100964 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100965 * make any assumptions about the content of this structure except
966 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100967typedef struct psa_hash_operation_s psa_hash_operation_t;
968
Jaeden Amero6a25b412019-01-04 11:47:44 +0000969/** \def PSA_HASH_OPERATION_INIT
970 *
971 * This macro returns a suitable initializer for a hash operation object
972 * of type #psa_hash_operation_t.
973 */
974#ifdef __DOXYGEN_ONLY__
975/* This is an example definition for documentation purposes.
976 * Implementations should define a suitable value in `crypto_struct.h`.
977 */
978#define PSA_HASH_OPERATION_INIT {0}
979#endif
980
981/** Return an initial value for a hash operation object.
982 */
983static psa_hash_operation_t psa_hash_operation_init(void);
984
Gilles Peskinef45adda2019-01-14 18:29:18 +0100985/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100986 *
987 * The sequence of operations to calculate a hash (message digest)
988 * is as follows:
989 * -# Allocate an operation object which will be passed to all the functions
990 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000991 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100992 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200993 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100994 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100995 * of the message each time. The hash that is calculated is the hash
996 * of the concatenation of these messages in order.
997 * -# To calculate the hash, call psa_hash_finish().
998 * To compare the hash with an expected value, call psa_hash_verify().
999 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001000 * If an error occurs at any step after a call to psa_hash_setup(), the
1001 * operation will need to be reset by a call to psa_hash_abort(). The
1002 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +00001003 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001004 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001005 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001006 * eventually terminate the operation. The following events terminate an
1007 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001008 * - A successful call to psa_hash_finish() or psa_hash_verify().
1009 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001010 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001011 * \param[in,out] operation The operation object to set up. It must have
1012 * been initialized as per the documentation for
1013 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001014 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1015 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001016 *
Gilles Peskine28538492018-07-11 17:34:00 +02001017 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001018 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001019 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +01001020 * \p alg is not a supported hash algorithm.
1021 * \retval #PSA_ERROR_INVALID_ARGUMENT
1022 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001023 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001024 * The operation state is not valid (it must be inactive).
Gilles Peskine28538492018-07-11 17:34:00 +02001025 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1026 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1027 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001028 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001029 * \retval #PSA_ERROR_BAD_STATE
1030 * The library has not been previously initialized by psa_crypto_init().
1031 * It is implementation-dependent whether a failure to initialize
1032 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001033 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001034psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001035 psa_algorithm_t alg);
1036
Gilles Peskine308b91d2018-02-08 09:47:44 +01001037/** Add a message fragment to a multipart hash operation.
1038 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001039 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001040 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001041 * If this function returns an error status, the operation enters an error
1042 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001043 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001044 * \param[in,out] operation Active hash operation.
1045 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001046 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001047 *
Gilles Peskine28538492018-07-11 17:34:00 +02001048 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001049 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001050 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001051 * The operation state is not valid (it muct be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001052 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1053 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1054 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001055 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001056 * \retval #PSA_ERROR_BAD_STATE
1057 * The library has not been previously initialized by psa_crypto_init().
1058 * It is implementation-dependent whether a failure to initialize
1059 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001060 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001061psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1062 const uint8_t *input,
1063 size_t input_length);
1064
Gilles Peskine308b91d2018-02-08 09:47:44 +01001065/** Finish the calculation of the hash of a message.
1066 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001067 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001068 * This function calculates the hash of the message formed by concatenating
1069 * the inputs passed to preceding calls to psa_hash_update().
1070 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001071 * When this function returns successfuly, the operation becomes inactive.
1072 * If this function returns an error status, the operation enters an error
1073 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074 *
1075 * \warning Applications should not call this function if they expect
1076 * a specific value for the hash. Call psa_hash_verify() instead.
1077 * Beware that comparing integrity or authenticity data such as
1078 * hash values with a function such as \c memcmp is risky
1079 * because the time taken by the comparison may leak information
1080 * about the hashed data which could allow an attacker to guess
1081 * a valid hash and thereby bypass security controls.
1082 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001083 * \param[in,out] operation Active hash operation.
1084 * \param[out] hash Buffer where the hash is to be written.
1085 * \param hash_size Size of the \p hash buffer in bytes.
1086 * \param[out] hash_length On success, the number of bytes
1087 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001088 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001089 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001090 *
Gilles Peskine28538492018-07-11 17:34:00 +02001091 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001092 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001093 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001094 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001095 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001096 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001097 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001098 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001099 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1100 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1101 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001102 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001103 * \retval #PSA_ERROR_BAD_STATE
1104 * The library has not been previously initialized by psa_crypto_init().
1105 * It is implementation-dependent whether a failure to initialize
1106 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001107 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001108psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1109 uint8_t *hash,
1110 size_t hash_size,
1111 size_t *hash_length);
1112
Gilles Peskine308b91d2018-02-08 09:47:44 +01001113/** Finish the calculation of the hash of a message and compare it with
1114 * an expected value.
1115 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001116 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001117 * This function calculates the hash of the message formed by concatenating
1118 * the inputs passed to preceding calls to psa_hash_update(). It then
1119 * compares the calculated hash with the expected hash passed as a
1120 * parameter to this function.
1121 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001122 * When this function returns successfuly, the operation becomes inactive.
1123 * If this function returns an error status, the operation enters an error
1124 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001125 *
Gilles Peskine19067982018-03-20 17:54:53 +01001126 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001127 * comparison between the actual hash and the expected hash is performed
1128 * in constant time.
1129 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001130 * \param[in,out] operation Active hash operation.
1131 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001132 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001133 *
Gilles Peskine28538492018-07-11 17:34:00 +02001134 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001135 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001136 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001137 * The hash of the message was calculated successfully, but it
1138 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001139 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001140 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001141 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1142 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1143 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001144 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001145 * \retval #PSA_ERROR_BAD_STATE
1146 * The library has not been previously initialized by psa_crypto_init().
1147 * It is implementation-dependent whether a failure to initialize
1148 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001149 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001150psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1151 const uint8_t *hash,
1152 size_t hash_length);
1153
Gilles Peskine308b91d2018-02-08 09:47:44 +01001154/** Abort a hash operation.
1155 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001156 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001157 * \p operation structure itself. Once aborted, the operation object
1158 * can be reused for another operation by calling
1159 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001160 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001161 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001162 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001163 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001164 * In particular, calling psa_hash_abort() after the operation has been
1165 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1166 * psa_hash_verify() is safe and has no effect.
1167 *
1168 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001169 *
Gilles Peskine28538492018-07-11 17:34:00 +02001170 * \retval #PSA_SUCCESS
Gilles Peskine28538492018-07-11 17:34:00 +02001171 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1172 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001173 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001174 * \retval #PSA_ERROR_BAD_STATE
1175 * The library has not been previously initialized by psa_crypto_init().
1176 * It is implementation-dependent whether a failure to initialize
1177 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001178 */
1179psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001180
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001181/** Clone a hash operation.
1182 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001183 * This function copies the state of an ongoing hash operation to
1184 * a new operation object. In other words, this function is equivalent
1185 * to calling psa_hash_setup() on \p target_operation with the same
1186 * algorithm that \p source_operation was set up for, then
1187 * psa_hash_update() on \p target_operation with the same input that
1188 * that was passed to \p source_operation. After this function returns, the
1189 * two objects are independent, i.e. subsequent calls involving one of
1190 * the objects do not affect the other object.
1191 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001192 * \param[in] source_operation The active hash operation to clone.
1193 * \param[in,out] target_operation The operation object to set up.
1194 * It must be initialized but not active.
1195 *
1196 * \retval #PSA_SUCCESS
1197 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001198 * The \p source_operation state is not valid (it must be active).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001199 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001200 * The \p target_operation state is not valid (it must be inactive).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001201 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1202 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001203 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001204 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001205 * \retval #PSA_ERROR_BAD_STATE
1206 * The library has not been previously initialized by psa_crypto_init().
1207 * It is implementation-dependent whether a failure to initialize
1208 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001209 */
1210psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1211 psa_hash_operation_t *target_operation);
1212
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001213/**@}*/
1214
Gilles Peskine8c9def32018-02-08 10:02:12 +01001215/** \defgroup MAC Message authentication codes
1216 * @{
1217 */
1218
Gilles Peskine69647a42019-01-14 20:18:12 +01001219/** Calculate the MAC (message authentication code) of a message.
1220 *
1221 * \note To verify the MAC of a message against an
1222 * expected value, use psa_mac_verify() instead.
1223 * Beware that comparing integrity or authenticity data such as
1224 * MAC values with a function such as \c memcmp is risky
1225 * because the time taken by the comparison may leak information
1226 * about the MAC value which could allow an attacker to guess
1227 * a valid MAC and thereby bypass security controls.
1228 *
1229 * \param handle Handle to the key to use for the operation.
1230 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001231 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001232 * \param[in] input Buffer containing the input message.
1233 * \param input_length Size of the \p input buffer in bytes.
1234 * \param[out] mac Buffer where the MAC value is to be written.
1235 * \param mac_size Size of the \p mac buffer in bytes.
1236 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001237 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001238 *
1239 * \retval #PSA_SUCCESS
1240 * Success.
1241 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001242 * \retval #PSA_ERROR_NOT_PERMITTED
1243 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001244 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001245 * \retval #PSA_ERROR_NOT_SUPPORTED
1246 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001247 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1248 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001249 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1250 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1251 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001252 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001253 * \retval #PSA_ERROR_STORAGE_FAILURE
1254 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001255 * \retval #PSA_ERROR_BAD_STATE
1256 * The library has not been previously initialized by psa_crypto_init().
1257 * It is implementation-dependent whether a failure to initialize
1258 * results in this error code.
1259 */
1260psa_status_t psa_mac_compute(psa_key_handle_t handle,
1261 psa_algorithm_t alg,
1262 const uint8_t *input,
1263 size_t input_length,
1264 uint8_t *mac,
1265 size_t mac_size,
1266 size_t *mac_length);
1267
1268/** Calculate the MAC of a message and compare it with a reference value.
1269 *
1270 * \param handle Handle to the key to use for the operation.
1271 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001272 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001273 * \param[in] input Buffer containing the input message.
1274 * \param input_length Size of the \p input buffer in bytes.
1275 * \param[out] mac Buffer containing the expected MAC value.
1276 * \param mac_length Size of the \p mac buffer in bytes.
1277 *
1278 * \retval #PSA_SUCCESS
1279 * The expected MAC is identical to the actual MAC of the input.
1280 * \retval #PSA_ERROR_INVALID_SIGNATURE
1281 * The MAC of the message was calculated successfully, but it
1282 * differs from the expected value.
1283 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001284 * \retval #PSA_ERROR_NOT_PERMITTED
1285 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001286 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001287 * \retval #PSA_ERROR_NOT_SUPPORTED
1288 * \p alg is not supported or is not a MAC algorithm.
1289 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1290 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1291 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001292 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001293 * \retval #PSA_ERROR_STORAGE_FAILURE
1294 * The key could not be retrieved from storage.
1295 * \retval #PSA_ERROR_BAD_STATE
1296 * The library has not been previously initialized by psa_crypto_init().
1297 * It is implementation-dependent whether a failure to initialize
1298 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001299 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001300psa_status_t psa_mac_verify(psa_key_handle_t handle,
1301 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001302 const uint8_t *input,
1303 size_t input_length,
1304 const uint8_t *mac,
Gilles Peskine13faa2d2020-01-30 16:32:21 +01001305 size_t mac_length);
Gilles Peskine69647a42019-01-14 20:18:12 +01001306
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001307/** The type of the state data structure for multipart MAC operations.
1308 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001309 * Before calling any function on a MAC operation object, the application must
1310 * initialize it by any of the following means:
1311 * - Set the structure to all-bits-zero, for example:
1312 * \code
1313 * psa_mac_operation_t operation;
1314 * memset(&operation, 0, sizeof(operation));
1315 * \endcode
1316 * - Initialize the structure to logical zero values, for example:
1317 * \code
1318 * psa_mac_operation_t operation = {0};
1319 * \endcode
1320 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1321 * for example:
1322 * \code
1323 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1324 * \endcode
1325 * - Assign the result of the function psa_mac_operation_init()
1326 * to the structure, for example:
1327 * \code
1328 * psa_mac_operation_t operation;
1329 * operation = psa_mac_operation_init();
1330 * \endcode
1331 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001332 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001333 * make any assumptions about the content of this structure except
1334 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001335typedef struct psa_mac_operation_s psa_mac_operation_t;
1336
Jaeden Amero769ce272019-01-04 11:48:03 +00001337/** \def PSA_MAC_OPERATION_INIT
1338 *
1339 * This macro returns a suitable initializer for a MAC operation object of type
1340 * #psa_mac_operation_t.
1341 */
1342#ifdef __DOXYGEN_ONLY__
1343/* This is an example definition for documentation purposes.
1344 * Implementations should define a suitable value in `crypto_struct.h`.
1345 */
1346#define PSA_MAC_OPERATION_INIT {0}
1347#endif
1348
1349/** Return an initial value for a MAC operation object.
1350 */
1351static psa_mac_operation_t psa_mac_operation_init(void);
1352
Gilles Peskinef45adda2019-01-14 18:29:18 +01001353/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001354 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001355 * This function sets up the calculation of the MAC
1356 * (message authentication code) of a byte string.
1357 * To verify the MAC of a message against an
1358 * expected value, use psa_mac_verify_setup() instead.
1359 *
1360 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001361 * -# Allocate an operation object which will be passed to all the functions
1362 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001363 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001364 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001365 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001366 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1367 * of the message each time. The MAC that is calculated is the MAC
1368 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001369 * -# At the end of the message, call psa_mac_sign_finish() to finish
1370 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001371 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001372 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1373 * operation will need to be reset by a call to psa_mac_abort(). The
1374 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001375 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001376 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001377 * After a successful call to psa_mac_sign_setup(), the application must
1378 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001379 * - A successful call to psa_mac_sign_finish().
1380 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001381 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001382 * \param[in,out] operation The operation object to set up. It must have
1383 * been initialized as per the documentation for
1384 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001385 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001386 * It must remain valid until the operation
1387 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001388 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001389 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001390 *
Gilles Peskine28538492018-07-11 17:34:00 +02001391 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001392 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001393 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001394 * \retval #PSA_ERROR_NOT_PERMITTED
1395 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001396 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001397 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001398 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001399 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1400 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1401 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001402 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001403 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001404 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001405 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001406 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001407 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001408 * The library has not been previously initialized by psa_crypto_init().
1409 * It is implementation-dependent whether a failure to initialize
1410 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001411 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001412psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001413 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001414 psa_algorithm_t alg);
1415
Gilles Peskinef45adda2019-01-14 18:29:18 +01001416/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001417 *
1418 * This function sets up the verification of the MAC
1419 * (message authentication code) of a byte string against an expected value.
1420 *
1421 * The sequence of operations to verify a MAC is as follows:
1422 * -# Allocate an operation object which will be passed to all the functions
1423 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001424 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001425 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001426 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001427 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1428 * of the message each time. The MAC that is calculated is the MAC
1429 * of the concatenation of these messages in order.
1430 * -# At the end of the message, call psa_mac_verify_finish() to finish
1431 * calculating the actual MAC of the message and verify it against
1432 * the expected value.
1433 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001434 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1435 * operation will need to be reset by a call to psa_mac_abort(). The
1436 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001437 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001438 *
1439 * After a successful call to psa_mac_verify_setup(), the application must
1440 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001441 * - A successful call to psa_mac_verify_finish().
1442 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001443 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001444 * \param[in,out] operation The operation object to set up. It must have
1445 * been initialized as per the documentation for
1446 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001447 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001448 * It must remain valid until the operation
1449 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001450 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1451 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001452 *
Gilles Peskine28538492018-07-11 17:34:00 +02001453 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001454 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001455 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001456 * \retval #PSA_ERROR_NOT_PERMITTED
1457 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001458 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001459 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001460 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001461 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1462 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1463 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001464 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001465 * \retval #PSA_ERROR_STORAGE_FAILURE
1466 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001467 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001468 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001469 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001470 * The library has not been previously initialized by psa_crypto_init().
1471 * It is implementation-dependent whether a failure to initialize
1472 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001473 */
1474psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001475 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001476 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001477
Gilles Peskinedcd14942018-07-12 00:30:52 +02001478/** Add a message fragment to a multipart MAC operation.
1479 *
1480 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1481 * before calling this function.
1482 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001483 * If this function returns an error status, the operation enters an error
1484 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001485 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001486 * \param[in,out] operation Active MAC operation.
1487 * \param[in] input Buffer containing the message fragment to add to
1488 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001489 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001490 *
1491 * \retval #PSA_SUCCESS
1492 * Success.
1493 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001494 * The operation state is not valid (it must be active).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001495 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1496 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1497 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001498 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001499 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001500 * \retval #PSA_ERROR_BAD_STATE
1501 * The library has not been previously initialized by psa_crypto_init().
1502 * It is implementation-dependent whether a failure to initialize
1503 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001504 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001505psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1506 const uint8_t *input,
1507 size_t input_length);
1508
Gilles Peskinedcd14942018-07-12 00:30:52 +02001509/** Finish the calculation of the MAC of a message.
1510 *
1511 * The application must call psa_mac_sign_setup() before calling this function.
1512 * This function calculates the MAC of the message formed by concatenating
1513 * the inputs passed to preceding calls to psa_mac_update().
1514 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001515 * When this function returns successfuly, the operation becomes inactive.
1516 * If this function returns an error status, the operation enters an error
1517 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001518 *
1519 * \warning Applications should not call this function if they expect
1520 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1521 * Beware that comparing integrity or authenticity data such as
1522 * MAC values with a function such as \c memcmp is risky
1523 * because the time taken by the comparison may leak information
1524 * about the MAC value which could allow an attacker to guess
1525 * a valid MAC and thereby bypass security controls.
1526 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001527 * \param[in,out] operation Active MAC operation.
1528 * \param[out] mac Buffer where the MAC value is to be written.
1529 * \param mac_size Size of the \p mac buffer in bytes.
1530 * \param[out] mac_length On success, the number of bytes
1531 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001532 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001533 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001534 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001535 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001536 *
1537 * \retval #PSA_SUCCESS
1538 * Success.
1539 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001540 * The operation state is not valid (it must be an active mac sign
1541 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001542 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001543 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001544 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1545 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1546 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1547 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001548 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001549 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001550 * \retval #PSA_ERROR_BAD_STATE
1551 * The library has not been previously initialized by psa_crypto_init().
1552 * It is implementation-dependent whether a failure to initialize
1553 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001554 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001555psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1556 uint8_t *mac,
1557 size_t mac_size,
1558 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001559
Gilles Peskinedcd14942018-07-12 00:30:52 +02001560/** Finish the calculation of the MAC of a message and compare it with
1561 * an expected value.
1562 *
1563 * The application must call psa_mac_verify_setup() before calling this function.
1564 * This function calculates the MAC of the message formed by concatenating
1565 * the inputs passed to preceding calls to psa_mac_update(). It then
1566 * compares the calculated MAC with the expected MAC passed as a
1567 * parameter to this function.
1568 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001569 * When this function returns successfuly, the operation becomes inactive.
1570 * If this function returns an error status, the operation enters an error
1571 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001572 *
1573 * \note Implementations shall make the best effort to ensure that the
1574 * comparison between the actual MAC and the expected MAC is performed
1575 * in constant time.
1576 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001577 * \param[in,out] operation Active MAC operation.
1578 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001579 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001580 *
1581 * \retval #PSA_SUCCESS
1582 * The expected MAC is identical to the actual MAC of the message.
1583 * \retval #PSA_ERROR_INVALID_SIGNATURE
1584 * The MAC of the message was calculated successfully, but it
1585 * differs from the expected MAC.
1586 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001587 * The operation state is not valid (it must be an active mac verify
1588 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001589 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1590 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1591 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001592 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001593 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001594 * \retval #PSA_ERROR_BAD_STATE
1595 * The library has not been previously initialized by psa_crypto_init().
1596 * It is implementation-dependent whether a failure to initialize
1597 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001598 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001599psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1600 const uint8_t *mac,
1601 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001602
Gilles Peskinedcd14942018-07-12 00:30:52 +02001603/** Abort a MAC operation.
1604 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001605 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001606 * \p operation structure itself. Once aborted, the operation object
1607 * can be reused for another operation by calling
1608 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001609 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001610 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001611 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001612 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001613 * In particular, calling psa_mac_abort() after the operation has been
1614 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1615 * psa_mac_verify_finish() is safe and has no effect.
1616 *
1617 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001618 *
1619 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02001620 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1621 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001622 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001623 * \retval #PSA_ERROR_BAD_STATE
1624 * The library has not been previously initialized by psa_crypto_init().
1625 * It is implementation-dependent whether a failure to initialize
1626 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001627 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001628psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1629
1630/**@}*/
1631
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001632/** \defgroup cipher Symmetric ciphers
1633 * @{
1634 */
1635
Gilles Peskine69647a42019-01-14 20:18:12 +01001636/** Encrypt a message using a symmetric cipher.
1637 *
1638 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001639 * vector). Use the multipart operation interface with a
1640 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001641 *
1642 * \param handle Handle to the key to use for the operation.
1643 * It must remain valid until the operation
1644 * terminates.
1645 * \param alg The cipher algorithm to compute
1646 * (\c PSA_ALG_XXX value such that
1647 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1648 * \param[in] input Buffer containing the message to encrypt.
1649 * \param input_length Size of the \p input buffer in bytes.
1650 * \param[out] output Buffer where the output is to be written.
1651 * The output contains the IV followed by
1652 * the ciphertext proper.
1653 * \param output_size Size of the \p output buffer in bytes.
1654 * \param[out] output_length On success, the number of bytes
1655 * that make up the output.
1656 *
1657 * \retval #PSA_SUCCESS
1658 * Success.
1659 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001660 * \retval #PSA_ERROR_NOT_PERMITTED
1661 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001662 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001663 * \retval #PSA_ERROR_NOT_SUPPORTED
1664 * \p alg is not supported or is not a cipher algorithm.
1665 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1666 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1667 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1668 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001669 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001670 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001671 * \retval #PSA_ERROR_BAD_STATE
1672 * The library has not been previously initialized by psa_crypto_init().
1673 * It is implementation-dependent whether a failure to initialize
1674 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001675 */
1676psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1677 psa_algorithm_t alg,
1678 const uint8_t *input,
1679 size_t input_length,
1680 uint8_t *output,
1681 size_t output_size,
1682 size_t *output_length);
1683
1684/** Decrypt a message using a symmetric cipher.
1685 *
1686 * This function decrypts a message encrypted with a symmetric cipher.
1687 *
1688 * \param handle Handle to the key to use for the operation.
1689 * It must remain valid until the operation
1690 * terminates.
1691 * \param alg The cipher algorithm to compute
1692 * (\c PSA_ALG_XXX value such that
1693 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1694 * \param[in] input Buffer containing the message to decrypt.
1695 * This consists of the IV followed by the
1696 * ciphertext proper.
1697 * \param input_length Size of the \p input buffer in bytes.
1698 * \param[out] output Buffer where the plaintext is to be written.
1699 * \param output_size Size of the \p output buffer in bytes.
1700 * \param[out] output_length On success, the number of bytes
1701 * that make up the output.
1702 *
1703 * \retval #PSA_SUCCESS
1704 * Success.
1705 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001706 * \retval #PSA_ERROR_NOT_PERMITTED
1707 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001708 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001709 * \retval #PSA_ERROR_NOT_SUPPORTED
1710 * \p alg is not supported or is not a cipher algorithm.
1711 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1712 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1713 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1714 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001715 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001716 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001717 * \retval #PSA_ERROR_BAD_STATE
1718 * The library has not been previously initialized by psa_crypto_init().
1719 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001720 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001721 */
1722psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1723 psa_algorithm_t alg,
1724 const uint8_t *input,
1725 size_t input_length,
1726 uint8_t *output,
1727 size_t output_size,
1728 size_t *output_length);
1729
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001730/** The type of the state data structure for multipart cipher operations.
1731 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001732 * Before calling any function on a cipher operation object, the application
1733 * must initialize it by any of the following means:
1734 * - Set the structure to all-bits-zero, for example:
1735 * \code
1736 * psa_cipher_operation_t operation;
1737 * memset(&operation, 0, sizeof(operation));
1738 * \endcode
1739 * - Initialize the structure to logical zero values, for example:
1740 * \code
1741 * psa_cipher_operation_t operation = {0};
1742 * \endcode
1743 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1744 * for example:
1745 * \code
1746 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1747 * \endcode
1748 * - Assign the result of the function psa_cipher_operation_init()
1749 * to the structure, for example:
1750 * \code
1751 * psa_cipher_operation_t operation;
1752 * operation = psa_cipher_operation_init();
1753 * \endcode
1754 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001755 * This is an implementation-defined \c struct. Applications should not
1756 * make any assumptions about the content of this structure except
1757 * as directed by the documentation of a specific implementation. */
1758typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1759
Jaeden Amero5bae2272019-01-04 11:48:27 +00001760/** \def PSA_CIPHER_OPERATION_INIT
1761 *
1762 * This macro returns a suitable initializer for a cipher operation object of
1763 * type #psa_cipher_operation_t.
1764 */
1765#ifdef __DOXYGEN_ONLY__
1766/* This is an example definition for documentation purposes.
1767 * Implementations should define a suitable value in `crypto_struct.h`.
1768 */
1769#define PSA_CIPHER_OPERATION_INIT {0}
1770#endif
1771
1772/** Return an initial value for a cipher operation object.
1773 */
1774static psa_cipher_operation_t psa_cipher_operation_init(void);
1775
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001776/** Set the key for a multipart symmetric encryption operation.
1777 *
1778 * The sequence of operations to encrypt a message with a symmetric cipher
1779 * is as follows:
1780 * -# Allocate an operation object which will be passed to all the functions
1781 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001782 * -# Initialize the operation object with one of the methods described in the
1783 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001784 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001785 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001786 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001787 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001788 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001789 * requires a specific IV value.
1790 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1791 * of the message each time.
1792 * -# Call psa_cipher_finish().
1793 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001794 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1795 * the operation will need to be reset by a call to psa_cipher_abort(). The
1796 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001797 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001798 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001799 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001800 * eventually terminate the operation. The following events terminate an
1801 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001802 * - A successful call to psa_cipher_finish().
1803 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001804 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001805 * \param[in,out] operation The operation object to set up. It must have
1806 * been initialized as per the documentation for
1807 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001808 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001809 * It must remain valid until the operation
1810 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001811 * \param alg The cipher algorithm to compute
1812 * (\c PSA_ALG_XXX value such that
1813 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001814 *
Gilles Peskine28538492018-07-11 17:34:00 +02001815 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001816 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001817 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001818 * \retval #PSA_ERROR_NOT_PERMITTED
1819 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001820 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001821 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001822 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001823 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1824 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1825 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001826 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001827 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001828 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001829 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001830 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001831 * The library has not been previously initialized by psa_crypto_init().
1832 * It is implementation-dependent whether a failure to initialize
1833 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001834 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001835psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001836 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001837 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001838
1839/** Set the key for a multipart symmetric decryption operation.
1840 *
1841 * The sequence of operations to decrypt a message with a symmetric cipher
1842 * is as follows:
1843 * -# Allocate an operation object which will be passed to all the functions
1844 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001845 * -# Initialize the operation object with one of the methods described in the
1846 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001847 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001848 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001849 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001850 * decryption. If the IV is prepended to the ciphertext, you can call
1851 * psa_cipher_update() on a buffer containing the IV followed by the
1852 * beginning of the message.
1853 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1854 * of the message each time.
1855 * -# Call psa_cipher_finish().
1856 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001857 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1858 * the operation will need to be reset by a call to psa_cipher_abort(). The
1859 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001860 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001861 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001862 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001863 * eventually terminate the operation. The following events terminate an
1864 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001865 * - A successful call to psa_cipher_finish().
1866 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001867 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001868 * \param[in,out] operation The operation object to set up. It must have
1869 * been initialized as per the documentation for
1870 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001871 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001872 * It must remain valid until the operation
1873 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001874 * \param alg The cipher algorithm to compute
1875 * (\c PSA_ALG_XXX value such that
1876 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001877 *
Gilles Peskine28538492018-07-11 17:34:00 +02001878 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001879 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001880 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001881 * \retval #PSA_ERROR_NOT_PERMITTED
1882 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001883 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001884 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001885 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001886 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1887 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1888 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001889 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001890 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001891 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001892 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001893 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001894 * The library has not been previously initialized by psa_crypto_init().
1895 * It is implementation-dependent whether a failure to initialize
1896 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001897 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001898psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001899 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001900 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001901
Gilles Peskinedcd14942018-07-12 00:30:52 +02001902/** Generate an IV for a symmetric encryption operation.
1903 *
1904 * This function generates a random IV (initialization vector), nonce
1905 * or initial counter value for the encryption operation as appropriate
1906 * for the chosen algorithm, key type and key size.
1907 *
1908 * The application must call psa_cipher_encrypt_setup() before
1909 * calling this function.
1910 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001911 * If this function returns an error status, the operation enters an error
1912 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001913 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001914 * \param[in,out] operation Active cipher operation.
1915 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001916 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001917 * \param[out] iv_length On success, the number of bytes of the
1918 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001919 *
1920 * \retval #PSA_SUCCESS
1921 * Success.
1922 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001923 * The operation state is not valid (it must be active, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001924 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001925 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001926 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1927 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1928 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001929 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001930 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001931 * \retval #PSA_ERROR_BAD_STATE
1932 * The library has not been previously initialized by psa_crypto_init().
1933 * It is implementation-dependent whether a failure to initialize
1934 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001935 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001936psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001937 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001938 size_t iv_size,
1939 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001940
Gilles Peskinedcd14942018-07-12 00:30:52 +02001941/** Set the IV for a symmetric encryption or decryption operation.
1942 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001943 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001944 * or initial counter value for the encryption or decryption operation.
1945 *
1946 * The application must call psa_cipher_encrypt_setup() before
1947 * calling this function.
1948 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001949 * If this function returns an error status, the operation enters an error
1950 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001951 *
1952 * \note When encrypting, applications should use psa_cipher_generate_iv()
1953 * instead of this function, unless implementing a protocol that requires
1954 * a non-random IV.
1955 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001956 * \param[in,out] operation Active cipher operation.
1957 * \param[in] iv Buffer containing the IV to use.
1958 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001959 *
1960 * \retval #PSA_SUCCESS
1961 * Success.
1962 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001963 * The operation state is not valid (it must be an active cipher
1964 * encrypt operation, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001965 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001966 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001967 * or the chosen algorithm does not use an IV.
1968 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1969 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1970 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001971 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001972 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001973 * \retval #PSA_ERROR_BAD_STATE
1974 * The library has not been previously initialized by psa_crypto_init().
1975 * It is implementation-dependent whether a failure to initialize
1976 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001977 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001978psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001979 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001980 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001981
Gilles Peskinedcd14942018-07-12 00:30:52 +02001982/** Encrypt or decrypt a message fragment in an active cipher operation.
1983 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001984 * Before calling this function, you must:
1985 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1986 * The choice of setup function determines whether this function
1987 * encrypts or decrypts its input.
1988 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1989 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001990 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001991 * If this function returns an error status, the operation enters an error
1992 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001993 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001994 * \param[in,out] operation Active cipher operation.
1995 * \param[in] input Buffer containing the message fragment to
1996 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001997 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001998 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001999 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002000 * \param[out] output_length On success, the number of bytes
2001 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002002 *
2003 * \retval #PSA_SUCCESS
2004 * Success.
2005 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002006 * The operation state is not valid (it must be active, with an IV set
2007 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02002008 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2009 * The size of the \p output buffer is too small.
2010 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2011 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2012 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002013 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002014 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002015 * \retval #PSA_ERROR_BAD_STATE
2016 * The library has not been previously initialized by psa_crypto_init().
2017 * It is implementation-dependent whether a failure to initialize
2018 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002019 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002020psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2021 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002022 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00002023 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02002024 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002025 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002026
Gilles Peskinedcd14942018-07-12 00:30:52 +02002027/** Finish encrypting or decrypting a message in a cipher operation.
2028 *
2029 * The application must call psa_cipher_encrypt_setup() or
2030 * psa_cipher_decrypt_setup() before calling this function. The choice
2031 * of setup function determines whether this function encrypts or
2032 * decrypts its input.
2033 *
2034 * This function finishes the encryption or decryption of the message
2035 * formed by concatenating the inputs passed to preceding calls to
2036 * psa_cipher_update().
2037 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002038 * When this function returns successfuly, the operation becomes inactive.
2039 * If this function returns an error status, the operation enters an error
2040 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002041 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002042 * \param[in,out] operation Active cipher operation.
2043 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002044 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002045 * \param[out] output_length On success, the number of bytes
2046 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002047 *
2048 * \retval #PSA_SUCCESS
2049 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02002050 * \retval #PSA_ERROR_INVALID_ARGUMENT
2051 * The total input size passed to this operation is not valid for
2052 * this particular algorithm. For example, the algorithm is a based
2053 * on block cipher and requires a whole number of blocks, but the
2054 * total input size is not a multiple of the block size.
2055 * \retval #PSA_ERROR_INVALID_PADDING
2056 * This is a decryption operation for an algorithm that includes
2057 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002058 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002059 * The operation state is not valid (it must be active, with an IV set
2060 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02002061 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2062 * The size of the \p output buffer is too small.
2063 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2064 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2065 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002066 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002067 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002068 * \retval #PSA_ERROR_BAD_STATE
2069 * The library has not been previously initialized by psa_crypto_init().
2070 * It is implementation-dependent whether a failure to initialize
2071 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002072 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002073psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002074 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002075 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002076 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002077
Gilles Peskinedcd14942018-07-12 00:30:52 +02002078/** Abort a cipher operation.
2079 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002080 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002081 * \p operation structure itself. Once aborted, the operation object
2082 * can be reused for another operation by calling
2083 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002084 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002085 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002086 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002087 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002088 * In particular, calling psa_cipher_abort() after the operation has been
2089 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2090 * is safe and has no effect.
2091 *
2092 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002093 *
2094 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02002095 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2096 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002097 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002098 * \retval #PSA_ERROR_BAD_STATE
2099 * The library has not been previously initialized by psa_crypto_init().
2100 * It is implementation-dependent whether a failure to initialize
2101 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002102 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002103psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2104
2105/**@}*/
2106
Gilles Peskine3b555712018-03-03 21:27:57 +01002107/** \defgroup aead Authenticated encryption with associated data (AEAD)
2108 * @{
2109 */
2110
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002111/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002112 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002113 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002114 * \param alg The AEAD algorithm to compute
2115 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002116 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002117 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002118 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002119 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002120 * but not encrypted.
2121 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002122 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002123 * encrypted.
2124 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002125 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002126 * encrypted data. The additional data is not
2127 * part of this output. For algorithms where the
2128 * encrypted data and the authentication tag
2129 * are defined as separate outputs, the
2130 * authentication tag is appended to the
2131 * encrypted data.
2132 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2133 * This must be at least
2134 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2135 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002136 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002137 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002138 *
Gilles Peskine28538492018-07-11 17:34:00 +02002139 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002140 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002141 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002142 * \retval #PSA_ERROR_NOT_PERMITTED
2143 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002144 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002145 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002146 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002147 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002148 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2149 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002150 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2151 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002152 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002153 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002154 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002155 * The library has not been previously initialized by psa_crypto_init().
2156 * It is implementation-dependent whether a failure to initialize
2157 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002158 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002159psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002160 psa_algorithm_t alg,
2161 const uint8_t *nonce,
2162 size_t nonce_length,
2163 const uint8_t *additional_data,
2164 size_t additional_data_length,
2165 const uint8_t *plaintext,
2166 size_t plaintext_length,
2167 uint8_t *ciphertext,
2168 size_t ciphertext_size,
2169 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002170
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002171/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002172 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002173 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002174 * \param alg The AEAD algorithm to compute
2175 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002176 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002177 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002178 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002179 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002180 * but not encrypted.
2181 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002182 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002183 * encrypted. For algorithms where the
2184 * encrypted data and the authentication tag
2185 * are defined as separate inputs, the buffer
2186 * must contain the encrypted data followed
2187 * by the authentication tag.
2188 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002189 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002190 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2191 * This must be at least
2192 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2193 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002194 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002195 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002196 *
Gilles Peskine28538492018-07-11 17:34:00 +02002197 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002198 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002199 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002200 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002201 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002202 * \retval #PSA_ERROR_NOT_PERMITTED
2203 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002204 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002205 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002206 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002207 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002208 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2209 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002210 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2211 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002212 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002213 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002214 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002215 * The library has not been previously initialized by psa_crypto_init().
2216 * It is implementation-dependent whether a failure to initialize
2217 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002218 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002219psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002220 psa_algorithm_t alg,
2221 const uint8_t *nonce,
2222 size_t nonce_length,
2223 const uint8_t *additional_data,
2224 size_t additional_data_length,
2225 const uint8_t *ciphertext,
2226 size_t ciphertext_length,
2227 uint8_t *plaintext,
2228 size_t plaintext_size,
2229 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002230
Gilles Peskine30a9e412019-01-14 18:36:12 +01002231/** The type of the state data structure for multipart AEAD operations.
2232 *
2233 * Before calling any function on an AEAD operation object, the application
2234 * must initialize it by any of the following means:
2235 * - Set the structure to all-bits-zero, for example:
2236 * \code
2237 * psa_aead_operation_t operation;
2238 * memset(&operation, 0, sizeof(operation));
2239 * \endcode
2240 * - Initialize the structure to logical zero values, for example:
2241 * \code
2242 * psa_aead_operation_t operation = {0};
2243 * \endcode
2244 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2245 * for example:
2246 * \code
2247 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2248 * \endcode
2249 * - Assign the result of the function psa_aead_operation_init()
2250 * to the structure, for example:
2251 * \code
2252 * psa_aead_operation_t operation;
2253 * operation = psa_aead_operation_init();
2254 * \endcode
2255 *
2256 * This is an implementation-defined \c struct. Applications should not
2257 * make any assumptions about the content of this structure except
2258 * as directed by the documentation of a specific implementation. */
2259typedef struct psa_aead_operation_s psa_aead_operation_t;
2260
2261/** \def PSA_AEAD_OPERATION_INIT
2262 *
2263 * This macro returns a suitable initializer for an AEAD operation object of
2264 * type #psa_aead_operation_t.
2265 */
2266#ifdef __DOXYGEN_ONLY__
2267/* This is an example definition for documentation purposes.
2268 * Implementations should define a suitable value in `crypto_struct.h`.
2269 */
2270#define PSA_AEAD_OPERATION_INIT {0}
2271#endif
2272
2273/** Return an initial value for an AEAD operation object.
2274 */
2275static psa_aead_operation_t psa_aead_operation_init(void);
2276
2277/** Set the key for a multipart authenticated encryption operation.
2278 *
2279 * The sequence of operations to encrypt a message with authentication
2280 * is as follows:
2281 * -# Allocate an operation object which will be passed to all the functions
2282 * listed here.
2283 * -# Initialize the operation object with one of the methods described in the
2284 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002285 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002286 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002287 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2288 * inputs to the subsequent calls to psa_aead_update_ad() and
2289 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2290 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002291 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2292 * generate or set the nonce. You should use
2293 * psa_aead_generate_nonce() unless the protocol you are implementing
2294 * requires a specific nonce value.
2295 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2296 * of the non-encrypted additional authenticated data each time.
2297 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002298 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002299 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002300 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002301 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2302 * the operation will need to be reset by a call to psa_aead_abort(). The
2303 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002304 * has been initialized.
2305 *
2306 * After a successful call to psa_aead_encrypt_setup(), the application must
2307 * eventually terminate the operation. The following events terminate an
2308 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002309 * - A successful call to psa_aead_finish().
2310 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002311 *
2312 * \param[in,out] operation The operation object to set up. It must have
2313 * been initialized as per the documentation for
2314 * #psa_aead_operation_t and not yet in use.
2315 * \param handle Handle to the key to use for the operation.
2316 * It must remain valid until the operation
2317 * terminates.
2318 * \param alg The AEAD algorithm to compute
2319 * (\c PSA_ALG_XXX value such that
2320 * #PSA_ALG_IS_AEAD(\p alg) is true).
2321 *
2322 * \retval #PSA_SUCCESS
2323 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002324 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002325 * The operation state is not valid (it must be inactive).
Andrew Thoelke340984b2019-09-11 21:33:41 +01002326 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002327 * \retval #PSA_ERROR_NOT_PERMITTED
2328 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002329 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002330 * \retval #PSA_ERROR_NOT_SUPPORTED
2331 * \p alg is not supported or is not an AEAD algorithm.
2332 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2333 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2334 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002335 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002336 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002337 * \retval #PSA_ERROR_BAD_STATE
2338 * The library has not been previously initialized by psa_crypto_init().
2339 * It is implementation-dependent whether a failure to initialize
2340 * results in this error code.
2341 */
2342psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2343 psa_key_handle_t handle,
2344 psa_algorithm_t alg);
2345
2346/** Set the key for a multipart authenticated decryption operation.
2347 *
2348 * The sequence of operations to decrypt a message with authentication
2349 * is as follows:
2350 * -# Allocate an operation object which will be passed to all the functions
2351 * listed here.
2352 * -# Initialize the operation object with one of the methods described in the
2353 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002354 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002355 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002356 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2357 * inputs to the subsequent calls to psa_aead_update_ad() and
2358 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2359 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002360 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2361 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2362 * of the non-encrypted additional authenticated data each time.
2363 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002364 * of the ciphertext to decrypt each time.
2365 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002366 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002367 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2368 * the operation will need to be reset by a call to psa_aead_abort(). The
2369 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002370 * has been initialized.
2371 *
2372 * After a successful call to psa_aead_decrypt_setup(), the application must
2373 * eventually terminate the operation. The following events terminate an
2374 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002375 * - A successful call to psa_aead_verify().
2376 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002377 *
2378 * \param[in,out] operation The operation object to set up. It must have
2379 * been initialized as per the documentation for
2380 * #psa_aead_operation_t and not yet in use.
2381 * \param handle Handle to the key to use for the operation.
2382 * It must remain valid until the operation
2383 * terminates.
2384 * \param alg The AEAD algorithm to compute
2385 * (\c PSA_ALG_XXX value such that
2386 * #PSA_ALG_IS_AEAD(\p alg) is true).
2387 *
2388 * \retval #PSA_SUCCESS
2389 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002390 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002391 * The operation state is not valid (it must be inactive).
Andrew Thoelke340984b2019-09-11 21:33:41 +01002392 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002393 * \retval #PSA_ERROR_NOT_PERMITTED
2394 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002395 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002396 * \retval #PSA_ERROR_NOT_SUPPORTED
2397 * \p alg is not supported or is not an AEAD algorithm.
2398 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2399 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2400 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002401 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002402 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002403 * \retval #PSA_ERROR_BAD_STATE
2404 * The library has not been previously initialized by psa_crypto_init().
2405 * It is implementation-dependent whether a failure to initialize
2406 * results in this error code.
2407 */
2408psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2409 psa_key_handle_t handle,
2410 psa_algorithm_t alg);
2411
2412/** Generate a random nonce for an authenticated encryption operation.
2413 *
2414 * This function generates a random nonce for the authenticated encryption
2415 * operation with an appropriate size for the chosen algorithm, key type
2416 * and key size.
2417 *
2418 * The application must call psa_aead_encrypt_setup() before
2419 * calling this function.
2420 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002421 * If this function returns an error status, the operation enters an error
2422 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002423 *
2424 * \param[in,out] operation Active AEAD operation.
2425 * \param[out] nonce Buffer where the generated nonce is to be
2426 * written.
2427 * \param nonce_size Size of the \p nonce buffer in bytes.
2428 * \param[out] nonce_length On success, the number of bytes of the
2429 * generated nonce.
2430 *
2431 * \retval #PSA_SUCCESS
2432 * Success.
2433 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002434 * The operation state is not valid (it must be an active aead encrypt
2435 operation, with no nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002436 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2437 * The size of the \p nonce buffer is too small.
2438 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2439 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2440 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002441 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002442 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002443 * \retval #PSA_ERROR_BAD_STATE
2444 * The library has not been previously initialized by psa_crypto_init().
2445 * It is implementation-dependent whether a failure to initialize
2446 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002447 */
2448psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002449 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002450 size_t nonce_size,
2451 size_t *nonce_length);
2452
2453/** Set the nonce for an authenticated encryption or decryption operation.
2454 *
2455 * This function sets the nonce for the authenticated
2456 * encryption or decryption operation.
2457 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002458 * The application must call psa_aead_encrypt_setup() or
2459 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002460 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002461 * If this function returns an error status, the operation enters an error
2462 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002463 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002464 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002465 * instead of this function, unless implementing a protocol that requires
2466 * a non-random IV.
2467 *
2468 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002469 * \param[in] nonce Buffer containing the nonce to use.
2470 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002471 *
2472 * \retval #PSA_SUCCESS
2473 * Success.
2474 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002475 * The operation state is not valid (it must be active, with no nonce
2476 * set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002477 * \retval #PSA_ERROR_INVALID_ARGUMENT
2478 * The size of \p nonce is not acceptable for the chosen algorithm.
2479 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2480 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2481 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002482 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002483 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002484 * \retval #PSA_ERROR_BAD_STATE
2485 * The library has not been previously initialized by psa_crypto_init().
2486 * It is implementation-dependent whether a failure to initialize
2487 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002488 */
2489psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002490 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002491 size_t nonce_length);
2492
Gilles Peskinebc59c852019-01-17 15:26:08 +01002493/** Declare the lengths of the message and additional data for AEAD.
2494 *
2495 * The application must call this function before calling
2496 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2497 * the operation requires it. If the algorithm does not require it,
2498 * calling this function is optional, but if this function is called
2499 * then the implementation must enforce the lengths.
2500 *
2501 * You may call this function before or after setting the nonce with
2502 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2503 *
2504 * - For #PSA_ALG_CCM, calling this function is required.
2505 * - For the other AEAD algorithms defined in this specification, calling
2506 * this function is not required.
2507 * - For vendor-defined algorithm, refer to the vendor documentation.
2508 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002509 * If this function returns an error status, the operation enters an error
2510 * state and must be aborted by calling psa_aead_abort().
2511 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002512 * \param[in,out] operation Active AEAD operation.
2513 * \param ad_length Size of the non-encrypted additional
2514 * authenticated data in bytes.
2515 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2516 *
2517 * \retval #PSA_SUCCESS
2518 * Success.
2519 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002520 * The operation state is not valid (it must be active, and
2521 * psa_aead_update_ad() and psa_aead_update() must not have been
2522 * called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002523 * \retval #PSA_ERROR_INVALID_ARGUMENT
2524 * At least one of the lengths is not acceptable for the chosen
2525 * algorithm.
2526 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2527 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2528 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002529 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002530 * \retval #PSA_ERROR_BAD_STATE
2531 * The library has not been previously initialized by psa_crypto_init().
2532 * It is implementation-dependent whether a failure to initialize
2533 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002534 */
2535psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2536 size_t ad_length,
2537 size_t plaintext_length);
2538
Gilles Peskine30a9e412019-01-14 18:36:12 +01002539/** Pass additional data to an active AEAD operation.
2540 *
2541 * Additional data is authenticated, but not encrypted.
2542 *
2543 * You may call this function multiple times to pass successive fragments
2544 * of the additional data. You may not call this function after passing
2545 * data to encrypt or decrypt with psa_aead_update().
2546 *
2547 * Before calling this function, you must:
2548 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2549 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2550 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002551 * If this function returns an error status, the operation enters an error
2552 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002553 *
2554 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2555 * there is no guarantee that the input is valid. Therefore, until
2556 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2557 * treat the input as untrusted and prepare to undo any action that
2558 * depends on the input if psa_aead_verify() returns an error status.
2559 *
2560 * \param[in,out] operation Active AEAD operation.
2561 * \param[in] input Buffer containing the fragment of
2562 * additional data.
2563 * \param input_length Size of the \p input buffer in bytes.
2564 *
2565 * \retval #PSA_SUCCESS
2566 * Success.
2567 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002568 * The operation state is not valid (it must be active, have a nonce
2569 * set, have lengths set if required by the algorithm, and
2570 * psa_aead_update() must not have been called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002571 * \retval #PSA_ERROR_INVALID_ARGUMENT
2572 * The total input length overflows the additional data length that
2573 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002574 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2575 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2576 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002577 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002578 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002579 * \retval #PSA_ERROR_BAD_STATE
2580 * The library has not been previously initialized by psa_crypto_init().
2581 * It is implementation-dependent whether a failure to initialize
2582 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002583 */
2584psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2585 const uint8_t *input,
2586 size_t input_length);
2587
2588/** Encrypt or decrypt a message fragment in an active AEAD operation.
2589 *
2590 * Before calling this function, you must:
2591 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2592 * The choice of setup function determines whether this function
2593 * encrypts or decrypts its input.
2594 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2595 * 3. Call psa_aead_update_ad() to pass all the additional data.
2596 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002597 * If this function returns an error status, the operation enters an error
2598 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002599 *
2600 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2601 * there is no guarantee that the input is valid. Therefore, until
2602 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2603 * - Do not use the output in any way other than storing it in a
2604 * confidential location. If you take any action that depends
2605 * on the tentative decrypted data, this action will need to be
2606 * undone if the input turns out not to be valid. Furthermore,
2607 * if an adversary can observe that this action took place
2608 * (for example through timing), they may be able to use this
2609 * fact as an oracle to decrypt any message encrypted with the
2610 * same key.
2611 * - In particular, do not copy the output anywhere but to a
2612 * memory or storage space that you have exclusive access to.
2613 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002614 * This function does not require the input to be aligned to any
2615 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002616 * a whole block at a time, it must consume all the input provided, but
2617 * it may delay the end of the corresponding output until a subsequent
2618 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2619 * provides sufficient input. The amount of data that can be delayed
2620 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002621 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002622 * \param[in,out] operation Active AEAD operation.
2623 * \param[in] input Buffer containing the message fragment to
2624 * encrypt or decrypt.
2625 * \param input_length Size of the \p input buffer in bytes.
2626 * \param[out] output Buffer where the output is to be written.
2627 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002628 * This must be at least
2629 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2630 * \p input_length) where \c alg is the
2631 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002632 * \param[out] output_length On success, the number of bytes
2633 * that make up the returned output.
2634 *
2635 * \retval #PSA_SUCCESS
2636 * Success.
2637 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002638 * The operation state is not valid (it must be active, have a nonce
2639 * set, and have lengths set if required by the algorithm).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002640 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2641 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002642 * You can determine a sufficient buffer size by calling
2643 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2644 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002645 * \retval #PSA_ERROR_INVALID_ARGUMENT
2646 * The total length of input to psa_aead_update_ad() so far is
2647 * less than the additional data length that was previously
2648 * specified with psa_aead_set_lengths().
2649 * \retval #PSA_ERROR_INVALID_ARGUMENT
2650 * The total input length overflows the plaintext length that
2651 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002652 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2653 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2654 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002655 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002656 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002657 * \retval #PSA_ERROR_BAD_STATE
2658 * The library has not been previously initialized by psa_crypto_init().
2659 * It is implementation-dependent whether a failure to initialize
2660 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002661 */
2662psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2663 const uint8_t *input,
2664 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002665 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002666 size_t output_size,
2667 size_t *output_length);
2668
2669/** Finish encrypting a message in an AEAD operation.
2670 *
2671 * The operation must have been set up with psa_aead_encrypt_setup().
2672 *
2673 * This function finishes the authentication of the additional data
2674 * formed by concatenating the inputs passed to preceding calls to
2675 * psa_aead_update_ad() with the plaintext formed by concatenating the
2676 * inputs passed to preceding calls to psa_aead_update().
2677 *
2678 * This function has two output buffers:
2679 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002680 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002681 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002682 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002683 * that the operation performs.
2684 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002685 * When this function returns successfuly, the operation becomes inactive.
2686 * If this function returns an error status, the operation enters an error
2687 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002688 *
2689 * \param[in,out] operation Active AEAD operation.
2690 * \param[out] ciphertext Buffer where the last part of the ciphertext
2691 * is to be written.
2692 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002693 * This must be at least
2694 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2695 * \c alg is the algorithm that is being
2696 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002697 * \param[out] ciphertext_length On success, the number of bytes of
2698 * returned ciphertext.
2699 * \param[out] tag Buffer where the authentication tag is
2700 * to be written.
2701 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002702 * This must be at least
2703 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2704 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002705 * \param[out] tag_length On success, the number of bytes
2706 * that make up the returned tag.
2707 *
2708 * \retval #PSA_SUCCESS
2709 * Success.
2710 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002711 * The operation state is not valid (it must be an active encryption
2712 * operation with a nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002713 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002714 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002715 * You can determine a sufficient buffer size for \p ciphertext by
2716 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2717 * where \c alg is the algorithm that is being calculated.
2718 * You can determine a sufficient buffer size for \p tag by
2719 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002720 * \retval #PSA_ERROR_INVALID_ARGUMENT
2721 * The total length of input to psa_aead_update_ad() so far is
2722 * less than the additional data length that was previously
2723 * specified with psa_aead_set_lengths().
2724 * \retval #PSA_ERROR_INVALID_ARGUMENT
2725 * The total length of input to psa_aead_update() so far is
2726 * less than the plaintext length that was previously
2727 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002728 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2729 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2730 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002731 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002732 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002733 * \retval #PSA_ERROR_BAD_STATE
2734 * The library has not been previously initialized by psa_crypto_init().
2735 * It is implementation-dependent whether a failure to initialize
2736 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002737 */
2738psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002739 uint8_t *ciphertext,
2740 size_t ciphertext_size,
2741 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002742 uint8_t *tag,
2743 size_t tag_size,
2744 size_t *tag_length);
2745
2746/** Finish authenticating and decrypting a message in an AEAD operation.
2747 *
2748 * The operation must have been set up with psa_aead_decrypt_setup().
2749 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002750 * This function finishes the authenticated decryption of the message
2751 * components:
2752 *
2753 * - The additional data consisting of the concatenation of the inputs
2754 * passed to preceding calls to psa_aead_update_ad().
2755 * - The ciphertext consisting of the concatenation of the inputs passed to
2756 * preceding calls to psa_aead_update().
2757 * - The tag passed to this function call.
2758 *
2759 * If the authentication tag is correct, this function outputs any remaining
2760 * plaintext and reports success. If the authentication tag is not correct,
2761 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002762 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002763 * When this function returns successfuly, the operation becomes inactive.
2764 * If this function returns an error status, the operation enters an error
2765 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002766 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002767 * \note Implementations shall make the best effort to ensure that the
2768 * comparison between the actual tag and the expected tag is performed
2769 * in constant time.
2770 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002771 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002772 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002773 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002774 * from previous calls to psa_aead_update()
2775 * that could not be processed until the end
2776 * of the input.
2777 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002778 * This must be at least
2779 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2780 * \c alg is the algorithm that is being
2781 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002782 * \param[out] plaintext_length On success, the number of bytes of
2783 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002784 * \param[in] tag Buffer containing the authentication tag.
2785 * \param tag_length Size of the \p tag buffer in bytes.
2786 *
2787 * \retval #PSA_SUCCESS
2788 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002789 * \retval #PSA_ERROR_INVALID_SIGNATURE
2790 * The calculations were successful, but the authentication tag is
2791 * not correct.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002792 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002793 * The operation state is not valid (it must be an active decryption
2794 * operation with a nonce set).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002795 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2796 * The size of the \p plaintext buffer is too small.
2797 * You can determine a sufficient buffer size for \p plaintext by
2798 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2799 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002800 * \retval #PSA_ERROR_INVALID_ARGUMENT
2801 * The total length of input to psa_aead_update_ad() so far is
2802 * less than the additional data length that was previously
2803 * specified with psa_aead_set_lengths().
2804 * \retval #PSA_ERROR_INVALID_ARGUMENT
2805 * The total length of input to psa_aead_update() so far is
2806 * less than the plaintext length that was previously
2807 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002808 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2809 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2810 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002811 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002812 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002813 * \retval #PSA_ERROR_BAD_STATE
2814 * The library has not been previously initialized by psa_crypto_init().
2815 * It is implementation-dependent whether a failure to initialize
2816 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002817 */
2818psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002819 uint8_t *plaintext,
2820 size_t plaintext_size,
2821 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002822 const uint8_t *tag,
2823 size_t tag_length);
2824
2825/** Abort an AEAD operation.
2826 *
2827 * Aborting an operation frees all associated resources except for the
2828 * \p operation structure itself. Once aborted, the operation object
2829 * can be reused for another operation by calling
2830 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2831 *
2832 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002833 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002834 *
2835 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002836 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2837 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002838 *
2839 * \param[in,out] operation Initialized AEAD operation.
2840 *
2841 * \retval #PSA_SUCCESS
Gilles Peskine30a9e412019-01-14 18:36:12 +01002842 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2843 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002844 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002845 * \retval #PSA_ERROR_BAD_STATE
2846 * The library has not been previously initialized by psa_crypto_init().
2847 * It is implementation-dependent whether a failure to initialize
2848 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002849 */
2850psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2851
Gilles Peskine3b555712018-03-03 21:27:57 +01002852/**@}*/
2853
Gilles Peskine20035e32018-02-03 22:44:14 +01002854/** \defgroup asymmetric Asymmetric cryptography
2855 * @{
2856 */
2857
2858/**
2859 * \brief Sign a hash or short message with a private key.
2860 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002861 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002862 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002863 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2864 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2865 * to determine the hash algorithm to use.
2866 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002867 * \param handle Handle to the key to use for the operation.
2868 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002869 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002870 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002871 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002872 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002873 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002874 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002875 * \param[out] signature_length On success, the number of bytes
2876 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002877 *
Gilles Peskine28538492018-07-11 17:34:00 +02002878 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002879 * \retval #PSA_ERROR_INVALID_HANDLE
2880 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002881 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002882 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002883 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002884 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002885 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002886 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002887 * \retval #PSA_ERROR_NOT_SUPPORTED
2888 * \retval #PSA_ERROR_INVALID_ARGUMENT
2889 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2890 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2891 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002892 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002893 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002894 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002895 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002896 * The library has not been previously initialized by psa_crypto_init().
2897 * It is implementation-dependent whether a failure to initialize
2898 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002899 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002900psa_status_t psa_sign_hash(psa_key_handle_t handle,
2901 psa_algorithm_t alg,
2902 const uint8_t *hash,
2903 size_t hash_length,
2904 uint8_t *signature,
2905 size_t signature_size,
2906 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002907
2908/**
2909 * \brief Verify the signature a hash or short message using a public key.
2910 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002911 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002912 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002913 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2914 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2915 * to determine the hash algorithm to use.
2916 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002917 * \param handle Handle to the key to use for the operation.
2918 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002919 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002920 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002921 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002922 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002923 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002924 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002925 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002926 *
Gilles Peskine28538492018-07-11 17:34:00 +02002927 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002928 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002929 * \retval #PSA_ERROR_INVALID_HANDLE
2930 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002931 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002932 * The calculation was perfomed successfully, but the passed
2933 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002934 * \retval #PSA_ERROR_NOT_SUPPORTED
2935 * \retval #PSA_ERROR_INVALID_ARGUMENT
2936 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2937 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2938 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002939 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002940 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002941 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002942 * The library has not been previously initialized by psa_crypto_init().
2943 * It is implementation-dependent whether a failure to initialize
2944 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002945 */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002946psa_status_t psa_verify_hash(psa_key_handle_t handle,
2947 psa_algorithm_t alg,
2948 const uint8_t *hash,
2949 size_t hash_length,
2950 const uint8_t *signature,
2951 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002952
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002953/**
2954 * \brief Encrypt a short message with a public key.
2955 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002956 * \param handle Handle to the key to use for the operation.
2957 * It must be a public key or an asymmetric
2958 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002959 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002960 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002961 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002962 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002963 * \param[in] salt A salt or label, if supported by the
2964 * encryption algorithm.
2965 * If the algorithm does not support a
2966 * salt, pass \c NULL.
2967 * If the algorithm supports an optional
2968 * salt and you do not want to pass a salt,
2969 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002970 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002971 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2972 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002973 * \param salt_length Size of the \p salt buffer in bytes.
2974 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002975 * \param[out] output Buffer where the encrypted message is to
2976 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002977 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002978 * \param[out] output_length On success, the number of bytes
2979 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002980 *
Gilles Peskine28538492018-07-11 17:34:00 +02002981 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002982 * \retval #PSA_ERROR_INVALID_HANDLE
2983 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002984 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002985 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002986 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002987 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002988 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002989 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002990 * \retval #PSA_ERROR_NOT_SUPPORTED
2991 * \retval #PSA_ERROR_INVALID_ARGUMENT
2992 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2993 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2994 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002995 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002996 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002997 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002998 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002999 * The library has not been previously initialized by psa_crypto_init().
3000 * It is implementation-dependent whether a failure to initialize
3001 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003002 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003003psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003004 psa_algorithm_t alg,
3005 const uint8_t *input,
3006 size_t input_length,
3007 const uint8_t *salt,
3008 size_t salt_length,
3009 uint8_t *output,
3010 size_t output_size,
3011 size_t *output_length);
3012
3013/**
3014 * \brief Decrypt a short message with a private key.
3015 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003016 * \param handle Handle to the key to use for the operation.
3017 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003018 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003019 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003020 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003021 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003022 * \param[in] salt A salt or label, if supported by the
3023 * encryption algorithm.
3024 * If the algorithm does not support a
3025 * salt, pass \c NULL.
3026 * If the algorithm supports an optional
3027 * salt and you do not want to pass a salt,
3028 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003029 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003030 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3031 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003032 * \param salt_length Size of the \p salt buffer in bytes.
3033 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003034 * \param[out] output Buffer where the decrypted message is to
3035 * be written.
3036 * \param output_size Size of the \c output buffer in bytes.
3037 * \param[out] output_length On success, the number of bytes
3038 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003039 *
Gilles Peskine28538492018-07-11 17:34:00 +02003040 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003041 * \retval #PSA_ERROR_INVALID_HANDLE
3042 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003043 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003044 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003045 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003046 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003047 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003048 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02003049 * \retval #PSA_ERROR_NOT_SUPPORTED
3050 * \retval #PSA_ERROR_INVALID_ARGUMENT
3051 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3052 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3053 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003054 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003055 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003056 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3057 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003058 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003059 * The library has not been previously initialized by psa_crypto_init().
3060 * It is implementation-dependent whether a failure to initialize
3061 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003062 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003063psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003064 psa_algorithm_t alg,
3065 const uint8_t *input,
3066 size_t input_length,
3067 const uint8_t *salt,
3068 size_t salt_length,
3069 uint8_t *output,
3070 size_t output_size,
3071 size_t *output_length);
3072
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003073/**@}*/
3074
Gilles Peskine35675b62019-05-16 17:26:11 +02003075/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003076 * @{
3077 */
3078
Gilles Peskine35675b62019-05-16 17:26:11 +02003079/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003080 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003081 * Before calling any function on a key derivation operation object, the
3082 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003083 * - Set the structure to all-bits-zero, for example:
3084 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003085 * psa_key_derivation_operation_t operation;
3086 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003087 * \endcode
3088 * - Initialize the structure to logical zero values, for example:
3089 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003090 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003091 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003092 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003093 * for example:
3094 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003095 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003096 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003097 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003098 * to the structure, for example:
3099 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003100 * psa_key_derivation_operation_t operation;
3101 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003102 * \endcode
3103 *
3104 * This is an implementation-defined \c struct. Applications should not
3105 * make any assumptions about the content of this structure except
3106 * as directed by the documentation of a specific implementation.
3107 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003108typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003109
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003110/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003111 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003112 * This macro returns a suitable initializer for a key derivation operation
3113 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003114 */
3115#ifdef __DOXYGEN_ONLY__
3116/* This is an example definition for documentation purposes.
3117 * Implementations should define a suitable value in `crypto_struct.h`.
3118 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003119#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003120#endif
3121
Gilles Peskine35675b62019-05-16 17:26:11 +02003122/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003123 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003124static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003125
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003126/** Set up a key derivation operation.
3127 *
3128 * A key derivation algorithm takes some inputs and uses them to generate
3129 * a byte stream in a deterministic way.
3130 * This byte stream can be used to produce keys and other
3131 * cryptographic material.
3132 *
3133 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003134 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3135 * -# Call psa_key_derivation_setup() to select the algorithm.
3136 * -# Provide the inputs for the key derivation by calling
3137 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3138 * as appropriate. Which inputs are needed, in what order, and whether
3139 * they may be keys and if so of what type depends on the algorithm.
3140 * -# Optionally set the operation's maximum capacity with
3141 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3142 * of or after providing inputs. For some algorithms, this step is mandatory
3143 * because the output depends on the maximum capacity.
3144 * -# To derive a key, call psa_key_derivation_output_key().
3145 * To derive a byte string for a different purpose, call
3146 * psa_key_derivation_output_bytes().
3147 * Successive calls to these functions use successive output bytes
3148 * calculated by the key derivation algorithm.
3149 * -# Clean up the key derivation operation object with
3150 * psa_key_derivation_abort().
3151 *
3152 * If this function returns an error, the key derivation operation object is
3153 * not changed.
3154 *
3155 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3156 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003157 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003158 * Implementations must reject an attempt to derive a key of size 0.
3159 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003160 * \param[in,out] operation The key derivation operation object
3161 * to set up. It must
3162 * have been initialized but not set up yet.
3163 * \param alg The key derivation algorithm to compute
3164 * (\c PSA_ALG_XXX value such that
3165 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3166 *
3167 * \retval #PSA_SUCCESS
3168 * Success.
3169 * \retval #PSA_ERROR_INVALID_ARGUMENT
3170 * \c alg is not a key derivation algorithm.
3171 * \retval #PSA_ERROR_NOT_SUPPORTED
3172 * \c alg is not supported or is not a key derivation algorithm.
3173 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3174 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3175 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003176 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003177 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003178 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003179 * The operation state is not valid (it must be inactive).
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003180 * \retval #PSA_ERROR_BAD_STATE
3181 * The library has not been previously initialized by psa_crypto_init().
3182 * It is implementation-dependent whether a failure to initialize
3183 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003184 */
3185psa_status_t psa_key_derivation_setup(
3186 psa_key_derivation_operation_t *operation,
3187 psa_algorithm_t alg);
3188
Gilles Peskine35675b62019-05-16 17:26:11 +02003189/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003190 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003191 * The capacity of a key derivation is the maximum number of bytes that it can
3192 * return. When you get *N* bytes of output from a key derivation operation,
3193 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003194 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003195 * \param[in] operation The operation to query.
3196 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003197 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003198 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003199 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003200 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003201 * The operation state is not valid (it must be active).
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003202 * \retval #PSA_ERROR_HARDWARE_FAILURE
3203 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003204 * \retval #PSA_ERROR_BAD_STATE
3205 * The library has not been previously initialized by psa_crypto_init().
3206 * It is implementation-dependent whether a failure to initialize
3207 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003208 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003209psa_status_t psa_key_derivation_get_capacity(
3210 const psa_key_derivation_operation_t *operation,
3211 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003212
Gilles Peskine35675b62019-05-16 17:26:11 +02003213/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003214 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003215 * The capacity of a key derivation operation is the maximum number of bytes
3216 * that the key derivation operation can return from this point onwards.
3217 *
3218 * \param[in,out] operation The key derivation operation object to modify.
3219 * \param capacity The new capacity of the operation.
3220 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003221 * current capacity.
3222 *
3223 * \retval #PSA_SUCCESS
3224 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003225 * \p capacity is larger than the operation's current capacity.
3226 * In this case, the operation object remains valid and its capacity
3227 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003228 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003229 * The operation state is not valid (it must be active).
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003230 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003231 * \retval #PSA_ERROR_HARDWARE_FAILURE
3232 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003233 * \retval #PSA_ERROR_BAD_STATE
3234 * The library has not been previously initialized by psa_crypto_init().
3235 * It is implementation-dependent whether a failure to initialize
3236 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003237 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003238psa_status_t psa_key_derivation_set_capacity(
3239 psa_key_derivation_operation_t *operation,
3240 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003241
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003242/** Use the maximum possible capacity for a key derivation operation.
3243 *
3244 * Use this value as the capacity argument when setting up a key derivation
3245 * to indicate that the operation should have the maximum possible capacity.
3246 * The value of the maximum possible capacity depends on the key derivation
3247 * algorithm.
3248 */
3249#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3250
3251/** Provide an input for key derivation or key agreement.
3252 *
3253 * Which inputs are required and in what order depends on the algorithm.
3254 * Refer to the documentation of each key derivation or key agreement
3255 * algorithm for information.
3256 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003257 * This function passes direct inputs, which is usually correct for
3258 * non-secret inputs. To pass a secret input, which should be in a key
3259 * object, call psa_key_derivation_input_key() instead of this function.
3260 * Refer to the documentation of individual step types
3261 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3262 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003263 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003264 * If this function returns an error status, the operation enters an error
3265 * state and must be aborted by calling psa_key_derivation_abort().
3266 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003267 * \param[in,out] operation The key derivation operation object to use.
3268 * It must have been set up with
3269 * psa_key_derivation_setup() and must not
3270 * have produced any output yet.
3271 * \param step Which step the input data is for.
3272 * \param[in] data Input data to use.
3273 * \param data_length Size of the \p data buffer in bytes.
3274 *
3275 * \retval #PSA_SUCCESS
3276 * Success.
3277 * \retval #PSA_ERROR_INVALID_ARGUMENT
3278 * \c step is not compatible with the operation's algorithm.
3279 * \retval #PSA_ERROR_INVALID_ARGUMENT
3280 * \c step does not allow direct inputs.
3281 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3282 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3283 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003284 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003285 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003286 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003287 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003288 * \retval #PSA_ERROR_BAD_STATE
3289 * The library has not been previously initialized by psa_crypto_init().
3290 * It is implementation-dependent whether a failure to initialize
3291 * results in this error code.
3292 */
3293psa_status_t psa_key_derivation_input_bytes(
3294 psa_key_derivation_operation_t *operation,
3295 psa_key_derivation_step_t step,
3296 const uint8_t *data,
3297 size_t data_length);
3298
3299/** Provide an input for key derivation in the form of a key.
3300 *
3301 * Which inputs are required and in what order depends on the algorithm.
3302 * Refer to the documentation of each key derivation or key agreement
3303 * algorithm for information.
3304 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003305 * This function obtains input from a key object, which is usually correct for
3306 * secret inputs or for non-secret personalization strings kept in the key
3307 * store. To pass a non-secret parameter which is not in the key store,
3308 * call psa_key_derivation_input_bytes() instead of this function.
3309 * Refer to the documentation of individual step types
3310 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3311 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003312 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003313 * If this function returns an error status, the operation enters an error
3314 * state and must be aborted by calling psa_key_derivation_abort().
3315 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003316 * \param[in,out] operation The key derivation operation object to use.
3317 * It must have been set up with
3318 * psa_key_derivation_setup() and must not
3319 * have produced any output yet.
3320 * \param step Which step the input data is for.
3321 * \param handle Handle to the key. It must have an
3322 * appropriate type for \p step and must
3323 * allow the usage #PSA_KEY_USAGE_DERIVE.
3324 *
3325 * \retval #PSA_SUCCESS
3326 * Success.
3327 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003328 * \retval #PSA_ERROR_NOT_PERMITTED
3329 * \retval #PSA_ERROR_INVALID_ARGUMENT
3330 * \c step is not compatible with the operation's algorithm.
3331 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine224b0d62019-09-23 18:13:17 +02003332 * \c step does not allow key inputs of the given type
3333 * or does not allow key inputs at all.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003334 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3335 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3336 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003337 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003338 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003339 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003340 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003341 * \retval #PSA_ERROR_BAD_STATE
3342 * The library has not been previously initialized by psa_crypto_init().
3343 * It is implementation-dependent whether a failure to initialize
3344 * results in this error code.
3345 */
3346psa_status_t psa_key_derivation_input_key(
3347 psa_key_derivation_operation_t *operation,
3348 psa_key_derivation_step_t step,
3349 psa_key_handle_t handle);
3350
3351/** Perform a key agreement and use the shared secret as input to a key
3352 * derivation.
3353 *
3354 * A key agreement algorithm takes two inputs: a private key \p private_key
3355 * a public key \p peer_key.
3356 * The result of this function is passed as input to a key derivation.
3357 * The output of this key derivation can be extracted by reading from the
3358 * resulting operation to produce keys and other cryptographic material.
3359 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003360 * If this function returns an error status, the operation enters an error
3361 * state and must be aborted by calling psa_key_derivation_abort().
3362 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003363 * \param[in,out] operation The key derivation operation object to use.
3364 * It must have been set up with
3365 * psa_key_derivation_setup() with a
3366 * key agreement and derivation algorithm
3367 * \c alg (\c PSA_ALG_XXX value such that
3368 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3369 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3370 * is false).
3371 * The operation must be ready for an
3372 * input of the type given by \p step.
3373 * \param step Which step the input data is for.
3374 * \param private_key Handle to the private key to use.
3375 * \param[in] peer_key Public key of the peer. The peer key must be in the
3376 * same format that psa_import_key() accepts for the
3377 * public key type corresponding to the type of
3378 * private_key. That is, this function performs the
3379 * equivalent of
3380 * #psa_import_key(...,
3381 * `peer_key`, `peer_key_length`) where
3382 * with key attributes indicating the public key
3383 * type corresponding to the type of `private_key`.
3384 * For example, for EC keys, this means that peer_key
3385 * is interpreted as a point on the curve that the
3386 * private key is on. The standard formats for public
3387 * keys are documented in the documentation of
3388 * psa_export_public_key().
3389 * \param peer_key_length Size of \p peer_key in bytes.
3390 *
3391 * \retval #PSA_SUCCESS
3392 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01003393 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003394 * The operation state is not valid for this key agreement \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003395 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003396 * \retval #PSA_ERROR_NOT_PERMITTED
3397 * \retval #PSA_ERROR_INVALID_ARGUMENT
3398 * \c private_key is not compatible with \c alg,
3399 * or \p peer_key is not valid for \c alg or not compatible with
3400 * \c private_key.
3401 * \retval #PSA_ERROR_NOT_SUPPORTED
3402 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskine224b0d62019-09-23 18:13:17 +02003403 * \retval #PSA_ERROR_INVALID_ARGUMENT
3404 * \c step does not allow an input resulting from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003405 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3406 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3407 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003408 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003409 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003410 * \retval #PSA_ERROR_BAD_STATE
3411 * The library has not been previously initialized by psa_crypto_init().
3412 * It is implementation-dependent whether a failure to initialize
3413 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003414 */
3415psa_status_t psa_key_derivation_key_agreement(
3416 psa_key_derivation_operation_t *operation,
3417 psa_key_derivation_step_t step,
3418 psa_key_handle_t private_key,
3419 const uint8_t *peer_key,
3420 size_t peer_key_length);
3421
Gilles Peskine35675b62019-05-16 17:26:11 +02003422/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003423 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003424 * This function calculates output bytes from a key derivation algorithm and
3425 * return those bytes.
3426 * If you view the key derivation's output as a stream of bytes, this
3427 * function destructively reads the requested number of bytes from the
3428 * stream.
3429 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003430 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003431 * If this function returns an error status other than
3432 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003433 * state and must be aborted by calling psa_key_derivation_abort().
3434 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003435 * \param[in,out] operation The key derivation operation object to read from.
3436 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003437 * \param output_length Number of bytes to output.
3438 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003439 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003440 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003441 * The operation's capacity was less than
3442 * \p output_length bytes. Note that in this case,
3443 * no output is written to the output buffer.
3444 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003445 * subsequent calls to this function will not
3446 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003447 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003448 * The operation state is not valid (it must be active and completed
3449 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003450 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3451 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3452 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003453 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003454 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003455 * \retval #PSA_ERROR_BAD_STATE
3456 * The library has not been previously initialized by psa_crypto_init().
3457 * It is implementation-dependent whether a failure to initialize
3458 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003459 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003460psa_status_t psa_key_derivation_output_bytes(
3461 psa_key_derivation_operation_t *operation,
3462 uint8_t *output,
3463 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003464
Gilles Peskine35675b62019-05-16 17:26:11 +02003465/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003466 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003467 * This function calculates output bytes from a key derivation algorithm
3468 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003469 * The key's location, usage policy, type and size are taken from
3470 * \p attributes.
3471 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003472 * If you view the key derivation's output as a stream of bytes, this
3473 * function destructively reads as many bytes as required from the
3474 * stream.
3475 * The operation's capacity decreases by the number of bytes read.
3476 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003477 * If this function returns an error status other than
3478 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003479 * state and must be aborted by calling psa_key_derivation_abort().
3480 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003481 * How much output is produced and consumed from the operation, and how
3482 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003483 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003484 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003485 * of a given size, this function is functionally equivalent to
3486 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003487 * and passing the resulting output to #psa_import_key.
3488 * However, this function has a security benefit:
3489 * if the implementation provides an isolation boundary then
3490 * the key material is not exposed outside the isolation boundary.
3491 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003492 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003493 * The following key types defined in this specification follow this scheme:
3494 *
3495 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003496 * - #PSA_KEY_TYPE_ARC4;
3497 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003498 * - #PSA_KEY_TYPE_DERIVE;
3499 * - #PSA_KEY_TYPE_HMAC.
3500 *
3501 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003502 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003503 * Montgomery curve), this function always draws a byte string whose
3504 * length is determined by the curve, and sets the mandatory bits
3505 * accordingly. That is:
3506 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01003507 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003508 * string and process it as specified in RFC 7748 &sect;5.
Paul Elliott8ff510a2020-06-02 17:19:28 +01003509 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003510 * string and process it as specified in RFC 7748 &sect;5.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003511 *
3512 * - For key types for which the key is represented by a single sequence of
3513 * \p bits bits with constraints as to which bit sequences are acceptable,
3514 * this function draws a byte string of length (\p bits / 8) bytes rounded
3515 * up to the nearest whole number of bytes. If the resulting byte string
3516 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3517 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003518 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003519 * for the output produced by psa_export_key().
3520 * The following key types defined in this specification follow this scheme:
3521 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003522 * - #PSA_KEY_TYPE_DES.
3523 * Force-set the parity bits, but discard forbidden weak keys.
3524 * For 2-key and 3-key triple-DES, the three keys are generated
3525 * successively (for example, for 3-key triple-DES,
3526 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3527 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003528 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003529 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003530 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003531 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003532 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003533 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003534 * Weierstrass curve).
3535 * For these key types, interpret the byte string as integer
3536 * in big-endian order. Discard it if it is not in the range
3537 * [0, *N* - 2] where *N* is the boundary of the private key domain
3538 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003539 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003540 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003541 * This method allows compliance to NIST standards, specifically
3542 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003543 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3544 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3545 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3546 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003547 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003548 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003549 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003550 * implementation-defined.
3551 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003552 * In all cases, the data that is read is discarded from the operation.
3553 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003554 *
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003555 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3556 * the input to that step must be provided with psa_key_derivation_input_key().
3557 * Future versions of this specification may include additional restrictions
3558 * on the derived key based on the attributes and strength of the secret key.
3559 *
Gilles Peskine20628592019-04-19 19:29:50 +02003560 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003561 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003562 * \param[out] handle On success, a handle to the newly created key.
3563 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003564 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003565 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003566 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003567 * If the key is persistent, the key material and the key's metadata
3568 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003569 * \retval #PSA_ERROR_ALREADY_EXISTS
3570 * This is an attempt to create a persistent key, and there is
3571 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003572 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003573 * There was not enough data to create the desired key.
3574 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003575 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003576 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003577 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003578 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003579 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003580 * \retval #PSA_ERROR_INVALID_ARGUMENT
3581 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003582 * \retval #PSA_ERROR_NOT_PERMITTED
3583 * The #PSA_KEY_DERIVATION_INPUT_SECRET input was not provided through
3584 * a key.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003585 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003586 * The operation state is not valid (it must be active and completed
3587 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003588 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3589 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3590 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3591 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003592 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003593 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003594 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003595 * The library has not been previously initialized by psa_crypto_init().
3596 * It is implementation-dependent whether a failure to initialize
3597 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003598 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003599psa_status_t psa_key_derivation_output_key(
3600 const psa_key_attributes_t *attributes,
3601 psa_key_derivation_operation_t *operation,
3602 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003603
Gilles Peskine35675b62019-05-16 17:26:11 +02003604/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003605 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003606 * Aborting an operation frees all associated resources except for the \c
3607 * operation structure itself. Once aborted, the operation object can be reused
3608 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003609 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003610 * This function may be called at any time after the operation
3611 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003612 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003613 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3614 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003615 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003616 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003617 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003618 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003619 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3620 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003621 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003622 * \retval #PSA_ERROR_BAD_STATE
3623 * The library has not been previously initialized by psa_crypto_init().
3624 * It is implementation-dependent whether a failure to initialize
3625 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003626 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003627psa_status_t psa_key_derivation_abort(
3628 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003629
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003630/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003631 *
3632 * \warning The raw result of a key agreement algorithm such as finite-field
3633 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3634 * not be used directly as key material. It should instead be passed as
3635 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003636 * a key derivation, use psa_key_derivation_key_agreement() and other
3637 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003638 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003639 * \param alg The key agreement algorithm to compute
3640 * (\c PSA_ALG_XXX value such that
3641 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3642 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003643 * \param private_key Handle to the private key to use.
3644 * \param[in] peer_key Public key of the peer. It must be
3645 * in the same format that psa_import_key()
3646 * accepts. The standard formats for public
3647 * keys are documented in the documentation
3648 * of psa_export_public_key().
3649 * \param peer_key_length Size of \p peer_key in bytes.
3650 * \param[out] output Buffer where the decrypted message is to
3651 * be written.
3652 * \param output_size Size of the \c output buffer in bytes.
3653 * \param[out] output_length On success, the number of bytes
3654 * that make up the returned output.
3655 *
3656 * \retval #PSA_SUCCESS
3657 * Success.
3658 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003659 * \retval #PSA_ERROR_NOT_PERMITTED
3660 * \retval #PSA_ERROR_INVALID_ARGUMENT
3661 * \p alg is not a key agreement algorithm
3662 * \retval #PSA_ERROR_INVALID_ARGUMENT
3663 * \p private_key is not compatible with \p alg,
3664 * or \p peer_key is not valid for \p alg or not compatible with
3665 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003666 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3667 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003668 * \retval #PSA_ERROR_NOT_SUPPORTED
3669 * \p alg is not a supported key agreement algorithm.
3670 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3671 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3672 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003673 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003674 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003675 * \retval #PSA_ERROR_BAD_STATE
3676 * The library has not been previously initialized by psa_crypto_init().
3677 * It is implementation-dependent whether a failure to initialize
3678 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003679 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003680psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3681 psa_key_handle_t private_key,
3682 const uint8_t *peer_key,
3683 size_t peer_key_length,
3684 uint8_t *output,
3685 size_t output_size,
3686 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003687
Gilles Peskineea0fb492018-07-12 17:17:20 +02003688/**@}*/
3689
Gilles Peskineedd76872018-07-20 17:42:05 +02003690/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003691 * @{
3692 */
3693
3694/**
3695 * \brief Generate random bytes.
3696 *
3697 * \warning This function **can** fail! Callers MUST check the return status
3698 * and MUST NOT use the content of the output buffer if the return
3699 * status is not #PSA_SUCCESS.
3700 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003701 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003702 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003703 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003704 * \param output_size Number of bytes to generate and output.
3705 *
Gilles Peskine28538492018-07-11 17:34:00 +02003706 * \retval #PSA_SUCCESS
3707 * \retval #PSA_ERROR_NOT_SUPPORTED
3708 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003709 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003710 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3711 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003712 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003713 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003714 * The library has not been previously initialized by psa_crypto_init().
3715 * It is implementation-dependent whether a failure to initialize
3716 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003717 */
3718psa_status_t psa_generate_random(uint8_t *output,
3719 size_t output_size);
3720
3721/**
3722 * \brief Generate a key or key pair.
3723 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003724 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02003725 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003726 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003727 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003728 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003729 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003730 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003731 * the public exponent is 65537.
3732 * The modulus is a product of two probabilistic primes
3733 * between 2^{n-1} and 2^n where n is the bit size specified in the
3734 * attributes.
3735 *
Gilles Peskine20628592019-04-19 19:29:50 +02003736 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003737 * \param[out] handle On success, a handle to the newly created key.
3738 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003739 *
Gilles Peskine28538492018-07-11 17:34:00 +02003740 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003741 * Success.
3742 * If the key is persistent, the key material and the key's metadata
3743 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003744 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003745 * This is an attempt to create a persistent key, and there is
3746 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003747 * \retval #PSA_ERROR_NOT_SUPPORTED
3748 * \retval #PSA_ERROR_INVALID_ARGUMENT
3749 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3750 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3751 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3752 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003753 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003754 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3755 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003756 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003757 * The library has not been previously initialized by psa_crypto_init().
3758 * It is implementation-dependent whether a failure to initialize
3759 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003760 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003761psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003762 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003763
3764/**@}*/
3765
Gilles Peskinee59236f2018-01-27 23:32:46 +01003766#ifdef __cplusplus
3767}
3768#endif
3769
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003770/* The file "crypto_sizes.h" contains definitions for size calculation
3771 * macros whose definitions are implementation-specific. */
3772#include "crypto_sizes.h"
3773
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003774/* The file "crypto_struct.h" contains definitions for
3775 * implementation-specific structs that are declared above. */
3776#include "crypto_struct.h"
3777
3778/* The file "crypto_extra.h" contains vendor-specific definitions. This
3779 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003780#include "crypto_extra.h"
3781
3782#endif /* PSA_CRYPTO_H */