blob: ea7d18d2bcd70fda88ccf090b0cff24c820ed69d [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
361 * keys with identifiers in the range
362 * #PSA_KEY_ID_VENDOR_MIN&ndash;#PSA_KEY_ID_VENDOR_MAX.
363 *
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 *
Gilles Peskine4a231b82019-05-06 18:56:14 +0200369 * Implementations may provide additional keys that can be opened with
370 * psa_open_key(). Such keys have a key identifier in the vendor range,
371 * as documented in the description of #psa_key_id_t.
372 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100373 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100374 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100375 *
376 * \retval #PSA_SUCCESS
377 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100378 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100379 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200380 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100381 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine225010f2019-05-06 18:44:55 +0200382 * \p id is invalid.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100383 * \retval #PSA_ERROR_NOT_PERMITTED
384 * The specified key exists, but the application does not have the
385 * permission to access it. Note that this specification does not
386 * define any way to create such a key, but it may be possible
387 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200388 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
389 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100390 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200391psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100392 psa_key_handle_t *handle);
393
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100394
Gilles Peskinef535eb22018-11-30 14:08:36 +0100395/** Close a key handle.
396 *
397 * If the handle designates a volatile key, destroy the key material and
398 * free all associated resources, just like psa_destroy_key().
399 *
400 * If the handle designates a persistent key, free all resources associated
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100401 * with the key in volatile memory. The key in persistent storage is
Gilles Peskinef535eb22018-11-30 14:08:36 +0100402 * not affected and can be opened again later with psa_open_key().
403 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100404 * If the key is currently in use in a multipart operation,
405 * the multipart operation is aborted.
406 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100407 * \param handle The key handle to close.
408 *
409 * \retval #PSA_SUCCESS
410 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100411 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100412 */
413psa_status_t psa_close_key(psa_key_handle_t handle);
414
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100415/**@}*/
416
417/** \defgroup import_export Key import and export
418 * @{
419 */
420
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100421/**
422 * \brief Import a key in binary format.
423 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100424 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100425 * documentation of psa_export_public_key() for the format of public keys
426 * and to the documentation of psa_export_key() for the format for
427 * other key types.
428 *
429 * This specification supports a single format for each key type.
430 * Implementations may support other formats as long as the standard
431 * format is supported. Implementations that support other formats
432 * should ensure that the formats are clearly unambiguous so as to
433 * minimize the risk that an invalid input is accidentally interpreted
434 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100435 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100436
Gilles Peskine20628592019-04-19 19:29:50 +0200437 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200438 * The key size is always determined from the
439 * \p data buffer.
440 * If the key size in \p attributes is nonzero,
441 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200442 * \param[out] handle On success, a handle to the newly created key.
443 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100444 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200445 * buffer is interpreted according to the type declared
446 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200447 * All implementations must support at least the format
448 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100449 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200450 * the chosen type. Implementations may allow other
451 * formats, but should be conservative: implementations
452 * should err on the side of rejecting content if it
453 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200454 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100455 *
Gilles Peskine28538492018-07-11 17:34:00 +0200456 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100457 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100458 * If the key is persistent, the key material and the key's metadata
459 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200460 * \retval #PSA_ERROR_ALREADY_EXISTS
461 * This is an attempt to create a persistent key, and there is
462 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200463 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200464 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200465 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200466 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200467 * The key attributes, as a whole, are invalid.
468 * \retval #PSA_ERROR_INVALID_ARGUMENT
469 * The key data is not correctly formatted.
470 * \retval #PSA_ERROR_INVALID_ARGUMENT
471 * The size in \p attributes is nonzero and does not match the size
472 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200473 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
474 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
475 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100476 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200477 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200478 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300479 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300480 * The library has not been previously initialized by psa_crypto_init().
481 * It is implementation-dependent whether a failure to initialize
482 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100483 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200484psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100485 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200486 size_t data_length,
487 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100488
489/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100490 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200491 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100492 * This function destroys a key from both volatile
Gilles Peskine154bd952018-04-19 08:38:16 +0200493 * memory and, if applicable, non-volatile storage. Implementations shall
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100494 * make a best effort to ensure that that the key material cannot be recovered.
Gilles Peskine154bd952018-04-19 08:38:16 +0200495 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100496 * This function also erases any metadata such as policies and frees all
497 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200498 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100499 * \param handle Handle to the key to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100500 *
Gilles Peskine28538492018-07-11 17:34:00 +0200501 * \retval #PSA_SUCCESS
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100502 * The key material has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200503 * \retval #PSA_ERROR_NOT_PERMITTED
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100504 * The key cannot be erased because it is
Gilles Peskine65eb8582018-04-19 08:28:58 +0200505 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100506 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200507 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200508 * There was an failure in communication with the cryptoprocessor.
509 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200510 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200511 * The storage is corrupted. Implementations shall make a best effort
512 * to erase key material even in this stage, however applications
513 * should be aware that it may be impossible to guarantee that the
514 * key material is not recoverable in such cases.
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200515 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200516 * An unexpected condition which is not a storage corruption or
517 * a communication failure occurred. The cryptoprocessor may have
518 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300519 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300520 * The library has not been previously initialized by psa_crypto_init().
521 * It is implementation-dependent whether a failure to initialize
522 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100523 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100524psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100525
526/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100527 * \brief Export a key in binary format.
528 *
529 * The output of this function can be passed to psa_import_key() to
530 * create an equivalent object.
531 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100532 * If the implementation of psa_import_key() supports other formats
533 * beyond the format specified here, the output from psa_export_key()
534 * must use the representation specified here, not the original
535 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100536 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100537 * For standard key types, the output format is as follows:
538 *
539 * - For symmetric keys (including MAC keys), the format is the
540 * raw bytes of the key.
541 * - For DES, the key data consists of 8 bytes. The parity bits must be
542 * correct.
543 * - For Triple-DES, the format is the concatenation of the
544 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200545 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200546 * is the non-encrypted DER encoding of the representation defined by
547 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
548 * ```
549 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200550 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200551 * modulus INTEGER, -- n
552 * publicExponent INTEGER, -- e
553 * privateExponent INTEGER, -- d
554 * prime1 INTEGER, -- p
555 * prime2 INTEGER, -- q
556 * exponent1 INTEGER, -- d mod (p-1)
557 * exponent2 INTEGER, -- d mod (q-1)
558 * coefficient INTEGER, -- (inverse of q) mod p
559 * }
560 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200561 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200562 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100563 * a representation of the private value as a `ceiling(m/8)`-byte string
564 * where `m` is the bit size associated with the curve, i.e. the bit size
565 * of the order of the curve's coordinate field. This byte string is
566 * in little-endian order for Montgomery curves (curve types
567 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
568 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
569 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100570 * This is the content of the `privateKey` field of the `ECPrivateKey`
571 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200572 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200573 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000574 * format is the representation of the private key `x` as a big-endian byte
575 * string. The length of the byte string is the private key size in bytes
576 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200577 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
578 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100579 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200580 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
581 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100582 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200583 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200584 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200585 * \param[out] data_length On success, the number of bytes
586 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100587 *
Gilles Peskine28538492018-07-11 17:34:00 +0200588 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100589 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200590 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200591 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200592 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100593 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200594 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
595 * The size of the \p data buffer is too small. You can determine a
596 * sufficient buffer size by calling
597 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
598 * where \c type is the key type
599 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200600 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
601 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200602 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300603 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300604 * The library has not been previously initialized by psa_crypto_init().
605 * It is implementation-dependent whether a failure to initialize
606 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100607 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100608psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100609 uint8_t *data,
610 size_t data_size,
611 size_t *data_length);
612
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100613/**
614 * \brief Export a public key or the public part of a key pair in binary format.
615 *
616 * The output of this function can be passed to psa_import_key() to
617 * create an object that is equivalent to the public key.
618 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000619 * This specification supports a single format for each key type.
620 * Implementations may support other formats as long as the standard
621 * format is supported. Implementations that support other formats
622 * should ensure that the formats are clearly unambiguous so as to
623 * minimize the risk that an invalid input is accidentally interpreted
624 * according to a different format.
625 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000626 * For standard key types, the output format is as follows:
627 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
628 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
629 * ```
630 * RSAPublicKey ::= SEQUENCE {
631 * modulus INTEGER, -- n
632 * publicExponent INTEGER } -- e
633 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000634 * - For elliptic curve public keys (key types for which
635 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
636 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
637 * Let `m` be the bit size associated with the curve, i.e. the bit size of
638 * `q` for a curve over `F_q`. The representation consists of:
639 * - The byte 0x04;
640 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
641 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200642 * - For Diffie-Hellman key exchange public keys (key types for which
643 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000644 * the format is the representation of the public key `y = g^x mod p` as a
645 * big-endian byte string. The length of the byte string is the length of the
646 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100647 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200648 * Exporting a public key object or the public part of a key pair is
649 * always permitted, regardless of the key's usage flags.
650 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100651 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200652 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200653 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200654 * \param[out] data_length On success, the number of bytes
655 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100656 *
Gilles Peskine28538492018-07-11 17:34:00 +0200657 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100658 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200659 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200660 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200661 * The key is neither a public key nor a key pair.
662 * \retval #PSA_ERROR_NOT_SUPPORTED
663 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
664 * The size of the \p data buffer is too small. You can determine a
665 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200666 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200667 * where \c type is the key type
668 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200669 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
670 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200671 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300672 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300673 * The library has not been previously initialized by psa_crypto_init().
674 * It is implementation-dependent whether a failure to initialize
675 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100676 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100677psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100678 uint8_t *data,
679 size_t data_size,
680 size_t *data_length);
681
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100682/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100683 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100684 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000685 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100686 * This function is primarily useful to copy a key from one location
687 * to another, since it populates a key using the material from
688 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200689 *
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100690 * This function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100691 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100692 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200693 * The policy on the source key must have the usage flag
694 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200695 * This flag is sufficient to permit the copy if the key has the lifetime
696 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
697 * Some secure elements do not provide a way to copy a key without
698 * making it extractable from the secure element. If a key is located
699 * in such a secure element, then the key must have both usage flags
700 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
701 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200702 *
Gilles Peskine20628592019-04-19 19:29:50 +0200703 * The resulting key may only be used in a way that conforms to
704 * both the policy of the original key and the policy specified in
705 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100706 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200707 * usage flags on the source policy and the usage flags in \p attributes.
708 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100709 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200710 * - If either of the policies allows an algorithm and the other policy
711 * allows a wildcard-based algorithm policy that includes this algorithm,
712 * the resulting key allows the same algorithm.
713 * - If the policies do not allow any algorithm in common, this function
714 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200715 *
Gilles Peskine20628592019-04-19 19:29:50 +0200716 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100717 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200718 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100719 * \param source_handle The key to copy. It must be a valid key handle.
Gilles Peskine20628592019-04-19 19:29:50 +0200720 * \param[in] attributes The attributes for the new key.
721 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200722 * - The key type and size may be 0. If either is
723 * nonzero, it must match the corresponding
724 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200725 * - The key location (the lifetime and, for
726 * persistent keys, the key identifier) is
727 * used directly.
728 * - The policy constraints (usage flags and
729 * algorithm policy) are combined from
730 * the source key and \p attributes so that
731 * both sets of restrictions apply, as
732 * described in the documentation of this function.
733 * \param[out] target_handle On success, a handle to the newly created key.
734 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200735 *
736 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100737 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200738 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200739 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200740 * This is an attempt to create a persistent key, and there is
741 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200742 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200743 * The lifetime or identifier in \p attributes are invalid.
744 * \retval #PSA_ERROR_INVALID_ARGUMENT
745 * The policy constraints on the source and specified in
746 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200747 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200748 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200749 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100750 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200751 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
752 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100753 * The source key is not exportable and its lifetime does not
754 * allow copying it to the target's lifetime.
755 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
756 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200757 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
758 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200759 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100760 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100761psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200762 const psa_key_attributes_t *attributes,
763 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100764
765/**@}*/
766
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100767/** \defgroup hash Message digests
768 * @{
769 */
770
Gilles Peskine69647a42019-01-14 20:18:12 +0100771/** Calculate the hash (digest) of a message.
772 *
773 * \note To verify the hash of a message against an
774 * expected value, use psa_hash_compare() instead.
775 *
776 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
777 * such that #PSA_ALG_IS_HASH(\p alg) is true).
778 * \param[in] input Buffer containing the message to hash.
779 * \param input_length Size of the \p input buffer in bytes.
780 * \param[out] hash Buffer where the hash is to be written.
781 * \param hash_size Size of the \p hash buffer in bytes.
782 * \param[out] hash_length On success, the number of bytes
783 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100784 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100785 *
786 * \retval #PSA_SUCCESS
787 * Success.
788 * \retval #PSA_ERROR_NOT_SUPPORTED
789 * \p alg is not supported or is not a hash algorithm.
790 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
791 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
792 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200793 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100794 */
795psa_status_t psa_hash_compute(psa_algorithm_t alg,
796 const uint8_t *input,
797 size_t input_length,
798 uint8_t *hash,
799 size_t hash_size,
800 size_t *hash_length);
801
802/** Calculate the hash (digest) of a message and compare it with a
803 * reference value.
804 *
805 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
806 * such that #PSA_ALG_IS_HASH(\p alg) is true).
807 * \param[in] input Buffer containing the message to hash.
808 * \param input_length Size of the \p input buffer in bytes.
809 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100810 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100811 *
812 * \retval #PSA_SUCCESS
813 * The expected hash is identical to the actual hash of the input.
814 * \retval #PSA_ERROR_INVALID_SIGNATURE
815 * The hash of the message was calculated successfully, but it
816 * differs from the expected hash.
817 * \retval #PSA_ERROR_NOT_SUPPORTED
818 * \p alg is not supported or is not a hash algorithm.
819 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
820 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
821 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200822 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100823 */
824psa_status_t psa_hash_compare(psa_algorithm_t alg,
825 const uint8_t *input,
826 size_t input_length,
827 const uint8_t *hash,
828 const size_t hash_length);
829
Gilles Peskine308b91d2018-02-08 09:47:44 +0100830/** The type of the state data structure for multipart hash operations.
831 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000832 * Before calling any function on a hash operation object, the application must
833 * initialize it by any of the following means:
834 * - Set the structure to all-bits-zero, for example:
835 * \code
836 * psa_hash_operation_t operation;
837 * memset(&operation, 0, sizeof(operation));
838 * \endcode
839 * - Initialize the structure to logical zero values, for example:
840 * \code
841 * psa_hash_operation_t operation = {0};
842 * \endcode
843 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
844 * for example:
845 * \code
846 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
847 * \endcode
848 * - Assign the result of the function psa_hash_operation_init()
849 * to the structure, for example:
850 * \code
851 * psa_hash_operation_t operation;
852 * operation = psa_hash_operation_init();
853 * \endcode
854 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100855 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100856 * make any assumptions about the content of this structure except
857 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100858typedef struct psa_hash_operation_s psa_hash_operation_t;
859
Jaeden Amero6a25b412019-01-04 11:47:44 +0000860/** \def PSA_HASH_OPERATION_INIT
861 *
862 * This macro returns a suitable initializer for a hash operation object
863 * of type #psa_hash_operation_t.
864 */
865#ifdef __DOXYGEN_ONLY__
866/* This is an example definition for documentation purposes.
867 * Implementations should define a suitable value in `crypto_struct.h`.
868 */
869#define PSA_HASH_OPERATION_INIT {0}
870#endif
871
872/** Return an initial value for a hash operation object.
873 */
874static psa_hash_operation_t psa_hash_operation_init(void);
875
Gilles Peskinef45adda2019-01-14 18:29:18 +0100876/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100877 *
878 * The sequence of operations to calculate a hash (message digest)
879 * is as follows:
880 * -# Allocate an operation object which will be passed to all the functions
881 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000882 * -# Initialize the operation object with one of the methods described in the
883 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200884 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100885 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100886 * of the message each time. The hash that is calculated is the hash
887 * of the concatenation of these messages in order.
888 * -# To calculate the hash, call psa_hash_finish().
889 * To compare the hash with an expected value, call psa_hash_verify().
890 *
891 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000892 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100893 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200894 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100895 * eventually terminate the operation. The following events terminate an
896 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100897 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100898 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100899 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000900 * \param[in,out] operation The operation object to set up. It must have
901 * been initialized as per the documentation for
902 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200903 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
904 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100905 *
Gilles Peskine28538492018-07-11 17:34:00 +0200906 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100907 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200908 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200909 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100910 * \retval #PSA_ERROR_BAD_STATE
911 * The operation state is not valid (already set up and not
912 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200913 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
914 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
915 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200916 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100917 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200918psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100919 psa_algorithm_t alg);
920
Gilles Peskine308b91d2018-02-08 09:47:44 +0100921/** Add a message fragment to a multipart hash operation.
922 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200923 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100924 *
925 * If this function returns an error status, the operation becomes inactive.
926 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200927 * \param[in,out] operation Active hash operation.
928 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200929 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100930 *
Gilles Peskine28538492018-07-11 17:34:00 +0200931 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100932 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200933 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100934 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200935 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
936 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
937 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200938 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100939 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100940psa_status_t psa_hash_update(psa_hash_operation_t *operation,
941 const uint8_t *input,
942 size_t input_length);
943
Gilles Peskine308b91d2018-02-08 09:47:44 +0100944/** Finish the calculation of the hash of a message.
945 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200946 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100947 * This function calculates the hash of the message formed by concatenating
948 * the inputs passed to preceding calls to psa_hash_update().
949 *
950 * When this function returns, the operation becomes inactive.
951 *
952 * \warning Applications should not call this function if they expect
953 * a specific value for the hash. Call psa_hash_verify() instead.
954 * Beware that comparing integrity or authenticity data such as
955 * hash values with a function such as \c memcmp is risky
956 * because the time taken by the comparison may leak information
957 * about the hashed data which could allow an attacker to guess
958 * a valid hash and thereby bypass security controls.
959 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200960 * \param[in,out] operation Active hash operation.
961 * \param[out] hash Buffer where the hash is to be written.
962 * \param hash_size Size of the \p hash buffer in bytes.
963 * \param[out] hash_length On success, the number of bytes
964 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200965 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200966 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100967 *
Gilles Peskine28538492018-07-11 17:34:00 +0200968 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100969 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200970 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100971 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200972 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200973 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200974 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100975 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200976 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
977 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
978 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200979 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100980 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100981psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
982 uint8_t *hash,
983 size_t hash_size,
984 size_t *hash_length);
985
Gilles Peskine308b91d2018-02-08 09:47:44 +0100986/** Finish the calculation of the hash of a message and compare it with
987 * an expected value.
988 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200989 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100990 * This function calculates the hash of the message formed by concatenating
991 * the inputs passed to preceding calls to psa_hash_update(). It then
992 * compares the calculated hash with the expected hash passed as a
993 * parameter to this function.
994 *
995 * When this function returns, the operation becomes inactive.
996 *
Gilles Peskine19067982018-03-20 17:54:53 +0100997 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100998 * comparison between the actual hash and the expected hash is performed
999 * in constant time.
1000 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001001 * \param[in,out] operation Active hash operation.
1002 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001003 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001004 *
Gilles Peskine28538492018-07-11 17:34:00 +02001005 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001006 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001007 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001008 * The hash of the message was calculated successfully, but it
1009 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001010 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001011 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001012 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1013 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1014 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001015 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001016 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001017psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1018 const uint8_t *hash,
1019 size_t hash_length);
1020
Gilles Peskine308b91d2018-02-08 09:47:44 +01001021/** Abort a hash operation.
1022 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001023 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001024 * \p operation structure itself. Once aborted, the operation object
1025 * can be reused for another operation by calling
1026 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001027 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001028 * You may call this function any time after the operation object has
1029 * been initialized by any of the following methods:
1030 * - A call to psa_hash_setup(), whether it succeeds or not.
1031 * - Initializing the \c struct to all-bits-zero.
1032 * - Initializing the \c struct to logical zeros, e.g.
1033 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001034 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001035 * In particular, calling psa_hash_abort() after the operation has been
1036 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1037 * psa_hash_verify() is safe and has no effect.
1038 *
1039 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001040 *
Gilles Peskine28538492018-07-11 17:34:00 +02001041 * \retval #PSA_SUCCESS
1042 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001043 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001044 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1045 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001046 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001047 */
1048psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001049
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001050/** Clone a hash operation.
1051 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001052 * This function copies the state of an ongoing hash operation to
1053 * a new operation object. In other words, this function is equivalent
1054 * to calling psa_hash_setup() on \p target_operation with the same
1055 * algorithm that \p source_operation was set up for, then
1056 * psa_hash_update() on \p target_operation with the same input that
1057 * that was passed to \p source_operation. After this function returns, the
1058 * two objects are independent, i.e. subsequent calls involving one of
1059 * the objects do not affect the other object.
1060 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001061 * \param[in] source_operation The active hash operation to clone.
1062 * \param[in,out] target_operation The operation object to set up.
1063 * It must be initialized but not active.
1064 *
1065 * \retval #PSA_SUCCESS
1066 * \retval #PSA_ERROR_BAD_STATE
1067 * \p source_operation is not an active hash operation.
1068 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001069 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001070 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1071 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001072 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001073 */
1074psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1075 psa_hash_operation_t *target_operation);
1076
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001077/**@}*/
1078
Gilles Peskine8c9def32018-02-08 10:02:12 +01001079/** \defgroup MAC Message authentication codes
1080 * @{
1081 */
1082
Gilles Peskine69647a42019-01-14 20:18:12 +01001083/** Calculate the MAC (message authentication code) of a message.
1084 *
1085 * \note To verify the MAC of a message against an
1086 * expected value, use psa_mac_verify() instead.
1087 * Beware that comparing integrity or authenticity data such as
1088 * MAC values with a function such as \c memcmp is risky
1089 * because the time taken by the comparison may leak information
1090 * about the MAC value which could allow an attacker to guess
1091 * a valid MAC and thereby bypass security controls.
1092 *
1093 * \param handle Handle to the key to use for the operation.
1094 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001095 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001096 * \param[in] input Buffer containing the input message.
1097 * \param input_length Size of the \p input buffer in bytes.
1098 * \param[out] mac Buffer where the MAC value is to be written.
1099 * \param mac_size Size of the \p mac buffer in bytes.
1100 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001101 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001102 *
1103 * \retval #PSA_SUCCESS
1104 * Success.
1105 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001106 * \retval #PSA_ERROR_NOT_PERMITTED
1107 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001108 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001109 * \retval #PSA_ERROR_NOT_SUPPORTED
1110 * \p alg is not supported or is not a MAC algorithm.
1111 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1112 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1113 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001114 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001115 * \retval #PSA_ERROR_BAD_STATE
1116 * The library has not been previously initialized by psa_crypto_init().
1117 * It is implementation-dependent whether a failure to initialize
1118 * results in this error code.
1119 */
1120psa_status_t psa_mac_compute(psa_key_handle_t handle,
1121 psa_algorithm_t alg,
1122 const uint8_t *input,
1123 size_t input_length,
1124 uint8_t *mac,
1125 size_t mac_size,
1126 size_t *mac_length);
1127
1128/** Calculate the MAC of a message and compare it with a reference value.
1129 *
1130 * \param handle Handle to the key to use for the operation.
1131 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001132 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001133 * \param[in] input Buffer containing the input message.
1134 * \param input_length Size of the \p input buffer in bytes.
1135 * \param[out] mac Buffer containing the expected MAC value.
1136 * \param mac_length Size of the \p mac buffer in bytes.
1137 *
1138 * \retval #PSA_SUCCESS
1139 * The expected MAC is identical to the actual MAC of the input.
1140 * \retval #PSA_ERROR_INVALID_SIGNATURE
1141 * The MAC of the message was calculated successfully, but it
1142 * differs from the expected value.
1143 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001144 * \retval #PSA_ERROR_NOT_PERMITTED
1145 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001146 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001147 * \retval #PSA_ERROR_NOT_SUPPORTED
1148 * \p alg is not supported or is not a MAC algorithm.
1149 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1150 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1151 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001152 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001153 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001154psa_status_t psa_mac_verify(psa_key_handle_t handle,
1155 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001156 const uint8_t *input,
1157 size_t input_length,
1158 const uint8_t *mac,
1159 const size_t mac_length);
1160
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001161/** The type of the state data structure for multipart MAC operations.
1162 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001163 * Before calling any function on a MAC operation object, the application must
1164 * initialize it by any of the following means:
1165 * - Set the structure to all-bits-zero, for example:
1166 * \code
1167 * psa_mac_operation_t operation;
1168 * memset(&operation, 0, sizeof(operation));
1169 * \endcode
1170 * - Initialize the structure to logical zero values, for example:
1171 * \code
1172 * psa_mac_operation_t operation = {0};
1173 * \endcode
1174 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1175 * for example:
1176 * \code
1177 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1178 * \endcode
1179 * - Assign the result of the function psa_mac_operation_init()
1180 * to the structure, for example:
1181 * \code
1182 * psa_mac_operation_t operation;
1183 * operation = psa_mac_operation_init();
1184 * \endcode
1185 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001186 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001187 * make any assumptions about the content of this structure except
1188 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001189typedef struct psa_mac_operation_s psa_mac_operation_t;
1190
Jaeden Amero769ce272019-01-04 11:48:03 +00001191/** \def PSA_MAC_OPERATION_INIT
1192 *
1193 * This macro returns a suitable initializer for a MAC operation object of type
1194 * #psa_mac_operation_t.
1195 */
1196#ifdef __DOXYGEN_ONLY__
1197/* This is an example definition for documentation purposes.
1198 * Implementations should define a suitable value in `crypto_struct.h`.
1199 */
1200#define PSA_MAC_OPERATION_INIT {0}
1201#endif
1202
1203/** Return an initial value for a MAC operation object.
1204 */
1205static psa_mac_operation_t psa_mac_operation_init(void);
1206
Gilles Peskinef45adda2019-01-14 18:29:18 +01001207/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001208 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001209 * This function sets up the calculation of the MAC
1210 * (message authentication code) of a byte string.
1211 * To verify the MAC of a message against an
1212 * expected value, use psa_mac_verify_setup() instead.
1213 *
1214 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001215 * -# Allocate an operation object which will be passed to all the functions
1216 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001217 * -# Initialize the operation object with one of the methods described in the
1218 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001219 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001220 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1221 * of the message each time. The MAC that is calculated is the MAC
1222 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001223 * -# At the end of the message, call psa_mac_sign_finish() to finish
1224 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001225 *
1226 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001227 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001228 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001229 * After a successful call to psa_mac_sign_setup(), the application must
1230 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001231 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001232 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001233 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001234 * \param[in,out] operation The operation object to set up. It must have
1235 * been initialized as per the documentation for
1236 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001237 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001238 * It must remain valid until the operation
1239 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001240 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001241 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001242 *
Gilles Peskine28538492018-07-11 17:34:00 +02001243 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001244 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001245 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001246 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001247 * \retval #PSA_ERROR_NOT_PERMITTED
1248 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001249 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001250 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001251 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001252 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1253 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1254 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001255 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001256 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001257 * The operation state is not valid (already set up and not
1258 * subsequently completed).
1259 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001260 * The library has not been previously initialized by psa_crypto_init().
1261 * It is implementation-dependent whether a failure to initialize
1262 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001263 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001264psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001265 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001266 psa_algorithm_t alg);
1267
Gilles Peskinef45adda2019-01-14 18:29:18 +01001268/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001269 *
1270 * This function sets up the verification of the MAC
1271 * (message authentication code) of a byte string against an expected value.
1272 *
1273 * The sequence of operations to verify a MAC is as follows:
1274 * -# Allocate an operation object which will be passed to all the functions
1275 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001276 * -# Initialize the operation object with one of the methods described in the
1277 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001278 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001279 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1280 * of the message each time. The MAC that is calculated is the MAC
1281 * of the concatenation of these messages in order.
1282 * -# At the end of the message, call psa_mac_verify_finish() to finish
1283 * calculating the actual MAC of the message and verify it against
1284 * the expected value.
1285 *
1286 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001287 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001288 *
1289 * After a successful call to psa_mac_verify_setup(), the application must
1290 * eventually terminate the operation through one of the following methods:
1291 * - A failed call to psa_mac_update().
1292 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1293 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001294 * \param[in,out] operation The operation object to set up. It must have
1295 * been initialized as per the documentation for
1296 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001297 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001298 * It must remain valid until the operation
1299 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001300 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1301 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001302 *
Gilles Peskine28538492018-07-11 17:34:00 +02001303 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001304 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001305 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001306 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001307 * \retval #PSA_ERROR_NOT_PERMITTED
1308 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001309 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001310 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001311 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001312 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1313 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1314 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001315 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001316 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001317 * The operation state is not valid (already set up and not
1318 * subsequently completed).
1319 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001320 * The library has not been previously initialized by psa_crypto_init().
1321 * It is implementation-dependent whether a failure to initialize
1322 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001323 */
1324psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001325 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001326 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001327
Gilles Peskinedcd14942018-07-12 00:30:52 +02001328/** Add a message fragment to a multipart MAC operation.
1329 *
1330 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1331 * before calling this function.
1332 *
1333 * If this function returns an error status, the operation becomes inactive.
1334 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001335 * \param[in,out] operation Active MAC operation.
1336 * \param[in] input Buffer containing the message fragment to add to
1337 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001338 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001339 *
1340 * \retval #PSA_SUCCESS
1341 * Success.
1342 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001343 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001344 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1345 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1346 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001347 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001348 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001349psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1350 const uint8_t *input,
1351 size_t input_length);
1352
Gilles Peskinedcd14942018-07-12 00:30:52 +02001353/** Finish the calculation of the MAC of a message.
1354 *
1355 * The application must call psa_mac_sign_setup() before calling this function.
1356 * This function calculates the MAC of the message formed by concatenating
1357 * the inputs passed to preceding calls to psa_mac_update().
1358 *
1359 * When this function returns, the operation becomes inactive.
1360 *
1361 * \warning Applications should not call this function if they expect
1362 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1363 * Beware that comparing integrity or authenticity data such as
1364 * MAC values with a function such as \c memcmp is risky
1365 * because the time taken by the comparison may leak information
1366 * about the MAC value which could allow an attacker to guess
1367 * a valid MAC and thereby bypass security controls.
1368 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001369 * \param[in,out] operation Active MAC operation.
1370 * \param[out] mac Buffer where the MAC value is to be written.
1371 * \param mac_size Size of the \p mac buffer in bytes.
1372 * \param[out] mac_length On success, the number of bytes
1373 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001374 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001375 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001376 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001377 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001378 *
1379 * \retval #PSA_SUCCESS
1380 * Success.
1381 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001382 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001383 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001384 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001385 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1386 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1387 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1388 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001389 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001390 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001391psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1392 uint8_t *mac,
1393 size_t mac_size,
1394 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001395
Gilles Peskinedcd14942018-07-12 00:30:52 +02001396/** Finish the calculation of the MAC of a message and compare it with
1397 * an expected value.
1398 *
1399 * The application must call psa_mac_verify_setup() before calling this function.
1400 * This function calculates the MAC of the message formed by concatenating
1401 * the inputs passed to preceding calls to psa_mac_update(). It then
1402 * compares the calculated MAC with the expected MAC passed as a
1403 * parameter to this function.
1404 *
1405 * When this function returns, the operation becomes inactive.
1406 *
1407 * \note Implementations shall make the best effort to ensure that the
1408 * comparison between the actual MAC and the expected MAC is performed
1409 * in constant time.
1410 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001411 * \param[in,out] operation Active MAC operation.
1412 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001413 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001414 *
1415 * \retval #PSA_SUCCESS
1416 * The expected MAC is identical to the actual MAC of the message.
1417 * \retval #PSA_ERROR_INVALID_SIGNATURE
1418 * The MAC of the message was calculated successfully, but it
1419 * differs from the expected MAC.
1420 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001421 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001422 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1423 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1424 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001425 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001426 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001427psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1428 const uint8_t *mac,
1429 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001430
Gilles Peskinedcd14942018-07-12 00:30:52 +02001431/** Abort a MAC operation.
1432 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001433 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001434 * \p operation structure itself. Once aborted, the operation object
1435 * can be reused for another operation by calling
1436 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001437 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001438 * You may call this function any time after the operation object has
1439 * been initialized by any of the following methods:
1440 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1441 * it succeeds or not.
1442 * - Initializing the \c struct to all-bits-zero.
1443 * - Initializing the \c struct to logical zeros, e.g.
1444 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001445 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001446 * In particular, calling psa_mac_abort() after the operation has been
1447 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1448 * psa_mac_verify_finish() is safe and has no effect.
1449 *
1450 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001451 *
1452 * \retval #PSA_SUCCESS
1453 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001454 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001455 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1456 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001457 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001458 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001459psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1460
1461/**@}*/
1462
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001463/** \defgroup cipher Symmetric ciphers
1464 * @{
1465 */
1466
Gilles Peskine69647a42019-01-14 20:18:12 +01001467/** Encrypt a message using a symmetric cipher.
1468 *
1469 * This function encrypts a message with a random IV (initialization
1470 * vector).
1471 *
1472 * \param handle Handle to the key to use for the operation.
1473 * It must remain valid until the operation
1474 * terminates.
1475 * \param alg The cipher algorithm to compute
1476 * (\c PSA_ALG_XXX value such that
1477 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1478 * \param[in] input Buffer containing the message to encrypt.
1479 * \param input_length Size of the \p input buffer in bytes.
1480 * \param[out] output Buffer where the output is to be written.
1481 * The output contains the IV followed by
1482 * the ciphertext proper.
1483 * \param output_size Size of the \p output buffer in bytes.
1484 * \param[out] output_length On success, the number of bytes
1485 * that make up the output.
1486 *
1487 * \retval #PSA_SUCCESS
1488 * Success.
1489 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001490 * \retval #PSA_ERROR_NOT_PERMITTED
1491 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001492 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001493 * \retval #PSA_ERROR_NOT_SUPPORTED
1494 * \p alg is not supported or is not a cipher algorithm.
1495 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1496 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1497 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1498 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001499 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001500 */
1501psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1502 psa_algorithm_t alg,
1503 const uint8_t *input,
1504 size_t input_length,
1505 uint8_t *output,
1506 size_t output_size,
1507 size_t *output_length);
1508
1509/** Decrypt a message using a symmetric cipher.
1510 *
1511 * This function decrypts a message encrypted with a symmetric cipher.
1512 *
1513 * \param handle Handle to the key to use for the operation.
1514 * It must remain valid until the operation
1515 * terminates.
1516 * \param alg The cipher algorithm to compute
1517 * (\c PSA_ALG_XXX value such that
1518 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1519 * \param[in] input Buffer containing the message to decrypt.
1520 * This consists of the IV followed by the
1521 * ciphertext proper.
1522 * \param input_length Size of the \p input buffer in bytes.
1523 * \param[out] output Buffer where the plaintext is to be written.
1524 * \param output_size Size of the \p output buffer in bytes.
1525 * \param[out] output_length On success, the number of bytes
1526 * that make up the output.
1527 *
1528 * \retval #PSA_SUCCESS
1529 * Success.
1530 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001531 * \retval #PSA_ERROR_NOT_PERMITTED
1532 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001533 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001534 * \retval #PSA_ERROR_NOT_SUPPORTED
1535 * \p alg is not supported or is not a cipher algorithm.
1536 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1537 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1538 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1539 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001540 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001541 */
1542psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1543 psa_algorithm_t alg,
1544 const uint8_t *input,
1545 size_t input_length,
1546 uint8_t *output,
1547 size_t output_size,
1548 size_t *output_length);
1549
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001550/** The type of the state data structure for multipart cipher operations.
1551 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001552 * Before calling any function on a cipher operation object, the application
1553 * must initialize it by any of the following means:
1554 * - Set the structure to all-bits-zero, for example:
1555 * \code
1556 * psa_cipher_operation_t operation;
1557 * memset(&operation, 0, sizeof(operation));
1558 * \endcode
1559 * - Initialize the structure to logical zero values, for example:
1560 * \code
1561 * psa_cipher_operation_t operation = {0};
1562 * \endcode
1563 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1564 * for example:
1565 * \code
1566 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1567 * \endcode
1568 * - Assign the result of the function psa_cipher_operation_init()
1569 * to the structure, for example:
1570 * \code
1571 * psa_cipher_operation_t operation;
1572 * operation = psa_cipher_operation_init();
1573 * \endcode
1574 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001575 * This is an implementation-defined \c struct. Applications should not
1576 * make any assumptions about the content of this structure except
1577 * as directed by the documentation of a specific implementation. */
1578typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1579
Jaeden Amero5bae2272019-01-04 11:48:27 +00001580/** \def PSA_CIPHER_OPERATION_INIT
1581 *
1582 * This macro returns a suitable initializer for a cipher operation object of
1583 * type #psa_cipher_operation_t.
1584 */
1585#ifdef __DOXYGEN_ONLY__
1586/* This is an example definition for documentation purposes.
1587 * Implementations should define a suitable value in `crypto_struct.h`.
1588 */
1589#define PSA_CIPHER_OPERATION_INIT {0}
1590#endif
1591
1592/** Return an initial value for a cipher operation object.
1593 */
1594static psa_cipher_operation_t psa_cipher_operation_init(void);
1595
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001596/** Set the key for a multipart symmetric encryption operation.
1597 *
1598 * The sequence of operations to encrypt a message with a symmetric cipher
1599 * is as follows:
1600 * -# Allocate an operation object which will be passed to all the functions
1601 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001602 * -# Initialize the operation object with one of the methods described in the
1603 * documentation for #psa_cipher_operation_t, e.g.
1604 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001605 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001606 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001607 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001608 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001609 * requires a specific IV value.
1610 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1611 * of the message each time.
1612 * -# Call psa_cipher_finish().
1613 *
1614 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001615 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001616 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001617 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001618 * eventually terminate the operation. The following events terminate an
1619 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001620 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001621 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001622 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001623 * \param[in,out] operation The operation object to set up. It must have
1624 * been initialized as per the documentation for
1625 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001626 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001627 * It must remain valid until the operation
1628 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001629 * \param alg The cipher algorithm to compute
1630 * (\c PSA_ALG_XXX value such that
1631 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001632 *
Gilles Peskine28538492018-07-11 17:34:00 +02001633 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001634 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001635 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001636 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001637 * \retval #PSA_ERROR_NOT_PERMITTED
1638 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001639 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001640 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001641 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001642 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1643 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1644 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001645 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001646 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001647 * The operation state is not valid (already set up and not
1648 * subsequently completed).
1649 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001650 * The library has not been previously initialized by psa_crypto_init().
1651 * It is implementation-dependent whether a failure to initialize
1652 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001653 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001654psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001655 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001656 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001657
1658/** Set the key for a multipart symmetric decryption operation.
1659 *
1660 * The sequence of operations to decrypt a message with a symmetric cipher
1661 * is as follows:
1662 * -# Allocate an operation object which will be passed to all the functions
1663 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001664 * -# Initialize the operation object with one of the methods described in the
1665 * documentation for #psa_cipher_operation_t, e.g.
1666 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001667 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001668 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001669 * decryption. If the IV is prepended to the ciphertext, you can call
1670 * psa_cipher_update() on a buffer containing the IV followed by the
1671 * beginning of the message.
1672 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1673 * of the message each time.
1674 * -# Call psa_cipher_finish().
1675 *
1676 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001677 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001678 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001679 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001680 * eventually terminate the operation. The following events terminate an
1681 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001682 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001683 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001684 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001685 * \param[in,out] operation The operation object to set up. It must have
1686 * been initialized as per the documentation for
1687 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001688 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001689 * It must remain valid until the operation
1690 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001691 * \param alg The cipher algorithm to compute
1692 * (\c PSA_ALG_XXX value such that
1693 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001694 *
Gilles Peskine28538492018-07-11 17:34:00 +02001695 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001696 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001697 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001698 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001699 * \retval #PSA_ERROR_NOT_PERMITTED
1700 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001701 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001702 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001703 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001704 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1705 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1706 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001707 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001708 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001709 * The operation state is not valid (already set up and not
1710 * subsequently completed).
1711 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001712 * The library has not been previously initialized by psa_crypto_init().
1713 * It is implementation-dependent whether a failure to initialize
1714 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001715 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001716psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001717 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001718 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001719
Gilles Peskinedcd14942018-07-12 00:30:52 +02001720/** Generate an IV for a symmetric encryption operation.
1721 *
1722 * This function generates a random IV (initialization vector), nonce
1723 * or initial counter value for the encryption operation as appropriate
1724 * for the chosen algorithm, key type and key size.
1725 *
1726 * The application must call psa_cipher_encrypt_setup() before
1727 * calling this function.
1728 *
1729 * If this function returns an error status, the operation becomes inactive.
1730 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001731 * \param[in,out] operation Active cipher operation.
1732 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001733 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001734 * \param[out] iv_length On success, the number of bytes of the
1735 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001736 *
1737 * \retval #PSA_SUCCESS
1738 * Success.
1739 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001740 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001741 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001742 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001743 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1744 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1745 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001746 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001747 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001748psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1749 unsigned char *iv,
1750 size_t iv_size,
1751 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001752
Gilles Peskinedcd14942018-07-12 00:30:52 +02001753/** Set the IV for a symmetric encryption or decryption operation.
1754 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001755 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001756 * or initial counter value for the encryption or decryption operation.
1757 *
1758 * The application must call psa_cipher_encrypt_setup() before
1759 * calling this function.
1760 *
1761 * If this function returns an error status, the operation becomes inactive.
1762 *
1763 * \note When encrypting, applications should use psa_cipher_generate_iv()
1764 * instead of this function, unless implementing a protocol that requires
1765 * a non-random IV.
1766 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001767 * \param[in,out] operation Active cipher operation.
1768 * \param[in] iv Buffer containing the IV to use.
1769 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001770 *
1771 * \retval #PSA_SUCCESS
1772 * Success.
1773 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001774 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001775 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001776 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001777 * or the chosen algorithm does not use an IV.
1778 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1779 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1780 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001781 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001782 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001783psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1784 const unsigned char *iv,
1785 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001786
Gilles Peskinedcd14942018-07-12 00:30:52 +02001787/** Encrypt or decrypt a message fragment in an active cipher operation.
1788 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001789 * Before calling this function, you must:
1790 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1791 * The choice of setup function determines whether this function
1792 * encrypts or decrypts its input.
1793 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1794 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001795 *
1796 * If this function returns an error status, the operation becomes inactive.
1797 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001798 * \param[in,out] operation Active cipher operation.
1799 * \param[in] input Buffer containing the message fragment to
1800 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001801 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001802 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001803 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001804 * \param[out] output_length On success, the number of bytes
1805 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001806 *
1807 * \retval #PSA_SUCCESS
1808 * Success.
1809 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001810 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001811 * not set, or already completed).
1812 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1813 * The size of the \p output buffer is too small.
1814 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1815 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1816 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001817 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001818 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001819psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1820 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001821 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001822 unsigned char *output,
1823 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001824 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001825
Gilles Peskinedcd14942018-07-12 00:30:52 +02001826/** Finish encrypting or decrypting a message in a cipher operation.
1827 *
1828 * The application must call psa_cipher_encrypt_setup() or
1829 * psa_cipher_decrypt_setup() before calling this function. The choice
1830 * of setup function determines whether this function encrypts or
1831 * decrypts its input.
1832 *
1833 * This function finishes the encryption or decryption of the message
1834 * formed by concatenating the inputs passed to preceding calls to
1835 * psa_cipher_update().
1836 *
1837 * When this function returns, the operation becomes inactive.
1838 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001839 * \param[in,out] operation Active cipher operation.
1840 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001841 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001842 * \param[out] output_length On success, the number of bytes
1843 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001844 *
1845 * \retval #PSA_SUCCESS
1846 * Success.
1847 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001848 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001849 * not set, or already completed).
1850 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1851 * The size of the \p output buffer is too small.
1852 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1853 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1854 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001855 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001856 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001857psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001858 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001859 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001860 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001861
Gilles Peskinedcd14942018-07-12 00:30:52 +02001862/** Abort a cipher operation.
1863 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001864 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001865 * \p operation structure itself. Once aborted, the operation object
1866 * can be reused for another operation by calling
1867 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001868 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001869 * You may call this function any time after the operation object has
1870 * been initialized by any of the following methods:
1871 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1872 * whether it succeeds or not.
1873 * - Initializing the \c struct to all-bits-zero.
1874 * - Initializing the \c struct to logical zeros, e.g.
1875 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001876 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001877 * In particular, calling psa_cipher_abort() after the operation has been
1878 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1879 * is safe and has no effect.
1880 *
1881 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001882 *
1883 * \retval #PSA_SUCCESS
1884 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001885 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001886 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1887 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001888 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001889 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001890psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1891
1892/**@}*/
1893
Gilles Peskine3b555712018-03-03 21:27:57 +01001894/** \defgroup aead Authenticated encryption with associated data (AEAD)
1895 * @{
1896 */
1897
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001898/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001899 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001900 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001901 * \param alg The AEAD algorithm to compute
1902 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001903 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001904 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001905 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001906 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001907 * but not encrypted.
1908 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001909 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001910 * encrypted.
1911 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001912 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001913 * encrypted data. The additional data is not
1914 * part of this output. For algorithms where the
1915 * encrypted data and the authentication tag
1916 * are defined as separate outputs, the
1917 * authentication tag is appended to the
1918 * encrypted data.
1919 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1920 * This must be at least
1921 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1922 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001923 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01001924 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001925 *
Gilles Peskine28538492018-07-11 17:34:00 +02001926 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001927 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001928 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001929 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001930 * \retval #PSA_ERROR_NOT_PERMITTED
1931 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001932 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001933 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001934 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001935 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1936 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1937 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001938 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001939 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001940 * The library has not been previously initialized by psa_crypto_init().
1941 * It is implementation-dependent whether a failure to initialize
1942 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001943 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001944psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001945 psa_algorithm_t alg,
1946 const uint8_t *nonce,
1947 size_t nonce_length,
1948 const uint8_t *additional_data,
1949 size_t additional_data_length,
1950 const uint8_t *plaintext,
1951 size_t plaintext_length,
1952 uint8_t *ciphertext,
1953 size_t ciphertext_size,
1954 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001955
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001956/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001957 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001958 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001959 * \param alg The AEAD algorithm to compute
1960 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001961 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001962 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001963 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001964 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001965 * but not encrypted.
1966 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001967 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001968 * encrypted. For algorithms where the
1969 * encrypted data and the authentication tag
1970 * are defined as separate inputs, the buffer
1971 * must contain the encrypted data followed
1972 * by the authentication tag.
1973 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001974 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001975 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1976 * This must be at least
1977 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1978 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001979 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01001980 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001981 *
Gilles Peskine28538492018-07-11 17:34:00 +02001982 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001983 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001984 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001985 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001986 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001987 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001988 * \retval #PSA_ERROR_NOT_PERMITTED
1989 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001990 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001991 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001992 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001993 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1994 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1995 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001996 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001997 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001998 * The library has not been previously initialized by psa_crypto_init().
1999 * It is implementation-dependent whether a failure to initialize
2000 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002001 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002002psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002003 psa_algorithm_t alg,
2004 const uint8_t *nonce,
2005 size_t nonce_length,
2006 const uint8_t *additional_data,
2007 size_t additional_data_length,
2008 const uint8_t *ciphertext,
2009 size_t ciphertext_length,
2010 uint8_t *plaintext,
2011 size_t plaintext_size,
2012 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002013
Gilles Peskine30a9e412019-01-14 18:36:12 +01002014/** The type of the state data structure for multipart AEAD operations.
2015 *
2016 * Before calling any function on an AEAD operation object, the application
2017 * must initialize it by any of the following means:
2018 * - Set the structure to all-bits-zero, for example:
2019 * \code
2020 * psa_aead_operation_t operation;
2021 * memset(&operation, 0, sizeof(operation));
2022 * \endcode
2023 * - Initialize the structure to logical zero values, for example:
2024 * \code
2025 * psa_aead_operation_t operation = {0};
2026 * \endcode
2027 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2028 * for example:
2029 * \code
2030 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2031 * \endcode
2032 * - Assign the result of the function psa_aead_operation_init()
2033 * to the structure, for example:
2034 * \code
2035 * psa_aead_operation_t operation;
2036 * operation = psa_aead_operation_init();
2037 * \endcode
2038 *
2039 * This is an implementation-defined \c struct. Applications should not
2040 * make any assumptions about the content of this structure except
2041 * as directed by the documentation of a specific implementation. */
2042typedef struct psa_aead_operation_s psa_aead_operation_t;
2043
2044/** \def PSA_AEAD_OPERATION_INIT
2045 *
2046 * This macro returns a suitable initializer for an AEAD operation object of
2047 * type #psa_aead_operation_t.
2048 */
2049#ifdef __DOXYGEN_ONLY__
2050/* This is an example definition for documentation purposes.
2051 * Implementations should define a suitable value in `crypto_struct.h`.
2052 */
2053#define PSA_AEAD_OPERATION_INIT {0}
2054#endif
2055
2056/** Return an initial value for an AEAD operation object.
2057 */
2058static psa_aead_operation_t psa_aead_operation_init(void);
2059
2060/** Set the key for a multipart authenticated encryption operation.
2061 *
2062 * The sequence of operations to encrypt a message with authentication
2063 * is as follows:
2064 * -# Allocate an operation object which will be passed to all the functions
2065 * listed here.
2066 * -# Initialize the operation object with one of the methods described in the
2067 * documentation for #psa_aead_operation_t, e.g.
2068 * PSA_AEAD_OPERATION_INIT.
2069 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002070 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2071 * inputs to the subsequent calls to psa_aead_update_ad() and
2072 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2073 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002074 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2075 * generate or set the nonce. You should use
2076 * psa_aead_generate_nonce() unless the protocol you are implementing
2077 * requires a specific nonce value.
2078 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2079 * of the non-encrypted additional authenticated data each time.
2080 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002081 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002082 * -# Call psa_aead_finish().
2083 *
2084 * The application may call psa_aead_abort() at any time after the operation
2085 * has been initialized.
2086 *
2087 * After a successful call to psa_aead_encrypt_setup(), the application must
2088 * eventually terminate the operation. The following events terminate an
2089 * operation:
2090 * - A failed call to any of the \c psa_aead_xxx functions.
2091 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2092 *
2093 * \param[in,out] operation The operation object to set up. It must have
2094 * been initialized as per the documentation for
2095 * #psa_aead_operation_t and not yet in use.
2096 * \param handle Handle to the key to use for the operation.
2097 * It must remain valid until the operation
2098 * terminates.
2099 * \param alg The AEAD algorithm to compute
2100 * (\c PSA_ALG_XXX value such that
2101 * #PSA_ALG_IS_AEAD(\p alg) is true).
2102 *
2103 * \retval #PSA_SUCCESS
2104 * Success.
2105 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002106 * \retval #PSA_ERROR_NOT_PERMITTED
2107 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002108 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002109 * \retval #PSA_ERROR_NOT_SUPPORTED
2110 * \p alg is not supported or is not an AEAD algorithm.
2111 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2112 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2113 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002114 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002115 * \retval #PSA_ERROR_BAD_STATE
2116 * The library has not been previously initialized by psa_crypto_init().
2117 * It is implementation-dependent whether a failure to initialize
2118 * results in this error code.
2119 */
2120psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2121 psa_key_handle_t handle,
2122 psa_algorithm_t alg);
2123
2124/** Set the key for a multipart authenticated decryption operation.
2125 *
2126 * The sequence of operations to decrypt a message with authentication
2127 * is as follows:
2128 * -# Allocate an operation object which will be passed to all the functions
2129 * listed here.
2130 * -# Initialize the operation object with one of the methods described in the
2131 * documentation for #psa_aead_operation_t, e.g.
2132 * PSA_AEAD_OPERATION_INIT.
2133 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002134 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2135 * inputs to the subsequent calls to psa_aead_update_ad() and
2136 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2137 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002138 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2139 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2140 * of the non-encrypted additional authenticated data each time.
2141 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002142 * of the ciphertext to decrypt each time.
2143 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002144 *
2145 * The application may call psa_aead_abort() at any time after the operation
2146 * has been initialized.
2147 *
2148 * After a successful call to psa_aead_decrypt_setup(), the application must
2149 * eventually terminate the operation. The following events terminate an
2150 * operation:
2151 * - A failed call to any of the \c psa_aead_xxx functions.
2152 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2153 *
2154 * \param[in,out] operation The operation object to set up. It must have
2155 * been initialized as per the documentation for
2156 * #psa_aead_operation_t and not yet in use.
2157 * \param handle Handle to the key to use for the operation.
2158 * It must remain valid until the operation
2159 * terminates.
2160 * \param alg The AEAD algorithm to compute
2161 * (\c PSA_ALG_XXX value such that
2162 * #PSA_ALG_IS_AEAD(\p alg) is true).
2163 *
2164 * \retval #PSA_SUCCESS
2165 * Success.
2166 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002167 * \retval #PSA_ERROR_NOT_PERMITTED
2168 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002169 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002170 * \retval #PSA_ERROR_NOT_SUPPORTED
2171 * \p alg is not supported or is not an AEAD algorithm.
2172 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2173 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2174 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002175 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002176 * \retval #PSA_ERROR_BAD_STATE
2177 * The library has not been previously initialized by psa_crypto_init().
2178 * It is implementation-dependent whether a failure to initialize
2179 * results in this error code.
2180 */
2181psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2182 psa_key_handle_t handle,
2183 psa_algorithm_t alg);
2184
2185/** Generate a random nonce for an authenticated encryption operation.
2186 *
2187 * This function generates a random nonce for the authenticated encryption
2188 * operation with an appropriate size for the chosen algorithm, key type
2189 * and key size.
2190 *
2191 * The application must call psa_aead_encrypt_setup() before
2192 * calling this function.
2193 *
2194 * If this function returns an error status, the operation becomes inactive.
2195 *
2196 * \param[in,out] operation Active AEAD operation.
2197 * \param[out] nonce Buffer where the generated nonce is to be
2198 * written.
2199 * \param nonce_size Size of the \p nonce buffer in bytes.
2200 * \param[out] nonce_length On success, the number of bytes of the
2201 * generated nonce.
2202 *
2203 * \retval #PSA_SUCCESS
2204 * Success.
2205 * \retval #PSA_ERROR_BAD_STATE
2206 * The operation state is not valid (not set up, or nonce already set).
2207 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2208 * The size of the \p nonce buffer is too small.
2209 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2210 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2211 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002212 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002213 */
2214psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2215 unsigned char *nonce,
2216 size_t nonce_size,
2217 size_t *nonce_length);
2218
2219/** Set the nonce for an authenticated encryption or decryption operation.
2220 *
2221 * This function sets the nonce for the authenticated
2222 * encryption or decryption operation.
2223 *
2224 * The application must call psa_aead_encrypt_setup() before
2225 * calling this function.
2226 *
2227 * If this function returns an error status, the operation becomes inactive.
2228 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002229 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002230 * instead of this function, unless implementing a protocol that requires
2231 * a non-random IV.
2232 *
2233 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002234 * \param[in] nonce Buffer containing the nonce to use.
2235 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002236 *
2237 * \retval #PSA_SUCCESS
2238 * Success.
2239 * \retval #PSA_ERROR_BAD_STATE
2240 * The operation state is not valid (not set up, or nonce already set).
2241 * \retval #PSA_ERROR_INVALID_ARGUMENT
2242 * The size of \p nonce is not acceptable for the chosen algorithm.
2243 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2244 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2245 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002246 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002247 */
2248psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2249 const unsigned char *nonce,
2250 size_t nonce_length);
2251
Gilles Peskinebc59c852019-01-17 15:26:08 +01002252/** Declare the lengths of the message and additional data for AEAD.
2253 *
2254 * The application must call this function before calling
2255 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2256 * the operation requires it. If the algorithm does not require it,
2257 * calling this function is optional, but if this function is called
2258 * then the implementation must enforce the lengths.
2259 *
2260 * You may call this function before or after setting the nonce with
2261 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2262 *
2263 * - For #PSA_ALG_CCM, calling this function is required.
2264 * - For the other AEAD algorithms defined in this specification, calling
2265 * this function is not required.
2266 * - For vendor-defined algorithm, refer to the vendor documentation.
2267 *
2268 * \param[in,out] operation Active AEAD operation.
2269 * \param ad_length Size of the non-encrypted additional
2270 * authenticated data in bytes.
2271 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2272 *
2273 * \retval #PSA_SUCCESS
2274 * Success.
2275 * \retval #PSA_ERROR_BAD_STATE
2276 * The operation state is not valid (not set up, already completed,
2277 * or psa_aead_update_ad() or psa_aead_update() already called).
2278 * \retval #PSA_ERROR_INVALID_ARGUMENT
2279 * At least one of the lengths is not acceptable for the chosen
2280 * algorithm.
2281 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2282 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2283 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002284 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinebc59c852019-01-17 15:26:08 +01002285 */
2286psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2287 size_t ad_length,
2288 size_t plaintext_length);
2289
Gilles Peskine30a9e412019-01-14 18:36:12 +01002290/** Pass additional data to an active AEAD operation.
2291 *
2292 * Additional data is authenticated, but not encrypted.
2293 *
2294 * You may call this function multiple times to pass successive fragments
2295 * of the additional data. You may not call this function after passing
2296 * data to encrypt or decrypt with psa_aead_update().
2297 *
2298 * Before calling this function, you must:
2299 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2300 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2301 *
2302 * If this function returns an error status, the operation becomes inactive.
2303 *
2304 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2305 * there is no guarantee that the input is valid. Therefore, until
2306 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2307 * treat the input as untrusted and prepare to undo any action that
2308 * depends on the input if psa_aead_verify() returns an error status.
2309 *
2310 * \param[in,out] operation Active AEAD operation.
2311 * \param[in] input Buffer containing the fragment of
2312 * additional data.
2313 * \param input_length Size of the \p input buffer in bytes.
2314 *
2315 * \retval #PSA_SUCCESS
2316 * Success.
2317 * \retval #PSA_ERROR_BAD_STATE
2318 * The operation state is not valid (not set up, nonce not set,
2319 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002320 * \retval #PSA_ERROR_INVALID_ARGUMENT
2321 * The total input length overflows the additional data length that
2322 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002323 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2324 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2325 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002326 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002327 */
2328psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2329 const uint8_t *input,
2330 size_t input_length);
2331
2332/** Encrypt or decrypt a message fragment in an active AEAD operation.
2333 *
2334 * Before calling this function, you must:
2335 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2336 * The choice of setup function determines whether this function
2337 * encrypts or decrypts its input.
2338 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2339 * 3. Call psa_aead_update_ad() to pass all the additional data.
2340 *
2341 * If this function returns an error status, the operation becomes inactive.
2342 *
2343 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2344 * there is no guarantee that the input is valid. Therefore, until
2345 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2346 * - Do not use the output in any way other than storing it in a
2347 * confidential location. If you take any action that depends
2348 * on the tentative decrypted data, this action will need to be
2349 * undone if the input turns out not to be valid. Furthermore,
2350 * if an adversary can observe that this action took place
2351 * (for example through timing), they may be able to use this
2352 * fact as an oracle to decrypt any message encrypted with the
2353 * same key.
2354 * - In particular, do not copy the output anywhere but to a
2355 * memory or storage space that you have exclusive access to.
2356 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002357 * This function does not require the input to be aligned to any
2358 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002359 * a whole block at a time, it must consume all the input provided, but
2360 * it may delay the end of the corresponding output until a subsequent
2361 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2362 * provides sufficient input. The amount of data that can be delayed
2363 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002364 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002365 * \param[in,out] operation Active AEAD operation.
2366 * \param[in] input Buffer containing the message fragment to
2367 * encrypt or decrypt.
2368 * \param input_length Size of the \p input buffer in bytes.
2369 * \param[out] output Buffer where the output is to be written.
2370 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002371 * This must be at least
2372 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2373 * \p input_length) where \c alg is the
2374 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002375 * \param[out] output_length On success, the number of bytes
2376 * that make up the returned output.
2377 *
2378 * \retval #PSA_SUCCESS
2379 * Success.
2380 * \retval #PSA_ERROR_BAD_STATE
2381 * The operation state is not valid (not set up, nonce not set
2382 * or already completed).
2383 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2384 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002385 * You can determine a sufficient buffer size by calling
2386 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2387 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002388 * \retval #PSA_ERROR_INVALID_ARGUMENT
2389 * The total length of input to psa_aead_update_ad() so far is
2390 * less than the additional data length that was previously
2391 * specified with psa_aead_set_lengths().
2392 * \retval #PSA_ERROR_INVALID_ARGUMENT
2393 * The total input length overflows the plaintext length that
2394 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002395 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2396 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2397 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002398 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002399 */
2400psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2401 const uint8_t *input,
2402 size_t input_length,
2403 unsigned char *output,
2404 size_t output_size,
2405 size_t *output_length);
2406
2407/** Finish encrypting a message in an AEAD operation.
2408 *
2409 * The operation must have been set up with psa_aead_encrypt_setup().
2410 *
2411 * This function finishes the authentication of the additional data
2412 * formed by concatenating the inputs passed to preceding calls to
2413 * psa_aead_update_ad() with the plaintext formed by concatenating the
2414 * inputs passed to preceding calls to psa_aead_update().
2415 *
2416 * This function has two output buffers:
2417 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002418 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002419 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002420 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002421 * that the operation performs.
2422 *
2423 * When this function returns, the operation becomes inactive.
2424 *
2425 * \param[in,out] operation Active AEAD operation.
2426 * \param[out] ciphertext Buffer where the last part of the ciphertext
2427 * is to be written.
2428 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002429 * This must be at least
2430 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2431 * \c alg is the algorithm that is being
2432 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002433 * \param[out] ciphertext_length On success, the number of bytes of
2434 * returned ciphertext.
2435 * \param[out] tag Buffer where the authentication tag is
2436 * to be written.
2437 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002438 * This must be at least
2439 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2440 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002441 * \param[out] tag_length On success, the number of bytes
2442 * that make up the returned tag.
2443 *
2444 * \retval #PSA_SUCCESS
2445 * Success.
2446 * \retval #PSA_ERROR_BAD_STATE
2447 * The operation state is not valid (not set up, nonce not set,
2448 * decryption, or already completed).
2449 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002450 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002451 * You can determine a sufficient buffer size for \p ciphertext by
2452 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2453 * where \c alg is the algorithm that is being calculated.
2454 * You can determine a sufficient buffer size for \p tag by
2455 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002456 * \retval #PSA_ERROR_INVALID_ARGUMENT
2457 * The total length of input to psa_aead_update_ad() so far is
2458 * less than the additional data length that was previously
2459 * specified with psa_aead_set_lengths().
2460 * \retval #PSA_ERROR_INVALID_ARGUMENT
2461 * The total length of input to psa_aead_update() so far is
2462 * less than the plaintext length that was previously
2463 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002464 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2465 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2466 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002467 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002468 */
2469psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002470 uint8_t *ciphertext,
2471 size_t ciphertext_size,
2472 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002473 uint8_t *tag,
2474 size_t tag_size,
2475 size_t *tag_length);
2476
2477/** Finish authenticating and decrypting a message in an AEAD operation.
2478 *
2479 * The operation must have been set up with psa_aead_decrypt_setup().
2480 *
2481 * This function finishes the authentication of the additional data
2482 * formed by concatenating the inputs passed to preceding calls to
2483 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2484 * inputs passed to preceding calls to psa_aead_update().
2485 *
2486 * When this function returns, the operation becomes inactive.
2487 *
2488 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002489 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002490 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002491 * from previous calls to psa_aead_update()
2492 * that could not be processed until the end
2493 * of the input.
2494 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002495 * This must be at least
2496 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2497 * \c alg is the algorithm that is being
2498 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002499 * \param[out] plaintext_length On success, the number of bytes of
2500 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002501 * \param[in] tag Buffer containing the authentication tag.
2502 * \param tag_length Size of the \p tag buffer in bytes.
2503 *
2504 * \retval #PSA_SUCCESS
2505 * Success.
2506 * \retval #PSA_ERROR_BAD_STATE
2507 * The operation state is not valid (not set up, nonce not set,
2508 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002509 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2510 * The size of the \p plaintext buffer is too small.
2511 * You can determine a sufficient buffer size for \p plaintext by
2512 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2513 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002514 * \retval #PSA_ERROR_INVALID_ARGUMENT
2515 * The total length of input to psa_aead_update_ad() so far is
2516 * less than the additional data length that was previously
2517 * specified with psa_aead_set_lengths().
2518 * \retval #PSA_ERROR_INVALID_ARGUMENT
2519 * The total length of input to psa_aead_update() so far is
2520 * less than the plaintext length that was previously
2521 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002522 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2523 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2524 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002525 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002526 */
2527psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002528 uint8_t *plaintext,
2529 size_t plaintext_size,
2530 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002531 const uint8_t *tag,
2532 size_t tag_length);
2533
2534/** Abort an AEAD operation.
2535 *
2536 * Aborting an operation frees all associated resources except for the
2537 * \p operation structure itself. Once aborted, the operation object
2538 * can be reused for another operation by calling
2539 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2540 *
2541 * You may call this function any time after the operation object has
2542 * been initialized by any of the following methods:
2543 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2544 * whether it succeeds or not.
2545 * - Initializing the \c struct to all-bits-zero.
2546 * - Initializing the \c struct to logical zeros, e.g.
2547 * `psa_aead_operation_t operation = {0}`.
2548 *
2549 * In particular, calling psa_aead_abort() after the operation has been
2550 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2551 * is safe and has no effect.
2552 *
2553 * \param[in,out] operation Initialized AEAD operation.
2554 *
2555 * \retval #PSA_SUCCESS
2556 * \retval #PSA_ERROR_BAD_STATE
2557 * \p operation is not an active AEAD operation.
2558 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2559 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002560 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002561 */
2562psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2563
Gilles Peskine3b555712018-03-03 21:27:57 +01002564/**@}*/
2565
Gilles Peskine20035e32018-02-03 22:44:14 +01002566/** \defgroup asymmetric Asymmetric cryptography
2567 * @{
2568 */
2569
2570/**
2571 * \brief Sign a hash or short message with a private key.
2572 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002573 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002574 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002575 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2576 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2577 * to determine the hash algorithm to use.
2578 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002579 * \param handle Handle to the key to use for the operation.
2580 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002581 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002582 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002583 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002584 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002585 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002586 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002587 * \param[out] signature_length On success, the number of bytes
2588 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002589 *
Gilles Peskine28538492018-07-11 17:34:00 +02002590 * \retval #PSA_SUCCESS
2591 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002592 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002593 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002594 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002595 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002596 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002597 * \retval #PSA_ERROR_NOT_SUPPORTED
2598 * \retval #PSA_ERROR_INVALID_ARGUMENT
2599 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2600 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2601 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002602 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002603 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002604 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002605 * The library has not been previously initialized by psa_crypto_init().
2606 * It is implementation-dependent whether a failure to initialize
2607 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002608 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002609psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002610 psa_algorithm_t alg,
2611 const uint8_t *hash,
2612 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002613 uint8_t *signature,
2614 size_t signature_size,
2615 size_t *signature_length);
2616
2617/**
2618 * \brief Verify the signature a hash or short message using a public key.
2619 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002620 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002621 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002622 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2623 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2624 * to determine the hash algorithm to use.
2625 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002626 * \param handle Handle to the key to use for the operation.
2627 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002628 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002629 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002630 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002631 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002632 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002633 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002634 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002635 *
Gilles Peskine28538492018-07-11 17:34:00 +02002636 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002637 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002638 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002639 * The calculation was perfomed successfully, but the passed
2640 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002641 * \retval #PSA_ERROR_NOT_SUPPORTED
2642 * \retval #PSA_ERROR_INVALID_ARGUMENT
2643 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2644 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2645 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002646 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002647 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002648 * The library has not been previously initialized by psa_crypto_init().
2649 * It is implementation-dependent whether a failure to initialize
2650 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002651 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002652psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002653 psa_algorithm_t alg,
2654 const uint8_t *hash,
2655 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002656 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002657 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002658
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002659/**
2660 * \brief Encrypt a short message with a public key.
2661 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002662 * \param handle Handle to the key to use for the operation.
2663 * It must be a public key or an asymmetric
2664 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002665 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002666 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002667 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002668 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002669 * \param[in] salt A salt or label, if supported by the
2670 * encryption algorithm.
2671 * If the algorithm does not support a
2672 * salt, pass \c NULL.
2673 * If the algorithm supports an optional
2674 * salt and you do not want to pass a salt,
2675 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002676 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002677 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2678 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002679 * \param salt_length Size of the \p salt buffer in bytes.
2680 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002681 * \param[out] output Buffer where the encrypted message is to
2682 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002683 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002684 * \param[out] output_length On success, the number of bytes
2685 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002686 *
Gilles Peskine28538492018-07-11 17:34:00 +02002687 * \retval #PSA_SUCCESS
2688 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002689 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002690 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002691 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002692 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002693 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002694 * \retval #PSA_ERROR_NOT_SUPPORTED
2695 * \retval #PSA_ERROR_INVALID_ARGUMENT
2696 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2697 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2698 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002699 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002700 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002701 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002702 * The library has not been previously initialized by psa_crypto_init().
2703 * It is implementation-dependent whether a failure to initialize
2704 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002705 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002706psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002707 psa_algorithm_t alg,
2708 const uint8_t *input,
2709 size_t input_length,
2710 const uint8_t *salt,
2711 size_t salt_length,
2712 uint8_t *output,
2713 size_t output_size,
2714 size_t *output_length);
2715
2716/**
2717 * \brief Decrypt a short message with a private key.
2718 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002719 * \param handle Handle to the key to use for the operation.
2720 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002721 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002722 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002723 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002724 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002725 * \param[in] salt A salt or label, if supported by the
2726 * encryption algorithm.
2727 * If the algorithm does not support a
2728 * salt, pass \c NULL.
2729 * If the algorithm supports an optional
2730 * salt and you do not want to pass a salt,
2731 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002732 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002733 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2734 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002735 * \param salt_length Size of the \p salt buffer in bytes.
2736 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002737 * \param[out] output Buffer where the decrypted message is to
2738 * be written.
2739 * \param output_size Size of the \c output buffer in bytes.
2740 * \param[out] output_length On success, the number of bytes
2741 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002742 *
Gilles Peskine28538492018-07-11 17:34:00 +02002743 * \retval #PSA_SUCCESS
2744 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002745 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002746 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002747 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002748 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002749 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002750 * \retval #PSA_ERROR_NOT_SUPPORTED
2751 * \retval #PSA_ERROR_INVALID_ARGUMENT
2752 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2753 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2754 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002755 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002756 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2757 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002758 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002759 * The library has not been previously initialized by psa_crypto_init().
2760 * It is implementation-dependent whether a failure to initialize
2761 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002762 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002763psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002764 psa_algorithm_t alg,
2765 const uint8_t *input,
2766 size_t input_length,
2767 const uint8_t *salt,
2768 size_t salt_length,
2769 uint8_t *output,
2770 size_t output_size,
2771 size_t *output_length);
2772
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002773/**@}*/
2774
Gilles Peskine35675b62019-05-16 17:26:11 +02002775/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002776 * @{
2777 */
2778
Gilles Peskine35675b62019-05-16 17:26:11 +02002779/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002780 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002781 * Before calling any function on a key derivation operation object, the
2782 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002783 * - Set the structure to all-bits-zero, for example:
2784 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002785 * psa_key_derivation_operation_t operation;
2786 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002787 * \endcode
2788 * - Initialize the structure to logical zero values, for example:
2789 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002790 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002791 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002792 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002793 * for example:
2794 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002795 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002796 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002797 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002798 * to the structure, for example:
2799 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002800 * psa_key_derivation_operation_t operation;
2801 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002802 * \endcode
2803 *
2804 * This is an implementation-defined \c struct. Applications should not
2805 * make any assumptions about the content of this structure except
2806 * as directed by the documentation of a specific implementation.
2807 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02002808typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002809
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002810/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002811 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002812 * This macro returns a suitable initializer for a key derivation operation
2813 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002814 */
2815#ifdef __DOXYGEN_ONLY__
2816/* This is an example definition for documentation purposes.
2817 * Implementations should define a suitable value in `crypto_struct.h`.
2818 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002819#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02002820#endif
2821
Gilles Peskine35675b62019-05-16 17:26:11 +02002822/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002823 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002824static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002825
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002826/** Set up a key derivation operation.
2827 *
2828 * A key derivation algorithm takes some inputs and uses them to generate
2829 * a byte stream in a deterministic way.
2830 * This byte stream can be used to produce keys and other
2831 * cryptographic material.
2832 *
2833 * To derive a key:
2834 * - Start with an initialized object of type #psa_key_derivation_operation_t.
2835 * - Call psa_key_derivation_setup() to select the algorithm.
2836 * - Provide the inputs for the key derivation by calling
2837 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2838 * as appropriate. Which inputs are needed, in what order, and whether
2839 * they may be keys and if so of what type depends on the algorithm.
2840 * - Optionally set the operation's maximum capacity with
2841 * psa_key_derivation_set_capacity(). You may do this before, in the middle
2842 * of or after providing inputs. For some algorithms, this step is mandatory
2843 * because the output depends on the maximum capacity.
2844 * - To derive a key, call psa_key_derivation_output_key().
2845 * To derive a byte string for a different purpose, call
2846 * - psa_key_derivation_output_bytes().
2847 * Successive calls to these functions use successive output bytes
2848 * calculated by the key derivation algorithm.
2849 * - Clean up the key derivation operation object with
2850 * psa_key_derivation_abort().
2851 *
2852 * \param[in,out] operation The key derivation operation object
2853 * to set up. It must
2854 * have been initialized but not set up yet.
2855 * \param alg The key derivation algorithm to compute
2856 * (\c PSA_ALG_XXX value such that
2857 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2858 *
2859 * \retval #PSA_SUCCESS
2860 * Success.
2861 * \retval #PSA_ERROR_INVALID_ARGUMENT
2862 * \c alg is not a key derivation algorithm.
2863 * \retval #PSA_ERROR_NOT_SUPPORTED
2864 * \c alg is not supported or is not a key derivation algorithm.
2865 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2866 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2867 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002868 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002869 * \retval #PSA_ERROR_BAD_STATE
2870 */
2871psa_status_t psa_key_derivation_setup(
2872 psa_key_derivation_operation_t *operation,
2873 psa_algorithm_t alg);
2874
Gilles Peskine35675b62019-05-16 17:26:11 +02002875/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002876 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002877 * The capacity of a key derivation is the maximum number of bytes that it can
2878 * return. When you get *N* bytes of output from a key derivation operation,
2879 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002880 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002881 * \param[in] operation The operation to query.
2882 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002883 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002884 * \retval #PSA_SUCCESS
2885 * \retval #PSA_ERROR_BAD_STATE
2886 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002887 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002888psa_status_t psa_key_derivation_get_capacity(
2889 const psa_key_derivation_operation_t *operation,
2890 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002891
Gilles Peskine35675b62019-05-16 17:26:11 +02002892/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002893 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002894 * The capacity of a key derivation operation is the maximum number of bytes
2895 * that the key derivation operation can return from this point onwards.
2896 *
2897 * \param[in,out] operation The key derivation operation object to modify.
2898 * \param capacity The new capacity of the operation.
2899 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002900 * current capacity.
2901 *
2902 * \retval #PSA_SUCCESS
2903 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02002904 * \p capacity is larger than the operation's current capacity.
2905 * In this case, the operation object remains valid and its capacity
2906 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002907 * \retval #PSA_ERROR_BAD_STATE
2908 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2909 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002910psa_status_t psa_key_derivation_set_capacity(
2911 psa_key_derivation_operation_t *operation,
2912 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002913
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002914/** Use the maximum possible capacity for a key derivation operation.
2915 *
2916 * Use this value as the capacity argument when setting up a key derivation
2917 * to indicate that the operation should have the maximum possible capacity.
2918 * The value of the maximum possible capacity depends on the key derivation
2919 * algorithm.
2920 */
2921#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
2922
2923/** Provide an input for key derivation or key agreement.
2924 *
2925 * Which inputs are required and in what order depends on the algorithm.
2926 * Refer to the documentation of each key derivation or key agreement
2927 * algorithm for information.
2928 *
2929 * This function passes direct inputs. Some inputs must be passed as keys
2930 * using psa_key_derivation_input_key() instead of this function. Refer to
2931 * the documentation of individual step types for information.
2932 *
2933 * \param[in,out] operation The key derivation operation object to use.
2934 * It must have been set up with
2935 * psa_key_derivation_setup() and must not
2936 * have produced any output yet.
2937 * \param step Which step the input data is for.
2938 * \param[in] data Input data to use.
2939 * \param data_length Size of the \p data buffer in bytes.
2940 *
2941 * \retval #PSA_SUCCESS
2942 * Success.
2943 * \retval #PSA_ERROR_INVALID_ARGUMENT
2944 * \c step is not compatible with the operation's algorithm.
2945 * \retval #PSA_ERROR_INVALID_ARGUMENT
2946 * \c step does not allow direct inputs.
2947 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2948 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2949 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002950 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002951 * \retval #PSA_ERROR_BAD_STATE
2952 * The value of \p step is not valid given the state of \p operation.
2953 * \retval #PSA_ERROR_BAD_STATE
2954 * The library has not been previously initialized by psa_crypto_init().
2955 * It is implementation-dependent whether a failure to initialize
2956 * results in this error code.
2957 */
2958psa_status_t psa_key_derivation_input_bytes(
2959 psa_key_derivation_operation_t *operation,
2960 psa_key_derivation_step_t step,
2961 const uint8_t *data,
2962 size_t data_length);
2963
2964/** Provide an input for key derivation in the form of a key.
2965 *
2966 * Which inputs are required and in what order depends on the algorithm.
2967 * Refer to the documentation of each key derivation or key agreement
2968 * algorithm for information.
2969 *
2970 * This function passes key inputs. Some inputs must be passed as keys
2971 * of the appropriate type using this function, while others must be
2972 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
2973 * the documentation of individual step types for information.
2974 *
2975 * \param[in,out] operation The key derivation operation object to use.
2976 * It must have been set up with
2977 * psa_key_derivation_setup() and must not
2978 * have produced any output yet.
2979 * \param step Which step the input data is for.
2980 * \param handle Handle to the key. It must have an
2981 * appropriate type for \p step and must
2982 * allow the usage #PSA_KEY_USAGE_DERIVE.
2983 *
2984 * \retval #PSA_SUCCESS
2985 * Success.
2986 * \retval #PSA_ERROR_INVALID_HANDLE
2987 * \retval #PSA_ERROR_DOES_NOT_EXIST
2988 * \retval #PSA_ERROR_NOT_PERMITTED
2989 * \retval #PSA_ERROR_INVALID_ARGUMENT
2990 * \c step is not compatible with the operation's algorithm.
2991 * \retval #PSA_ERROR_INVALID_ARGUMENT
2992 * \c step does not allow key inputs.
2993 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2994 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2995 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002996 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002997 * \retval #PSA_ERROR_BAD_STATE
2998 * The value of \p step is not valid given the state of \p operation.
2999 * \retval #PSA_ERROR_BAD_STATE
3000 * The library has not been previously initialized by psa_crypto_init().
3001 * It is implementation-dependent whether a failure to initialize
3002 * results in this error code.
3003 */
3004psa_status_t psa_key_derivation_input_key(
3005 psa_key_derivation_operation_t *operation,
3006 psa_key_derivation_step_t step,
3007 psa_key_handle_t handle);
3008
3009/** Perform a key agreement and use the shared secret as input to a key
3010 * derivation.
3011 *
3012 * A key agreement algorithm takes two inputs: a private key \p private_key
3013 * a public key \p peer_key.
3014 * The result of this function is passed as input to a key derivation.
3015 * The output of this key derivation can be extracted by reading from the
3016 * resulting operation to produce keys and other cryptographic material.
3017 *
3018 * \param[in,out] operation The key derivation operation object to use.
3019 * It must have been set up with
3020 * psa_key_derivation_setup() with a
3021 * key agreement and derivation algorithm
3022 * \c alg (\c PSA_ALG_XXX value such that
3023 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3024 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3025 * is false).
3026 * The operation must be ready for an
3027 * input of the type given by \p step.
3028 * \param step Which step the input data is for.
3029 * \param private_key Handle to the private key to use.
3030 * \param[in] peer_key Public key of the peer. The peer key must be in the
3031 * same format that psa_import_key() accepts for the
3032 * public key type corresponding to the type of
3033 * private_key. That is, this function performs the
3034 * equivalent of
3035 * #psa_import_key(...,
3036 * `peer_key`, `peer_key_length`) where
3037 * with key attributes indicating the public key
3038 * type corresponding to the type of `private_key`.
3039 * For example, for EC keys, this means that peer_key
3040 * is interpreted as a point on the curve that the
3041 * private key is on. The standard formats for public
3042 * keys are documented in the documentation of
3043 * psa_export_public_key().
3044 * \param peer_key_length Size of \p peer_key in bytes.
3045 *
3046 * \retval #PSA_SUCCESS
3047 * Success.
3048 * \retval #PSA_ERROR_INVALID_HANDLE
3049 * \retval #PSA_ERROR_DOES_NOT_EXIST
3050 * \retval #PSA_ERROR_NOT_PERMITTED
3051 * \retval #PSA_ERROR_INVALID_ARGUMENT
3052 * \c private_key is not compatible with \c alg,
3053 * or \p peer_key is not valid for \c alg or not compatible with
3054 * \c private_key.
3055 * \retval #PSA_ERROR_NOT_SUPPORTED
3056 * \c alg is not supported or is not a key derivation algorithm.
3057 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3058 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3059 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003060 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003061 */
3062psa_status_t psa_key_derivation_key_agreement(
3063 psa_key_derivation_operation_t *operation,
3064 psa_key_derivation_step_t step,
3065 psa_key_handle_t private_key,
3066 const uint8_t *peer_key,
3067 size_t peer_key_length);
3068
Gilles Peskine35675b62019-05-16 17:26:11 +02003069/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003070 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003071 * This function calculates output bytes from a key derivation algorithm and
3072 * return those bytes.
3073 * If you view the key derivation's output as a stream of bytes, this
3074 * function destructively reads the requested number of bytes from the
3075 * stream.
3076 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003077 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003078 * \param[in,out] operation The key derivation operation object to read from.
3079 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003080 * \param output_length Number of bytes to output.
3081 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003082 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003083 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003084 * The operation's capacity was less than
3085 * \p output_length bytes. Note that in this case,
3086 * no output is written to the output buffer.
3087 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003088 * subsequent calls to this function will not
3089 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003090 * \retval #PSA_ERROR_BAD_STATE
3091 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3092 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3093 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003094 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003095 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003096psa_status_t psa_key_derivation_output_bytes(
3097 psa_key_derivation_operation_t *operation,
3098 uint8_t *output,
3099 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003100
Gilles Peskine35675b62019-05-16 17:26:11 +02003101/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003102 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003103 * This function calculates output bytes from a key derivation algorithm
3104 * and uses those bytes to generate a key deterministically.
3105 * If you view the key derivation's output as a stream of bytes, this
3106 * function destructively reads as many bytes as required from the
3107 * stream.
3108 * The operation's capacity decreases by the number of bytes read.
3109 *
3110 * How much output is produced and consumed from the operation, and how
3111 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003112 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003113 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003114 * of a given size, this function is functionally equivalent to
3115 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003116 * and passing the resulting output to #psa_import_key.
3117 * However, this function has a security benefit:
3118 * if the implementation provides an isolation boundary then
3119 * the key material is not exposed outside the isolation boundary.
3120 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003121 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003122 * The following key types defined in this specification follow this scheme:
3123 *
3124 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003125 * - #PSA_KEY_TYPE_ARC4;
3126 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003127 * - #PSA_KEY_TYPE_DERIVE;
3128 * - #PSA_KEY_TYPE_HMAC.
3129 *
3130 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003131 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003132 * Montgomery curve), this function always draws a byte string whose
3133 * length is determined by the curve, and sets the mandatory bits
3134 * accordingly. That is:
3135 *
3136 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3137 * and process it as specified in RFC 7748 &sect;5.
3138 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3139 * and process it as specified in RFC 7748 &sect;5.
3140 *
3141 * - For key types for which the key is represented by a single sequence of
3142 * \p bits bits with constraints as to which bit sequences are acceptable,
3143 * this function draws a byte string of length (\p bits / 8) bytes rounded
3144 * up to the nearest whole number of bytes. If the resulting byte string
3145 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3146 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003147 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003148 * for the output produced by psa_export_key().
3149 * The following key types defined in this specification follow this scheme:
3150 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003151 * - #PSA_KEY_TYPE_DES.
3152 * Force-set the parity bits, but discard forbidden weak keys.
3153 * For 2-key and 3-key triple-DES, the three keys are generated
3154 * successively (for example, for 3-key triple-DES,
3155 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3156 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003157 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003158 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003159 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003160 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003161 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003162 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003163 * Weierstrass curve).
3164 * For these key types, interpret the byte string as integer
3165 * in big-endian order. Discard it if it is not in the range
3166 * [0, *N* - 2] where *N* is the boundary of the private key domain
3167 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003168 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003169 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003170 * This method allows compliance to NIST standards, specifically
3171 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003172 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3173 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3174 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3175 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003176 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003177 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003178 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003179 * implementation-defined.
3180 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003181 * In all cases, the data that is read is discarded from the operation.
3182 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003183 *
Gilles Peskine20628592019-04-19 19:29:50 +02003184 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003185 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003186 * \param[out] handle On success, a handle to the newly created key.
3187 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003188 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003189 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003190 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003191 * If the key is persistent, the key material and the key's metadata
3192 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003193 * \retval #PSA_ERROR_ALREADY_EXISTS
3194 * This is an attempt to create a persistent key, and there is
3195 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003196 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003197 * There was not enough data to create the desired key.
3198 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003199 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003200 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003201 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003202 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003203 * implementation in general or in this particular location.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003204 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003205 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3206 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3207 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3208 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003209 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003210 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003211 * The library has not been previously initialized by psa_crypto_init().
3212 * It is implementation-dependent whether a failure to initialize
3213 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003214 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003215psa_status_t psa_key_derivation_output_key(
3216 const psa_key_attributes_t *attributes,
3217 psa_key_derivation_operation_t *operation,
3218 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003219
Gilles Peskine35675b62019-05-16 17:26:11 +02003220/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003221 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003222 * Once a key derivation operation has been aborted, its capacity is zero.
3223 * Aborting an operation frees all associated resources except for the
3224 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003225 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003226 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003227 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003228 * psa_key_derivation_operation_init() or a zero value. In particular,
3229 * it is valid to call psa_key_derivation_abort() twice, or to call
3230 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003231 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003232 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003233 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003234 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003235 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003236 * \retval #PSA_SUCCESS
3237 * \retval #PSA_ERROR_BAD_STATE
3238 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3239 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003240 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003241 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003242psa_status_t psa_key_derivation_abort(
3243 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003244
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003245/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003246 *
3247 * \warning The raw result of a key agreement algorithm such as finite-field
3248 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3249 * not be used directly as key material. It should instead be passed as
3250 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003251 * a key derivation, use psa_key_derivation_key_agreement() and other
3252 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003253 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003254 * \param alg The key agreement algorithm to compute
3255 * (\c PSA_ALG_XXX value such that
3256 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3257 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003258 * \param private_key Handle to the private key to use.
3259 * \param[in] peer_key Public key of the peer. It must be
3260 * in the same format that psa_import_key()
3261 * accepts. The standard formats for public
3262 * keys are documented in the documentation
3263 * of psa_export_public_key().
3264 * \param peer_key_length Size of \p peer_key in bytes.
3265 * \param[out] output Buffer where the decrypted message is to
3266 * be written.
3267 * \param output_size Size of the \c output buffer in bytes.
3268 * \param[out] output_length On success, the number of bytes
3269 * that make up the returned output.
3270 *
3271 * \retval #PSA_SUCCESS
3272 * Success.
3273 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003274 * \retval #PSA_ERROR_NOT_PERMITTED
3275 * \retval #PSA_ERROR_INVALID_ARGUMENT
3276 * \p alg is not a key agreement algorithm
3277 * \retval #PSA_ERROR_INVALID_ARGUMENT
3278 * \p private_key is not compatible with \p alg,
3279 * or \p peer_key is not valid for \p alg or not compatible with
3280 * \p private_key.
3281 * \retval #PSA_ERROR_NOT_SUPPORTED
3282 * \p alg is not a supported key agreement algorithm.
3283 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3284 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3285 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003286 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine769c7a62019-01-18 16:42:29 +01003287 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003288psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3289 psa_key_handle_t private_key,
3290 const uint8_t *peer_key,
3291 size_t peer_key_length,
3292 uint8_t *output,
3293 size_t output_size,
3294 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003295
Gilles Peskineea0fb492018-07-12 17:17:20 +02003296/**@}*/
3297
Gilles Peskineedd76872018-07-20 17:42:05 +02003298/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003299 * @{
3300 */
3301
3302/**
3303 * \brief Generate random bytes.
3304 *
3305 * \warning This function **can** fail! Callers MUST check the return status
3306 * and MUST NOT use the content of the output buffer if the return
3307 * status is not #PSA_SUCCESS.
3308 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003309 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003310 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003311 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003312 * \param output_size Number of bytes to generate and output.
3313 *
Gilles Peskine28538492018-07-11 17:34:00 +02003314 * \retval #PSA_SUCCESS
3315 * \retval #PSA_ERROR_NOT_SUPPORTED
3316 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3317 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3318 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003319 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003320 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003321 * The library has not been previously initialized by psa_crypto_init().
3322 * It is implementation-dependent whether a failure to initialize
3323 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003324 */
3325psa_status_t psa_generate_random(uint8_t *output,
3326 size_t output_size);
3327
3328/**
3329 * \brief Generate a key or key pair.
3330 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003331 * The key is generated randomly.
3332 * Its location, policy, type and size are taken from \p attributes.
3333 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003334 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003335 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003336 * the public exponent is 65537.
3337 * The modulus is a product of two probabilistic primes
3338 * between 2^{n-1} and 2^n where n is the bit size specified in the
3339 * attributes.
3340 *
Gilles Peskine20628592019-04-19 19:29:50 +02003341 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003342 * \param[out] handle On success, a handle to the newly created key.
3343 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003344 *
Gilles Peskine28538492018-07-11 17:34:00 +02003345 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003346 * Success.
3347 * If the key is persistent, the key material and the key's metadata
3348 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003349 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003350 * This is an attempt to create a persistent key, and there is
3351 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003352 * \retval #PSA_ERROR_NOT_SUPPORTED
3353 * \retval #PSA_ERROR_INVALID_ARGUMENT
3354 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3355 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3356 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3357 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003358 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003359 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003360 * The library has not been previously initialized by psa_crypto_init().
3361 * It is implementation-dependent whether a failure to initialize
3362 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003363 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003364psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003365 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003366
3367/**@}*/
3368
Gilles Peskinee59236f2018-01-27 23:32:46 +01003369#ifdef __cplusplus
3370}
3371#endif
3372
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003373/* The file "crypto_sizes.h" contains definitions for size calculation
3374 * macros whose definitions are implementation-specific. */
3375#include "crypto_sizes.h"
3376
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003377/* The file "crypto_struct.h" contains definitions for
3378 * implementation-specific structs that are declared above. */
3379#include "crypto_struct.h"
3380
3381/* The file "crypto_extra.h" contains vendor-specific definitions. This
3382 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003383#include "crypto_extra.h"
3384
3385#endif /* PSA_CRYPTO_H */