blob: e067cbdd11a60f939bccbd6549a5c811be21491a [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +020089 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +020090 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
Gilles Peskine105f67f2019-07-23 18:16:05 +020096/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +020097 * @{
98 */
99
Gilles Peskinea0c06552019-05-21 15:54:54 +0200100/** \def PSA_KEY_ATTRIBUTES_INIT
101 *
102 * This macro returns a suitable initializer for a key attribute structure
103 * of type #psa_key_attributes_t.
104 */
105#ifdef __DOXYGEN_ONLY__
106/* This is an example definition for documentation purposes.
107 * Implementations should define a suitable value in `crypto_struct.h`.
108 */
109#define PSA_KEY_ATTRIBUTES_INIT {0}
110#endif
111
112/** Return an initial value for a key attributes structure.
113 */
114static psa_key_attributes_t psa_key_attributes_init(void);
115
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200116/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200117 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200118 * If the attribute structure currently declares the key as volatile (which
119 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200120 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200121 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200122 * This function does not access storage, it merely stores the given
123 * value in the structure.
124 * The persistent key will be written to storage when the attribute
125 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200126 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200127 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200128 *
Gilles Peskine20628592019-04-19 19:29:50 +0200129 * This function may be declared as `static` (i.e. without external
130 * linkage). This function may be provided as a function-like macro,
131 * but in this case it must evaluate each of its arguments exactly once.
132 *
133 * \param[out] attributes The attribute structure to write to.
134 * \param id The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200135 */
136static void psa_set_key_id(psa_key_attributes_t *attributes,
137 psa_key_id_t id);
138
139/** Set the location of a persistent key.
140 *
141 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200142 * with psa_set_key_id(). By default, a key that has a persistent identifier
143 * is stored in the default storage area identifier by
144 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
145 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200146 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200147 * This function does not access storage, it merely stores the given
148 * value in the structure.
149 * The persistent key will be written to storage when the attribute
150 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200151 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200152 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200153 *
154 * This function may be declared as `static` (i.e. without external
155 * linkage). This function may be provided as a function-like macro,
156 * but in this case it must evaluate each of its arguments exactly once.
157 *
158 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200159 * \param lifetime The lifetime for the key.
160 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200161 * key will be volatile, and the key identifier
162 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200163 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200164static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
165 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200166
Gilles Peskine20628592019-04-19 19:29:50 +0200167/** Retrieve the key identifier from key attributes.
168 *
169 * This function may be declared as `static` (i.e. without external
170 * linkage). This function may be provided as a function-like macro,
171 * but in this case it must evaluate its argument exactly once.
172 *
173 * \param[in] attributes The key attribute structure to query.
174 *
175 * \return The persistent identifier stored in the attribute structure.
176 * This value is unspecified if the attribute structure declares
177 * the key as volatile.
178 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200179static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
180
Gilles Peskine20628592019-04-19 19:29:50 +0200181/** Retrieve the lifetime from key attributes.
182 *
183 * This function may be declared as `static` (i.e. without external
184 * linkage). This function may be provided as a function-like macro,
185 * but in this case it must evaluate its argument exactly once.
186 *
187 * \param[in] attributes The key attribute structure to query.
188 *
189 * \return The lifetime value stored in the attribute structure.
190 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200191static psa_key_lifetime_t psa_get_key_lifetime(
192 const psa_key_attributes_t *attributes);
193
Gilles Peskine20628592019-04-19 19:29:50 +0200194/** Declare usage flags for a key.
195 *
196 * Usage flags are part of a key's usage policy. They encode what
197 * kind of operations are permitted on the key. For more details,
198 * refer to the documentation of the type #psa_key_usage_t.
199 *
200 * This function overwrites any usage flags
201 * previously set in \p attributes.
202 *
203 * This function may be declared as `static` (i.e. without external
204 * linkage). This function may be provided as a function-like macro,
205 * but in this case it must evaluate each of its arguments exactly once.
206 *
207 * \param[out] attributes The attribute structure to write to.
208 * \param usage_flags The usage flags to write.
209 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200210static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
211 psa_key_usage_t usage_flags);
212
Gilles Peskine20628592019-04-19 19:29:50 +0200213/** Retrieve the usage flags from key attributes.
214 *
215 * This function may be declared as `static` (i.e. without external
216 * linkage). This function may be provided as a function-like macro,
217 * but in this case it must evaluate its argument exactly once.
218 *
219 * \param[in] attributes The key attribute structure to query.
220 *
221 * \return The usage flags stored in the attribute structure.
222 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200223static psa_key_usage_t psa_get_key_usage_flags(
224 const psa_key_attributes_t *attributes);
225
Gilles Peskine20628592019-04-19 19:29:50 +0200226/** Declare the permitted algorithm policy for a key.
227 *
228 * The permitted algorithm policy of a key encodes which algorithm or
229 * algorithms are permitted to be used with this key.
230 *
231 * This function overwrites any algorithm policy
232 * previously set in \p attributes.
233 *
234 * This function may be declared as `static` (i.e. without external
235 * linkage). This function may be provided as a function-like macro,
236 * but in this case it must evaluate each of its arguments exactly once.
237 *
238 * \param[out] attributes The attribute structure to write to.
239 * \param alg The permitted algorithm policy to write.
240 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200241static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
242 psa_algorithm_t alg);
243
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100244
Gilles Peskine20628592019-04-19 19:29:50 +0200245/** Retrieve the algorithm policy from key attributes.
246 *
247 * This function may be declared as `static` (i.e. without external
248 * linkage). This function may be provided as a function-like macro,
249 * but in this case it must evaluate its argument exactly once.
250 *
251 * \param[in] attributes The key attribute structure to query.
252 *
253 * \return The algorithm stored in the attribute structure.
254 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200255static psa_algorithm_t psa_get_key_algorithm(
256 const psa_key_attributes_t *attributes);
257
Gilles Peskine20628592019-04-19 19:29:50 +0200258/** Declare the type of a key.
259 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200260 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200261 * previously set in \p attributes.
262 *
263 * This function may be declared as `static` (i.e. without external
264 * linkage). This function may be provided as a function-like macro,
265 * but in this case it must evaluate each of its arguments exactly once.
266 *
267 * \param[out] attributes The attribute structure to write to.
268 * \param type The key type to write.
269 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200270static void psa_set_key_type(psa_key_attributes_t *attributes,
271 psa_key_type_t type);
272
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100273
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200274/** Declare the size of a key.
275 *
276 * This function overwrites any key size previously set in \p attributes.
277 *
278 * This function may be declared as `static` (i.e. without external
279 * linkage). This function may be provided as a function-like macro,
280 * but in this case it must evaluate each of its arguments exactly once.
281 *
282 * \param[out] attributes The attribute structure to write to.
283 * \param bits The key size in bits.
284 */
285static void psa_set_key_bits(psa_key_attributes_t *attributes,
286 size_t bits);
287
Gilles Peskine20628592019-04-19 19:29:50 +0200288/** Retrieve the key type from key attributes.
289 *
290 * This function may be declared as `static` (i.e. without external
291 * linkage). This function may be provided as a function-like macro,
292 * but in this case it must evaluate its argument exactly once.
293 *
294 * \param[in] attributes The key attribute structure to query.
295 *
296 * \return The key type stored in the attribute structure.
297 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200298static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
299
Gilles Peskine20628592019-04-19 19:29:50 +0200300/** Retrieve the key size from key attributes.
301 *
302 * This function may be declared as `static` (i.e. without external
303 * linkage). This function may be provided as a function-like macro,
304 * but in this case it must evaluate its argument exactly once.
305 *
306 * \param[in] attributes The key attribute structure to query.
307 *
308 * \return The key size stored in the attribute structure, in bits.
309 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200310static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
311
Gilles Peskine20628592019-04-19 19:29:50 +0200312/** Retrieve the attributes of a key.
313 *
314 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200315 * psa_reset_key_attributes(). It then copies the attributes of
316 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200317 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200318 * \note This function may allocate memory or other resources.
319 * Once you have called this function on an attribute structure,
320 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200321 *
Gilles Peskine20628592019-04-19 19:29:50 +0200322 * \param[in] handle Handle to the key to query.
323 * \param[in,out] attributes On success, the attributes of the key.
324 * On failure, equivalent to a
325 * freshly-initialized structure.
326 *
327 * \retval #PSA_SUCCESS
328 * \retval #PSA_ERROR_INVALID_HANDLE
329 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
330 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
331 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200332psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
333 psa_key_attributes_t *attributes);
334
Gilles Peskine20628592019-04-19 19:29:50 +0200335/** Reset a key attribute structure to a freshly initialized state.
336 *
337 * You must initialize the attribute structure as described in the
338 * documentation of the type #psa_key_attributes_t before calling this
339 * function. Once the structure has been initialized, you may call this
340 * function at any time.
341 *
342 * This function frees any auxiliary resources that the structure
343 * may contain.
344 *
345 * \param[in,out] attributes The attribute structure to reset.
346 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200347void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200348
Gilles Peskine87a5e562019-04-17 12:28:25 +0200349/**@}*/
350
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100351/** \defgroup key_management Key management
352 * @{
353 */
354
Gilles Peskinef535eb22018-11-30 14:08:36 +0100355/** Open a handle to an existing persistent key.
356 *
Gilles Peskine4754cde2019-05-21 15:56:29 +0200357 * Open a handle to a persistent key. A key is persistent if it was created
358 * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
359 * always has a nonzero key identifier, set with psa_set_key_id() when
360 * creating the key. Implementations may provide additional pre-provisioned
Andrew Thoelke203491c2019-08-21 17:55:30 +0100361 * keys that can be opened with psa_open_key(). Such keys have a key identifier
362 * in the vendor range, as documented in the description of #psa_key_id_t.
Gilles Peskine4754cde2019-05-21 15:56:29 +0200363 *
364 * The application must eventually close the handle with psa_close_key()
365 * to release associated resources. If the application dies without calling
366 * psa_close_key(), the implementation should perform the equivalent of a
367 * call to psa_close_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100368 *
Andrew Thoelke9741b112019-08-21 18:20:41 +0100369 * Some implementations permit an application to open the same key multiple
370 * times. Applications that rely on this behavior will not be portable to
371 * implementations that only permit a single key handle to be opened. See
372 * also :ref:\`key-handles\`.
373 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100374 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100375 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100376 *
377 * \retval #PSA_SUCCESS
378 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100379 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100380 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Andrew Thoelke9741b112019-08-21 18:20:41 +0100381 * The implementation does not have sufficient resources to open the
382 * key. This can be due to reaching an implementation limit on the
383 * number of open keys, the number of open key handles, or available
384 * memory.
David Saadab4ecc272019-02-14 13:48:10 +0200385 * \retval #PSA_ERROR_DOES_NOT_EXIST
Andrew Thoelke9741b112019-08-21 18:20:41 +0100386 * There is no persistent key with key identifier \p id.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100387 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrew Thoelke9741b112019-08-21 18:20:41 +0100388 * \p id is not a valid persistent key identifier.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100389 * \retval #PSA_ERROR_NOT_PERMITTED
390 * The specified key exists, but the application does not have the
391 * permission to access it. Note that this specification does not
392 * define any way to create such a key, but it may be possible
393 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200394 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
395 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100396 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200397psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100398 psa_key_handle_t *handle);
399
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100400
Gilles Peskinef535eb22018-11-30 14:08:36 +0100401/** Close a key handle.
402 *
403 * If the handle designates a volatile key, destroy the key material and
404 * free all associated resources, just like psa_destroy_key().
405 *
406 * If the handle designates a persistent key, free all resources associated
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100407 * with the key in volatile memory. The key in persistent storage is
Gilles Peskinef535eb22018-11-30 14:08:36 +0100408 * not affected and can be opened again later with psa_open_key().
409 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100410 * If the key is currently in use in a multipart operation,
411 * the multipart operation is aborted.
412 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100413 * \param handle The key handle to close.
414 *
415 * \retval #PSA_SUCCESS
416 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100417 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100418 */
419psa_status_t psa_close_key(psa_key_handle_t handle);
420
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100421/**@}*/
422
423/** \defgroup import_export Key import and export
424 * @{
425 */
426
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100427/**
428 * \brief Import a key in binary format.
429 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100430 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100431 * documentation of psa_export_public_key() for the format of public keys
432 * and to the documentation of psa_export_key() for the format for
433 * other key types.
434 *
435 * This specification supports a single format for each key type.
436 * Implementations may support other formats as long as the standard
437 * format is supported. Implementations that support other formats
438 * should ensure that the formats are clearly unambiguous so as to
439 * minimize the risk that an invalid input is accidentally interpreted
440 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100441 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100442
Gilles Peskine20628592019-04-19 19:29:50 +0200443 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200444 * The key size is always determined from the
445 * \p data buffer.
446 * If the key size in \p attributes is nonzero,
447 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200448 * \param[out] handle On success, a handle to the newly created key.
449 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100450 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200451 * buffer is interpreted according to the type declared
452 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200453 * All implementations must support at least the format
454 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100455 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200456 * the chosen type. Implementations may allow other
457 * formats, but should be conservative: implementations
458 * should err on the side of rejecting content if it
459 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200460 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100461 *
Gilles Peskine28538492018-07-11 17:34:00 +0200462 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100463 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100464 * If the key is persistent, the key material and the key's metadata
465 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200466 * \retval #PSA_ERROR_ALREADY_EXISTS
467 * This is an attempt to create a persistent key, and there is
468 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200469 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200470 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200471 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200472 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200473 * The key attributes, as a whole, are invalid.
474 * \retval #PSA_ERROR_INVALID_ARGUMENT
475 * The key data is not correctly formatted.
476 * \retval #PSA_ERROR_INVALID_ARGUMENT
477 * The size in \p attributes is nonzero and does not match the size
478 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200479 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
480 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
481 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100482 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200483 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200484 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300485 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300486 * The library has not been previously initialized by psa_crypto_init().
487 * It is implementation-dependent whether a failure to initialize
488 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100489 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200490psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100491 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200492 size_t data_length,
493 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100494
495/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100496 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200497 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100498 * This function destroys a key from both volatile
Gilles Peskine154bd952018-04-19 08:38:16 +0200499 * memory and, if applicable, non-volatile storage. Implementations shall
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100500 * make a best effort to ensure that that the key material cannot be recovered.
Gilles Peskine154bd952018-04-19 08:38:16 +0200501 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100502 * This function also erases any metadata such as policies and frees all
503 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200504 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100505 * \param handle Handle to the key to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100506 *
Gilles Peskine28538492018-07-11 17:34:00 +0200507 * \retval #PSA_SUCCESS
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100508 * The key material has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200509 * \retval #PSA_ERROR_NOT_PERMITTED
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100510 * The key cannot be erased because it is
Gilles Peskine65eb8582018-04-19 08:28:58 +0200511 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100512 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200513 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200514 * There was an failure in communication with the cryptoprocessor.
515 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200516 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200517 * The storage is corrupted. Implementations shall make a best effort
518 * to erase key material even in this stage, however applications
519 * should be aware that it may be impossible to guarantee that the
520 * key material is not recoverable in such cases.
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200521 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200522 * An unexpected condition which is not a storage corruption or
523 * a communication failure occurred. The cryptoprocessor may have
524 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300525 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300526 * The library has not been previously initialized by psa_crypto_init().
527 * It is implementation-dependent whether a failure to initialize
528 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100529 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100530psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100531
532/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100533 * \brief Export a key in binary format.
534 *
535 * The output of this function can be passed to psa_import_key() to
536 * create an equivalent object.
537 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100538 * If the implementation of psa_import_key() supports other formats
539 * beyond the format specified here, the output from psa_export_key()
540 * must use the representation specified here, not the original
541 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100543 * For standard key types, the output format is as follows:
544 *
545 * - For symmetric keys (including MAC keys), the format is the
546 * raw bytes of the key.
547 * - For DES, the key data consists of 8 bytes. The parity bits must be
548 * correct.
549 * - For Triple-DES, the format is the concatenation of the
550 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200551 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200552 * is the non-encrypted DER encoding of the representation defined by
553 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
554 * ```
555 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200556 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200557 * modulus INTEGER, -- n
558 * publicExponent INTEGER, -- e
559 * privateExponent INTEGER, -- d
560 * prime1 INTEGER, -- p
561 * prime2 INTEGER, -- q
562 * exponent1 INTEGER, -- d mod (p-1)
563 * exponent2 INTEGER, -- d mod (q-1)
564 * coefficient INTEGER, -- (inverse of q) mod p
565 * }
566 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200567 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200568 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100569 * a representation of the private value as a `ceiling(m/8)`-byte string
570 * where `m` is the bit size associated with the curve, i.e. the bit size
571 * of the order of the curve's coordinate field. This byte string is
572 * in little-endian order for Montgomery curves (curve types
573 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
574 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
575 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100576 * This is the content of the `privateKey` field of the `ECPrivateKey`
577 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200578 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200579 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000580 * format is the representation of the private key `x` as a big-endian byte
581 * string. The length of the byte string is the private key size in bytes
582 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200583 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
584 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100585 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200586 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
587 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100588 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200589 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200590 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200591 * \param[out] data_length On success, the number of bytes
592 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100593 *
Gilles Peskine28538492018-07-11 17:34:00 +0200594 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100595 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200596 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200597 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200598 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100599 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200600 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
601 * The size of the \p data buffer is too small. You can determine a
602 * sufficient buffer size by calling
603 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
604 * where \c type is the key type
605 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200606 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
607 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200608 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300609 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300610 * 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.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100613 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100614psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100615 uint8_t *data,
616 size_t data_size,
617 size_t *data_length);
618
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100619/**
620 * \brief Export a public key or the public part of a key pair in binary format.
621 *
622 * The output of this function can be passed to psa_import_key() to
623 * create an object that is equivalent to the public key.
624 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000625 * This specification supports a single format for each key type.
626 * Implementations may support other formats as long as the standard
627 * format is supported. Implementations that support other formats
628 * should ensure that the formats are clearly unambiguous so as to
629 * minimize the risk that an invalid input is accidentally interpreted
630 * according to a different format.
631 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000632 * For standard key types, the output format is as follows:
633 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
634 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
635 * ```
636 * RSAPublicKey ::= SEQUENCE {
637 * modulus INTEGER, -- n
638 * publicExponent INTEGER } -- e
639 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000640 * - For elliptic curve public keys (key types for which
641 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
642 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
643 * Let `m` be the bit size associated with the curve, i.e. the bit size of
644 * `q` for a curve over `F_q`. The representation consists of:
645 * - The byte 0x04;
646 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
647 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200648 * - For Diffie-Hellman key exchange public keys (key types for which
649 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000650 * the format is the representation of the public key `y = g^x mod p` as a
651 * big-endian byte string. The length of the byte string is the length of the
652 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100653 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200654 * Exporting a public key object or the public part of a key pair is
655 * always permitted, regardless of the key's usage flags.
656 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100657 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200658 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200659 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200660 * \param[out] data_length On success, the number of bytes
661 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100662 *
Gilles Peskine28538492018-07-11 17:34:00 +0200663 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100664 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200665 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200666 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200667 * The key is neither a public key nor a key pair.
668 * \retval #PSA_ERROR_NOT_SUPPORTED
669 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
670 * The size of the \p data buffer is too small. You can determine a
671 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200672 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200673 * where \c type is the key type
674 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200675 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
676 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200677 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300678 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300679 * The library has not been previously initialized by psa_crypto_init().
680 * It is implementation-dependent whether a failure to initialize
681 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100682 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100683psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100684 uint8_t *data,
685 size_t data_size,
686 size_t *data_length);
687
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100688/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100689 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100690 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000691 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100692 * This function is primarily useful to copy a key from one location
693 * to another, since it populates a key using the material from
694 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200695 *
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100696 * This function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100697 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100698 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200699 * The policy on the source key must have the usage flag
700 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200701 * This flag is sufficient to permit the copy if the key has the lifetime
702 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
703 * Some secure elements do not provide a way to copy a key without
704 * making it extractable from the secure element. If a key is located
705 * in such a secure element, then the key must have both usage flags
706 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
707 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200708 *
Gilles Peskine20628592019-04-19 19:29:50 +0200709 * The resulting key may only be used in a way that conforms to
710 * both the policy of the original key and the policy specified in
711 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100712 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200713 * usage flags on the source policy and the usage flags in \p attributes.
714 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100715 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200716 * - If either of the policies allows an algorithm and the other policy
717 * allows a wildcard-based algorithm policy that includes this algorithm,
718 * the resulting key allows the same algorithm.
719 * - If the policies do not allow any algorithm in common, this function
720 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200721 *
Gilles Peskine20628592019-04-19 19:29:50 +0200722 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100723 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200724 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100725 * \param source_handle The key to copy. It must be a valid key handle.
Gilles Peskine20628592019-04-19 19:29:50 +0200726 * \param[in] attributes The attributes for the new key.
727 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200728 * - The key type and size may be 0. If either is
729 * nonzero, it must match the corresponding
730 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200731 * - The key location (the lifetime and, for
732 * persistent keys, the key identifier) is
733 * used directly.
734 * - The policy constraints (usage flags and
735 * algorithm policy) are combined from
736 * the source key and \p attributes so that
737 * both sets of restrictions apply, as
738 * described in the documentation of this function.
739 * \param[out] target_handle On success, a handle to the newly created key.
740 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200741 *
742 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100743 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200744 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200745 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200746 * This is an attempt to create a persistent key, and there is
747 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200748 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200749 * The lifetime or identifier in \p attributes are invalid.
750 * \retval #PSA_ERROR_INVALID_ARGUMENT
751 * The policy constraints on the source and specified in
752 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200753 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200754 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200755 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100756 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200757 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
758 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100759 * The source key is not exportable and its lifetime does not
760 * allow copying it to the target's lifetime.
761 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
762 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200763 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
764 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200765 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100766 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100767psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200768 const psa_key_attributes_t *attributes,
769 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100770
771/**@}*/
772
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100773/** \defgroup hash Message digests
774 * @{
775 */
776
Gilles Peskine69647a42019-01-14 20:18:12 +0100777/** Calculate the hash (digest) of a message.
778 *
779 * \note To verify the hash of a message against an
780 * expected value, use psa_hash_compare() instead.
781 *
782 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
783 * such that #PSA_ALG_IS_HASH(\p alg) is true).
784 * \param[in] input Buffer containing the message to hash.
785 * \param input_length Size of the \p input buffer in bytes.
786 * \param[out] hash Buffer where the hash is to be written.
787 * \param hash_size Size of the \p hash buffer in bytes.
788 * \param[out] hash_length On success, the number of bytes
789 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100790 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100791 *
792 * \retval #PSA_SUCCESS
793 * Success.
794 * \retval #PSA_ERROR_NOT_SUPPORTED
795 * \p alg is not supported or is not a hash algorithm.
796 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
797 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
798 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200799 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100800 */
801psa_status_t psa_hash_compute(psa_algorithm_t alg,
802 const uint8_t *input,
803 size_t input_length,
804 uint8_t *hash,
805 size_t hash_size,
806 size_t *hash_length);
807
808/** Calculate the hash (digest) of a message and compare it with a
809 * reference value.
810 *
811 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
812 * such that #PSA_ALG_IS_HASH(\p alg) is true).
813 * \param[in] input Buffer containing the message to hash.
814 * \param input_length Size of the \p input buffer in bytes.
815 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100816 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100817 *
818 * \retval #PSA_SUCCESS
819 * The expected hash is identical to the actual hash of the input.
820 * \retval #PSA_ERROR_INVALID_SIGNATURE
821 * The hash of the message was calculated successfully, but it
822 * differs from the expected hash.
823 * \retval #PSA_ERROR_NOT_SUPPORTED
824 * \p alg is not supported or is not a hash algorithm.
825 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
826 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
827 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200828 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100829 */
830psa_status_t psa_hash_compare(psa_algorithm_t alg,
831 const uint8_t *input,
832 size_t input_length,
833 const uint8_t *hash,
834 const size_t hash_length);
835
Gilles Peskine308b91d2018-02-08 09:47:44 +0100836/** The type of the state data structure for multipart hash operations.
837 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000838 * Before calling any function on a hash operation object, the application must
839 * initialize it by any of the following means:
840 * - Set the structure to all-bits-zero, for example:
841 * \code
842 * psa_hash_operation_t operation;
843 * memset(&operation, 0, sizeof(operation));
844 * \endcode
845 * - Initialize the structure to logical zero values, for example:
846 * \code
847 * psa_hash_operation_t operation = {0};
848 * \endcode
849 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
850 * for example:
851 * \code
852 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
853 * \endcode
854 * - Assign the result of the function psa_hash_operation_init()
855 * to the structure, for example:
856 * \code
857 * psa_hash_operation_t operation;
858 * operation = psa_hash_operation_init();
859 * \endcode
860 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100861 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100862 * make any assumptions about the content of this structure except
863 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100864typedef struct psa_hash_operation_s psa_hash_operation_t;
865
Jaeden Amero6a25b412019-01-04 11:47:44 +0000866/** \def PSA_HASH_OPERATION_INIT
867 *
868 * This macro returns a suitable initializer for a hash operation object
869 * of type #psa_hash_operation_t.
870 */
871#ifdef __DOXYGEN_ONLY__
872/* This is an example definition for documentation purposes.
873 * Implementations should define a suitable value in `crypto_struct.h`.
874 */
875#define PSA_HASH_OPERATION_INIT {0}
876#endif
877
878/** Return an initial value for a hash operation object.
879 */
880static psa_hash_operation_t psa_hash_operation_init(void);
881
Gilles Peskinef45adda2019-01-14 18:29:18 +0100882/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100883 *
884 * The sequence of operations to calculate a hash (message digest)
885 * is as follows:
886 * -# Allocate an operation object which will be passed to all the functions
887 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000888 * -# Initialize the operation object with one of the methods described in the
889 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200890 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100891 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100892 * of the message each time. The hash that is calculated is the hash
893 * of the concatenation of these messages in order.
894 * -# To calculate the hash, call psa_hash_finish().
895 * To compare the hash with an expected value, call psa_hash_verify().
896 *
897 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000898 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100899 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200900 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100901 * eventually terminate the operation. The following events terminate an
902 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100903 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100904 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100905 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000906 * \param[in,out] operation The operation object to set up. It must have
907 * been initialized as per the documentation for
908 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200909 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
910 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100911 *
Gilles Peskine28538492018-07-11 17:34:00 +0200912 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100913 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200914 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200915 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100916 * \retval #PSA_ERROR_BAD_STATE
917 * The operation state is not valid (already set up and not
918 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200919 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
920 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
921 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200922 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100923 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200924psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100925 psa_algorithm_t alg);
926
Gilles Peskine308b91d2018-02-08 09:47:44 +0100927/** Add a message fragment to a multipart hash operation.
928 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200929 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100930 *
931 * If this function returns an error status, the operation becomes inactive.
932 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200933 * \param[in,out] operation Active hash operation.
934 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200935 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100936 *
Gilles Peskine28538492018-07-11 17:34:00 +0200937 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100938 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200939 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100940 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200941 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
942 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
943 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200944 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100945 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100946psa_status_t psa_hash_update(psa_hash_operation_t *operation,
947 const uint8_t *input,
948 size_t input_length);
949
Gilles Peskine308b91d2018-02-08 09:47:44 +0100950/** Finish the calculation of the hash of a message.
951 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200952 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100953 * This function calculates the hash of the message formed by concatenating
954 * the inputs passed to preceding calls to psa_hash_update().
955 *
956 * When this function returns, the operation becomes inactive.
957 *
958 * \warning Applications should not call this function if they expect
959 * a specific value for the hash. Call psa_hash_verify() instead.
960 * Beware that comparing integrity or authenticity data such as
961 * hash values with a function such as \c memcmp is risky
962 * because the time taken by the comparison may leak information
963 * about the hashed data which could allow an attacker to guess
964 * a valid hash and thereby bypass security controls.
965 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200966 * \param[in,out] operation Active hash operation.
967 * \param[out] hash Buffer where the hash is to be written.
968 * \param hash_size Size of the \p hash buffer in bytes.
969 * \param[out] hash_length On success, the number of bytes
970 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200971 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200972 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100973 *
Gilles Peskine28538492018-07-11 17:34:00 +0200974 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100975 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200976 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100977 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200978 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200979 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200980 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100981 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200982 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
983 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
984 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200985 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100986 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100987psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
988 uint8_t *hash,
989 size_t hash_size,
990 size_t *hash_length);
991
Gilles Peskine308b91d2018-02-08 09:47:44 +0100992/** Finish the calculation of the hash of a message and compare it with
993 * an expected value.
994 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200995 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100996 * This function calculates the hash of the message formed by concatenating
997 * the inputs passed to preceding calls to psa_hash_update(). It then
998 * compares the calculated hash with the expected hash passed as a
999 * parameter to this function.
1000 *
1001 * When this function returns, the operation becomes inactive.
1002 *
Gilles Peskine19067982018-03-20 17:54:53 +01001003 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001004 * comparison between the actual hash and the expected hash is performed
1005 * in constant time.
1006 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001007 * \param[in,out] operation Active hash operation.
1008 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001009 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001010 *
Gilles Peskine28538492018-07-11 17:34:00 +02001011 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001012 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001013 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001014 * The hash of the message was calculated successfully, but it
1015 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001016 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001017 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001018 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1019 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1020 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001021 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001022 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001023psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1024 const uint8_t *hash,
1025 size_t hash_length);
1026
Gilles Peskine308b91d2018-02-08 09:47:44 +01001027/** Abort a hash operation.
1028 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001029 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001030 * \p operation structure itself. Once aborted, the operation object
1031 * can be reused for another operation by calling
1032 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001033 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001034 * You may call this function any time after the operation object has
1035 * been initialized by any of the following methods:
1036 * - A call to psa_hash_setup(), whether it succeeds or not.
1037 * - Initializing the \c struct to all-bits-zero.
1038 * - Initializing the \c struct to logical zeros, e.g.
1039 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001040 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001041 * In particular, calling psa_hash_abort() after the operation has been
1042 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1043 * psa_hash_verify() is safe and has no effect.
1044 *
1045 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001046 *
Gilles Peskine28538492018-07-11 17:34:00 +02001047 * \retval #PSA_SUCCESS
1048 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001049 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001050 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1051 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001052 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001053 */
1054psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001055
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001056/** Clone a hash operation.
1057 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001058 * This function copies the state of an ongoing hash operation to
1059 * a new operation object. In other words, this function is equivalent
1060 * to calling psa_hash_setup() on \p target_operation with the same
1061 * algorithm that \p source_operation was set up for, then
1062 * psa_hash_update() on \p target_operation with the same input that
1063 * that was passed to \p source_operation. After this function returns, the
1064 * two objects are independent, i.e. subsequent calls involving one of
1065 * the objects do not affect the other object.
1066 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001067 * \param[in] source_operation The active hash operation to clone.
1068 * \param[in,out] target_operation The operation object to set up.
1069 * It must be initialized but not active.
1070 *
1071 * \retval #PSA_SUCCESS
1072 * \retval #PSA_ERROR_BAD_STATE
1073 * \p source_operation is not an active hash operation.
1074 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001075 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001076 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1077 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001078 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001079 */
1080psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1081 psa_hash_operation_t *target_operation);
1082
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001083/**@}*/
1084
Gilles Peskine8c9def32018-02-08 10:02:12 +01001085/** \defgroup MAC Message authentication codes
1086 * @{
1087 */
1088
Gilles Peskine69647a42019-01-14 20:18:12 +01001089/** Calculate the MAC (message authentication code) of a message.
1090 *
1091 * \note To verify the MAC of a message against an
1092 * expected value, use psa_mac_verify() instead.
1093 * Beware that comparing integrity or authenticity data such as
1094 * MAC values with a function such as \c memcmp is risky
1095 * because the time taken by the comparison may leak information
1096 * about the MAC value which could allow an attacker to guess
1097 * a valid MAC and thereby bypass security controls.
1098 *
1099 * \param handle Handle to the key to use for the operation.
1100 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001101 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001102 * \param[in] input Buffer containing the input message.
1103 * \param input_length Size of the \p input buffer in bytes.
1104 * \param[out] mac Buffer where the MAC value is to be written.
1105 * \param mac_size Size of the \p mac buffer in bytes.
1106 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001107 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001108 *
1109 * \retval #PSA_SUCCESS
1110 * Success.
1111 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001112 * \retval #PSA_ERROR_NOT_PERMITTED
1113 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001114 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001115 * \retval #PSA_ERROR_NOT_SUPPORTED
1116 * \p alg is not supported or is not a MAC algorithm.
1117 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1118 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1119 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001120 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001121 * \retval #PSA_ERROR_BAD_STATE
1122 * The library has not been previously initialized by psa_crypto_init().
1123 * It is implementation-dependent whether a failure to initialize
1124 * results in this error code.
1125 */
1126psa_status_t psa_mac_compute(psa_key_handle_t handle,
1127 psa_algorithm_t alg,
1128 const uint8_t *input,
1129 size_t input_length,
1130 uint8_t *mac,
1131 size_t mac_size,
1132 size_t *mac_length);
1133
1134/** Calculate the MAC of a message and compare it with a reference value.
1135 *
1136 * \param handle Handle to the key to use for the operation.
1137 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001138 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001139 * \param[in] input Buffer containing the input message.
1140 * \param input_length Size of the \p input buffer in bytes.
1141 * \param[out] mac Buffer containing the expected MAC value.
1142 * \param mac_length Size of the \p mac buffer in bytes.
1143 *
1144 * \retval #PSA_SUCCESS
1145 * The expected MAC is identical to the actual MAC of the input.
1146 * \retval #PSA_ERROR_INVALID_SIGNATURE
1147 * The MAC of the message was calculated successfully, but it
1148 * differs from the expected value.
1149 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001150 * \retval #PSA_ERROR_NOT_PERMITTED
1151 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001152 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001153 * \retval #PSA_ERROR_NOT_SUPPORTED
1154 * \p alg is not supported or is not a MAC algorithm.
1155 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1156 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1157 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001158 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001159 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001160psa_status_t psa_mac_verify(psa_key_handle_t handle,
1161 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001162 const uint8_t *input,
1163 size_t input_length,
1164 const uint8_t *mac,
1165 const size_t mac_length);
1166
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001167/** The type of the state data structure for multipart MAC operations.
1168 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001169 * Before calling any function on a MAC operation object, the application must
1170 * initialize it by any of the following means:
1171 * - Set the structure to all-bits-zero, for example:
1172 * \code
1173 * psa_mac_operation_t operation;
1174 * memset(&operation, 0, sizeof(operation));
1175 * \endcode
1176 * - Initialize the structure to logical zero values, for example:
1177 * \code
1178 * psa_mac_operation_t operation = {0};
1179 * \endcode
1180 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1181 * for example:
1182 * \code
1183 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1184 * \endcode
1185 * - Assign the result of the function psa_mac_operation_init()
1186 * to the structure, for example:
1187 * \code
1188 * psa_mac_operation_t operation;
1189 * operation = psa_mac_operation_init();
1190 * \endcode
1191 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001192 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001193 * make any assumptions about the content of this structure except
1194 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001195typedef struct psa_mac_operation_s psa_mac_operation_t;
1196
Jaeden Amero769ce272019-01-04 11:48:03 +00001197/** \def PSA_MAC_OPERATION_INIT
1198 *
1199 * This macro returns a suitable initializer for a MAC operation object of type
1200 * #psa_mac_operation_t.
1201 */
1202#ifdef __DOXYGEN_ONLY__
1203/* This is an example definition for documentation purposes.
1204 * Implementations should define a suitable value in `crypto_struct.h`.
1205 */
1206#define PSA_MAC_OPERATION_INIT {0}
1207#endif
1208
1209/** Return an initial value for a MAC operation object.
1210 */
1211static psa_mac_operation_t psa_mac_operation_init(void);
1212
Gilles Peskinef45adda2019-01-14 18:29:18 +01001213/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001214 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001215 * This function sets up the calculation of the MAC
1216 * (message authentication code) of a byte string.
1217 * To verify the MAC of a message against an
1218 * expected value, use psa_mac_verify_setup() instead.
1219 *
1220 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001221 * -# Allocate an operation object which will be passed to all the functions
1222 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001223 * -# Initialize the operation object with one of the methods described in the
1224 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001225 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001226 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1227 * of the message each time. The MAC that is calculated is the MAC
1228 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001229 * -# At the end of the message, call psa_mac_sign_finish() to finish
1230 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001231 *
1232 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001233 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001234 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001235 * After a successful call to psa_mac_sign_setup(), the application must
1236 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001237 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001238 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001239 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001240 * \param[in,out] operation The operation object to set up. It must have
1241 * been initialized as per the documentation for
1242 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001243 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001244 * It must remain valid until the operation
1245 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001246 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001247 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001248 *
Gilles Peskine28538492018-07-11 17:34:00 +02001249 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001250 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001251 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001252 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001253 * \retval #PSA_ERROR_NOT_PERMITTED
1254 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001255 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001256 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001257 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001258 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1259 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1260 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001261 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001262 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001263 * The operation state is not valid (already set up and not
1264 * subsequently completed).
1265 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001266 * The library has not been previously initialized by psa_crypto_init().
1267 * It is implementation-dependent whether a failure to initialize
1268 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001269 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001270psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001271 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001272 psa_algorithm_t alg);
1273
Gilles Peskinef45adda2019-01-14 18:29:18 +01001274/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001275 *
1276 * This function sets up the verification of the MAC
1277 * (message authentication code) of a byte string against an expected value.
1278 *
1279 * The sequence of operations to verify a MAC is as follows:
1280 * -# Allocate an operation object which will be passed to all the functions
1281 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001282 * -# Initialize the operation object with one of the methods described in the
1283 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001284 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001285 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1286 * of the message each time. The MAC that is calculated is the MAC
1287 * of the concatenation of these messages in order.
1288 * -# At the end of the message, call psa_mac_verify_finish() to finish
1289 * calculating the actual MAC of the message and verify it against
1290 * the expected value.
1291 *
1292 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001293 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001294 *
1295 * After a successful call to psa_mac_verify_setup(), the application must
1296 * eventually terminate the operation through one of the following methods:
1297 * - A failed call to psa_mac_update().
1298 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1299 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001300 * \param[in,out] operation The operation object to set up. It must have
1301 * been initialized as per the documentation for
1302 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001303 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001304 * It must remain valid until the operation
1305 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001306 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1307 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001308 *
Gilles Peskine28538492018-07-11 17:34:00 +02001309 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001310 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001311 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001312 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001313 * \retval #PSA_ERROR_NOT_PERMITTED
1314 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001315 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001316 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001317 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001318 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1319 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1320 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001321 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001322 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001323 * The operation state is not valid (already set up and not
1324 * subsequently completed).
1325 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001326 * The library has not been previously initialized by psa_crypto_init().
1327 * It is implementation-dependent whether a failure to initialize
1328 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001329 */
1330psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001331 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001332 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001333
Gilles Peskinedcd14942018-07-12 00:30:52 +02001334/** Add a message fragment to a multipart MAC operation.
1335 *
1336 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1337 * before calling this function.
1338 *
1339 * If this function returns an error status, the operation becomes inactive.
1340 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001341 * \param[in,out] operation Active MAC operation.
1342 * \param[in] input Buffer containing the message fragment to add to
1343 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001344 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001345 *
1346 * \retval #PSA_SUCCESS
1347 * Success.
1348 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001349 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001350 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1351 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1352 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001353 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001354 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001355psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1356 const uint8_t *input,
1357 size_t input_length);
1358
Gilles Peskinedcd14942018-07-12 00:30:52 +02001359/** Finish the calculation of the MAC of a message.
1360 *
1361 * The application must call psa_mac_sign_setup() before calling this function.
1362 * This function calculates the MAC of the message formed by concatenating
1363 * the inputs passed to preceding calls to psa_mac_update().
1364 *
1365 * When this function returns, the operation becomes inactive.
1366 *
1367 * \warning Applications should not call this function if they expect
1368 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1369 * Beware that comparing integrity or authenticity data such as
1370 * MAC values with a function such as \c memcmp is risky
1371 * because the time taken by the comparison may leak information
1372 * about the MAC value which could allow an attacker to guess
1373 * a valid MAC and thereby bypass security controls.
1374 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001375 * \param[in,out] operation Active MAC operation.
1376 * \param[out] mac Buffer where the MAC value is to be written.
1377 * \param mac_size Size of the \p mac buffer in bytes.
1378 * \param[out] mac_length On success, the number of bytes
1379 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001380 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001381 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001382 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001383 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001384 *
1385 * \retval #PSA_SUCCESS
1386 * Success.
1387 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001388 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001389 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001390 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001391 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1392 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1393 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1394 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001395 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001396 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001397psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1398 uint8_t *mac,
1399 size_t mac_size,
1400 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001401
Gilles Peskinedcd14942018-07-12 00:30:52 +02001402/** Finish the calculation of the MAC of a message and compare it with
1403 * an expected value.
1404 *
1405 * The application must call psa_mac_verify_setup() before calling this function.
1406 * This function calculates the MAC of the message formed by concatenating
1407 * the inputs passed to preceding calls to psa_mac_update(). It then
1408 * compares the calculated MAC with the expected MAC passed as a
1409 * parameter to this function.
1410 *
1411 * When this function returns, the operation becomes inactive.
1412 *
1413 * \note Implementations shall make the best effort to ensure that the
1414 * comparison between the actual MAC and the expected MAC is performed
1415 * in constant time.
1416 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001417 * \param[in,out] operation Active MAC operation.
1418 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001419 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001420 *
1421 * \retval #PSA_SUCCESS
1422 * The expected MAC is identical to the actual MAC of the message.
1423 * \retval #PSA_ERROR_INVALID_SIGNATURE
1424 * The MAC of the message was calculated successfully, but it
1425 * differs from the expected MAC.
1426 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001427 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001428 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1429 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1430 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001431 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001432 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001433psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1434 const uint8_t *mac,
1435 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001436
Gilles Peskinedcd14942018-07-12 00:30:52 +02001437/** Abort a MAC operation.
1438 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001439 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001440 * \p operation structure itself. Once aborted, the operation object
1441 * can be reused for another operation by calling
1442 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001443 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001444 * You may call this function any time after the operation object has
1445 * been initialized by any of the following methods:
1446 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1447 * it succeeds or not.
1448 * - Initializing the \c struct to all-bits-zero.
1449 * - Initializing the \c struct to logical zeros, e.g.
1450 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001451 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001452 * In particular, calling psa_mac_abort() after the operation has been
1453 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1454 * psa_mac_verify_finish() is safe and has no effect.
1455 *
1456 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001457 *
1458 * \retval #PSA_SUCCESS
1459 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001460 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001461 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1462 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001463 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001464 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001465psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1466
1467/**@}*/
1468
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001469/** \defgroup cipher Symmetric ciphers
1470 * @{
1471 */
1472
Gilles Peskine69647a42019-01-14 20:18:12 +01001473/** Encrypt a message using a symmetric cipher.
1474 *
1475 * This function encrypts a message with a random IV (initialization
1476 * vector).
1477 *
1478 * \param handle Handle to the key to use for the operation.
1479 * It must remain valid until the operation
1480 * terminates.
1481 * \param alg The cipher algorithm to compute
1482 * (\c PSA_ALG_XXX value such that
1483 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1484 * \param[in] input Buffer containing the message to encrypt.
1485 * \param input_length Size of the \p input buffer in bytes.
1486 * \param[out] output Buffer where the output is to be written.
1487 * The output contains the IV followed by
1488 * the ciphertext proper.
1489 * \param output_size Size of the \p output buffer in bytes.
1490 * \param[out] output_length On success, the number of bytes
1491 * that make up the output.
1492 *
1493 * \retval #PSA_SUCCESS
1494 * Success.
1495 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001496 * \retval #PSA_ERROR_NOT_PERMITTED
1497 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001498 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001499 * \retval #PSA_ERROR_NOT_SUPPORTED
1500 * \p alg is not supported or is not a cipher algorithm.
1501 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1502 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1503 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1504 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001505 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001506 */
1507psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1508 psa_algorithm_t alg,
1509 const uint8_t *input,
1510 size_t input_length,
1511 uint8_t *output,
1512 size_t output_size,
1513 size_t *output_length);
1514
1515/** Decrypt a message using a symmetric cipher.
1516 *
1517 * This function decrypts a message encrypted with a symmetric cipher.
1518 *
1519 * \param handle Handle to the key to use for the operation.
1520 * It must remain valid until the operation
1521 * terminates.
1522 * \param alg The cipher algorithm to compute
1523 * (\c PSA_ALG_XXX value such that
1524 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1525 * \param[in] input Buffer containing the message to decrypt.
1526 * This consists of the IV followed by the
1527 * ciphertext proper.
1528 * \param input_length Size of the \p input buffer in bytes.
1529 * \param[out] output Buffer where the plaintext is to be written.
1530 * \param output_size Size of the \p output buffer in bytes.
1531 * \param[out] output_length On success, the number of bytes
1532 * that make up the output.
1533 *
1534 * \retval #PSA_SUCCESS
1535 * Success.
1536 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001537 * \retval #PSA_ERROR_NOT_PERMITTED
1538 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001539 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001540 * \retval #PSA_ERROR_NOT_SUPPORTED
1541 * \p alg is not supported or is not a cipher algorithm.
1542 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1543 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1544 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1545 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001546 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001547 */
1548psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1549 psa_algorithm_t alg,
1550 const uint8_t *input,
1551 size_t input_length,
1552 uint8_t *output,
1553 size_t output_size,
1554 size_t *output_length);
1555
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001556/** The type of the state data structure for multipart cipher operations.
1557 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001558 * Before calling any function on a cipher operation object, the application
1559 * must initialize it by any of the following means:
1560 * - Set the structure to all-bits-zero, for example:
1561 * \code
1562 * psa_cipher_operation_t operation;
1563 * memset(&operation, 0, sizeof(operation));
1564 * \endcode
1565 * - Initialize the structure to logical zero values, for example:
1566 * \code
1567 * psa_cipher_operation_t operation = {0};
1568 * \endcode
1569 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1570 * for example:
1571 * \code
1572 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1573 * \endcode
1574 * - Assign the result of the function psa_cipher_operation_init()
1575 * to the structure, for example:
1576 * \code
1577 * psa_cipher_operation_t operation;
1578 * operation = psa_cipher_operation_init();
1579 * \endcode
1580 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001581 * This is an implementation-defined \c struct. Applications should not
1582 * make any assumptions about the content of this structure except
1583 * as directed by the documentation of a specific implementation. */
1584typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1585
Jaeden Amero5bae2272019-01-04 11:48:27 +00001586/** \def PSA_CIPHER_OPERATION_INIT
1587 *
1588 * This macro returns a suitable initializer for a cipher operation object of
1589 * type #psa_cipher_operation_t.
1590 */
1591#ifdef __DOXYGEN_ONLY__
1592/* This is an example definition for documentation purposes.
1593 * Implementations should define a suitable value in `crypto_struct.h`.
1594 */
1595#define PSA_CIPHER_OPERATION_INIT {0}
1596#endif
1597
1598/** Return an initial value for a cipher operation object.
1599 */
1600static psa_cipher_operation_t psa_cipher_operation_init(void);
1601
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001602/** Set the key for a multipart symmetric encryption operation.
1603 *
1604 * The sequence of operations to encrypt a message with a symmetric cipher
1605 * is as follows:
1606 * -# Allocate an operation object which will be passed to all the functions
1607 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001608 * -# Initialize the operation object with one of the methods described in the
1609 * documentation for #psa_cipher_operation_t, e.g.
1610 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001611 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001612 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001613 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001614 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001615 * requires a specific IV value.
1616 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1617 * of the message each time.
1618 * -# Call psa_cipher_finish().
1619 *
1620 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001621 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001622 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001623 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001624 * eventually terminate the operation. The following events terminate an
1625 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001626 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001627 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001628 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001629 * \param[in,out] operation The operation object to set up. It must have
1630 * been initialized as per the documentation for
1631 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001632 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001633 * It must remain valid until the operation
1634 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001635 * \param alg The cipher algorithm to compute
1636 * (\c PSA_ALG_XXX value such that
1637 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001638 *
Gilles Peskine28538492018-07-11 17:34:00 +02001639 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001640 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001641 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001642 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001643 * \retval #PSA_ERROR_NOT_PERMITTED
1644 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001645 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001646 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001647 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001648 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1649 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1650 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001651 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001652 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001653 * The operation state is not valid (already set up and not
1654 * subsequently completed).
1655 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001656 * The library has not been previously initialized by psa_crypto_init().
1657 * It is implementation-dependent whether a failure to initialize
1658 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001659 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001660psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001661 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001662 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001663
1664/** Set the key for a multipart symmetric decryption operation.
1665 *
1666 * The sequence of operations to decrypt a message with a symmetric cipher
1667 * is as follows:
1668 * -# Allocate an operation object which will be passed to all the functions
1669 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001670 * -# Initialize the operation object with one of the methods described in the
1671 * documentation for #psa_cipher_operation_t, e.g.
1672 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001673 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001674 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001675 * decryption. If the IV is prepended to the ciphertext, you can call
1676 * psa_cipher_update() on a buffer containing the IV followed by the
1677 * beginning of the message.
1678 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1679 * of the message each time.
1680 * -# Call psa_cipher_finish().
1681 *
1682 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001683 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001684 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001685 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001686 * eventually terminate the operation. The following events terminate an
1687 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001688 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001689 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001690 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001691 * \param[in,out] operation The operation object to set up. It must have
1692 * been initialized as per the documentation for
1693 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001694 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001695 * It must remain valid until the operation
1696 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001697 * \param alg The cipher algorithm to compute
1698 * (\c PSA_ALG_XXX value such that
1699 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001700 *
Gilles Peskine28538492018-07-11 17:34:00 +02001701 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001702 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001703 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001704 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001705 * \retval #PSA_ERROR_NOT_PERMITTED
1706 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001707 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001708 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001709 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001710 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1711 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1712 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001713 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001714 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001715 * The operation state is not valid (already set up and not
1716 * subsequently completed).
1717 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001718 * The library has not been previously initialized by psa_crypto_init().
1719 * It is implementation-dependent whether a failure to initialize
1720 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001721 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001722psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001723 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001724 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001725
Gilles Peskinedcd14942018-07-12 00:30:52 +02001726/** Generate an IV for a symmetric encryption operation.
1727 *
1728 * This function generates a random IV (initialization vector), nonce
1729 * or initial counter value for the encryption operation as appropriate
1730 * for the chosen algorithm, key type and key size.
1731 *
1732 * The application must call psa_cipher_encrypt_setup() before
1733 * calling this function.
1734 *
1735 * If this function returns an error status, the operation becomes inactive.
1736 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001737 * \param[in,out] operation Active cipher operation.
1738 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001739 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001740 * \param[out] iv_length On success, the number of bytes of the
1741 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001742 *
1743 * \retval #PSA_SUCCESS
1744 * Success.
1745 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001746 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001747 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001748 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001749 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1750 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1751 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001752 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001753 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001754psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001755 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001756 size_t iv_size,
1757 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001758
Gilles Peskinedcd14942018-07-12 00:30:52 +02001759/** Set the IV for a symmetric encryption or decryption operation.
1760 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001761 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001762 * or initial counter value for the encryption or decryption operation.
1763 *
1764 * The application must call psa_cipher_encrypt_setup() before
1765 * calling this function.
1766 *
1767 * If this function returns an error status, the operation becomes inactive.
1768 *
1769 * \note When encrypting, applications should use psa_cipher_generate_iv()
1770 * instead of this function, unless implementing a protocol that requires
1771 * a non-random IV.
1772 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001773 * \param[in,out] operation Active cipher operation.
1774 * \param[in] iv Buffer containing the IV to use.
1775 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001776 *
1777 * \retval #PSA_SUCCESS
1778 * Success.
1779 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001780 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001781 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001782 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001783 * or the chosen algorithm does not use an IV.
1784 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1785 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1786 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001787 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001788 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001789psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001790 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001791 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001792
Gilles Peskinedcd14942018-07-12 00:30:52 +02001793/** Encrypt or decrypt a message fragment in an active cipher operation.
1794 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001795 * Before calling this function, you must:
1796 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1797 * The choice of setup function determines whether this function
1798 * encrypts or decrypts its input.
1799 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1800 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001801 *
1802 * If this function returns an error status, the operation becomes inactive.
1803 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001804 * \param[in,out] operation Active cipher operation.
1805 * \param[in] input Buffer containing the message fragment to
1806 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001807 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001808 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001809 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001810 * \param[out] output_length On success, the number of bytes
1811 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001812 *
1813 * \retval #PSA_SUCCESS
1814 * Success.
1815 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001816 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001817 * not set, or already completed).
1818 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1819 * The size of the \p output buffer is too small.
1820 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1821 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1822 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001823 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001824 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001825psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1826 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001827 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001828 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001829 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001830 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001831
Gilles Peskinedcd14942018-07-12 00:30:52 +02001832/** Finish encrypting or decrypting a message in a cipher operation.
1833 *
1834 * The application must call psa_cipher_encrypt_setup() or
1835 * psa_cipher_decrypt_setup() before calling this function. The choice
1836 * of setup function determines whether this function encrypts or
1837 * decrypts its input.
1838 *
1839 * This function finishes the encryption or decryption of the message
1840 * formed by concatenating the inputs passed to preceding calls to
1841 * psa_cipher_update().
1842 *
1843 * When this function returns, the operation becomes inactive.
1844 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001845 * \param[in,out] operation Active cipher operation.
1846 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001847 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001848 * \param[out] output_length On success, the number of bytes
1849 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001850 *
1851 * \retval #PSA_SUCCESS
1852 * Success.
1853 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001854 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001855 * not set, or already completed).
1856 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1857 * The size of the \p output buffer is too small.
1858 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1859 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1860 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001861 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001862 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001863psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001864 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001865 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001866 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001867
Gilles Peskinedcd14942018-07-12 00:30:52 +02001868/** Abort a cipher operation.
1869 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001870 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001871 * \p operation structure itself. Once aborted, the operation object
1872 * can be reused for another operation by calling
1873 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001874 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001875 * You may call this function any time after the operation object has
1876 * been initialized by any of the following methods:
1877 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1878 * whether it succeeds or not.
1879 * - Initializing the \c struct to all-bits-zero.
1880 * - Initializing the \c struct to logical zeros, e.g.
1881 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001882 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001883 * In particular, calling psa_cipher_abort() after the operation has been
1884 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1885 * is safe and has no effect.
1886 *
1887 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001888 *
1889 * \retval #PSA_SUCCESS
1890 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001891 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001892 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1893 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001894 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001895 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001896psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1897
1898/**@}*/
1899
Gilles Peskine3b555712018-03-03 21:27:57 +01001900/** \defgroup aead Authenticated encryption with associated data (AEAD)
1901 * @{
1902 */
1903
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001904/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001905 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001906 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001907 * \param alg The AEAD algorithm to compute
1908 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001909 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001910 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001911 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001912 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001913 * but not encrypted.
1914 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001915 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001916 * encrypted.
1917 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001918 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001919 * encrypted data. The additional data is not
1920 * part of this output. For algorithms where the
1921 * encrypted data and the authentication tag
1922 * are defined as separate outputs, the
1923 * authentication tag is appended to the
1924 * encrypted data.
1925 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1926 * This must be at least
1927 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1928 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001929 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01001930 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001931 *
Gilles Peskine28538492018-07-11 17:34:00 +02001932 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001933 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001934 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001935 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001936 * \retval #PSA_ERROR_NOT_PERMITTED
1937 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001938 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001939 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001940 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001941 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1942 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1943 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001944 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001945 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001946 * The library has not been previously initialized by psa_crypto_init().
1947 * It is implementation-dependent whether a failure to initialize
1948 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001949 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001950psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001951 psa_algorithm_t alg,
1952 const uint8_t *nonce,
1953 size_t nonce_length,
1954 const uint8_t *additional_data,
1955 size_t additional_data_length,
1956 const uint8_t *plaintext,
1957 size_t plaintext_length,
1958 uint8_t *ciphertext,
1959 size_t ciphertext_size,
1960 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001961
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001962/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001963 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001964 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001965 * \param alg The AEAD algorithm to compute
1966 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001967 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001968 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001969 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001970 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001971 * but not encrypted.
1972 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001973 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001974 * encrypted. For algorithms where the
1975 * encrypted data and the authentication tag
1976 * are defined as separate inputs, the buffer
1977 * must contain the encrypted data followed
1978 * by the authentication tag.
1979 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001980 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001981 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1982 * This must be at least
1983 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1984 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001985 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01001986 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001987 *
Gilles Peskine28538492018-07-11 17:34:00 +02001988 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001989 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001990 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001991 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001992 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001993 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001994 * \retval #PSA_ERROR_NOT_PERMITTED
1995 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001996 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001997 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001998 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001999 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2000 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2001 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002002 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002003 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002004 * The library has not been previously initialized by psa_crypto_init().
2005 * It is implementation-dependent whether a failure to initialize
2006 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002007 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002008psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002009 psa_algorithm_t alg,
2010 const uint8_t *nonce,
2011 size_t nonce_length,
2012 const uint8_t *additional_data,
2013 size_t additional_data_length,
2014 const uint8_t *ciphertext,
2015 size_t ciphertext_length,
2016 uint8_t *plaintext,
2017 size_t plaintext_size,
2018 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002019
Gilles Peskine30a9e412019-01-14 18:36:12 +01002020/** The type of the state data structure for multipart AEAD operations.
2021 *
2022 * Before calling any function on an AEAD operation object, the application
2023 * must initialize it by any of the following means:
2024 * - Set the structure to all-bits-zero, for example:
2025 * \code
2026 * psa_aead_operation_t operation;
2027 * memset(&operation, 0, sizeof(operation));
2028 * \endcode
2029 * - Initialize the structure to logical zero values, for example:
2030 * \code
2031 * psa_aead_operation_t operation = {0};
2032 * \endcode
2033 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2034 * for example:
2035 * \code
2036 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2037 * \endcode
2038 * - Assign the result of the function psa_aead_operation_init()
2039 * to the structure, for example:
2040 * \code
2041 * psa_aead_operation_t operation;
2042 * operation = psa_aead_operation_init();
2043 * \endcode
2044 *
2045 * This is an implementation-defined \c struct. Applications should not
2046 * make any assumptions about the content of this structure except
2047 * as directed by the documentation of a specific implementation. */
2048typedef struct psa_aead_operation_s psa_aead_operation_t;
2049
2050/** \def PSA_AEAD_OPERATION_INIT
2051 *
2052 * This macro returns a suitable initializer for an AEAD operation object of
2053 * type #psa_aead_operation_t.
2054 */
2055#ifdef __DOXYGEN_ONLY__
2056/* This is an example definition for documentation purposes.
2057 * Implementations should define a suitable value in `crypto_struct.h`.
2058 */
2059#define PSA_AEAD_OPERATION_INIT {0}
2060#endif
2061
2062/** Return an initial value for an AEAD operation object.
2063 */
2064static psa_aead_operation_t psa_aead_operation_init(void);
2065
2066/** Set the key for a multipart authenticated encryption operation.
2067 *
2068 * The sequence of operations to encrypt a message with authentication
2069 * is as follows:
2070 * -# Allocate an operation object which will be passed to all the functions
2071 * listed here.
2072 * -# Initialize the operation object with one of the methods described in the
2073 * documentation for #psa_aead_operation_t, e.g.
2074 * PSA_AEAD_OPERATION_INIT.
2075 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002076 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2077 * inputs to the subsequent calls to psa_aead_update_ad() and
2078 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2079 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002080 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2081 * generate or set the nonce. You should use
2082 * psa_aead_generate_nonce() unless the protocol you are implementing
2083 * requires a specific nonce value.
2084 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2085 * of the non-encrypted additional authenticated data each time.
2086 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002087 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002088 * -# Call psa_aead_finish().
2089 *
2090 * The application may call psa_aead_abort() at any time after the operation
2091 * has been initialized.
2092 *
2093 * After a successful call to psa_aead_encrypt_setup(), the application must
2094 * eventually terminate the operation. The following events terminate an
2095 * operation:
2096 * - A failed call to any of the \c psa_aead_xxx functions.
2097 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2098 *
2099 * \param[in,out] operation The operation object to set up. It must have
2100 * been initialized as per the documentation for
2101 * #psa_aead_operation_t and not yet in use.
2102 * \param handle Handle to the key to use for the operation.
2103 * It must remain valid until the operation
2104 * terminates.
2105 * \param alg The AEAD algorithm to compute
2106 * (\c PSA_ALG_XXX value such that
2107 * #PSA_ALG_IS_AEAD(\p alg) is true).
2108 *
2109 * \retval #PSA_SUCCESS
2110 * Success.
2111 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002112 * \retval #PSA_ERROR_NOT_PERMITTED
2113 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002114 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002115 * \retval #PSA_ERROR_NOT_SUPPORTED
2116 * \p alg is not supported or is not an AEAD algorithm.
2117 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2118 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2119 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002120 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002121 * \retval #PSA_ERROR_BAD_STATE
2122 * The library has not been previously initialized by psa_crypto_init().
2123 * It is implementation-dependent whether a failure to initialize
2124 * results in this error code.
2125 */
2126psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2127 psa_key_handle_t handle,
2128 psa_algorithm_t alg);
2129
2130/** Set the key for a multipart authenticated decryption operation.
2131 *
2132 * The sequence of operations to decrypt a message with authentication
2133 * is as follows:
2134 * -# Allocate an operation object which will be passed to all the functions
2135 * listed here.
2136 * -# Initialize the operation object with one of the methods described in the
2137 * documentation for #psa_aead_operation_t, e.g.
2138 * PSA_AEAD_OPERATION_INIT.
2139 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002140 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2141 * inputs to the subsequent calls to psa_aead_update_ad() and
2142 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2143 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002144 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2145 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2146 * of the non-encrypted additional authenticated data each time.
2147 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002148 * of the ciphertext to decrypt each time.
2149 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002150 *
2151 * The application may call psa_aead_abort() at any time after the operation
2152 * has been initialized.
2153 *
2154 * After a successful call to psa_aead_decrypt_setup(), the application must
2155 * eventually terminate the operation. The following events terminate an
2156 * operation:
2157 * - A failed call to any of the \c psa_aead_xxx functions.
2158 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2159 *
2160 * \param[in,out] operation The operation object to set up. It must have
2161 * been initialized as per the documentation for
2162 * #psa_aead_operation_t and not yet in use.
2163 * \param handle Handle to the key to use for the operation.
2164 * It must remain valid until the operation
2165 * terminates.
2166 * \param alg The AEAD algorithm to compute
2167 * (\c PSA_ALG_XXX value such that
2168 * #PSA_ALG_IS_AEAD(\p alg) is true).
2169 *
2170 * \retval #PSA_SUCCESS
2171 * Success.
2172 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002173 * \retval #PSA_ERROR_NOT_PERMITTED
2174 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002175 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002176 * \retval #PSA_ERROR_NOT_SUPPORTED
2177 * \p alg is not supported or is not an AEAD algorithm.
2178 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2179 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2180 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002181 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002182 * \retval #PSA_ERROR_BAD_STATE
2183 * The library has not been previously initialized by psa_crypto_init().
2184 * It is implementation-dependent whether a failure to initialize
2185 * results in this error code.
2186 */
2187psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2188 psa_key_handle_t handle,
2189 psa_algorithm_t alg);
2190
2191/** Generate a random nonce for an authenticated encryption operation.
2192 *
2193 * This function generates a random nonce for the authenticated encryption
2194 * operation with an appropriate size for the chosen algorithm, key type
2195 * and key size.
2196 *
2197 * The application must call psa_aead_encrypt_setup() before
2198 * calling this function.
2199 *
2200 * If this function returns an error status, the operation becomes inactive.
2201 *
2202 * \param[in,out] operation Active AEAD operation.
2203 * \param[out] nonce Buffer where the generated nonce is to be
2204 * written.
2205 * \param nonce_size Size of the \p nonce buffer in bytes.
2206 * \param[out] nonce_length On success, the number of bytes of the
2207 * generated nonce.
2208 *
2209 * \retval #PSA_SUCCESS
2210 * Success.
2211 * \retval #PSA_ERROR_BAD_STATE
2212 * The operation state is not valid (not set up, or nonce already set).
2213 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2214 * The size of the \p nonce buffer is too small.
2215 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2216 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2217 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002218 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002219 */
2220psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002221 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002222 size_t nonce_size,
2223 size_t *nonce_length);
2224
2225/** Set the nonce for an authenticated encryption or decryption operation.
2226 *
2227 * This function sets the nonce for the authenticated
2228 * encryption or decryption operation.
2229 *
2230 * The application must call psa_aead_encrypt_setup() before
2231 * calling this function.
2232 *
2233 * If this function returns an error status, the operation becomes inactive.
2234 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002235 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002236 * instead of this function, unless implementing a protocol that requires
2237 * a non-random IV.
2238 *
2239 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002240 * \param[in] nonce Buffer containing the nonce to use.
2241 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002242 *
2243 * \retval #PSA_SUCCESS
2244 * Success.
2245 * \retval #PSA_ERROR_BAD_STATE
2246 * The operation state is not valid (not set up, or nonce already set).
2247 * \retval #PSA_ERROR_INVALID_ARGUMENT
2248 * The size of \p nonce is not acceptable for the chosen algorithm.
2249 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2250 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2251 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002252 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002253 */
2254psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002255 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002256 size_t nonce_length);
2257
Gilles Peskinebc59c852019-01-17 15:26:08 +01002258/** Declare the lengths of the message and additional data for AEAD.
2259 *
2260 * The application must call this function before calling
2261 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2262 * the operation requires it. If the algorithm does not require it,
2263 * calling this function is optional, but if this function is called
2264 * then the implementation must enforce the lengths.
2265 *
2266 * You may call this function before or after setting the nonce with
2267 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2268 *
2269 * - For #PSA_ALG_CCM, calling this function is required.
2270 * - For the other AEAD algorithms defined in this specification, calling
2271 * this function is not required.
2272 * - For vendor-defined algorithm, refer to the vendor documentation.
2273 *
2274 * \param[in,out] operation Active AEAD operation.
2275 * \param ad_length Size of the non-encrypted additional
2276 * authenticated data in bytes.
2277 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2278 *
2279 * \retval #PSA_SUCCESS
2280 * Success.
2281 * \retval #PSA_ERROR_BAD_STATE
2282 * The operation state is not valid (not set up, already completed,
2283 * or psa_aead_update_ad() or psa_aead_update() already called).
2284 * \retval #PSA_ERROR_INVALID_ARGUMENT
2285 * At least one of the lengths is not acceptable for the chosen
2286 * algorithm.
2287 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2288 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2289 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002290 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinebc59c852019-01-17 15:26:08 +01002291 */
2292psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2293 size_t ad_length,
2294 size_t plaintext_length);
2295
Gilles Peskine30a9e412019-01-14 18:36:12 +01002296/** Pass additional data to an active AEAD operation.
2297 *
2298 * Additional data is authenticated, but not encrypted.
2299 *
2300 * You may call this function multiple times to pass successive fragments
2301 * of the additional data. You may not call this function after passing
2302 * data to encrypt or decrypt with psa_aead_update().
2303 *
2304 * Before calling this function, you must:
2305 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2306 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2307 *
2308 * If this function returns an error status, the operation becomes inactive.
2309 *
2310 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2311 * there is no guarantee that the input is valid. Therefore, until
2312 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2313 * treat the input as untrusted and prepare to undo any action that
2314 * depends on the input if psa_aead_verify() returns an error status.
2315 *
2316 * \param[in,out] operation Active AEAD operation.
2317 * \param[in] input Buffer containing the fragment of
2318 * additional data.
2319 * \param input_length Size of the \p input buffer in bytes.
2320 *
2321 * \retval #PSA_SUCCESS
2322 * Success.
2323 * \retval #PSA_ERROR_BAD_STATE
2324 * The operation state is not valid (not set up, nonce not set,
2325 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002326 * \retval #PSA_ERROR_INVALID_ARGUMENT
2327 * The total input length overflows the additional data length that
2328 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002329 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2330 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2331 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002332 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002333 */
2334psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2335 const uint8_t *input,
2336 size_t input_length);
2337
2338/** Encrypt or decrypt a message fragment in an active AEAD operation.
2339 *
2340 * Before calling this function, you must:
2341 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2342 * The choice of setup function determines whether this function
2343 * encrypts or decrypts its input.
2344 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2345 * 3. Call psa_aead_update_ad() to pass all the additional data.
2346 *
2347 * If this function returns an error status, the operation becomes inactive.
2348 *
2349 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2350 * there is no guarantee that the input is valid. Therefore, until
2351 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2352 * - Do not use the output in any way other than storing it in a
2353 * confidential location. If you take any action that depends
2354 * on the tentative decrypted data, this action will need to be
2355 * undone if the input turns out not to be valid. Furthermore,
2356 * if an adversary can observe that this action took place
2357 * (for example through timing), they may be able to use this
2358 * fact as an oracle to decrypt any message encrypted with the
2359 * same key.
2360 * - In particular, do not copy the output anywhere but to a
2361 * memory or storage space that you have exclusive access to.
2362 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002363 * This function does not require the input to be aligned to any
2364 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002365 * a whole block at a time, it must consume all the input provided, but
2366 * it may delay the end of the corresponding output until a subsequent
2367 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2368 * provides sufficient input. The amount of data that can be delayed
2369 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002370 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002371 * \param[in,out] operation Active AEAD operation.
2372 * \param[in] input Buffer containing the message fragment to
2373 * encrypt or decrypt.
2374 * \param input_length Size of the \p input buffer in bytes.
2375 * \param[out] output Buffer where the output is to be written.
2376 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002377 * This must be at least
2378 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2379 * \p input_length) where \c alg is the
2380 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002381 * \param[out] output_length On success, the number of bytes
2382 * that make up the returned output.
2383 *
2384 * \retval #PSA_SUCCESS
2385 * Success.
2386 * \retval #PSA_ERROR_BAD_STATE
2387 * The operation state is not valid (not set up, nonce not set
2388 * or already completed).
2389 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2390 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002391 * You can determine a sufficient buffer size by calling
2392 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2393 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002394 * \retval #PSA_ERROR_INVALID_ARGUMENT
2395 * The total length of input to psa_aead_update_ad() so far is
2396 * less than the additional data length that was previously
2397 * specified with psa_aead_set_lengths().
2398 * \retval #PSA_ERROR_INVALID_ARGUMENT
2399 * The total input length overflows the plaintext length that
2400 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002401 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2402 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2403 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002404 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002405 */
2406psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2407 const uint8_t *input,
2408 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002409 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002410 size_t output_size,
2411 size_t *output_length);
2412
2413/** Finish encrypting a message in an AEAD operation.
2414 *
2415 * The operation must have been set up with psa_aead_encrypt_setup().
2416 *
2417 * This function finishes the authentication of the additional data
2418 * formed by concatenating the inputs passed to preceding calls to
2419 * psa_aead_update_ad() with the plaintext formed by concatenating the
2420 * inputs passed to preceding calls to psa_aead_update().
2421 *
2422 * This function has two output buffers:
2423 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002424 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002425 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002426 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002427 * that the operation performs.
2428 *
2429 * When this function returns, the operation becomes inactive.
2430 *
2431 * \param[in,out] operation Active AEAD operation.
2432 * \param[out] ciphertext Buffer where the last part of the ciphertext
2433 * is to be written.
2434 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002435 * This must be at least
2436 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2437 * \c alg is the algorithm that is being
2438 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002439 * \param[out] ciphertext_length On success, the number of bytes of
2440 * returned ciphertext.
2441 * \param[out] tag Buffer where the authentication tag is
2442 * to be written.
2443 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002444 * This must be at least
2445 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2446 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002447 * \param[out] tag_length On success, the number of bytes
2448 * that make up the returned tag.
2449 *
2450 * \retval #PSA_SUCCESS
2451 * Success.
2452 * \retval #PSA_ERROR_BAD_STATE
2453 * The operation state is not valid (not set up, nonce not set,
2454 * decryption, or already completed).
2455 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002456 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002457 * You can determine a sufficient buffer size for \p ciphertext by
2458 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2459 * where \c alg is the algorithm that is being calculated.
2460 * You can determine a sufficient buffer size for \p tag by
2461 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002462 * \retval #PSA_ERROR_INVALID_ARGUMENT
2463 * The total length of input to psa_aead_update_ad() so far is
2464 * less than the additional data length that was previously
2465 * specified with psa_aead_set_lengths().
2466 * \retval #PSA_ERROR_INVALID_ARGUMENT
2467 * The total length of input to psa_aead_update() so far is
2468 * less than the plaintext length that was previously
2469 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002470 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2471 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2472 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002473 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002474 */
2475psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002476 uint8_t *ciphertext,
2477 size_t ciphertext_size,
2478 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002479 uint8_t *tag,
2480 size_t tag_size,
2481 size_t *tag_length);
2482
2483/** Finish authenticating and decrypting a message in an AEAD operation.
2484 *
2485 * The operation must have been set up with psa_aead_decrypt_setup().
2486 *
2487 * This function finishes the authentication of the additional data
2488 * formed by concatenating the inputs passed to preceding calls to
2489 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2490 * inputs passed to preceding calls to psa_aead_update().
2491 *
2492 * When this function returns, the operation becomes inactive.
2493 *
2494 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002495 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002496 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002497 * from previous calls to psa_aead_update()
2498 * that could not be processed until the end
2499 * of the input.
2500 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002501 * This must be at least
2502 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2503 * \c alg is the algorithm that is being
2504 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002505 * \param[out] plaintext_length On success, the number of bytes of
2506 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002507 * \param[in] tag Buffer containing the authentication tag.
2508 * \param tag_length Size of the \p tag buffer in bytes.
2509 *
2510 * \retval #PSA_SUCCESS
2511 * Success.
2512 * \retval #PSA_ERROR_BAD_STATE
2513 * The operation state is not valid (not set up, nonce not set,
2514 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002515 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2516 * The size of the \p plaintext buffer is too small.
2517 * You can determine a sufficient buffer size for \p plaintext by
2518 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2519 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002520 * \retval #PSA_ERROR_INVALID_ARGUMENT
2521 * The total length of input to psa_aead_update_ad() so far is
2522 * less than the additional data length that was previously
2523 * specified with psa_aead_set_lengths().
2524 * \retval #PSA_ERROR_INVALID_ARGUMENT
2525 * The total length of input to psa_aead_update() so far is
2526 * less than the plaintext length that was previously
2527 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002528 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2529 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2530 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002531 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002532 */
2533psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002534 uint8_t *plaintext,
2535 size_t plaintext_size,
2536 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002537 const uint8_t *tag,
2538 size_t tag_length);
2539
2540/** Abort an AEAD operation.
2541 *
2542 * Aborting an operation frees all associated resources except for the
2543 * \p operation structure itself. Once aborted, the operation object
2544 * can be reused for another operation by calling
2545 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2546 *
2547 * You may call this function any time after the operation object has
2548 * been initialized by any of the following methods:
2549 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2550 * whether it succeeds or not.
2551 * - Initializing the \c struct to all-bits-zero.
2552 * - Initializing the \c struct to logical zeros, e.g.
2553 * `psa_aead_operation_t operation = {0}`.
2554 *
2555 * In particular, calling psa_aead_abort() after the operation has been
2556 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2557 * is safe and has no effect.
2558 *
2559 * \param[in,out] operation Initialized AEAD operation.
2560 *
2561 * \retval #PSA_SUCCESS
2562 * \retval #PSA_ERROR_BAD_STATE
2563 * \p operation is not an active AEAD operation.
2564 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2565 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002566 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002567 */
2568psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2569
Gilles Peskine3b555712018-03-03 21:27:57 +01002570/**@}*/
2571
Gilles Peskine20035e32018-02-03 22:44:14 +01002572/** \defgroup asymmetric Asymmetric cryptography
2573 * @{
2574 */
2575
2576/**
2577 * \brief Sign a hash or short message with a private key.
2578 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002579 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002580 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002581 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2582 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2583 * to determine the hash algorithm to use.
2584 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002585 * \param handle Handle to the key to use for the operation.
2586 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002587 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002588 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002589 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002590 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002591 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002592 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002593 * \param[out] signature_length On success, the number of bytes
2594 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002595 *
Gilles Peskine28538492018-07-11 17:34:00 +02002596 * \retval #PSA_SUCCESS
2597 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002598 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002599 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002600 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002601 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002602 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002603 * \retval #PSA_ERROR_NOT_SUPPORTED
2604 * \retval #PSA_ERROR_INVALID_ARGUMENT
2605 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2606 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2607 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002608 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002609 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002610 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002611 * The library has not been previously initialized by psa_crypto_init().
2612 * It is implementation-dependent whether a failure to initialize
2613 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002614 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002615psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002616 psa_algorithm_t alg,
2617 const uint8_t *hash,
2618 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002619 uint8_t *signature,
2620 size_t signature_size,
2621 size_t *signature_length);
2622
2623/**
2624 * \brief Verify the signature a hash or short message using a public key.
2625 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002626 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002627 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002628 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2629 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2630 * to determine the hash algorithm to use.
2631 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002632 * \param handle Handle to the key to use for the operation.
2633 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002634 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002635 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002636 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002637 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002638 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002639 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002640 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002641 *
Gilles Peskine28538492018-07-11 17:34:00 +02002642 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002643 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002644 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002645 * The calculation was perfomed successfully, but the passed
2646 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002647 * \retval #PSA_ERROR_NOT_SUPPORTED
2648 * \retval #PSA_ERROR_INVALID_ARGUMENT
2649 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2650 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2651 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002652 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002653 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002654 * The library has not been previously initialized by psa_crypto_init().
2655 * It is implementation-dependent whether a failure to initialize
2656 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002657 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002658psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002659 psa_algorithm_t alg,
2660 const uint8_t *hash,
2661 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002662 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002663 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002664
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002665/**
2666 * \brief Encrypt a short message with a public key.
2667 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002668 * \param handle Handle to the key to use for the operation.
2669 * It must be a public key or an asymmetric
2670 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002671 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002672 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002673 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002674 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002675 * \param[in] salt A salt or label, if supported by the
2676 * encryption algorithm.
2677 * If the algorithm does not support a
2678 * salt, pass \c NULL.
2679 * If the algorithm supports an optional
2680 * salt and you do not want to pass a salt,
2681 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002682 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002683 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2684 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002685 * \param salt_length Size of the \p salt buffer in bytes.
2686 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002687 * \param[out] output Buffer where the encrypted message is to
2688 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002689 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002690 * \param[out] output_length On success, the number of bytes
2691 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002692 *
Gilles Peskine28538492018-07-11 17:34:00 +02002693 * \retval #PSA_SUCCESS
2694 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002695 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002696 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002697 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002698 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002699 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002700 * \retval #PSA_ERROR_NOT_SUPPORTED
2701 * \retval #PSA_ERROR_INVALID_ARGUMENT
2702 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2703 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2704 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002705 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002706 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002707 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002708 * The library has not been previously initialized by psa_crypto_init().
2709 * It is implementation-dependent whether a failure to initialize
2710 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002711 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002712psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002713 psa_algorithm_t alg,
2714 const uint8_t *input,
2715 size_t input_length,
2716 const uint8_t *salt,
2717 size_t salt_length,
2718 uint8_t *output,
2719 size_t output_size,
2720 size_t *output_length);
2721
2722/**
2723 * \brief Decrypt a short message with a private key.
2724 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002725 * \param handle Handle to the key to use for the operation.
2726 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002727 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002728 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002729 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002730 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002731 * \param[in] salt A salt or label, if supported by the
2732 * encryption algorithm.
2733 * If the algorithm does not support a
2734 * salt, pass \c NULL.
2735 * If the algorithm supports an optional
2736 * salt and you do not want to pass a salt,
2737 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002738 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002739 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2740 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002741 * \param salt_length Size of the \p salt buffer in bytes.
2742 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002743 * \param[out] output Buffer where the decrypted message is to
2744 * be written.
2745 * \param output_size Size of the \c output buffer in bytes.
2746 * \param[out] output_length On success, the number of bytes
2747 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002748 *
Gilles Peskine28538492018-07-11 17:34:00 +02002749 * \retval #PSA_SUCCESS
2750 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002751 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002752 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002753 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002754 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002755 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002756 * \retval #PSA_ERROR_NOT_SUPPORTED
2757 * \retval #PSA_ERROR_INVALID_ARGUMENT
2758 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2759 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2760 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002761 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002762 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2763 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002764 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002765 * The library has not been previously initialized by psa_crypto_init().
2766 * It is implementation-dependent whether a failure to initialize
2767 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002768 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002769psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002770 psa_algorithm_t alg,
2771 const uint8_t *input,
2772 size_t input_length,
2773 const uint8_t *salt,
2774 size_t salt_length,
2775 uint8_t *output,
2776 size_t output_size,
2777 size_t *output_length);
2778
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002779/**@}*/
2780
Gilles Peskine35675b62019-05-16 17:26:11 +02002781/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002782 * @{
2783 */
2784
Gilles Peskine35675b62019-05-16 17:26:11 +02002785/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002786 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002787 * Before calling any function on a key derivation operation object, the
2788 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002789 * - Set the structure to all-bits-zero, for example:
2790 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002791 * psa_key_derivation_operation_t operation;
2792 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002793 * \endcode
2794 * - Initialize the structure to logical zero values, for example:
2795 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002796 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002797 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002798 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002799 * for example:
2800 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002801 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002802 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002803 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002804 * to the structure, for example:
2805 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002806 * psa_key_derivation_operation_t operation;
2807 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002808 * \endcode
2809 *
2810 * This is an implementation-defined \c struct. Applications should not
2811 * make any assumptions about the content of this structure except
2812 * as directed by the documentation of a specific implementation.
2813 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02002814typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002815
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002816/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002817 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002818 * This macro returns a suitable initializer for a key derivation operation
2819 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002820 */
2821#ifdef __DOXYGEN_ONLY__
2822/* This is an example definition for documentation purposes.
2823 * Implementations should define a suitable value in `crypto_struct.h`.
2824 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002825#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02002826#endif
2827
Gilles Peskine35675b62019-05-16 17:26:11 +02002828/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002829 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002830static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002831
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002832/** Set up a key derivation operation.
2833 *
2834 * A key derivation algorithm takes some inputs and uses them to generate
2835 * a byte stream in a deterministic way.
2836 * This byte stream can be used to produce keys and other
2837 * cryptographic material.
2838 *
2839 * To derive a key:
2840 * - Start with an initialized object of type #psa_key_derivation_operation_t.
2841 * - Call psa_key_derivation_setup() to select the algorithm.
2842 * - Provide the inputs for the key derivation by calling
2843 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2844 * as appropriate. Which inputs are needed, in what order, and whether
2845 * they may be keys and if so of what type depends on the algorithm.
2846 * - Optionally set the operation's maximum capacity with
2847 * psa_key_derivation_set_capacity(). You may do this before, in the middle
2848 * of or after providing inputs. For some algorithms, this step is mandatory
2849 * because the output depends on the maximum capacity.
2850 * - To derive a key, call psa_key_derivation_output_key().
2851 * To derive a byte string for a different purpose, call
2852 * - psa_key_derivation_output_bytes().
2853 * Successive calls to these functions use successive output bytes
2854 * calculated by the key derivation algorithm.
2855 * - Clean up the key derivation operation object with
2856 * psa_key_derivation_abort().
2857 *
2858 * \param[in,out] operation The key derivation operation object
2859 * to set up. It must
2860 * have been initialized but not set up yet.
2861 * \param alg The key derivation algorithm to compute
2862 * (\c PSA_ALG_XXX value such that
2863 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2864 *
2865 * \retval #PSA_SUCCESS
2866 * Success.
2867 * \retval #PSA_ERROR_INVALID_ARGUMENT
2868 * \c alg is not a key derivation algorithm.
2869 * \retval #PSA_ERROR_NOT_SUPPORTED
2870 * \c alg is not supported or is not a key derivation algorithm.
2871 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2872 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2873 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002874 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002875 * \retval #PSA_ERROR_BAD_STATE
2876 */
2877psa_status_t psa_key_derivation_setup(
2878 psa_key_derivation_operation_t *operation,
2879 psa_algorithm_t alg);
2880
Gilles Peskine35675b62019-05-16 17:26:11 +02002881/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002882 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002883 * The capacity of a key derivation is the maximum number of bytes that it can
2884 * return. When you get *N* bytes of output from a key derivation operation,
2885 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002886 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002887 * \param[in] operation The operation to query.
2888 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002889 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002890 * \retval #PSA_SUCCESS
2891 * \retval #PSA_ERROR_BAD_STATE
2892 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002893 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002894psa_status_t psa_key_derivation_get_capacity(
2895 const psa_key_derivation_operation_t *operation,
2896 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002897
Gilles Peskine35675b62019-05-16 17:26:11 +02002898/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002899 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002900 * The capacity of a key derivation operation is the maximum number of bytes
2901 * that the key derivation operation can return from this point onwards.
2902 *
2903 * \param[in,out] operation The key derivation operation object to modify.
2904 * \param capacity The new capacity of the operation.
2905 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002906 * current capacity.
2907 *
2908 * \retval #PSA_SUCCESS
2909 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02002910 * \p capacity is larger than the operation's current capacity.
2911 * In this case, the operation object remains valid and its capacity
2912 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002913 * \retval #PSA_ERROR_BAD_STATE
2914 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2915 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002916psa_status_t psa_key_derivation_set_capacity(
2917 psa_key_derivation_operation_t *operation,
2918 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002919
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002920/** Use the maximum possible capacity for a key derivation operation.
2921 *
2922 * Use this value as the capacity argument when setting up a key derivation
2923 * to indicate that the operation should have the maximum possible capacity.
2924 * The value of the maximum possible capacity depends on the key derivation
2925 * algorithm.
2926 */
2927#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
2928
2929/** Provide an input for key derivation or key agreement.
2930 *
2931 * Which inputs are required and in what order depends on the algorithm.
2932 * Refer to the documentation of each key derivation or key agreement
2933 * algorithm for information.
2934 *
2935 * This function passes direct inputs. Some inputs must be passed as keys
2936 * using psa_key_derivation_input_key() instead of this function. Refer to
2937 * the documentation of individual step types for information.
2938 *
2939 * \param[in,out] operation The key derivation operation object to use.
2940 * It must have been set up with
2941 * psa_key_derivation_setup() and must not
2942 * have produced any output yet.
2943 * \param step Which step the input data is for.
2944 * \param[in] data Input data to use.
2945 * \param data_length Size of the \p data buffer in bytes.
2946 *
2947 * \retval #PSA_SUCCESS
2948 * Success.
2949 * \retval #PSA_ERROR_INVALID_ARGUMENT
2950 * \c step is not compatible with the operation's algorithm.
2951 * \retval #PSA_ERROR_INVALID_ARGUMENT
2952 * \c step does not allow direct inputs.
2953 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2954 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2955 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002956 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002957 * \retval #PSA_ERROR_BAD_STATE
2958 * The value of \p step is not valid given the state of \p operation.
2959 * \retval #PSA_ERROR_BAD_STATE
2960 * The library has not been previously initialized by psa_crypto_init().
2961 * It is implementation-dependent whether a failure to initialize
2962 * results in this error code.
2963 */
2964psa_status_t psa_key_derivation_input_bytes(
2965 psa_key_derivation_operation_t *operation,
2966 psa_key_derivation_step_t step,
2967 const uint8_t *data,
2968 size_t data_length);
2969
2970/** Provide an input for key derivation in the form of a key.
2971 *
2972 * Which inputs are required and in what order depends on the algorithm.
2973 * Refer to the documentation of each key derivation or key agreement
2974 * algorithm for information.
2975 *
2976 * This function passes key inputs. Some inputs must be passed as keys
2977 * of the appropriate type using this function, while others must be
2978 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
2979 * the documentation of individual step types for information.
2980 *
2981 * \param[in,out] operation The key derivation operation object to use.
2982 * It must have been set up with
2983 * psa_key_derivation_setup() and must not
2984 * have produced any output yet.
2985 * \param step Which step the input data is for.
2986 * \param handle Handle to the key. It must have an
2987 * appropriate type for \p step and must
2988 * allow the usage #PSA_KEY_USAGE_DERIVE.
2989 *
2990 * \retval #PSA_SUCCESS
2991 * Success.
2992 * \retval #PSA_ERROR_INVALID_HANDLE
2993 * \retval #PSA_ERROR_DOES_NOT_EXIST
2994 * \retval #PSA_ERROR_NOT_PERMITTED
2995 * \retval #PSA_ERROR_INVALID_ARGUMENT
2996 * \c step is not compatible with the operation's algorithm.
2997 * \retval #PSA_ERROR_INVALID_ARGUMENT
2998 * \c step does not allow key inputs.
2999 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3000 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3001 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003002 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003003 * \retval #PSA_ERROR_BAD_STATE
3004 * The value of \p step is not valid given the state of \p operation.
3005 * \retval #PSA_ERROR_BAD_STATE
3006 * The library has not been previously initialized by psa_crypto_init().
3007 * It is implementation-dependent whether a failure to initialize
3008 * results in this error code.
3009 */
3010psa_status_t psa_key_derivation_input_key(
3011 psa_key_derivation_operation_t *operation,
3012 psa_key_derivation_step_t step,
3013 psa_key_handle_t handle);
3014
3015/** Perform a key agreement and use the shared secret as input to a key
3016 * derivation.
3017 *
3018 * A key agreement algorithm takes two inputs: a private key \p private_key
3019 * a public key \p peer_key.
3020 * The result of this function is passed as input to a key derivation.
3021 * The output of this key derivation can be extracted by reading from the
3022 * resulting operation to produce keys and other cryptographic material.
3023 *
3024 * \param[in,out] operation The key derivation operation object to use.
3025 * It must have been set up with
3026 * psa_key_derivation_setup() with a
3027 * key agreement and derivation algorithm
3028 * \c alg (\c PSA_ALG_XXX value such that
3029 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3030 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3031 * is false).
3032 * The operation must be ready for an
3033 * input of the type given by \p step.
3034 * \param step Which step the input data is for.
3035 * \param private_key Handle to the private key to use.
3036 * \param[in] peer_key Public key of the peer. The peer key must be in the
3037 * same format that psa_import_key() accepts for the
3038 * public key type corresponding to the type of
3039 * private_key. That is, this function performs the
3040 * equivalent of
3041 * #psa_import_key(...,
3042 * `peer_key`, `peer_key_length`) where
3043 * with key attributes indicating the public key
3044 * type corresponding to the type of `private_key`.
3045 * For example, for EC keys, this means that peer_key
3046 * is interpreted as a point on the curve that the
3047 * private key is on. The standard formats for public
3048 * keys are documented in the documentation of
3049 * psa_export_public_key().
3050 * \param peer_key_length Size of \p peer_key in bytes.
3051 *
3052 * \retval #PSA_SUCCESS
3053 * Success.
3054 * \retval #PSA_ERROR_INVALID_HANDLE
3055 * \retval #PSA_ERROR_DOES_NOT_EXIST
3056 * \retval #PSA_ERROR_NOT_PERMITTED
3057 * \retval #PSA_ERROR_INVALID_ARGUMENT
3058 * \c private_key is not compatible with \c alg,
3059 * or \p peer_key is not valid for \c alg or not compatible with
3060 * \c private_key.
3061 * \retval #PSA_ERROR_NOT_SUPPORTED
3062 * \c alg is not supported or is not a key derivation algorithm.
3063 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3064 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3065 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003066 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003067 */
3068psa_status_t psa_key_derivation_key_agreement(
3069 psa_key_derivation_operation_t *operation,
3070 psa_key_derivation_step_t step,
3071 psa_key_handle_t private_key,
3072 const uint8_t *peer_key,
3073 size_t peer_key_length);
3074
Gilles Peskine35675b62019-05-16 17:26:11 +02003075/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003076 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003077 * This function calculates output bytes from a key derivation algorithm and
3078 * return those bytes.
3079 * If you view the key derivation's output as a stream of bytes, this
3080 * function destructively reads the requested number of bytes from the
3081 * stream.
3082 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003083 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003084 * \param[in,out] operation The key derivation operation object to read from.
3085 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003086 * \param output_length Number of bytes to output.
3087 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003088 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003089 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003090 * The operation's capacity was less than
3091 * \p output_length bytes. Note that in this case,
3092 * no output is written to the output buffer.
3093 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003094 * subsequent calls to this function will not
3095 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003096 * \retval #PSA_ERROR_BAD_STATE
3097 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3098 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3099 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003100 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003101 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003102psa_status_t psa_key_derivation_output_bytes(
3103 psa_key_derivation_operation_t *operation,
3104 uint8_t *output,
3105 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003106
Gilles Peskine35675b62019-05-16 17:26:11 +02003107/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003108 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003109 * This function calculates output bytes from a key derivation algorithm
3110 * and uses those bytes to generate a key deterministically.
3111 * If you view the key derivation's output as a stream of bytes, this
3112 * function destructively reads as many bytes as required from the
3113 * stream.
3114 * The operation's capacity decreases by the number of bytes read.
3115 *
3116 * How much output is produced and consumed from the operation, and how
3117 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003118 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003119 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003120 * of a given size, this function is functionally equivalent to
3121 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003122 * and passing the resulting output to #psa_import_key.
3123 * However, this function has a security benefit:
3124 * if the implementation provides an isolation boundary then
3125 * the key material is not exposed outside the isolation boundary.
3126 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003127 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003128 * The following key types defined in this specification follow this scheme:
3129 *
3130 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003131 * - #PSA_KEY_TYPE_ARC4;
3132 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003133 * - #PSA_KEY_TYPE_DERIVE;
3134 * - #PSA_KEY_TYPE_HMAC.
3135 *
3136 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003137 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003138 * Montgomery curve), this function always draws a byte string whose
3139 * length is determined by the curve, and sets the mandatory bits
3140 * accordingly. That is:
3141 *
3142 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3143 * and process it as specified in RFC 7748 &sect;5.
3144 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3145 * and process it as specified in RFC 7748 &sect;5.
3146 *
3147 * - For key types for which the key is represented by a single sequence of
3148 * \p bits bits with constraints as to which bit sequences are acceptable,
3149 * this function draws a byte string of length (\p bits / 8) bytes rounded
3150 * up to the nearest whole number of bytes. If the resulting byte string
3151 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3152 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003153 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003154 * for the output produced by psa_export_key().
3155 * The following key types defined in this specification follow this scheme:
3156 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003157 * - #PSA_KEY_TYPE_DES.
3158 * Force-set the parity bits, but discard forbidden weak keys.
3159 * For 2-key and 3-key triple-DES, the three keys are generated
3160 * successively (for example, for 3-key triple-DES,
3161 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3162 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003163 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003164 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003165 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003166 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003167 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003168 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003169 * Weierstrass curve).
3170 * For these key types, interpret the byte string as integer
3171 * in big-endian order. Discard it if it is not in the range
3172 * [0, *N* - 2] where *N* is the boundary of the private key domain
3173 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003174 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003175 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003176 * This method allows compliance to NIST standards, specifically
3177 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003178 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3179 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3180 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3181 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003182 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003183 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003184 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003185 * implementation-defined.
3186 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003187 * In all cases, the data that is read is discarded from the operation.
3188 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003189 *
Gilles Peskine20628592019-04-19 19:29:50 +02003190 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003191 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003192 * \param[out] handle On success, a handle to the newly created key.
3193 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003194 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003195 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003196 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003197 * If the key is persistent, the key material and the key's metadata
3198 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003199 * \retval #PSA_ERROR_ALREADY_EXISTS
3200 * This is an attempt to create a persistent key, and there is
3201 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003202 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003203 * There was not enough data to create the desired key.
3204 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003205 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003206 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003207 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003208 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003209 * implementation in general or in this particular location.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003210 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003211 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3212 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3213 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3214 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003215 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003216 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003217 * The library has not been previously initialized by psa_crypto_init().
3218 * It is implementation-dependent whether a failure to initialize
3219 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003220 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003221psa_status_t psa_key_derivation_output_key(
3222 const psa_key_attributes_t *attributes,
3223 psa_key_derivation_operation_t *operation,
3224 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003225
Gilles Peskine35675b62019-05-16 17:26:11 +02003226/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003227 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003228 * Once a key derivation operation has been aborted, its capacity is zero.
3229 * Aborting an operation frees all associated resources except for the
3230 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003231 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003232 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003233 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003234 * psa_key_derivation_operation_init() or a zero value. In particular,
3235 * it is valid to call psa_key_derivation_abort() twice, or to call
3236 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003237 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003238 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003239 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003240 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003241 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003242 * \retval #PSA_SUCCESS
3243 * \retval #PSA_ERROR_BAD_STATE
3244 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3245 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003246 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003247 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003248psa_status_t psa_key_derivation_abort(
3249 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003250
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003251/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003252 *
3253 * \warning The raw result of a key agreement algorithm such as finite-field
3254 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3255 * not be used directly as key material. It should instead be passed as
3256 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003257 * a key derivation, use psa_key_derivation_key_agreement() and other
3258 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003259 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003260 * \param alg The key agreement algorithm to compute
3261 * (\c PSA_ALG_XXX value such that
3262 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3263 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003264 * \param private_key Handle to the private key to use.
3265 * \param[in] peer_key Public key of the peer. It must be
3266 * in the same format that psa_import_key()
3267 * accepts. The standard formats for public
3268 * keys are documented in the documentation
3269 * of psa_export_public_key().
3270 * \param peer_key_length Size of \p peer_key in bytes.
3271 * \param[out] output Buffer where the decrypted message is to
3272 * be written.
3273 * \param output_size Size of the \c output buffer in bytes.
3274 * \param[out] output_length On success, the number of bytes
3275 * that make up the returned output.
3276 *
3277 * \retval #PSA_SUCCESS
3278 * Success.
3279 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003280 * \retval #PSA_ERROR_NOT_PERMITTED
3281 * \retval #PSA_ERROR_INVALID_ARGUMENT
3282 * \p alg is not a key agreement algorithm
3283 * \retval #PSA_ERROR_INVALID_ARGUMENT
3284 * \p private_key is not compatible with \p alg,
3285 * or \p peer_key is not valid for \p alg or not compatible with
3286 * \p private_key.
3287 * \retval #PSA_ERROR_NOT_SUPPORTED
3288 * \p alg is not a supported key agreement algorithm.
3289 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3290 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3291 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003292 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine769c7a62019-01-18 16:42:29 +01003293 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003294psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3295 psa_key_handle_t private_key,
3296 const uint8_t *peer_key,
3297 size_t peer_key_length,
3298 uint8_t *output,
3299 size_t output_size,
3300 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003301
Gilles Peskineea0fb492018-07-12 17:17:20 +02003302/**@}*/
3303
Gilles Peskineedd76872018-07-20 17:42:05 +02003304/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003305 * @{
3306 */
3307
3308/**
3309 * \brief Generate random bytes.
3310 *
3311 * \warning This function **can** fail! Callers MUST check the return status
3312 * and MUST NOT use the content of the output buffer if the return
3313 * status is not #PSA_SUCCESS.
3314 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003315 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003316 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003317 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003318 * \param output_size Number of bytes to generate and output.
3319 *
Gilles Peskine28538492018-07-11 17:34:00 +02003320 * \retval #PSA_SUCCESS
3321 * \retval #PSA_ERROR_NOT_SUPPORTED
3322 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3323 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3324 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003325 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003326 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003327 * The library has not been previously initialized by psa_crypto_init().
3328 * It is implementation-dependent whether a failure to initialize
3329 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003330 */
3331psa_status_t psa_generate_random(uint8_t *output,
3332 size_t output_size);
3333
3334/**
3335 * \brief Generate a key or key pair.
3336 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003337 * The key is generated randomly.
3338 * Its location, policy, type and size are taken from \p attributes.
3339 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003340 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003341 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003342 * the public exponent is 65537.
3343 * The modulus is a product of two probabilistic primes
3344 * between 2^{n-1} and 2^n where n is the bit size specified in the
3345 * attributes.
3346 *
Gilles Peskine20628592019-04-19 19:29:50 +02003347 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003348 * \param[out] handle On success, a handle to the newly created key.
3349 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003350 *
Gilles Peskine28538492018-07-11 17:34:00 +02003351 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003352 * Success.
3353 * If the key is persistent, the key material and the key's metadata
3354 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003355 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003356 * This is an attempt to create a persistent key, and there is
3357 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003358 * \retval #PSA_ERROR_NOT_SUPPORTED
3359 * \retval #PSA_ERROR_INVALID_ARGUMENT
3360 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3361 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3362 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3363 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003364 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003365 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003366 * The library has not been previously initialized by psa_crypto_init().
3367 * It is implementation-dependent whether a failure to initialize
3368 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003369 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003370psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003371 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003372
3373/**@}*/
3374
Gilles Peskinee59236f2018-01-27 23:32:46 +01003375#ifdef __cplusplus
3376}
3377#endif
3378
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003379/* The file "crypto_sizes.h" contains definitions for size calculation
3380 * macros whose definitions are implementation-specific. */
3381#include "crypto_sizes.h"
3382
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003383/* The file "crypto_struct.h" contains definitions for
3384 * implementation-specific structs that are declared above. */
3385#include "crypto_struct.h"
3386
3387/* The file "crypto_extra.h" contains vendor-specific definitions. This
3388 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003389#include "crypto_extra.h"
3390
3391#endif /* PSA_CRYPTO_H */