blob: 0d0de2e0a556d07d583fdbbcaf405ca8bcd8869d [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
89 * \retval #PSA_ERROR_TAMPERING_DETECTED
90 * \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 Peskine87a5e562019-04-17 12:28:25 +020096/** \defgroup attributes Key attributes
97 * @{
98 */
99
100/** The type of a structure containing key attributes.
101 *
102 * This is an opaque structure that can represent the metadata of a key
Gilles Peskine9c640f92019-04-28 11:36:21 +0200103 * object. Metadata that can be stored in attributes includes:
104 * - The location of the key in storage, indicated by its key identifier
105 * and its lifetime.
106 * - The key's policy, comprising usage flags and a specification of
107 * the permitted algorithm(s).
108 * - Information about the key itself: the key type, the key size, and
109 * for some key type additional domain parameters.
110 * - Implementations may define additional attributes.
Gilles Peskine87a5e562019-04-17 12:28:25 +0200111 *
112 * The actual key material is not considered an attribute of a key.
113 * Key attributes do not contain information that is generally considered
114 * highly confidential.
Gilles Peskine20628592019-04-19 19:29:50 +0200115 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200116 * An attribute structure can be a simple data structure where each function
117 * `psa_set_key_xxx` sets a field and the corresponding function
118 * `psa_get_key_xxx` retrieves the value of the corresponding field.
119 * However, implementations may report values that are equivalent to the
120 * original one, but have a different encoding. For example, an
121 * implementation may use a more compact representation for types where
122 * many bit-patterns are invalid or not supported, and store all values
123 * that it does not support as a special marker value. In such an
124 * implementation, after setting an invalid value, the corresponding
125 * get function returns an invalid value which may not be the one that
126 * was originally stored.
127 *
128 * An attribute structure may contain references to auxiliary resources,
129 * for example pointers to allocated memory or indirect references to
130 * pre-calculated values. In order to free such resources, the application
131 * must call psa_reset_key_attributes(). As an exception, calling
132 * psa_reset_key_attributes() on an attribute structure is optional if
133 * the structure has only been modified by the following functions
134 * since it was initialized or last reset with psa_reset_key_attributes():
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200135 * - psa_set_key_id()
136 * - psa_set_key_lifetime()
Gilles Peskine9c640f92019-04-28 11:36:21 +0200137 * - psa_set_key_type()
138 * - psa_set_key_bits()
139 * - psa_set_key_usage_flags()
140 * - psa_set_key_algorithm()
141 *
Gilles Peskine20628592019-04-19 19:29:50 +0200142 * Before calling any function on a key attribute structure, the application
143 * must initialize it by any of the following means:
144 * - Set the structure to all-bits-zero, for example:
145 * \code
146 * psa_key_attributes_t attributes;
147 * memset(&attributes, 0, sizeof(attributes));
148 * \endcode
149 * - Initialize the structure to logical zero values, for example:
150 * \code
151 * psa_key_attributes_t attributes = {0};
152 * \endcode
153 * - Initialize the structure to the initializer #PSA_KEY_ATTRIBUTES_INIT,
154 * for example:
155 * \code
156 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
157 * \endcode
158 * - Assign the result of the function psa_key_attributes_init()
159 * to the structure, for example:
160 * \code
161 * psa_key_attributes_t attributes;
162 * attributes = psa_key_attributes_init();
163 * \endcode
164 *
165 * A freshly initialized attribute structure contains the following
166 * values:
167 *
168 * - lifetime: #PSA_KEY_LIFETIME_VOLATILE.
169 * - key identifier: unspecified.
170 * - type: \c 0, with no domain parameters.
171 * - key size: \c 0.
172 * - usage flags: \c 0.
173 * - algorithm: \c 0.
174 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200175 * A typical sequence to create a key is as follows:
176 * -# Create and initialize an attribute structure.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200177 * -# If the key is persistent, call psa_set_key_id().
178 * Also call psa_set_key_lifetime() to place the key in a non-default
179 * location.
Gilles Peskine9c640f92019-04-28 11:36:21 +0200180 * -# Set the key policy with psa_set_key_usage_flags() and
181 * psa_set_key_algorithm().
182 * -# Set the key type with psa_set_key_type(). If the key type requires
183 * domain parameters, call psa_set_key_domain_parameters() instead.
184 * Skip this step if copying an existing key with psa_copy_key().
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100185 * -# When generating a random key with psa_generate_random_key() or deriving a key
186 * with psa_generate_derived_key(), set the desired key size with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200187 * psa_set_key_bits().
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100188 * -# Call a key creation function: psa_import_key(), psa_generate_random_key(),
189 * psa_generate_derived_key() or psa_copy_key(). This function reads
Gilles Peskine1ea5e442019-05-02 20:31:10 +0200190 * the attribute structure, creates a key with these attributes, and
191 * outputs a handle to the newly created key.
192 * -# The attribute structure is now no longer necessary. If you called
Gilles Peskine9c640f92019-04-28 11:36:21 +0200193 * psa_set_key_domain_parameters() earlier, you must call
194 * psa_reset_key_attributes() to free any resources used by the
195 * domain parameters. Otherwise calling psa_reset_key_attributes()
196 * is optional.
Gilles Peskine20628592019-04-19 19:29:50 +0200197 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200198 * A typical sequence to query a key's attributes is as follows:
199 * -# Call psa_get_key_attributes().
200 * -# Call `psa_get_key_xxx` functions to retrieve the attribute(s) that
201 * you are interested in.
202 * -# Call psa_reset_key_attributes() to free any resources that may be
203 * used by the attribute structure.
204 *
205 * Once a key has been created, it is impossible to change its attributes.
Gilles Peskine87a5e562019-04-17 12:28:25 +0200206 */
207typedef struct psa_key_attributes_s psa_key_attributes_t;
208
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200209/** Declare a key as persistent and set its key identifier.
210 *
211 * If the attribute structure declares the key as volatile (which is
212 * the default content of an attribute structure), this function sets
213 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200214 *
215 * This function does not access storage, it merely fills the attribute
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200216 * structure with given value. The persistent key will be written to
Gilles Peskine20628592019-04-19 19:29:50 +0200217 * storage when the attribute structure is passed to a key creation
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100218 * function such as psa_import_key(), psa_generate_random_key(),
219 * psa_generate_derived_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200220 *
Gilles Peskine20628592019-04-19 19:29:50 +0200221 * This function may be declared as `static` (i.e. without external
222 * linkage). This function may be provided as a function-like macro,
223 * but in this case it must evaluate each of its arguments exactly once.
224 *
225 * \param[out] attributes The attribute structure to write to.
226 * \param id The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200227 */
228static void psa_set_key_id(psa_key_attributes_t *attributes,
229 psa_key_id_t id);
230
231/** Set the location of a persistent key.
232 *
233 * To make a key persistent, you must give it a persistent key identifier
234 * with psa_set_key_id().
235 *
236 * This function does not access storage, it merely fills the attribute
237 * structure with given value. The persistent key will be written to
238 * storage when the attribute structure is passed to a key creation
239 * function such as psa_import_key(), psa_generate_random_key(),
240 * psa_generate_derived_key() or psa_copy_key().
241 *
242 * This function may be declared as `static` (i.e. without external
243 * linkage). This function may be provided as a function-like macro,
244 * but in this case it must evaluate each of its arguments exactly once.
245 *
246 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200247 * \param lifetime The lifetime for the key.
248 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200249 * key will be volatile, and the key identifier
250 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200251 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200252static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
253 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200254
Gilles Peskine20628592019-04-19 19:29:50 +0200255/** Retrieve the key identifier from key attributes.
256 *
257 * This function may be declared as `static` (i.e. without external
258 * linkage). This function may be provided as a function-like macro,
259 * but in this case it must evaluate its argument exactly once.
260 *
261 * \param[in] attributes The key attribute structure to query.
262 *
263 * \return The persistent identifier stored in the attribute structure.
264 * This value is unspecified if the attribute structure declares
265 * the key as volatile.
266 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200267static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
268
Gilles Peskine20628592019-04-19 19:29:50 +0200269/** Retrieve the lifetime from key attributes.
270 *
271 * This function may be declared as `static` (i.e. without external
272 * linkage). This function may be provided as a function-like macro,
273 * but in this case it must evaluate its argument exactly once.
274 *
275 * \param[in] attributes The key attribute structure to query.
276 *
277 * \return The lifetime value stored in the attribute structure.
278 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200279static psa_key_lifetime_t psa_get_key_lifetime(
280 const psa_key_attributes_t *attributes);
281
Gilles Peskine20628592019-04-19 19:29:50 +0200282/** Declare usage flags for a key.
283 *
284 * Usage flags are part of a key's usage policy. They encode what
285 * kind of operations are permitted on the key. For more details,
286 * refer to the documentation of the type #psa_key_usage_t.
287 *
288 * This function overwrites any usage flags
289 * previously set in \p attributes.
290 *
291 * This function may be declared as `static` (i.e. without external
292 * linkage). This function may be provided as a function-like macro,
293 * but in this case it must evaluate each of its arguments exactly once.
294 *
295 * \param[out] attributes The attribute structure to write to.
296 * \param usage_flags The usage flags to write.
297 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200298static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
299 psa_key_usage_t usage_flags);
300
Gilles Peskine20628592019-04-19 19:29:50 +0200301/** Retrieve the usage flags from key attributes.
302 *
303 * This function may be declared as `static` (i.e. without external
304 * linkage). This function may be provided as a function-like macro,
305 * but in this case it must evaluate its argument exactly once.
306 *
307 * \param[in] attributes The key attribute structure to query.
308 *
309 * \return The usage flags stored in the attribute structure.
310 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200311static psa_key_usage_t psa_get_key_usage_flags(
312 const psa_key_attributes_t *attributes);
313
Gilles Peskine20628592019-04-19 19:29:50 +0200314/** Declare the permitted algorithm policy for a key.
315 *
316 * The permitted algorithm policy of a key encodes which algorithm or
317 * algorithms are permitted to be used with this key.
318 *
319 * This function overwrites any algorithm policy
320 * previously set in \p attributes.
321 *
322 * This function may be declared as `static` (i.e. without external
323 * linkage). This function may be provided as a function-like macro,
324 * but in this case it must evaluate each of its arguments exactly once.
325 *
326 * \param[out] attributes The attribute structure to write to.
327 * \param alg The permitted algorithm policy to write.
328 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200329static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
330 psa_algorithm_t alg);
331
Gilles Peskine20628592019-04-19 19:29:50 +0200332/** Retrieve the algorithm policy from key attributes.
333 *
334 * This function may be declared as `static` (i.e. without external
335 * linkage). This function may be provided as a function-like macro,
336 * but in this case it must evaluate its argument exactly once.
337 *
338 * \param[in] attributes The key attribute structure to query.
339 *
340 * \return The algorithm stored in the attribute structure.
341 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200342static psa_algorithm_t psa_get_key_algorithm(
343 const psa_key_attributes_t *attributes);
344
Gilles Peskine20628592019-04-19 19:29:50 +0200345/** Declare the type of a key.
346 *
347 * If a type requires domain parameters, you must call
348 * psa_set_key_domain_parameters() instead of this function.
349 *
350 * This function overwrites any key type and domain parameters
351 * previously set in \p attributes.
352 *
353 * This function may be declared as `static` (i.e. without external
354 * linkage). This function may be provided as a function-like macro,
355 * but in this case it must evaluate each of its arguments exactly once.
356 *
357 * \param[out] attributes The attribute structure to write to.
358 * \param type The key type to write.
359 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200360static void psa_set_key_type(psa_key_attributes_t *attributes,
361 psa_key_type_t type);
362
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200363/** Declare the size of a key.
364 *
365 * This function overwrites any key size previously set in \p attributes.
366 *
367 * This function may be declared as `static` (i.e. without external
368 * linkage). This function may be provided as a function-like macro,
369 * but in this case it must evaluate each of its arguments exactly once.
370 *
371 * \param[out] attributes The attribute structure to write to.
372 * \param bits The key size in bits.
373 */
374static void psa_set_key_bits(psa_key_attributes_t *attributes,
375 size_t bits);
376
Gilles Peskine20628592019-04-19 19:29:50 +0200377/** Retrieve the key type from key attributes.
378 *
379 * This function may be declared as `static` (i.e. without external
380 * linkage). This function may be provided as a function-like macro,
381 * but in this case it must evaluate its argument exactly once.
382 *
383 * \param[in] attributes The key attribute structure to query.
384 *
385 * \return The key type stored in the attribute structure.
386 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200387static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
388
Gilles Peskine20628592019-04-19 19:29:50 +0200389/** Retrieve the key size from key attributes.
390 *
391 * This function may be declared as `static` (i.e. without external
392 * linkage). This function may be provided as a function-like macro,
393 * but in this case it must evaluate its argument exactly once.
394 *
395 * \param[in] attributes The key attribute structure to query.
396 *
397 * \return The key size stored in the attribute structure, in bits.
398 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200399static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
400
Gilles Peskineb699f072019-04-26 16:06:02 +0200401/**
402 * \brief Set domain parameters for a key.
403 *
404 * Some key types require additional domain parameters in addition to
405 * the key type identifier and the key size.
406 * The format for the required domain parameters varies by the key type.
407 *
Gilles Peskinee56e8782019-04-26 17:34:02 +0200408 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEYPAIR),
409 * the domain parameter data consists of the public exponent,
Gilles Peskineb699f072019-04-26 16:06:02 +0200410 * represented as a big-endian integer with no leading zeros.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200411 * This information is used when generating an RSA key pair.
Gilles Peskineb699f072019-04-26 16:06:02 +0200412 * When importing a key, the public exponent is read from the imported
413 * key data and the exponent recorded in the attribute structure is ignored.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200414 * As an exception, the public exponent 65537 is represented by an empty
415 * byte string.
416 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEYPAIR),
Gilles Peskineb699f072019-04-26 16:06:02 +0200417 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
418 * ```
419 * Dss-Parms ::= SEQUENCE {
420 * p INTEGER,
421 * q INTEGER,
422 * g INTEGER
423 * }
424 * ```
Gilles Peskinee56e8782019-04-26 17:34:02 +0200425 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY or
426 * #PSA_KEY_TYPE_DH_KEYPAIR), the
Gilles Peskineb699f072019-04-26 16:06:02 +0200427 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
428 * ```
429 * DomainParameters ::= SEQUENCE {
430 * p INTEGER, -- odd prime, p=jq +1
431 * g INTEGER, -- generator, g
432 * q INTEGER, -- factor of p-1
433 * j INTEGER OPTIONAL, -- subgroup factor
434 * validationParms ValidationParms OPTIONAL
435 * }
436 * ValidationParms ::= SEQUENCE {
437 * seed BIT STRING,
438 * pgenCounter INTEGER
439 * }
440 * ```
441 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200442 * \note This function may allocate memory or other resources.
443 * Once you have called this function on an attribute structure,
444 * you must call psa_reset_key_attributes() to free these resources.
445 *
Gilles Peskineb699f072019-04-26 16:06:02 +0200446 * \param[in,out] attributes Attribute structure where the specified domain
447 * parameters will be stored.
448 * If this function fails, the content of
449 * \p attributes is not modified.
450 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
451 * \param[in] data Buffer containing the key domain parameters.
452 * The content of this buffer is interpreted
453 * according to \p type as described above.
454 * \param data_length Size of the \p data buffer in bytes.
455 *
456 * \retval #PSA_SUCCESS
457 * \retval #PSA_ERROR_INVALID_ARGUMENT
458 * \retval #PSA_ERROR_NOT_SUPPORTED
459 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
460 */
461psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
462 psa_key_type_t type,
463 const uint8_t *data,
464 size_t data_length);
465
466/**
467 * \brief Get domain parameters for a key.
468 *
469 * Get the domain parameters for a key with this function, if any. The format
470 * of the domain parameters written to \p data is specified in the
471 * documentation for psa_set_key_domain_parameters().
472 *
473 * \param[in] attributes The key attribute structure to query.
474 * \param[out] data On success, the key domain parameters.
475 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineaa02c172019-04-28 11:44:17 +0200476 * The buffer is guaranteed to be large
477 * enough if its size in bytes is at least
478 * the value given by
479 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
Gilles Peskineb699f072019-04-26 16:06:02 +0200480 * \param[out] data_length On success, the number of bytes
481 * that make up the key domain parameters data.
482 *
483 * \retval #PSA_SUCCESS
484 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
485 */
486psa_status_t psa_get_key_domain_parameters(
487 const psa_key_attributes_t *attributes,
488 uint8_t *data,
489 size_t data_size,
490 size_t *data_length);
491
Gilles Peskine20628592019-04-19 19:29:50 +0200492/** Retrieve the attributes of a key.
493 *
494 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200495 * psa_reset_key_attributes(). It then copies the attributes of
496 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200497 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200498 * \note This function may allocate memory or other resources.
499 * Once you have called this function on an attribute structure,
500 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200501 *
Gilles Peskine20628592019-04-19 19:29:50 +0200502 * \param[in] handle Handle to the key to query.
503 * \param[in,out] attributes On success, the attributes of the key.
504 * On failure, equivalent to a
505 * freshly-initialized structure.
506 *
507 * \retval #PSA_SUCCESS
508 * \retval #PSA_ERROR_INVALID_HANDLE
509 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
510 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
511 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200512psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
513 psa_key_attributes_t *attributes);
514
Gilles Peskine20628592019-04-19 19:29:50 +0200515/** Reset a key attribute structure to a freshly initialized state.
516 *
517 * You must initialize the attribute structure as described in the
518 * documentation of the type #psa_key_attributes_t before calling this
519 * function. Once the structure has been initialized, you may call this
520 * function at any time.
521 *
522 * This function frees any auxiliary resources that the structure
523 * may contain.
524 *
525 * \param[in,out] attributes The attribute structure to reset.
526 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200527void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200528
Gilles Peskine87a5e562019-04-17 12:28:25 +0200529/**@}*/
530
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100531/** \defgroup key_management Key management
532 * @{
533 */
534
Gilles Peskinef535eb22018-11-30 14:08:36 +0100535/** Open a handle to an existing persistent key.
536 *
537 * Open a handle to a key which was previously created with psa_create_key().
538 *
539 * \param lifetime The lifetime of the key. This designates a storage
540 * area where the key material is stored. This must not
541 * be #PSA_KEY_LIFETIME_VOLATILE.
542 * \param id The persistent identifier of the key.
543 * \param[out] handle On success, a handle to a key slot which contains
544 * the data and metadata loaded from the specified
545 * persistent location.
546 *
547 * \retval #PSA_SUCCESS
548 * Success. The application can now use the value of `*handle`
549 * to access the newly allocated key slot.
550 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200551 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100552 * \retval #PSA_ERROR_INVALID_ARGUMENT
553 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
554 * \retval #PSA_ERROR_INVALID_ARGUMENT
555 * \p id is invalid for the specified lifetime.
556 * \retval #PSA_ERROR_NOT_SUPPORTED
557 * \p lifetime is not supported.
558 * \retval #PSA_ERROR_NOT_PERMITTED
559 * The specified key exists, but the application does not have the
560 * permission to access it. Note that this specification does not
561 * define any way to create such a key, but it may be possible
562 * through implementation-specific means.
563 */
564psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
565 psa_key_id_t id,
566 psa_key_handle_t *handle);
567
Gilles Peskinef535eb22018-11-30 14:08:36 +0100568/** Close a key handle.
569 *
570 * If the handle designates a volatile key, destroy the key material and
571 * free all associated resources, just like psa_destroy_key().
572 *
573 * If the handle designates a persistent key, free all resources associated
574 * with the key in volatile memory. The key slot in persistent storage is
575 * not affected and can be opened again later with psa_open_key().
576 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100577 * If the key is currently in use in a multipart operation,
578 * the multipart operation is aborted.
579 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100580 * \param handle The key handle to close.
581 *
582 * \retval #PSA_SUCCESS
583 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100584 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100585 */
586psa_status_t psa_close_key(psa_key_handle_t handle);
587
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100588/**@}*/
589
590/** \defgroup import_export Key import and export
591 * @{
592 */
593
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100594/**
595 * \brief Import a key in binary format.
596 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100597 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100598 * documentation of psa_export_public_key() for the format of public keys
599 * and to the documentation of psa_export_key() for the format for
600 * other key types.
601 *
602 * This specification supports a single format for each key type.
603 * Implementations may support other formats as long as the standard
604 * format is supported. Implementations that support other formats
605 * should ensure that the formats are clearly unambiguous so as to
606 * minimize the risk that an invalid input is accidentally interpreted
607 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100608 *
Gilles Peskine20628592019-04-19 19:29:50 +0200609 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200610 * The key size is always determined from the
611 * \p data buffer.
612 * If the key size in \p attributes is nonzero,
613 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200614 * \param[out] handle On success, a handle to the newly created key.
615 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100616 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine20628592019-04-19 19:29:50 +0200617 * buffer is interpreted according to the type and,
618 * if applicable, domain parameters declared in
619 * \p attributes.
620 * All implementations must support at least the format
621 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100622 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200623 * the chosen type. Implementations may allow other
624 * formats, but should be conservative: implementations
625 * should err on the side of rejecting content if it
626 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200627 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100628 *
Gilles Peskine28538492018-07-11 17:34:00 +0200629 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100630 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100631 * If the key is persistent, the key material and the key's metadata
632 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200633 * \retval #PSA_ERROR_ALREADY_EXISTS
634 * This is an attempt to create a persistent key, and there is
635 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200636 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200637 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200638 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200639 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200640 * The key attributes, as a whole, are invalid.
641 * \retval #PSA_ERROR_INVALID_ARGUMENT
642 * The key data is not correctly formatted.
643 * \retval #PSA_ERROR_INVALID_ARGUMENT
644 * The size in \p attributes is nonzero and does not match the size
645 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200646 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
647 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
648 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100649 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200650 * \retval #PSA_ERROR_HARDWARE_FAILURE
651 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300652 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300653 * The library has not been previously initialized by psa_crypto_init().
654 * It is implementation-dependent whether a failure to initialize
655 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100656 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200657psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
658 psa_key_handle_t *handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100659 const uint8_t *data,
660 size_t data_length);
661
662/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100663 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200664 *
665 * This function destroys the content of the key slot from both volatile
666 * memory and, if applicable, non-volatile storage. Implementations shall
667 * make a best effort to ensure that any previous content of the slot is
668 * unrecoverable.
669 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100670 * This function also erases any metadata such as policies and frees all
671 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200672 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100673 * If the key is currently in use in a multipart operation,
674 * the multipart operation is aborted.
675 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100676 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677 *
Gilles Peskine28538492018-07-11 17:34:00 +0200678 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200679 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200680 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200681 * The slot holds content and cannot be erased because it is
682 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100683 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200684 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200685 * There was an failure in communication with the cryptoprocessor.
686 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200687 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200688 * The storage is corrupted. Implementations shall make a best effort
689 * to erase key material even in this stage, however applications
690 * should be aware that it may be impossible to guarantee that the
691 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200692 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200693 * An unexpected condition which is not a storage corruption or
694 * a communication failure occurred. The cryptoprocessor may have
695 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300696 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300697 * The library has not been previously initialized by psa_crypto_init().
698 * It is implementation-dependent whether a failure to initialize
699 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100700 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100701psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100702
703/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100704 * \brief Export a key in binary format.
705 *
706 * The output of this function can be passed to psa_import_key() to
707 * create an equivalent object.
708 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100709 * If the implementation of psa_import_key() supports other formats
710 * beyond the format specified here, the output from psa_export_key()
711 * must use the representation specified here, not the original
712 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100713 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100714 * For standard key types, the output format is as follows:
715 *
716 * - For symmetric keys (including MAC keys), the format is the
717 * raw bytes of the key.
718 * - For DES, the key data consists of 8 bytes. The parity bits must be
719 * correct.
720 * - For Triple-DES, the format is the concatenation of the
721 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100722 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200723 * is the non-encrypted DER encoding of the representation defined by
724 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
725 * ```
726 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200727 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200728 * modulus INTEGER, -- n
729 * publicExponent INTEGER, -- e
730 * privateExponent INTEGER, -- d
731 * prime1 INTEGER, -- p
732 * prime2 INTEGER, -- q
733 * exponent1 INTEGER, -- d mod (p-1)
734 * exponent2 INTEGER, -- d mod (q-1)
735 * coefficient INTEGER, -- (inverse of q) mod p
736 * }
737 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000738 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
739 * representation of the private key `x` as a big-endian byte string. The
740 * length of the byte string is the private key size in bytes (leading zeroes
741 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200742 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100743 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100744 * a representation of the private value as a `ceiling(m/8)`-byte string
745 * where `m` is the bit size associated with the curve, i.e. the bit size
746 * of the order of the curve's coordinate field. This byte string is
747 * in little-endian order for Montgomery curves (curve types
748 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
749 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
750 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100751 * This is the content of the `privateKey` field of the `ECPrivateKey`
752 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000753 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
754 * format is the representation of the private key `x` as a big-endian byte
755 * string. The length of the byte string is the private key size in bytes
756 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200757 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
758 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100759 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100760 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200761 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200762 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200763 * \param[out] data_length On success, the number of bytes
764 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100765 *
Gilles Peskine28538492018-07-11 17:34:00 +0200766 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100767 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200768 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200769 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100770 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200771 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
772 * The size of the \p data buffer is too small. You can determine a
773 * sufficient buffer size by calling
774 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
775 * where \c type is the key type
776 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200777 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
778 * \retval #PSA_ERROR_HARDWARE_FAILURE
779 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300780 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300781 * The library has not been previously initialized by psa_crypto_init().
782 * It is implementation-dependent whether a failure to initialize
783 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100784 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100785psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100786 uint8_t *data,
787 size_t data_size,
788 size_t *data_length);
789
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100790/**
791 * \brief Export a public key or the public part of a key pair in binary format.
792 *
793 * The output of this function can be passed to psa_import_key() to
794 * create an object that is equivalent to the public key.
795 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000796 * This specification supports a single format for each key type.
797 * Implementations may support other formats as long as the standard
798 * format is supported. Implementations that support other formats
799 * should ensure that the formats are clearly unambiguous so as to
800 * minimize the risk that an invalid input is accidentally interpreted
801 * according to a different format.
802 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000803 * For standard key types, the output format is as follows:
804 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
805 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
806 * ```
807 * RSAPublicKey ::= SEQUENCE {
808 * modulus INTEGER, -- n
809 * publicExponent INTEGER } -- e
810 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000811 * - For elliptic curve public keys (key types for which
812 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
813 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
814 * Let `m` be the bit size associated with the curve, i.e. the bit size of
815 * `q` for a curve over `F_q`. The representation consists of:
816 * - The byte 0x04;
817 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
818 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000819 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
820 * representation of the public key `y = g^x mod p` as a big-endian byte
821 * string. The length of the byte string is the length of the base prime `p`
822 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000823 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
824 * the format is the representation of the public key `y = g^x mod p` as a
825 * big-endian byte string. The length of the byte string is the length of the
826 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100827 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100828 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200829 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200830 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200831 * \param[out] data_length On success, the number of bytes
832 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100833 *
Gilles Peskine28538492018-07-11 17:34:00 +0200834 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100835 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200836 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200837 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200838 * The key is neither a public key nor a key pair.
839 * \retval #PSA_ERROR_NOT_SUPPORTED
840 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
841 * The size of the \p data buffer is too small. You can determine a
842 * sufficient buffer size by calling
843 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
844 * where \c type is the key type
845 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200846 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
847 * \retval #PSA_ERROR_HARDWARE_FAILURE
848 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300849 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300850 * The library has not been previously initialized by psa_crypto_init().
851 * It is implementation-dependent whether a failure to initialize
852 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100853 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100854psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100855 uint8_t *data,
856 size_t data_size,
857 size_t *data_length);
858
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100859/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100860 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100861 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000862 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100863 * This function is primarily useful to copy a key from one location
864 * to another, since it populates a key using the material from
865 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200866 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100867 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100868 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100869 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100870 *
Gilles Peskine20628592019-04-19 19:29:50 +0200871 * The resulting key may only be used in a way that conforms to
872 * both the policy of the original key and the policy specified in
873 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100874 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200875 * usage flags on the source policy and the usage flags in \p attributes.
876 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100877 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200878 * - If either of the policies allows an algorithm and the other policy
879 * allows a wildcard-based algorithm policy that includes this algorithm,
880 * the resulting key allows the same algorithm.
881 * - If the policies do not allow any algorithm in common, this function
882 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200883 *
Gilles Peskine20628592019-04-19 19:29:50 +0200884 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100885 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200886 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100887 * \param source_handle The key to copy. It must be a handle to an
888 * occupied slot.
Gilles Peskine20628592019-04-19 19:29:50 +0200889 * \param[in] attributes The attributes for the new key.
890 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200891 * - The key type and size may be 0. If either is
892 * nonzero, it must match the corresponding
893 * attribute of the source key.
894 * - If \p attributes contains domain parameters,
895 * they must match the domain parameters of
896 * the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200897 * - The key location (the lifetime and, for
898 * persistent keys, the key identifier) is
899 * used directly.
900 * - The policy constraints (usage flags and
901 * algorithm policy) are combined from
902 * the source key and \p attributes so that
903 * both sets of restrictions apply, as
904 * described in the documentation of this function.
905 * \param[out] target_handle On success, a handle to the newly created key.
906 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200907 *
908 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100909 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200910 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200911 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200912 * This is an attempt to create a persistent key, and there is
913 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200914 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200915 * The lifetime or identifier in \p attributes are invalid.
916 * \retval #PSA_ERROR_INVALID_ARGUMENT
917 * The policy constraints on the source and specified in
918 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200919 * \retval #PSA_ERROR_INVALID_ARGUMENT
920 * \p attributes specifies a key type, domain parameters or key size
921 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100922 * \retval #PSA_ERROR_NOT_PERMITTED
923 * The source key is not exportable and its lifetime does not
924 * allow copying it to the target's lifetime.
925 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
926 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200927 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
928 * \retval #PSA_ERROR_HARDWARE_FAILURE
929 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100930 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100931psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200932 const psa_key_attributes_t *attributes,
933 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100934
935/**@}*/
936
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100937/** \defgroup hash Message digests
938 * @{
939 */
940
Gilles Peskine69647a42019-01-14 20:18:12 +0100941/** Calculate the hash (digest) of a message.
942 *
943 * \note To verify the hash of a message against an
944 * expected value, use psa_hash_compare() instead.
945 *
946 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
947 * such that #PSA_ALG_IS_HASH(\p alg) is true).
948 * \param[in] input Buffer containing the message to hash.
949 * \param input_length Size of the \p input buffer in bytes.
950 * \param[out] hash Buffer where the hash is to be written.
951 * \param hash_size Size of the \p hash buffer in bytes.
952 * \param[out] hash_length On success, the number of bytes
953 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100954 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100955 *
956 * \retval #PSA_SUCCESS
957 * Success.
958 * \retval #PSA_ERROR_NOT_SUPPORTED
959 * \p alg is not supported or is not a hash algorithm.
960 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
961 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
962 * \retval #PSA_ERROR_HARDWARE_FAILURE
963 * \retval #PSA_ERROR_TAMPERING_DETECTED
964 */
965psa_status_t psa_hash_compute(psa_algorithm_t alg,
966 const uint8_t *input,
967 size_t input_length,
968 uint8_t *hash,
969 size_t hash_size,
970 size_t *hash_length);
971
972/** Calculate the hash (digest) of a message and compare it with a
973 * reference value.
974 *
975 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
976 * such that #PSA_ALG_IS_HASH(\p alg) is true).
977 * \param[in] input Buffer containing the message to hash.
978 * \param input_length Size of the \p input buffer in bytes.
979 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100980 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100981 *
982 * \retval #PSA_SUCCESS
983 * The expected hash is identical to the actual hash of the input.
984 * \retval #PSA_ERROR_INVALID_SIGNATURE
985 * The hash of the message was calculated successfully, but it
986 * differs from the expected hash.
987 * \retval #PSA_ERROR_NOT_SUPPORTED
988 * \p alg is not supported or is not a hash algorithm.
989 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
990 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
991 * \retval #PSA_ERROR_HARDWARE_FAILURE
992 * \retval #PSA_ERROR_TAMPERING_DETECTED
993 */
994psa_status_t psa_hash_compare(psa_algorithm_t alg,
995 const uint8_t *input,
996 size_t input_length,
997 const uint8_t *hash,
998 const size_t hash_length);
999
Gilles Peskine308b91d2018-02-08 09:47:44 +01001000/** The type of the state data structure for multipart hash operations.
1001 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001002 * Before calling any function on a hash operation object, the application must
1003 * initialize it by any of the following means:
1004 * - Set the structure to all-bits-zero, for example:
1005 * \code
1006 * psa_hash_operation_t operation;
1007 * memset(&operation, 0, sizeof(operation));
1008 * \endcode
1009 * - Initialize the structure to logical zero values, for example:
1010 * \code
1011 * psa_hash_operation_t operation = {0};
1012 * \endcode
1013 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
1014 * for example:
1015 * \code
1016 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
1017 * \endcode
1018 * - Assign the result of the function psa_hash_operation_init()
1019 * to the structure, for example:
1020 * \code
1021 * psa_hash_operation_t operation;
1022 * operation = psa_hash_operation_init();
1023 * \endcode
1024 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001025 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001026 * make any assumptions about the content of this structure except
1027 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001028typedef struct psa_hash_operation_s psa_hash_operation_t;
1029
Jaeden Amero6a25b412019-01-04 11:47:44 +00001030/** \def PSA_HASH_OPERATION_INIT
1031 *
1032 * This macro returns a suitable initializer for a hash operation object
1033 * of type #psa_hash_operation_t.
1034 */
1035#ifdef __DOXYGEN_ONLY__
1036/* This is an example definition for documentation purposes.
1037 * Implementations should define a suitable value in `crypto_struct.h`.
1038 */
1039#define PSA_HASH_OPERATION_INIT {0}
1040#endif
1041
1042/** Return an initial value for a hash operation object.
1043 */
1044static psa_hash_operation_t psa_hash_operation_init(void);
1045
Gilles Peskinef45adda2019-01-14 18:29:18 +01001046/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001047 *
1048 * The sequence of operations to calculate a hash (message digest)
1049 * is as follows:
1050 * -# Allocate an operation object which will be passed to all the functions
1051 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +00001052 * -# Initialize the operation object with one of the methods described in the
1053 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001054 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001055 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001056 * of the message each time. The hash that is calculated is the hash
1057 * of the concatenation of these messages in order.
1058 * -# To calculate the hash, call psa_hash_finish().
1059 * To compare the hash with an expected value, call psa_hash_verify().
1060 *
1061 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +00001062 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001063 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001064 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001065 * eventually terminate the operation. The following events terminate an
1066 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001067 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001068 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001069 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001070 * \param[in,out] operation The operation object to set up. It must have
1071 * been initialized as per the documentation for
1072 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001073 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1074 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001075 *
Gilles Peskine28538492018-07-11 17:34:00 +02001076 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001077 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001078 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001079 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001080 * \retval #PSA_ERROR_BAD_STATE
1081 * The operation state is not valid (already set up and not
1082 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001083 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1084 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1085 * \retval #PSA_ERROR_HARDWARE_FAILURE
1086 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001087 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001088psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001089 psa_algorithm_t alg);
1090
Gilles Peskine308b91d2018-02-08 09:47:44 +01001091/** Add a message fragment to a multipart hash operation.
1092 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001093 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001094 *
1095 * If this function returns an error status, the operation becomes inactive.
1096 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001097 * \param[in,out] operation Active hash operation.
1098 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001099 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001100 *
Gilles Peskine28538492018-07-11 17:34:00 +02001101 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001102 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001103 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001104 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001105 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1106 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1107 * \retval #PSA_ERROR_HARDWARE_FAILURE
1108 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001109 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001110psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1111 const uint8_t *input,
1112 size_t input_length);
1113
Gilles Peskine308b91d2018-02-08 09:47:44 +01001114/** Finish the calculation of the hash of a message.
1115 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001116 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001117 * This function calculates the hash of the message formed by concatenating
1118 * the inputs passed to preceding calls to psa_hash_update().
1119 *
1120 * When this function returns, the operation becomes inactive.
1121 *
1122 * \warning Applications should not call this function if they expect
1123 * a specific value for the hash. Call psa_hash_verify() instead.
1124 * Beware that comparing integrity or authenticity data such as
1125 * hash values with a function such as \c memcmp is risky
1126 * because the time taken by the comparison may leak information
1127 * about the hashed data which could allow an attacker to guess
1128 * a valid hash and thereby bypass security controls.
1129 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001130 * \param[in,out] operation Active hash operation.
1131 * \param[out] hash Buffer where the hash is to be written.
1132 * \param hash_size Size of the \p hash buffer in bytes.
1133 * \param[out] hash_length On success, the number of bytes
1134 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001135 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001136 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001137 *
Gilles Peskine28538492018-07-11 17:34:00 +02001138 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001139 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001140 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001141 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001142 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001143 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001144 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001145 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001146 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1147 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1148 * \retval #PSA_ERROR_HARDWARE_FAILURE
1149 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001150 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001151psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1152 uint8_t *hash,
1153 size_t hash_size,
1154 size_t *hash_length);
1155
Gilles Peskine308b91d2018-02-08 09:47:44 +01001156/** Finish the calculation of the hash of a message and compare it with
1157 * an expected value.
1158 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001159 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001160 * This function calculates the hash of the message formed by concatenating
1161 * the inputs passed to preceding calls to psa_hash_update(). It then
1162 * compares the calculated hash with the expected hash passed as a
1163 * parameter to this function.
1164 *
1165 * When this function returns, the operation becomes inactive.
1166 *
Gilles Peskine19067982018-03-20 17:54:53 +01001167 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001168 * comparison between the actual hash and the expected hash is performed
1169 * in constant time.
1170 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001171 * \param[in,out] operation Active hash operation.
1172 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001173 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001174 *
Gilles Peskine28538492018-07-11 17:34:00 +02001175 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001176 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001177 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001178 * The hash of the message was calculated successfully, but it
1179 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001180 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001181 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001182 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1183 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1184 * \retval #PSA_ERROR_HARDWARE_FAILURE
1185 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001186 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001187psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1188 const uint8_t *hash,
1189 size_t hash_length);
1190
Gilles Peskine308b91d2018-02-08 09:47:44 +01001191/** Abort a hash operation.
1192 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001193 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001194 * \p operation structure itself. Once aborted, the operation object
1195 * can be reused for another operation by calling
1196 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001197 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001198 * You may call this function any time after the operation object has
1199 * been initialized by any of the following methods:
1200 * - A call to psa_hash_setup(), whether it succeeds or not.
1201 * - Initializing the \c struct to all-bits-zero.
1202 * - Initializing the \c struct to logical zeros, e.g.
1203 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001204 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001205 * In particular, calling psa_hash_abort() after the operation has been
1206 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1207 * psa_hash_verify() is safe and has no effect.
1208 *
1209 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001210 *
Gilles Peskine28538492018-07-11 17:34:00 +02001211 * \retval #PSA_SUCCESS
1212 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001213 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001214 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1215 * \retval #PSA_ERROR_HARDWARE_FAILURE
1216 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001217 */
1218psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001219
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001220/** Clone a hash operation.
1221 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001222 * This function copies the state of an ongoing hash operation to
1223 * a new operation object. In other words, this function is equivalent
1224 * to calling psa_hash_setup() on \p target_operation with the same
1225 * algorithm that \p source_operation was set up for, then
1226 * psa_hash_update() on \p target_operation with the same input that
1227 * that was passed to \p source_operation. After this function returns, the
1228 * two objects are independent, i.e. subsequent calls involving one of
1229 * the objects do not affect the other object.
1230 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001231 * \param[in] source_operation The active hash operation to clone.
1232 * \param[in,out] target_operation The operation object to set up.
1233 * It must be initialized but not active.
1234 *
1235 * \retval #PSA_SUCCESS
1236 * \retval #PSA_ERROR_BAD_STATE
1237 * \p source_operation is not an active hash operation.
1238 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001239 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001240 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1241 * \retval #PSA_ERROR_HARDWARE_FAILURE
1242 * \retval #PSA_ERROR_TAMPERING_DETECTED
1243 */
1244psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1245 psa_hash_operation_t *target_operation);
1246
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001247/**@}*/
1248
Gilles Peskine8c9def32018-02-08 10:02:12 +01001249/** \defgroup MAC Message authentication codes
1250 * @{
1251 */
1252
Gilles Peskine69647a42019-01-14 20:18:12 +01001253/** Calculate the MAC (message authentication code) of a message.
1254 *
1255 * \note To verify the MAC of a message against an
1256 * expected value, use psa_mac_verify() instead.
1257 * Beware that comparing integrity or authenticity data such as
1258 * MAC values with a function such as \c memcmp is risky
1259 * because the time taken by the comparison may leak information
1260 * about the MAC value which could allow an attacker to guess
1261 * a valid MAC and thereby bypass security controls.
1262 *
1263 * \param handle Handle to the key to use for the operation.
1264 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001265 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001266 * \param[in] input Buffer containing the input message.
1267 * \param input_length Size of the \p input buffer in bytes.
1268 * \param[out] mac Buffer where the MAC value is to be written.
1269 * \param mac_size Size of the \p mac buffer in bytes.
1270 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001271 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001272 *
1273 * \retval #PSA_SUCCESS
1274 * Success.
1275 * \retval #PSA_ERROR_INVALID_HANDLE
1276 * \retval #PSA_ERROR_EMPTY_SLOT
1277 * \retval #PSA_ERROR_NOT_PERMITTED
1278 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001279 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001280 * \retval #PSA_ERROR_NOT_SUPPORTED
1281 * \p alg is not supported or is not a MAC algorithm.
1282 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1283 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1284 * \retval #PSA_ERROR_HARDWARE_FAILURE
1285 * \retval #PSA_ERROR_TAMPERING_DETECTED
1286 * \retval #PSA_ERROR_BAD_STATE
1287 * The library has not been previously initialized by psa_crypto_init().
1288 * It is implementation-dependent whether a failure to initialize
1289 * results in this error code.
1290 */
1291psa_status_t psa_mac_compute(psa_key_handle_t handle,
1292 psa_algorithm_t alg,
1293 const uint8_t *input,
1294 size_t input_length,
1295 uint8_t *mac,
1296 size_t mac_size,
1297 size_t *mac_length);
1298
1299/** Calculate the MAC of a message and compare it with a reference value.
1300 *
1301 * \param handle Handle to the key to use for the operation.
1302 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001303 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001304 * \param[in] input Buffer containing the input message.
1305 * \param input_length Size of the \p input buffer in bytes.
1306 * \param[out] mac Buffer containing the expected MAC value.
1307 * \param mac_length Size of the \p mac buffer in bytes.
1308 *
1309 * \retval #PSA_SUCCESS
1310 * The expected MAC is identical to the actual MAC of the input.
1311 * \retval #PSA_ERROR_INVALID_SIGNATURE
1312 * The MAC of the message was calculated successfully, but it
1313 * differs from the expected value.
1314 * \retval #PSA_ERROR_INVALID_HANDLE
1315 * \retval #PSA_ERROR_EMPTY_SLOT
1316 * \retval #PSA_ERROR_NOT_PERMITTED
1317 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001318 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001319 * \retval #PSA_ERROR_NOT_SUPPORTED
1320 * \p alg is not supported or is not a MAC algorithm.
1321 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1322 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1323 * \retval #PSA_ERROR_HARDWARE_FAILURE
1324 * \retval #PSA_ERROR_TAMPERING_DETECTED
1325 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001326psa_status_t psa_mac_verify(psa_key_handle_t handle,
1327 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001328 const uint8_t *input,
1329 size_t input_length,
1330 const uint8_t *mac,
1331 const size_t mac_length);
1332
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001333/** The type of the state data structure for multipart MAC operations.
1334 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001335 * Before calling any function on a MAC operation object, the application must
1336 * initialize it by any of the following means:
1337 * - Set the structure to all-bits-zero, for example:
1338 * \code
1339 * psa_mac_operation_t operation;
1340 * memset(&operation, 0, sizeof(operation));
1341 * \endcode
1342 * - Initialize the structure to logical zero values, for example:
1343 * \code
1344 * psa_mac_operation_t operation = {0};
1345 * \endcode
1346 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1347 * for example:
1348 * \code
1349 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1350 * \endcode
1351 * - Assign the result of the function psa_mac_operation_init()
1352 * to the structure, for example:
1353 * \code
1354 * psa_mac_operation_t operation;
1355 * operation = psa_mac_operation_init();
1356 * \endcode
1357 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001358 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001359 * make any assumptions about the content of this structure except
1360 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001361typedef struct psa_mac_operation_s psa_mac_operation_t;
1362
Jaeden Amero769ce272019-01-04 11:48:03 +00001363/** \def PSA_MAC_OPERATION_INIT
1364 *
1365 * This macro returns a suitable initializer for a MAC operation object of type
1366 * #psa_mac_operation_t.
1367 */
1368#ifdef __DOXYGEN_ONLY__
1369/* This is an example definition for documentation purposes.
1370 * Implementations should define a suitable value in `crypto_struct.h`.
1371 */
1372#define PSA_MAC_OPERATION_INIT {0}
1373#endif
1374
1375/** Return an initial value for a MAC operation object.
1376 */
1377static psa_mac_operation_t psa_mac_operation_init(void);
1378
Gilles Peskinef45adda2019-01-14 18:29:18 +01001379/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001380 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001381 * This function sets up the calculation of the MAC
1382 * (message authentication code) of a byte string.
1383 * To verify the MAC of a message against an
1384 * expected value, use psa_mac_verify_setup() instead.
1385 *
1386 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001387 * -# Allocate an operation object which will be passed to all the functions
1388 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001389 * -# Initialize the operation object with one of the methods described in the
1390 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001391 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001392 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1393 * of the message each time. The MAC that is calculated is the MAC
1394 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001395 * -# At the end of the message, call psa_mac_sign_finish() to finish
1396 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001397 *
1398 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001399 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001400 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001401 * After a successful call to psa_mac_sign_setup(), the application must
1402 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001403 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001404 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001405 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001406 * \param[in,out] operation The operation object to set up. It must have
1407 * been initialized as per the documentation for
1408 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001409 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001410 * It must remain valid until the operation
1411 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001412 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001413 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001414 *
Gilles Peskine28538492018-07-11 17:34:00 +02001415 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001416 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001417 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001418 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001419 * \retval #PSA_ERROR_NOT_PERMITTED
1420 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001421 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001422 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001423 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001424 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1425 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1426 * \retval #PSA_ERROR_HARDWARE_FAILURE
1427 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001428 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001429 * The operation state is not valid (already set up and not
1430 * subsequently completed).
1431 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001432 * The library has not been previously initialized by psa_crypto_init().
1433 * It is implementation-dependent whether a failure to initialize
1434 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001435 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001436psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001437 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001438 psa_algorithm_t alg);
1439
Gilles Peskinef45adda2019-01-14 18:29:18 +01001440/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001441 *
1442 * This function sets up the verification of the MAC
1443 * (message authentication code) of a byte string against an expected value.
1444 *
1445 * The sequence of operations to verify a MAC is as follows:
1446 * -# Allocate an operation object which will be passed to all the functions
1447 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001448 * -# Initialize the operation object with one of the methods described in the
1449 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001450 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001451 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1452 * of the message each time. The MAC that is calculated is the MAC
1453 * of the concatenation of these messages in order.
1454 * -# At the end of the message, call psa_mac_verify_finish() to finish
1455 * calculating the actual MAC of the message and verify it against
1456 * the expected value.
1457 *
1458 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001459 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001460 *
1461 * After a successful call to psa_mac_verify_setup(), the application must
1462 * eventually terminate the operation through one of the following methods:
1463 * - A failed call to psa_mac_update().
1464 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1465 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001466 * \param[in,out] operation The operation object to set up. It must have
1467 * been initialized as per the documentation for
1468 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001469 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001470 * It must remain valid until the operation
1471 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001472 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1473 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001474 *
Gilles Peskine28538492018-07-11 17:34:00 +02001475 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001476 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001477 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001478 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001479 * \retval #PSA_ERROR_NOT_PERMITTED
1480 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001481 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001482 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001483 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001484 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1485 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1486 * \retval #PSA_ERROR_HARDWARE_FAILURE
1487 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001488 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001489 * The operation state is not valid (already set up and not
1490 * subsequently completed).
1491 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001492 * The library has not been previously initialized by psa_crypto_init().
1493 * It is implementation-dependent whether a failure to initialize
1494 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001495 */
1496psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001497 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001498 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001499
Gilles Peskinedcd14942018-07-12 00:30:52 +02001500/** Add a message fragment to a multipart MAC operation.
1501 *
1502 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1503 * before calling this function.
1504 *
1505 * If this function returns an error status, the operation becomes inactive.
1506 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001507 * \param[in,out] operation Active MAC operation.
1508 * \param[in] input Buffer containing the message fragment to add to
1509 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001510 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001511 *
1512 * \retval #PSA_SUCCESS
1513 * Success.
1514 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001515 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001516 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1517 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1518 * \retval #PSA_ERROR_HARDWARE_FAILURE
1519 * \retval #PSA_ERROR_TAMPERING_DETECTED
1520 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001521psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1522 const uint8_t *input,
1523 size_t input_length);
1524
Gilles Peskinedcd14942018-07-12 00:30:52 +02001525/** Finish the calculation of the MAC of a message.
1526 *
1527 * The application must call psa_mac_sign_setup() before calling this function.
1528 * This function calculates the MAC of the message formed by concatenating
1529 * the inputs passed to preceding calls to psa_mac_update().
1530 *
1531 * When this function returns, the operation becomes inactive.
1532 *
1533 * \warning Applications should not call this function if they expect
1534 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1535 * Beware that comparing integrity or authenticity data such as
1536 * MAC values with a function such as \c memcmp is risky
1537 * because the time taken by the comparison may leak information
1538 * about the MAC value which could allow an attacker to guess
1539 * a valid MAC and thereby bypass security controls.
1540 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001541 * \param[in,out] operation Active MAC operation.
1542 * \param[out] mac Buffer where the MAC value is to be written.
1543 * \param mac_size Size of the \p mac buffer in bytes.
1544 * \param[out] mac_length On success, the number of bytes
1545 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001546 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001547 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001548 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001549 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001550 *
1551 * \retval #PSA_SUCCESS
1552 * Success.
1553 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001554 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001555 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001556 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001557 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1558 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1559 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1560 * \retval #PSA_ERROR_HARDWARE_FAILURE
1561 * \retval #PSA_ERROR_TAMPERING_DETECTED
1562 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001563psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1564 uint8_t *mac,
1565 size_t mac_size,
1566 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001567
Gilles Peskinedcd14942018-07-12 00:30:52 +02001568/** Finish the calculation of the MAC of a message and compare it with
1569 * an expected value.
1570 *
1571 * The application must call psa_mac_verify_setup() before calling this function.
1572 * This function calculates the MAC of the message formed by concatenating
1573 * the inputs passed to preceding calls to psa_mac_update(). It then
1574 * compares the calculated MAC with the expected MAC passed as a
1575 * parameter to this function.
1576 *
1577 * When this function returns, the operation becomes inactive.
1578 *
1579 * \note Implementations shall make the best effort to ensure that the
1580 * comparison between the actual MAC and the expected MAC is performed
1581 * in constant time.
1582 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001583 * \param[in,out] operation Active MAC operation.
1584 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001585 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001586 *
1587 * \retval #PSA_SUCCESS
1588 * The expected MAC is identical to the actual MAC of the message.
1589 * \retval #PSA_ERROR_INVALID_SIGNATURE
1590 * The MAC of the message was calculated successfully, but it
1591 * differs from the expected MAC.
1592 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001593 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001594 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1595 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1596 * \retval #PSA_ERROR_HARDWARE_FAILURE
1597 * \retval #PSA_ERROR_TAMPERING_DETECTED
1598 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001599psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1600 const uint8_t *mac,
1601 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001602
Gilles Peskinedcd14942018-07-12 00:30:52 +02001603/** Abort a MAC operation.
1604 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001605 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001606 * \p operation structure itself. Once aborted, the operation object
1607 * can be reused for another operation by calling
1608 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001609 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001610 * You may call this function any time after the operation object has
1611 * been initialized by any of the following methods:
1612 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1613 * it succeeds or not.
1614 * - Initializing the \c struct to all-bits-zero.
1615 * - Initializing the \c struct to logical zeros, e.g.
1616 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001617 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001618 * In particular, calling psa_mac_abort() after the operation has been
1619 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1620 * psa_mac_verify_finish() is safe and has no effect.
1621 *
1622 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001623 *
1624 * \retval #PSA_SUCCESS
1625 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001626 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001627 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1628 * \retval #PSA_ERROR_HARDWARE_FAILURE
1629 * \retval #PSA_ERROR_TAMPERING_DETECTED
1630 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001631psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1632
1633/**@}*/
1634
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001635/** \defgroup cipher Symmetric ciphers
1636 * @{
1637 */
1638
Gilles Peskine69647a42019-01-14 20:18:12 +01001639/** Encrypt a message using a symmetric cipher.
1640 *
1641 * This function encrypts a message with a random IV (initialization
1642 * vector).
1643 *
1644 * \param handle Handle to the key to use for the operation.
1645 * It must remain valid until the operation
1646 * terminates.
1647 * \param alg The cipher algorithm to compute
1648 * (\c PSA_ALG_XXX value such that
1649 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1650 * \param[in] input Buffer containing the message to encrypt.
1651 * \param input_length Size of the \p input buffer in bytes.
1652 * \param[out] output Buffer where the output is to be written.
1653 * The output contains the IV followed by
1654 * the ciphertext proper.
1655 * \param output_size Size of the \p output buffer in bytes.
1656 * \param[out] output_length On success, the number of bytes
1657 * that make up the output.
1658 *
1659 * \retval #PSA_SUCCESS
1660 * Success.
1661 * \retval #PSA_ERROR_INVALID_HANDLE
1662 * \retval #PSA_ERROR_EMPTY_SLOT
1663 * \retval #PSA_ERROR_NOT_PERMITTED
1664 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001665 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001666 * \retval #PSA_ERROR_NOT_SUPPORTED
1667 * \p alg is not supported or is not a cipher algorithm.
1668 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1669 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1670 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1671 * \retval #PSA_ERROR_HARDWARE_FAILURE
1672 * \retval #PSA_ERROR_TAMPERING_DETECTED
1673 */
1674psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1675 psa_algorithm_t alg,
1676 const uint8_t *input,
1677 size_t input_length,
1678 uint8_t *output,
1679 size_t output_size,
1680 size_t *output_length);
1681
1682/** Decrypt a message using a symmetric cipher.
1683 *
1684 * This function decrypts a message encrypted with a symmetric cipher.
1685 *
1686 * \param handle Handle to the key to use for the operation.
1687 * It must remain valid until the operation
1688 * terminates.
1689 * \param alg The cipher algorithm to compute
1690 * (\c PSA_ALG_XXX value such that
1691 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1692 * \param[in] input Buffer containing the message to decrypt.
1693 * This consists of the IV followed by the
1694 * ciphertext proper.
1695 * \param input_length Size of the \p input buffer in bytes.
1696 * \param[out] output Buffer where the plaintext is to be written.
1697 * \param output_size Size of the \p output buffer in bytes.
1698 * \param[out] output_length On success, the number of bytes
1699 * that make up the output.
1700 *
1701 * \retval #PSA_SUCCESS
1702 * Success.
1703 * \retval #PSA_ERROR_INVALID_HANDLE
1704 * \retval #PSA_ERROR_EMPTY_SLOT
1705 * \retval #PSA_ERROR_NOT_PERMITTED
1706 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001707 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001708 * \retval #PSA_ERROR_NOT_SUPPORTED
1709 * \p alg is not supported or is not a cipher algorithm.
1710 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1711 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1712 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1713 * \retval #PSA_ERROR_HARDWARE_FAILURE
1714 * \retval #PSA_ERROR_TAMPERING_DETECTED
1715 */
1716psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1717 psa_algorithm_t alg,
1718 const uint8_t *input,
1719 size_t input_length,
1720 uint8_t *output,
1721 size_t output_size,
1722 size_t *output_length);
1723
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001724/** The type of the state data structure for multipart cipher operations.
1725 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001726 * Before calling any function on a cipher operation object, the application
1727 * must initialize it by any of the following means:
1728 * - Set the structure to all-bits-zero, for example:
1729 * \code
1730 * psa_cipher_operation_t operation;
1731 * memset(&operation, 0, sizeof(operation));
1732 * \endcode
1733 * - Initialize the structure to logical zero values, for example:
1734 * \code
1735 * psa_cipher_operation_t operation = {0};
1736 * \endcode
1737 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1738 * for example:
1739 * \code
1740 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1741 * \endcode
1742 * - Assign the result of the function psa_cipher_operation_init()
1743 * to the structure, for example:
1744 * \code
1745 * psa_cipher_operation_t operation;
1746 * operation = psa_cipher_operation_init();
1747 * \endcode
1748 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001749 * This is an implementation-defined \c struct. Applications should not
1750 * make any assumptions about the content of this structure except
1751 * as directed by the documentation of a specific implementation. */
1752typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1753
Jaeden Amero5bae2272019-01-04 11:48:27 +00001754/** \def PSA_CIPHER_OPERATION_INIT
1755 *
1756 * This macro returns a suitable initializer for a cipher operation object of
1757 * type #psa_cipher_operation_t.
1758 */
1759#ifdef __DOXYGEN_ONLY__
1760/* This is an example definition for documentation purposes.
1761 * Implementations should define a suitable value in `crypto_struct.h`.
1762 */
1763#define PSA_CIPHER_OPERATION_INIT {0}
1764#endif
1765
1766/** Return an initial value for a cipher operation object.
1767 */
1768static psa_cipher_operation_t psa_cipher_operation_init(void);
1769
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001770/** Set the key for a multipart symmetric encryption operation.
1771 *
1772 * The sequence of operations to encrypt a message with a symmetric cipher
1773 * is as follows:
1774 * -# Allocate an operation object which will be passed to all the functions
1775 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001776 * -# Initialize the operation object with one of the methods described in the
1777 * documentation for #psa_cipher_operation_t, e.g.
1778 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001779 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001780 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001781 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001782 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001783 * requires a specific IV value.
1784 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1785 * of the message each time.
1786 * -# Call psa_cipher_finish().
1787 *
1788 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001789 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001790 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001791 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001792 * eventually terminate the operation. The following events terminate an
1793 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001794 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001795 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001796 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001797 * \param[in,out] operation The operation object to set up. It must have
1798 * been initialized as per the documentation for
1799 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001800 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001801 * It must remain valid until the operation
1802 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001803 * \param alg The cipher algorithm to compute
1804 * (\c PSA_ALG_XXX value such that
1805 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001806 *
Gilles Peskine28538492018-07-11 17:34:00 +02001807 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001808 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001809 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001810 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001811 * \retval #PSA_ERROR_NOT_PERMITTED
1812 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001813 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001814 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001815 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001816 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1817 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1818 * \retval #PSA_ERROR_HARDWARE_FAILURE
1819 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001820 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001821 * The operation state is not valid (already set up and not
1822 * subsequently completed).
1823 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001824 * The library has not been previously initialized by psa_crypto_init().
1825 * It is implementation-dependent whether a failure to initialize
1826 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001827 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001828psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001829 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001830 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001831
1832/** Set the key for a multipart symmetric decryption operation.
1833 *
1834 * The sequence of operations to decrypt a message with a symmetric cipher
1835 * is as follows:
1836 * -# Allocate an operation object which will be passed to all the functions
1837 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001838 * -# Initialize the operation object with one of the methods described in the
1839 * documentation for #psa_cipher_operation_t, e.g.
1840 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001841 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001842 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001843 * decryption. If the IV is prepended to the ciphertext, you can call
1844 * psa_cipher_update() on a buffer containing the IV followed by the
1845 * beginning of the message.
1846 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1847 * of the message each time.
1848 * -# Call psa_cipher_finish().
1849 *
1850 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001851 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001852 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001853 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001854 * eventually terminate the operation. The following events terminate an
1855 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001856 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001857 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001858 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001859 * \param[in,out] operation The operation object to set up. It must have
1860 * been initialized as per the documentation for
1861 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001862 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001863 * It must remain valid until the operation
1864 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001865 * \param alg The cipher algorithm to compute
1866 * (\c PSA_ALG_XXX value such that
1867 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001868 *
Gilles Peskine28538492018-07-11 17:34:00 +02001869 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001870 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001871 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001872 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001873 * \retval #PSA_ERROR_NOT_PERMITTED
1874 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001875 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001876 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001877 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001878 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1879 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1880 * \retval #PSA_ERROR_HARDWARE_FAILURE
1881 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001882 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001883 * The operation state is not valid (already set up and not
1884 * subsequently completed).
1885 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001886 * The library has not been previously initialized by psa_crypto_init().
1887 * It is implementation-dependent whether a failure to initialize
1888 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001889 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001890psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001891 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001892 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001893
Gilles Peskinedcd14942018-07-12 00:30:52 +02001894/** Generate an IV for a symmetric encryption operation.
1895 *
1896 * This function generates a random IV (initialization vector), nonce
1897 * or initial counter value for the encryption operation as appropriate
1898 * for the chosen algorithm, key type and key size.
1899 *
1900 * The application must call psa_cipher_encrypt_setup() before
1901 * calling this function.
1902 *
1903 * If this function returns an error status, the operation becomes inactive.
1904 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001905 * \param[in,out] operation Active cipher operation.
1906 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001907 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001908 * \param[out] iv_length On success, the number of bytes of the
1909 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001910 *
1911 * \retval #PSA_SUCCESS
1912 * Success.
1913 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001914 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001915 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001916 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001917 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1918 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1919 * \retval #PSA_ERROR_HARDWARE_FAILURE
1920 * \retval #PSA_ERROR_TAMPERING_DETECTED
1921 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001922psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1923 unsigned char *iv,
1924 size_t iv_size,
1925 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001926
Gilles Peskinedcd14942018-07-12 00:30:52 +02001927/** Set the IV for a symmetric encryption or decryption operation.
1928 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001929 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001930 * or initial counter value for the encryption or decryption operation.
1931 *
1932 * The application must call psa_cipher_encrypt_setup() before
1933 * calling this function.
1934 *
1935 * If this function returns an error status, the operation becomes inactive.
1936 *
1937 * \note When encrypting, applications should use psa_cipher_generate_iv()
1938 * instead of this function, unless implementing a protocol that requires
1939 * a non-random IV.
1940 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001941 * \param[in,out] operation Active cipher operation.
1942 * \param[in] iv Buffer containing the IV to use.
1943 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001944 *
1945 * \retval #PSA_SUCCESS
1946 * Success.
1947 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001948 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001949 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001950 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001951 * or the chosen algorithm does not use an IV.
1952 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1953 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1954 * \retval #PSA_ERROR_HARDWARE_FAILURE
1955 * \retval #PSA_ERROR_TAMPERING_DETECTED
1956 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001957psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1958 const unsigned char *iv,
1959 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001960
Gilles Peskinedcd14942018-07-12 00:30:52 +02001961/** Encrypt or decrypt a message fragment in an active cipher operation.
1962 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001963 * Before calling this function, you must:
1964 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1965 * The choice of setup function determines whether this function
1966 * encrypts or decrypts its input.
1967 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1968 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001969 *
1970 * If this function returns an error status, the operation becomes inactive.
1971 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001972 * \param[in,out] operation Active cipher operation.
1973 * \param[in] input Buffer containing the message fragment to
1974 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001975 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001976 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001977 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001978 * \param[out] output_length On success, the number of bytes
1979 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001980 *
1981 * \retval #PSA_SUCCESS
1982 * Success.
1983 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001984 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001985 * not set, or already completed).
1986 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1987 * The size of the \p output buffer is too small.
1988 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1989 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1990 * \retval #PSA_ERROR_HARDWARE_FAILURE
1991 * \retval #PSA_ERROR_TAMPERING_DETECTED
1992 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001993psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1994 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001995 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001996 unsigned char *output,
1997 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001998 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001999
Gilles Peskinedcd14942018-07-12 00:30:52 +02002000/** Finish encrypting or decrypting a message in a cipher operation.
2001 *
2002 * The application must call psa_cipher_encrypt_setup() or
2003 * psa_cipher_decrypt_setup() before calling this function. The choice
2004 * of setup function determines whether this function encrypts or
2005 * decrypts its input.
2006 *
2007 * This function finishes the encryption or decryption of the message
2008 * formed by concatenating the inputs passed to preceding calls to
2009 * psa_cipher_update().
2010 *
2011 * When this function returns, the operation becomes inactive.
2012 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002013 * \param[in,out] operation Active cipher operation.
2014 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002015 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002016 * \param[out] output_length On success, the number of bytes
2017 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002018 *
2019 * \retval #PSA_SUCCESS
2020 * Success.
2021 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002022 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02002023 * not set, or already completed).
2024 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2025 * The size of the \p output buffer is too small.
2026 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2027 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2028 * \retval #PSA_ERROR_HARDWARE_FAILURE
2029 * \retval #PSA_ERROR_TAMPERING_DETECTED
2030 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002031psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002032 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002033 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002034 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002035
Gilles Peskinedcd14942018-07-12 00:30:52 +02002036/** Abort a cipher operation.
2037 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002038 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002039 * \p operation structure itself. Once aborted, the operation object
2040 * can be reused for another operation by calling
2041 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002042 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002043 * You may call this function any time after the operation object has
2044 * been initialized by any of the following methods:
2045 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2046 * whether it succeeds or not.
2047 * - Initializing the \c struct to all-bits-zero.
2048 * - Initializing the \c struct to logical zeros, e.g.
2049 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002050 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002051 * In particular, calling psa_cipher_abort() after the operation has been
2052 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2053 * is safe and has no effect.
2054 *
2055 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002056 *
2057 * \retval #PSA_SUCCESS
2058 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002059 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002060 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2061 * \retval #PSA_ERROR_HARDWARE_FAILURE
2062 * \retval #PSA_ERROR_TAMPERING_DETECTED
2063 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002064psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2065
2066/**@}*/
2067
Gilles Peskine3b555712018-03-03 21:27:57 +01002068/** \defgroup aead Authenticated encryption with associated data (AEAD)
2069 * @{
2070 */
2071
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002072/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002073 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002074 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002075 * \param alg The AEAD algorithm to compute
2076 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002077 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002078 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002079 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002080 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002081 * but not encrypted.
2082 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002083 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002084 * encrypted.
2085 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002086 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002087 * encrypted data. The additional data is not
2088 * part of this output. For algorithms where the
2089 * encrypted data and the authentication tag
2090 * are defined as separate outputs, the
2091 * authentication tag is appended to the
2092 * encrypted data.
2093 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2094 * This must be at least
2095 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2096 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002097 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002098 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002099 *
Gilles Peskine28538492018-07-11 17:34:00 +02002100 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002101 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002102 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002103 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002104 * \retval #PSA_ERROR_NOT_PERMITTED
2105 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002106 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002107 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002108 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002109 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2110 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2111 * \retval #PSA_ERROR_HARDWARE_FAILURE
2112 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002113 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002114 * The library has not been previously initialized by psa_crypto_init().
2115 * It is implementation-dependent whether a failure to initialize
2116 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002117 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002118psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002119 psa_algorithm_t alg,
2120 const uint8_t *nonce,
2121 size_t nonce_length,
2122 const uint8_t *additional_data,
2123 size_t additional_data_length,
2124 const uint8_t *plaintext,
2125 size_t plaintext_length,
2126 uint8_t *ciphertext,
2127 size_t ciphertext_size,
2128 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002129
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002130/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002131 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002132 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002133 * \param alg The AEAD algorithm to compute
2134 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002135 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002136 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002137 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002138 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002139 * but not encrypted.
2140 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002141 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002142 * encrypted. For algorithms where the
2143 * encrypted data and the authentication tag
2144 * are defined as separate inputs, the buffer
2145 * must contain the encrypted data followed
2146 * by the authentication tag.
2147 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002148 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002149 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2150 * This must be at least
2151 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2152 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002153 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002154 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002155 *
Gilles Peskine28538492018-07-11 17:34:00 +02002156 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002157 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002158 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002159 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002160 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002161 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002162 * \retval #PSA_ERROR_NOT_PERMITTED
2163 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002164 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002165 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002166 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002167 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2168 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2169 * \retval #PSA_ERROR_HARDWARE_FAILURE
2170 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002171 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002172 * The library has not been previously initialized by psa_crypto_init().
2173 * It is implementation-dependent whether a failure to initialize
2174 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002175 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002176psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002177 psa_algorithm_t alg,
2178 const uint8_t *nonce,
2179 size_t nonce_length,
2180 const uint8_t *additional_data,
2181 size_t additional_data_length,
2182 const uint8_t *ciphertext,
2183 size_t ciphertext_length,
2184 uint8_t *plaintext,
2185 size_t plaintext_size,
2186 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002187
Gilles Peskine30a9e412019-01-14 18:36:12 +01002188/** The type of the state data structure for multipart AEAD operations.
2189 *
2190 * Before calling any function on an AEAD operation object, the application
2191 * must initialize it by any of the following means:
2192 * - Set the structure to all-bits-zero, for example:
2193 * \code
2194 * psa_aead_operation_t operation;
2195 * memset(&operation, 0, sizeof(operation));
2196 * \endcode
2197 * - Initialize the structure to logical zero values, for example:
2198 * \code
2199 * psa_aead_operation_t operation = {0};
2200 * \endcode
2201 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2202 * for example:
2203 * \code
2204 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2205 * \endcode
2206 * - Assign the result of the function psa_aead_operation_init()
2207 * to the structure, for example:
2208 * \code
2209 * psa_aead_operation_t operation;
2210 * operation = psa_aead_operation_init();
2211 * \endcode
2212 *
2213 * This is an implementation-defined \c struct. Applications should not
2214 * make any assumptions about the content of this structure except
2215 * as directed by the documentation of a specific implementation. */
2216typedef struct psa_aead_operation_s psa_aead_operation_t;
2217
2218/** \def PSA_AEAD_OPERATION_INIT
2219 *
2220 * This macro returns a suitable initializer for an AEAD operation object of
2221 * type #psa_aead_operation_t.
2222 */
2223#ifdef __DOXYGEN_ONLY__
2224/* This is an example definition for documentation purposes.
2225 * Implementations should define a suitable value in `crypto_struct.h`.
2226 */
2227#define PSA_AEAD_OPERATION_INIT {0}
2228#endif
2229
2230/** Return an initial value for an AEAD operation object.
2231 */
2232static psa_aead_operation_t psa_aead_operation_init(void);
2233
2234/** Set the key for a multipart authenticated encryption operation.
2235 *
2236 * The sequence of operations to encrypt a message with authentication
2237 * is as follows:
2238 * -# Allocate an operation object which will be passed to all the functions
2239 * listed here.
2240 * -# Initialize the operation object with one of the methods described in the
2241 * documentation for #psa_aead_operation_t, e.g.
2242 * PSA_AEAD_OPERATION_INIT.
2243 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002244 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2245 * inputs to the subsequent calls to psa_aead_update_ad() and
2246 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2247 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002248 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2249 * generate or set the nonce. You should use
2250 * psa_aead_generate_nonce() unless the protocol you are implementing
2251 * requires a specific nonce value.
2252 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2253 * of the non-encrypted additional authenticated data each time.
2254 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002255 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002256 * -# Call psa_aead_finish().
2257 *
2258 * The application may call psa_aead_abort() at any time after the operation
2259 * has been initialized.
2260 *
2261 * After a successful call to psa_aead_encrypt_setup(), the application must
2262 * eventually terminate the operation. The following events terminate an
2263 * operation:
2264 * - A failed call to any of the \c psa_aead_xxx functions.
2265 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2266 *
2267 * \param[in,out] operation The operation object to set up. It must have
2268 * been initialized as per the documentation for
2269 * #psa_aead_operation_t and not yet in use.
2270 * \param handle Handle to the key to use for the operation.
2271 * It must remain valid until the operation
2272 * terminates.
2273 * \param alg The AEAD algorithm to compute
2274 * (\c PSA_ALG_XXX value such that
2275 * #PSA_ALG_IS_AEAD(\p alg) is true).
2276 *
2277 * \retval #PSA_SUCCESS
2278 * Success.
2279 * \retval #PSA_ERROR_INVALID_HANDLE
2280 * \retval #PSA_ERROR_EMPTY_SLOT
2281 * \retval #PSA_ERROR_NOT_PERMITTED
2282 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002283 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002284 * \retval #PSA_ERROR_NOT_SUPPORTED
2285 * \p alg is not supported or is not an AEAD algorithm.
2286 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2287 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2288 * \retval #PSA_ERROR_HARDWARE_FAILURE
2289 * \retval #PSA_ERROR_TAMPERING_DETECTED
2290 * \retval #PSA_ERROR_BAD_STATE
2291 * The library has not been previously initialized by psa_crypto_init().
2292 * It is implementation-dependent whether a failure to initialize
2293 * results in this error code.
2294 */
2295psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2296 psa_key_handle_t handle,
2297 psa_algorithm_t alg);
2298
2299/** Set the key for a multipart authenticated decryption operation.
2300 *
2301 * The sequence of operations to decrypt a message with authentication
2302 * is as follows:
2303 * -# Allocate an operation object which will be passed to all the functions
2304 * listed here.
2305 * -# Initialize the operation object with one of the methods described in the
2306 * documentation for #psa_aead_operation_t, e.g.
2307 * PSA_AEAD_OPERATION_INIT.
2308 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002309 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2310 * inputs to the subsequent calls to psa_aead_update_ad() and
2311 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2312 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002313 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2314 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2315 * of the non-encrypted additional authenticated data each time.
2316 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002317 * of the ciphertext to decrypt each time.
2318 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002319 *
2320 * The application may call psa_aead_abort() at any time after the operation
2321 * has been initialized.
2322 *
2323 * After a successful call to psa_aead_decrypt_setup(), the application must
2324 * eventually terminate the operation. The following events terminate an
2325 * operation:
2326 * - A failed call to any of the \c psa_aead_xxx functions.
2327 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2328 *
2329 * \param[in,out] operation The operation object to set up. It must have
2330 * been initialized as per the documentation for
2331 * #psa_aead_operation_t and not yet in use.
2332 * \param handle Handle to the key to use for the operation.
2333 * It must remain valid until the operation
2334 * terminates.
2335 * \param alg The AEAD algorithm to compute
2336 * (\c PSA_ALG_XXX value such that
2337 * #PSA_ALG_IS_AEAD(\p alg) is true).
2338 *
2339 * \retval #PSA_SUCCESS
2340 * Success.
2341 * \retval #PSA_ERROR_INVALID_HANDLE
2342 * \retval #PSA_ERROR_EMPTY_SLOT
2343 * \retval #PSA_ERROR_NOT_PERMITTED
2344 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002345 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002346 * \retval #PSA_ERROR_NOT_SUPPORTED
2347 * \p alg is not supported or is not an AEAD algorithm.
2348 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2349 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2350 * \retval #PSA_ERROR_HARDWARE_FAILURE
2351 * \retval #PSA_ERROR_TAMPERING_DETECTED
2352 * \retval #PSA_ERROR_BAD_STATE
2353 * The library has not been previously initialized by psa_crypto_init().
2354 * It is implementation-dependent whether a failure to initialize
2355 * results in this error code.
2356 */
2357psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2358 psa_key_handle_t handle,
2359 psa_algorithm_t alg);
2360
2361/** Generate a random nonce for an authenticated encryption operation.
2362 *
2363 * This function generates a random nonce for the authenticated encryption
2364 * operation with an appropriate size for the chosen algorithm, key type
2365 * and key size.
2366 *
2367 * The application must call psa_aead_encrypt_setup() before
2368 * calling this function.
2369 *
2370 * If this function returns an error status, the operation becomes inactive.
2371 *
2372 * \param[in,out] operation Active AEAD operation.
2373 * \param[out] nonce Buffer where the generated nonce is to be
2374 * written.
2375 * \param nonce_size Size of the \p nonce buffer in bytes.
2376 * \param[out] nonce_length On success, the number of bytes of the
2377 * generated nonce.
2378 *
2379 * \retval #PSA_SUCCESS
2380 * Success.
2381 * \retval #PSA_ERROR_BAD_STATE
2382 * The operation state is not valid (not set up, or nonce already set).
2383 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2384 * The size of the \p nonce buffer is too small.
2385 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2386 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2387 * \retval #PSA_ERROR_HARDWARE_FAILURE
2388 * \retval #PSA_ERROR_TAMPERING_DETECTED
2389 */
2390psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2391 unsigned char *nonce,
2392 size_t nonce_size,
2393 size_t *nonce_length);
2394
2395/** Set the nonce for an authenticated encryption or decryption operation.
2396 *
2397 * This function sets the nonce for the authenticated
2398 * encryption or decryption operation.
2399 *
2400 * The application must call psa_aead_encrypt_setup() before
2401 * calling this function.
2402 *
2403 * If this function returns an error status, the operation becomes inactive.
2404 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002405 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002406 * instead of this function, unless implementing a protocol that requires
2407 * a non-random IV.
2408 *
2409 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002410 * \param[in] nonce Buffer containing the nonce to use.
2411 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002412 *
2413 * \retval #PSA_SUCCESS
2414 * Success.
2415 * \retval #PSA_ERROR_BAD_STATE
2416 * The operation state is not valid (not set up, or nonce already set).
2417 * \retval #PSA_ERROR_INVALID_ARGUMENT
2418 * The size of \p nonce is not acceptable for the chosen algorithm.
2419 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2420 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2421 * \retval #PSA_ERROR_HARDWARE_FAILURE
2422 * \retval #PSA_ERROR_TAMPERING_DETECTED
2423 */
2424psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2425 const unsigned char *nonce,
2426 size_t nonce_length);
2427
Gilles Peskinebc59c852019-01-17 15:26:08 +01002428/** Declare the lengths of the message and additional data for AEAD.
2429 *
2430 * The application must call this function before calling
2431 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2432 * the operation requires it. If the algorithm does not require it,
2433 * calling this function is optional, but if this function is called
2434 * then the implementation must enforce the lengths.
2435 *
2436 * You may call this function before or after setting the nonce with
2437 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2438 *
2439 * - For #PSA_ALG_CCM, calling this function is required.
2440 * - For the other AEAD algorithms defined in this specification, calling
2441 * this function is not required.
2442 * - For vendor-defined algorithm, refer to the vendor documentation.
2443 *
2444 * \param[in,out] operation Active AEAD operation.
2445 * \param ad_length Size of the non-encrypted additional
2446 * authenticated data in bytes.
2447 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2448 *
2449 * \retval #PSA_SUCCESS
2450 * Success.
2451 * \retval #PSA_ERROR_BAD_STATE
2452 * The operation state is not valid (not set up, already completed,
2453 * or psa_aead_update_ad() or psa_aead_update() already called).
2454 * \retval #PSA_ERROR_INVALID_ARGUMENT
2455 * At least one of the lengths is not acceptable for the chosen
2456 * algorithm.
2457 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2458 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2459 * \retval #PSA_ERROR_HARDWARE_FAILURE
2460 * \retval #PSA_ERROR_TAMPERING_DETECTED
2461 */
2462psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2463 size_t ad_length,
2464 size_t plaintext_length);
2465
Gilles Peskine30a9e412019-01-14 18:36:12 +01002466/** Pass additional data to an active AEAD operation.
2467 *
2468 * Additional data is authenticated, but not encrypted.
2469 *
2470 * You may call this function multiple times to pass successive fragments
2471 * of the additional data. You may not call this function after passing
2472 * data to encrypt or decrypt with psa_aead_update().
2473 *
2474 * Before calling this function, you must:
2475 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2476 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2477 *
2478 * If this function returns an error status, the operation becomes inactive.
2479 *
2480 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2481 * there is no guarantee that the input is valid. Therefore, until
2482 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2483 * treat the input as untrusted and prepare to undo any action that
2484 * depends on the input if psa_aead_verify() returns an error status.
2485 *
2486 * \param[in,out] operation Active AEAD operation.
2487 * \param[in] input Buffer containing the fragment of
2488 * additional data.
2489 * \param input_length Size of the \p input buffer in bytes.
2490 *
2491 * \retval #PSA_SUCCESS
2492 * Success.
2493 * \retval #PSA_ERROR_BAD_STATE
2494 * The operation state is not valid (not set up, nonce not set,
2495 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002496 * \retval #PSA_ERROR_INVALID_ARGUMENT
2497 * The total input length overflows the additional data length that
2498 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002499 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2500 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2501 * \retval #PSA_ERROR_HARDWARE_FAILURE
2502 * \retval #PSA_ERROR_TAMPERING_DETECTED
2503 */
2504psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2505 const uint8_t *input,
2506 size_t input_length);
2507
2508/** Encrypt or decrypt a message fragment in an active AEAD operation.
2509 *
2510 * Before calling this function, you must:
2511 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2512 * The choice of setup function determines whether this function
2513 * encrypts or decrypts its input.
2514 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2515 * 3. Call psa_aead_update_ad() to pass all the additional data.
2516 *
2517 * If this function returns an error status, the operation becomes inactive.
2518 *
2519 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2520 * there is no guarantee that the input is valid. Therefore, until
2521 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2522 * - Do not use the output in any way other than storing it in a
2523 * confidential location. If you take any action that depends
2524 * on the tentative decrypted data, this action will need to be
2525 * undone if the input turns out not to be valid. Furthermore,
2526 * if an adversary can observe that this action took place
2527 * (for example through timing), they may be able to use this
2528 * fact as an oracle to decrypt any message encrypted with the
2529 * same key.
2530 * - In particular, do not copy the output anywhere but to a
2531 * memory or storage space that you have exclusive access to.
2532 *
2533 * \param[in,out] operation Active AEAD operation.
2534 * \param[in] input Buffer containing the message fragment to
2535 * encrypt or decrypt.
2536 * \param input_length Size of the \p input buffer in bytes.
2537 * \param[out] output Buffer where the output is to be written.
2538 * \param output_size Size of the \p output buffer in bytes.
2539 * \param[out] output_length On success, the number of bytes
2540 * that make up the returned output.
2541 *
2542 * \retval #PSA_SUCCESS
2543 * Success.
2544 * \retval #PSA_ERROR_BAD_STATE
2545 * The operation state is not valid (not set up, nonce not set
2546 * or already completed).
2547 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2548 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002549 * \retval #PSA_ERROR_INVALID_ARGUMENT
2550 * The total length of input to psa_aead_update_ad() so far is
2551 * less than the additional data length that was previously
2552 * specified with psa_aead_set_lengths().
2553 * \retval #PSA_ERROR_INVALID_ARGUMENT
2554 * The total input length overflows the plaintext length that
2555 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002556 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2557 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2558 * \retval #PSA_ERROR_HARDWARE_FAILURE
2559 * \retval #PSA_ERROR_TAMPERING_DETECTED
2560 */
2561psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2562 const uint8_t *input,
2563 size_t input_length,
2564 unsigned char *output,
2565 size_t output_size,
2566 size_t *output_length);
2567
2568/** Finish encrypting a message in an AEAD operation.
2569 *
2570 * The operation must have been set up with psa_aead_encrypt_setup().
2571 *
2572 * This function finishes the authentication of the additional data
2573 * formed by concatenating the inputs passed to preceding calls to
2574 * psa_aead_update_ad() with the plaintext formed by concatenating the
2575 * inputs passed to preceding calls to psa_aead_update().
2576 *
2577 * This function has two output buffers:
2578 * - \p ciphertext contains trailing ciphertext that was buffered from
2579 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2580 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2581 * will not contain any output and can be a 0-sized buffer.
2582 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002583 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002584 * that the operation performs.
2585 *
2586 * When this function returns, the operation becomes inactive.
2587 *
2588 * \param[in,out] operation Active AEAD operation.
2589 * \param[out] ciphertext Buffer where the last part of the ciphertext
2590 * is to be written.
2591 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2592 * \param[out] ciphertext_length On success, the number of bytes of
2593 * returned ciphertext.
2594 * \param[out] tag Buffer where the authentication tag is
2595 * to be written.
2596 * \param tag_size Size of the \p tag buffer in bytes.
2597 * \param[out] tag_length On success, the number of bytes
2598 * that make up the returned tag.
2599 *
2600 * \retval #PSA_SUCCESS
2601 * Success.
2602 * \retval #PSA_ERROR_BAD_STATE
2603 * The operation state is not valid (not set up, nonce not set,
2604 * decryption, or already completed).
2605 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002606 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002607 * \retval #PSA_ERROR_INVALID_ARGUMENT
2608 * The total length of input to psa_aead_update_ad() so far is
2609 * less than the additional data length that was previously
2610 * specified with psa_aead_set_lengths().
2611 * \retval #PSA_ERROR_INVALID_ARGUMENT
2612 * The total length of input to psa_aead_update() so far is
2613 * less than the plaintext length that was previously
2614 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002615 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2616 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2617 * \retval #PSA_ERROR_HARDWARE_FAILURE
2618 * \retval #PSA_ERROR_TAMPERING_DETECTED
2619 */
2620psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002621 uint8_t *ciphertext,
2622 size_t ciphertext_size,
2623 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002624 uint8_t *tag,
2625 size_t tag_size,
2626 size_t *tag_length);
2627
2628/** Finish authenticating and decrypting a message in an AEAD operation.
2629 *
2630 * The operation must have been set up with psa_aead_decrypt_setup().
2631 *
2632 * This function finishes the authentication of the additional data
2633 * formed by concatenating the inputs passed to preceding calls to
2634 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2635 * inputs passed to preceding calls to psa_aead_update().
2636 *
2637 * When this function returns, the operation becomes inactive.
2638 *
2639 * \param[in,out] operation Active AEAD operation.
2640 * \param[in] tag Buffer containing the authentication tag.
2641 * \param tag_length Size of the \p tag buffer in bytes.
2642 *
2643 * \retval #PSA_SUCCESS
2644 * Success.
2645 * \retval #PSA_ERROR_BAD_STATE
2646 * The operation state is not valid (not set up, nonce not set,
2647 * encryption, or already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002648 * \retval #PSA_ERROR_INVALID_ARGUMENT
2649 * The total length of input to psa_aead_update_ad() so far is
2650 * less than the additional data length that was previously
2651 * specified with psa_aead_set_lengths().
2652 * \retval #PSA_ERROR_INVALID_ARGUMENT
2653 * The total length of input to psa_aead_update() so far is
2654 * less than the plaintext length that was previously
2655 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002656 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2657 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2658 * \retval #PSA_ERROR_HARDWARE_FAILURE
2659 * \retval #PSA_ERROR_TAMPERING_DETECTED
2660 */
2661psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2662 const uint8_t *tag,
2663 size_t tag_length);
2664
2665/** Abort an AEAD operation.
2666 *
2667 * Aborting an operation frees all associated resources except for the
2668 * \p operation structure itself. Once aborted, the operation object
2669 * can be reused for another operation by calling
2670 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2671 *
2672 * You may call this function any time after the operation object has
2673 * been initialized by any of the following methods:
2674 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2675 * whether it succeeds or not.
2676 * - Initializing the \c struct to all-bits-zero.
2677 * - Initializing the \c struct to logical zeros, e.g.
2678 * `psa_aead_operation_t operation = {0}`.
2679 *
2680 * In particular, calling psa_aead_abort() after the operation has been
2681 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2682 * is safe and has no effect.
2683 *
2684 * \param[in,out] operation Initialized AEAD operation.
2685 *
2686 * \retval #PSA_SUCCESS
2687 * \retval #PSA_ERROR_BAD_STATE
2688 * \p operation is not an active AEAD operation.
2689 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2690 * \retval #PSA_ERROR_HARDWARE_FAILURE
2691 * \retval #PSA_ERROR_TAMPERING_DETECTED
2692 */
2693psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2694
Gilles Peskine3b555712018-03-03 21:27:57 +01002695/**@}*/
2696
Gilles Peskine20035e32018-02-03 22:44:14 +01002697/** \defgroup asymmetric Asymmetric cryptography
2698 * @{
2699 */
2700
2701/**
2702 * \brief Sign a hash or short message with a private key.
2703 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002704 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002705 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002706 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2707 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2708 * to determine the hash algorithm to use.
2709 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002710 * \param handle Handle to the key to use for the operation.
2711 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002712 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002713 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002714 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002715 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002716 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002717 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002718 * \param[out] signature_length On success, the number of bytes
2719 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002720 *
Gilles Peskine28538492018-07-11 17:34:00 +02002721 * \retval #PSA_SUCCESS
2722 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002723 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002724 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002725 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002726 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002727 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002728 * \retval #PSA_ERROR_NOT_SUPPORTED
2729 * \retval #PSA_ERROR_INVALID_ARGUMENT
2730 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2731 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2732 * \retval #PSA_ERROR_HARDWARE_FAILURE
2733 * \retval #PSA_ERROR_TAMPERING_DETECTED
2734 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002735 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002736 * The library has not been previously initialized by psa_crypto_init().
2737 * It is implementation-dependent whether a failure to initialize
2738 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002739 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002740psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002741 psa_algorithm_t alg,
2742 const uint8_t *hash,
2743 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002744 uint8_t *signature,
2745 size_t signature_size,
2746 size_t *signature_length);
2747
2748/**
2749 * \brief Verify the signature a hash or short message using a public key.
2750 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002751 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002752 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002753 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2754 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2755 * to determine the hash algorithm to use.
2756 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002757 * \param handle Handle to the key to use for the operation.
2758 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002759 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002760 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002761 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002762 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002763 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002764 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002765 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002766 *
Gilles Peskine28538492018-07-11 17:34:00 +02002767 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002768 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002769 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002770 * The calculation was perfomed successfully, but the passed
2771 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002772 * \retval #PSA_ERROR_NOT_SUPPORTED
2773 * \retval #PSA_ERROR_INVALID_ARGUMENT
2774 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2775 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2776 * \retval #PSA_ERROR_HARDWARE_FAILURE
2777 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002778 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002779 * The library has not been previously initialized by psa_crypto_init().
2780 * It is implementation-dependent whether a failure to initialize
2781 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002782 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002783psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002784 psa_algorithm_t alg,
2785 const uint8_t *hash,
2786 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002787 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002788 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002789
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002790/**
2791 * \brief Encrypt a short message with a public key.
2792 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002793 * \param handle Handle to the key to use for the operation.
2794 * It must be a public key or an asymmetric
2795 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002796 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002797 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002798 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002799 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002800 * \param[in] salt A salt or label, if supported by the
2801 * encryption algorithm.
2802 * If the algorithm does not support a
2803 * salt, pass \c NULL.
2804 * If the algorithm supports an optional
2805 * salt and you do not want to pass a salt,
2806 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002807 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002808 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2809 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002810 * \param salt_length Size of the \p salt buffer in bytes.
2811 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002812 * \param[out] output Buffer where the encrypted message is to
2813 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002814 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002815 * \param[out] output_length On success, the number of bytes
2816 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002817 *
Gilles Peskine28538492018-07-11 17:34:00 +02002818 * \retval #PSA_SUCCESS
2819 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002820 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002821 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002822 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002823 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002824 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002825 * \retval #PSA_ERROR_NOT_SUPPORTED
2826 * \retval #PSA_ERROR_INVALID_ARGUMENT
2827 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2828 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2829 * \retval #PSA_ERROR_HARDWARE_FAILURE
2830 * \retval #PSA_ERROR_TAMPERING_DETECTED
2831 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002832 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002833 * The library has not been previously initialized by psa_crypto_init().
2834 * It is implementation-dependent whether a failure to initialize
2835 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002836 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002837psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002838 psa_algorithm_t alg,
2839 const uint8_t *input,
2840 size_t input_length,
2841 const uint8_t *salt,
2842 size_t salt_length,
2843 uint8_t *output,
2844 size_t output_size,
2845 size_t *output_length);
2846
2847/**
2848 * \brief Decrypt a short message with a private key.
2849 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002850 * \param handle Handle to the key to use for the operation.
2851 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002852 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002853 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002854 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002855 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002856 * \param[in] salt A salt or label, if supported by the
2857 * encryption algorithm.
2858 * If the algorithm does not support a
2859 * salt, pass \c NULL.
2860 * If the algorithm supports an optional
2861 * salt and you do not want to pass a salt,
2862 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002863 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002864 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2865 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002866 * \param salt_length Size of the \p salt buffer in bytes.
2867 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002868 * \param[out] output Buffer where the decrypted message is to
2869 * be written.
2870 * \param output_size Size of the \c output buffer in bytes.
2871 * \param[out] output_length On success, the number of bytes
2872 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002873 *
Gilles Peskine28538492018-07-11 17:34:00 +02002874 * \retval #PSA_SUCCESS
2875 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002876 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002877 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002878 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002879 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002880 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002881 * \retval #PSA_ERROR_NOT_SUPPORTED
2882 * \retval #PSA_ERROR_INVALID_ARGUMENT
2883 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2884 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2885 * \retval #PSA_ERROR_HARDWARE_FAILURE
2886 * \retval #PSA_ERROR_TAMPERING_DETECTED
2887 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2888 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002889 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002890 * The library has not been previously initialized by psa_crypto_init().
2891 * It is implementation-dependent whether a failure to initialize
2892 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002893 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002894psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002895 psa_algorithm_t alg,
2896 const uint8_t *input,
2897 size_t input_length,
2898 const uint8_t *salt,
2899 size_t salt_length,
2900 uint8_t *output,
2901 size_t output_size,
2902 size_t *output_length);
2903
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002904/**@}*/
2905
Gilles Peskineedd76872018-07-20 17:42:05 +02002906/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002907 * @{
2908 */
2909
2910/** The type of the state data structure for generators.
2911 *
2912 * Before calling any function on a generator, the application must
2913 * initialize it by any of the following means:
2914 * - Set the structure to all-bits-zero, for example:
2915 * \code
2916 * psa_crypto_generator_t generator;
2917 * memset(&generator, 0, sizeof(generator));
2918 * \endcode
2919 * - Initialize the structure to logical zero values, for example:
2920 * \code
2921 * psa_crypto_generator_t generator = {0};
2922 * \endcode
2923 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2924 * for example:
2925 * \code
2926 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2927 * \endcode
2928 * - Assign the result of the function psa_crypto_generator_init()
2929 * to the structure, for example:
2930 * \code
2931 * psa_crypto_generator_t generator;
2932 * generator = psa_crypto_generator_init();
2933 * \endcode
2934 *
2935 * This is an implementation-defined \c struct. Applications should not
2936 * make any assumptions about the content of this structure except
2937 * as directed by the documentation of a specific implementation.
2938 */
2939typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2940
2941/** \def PSA_CRYPTO_GENERATOR_INIT
2942 *
2943 * This macro returns a suitable initializer for a generator object
2944 * of type #psa_crypto_generator_t.
2945 */
2946#ifdef __DOXYGEN_ONLY__
2947/* This is an example definition for documentation purposes.
2948 * Implementations should define a suitable value in `crypto_struct.h`.
2949 */
2950#define PSA_CRYPTO_GENERATOR_INIT {0}
2951#endif
2952
2953/** Return an initial value for a generator object.
2954 */
2955static psa_crypto_generator_t psa_crypto_generator_init(void);
2956
2957/** Retrieve the current capacity of a generator.
2958 *
2959 * The capacity of a generator is the maximum number of bytes that it can
2960 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2961 *
2962 * \param[in] generator The generator to query.
2963 * \param[out] capacity On success, the capacity of the generator.
2964 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002965 * \retval #PSA_SUCCESS
2966 * \retval #PSA_ERROR_BAD_STATE
2967 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002968 */
2969psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2970 size_t *capacity);
2971
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002972/** Set the maximum capacity of a generator.
2973 *
2974 * \param[in,out] generator The generator object to modify.
2975 * \param capacity The new capacity of the generator.
2976 * It must be less or equal to the generator's
2977 * current capacity.
2978 *
2979 * \retval #PSA_SUCCESS
2980 * \retval #PSA_ERROR_INVALID_ARGUMENT
2981 * \p capacity is larger than the generator's current capacity.
2982 * \retval #PSA_ERROR_BAD_STATE
2983 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2984 */
2985psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
2986 size_t capacity);
2987
Gilles Peskineeab56e42018-07-12 17:12:33 +02002988/** Read some data from a generator.
2989 *
2990 * This function reads and returns a sequence of bytes from a generator.
2991 * The data that is read is discarded from the generator. The generator's
2992 * capacity is decreased by the number of bytes read.
2993 *
2994 * \param[in,out] generator The generator object to read from.
2995 * \param[out] output Buffer where the generator output will be
2996 * written.
2997 * \param output_length Number of bytes to output.
2998 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002999 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003000 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskineeab56e42018-07-12 17:12:33 +02003001 * There were fewer than \p output_length bytes
3002 * in the generator. Note that in this case, no
3003 * output is written to the output buffer.
3004 * The generator's capacity is set to 0, thus
3005 * subsequent calls to this function will not
3006 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003007 * \retval #PSA_ERROR_BAD_STATE
3008 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3009 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3010 * \retval #PSA_ERROR_HARDWARE_FAILURE
3011 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003012 */
3013psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
3014 uint8_t *output,
3015 size_t output_length);
3016
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003017/** Generate a key deterministically from data read from a generator.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003018 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003019 * This function uses the output of a generator to derive a key.
3020 * How much output it consumes and how the key is derived depends on the
3021 * key type.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003022 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003023 * - For key types for which the key is an arbitrary sequence of bytes
3024 * of a given size,
3025 * this function is functionally equivalent to calling #psa_generator_read
3026 * and passing the resulting output to #psa_import_key.
3027 * However, this function has a security benefit:
3028 * if the implementation provides an isolation boundary then
3029 * the key material is not exposed outside the isolation boundary.
3030 * As a consequence, for these key types, this function always consumes
3031 * exactly (\p bits / 8) bytes from the generator.
3032 * The following key types defined in this specification follow this scheme:
3033 *
3034 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003035 * - #PSA_KEY_TYPE_ARC4;
3036 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003037 * - #PSA_KEY_TYPE_DERIVE;
3038 * - #PSA_KEY_TYPE_HMAC.
3039 *
3040 * - For ECC keys on a Montgomery elliptic curve
3041 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3042 * Montgomery curve), this function always draws a byte string whose
3043 * length is determined by the curve, and sets the mandatory bits
3044 * accordingly. That is:
3045 *
3046 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3047 * and process it as specified in RFC 7748 &sect;5.
3048 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3049 * and process it as specified in RFC 7748 &sect;5.
3050 *
3051 * - For key types for which the key is represented by a single sequence of
3052 * \p bits bits with constraints as to which bit sequences are acceptable,
3053 * this function draws a byte string of length (\p bits / 8) bytes rounded
3054 * up to the nearest whole number of bytes. If the resulting byte string
3055 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3056 * This process is repeated until an acceptable byte string is drawn.
3057 * The byte string drawn from the generator is interpreted as specified
3058 * for the output produced by psa_export_key().
3059 * The following key types defined in this specification follow this scheme:
3060 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003061 * - #PSA_KEY_TYPE_DES.
3062 * Force-set the parity bits, but discard forbidden weak keys.
3063 * For 2-key and 3-key triple-DES, the three keys are generated
3064 * successively (for example, for 3-key triple-DES,
3065 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3066 * discard the first 8 bytes, use the next 8 bytes as the first key,
3067 * and continue reading output from the generator to derive the other
3068 * two keys).
3069 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR),
3070 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
3071 * ECC keys on a Weierstrass elliptic curve
3072 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3073 * Weierstrass curve).
3074 * For these key types, interpret the byte string as integer
3075 * in big-endian order. Discard it if it is not in the range
3076 * [0, *N* - 2] where *N* is the boundary of the private key domain
3077 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003078 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003079 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003080 * This method allows compliance to NIST standards, specifically
3081 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003082 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3083 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3084 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3085 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003086 *
3087 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
3088 * the way in which the generator output is consumed is
3089 * implementation-defined.
3090 *
3091 * In all cases, the data that is read is discarded from the generator.
3092 * The generator's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003093 *
Gilles Peskine20628592019-04-19 19:29:50 +02003094 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003095 * \param[out] handle On success, a handle to the newly created key.
3096 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003097 * \param[in,out] generator The generator object to read from.
3098 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003099 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003100 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003101 * If the key is persistent, the key material and the key's metadata
3102 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003103 * \retval #PSA_ERROR_ALREADY_EXISTS
3104 * This is an attempt to create a persistent key, and there is
3105 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003106 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003107 * There was not enough data to create the desired key.
3108 * Note that in this case, no output is written to the output buffer.
3109 * The generator's capacity is set to 0, thus subsequent calls to
3110 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003111 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003112 * The key type or key size is not supported, either by the
3113 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003114 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003115 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3116 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3117 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3118 * \retval #PSA_ERROR_HARDWARE_FAILURE
3119 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003120 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003121 * The library has not been previously initialized by psa_crypto_init().
3122 * It is implementation-dependent whether a failure to initialize
3123 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003124 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003125psa_status_t psa_generate_derived_key(const psa_key_attributes_t *attributes,
Gilles Peskine87a5e562019-04-17 12:28:25 +02003126 psa_key_handle_t *handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003127 psa_crypto_generator_t *generator);
3128
3129/** Abort a generator.
3130 *
3131 * Once a generator has been aborted, its capacity is zero.
3132 * Aborting a generator frees all associated resources except for the
3133 * \c generator structure itself.
3134 *
3135 * This function may be called at any time as long as the generator
3136 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
3137 * psa_crypto_generator_init() or a zero value. In particular, it is valid
3138 * to call psa_generator_abort() twice, or to call psa_generator_abort()
3139 * on a generator that has not been set up.
3140 *
3141 * Once aborted, the generator object may be called.
3142 *
3143 * \param[in,out] generator The generator to abort.
3144 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003145 * \retval #PSA_SUCCESS
3146 * \retval #PSA_ERROR_BAD_STATE
3147 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3148 * \retval #PSA_ERROR_HARDWARE_FAILURE
3149 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003150 */
3151psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3152
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003153/** Use the maximum possible capacity for a generator.
3154 *
3155 * Use this value as the capacity argument when setting up a generator
3156 * to indicate that the generator should have the maximum possible capacity.
3157 * The value of the maximum possible capacity depends on the generator
3158 * algorithm.
3159 */
3160#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
3161
Gilles Peskineeab56e42018-07-12 17:12:33 +02003162/**@}*/
3163
Gilles Peskineea0fb492018-07-12 17:17:20 +02003164/** \defgroup derivation Key derivation
3165 * @{
3166 */
3167
3168/** Set up a key derivation operation.
3169 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003170 * A key derivation algorithm takes some inputs and uses them to create
3171 * a byte generator which can be used to produce keys and other
3172 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003173 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003174 * To use a generator for key derivation:
3175 * - Start with an initialized object of type #psa_crypto_generator_t.
3176 * - Call psa_key_derivation_setup() to select the algorithm.
3177 * - Provide the inputs for the key derivation by calling
3178 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3179 * as appropriate. Which inputs are needed, in what order, and whether
3180 * they may be keys and if so of what type depends on the algorithm.
3181 * - Optionally set the generator's maximum capacity with
3182 * psa_set_generator_capacity(). You may do this before, in the middle of
3183 * or after providing inputs. For some algorithms, this step is mandatory
3184 * because the output depends on the maximum capacity.
3185 * - Generate output with psa_generator_read() or
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003186 * psa_generate_derived_key(). Successive calls to these functions
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003187 * use successive output bytes from the generator.
3188 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02003189 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003190 * \param[in,out] generator The generator object to set up. It must
3191 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003192 * \param alg The key derivation algorithm to compute
3193 * (\c PSA_ALG_XXX value such that
3194 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02003195 *
3196 * \retval #PSA_SUCCESS
3197 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003198 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003199 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003200 * \retval #PSA_ERROR_NOT_SUPPORTED
3201 * \c alg is not supported or is not a key derivation algorithm.
3202 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3203 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3204 * \retval #PSA_ERROR_HARDWARE_FAILURE
3205 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003206 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003207 */
3208psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
3209 psa_algorithm_t alg);
3210
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003211/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003212 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003213 * Which inputs are required and in what order depends on the algorithm.
3214 * Refer to the documentation of each key derivation or key agreement
3215 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003216 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003217 * This function passes direct inputs. Some inputs must be passed as keys
3218 * using psa_key_derivation_input_key() instead of this function. Refer to
3219 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003220 *
3221 * \param[in,out] generator The generator object to use. It must
3222 * have been set up with
3223 * psa_key_derivation_setup() and must not
3224 * have produced any output yet.
3225 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003226 * \param[in] data Input data to use.
3227 * \param data_length Size of the \p data buffer in bytes.
3228 *
3229 * \retval #PSA_SUCCESS
3230 * Success.
3231 * \retval #PSA_ERROR_INVALID_ARGUMENT
3232 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003233 * \retval #PSA_ERROR_INVALID_ARGUMENT
3234 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003235 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3236 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3237 * \retval #PSA_ERROR_HARDWARE_FAILURE
3238 * \retval #PSA_ERROR_TAMPERING_DETECTED
3239 * \retval #PSA_ERROR_BAD_STATE
3240 * The value of \p step is not valid given the state of \p generator.
3241 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003242 * The library has not been previously initialized by psa_crypto_init().
3243 * It is implementation-dependent whether a failure to initialize
3244 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003245 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003246psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3247 psa_key_derivation_step_t step,
3248 const uint8_t *data,
3249 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003250
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003251/** Provide an input for key derivation in the form of a key.
3252 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003253 * Which inputs are required and in what order depends on the algorithm.
3254 * Refer to the documentation of each key derivation or key agreement
3255 * algorithm for information.
3256 *
3257 * This function passes key inputs. Some inputs must be passed as keys
3258 * of the appropriate type using this function, while others must be
3259 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3260 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003261 *
3262 * \param[in,out] generator The generator object to use. It must
3263 * have been set up with
3264 * psa_key_derivation_setup() and must not
3265 * have produced any output yet.
3266 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003267 * \param handle Handle to the key. It must have an
3268 * appropriate type for \p step and must
3269 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003270 *
3271 * \retval #PSA_SUCCESS
3272 * Success.
3273 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine31351842019-04-09 12:00:00 +02003274 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003275 * \retval #PSA_ERROR_NOT_PERMITTED
3276 * \retval #PSA_ERROR_INVALID_ARGUMENT
3277 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003278 * \retval #PSA_ERROR_INVALID_ARGUMENT
3279 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003280 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3281 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3282 * \retval #PSA_ERROR_HARDWARE_FAILURE
3283 * \retval #PSA_ERROR_TAMPERING_DETECTED
3284 * \retval #PSA_ERROR_BAD_STATE
3285 * The value of \p step is not valid given the state of \p generator.
3286 * \retval #PSA_ERROR_BAD_STATE
3287 * The library has not been previously initialized by psa_crypto_init().
3288 * It is implementation-dependent whether a failure to initialize
3289 * results in this error code.
3290 */
3291psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3292 psa_key_derivation_step_t step,
3293 psa_key_handle_t handle);
3294
Gilles Peskine969c5d62019-01-16 15:53:06 +01003295/** Perform a key agreement and use the shared secret as input to a key
3296 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003297 *
3298 * A key agreement algorithm takes two inputs: a private key \p private_key
3299 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003300 * The result of this function is passed as input to a key derivation.
3301 * The output of this key derivation can be extracted by reading from the
3302 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003303 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003304 * \param[in,out] generator The generator object to use. It must
3305 * have been set up with
3306 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003307 * key agreement and derivation algorithm
3308 * \c alg (\c PSA_ALG_XXX value such that
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003309 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3310 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
Gilles Peskine6843c292019-01-18 16:44:49 +01003311 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003312 * The generator must be ready for an
3313 * input of the type given by \p step.
3314 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003315 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003316 * \param[in] peer_key Public key of the peer. The peer key must be in the
3317 * same format that psa_import_key() accepts for the
3318 * public key type corresponding to the type of
3319 * private_key. That is, this function performs the
3320 * equivalent of
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003321 * #psa_import_key(`internal_public_key_handle`,
3322 * #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(`private_key_type`),
3323 * `peer_key`, `peer_key_length`) where
Jaeden Amero8afbff82019-01-14 16:56:20 +00003324 * `private_key_type` is the type of `private_key`.
3325 * For example, for EC keys, this means that peer_key
3326 * is interpreted as a point on the curve that the
3327 * private key is on. The standard formats for public
3328 * keys are documented in the documentation of
3329 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003330 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003331 *
3332 * \retval #PSA_SUCCESS
3333 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003334 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02003335 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine01d718c2018-09-18 12:01:02 +02003336 * \retval #PSA_ERROR_NOT_PERMITTED
3337 * \retval #PSA_ERROR_INVALID_ARGUMENT
3338 * \c private_key is not compatible with \c alg,
3339 * or \p peer_key is not valid for \c alg or not compatible with
3340 * \c private_key.
3341 * \retval #PSA_ERROR_NOT_SUPPORTED
3342 * \c alg is not supported or is not a key derivation algorithm.
3343 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3344 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3345 * \retval #PSA_ERROR_HARDWARE_FAILURE
3346 * \retval #PSA_ERROR_TAMPERING_DETECTED
3347 */
3348psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003349 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003350 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003351 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003352 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003353
Gilles Peskine769c7a62019-01-18 16:42:29 +01003354/** Perform a key agreement and use the shared secret as input to a key
3355 * derivation.
3356 *
3357 * A key agreement algorithm takes two inputs: a private key \p private_key
3358 * a public key \p peer_key.
3359 *
3360 * \warning The raw result of a key agreement algorithm such as finite-field
3361 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3362 * not be used directly as key material. It should instead be passed as
3363 * input to a key derivation algorithm. To chain a key agreement with
3364 * a key derivation, use psa_key_agreement() and other functions from
3365 * the key derivation and generator interface.
3366 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003367 * \param alg The key agreement algorithm to compute
3368 * (\c PSA_ALG_XXX value such that
3369 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3370 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003371 * \param private_key Handle to the private key to use.
3372 * \param[in] peer_key Public key of the peer. It must be
3373 * in the same format that psa_import_key()
3374 * accepts. The standard formats for public
3375 * keys are documented in the documentation
3376 * of psa_export_public_key().
3377 * \param peer_key_length Size of \p peer_key in bytes.
3378 * \param[out] output Buffer where the decrypted message is to
3379 * be written.
3380 * \param output_size Size of the \c output buffer in bytes.
3381 * \param[out] output_length On success, the number of bytes
3382 * that make up the returned output.
3383 *
3384 * \retval #PSA_SUCCESS
3385 * Success.
3386 * \retval #PSA_ERROR_INVALID_HANDLE
3387 * \retval #PSA_ERROR_EMPTY_SLOT
3388 * \retval #PSA_ERROR_NOT_PERMITTED
3389 * \retval #PSA_ERROR_INVALID_ARGUMENT
3390 * \p alg is not a key agreement algorithm
3391 * \retval #PSA_ERROR_INVALID_ARGUMENT
3392 * \p private_key is not compatible with \p alg,
3393 * or \p peer_key is not valid for \p alg or not compatible with
3394 * \p private_key.
3395 * \retval #PSA_ERROR_NOT_SUPPORTED
3396 * \p alg is not a supported key agreement algorithm.
3397 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3398 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3399 * \retval #PSA_ERROR_HARDWARE_FAILURE
3400 * \retval #PSA_ERROR_TAMPERING_DETECTED
3401 */
3402psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3403 psa_key_handle_t private_key,
3404 const uint8_t *peer_key,
3405 size_t peer_key_length,
3406 uint8_t *output,
3407 size_t output_size,
3408 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003409
3410/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003411
3412/** \defgroup random Random generation
3413 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003414 */
3415
3416/**
3417 * \brief Generate random bytes.
3418 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003419 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003420 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003421 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003422 *
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003423 * \note To generate a key, use psa_generate_random_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003424 *
3425 * \param[out] output Output buffer for the generated data.
3426 * \param output_size Number of bytes to generate and output.
3427 *
3428 * \retval #PSA_SUCCESS
3429 * \retval #PSA_ERROR_NOT_SUPPORTED
3430 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3431 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3432 * \retval #PSA_ERROR_HARDWARE_FAILURE
3433 * \retval #PSA_ERROR_TAMPERING_DETECTED
3434 * \retval #PSA_ERROR_BAD_STATE
3435 * The library has not been previously initialized by psa_crypto_init().
3436 * It is implementation-dependent whether a failure to initialize
3437 * results in this error code.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003438 */
3439psa_status_t psa_generate_random(uint8_t *output,
3440 size_t output_size);
3441
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003442/**
3443 * \brief Generate a key or key pair.
3444 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003445 * The key is generated randomly.
3446 * Its location, policy, type and size are taken from \p attributes.
3447 *
3448 * If the type requires additional domain parameters, these are taken
3449 * from \p attributes as well. The following types use domain parameters:
3450 * - When generating an RSA key (#PSA_KEY_TYPE_RSA_KEYPAIR),
3451 * the default public exponent is 65537. This value is used if
3452 * \p attributes was set with psa_set_key_type() or by passing an empty
3453 * byte string as domain parameters to psa_set_key_domain_parameters().
3454 * If psa_set_key_domain_parameters() was used to set a non-empty
3455 * domain parameter string in \p attributes, this string is read as
3456 * a big-endian integer which is used as the public exponent.
3457 * - When generating a DSA key (#PSA_KEY_TYPE_DSA_KEYPAIR) or a
3458 * Diffie-Hellman key (#PSA_KEY_TYPE_DH_KEYPAIR), the domain parameters
3459 * from \p attributes are interpreted as described for
3460 * psa_set_key_domain_parameters().
3461 *
Gilles Peskine20628592019-04-19 19:29:50 +02003462 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003463 * \param[out] handle On success, a handle to the newly created key.
3464 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003465 *
Gilles Peskine28538492018-07-11 17:34:00 +02003466 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003467 * Success.
3468 * If the key is persistent, the key material and the key's metadata
3469 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003470 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003471 * This is an attempt to create a persistent key, and there is
3472 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003473 * \retval #PSA_ERROR_NOT_SUPPORTED
3474 * \retval #PSA_ERROR_INVALID_ARGUMENT
3475 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3476 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3477 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3478 * \retval #PSA_ERROR_HARDWARE_FAILURE
3479 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003480 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003481 * The library has not been previously initialized by psa_crypto_init().
3482 * It is implementation-dependent whether a failure to initialize
3483 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003484 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003485psa_status_t psa_generate_random_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003486 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003487
3488/**@}*/
3489
Gilles Peskinee59236f2018-01-27 23:32:46 +01003490#ifdef __cplusplus
3491}
3492#endif
3493
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003494/* The file "crypto_sizes.h" contains definitions for size calculation
3495 * macros whose definitions are implementation-specific. */
3496#include "crypto_sizes.h"
3497
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003498/* The file "crypto_struct.h" contains definitions for
3499 * implementation-specific structs that are declared above. */
3500#include "crypto_struct.h"
3501
3502/* The file "crypto_extra.h" contains vendor-specific definitions. This
3503 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003504#include "crypto_extra.h"
3505
3506#endif /* PSA_CRYPTO_H */