blob: 51a2b0e52c41c48807d7fa075ca53feba874d005 [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():
135 * - psa_make_key_persistent()
136 * - psa_set_key_type()
137 * - psa_set_key_bits()
138 * - psa_set_key_usage_flags()
139 * - psa_set_key_algorithm()
140 *
Gilles Peskine20628592019-04-19 19:29:50 +0200141 * Before calling any function on a key attribute structure, the application
142 * must initialize it by any of the following means:
143 * - Set the structure to all-bits-zero, for example:
144 * \code
145 * psa_key_attributes_t attributes;
146 * memset(&attributes, 0, sizeof(attributes));
147 * \endcode
148 * - Initialize the structure to logical zero values, for example:
149 * \code
150 * psa_key_attributes_t attributes = {0};
151 * \endcode
152 * - Initialize the structure to the initializer #PSA_KEY_ATTRIBUTES_INIT,
153 * for example:
154 * \code
155 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
156 * \endcode
157 * - Assign the result of the function psa_key_attributes_init()
158 * to the structure, for example:
159 * \code
160 * psa_key_attributes_t attributes;
161 * attributes = psa_key_attributes_init();
162 * \endcode
163 *
164 * A freshly initialized attribute structure contains the following
165 * values:
166 *
167 * - lifetime: #PSA_KEY_LIFETIME_VOLATILE.
168 * - key identifier: unspecified.
169 * - type: \c 0, with no domain parameters.
170 * - key size: \c 0.
171 * - usage flags: \c 0.
172 * - algorithm: \c 0.
173 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200174 * A typical sequence to create a key is as follows:
175 * -# Create and initialize an attribute structure.
176 * -# If the key is persistent, call psa_make_key_persistent().
177 * -# Set the key policy with psa_set_key_usage_flags() and
178 * psa_set_key_algorithm().
179 * -# Set the key type with psa_set_key_type(). If the key type requires
180 * domain parameters, call psa_set_key_domain_parameters() instead.
181 * Skip this step if copying an existing key with psa_copy_key().
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100182 * -# When generating a random key with psa_generate_random_key() or deriving a key
183 * with psa_generate_derived_key(), set the desired key size with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200184 * psa_set_key_bits().
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100185 * -# Call a key creation function: psa_import_key(), psa_generate_random_key(),
186 * psa_generate_derived_key() or psa_copy_key(). This function reads
Gilles Peskine1ea5e442019-05-02 20:31:10 +0200187 * the attribute structure, creates a key with these attributes, and
188 * outputs a handle to the newly created key.
189 * -# The attribute structure is now no longer necessary. If you called
Gilles Peskine9c640f92019-04-28 11:36:21 +0200190 * psa_set_key_domain_parameters() earlier, you must call
191 * psa_reset_key_attributes() to free any resources used by the
192 * domain parameters. Otherwise calling psa_reset_key_attributes()
193 * is optional.
Gilles Peskine20628592019-04-19 19:29:50 +0200194 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200195 * A typical sequence to query a key's attributes is as follows:
196 * -# Call psa_get_key_attributes().
197 * -# Call `psa_get_key_xxx` functions to retrieve the attribute(s) that
198 * you are interested in.
199 * -# Call psa_reset_key_attributes() to free any resources that may be
200 * used by the attribute structure.
201 *
202 * Once a key has been created, it is impossible to change its attributes.
Gilles Peskine87a5e562019-04-17 12:28:25 +0200203 */
204typedef struct psa_key_attributes_s psa_key_attributes_t;
205
Gilles Peskine20628592019-04-19 19:29:50 +0200206/** Declare a key as persistent.
207 *
208 * This function does not access storage, it merely fills the attribute
209 * structure with given values. The persistent key will be written to
210 * storage when the attribute structure is passed to a key creation
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100211 * function such as psa_import_key(), psa_generate_random_key(),
212 * psa_generate_derived_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200213 *
214 * This function overwrites any identifier and lifetime values
215 * previously set in \p attributes.
216 *
217 * This function may be declared as `static` (i.e. without external
218 * linkage). This function may be provided as a function-like macro,
219 * but in this case it must evaluate each of its arguments exactly once.
220 *
221 * \param[out] attributes The attribute structure to write to.
222 * \param id The persistent identifier for the key.
223 * \param lifetime The lifetime for the key.
224 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
225 * key will be volatile, and \p id is ignored.
226 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200227static void psa_make_key_persistent(psa_key_attributes_t *attributes,
228 psa_key_id_t id,
229 psa_key_lifetime_t lifetime);
230
Gilles Peskine20628592019-04-19 19:29:50 +0200231/** Retrieve the key identifier from key attributes.
232 *
233 * This function may be declared as `static` (i.e. without external
234 * linkage). This function may be provided as a function-like macro,
235 * but in this case it must evaluate its argument exactly once.
236 *
237 * \param[in] attributes The key attribute structure to query.
238 *
239 * \return The persistent identifier stored in the attribute structure.
240 * This value is unspecified if the attribute structure declares
241 * the key as volatile.
242 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200243static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
244
Gilles Peskine20628592019-04-19 19:29:50 +0200245/** Retrieve the lifetime from key attributes.
246 *
247 * This function may be declared as `static` (i.e. without external
248 * linkage). This function may be provided as a function-like macro,
249 * but in this case it must evaluate its argument exactly once.
250 *
251 * \param[in] attributes The key attribute structure to query.
252 *
253 * \return The lifetime value stored in the attribute structure.
254 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200255static psa_key_lifetime_t psa_get_key_lifetime(
256 const psa_key_attributes_t *attributes);
257
Gilles Peskine20628592019-04-19 19:29:50 +0200258/** Declare usage flags for a key.
259 *
260 * Usage flags are part of a key's usage policy. They encode what
261 * kind of operations are permitted on the key. For more details,
262 * refer to the documentation of the type #psa_key_usage_t.
263 *
264 * This function overwrites any usage flags
265 * previously set in \p attributes.
266 *
267 * This function may be declared as `static` (i.e. without external
268 * linkage). This function may be provided as a function-like macro,
269 * but in this case it must evaluate each of its arguments exactly once.
270 *
271 * \param[out] attributes The attribute structure to write to.
272 * \param usage_flags The usage flags to write.
273 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200274static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
275 psa_key_usage_t usage_flags);
276
Gilles Peskine20628592019-04-19 19:29:50 +0200277/** Retrieve the usage flags from key attributes.
278 *
279 * This function may be declared as `static` (i.e. without external
280 * linkage). This function may be provided as a function-like macro,
281 * but in this case it must evaluate its argument exactly once.
282 *
283 * \param[in] attributes The key attribute structure to query.
284 *
285 * \return The usage flags stored in the attribute structure.
286 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200287static psa_key_usage_t psa_get_key_usage_flags(
288 const psa_key_attributes_t *attributes);
289
Gilles Peskine20628592019-04-19 19:29:50 +0200290/** Declare the permitted algorithm policy for a key.
291 *
292 * The permitted algorithm policy of a key encodes which algorithm or
293 * algorithms are permitted to be used with this key.
294 *
295 * This function overwrites any algorithm policy
296 * previously set in \p attributes.
297 *
298 * This function may be declared as `static` (i.e. without external
299 * linkage). This function may be provided as a function-like macro,
300 * but in this case it must evaluate each of its arguments exactly once.
301 *
302 * \param[out] attributes The attribute structure to write to.
303 * \param alg The permitted algorithm policy to write.
304 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200305static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
306 psa_algorithm_t alg);
307
Gilles Peskine20628592019-04-19 19:29:50 +0200308/** Retrieve the algorithm policy from key attributes.
309 *
310 * This function may be declared as `static` (i.e. without external
311 * linkage). This function may be provided as a function-like macro,
312 * but in this case it must evaluate its argument exactly once.
313 *
314 * \param[in] attributes The key attribute structure to query.
315 *
316 * \return The algorithm stored in the attribute structure.
317 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200318static psa_algorithm_t psa_get_key_algorithm(
319 const psa_key_attributes_t *attributes);
320
Gilles Peskine20628592019-04-19 19:29:50 +0200321/** Declare the type of a key.
322 *
323 * If a type requires domain parameters, you must call
324 * psa_set_key_domain_parameters() instead of this function.
325 *
326 * This function overwrites any key type and domain parameters
327 * previously set in \p attributes.
328 *
329 * This function may be declared as `static` (i.e. without external
330 * linkage). This function may be provided as a function-like macro,
331 * but in this case it must evaluate each of its arguments exactly once.
332 *
333 * \param[out] attributes The attribute structure to write to.
334 * \param type The key type to write.
335 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200336static void psa_set_key_type(psa_key_attributes_t *attributes,
337 psa_key_type_t type);
338
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200339/** Declare the size of a key.
340 *
341 * This function overwrites any key size previously set in \p attributes.
342 *
343 * This function may be declared as `static` (i.e. without external
344 * linkage). This function may be provided as a function-like macro,
345 * but in this case it must evaluate each of its arguments exactly once.
346 *
347 * \param[out] attributes The attribute structure to write to.
348 * \param bits The key size in bits.
349 */
350static void psa_set_key_bits(psa_key_attributes_t *attributes,
351 size_t bits);
352
Gilles Peskine20628592019-04-19 19:29:50 +0200353/** Retrieve the key type from key attributes.
354 *
355 * This function may be declared as `static` (i.e. without external
356 * linkage). This function may be provided as a function-like macro,
357 * but in this case it must evaluate its argument exactly once.
358 *
359 * \param[in] attributes The key attribute structure to query.
360 *
361 * \return The key type stored in the attribute structure.
362 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200363static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
364
Gilles Peskine20628592019-04-19 19:29:50 +0200365/** Retrieve the key size from key 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 its argument exactly once.
370 *
371 * \param[in] attributes The key attribute structure to query.
372 *
373 * \return The key size stored in the attribute structure, in bits.
374 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200375static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
376
Gilles Peskineb699f072019-04-26 16:06:02 +0200377/**
378 * \brief Set domain parameters for a key.
379 *
380 * Some key types require additional domain parameters in addition to
381 * the key type identifier and the key size.
382 * The format for the required domain parameters varies by the key type.
383 *
Gilles Peskinee56e8782019-04-26 17:34:02 +0200384 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEYPAIR),
385 * the domain parameter data consists of the public exponent,
Gilles Peskineb699f072019-04-26 16:06:02 +0200386 * represented as a big-endian integer with no leading zeros.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200387 * This information is used when generating an RSA key pair.
Gilles Peskineb699f072019-04-26 16:06:02 +0200388 * When importing a key, the public exponent is read from the imported
389 * key data and the exponent recorded in the attribute structure is ignored.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200390 * As an exception, the public exponent 65537 is represented by an empty
391 * byte string.
392 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEYPAIR),
Gilles Peskineb699f072019-04-26 16:06:02 +0200393 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
394 * ```
395 * Dss-Parms ::= SEQUENCE {
396 * p INTEGER,
397 * q INTEGER,
398 * g INTEGER
399 * }
400 * ```
Gilles Peskinee56e8782019-04-26 17:34:02 +0200401 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY or
402 * #PSA_KEY_TYPE_DH_KEYPAIR), the
Gilles Peskineb699f072019-04-26 16:06:02 +0200403 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
404 * ```
405 * DomainParameters ::= SEQUENCE {
406 * p INTEGER, -- odd prime, p=jq +1
407 * g INTEGER, -- generator, g
408 * q INTEGER, -- factor of p-1
409 * j INTEGER OPTIONAL, -- subgroup factor
410 * validationParms ValidationParms OPTIONAL
411 * }
412 * ValidationParms ::= SEQUENCE {
413 * seed BIT STRING,
414 * pgenCounter INTEGER
415 * }
416 * ```
417 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200418 * \note This function may allocate memory or other resources.
419 * Once you have called this function on an attribute structure,
420 * you must call psa_reset_key_attributes() to free these resources.
421 *
Gilles Peskineb699f072019-04-26 16:06:02 +0200422 * \param[in,out] attributes Attribute structure where the specified domain
423 * parameters will be stored.
424 * If this function fails, the content of
425 * \p attributes is not modified.
426 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
427 * \param[in] data Buffer containing the key domain parameters.
428 * The content of this buffer is interpreted
429 * according to \p type as described above.
430 * \param data_length Size of the \p data buffer in bytes.
431 *
432 * \retval #PSA_SUCCESS
433 * \retval #PSA_ERROR_INVALID_ARGUMENT
434 * \retval #PSA_ERROR_NOT_SUPPORTED
435 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
436 */
437psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
438 psa_key_type_t type,
439 const uint8_t *data,
440 size_t data_length);
441
442/**
443 * \brief Get domain parameters for a key.
444 *
445 * Get the domain parameters for a key with this function, if any. The format
446 * of the domain parameters written to \p data is specified in the
447 * documentation for psa_set_key_domain_parameters().
448 *
449 * \param[in] attributes The key attribute structure to query.
450 * \param[out] data On success, the key domain parameters.
451 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineaa02c172019-04-28 11:44:17 +0200452 * The buffer is guaranteed to be large
453 * enough if its size in bytes is at least
454 * the value given by
455 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
Gilles Peskineb699f072019-04-26 16:06:02 +0200456 * \param[out] data_length On success, the number of bytes
457 * that make up the key domain parameters data.
458 *
459 * \retval #PSA_SUCCESS
460 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
461 */
462psa_status_t psa_get_key_domain_parameters(
463 const psa_key_attributes_t *attributes,
464 uint8_t *data,
465 size_t data_size,
466 size_t *data_length);
467
Gilles Peskine20628592019-04-19 19:29:50 +0200468/** Retrieve the attributes of a key.
469 *
470 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200471 * psa_reset_key_attributes(). It then copies the attributes of
472 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200473 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200474 * \note This function may allocate memory or other resources.
475 * Once you have called this function on an attribute structure,
476 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200477 *
Gilles Peskine20628592019-04-19 19:29:50 +0200478 * \param[in] handle Handle to the key to query.
479 * \param[in,out] attributes On success, the attributes of the key.
480 * On failure, equivalent to a
481 * freshly-initialized structure.
482 *
483 * \retval #PSA_SUCCESS
484 * \retval #PSA_ERROR_INVALID_HANDLE
485 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
486 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
487 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200488psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
489 psa_key_attributes_t *attributes);
490
Gilles Peskine20628592019-04-19 19:29:50 +0200491/** Reset a key attribute structure to a freshly initialized state.
492 *
493 * You must initialize the attribute structure as described in the
494 * documentation of the type #psa_key_attributes_t before calling this
495 * function. Once the structure has been initialized, you may call this
496 * function at any time.
497 *
498 * This function frees any auxiliary resources that the structure
499 * may contain.
500 *
501 * \param[in,out] attributes The attribute structure to reset.
502 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200503void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200504
Gilles Peskine87a5e562019-04-17 12:28:25 +0200505/**@}*/
506
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100507/** \defgroup key_management Key management
508 * @{
509 */
510
Gilles Peskinef535eb22018-11-30 14:08:36 +0100511/** Open a handle to an existing persistent key.
512 *
513 * Open a handle to a key which was previously created with psa_create_key().
514 *
515 * \param lifetime The lifetime of the key. This designates a storage
516 * area where the key material is stored. This must not
517 * be #PSA_KEY_LIFETIME_VOLATILE.
518 * \param id The persistent identifier of the key.
519 * \param[out] handle On success, a handle to a key slot which contains
520 * the data and metadata loaded from the specified
521 * persistent location.
522 *
523 * \retval #PSA_SUCCESS
524 * Success. The application can now use the value of `*handle`
525 * to access the newly allocated key slot.
526 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200527 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100528 * \retval #PSA_ERROR_INVALID_ARGUMENT
529 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
530 * \retval #PSA_ERROR_INVALID_ARGUMENT
531 * \p id is invalid for the specified lifetime.
532 * \retval #PSA_ERROR_NOT_SUPPORTED
533 * \p lifetime is not supported.
534 * \retval #PSA_ERROR_NOT_PERMITTED
535 * The specified key exists, but the application does not have the
536 * permission to access it. Note that this specification does not
537 * define any way to create such a key, but it may be possible
538 * through implementation-specific means.
539 */
540psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
541 psa_key_id_t id,
542 psa_key_handle_t *handle);
543
Gilles Peskinef535eb22018-11-30 14:08:36 +0100544/** Close a key handle.
545 *
546 * If the handle designates a volatile key, destroy the key material and
547 * free all associated resources, just like psa_destroy_key().
548 *
549 * If the handle designates a persistent key, free all resources associated
550 * with the key in volatile memory. The key slot in persistent storage is
551 * not affected and can be opened again later with psa_open_key().
552 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100553 * If the key is currently in use in a multipart operation,
554 * the multipart operation is aborted.
555 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100556 * \param handle The key handle to close.
557 *
558 * \retval #PSA_SUCCESS
559 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100560 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100561 */
562psa_status_t psa_close_key(psa_key_handle_t handle);
563
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100564/**@}*/
565
566/** \defgroup import_export Key import and export
567 * @{
568 */
569
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100570/**
571 * \brief Import a key in binary format.
572 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100573 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100574 * documentation of psa_export_public_key() for the format of public keys
575 * and to the documentation of psa_export_key() for the format for
576 * other key types.
577 *
578 * This specification supports a single format for each key type.
579 * Implementations may support other formats as long as the standard
580 * format is supported. Implementations that support other formats
581 * should ensure that the formats are clearly unambiguous so as to
582 * minimize the risk that an invalid input is accidentally interpreted
583 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100584 *
Gilles Peskine20628592019-04-19 19:29:50 +0200585 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200586 * The key size is always determined from the
587 * \p data buffer.
588 * If the key size in \p attributes is nonzero,
589 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200590 * \param[out] handle On success, a handle to the newly created key.
591 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100592 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine20628592019-04-19 19:29:50 +0200593 * buffer is interpreted according to the type and,
594 * if applicable, domain parameters declared in
595 * \p attributes.
596 * All implementations must support at least the format
597 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100598 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200599 * the chosen type. Implementations may allow other
600 * formats, but should be conservative: implementations
601 * should err on the side of rejecting content if it
602 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200603 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100604 *
Gilles Peskine28538492018-07-11 17:34:00 +0200605 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100606 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100607 * If the key is persistent, the key material and the key's metadata
608 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200609 * \retval #PSA_ERROR_ALREADY_EXISTS
610 * This is an attempt to create a persistent key, and there is
611 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200612 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200613 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200614 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200615 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200616 * The key attributes, as a whole, are invalid.
617 * \retval #PSA_ERROR_INVALID_ARGUMENT
618 * The key data is not correctly formatted.
619 * \retval #PSA_ERROR_INVALID_ARGUMENT
620 * The size in \p attributes is nonzero and does not match the size
621 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200622 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
623 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
624 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100625 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200626 * \retval #PSA_ERROR_HARDWARE_FAILURE
627 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300628 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300629 * The library has not been previously initialized by psa_crypto_init().
630 * It is implementation-dependent whether a failure to initialize
631 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100632 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200633psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
634 psa_key_handle_t *handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100635 const uint8_t *data,
636 size_t data_length);
637
638/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100639 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200640 *
641 * This function destroys the content of the key slot from both volatile
642 * memory and, if applicable, non-volatile storage. Implementations shall
643 * make a best effort to ensure that any previous content of the slot is
644 * unrecoverable.
645 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100646 * This function also erases any metadata such as policies and frees all
647 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200648 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100649 * If the key is currently in use in a multipart operation,
650 * the multipart operation is aborted.
651 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100652 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100653 *
Gilles Peskine28538492018-07-11 17:34:00 +0200654 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200655 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200656 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200657 * The slot holds content and cannot be erased because it is
658 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100659 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200660 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200661 * There was an failure in communication with the cryptoprocessor.
662 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200663 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200664 * The storage is corrupted. Implementations shall make a best effort
665 * to erase key material even in this stage, however applications
666 * should be aware that it may be impossible to guarantee that the
667 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200668 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200669 * An unexpected condition which is not a storage corruption or
670 * a communication failure occurred. The cryptoprocessor may have
671 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300672 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300673 * The library has not been previously initialized by psa_crypto_init().
674 * It is implementation-dependent whether a failure to initialize
675 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100677psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678
679/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100680 * \brief Export a key in binary format.
681 *
682 * The output of this function can be passed to psa_import_key() to
683 * create an equivalent object.
684 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100685 * If the implementation of psa_import_key() supports other formats
686 * beyond the format specified here, the output from psa_export_key()
687 * must use the representation specified here, not the original
688 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100689 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100690 * For standard key types, the output format is as follows:
691 *
692 * - For symmetric keys (including MAC keys), the format is the
693 * raw bytes of the key.
694 * - For DES, the key data consists of 8 bytes. The parity bits must be
695 * correct.
696 * - For Triple-DES, the format is the concatenation of the
697 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100698 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200699 * is the non-encrypted DER encoding of the representation defined by
700 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
701 * ```
702 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200703 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200704 * modulus INTEGER, -- n
705 * publicExponent INTEGER, -- e
706 * privateExponent INTEGER, -- d
707 * prime1 INTEGER, -- p
708 * prime2 INTEGER, -- q
709 * exponent1 INTEGER, -- d mod (p-1)
710 * exponent2 INTEGER, -- d mod (q-1)
711 * coefficient INTEGER, -- (inverse of q) mod p
712 * }
713 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000714 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
715 * representation of the private key `x` as a big-endian byte string. The
716 * length of the byte string is the private key size in bytes (leading zeroes
717 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200718 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100719 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100720 * a representation of the private value as a `ceiling(m/8)`-byte string
721 * where `m` is the bit size associated with the curve, i.e. the bit size
722 * of the order of the curve's coordinate field. This byte string is
723 * in little-endian order for Montgomery curves (curve types
724 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
725 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
726 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100727 * This is the content of the `privateKey` field of the `ECPrivateKey`
728 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000729 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
730 * format is the representation of the private key `x` as a big-endian byte
731 * string. The length of the byte string is the private key size in bytes
732 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200733 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
734 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100735 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200736 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
737 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100738 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200739 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200740 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200741 * \param[out] data_length On success, the number of bytes
742 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100743 *
Gilles Peskine28538492018-07-11 17:34:00 +0200744 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100745 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200746 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200747 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200748 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100749 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200750 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
751 * The size of the \p data buffer is too small. You can determine a
752 * sufficient buffer size by calling
753 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
754 * where \c type is the key type
755 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200756 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
757 * \retval #PSA_ERROR_HARDWARE_FAILURE
758 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300759 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300760 * The library has not been previously initialized by psa_crypto_init().
761 * It is implementation-dependent whether a failure to initialize
762 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100763 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100764psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100765 uint8_t *data,
766 size_t data_size,
767 size_t *data_length);
768
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100769/**
770 * \brief Export a public key or the public part of a key pair in binary format.
771 *
772 * The output of this function can be passed to psa_import_key() to
773 * create an object that is equivalent to the public key.
774 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000775 * This specification supports a single format for each key type.
776 * Implementations may support other formats as long as the standard
777 * format is supported. Implementations that support other formats
778 * should ensure that the formats are clearly unambiguous so as to
779 * minimize the risk that an invalid input is accidentally interpreted
780 * according to a different format.
781 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000782 * For standard key types, the output format is as follows:
783 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
784 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
785 * ```
786 * RSAPublicKey ::= SEQUENCE {
787 * modulus INTEGER, -- n
788 * publicExponent INTEGER } -- e
789 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000790 * - For elliptic curve public keys (key types for which
791 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
792 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
793 * Let `m` be the bit size associated with the curve, i.e. the bit size of
794 * `q` for a curve over `F_q`. The representation consists of:
795 * - The byte 0x04;
796 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
797 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000798 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
799 * representation of the public key `y = g^x mod p` as a big-endian byte
800 * string. The length of the byte string is the length of the base prime `p`
801 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000802 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
803 * the format is the representation of the public key `y = g^x mod p` as a
804 * big-endian byte string. The length of the byte string is the length of the
805 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100806 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200807 * Exporting a public key object or the public part of a key pair is
808 * always permitted, regardless of the key's usage flags.
809 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100810 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200811 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200812 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200813 * \param[out] data_length On success, the number of bytes
814 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100815 *
Gilles Peskine28538492018-07-11 17:34:00 +0200816 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100817 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200818 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200819 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200820 * The key is neither a public key nor a key pair.
821 * \retval #PSA_ERROR_NOT_SUPPORTED
822 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
823 * The size of the \p data buffer is too small. You can determine a
824 * sufficient buffer size by calling
825 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
826 * where \c type is the key type
827 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200828 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
829 * \retval #PSA_ERROR_HARDWARE_FAILURE
830 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300831 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300832 * The library has not been previously initialized by psa_crypto_init().
833 * It is implementation-dependent whether a failure to initialize
834 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100835 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100836psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100837 uint8_t *data,
838 size_t data_size,
839 size_t *data_length);
840
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100841/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100842 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100843 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000844 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100845 * This function is primarily useful to copy a key from one location
846 * to another, since it populates a key using the material from
847 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200848 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100849 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100850 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100851 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100852 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200853 * The policy on the source key must have the usage flag
854 * #PSA_KEY_USAGE_COPY set.
855 * In addition, some lifetimes also require the source key to have the
856 * usage flag #PSA_KEY_USAGE_EXPORT, because otherwise the source key
857 * is locked inside a secure processing environment and cannot be
858 * extracted. For keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE or
859 * #PSA_KEY_LIFETIME_PERSISTENT, the usage flag #PSA_KEY_USAGE_COPY
860 * is sufficient to permit the copy.
861 *
Gilles Peskine20628592019-04-19 19:29:50 +0200862 * The resulting key may only be used in a way that conforms to
863 * both the policy of the original key and the policy specified in
864 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100865 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200866 * usage flags on the source policy and the usage flags in \p attributes.
867 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100868 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200869 * - If either of the policies allows an algorithm and the other policy
870 * allows a wildcard-based algorithm policy that includes this algorithm,
871 * the resulting key allows the same algorithm.
872 * - If the policies do not allow any algorithm in common, this function
873 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200874 *
Gilles Peskine20628592019-04-19 19:29:50 +0200875 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100876 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200877 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100878 * \param source_handle The key to copy. It must be a handle to an
879 * occupied slot.
Gilles Peskine20628592019-04-19 19:29:50 +0200880 * \param[in] attributes The attributes for the new key.
881 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200882 * - The key type and size may be 0. If either is
883 * nonzero, it must match the corresponding
884 * attribute of the source key.
885 * - If \p attributes contains domain parameters,
886 * they must match the domain parameters of
887 * the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200888 * - The key location (the lifetime and, for
889 * persistent keys, the key identifier) is
890 * used directly.
891 * - The policy constraints (usage flags and
892 * algorithm policy) are combined from
893 * the source key and \p attributes so that
894 * both sets of restrictions apply, as
895 * described in the documentation of this function.
896 * \param[out] target_handle On success, a handle to the newly created key.
897 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200898 *
899 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100900 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200901 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200902 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200903 * This is an attempt to create a persistent key, and there is
904 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200905 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200906 * The lifetime or identifier in \p attributes are invalid.
907 * \retval #PSA_ERROR_INVALID_ARGUMENT
908 * The policy constraints on the source and specified in
909 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200910 * \retval #PSA_ERROR_INVALID_ARGUMENT
911 * \p attributes specifies a key type, domain parameters or key size
912 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100913 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200914 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
915 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100916 * The source key is not exportable and its lifetime does not
917 * allow copying it to the target's lifetime.
918 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
919 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200920 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
921 * \retval #PSA_ERROR_HARDWARE_FAILURE
922 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100923 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100924psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200925 const psa_key_attributes_t *attributes,
926 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100927
928/**@}*/
929
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100930/** \defgroup hash Message digests
931 * @{
932 */
933
Gilles Peskine69647a42019-01-14 20:18:12 +0100934/** Calculate the hash (digest) of a message.
935 *
936 * \note To verify the hash of a message against an
937 * expected value, use psa_hash_compare() instead.
938 *
939 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
940 * such that #PSA_ALG_IS_HASH(\p alg) is true).
941 * \param[in] input Buffer containing the message to hash.
942 * \param input_length Size of the \p input buffer in bytes.
943 * \param[out] hash Buffer where the hash is to be written.
944 * \param hash_size Size of the \p hash buffer in bytes.
945 * \param[out] hash_length On success, the number of bytes
946 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100947 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100948 *
949 * \retval #PSA_SUCCESS
950 * Success.
951 * \retval #PSA_ERROR_NOT_SUPPORTED
952 * \p alg is not supported or is not a hash algorithm.
953 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
954 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
955 * \retval #PSA_ERROR_HARDWARE_FAILURE
956 * \retval #PSA_ERROR_TAMPERING_DETECTED
957 */
958psa_status_t psa_hash_compute(psa_algorithm_t alg,
959 const uint8_t *input,
960 size_t input_length,
961 uint8_t *hash,
962 size_t hash_size,
963 size_t *hash_length);
964
965/** Calculate the hash (digest) of a message and compare it with a
966 * reference value.
967 *
968 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
969 * such that #PSA_ALG_IS_HASH(\p alg) is true).
970 * \param[in] input Buffer containing the message to hash.
971 * \param input_length Size of the \p input buffer in bytes.
972 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100973 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100974 *
975 * \retval #PSA_SUCCESS
976 * The expected hash is identical to the actual hash of the input.
977 * \retval #PSA_ERROR_INVALID_SIGNATURE
978 * The hash of the message was calculated successfully, but it
979 * differs from the expected hash.
980 * \retval #PSA_ERROR_NOT_SUPPORTED
981 * \p alg is not supported or is not a hash algorithm.
982 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
983 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
984 * \retval #PSA_ERROR_HARDWARE_FAILURE
985 * \retval #PSA_ERROR_TAMPERING_DETECTED
986 */
987psa_status_t psa_hash_compare(psa_algorithm_t alg,
988 const uint8_t *input,
989 size_t input_length,
990 const uint8_t *hash,
991 const size_t hash_length);
992
Gilles Peskine308b91d2018-02-08 09:47:44 +0100993/** The type of the state data structure for multipart hash operations.
994 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000995 * Before calling any function on a hash operation object, the application must
996 * initialize it by any of the following means:
997 * - Set the structure to all-bits-zero, for example:
998 * \code
999 * psa_hash_operation_t operation;
1000 * memset(&operation, 0, sizeof(operation));
1001 * \endcode
1002 * - Initialize the structure to logical zero values, for example:
1003 * \code
1004 * psa_hash_operation_t operation = {0};
1005 * \endcode
1006 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
1007 * for example:
1008 * \code
1009 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
1010 * \endcode
1011 * - Assign the result of the function psa_hash_operation_init()
1012 * to the structure, for example:
1013 * \code
1014 * psa_hash_operation_t operation;
1015 * operation = psa_hash_operation_init();
1016 * \endcode
1017 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001018 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001019 * make any assumptions about the content of this structure except
1020 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001021typedef struct psa_hash_operation_s psa_hash_operation_t;
1022
Jaeden Amero6a25b412019-01-04 11:47:44 +00001023/** \def PSA_HASH_OPERATION_INIT
1024 *
1025 * This macro returns a suitable initializer for a hash operation object
1026 * of type #psa_hash_operation_t.
1027 */
1028#ifdef __DOXYGEN_ONLY__
1029/* This is an example definition for documentation purposes.
1030 * Implementations should define a suitable value in `crypto_struct.h`.
1031 */
1032#define PSA_HASH_OPERATION_INIT {0}
1033#endif
1034
1035/** Return an initial value for a hash operation object.
1036 */
1037static psa_hash_operation_t psa_hash_operation_init(void);
1038
Gilles Peskinef45adda2019-01-14 18:29:18 +01001039/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001040 *
1041 * The sequence of operations to calculate a hash (message digest)
1042 * is as follows:
1043 * -# Allocate an operation object which will be passed to all the functions
1044 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +00001045 * -# Initialize the operation object with one of the methods described in the
1046 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001047 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001048 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001049 * of the message each time. The hash that is calculated is the hash
1050 * of the concatenation of these messages in order.
1051 * -# To calculate the hash, call psa_hash_finish().
1052 * To compare the hash with an expected value, call psa_hash_verify().
1053 *
1054 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +00001055 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001056 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001057 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001058 * eventually terminate the operation. The following events terminate an
1059 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001060 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001061 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001062 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001063 * \param[in,out] operation The operation object to set up. It must have
1064 * been initialized as per the documentation for
1065 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001066 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1067 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001068 *
Gilles Peskine28538492018-07-11 17:34:00 +02001069 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001070 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001071 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001072 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001073 * \retval #PSA_ERROR_BAD_STATE
1074 * The operation state is not valid (already set up and not
1075 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001076 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1077 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1078 * \retval #PSA_ERROR_HARDWARE_FAILURE
1079 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001080 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001081psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001082 psa_algorithm_t alg);
1083
Gilles Peskine308b91d2018-02-08 09:47:44 +01001084/** Add a message fragment to a multipart hash operation.
1085 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001086 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001087 *
1088 * If this function returns an error status, the operation becomes inactive.
1089 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001090 * \param[in,out] operation Active hash operation.
1091 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001092 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001093 *
Gilles Peskine28538492018-07-11 17:34:00 +02001094 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001095 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001096 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001097 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001098 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1099 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1100 * \retval #PSA_ERROR_HARDWARE_FAILURE
1101 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001102 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001103psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1104 const uint8_t *input,
1105 size_t input_length);
1106
Gilles Peskine308b91d2018-02-08 09:47:44 +01001107/** Finish the calculation of the hash of a message.
1108 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001109 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001110 * This function calculates the hash of the message formed by concatenating
1111 * the inputs passed to preceding calls to psa_hash_update().
1112 *
1113 * When this function returns, the operation becomes inactive.
1114 *
1115 * \warning Applications should not call this function if they expect
1116 * a specific value for the hash. Call psa_hash_verify() instead.
1117 * Beware that comparing integrity or authenticity data such as
1118 * hash values with a function such as \c memcmp is risky
1119 * because the time taken by the comparison may leak information
1120 * about the hashed data which could allow an attacker to guess
1121 * a valid hash and thereby bypass security controls.
1122 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001123 * \param[in,out] operation Active hash operation.
1124 * \param[out] hash Buffer where the hash is to be written.
1125 * \param hash_size Size of the \p hash buffer in bytes.
1126 * \param[out] hash_length On success, the number of bytes
1127 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001128 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001129 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001130 *
Gilles Peskine28538492018-07-11 17:34:00 +02001131 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001132 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001133 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001134 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001135 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001136 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001137 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001138 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001139 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1140 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1141 * \retval #PSA_ERROR_HARDWARE_FAILURE
1142 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001143 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001144psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1145 uint8_t *hash,
1146 size_t hash_size,
1147 size_t *hash_length);
1148
Gilles Peskine308b91d2018-02-08 09:47:44 +01001149/** Finish the calculation of the hash of a message and compare it with
1150 * an expected value.
1151 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001152 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001153 * This function calculates the hash of the message formed by concatenating
1154 * the inputs passed to preceding calls to psa_hash_update(). It then
1155 * compares the calculated hash with the expected hash passed as a
1156 * parameter to this function.
1157 *
1158 * When this function returns, the operation becomes inactive.
1159 *
Gilles Peskine19067982018-03-20 17:54:53 +01001160 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001161 * comparison between the actual hash and the expected hash is performed
1162 * in constant time.
1163 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001164 * \param[in,out] operation Active hash operation.
1165 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001166 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001167 *
Gilles Peskine28538492018-07-11 17:34:00 +02001168 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001169 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001170 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001171 * The hash of the message was calculated successfully, but it
1172 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001173 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001174 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001175 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1176 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1177 * \retval #PSA_ERROR_HARDWARE_FAILURE
1178 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001179 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001180psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1181 const uint8_t *hash,
1182 size_t hash_length);
1183
Gilles Peskine308b91d2018-02-08 09:47:44 +01001184/** Abort a hash operation.
1185 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001186 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001187 * \p operation structure itself. Once aborted, the operation object
1188 * can be reused for another operation by calling
1189 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001190 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001191 * You may call this function any time after the operation object has
1192 * been initialized by any of the following methods:
1193 * - A call to psa_hash_setup(), whether it succeeds or not.
1194 * - Initializing the \c struct to all-bits-zero.
1195 * - Initializing the \c struct to logical zeros, e.g.
1196 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001197 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001198 * In particular, calling psa_hash_abort() after the operation has been
1199 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1200 * psa_hash_verify() is safe and has no effect.
1201 *
1202 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001203 *
Gilles Peskine28538492018-07-11 17:34:00 +02001204 * \retval #PSA_SUCCESS
1205 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001206 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001207 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1208 * \retval #PSA_ERROR_HARDWARE_FAILURE
1209 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001210 */
1211psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001212
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001213/** Clone a hash operation.
1214 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001215 * This function copies the state of an ongoing hash operation to
1216 * a new operation object. In other words, this function is equivalent
1217 * to calling psa_hash_setup() on \p target_operation with the same
1218 * algorithm that \p source_operation was set up for, then
1219 * psa_hash_update() on \p target_operation with the same input that
1220 * that was passed to \p source_operation. After this function returns, the
1221 * two objects are independent, i.e. subsequent calls involving one of
1222 * the objects do not affect the other object.
1223 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001224 * \param[in] source_operation The active hash operation to clone.
1225 * \param[in,out] target_operation The operation object to set up.
1226 * It must be initialized but not active.
1227 *
1228 * \retval #PSA_SUCCESS
1229 * \retval #PSA_ERROR_BAD_STATE
1230 * \p source_operation is not an active hash operation.
1231 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001232 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001233 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1234 * \retval #PSA_ERROR_HARDWARE_FAILURE
1235 * \retval #PSA_ERROR_TAMPERING_DETECTED
1236 */
1237psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1238 psa_hash_operation_t *target_operation);
1239
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001240/**@}*/
1241
Gilles Peskine8c9def32018-02-08 10:02:12 +01001242/** \defgroup MAC Message authentication codes
1243 * @{
1244 */
1245
Gilles Peskine69647a42019-01-14 20:18:12 +01001246/** Calculate the MAC (message authentication code) of a message.
1247 *
1248 * \note To verify the MAC of a message against an
1249 * expected value, use psa_mac_verify() instead.
1250 * Beware that comparing integrity or authenticity data such as
1251 * MAC values with a function such as \c memcmp is risky
1252 * because the time taken by the comparison may leak information
1253 * about the MAC value which could allow an attacker to guess
1254 * a valid MAC and thereby bypass security controls.
1255 *
1256 * \param handle Handle to the key to use for the operation.
1257 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001258 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001259 * \param[in] input Buffer containing the input message.
1260 * \param input_length Size of the \p input buffer in bytes.
1261 * \param[out] mac Buffer where the MAC value is to be written.
1262 * \param mac_size Size of the \p mac buffer in bytes.
1263 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001264 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001265 *
1266 * \retval #PSA_SUCCESS
1267 * Success.
1268 * \retval #PSA_ERROR_INVALID_HANDLE
1269 * \retval #PSA_ERROR_EMPTY_SLOT
1270 * \retval #PSA_ERROR_NOT_PERMITTED
1271 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001272 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001273 * \retval #PSA_ERROR_NOT_SUPPORTED
1274 * \p alg is not supported or is not a MAC algorithm.
1275 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1276 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1277 * \retval #PSA_ERROR_HARDWARE_FAILURE
1278 * \retval #PSA_ERROR_TAMPERING_DETECTED
1279 * \retval #PSA_ERROR_BAD_STATE
1280 * The library has not been previously initialized by psa_crypto_init().
1281 * It is implementation-dependent whether a failure to initialize
1282 * results in this error code.
1283 */
1284psa_status_t psa_mac_compute(psa_key_handle_t handle,
1285 psa_algorithm_t alg,
1286 const uint8_t *input,
1287 size_t input_length,
1288 uint8_t *mac,
1289 size_t mac_size,
1290 size_t *mac_length);
1291
1292/** Calculate the MAC of a message and compare it with a reference value.
1293 *
1294 * \param handle Handle to the key to use for the operation.
1295 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001296 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001297 * \param[in] input Buffer containing the input message.
1298 * \param input_length Size of the \p input buffer in bytes.
1299 * \param[out] mac Buffer containing the expected MAC value.
1300 * \param mac_length Size of the \p mac buffer in bytes.
1301 *
1302 * \retval #PSA_SUCCESS
1303 * The expected MAC is identical to the actual MAC of the input.
1304 * \retval #PSA_ERROR_INVALID_SIGNATURE
1305 * The MAC of the message was calculated successfully, but it
1306 * differs from the expected value.
1307 * \retval #PSA_ERROR_INVALID_HANDLE
1308 * \retval #PSA_ERROR_EMPTY_SLOT
1309 * \retval #PSA_ERROR_NOT_PERMITTED
1310 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001311 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001312 * \retval #PSA_ERROR_NOT_SUPPORTED
1313 * \p alg is not supported or is not a MAC algorithm.
1314 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1315 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1316 * \retval #PSA_ERROR_HARDWARE_FAILURE
1317 * \retval #PSA_ERROR_TAMPERING_DETECTED
1318 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001319psa_status_t psa_mac_verify(psa_key_handle_t handle,
1320 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001321 const uint8_t *input,
1322 size_t input_length,
1323 const uint8_t *mac,
1324 const size_t mac_length);
1325
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001326/** The type of the state data structure for multipart MAC operations.
1327 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001328 * Before calling any function on a MAC operation object, the application must
1329 * initialize it by any of the following means:
1330 * - Set the structure to all-bits-zero, for example:
1331 * \code
1332 * psa_mac_operation_t operation;
1333 * memset(&operation, 0, sizeof(operation));
1334 * \endcode
1335 * - Initialize the structure to logical zero values, for example:
1336 * \code
1337 * psa_mac_operation_t operation = {0};
1338 * \endcode
1339 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1340 * for example:
1341 * \code
1342 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1343 * \endcode
1344 * - Assign the result of the function psa_mac_operation_init()
1345 * to the structure, for example:
1346 * \code
1347 * psa_mac_operation_t operation;
1348 * operation = psa_mac_operation_init();
1349 * \endcode
1350 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001351 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001352 * make any assumptions about the content of this structure except
1353 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001354typedef struct psa_mac_operation_s psa_mac_operation_t;
1355
Jaeden Amero769ce272019-01-04 11:48:03 +00001356/** \def PSA_MAC_OPERATION_INIT
1357 *
1358 * This macro returns a suitable initializer for a MAC operation object of type
1359 * #psa_mac_operation_t.
1360 */
1361#ifdef __DOXYGEN_ONLY__
1362/* This is an example definition for documentation purposes.
1363 * Implementations should define a suitable value in `crypto_struct.h`.
1364 */
1365#define PSA_MAC_OPERATION_INIT {0}
1366#endif
1367
1368/** Return an initial value for a MAC operation object.
1369 */
1370static psa_mac_operation_t psa_mac_operation_init(void);
1371
Gilles Peskinef45adda2019-01-14 18:29:18 +01001372/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001373 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001374 * This function sets up the calculation of the MAC
1375 * (message authentication code) of a byte string.
1376 * To verify the MAC of a message against an
1377 * expected value, use psa_mac_verify_setup() instead.
1378 *
1379 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001380 * -# Allocate an operation object which will be passed to all the functions
1381 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001382 * -# Initialize the operation object with one of the methods described in the
1383 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001384 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001385 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1386 * of the message each time. The MAC that is calculated is the MAC
1387 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001388 * -# At the end of the message, call psa_mac_sign_finish() to finish
1389 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001390 *
1391 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001392 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001393 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001394 * After a successful call to psa_mac_sign_setup(), the application must
1395 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001396 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001397 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001398 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001399 * \param[in,out] operation The operation object to set up. It must have
1400 * been initialized as per the documentation for
1401 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001402 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001403 * It must remain valid until the operation
1404 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001405 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001406 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001407 *
Gilles Peskine28538492018-07-11 17:34:00 +02001408 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001409 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001410 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001411 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001412 * \retval #PSA_ERROR_NOT_PERMITTED
1413 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001414 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001415 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001416 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001417 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1418 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1419 * \retval #PSA_ERROR_HARDWARE_FAILURE
1420 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001421 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001422 * The operation state is not valid (already set up and not
1423 * subsequently completed).
1424 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001425 * The library has not been previously initialized by psa_crypto_init().
1426 * It is implementation-dependent whether a failure to initialize
1427 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001428 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001429psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001430 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001431 psa_algorithm_t alg);
1432
Gilles Peskinef45adda2019-01-14 18:29:18 +01001433/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001434 *
1435 * This function sets up the verification of the MAC
1436 * (message authentication code) of a byte string against an expected value.
1437 *
1438 * The sequence of operations to verify a MAC is as follows:
1439 * -# Allocate an operation object which will be passed to all the functions
1440 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001441 * -# Initialize the operation object with one of the methods described in the
1442 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001443 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001444 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1445 * of the message each time. The MAC that is calculated is the MAC
1446 * of the concatenation of these messages in order.
1447 * -# At the end of the message, call psa_mac_verify_finish() to finish
1448 * calculating the actual MAC of the message and verify it against
1449 * the expected value.
1450 *
1451 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001452 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001453 *
1454 * After a successful call to psa_mac_verify_setup(), the application must
1455 * eventually terminate the operation through one of the following methods:
1456 * - A failed call to psa_mac_update().
1457 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1458 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001459 * \param[in,out] operation The operation object to set up. It must have
1460 * been initialized as per the documentation for
1461 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001462 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001463 * It must remain valid until the operation
1464 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001465 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1466 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001467 *
Gilles Peskine28538492018-07-11 17:34:00 +02001468 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001469 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001470 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001471 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001472 * \retval #PSA_ERROR_NOT_PERMITTED
1473 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001474 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001475 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001476 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001477 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1478 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1479 * \retval #PSA_ERROR_HARDWARE_FAILURE
1480 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001481 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001482 * The operation state is not valid (already set up and not
1483 * subsequently completed).
1484 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001485 * The library has not been previously initialized by psa_crypto_init().
1486 * It is implementation-dependent whether a failure to initialize
1487 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001488 */
1489psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001490 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001491 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001492
Gilles Peskinedcd14942018-07-12 00:30:52 +02001493/** Add a message fragment to a multipart MAC operation.
1494 *
1495 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1496 * before calling this function.
1497 *
1498 * If this function returns an error status, the operation becomes inactive.
1499 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001500 * \param[in,out] operation Active MAC operation.
1501 * \param[in] input Buffer containing the message fragment to add to
1502 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001503 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001504 *
1505 * \retval #PSA_SUCCESS
1506 * Success.
1507 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001508 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001509 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1510 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1511 * \retval #PSA_ERROR_HARDWARE_FAILURE
1512 * \retval #PSA_ERROR_TAMPERING_DETECTED
1513 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001514psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1515 const uint8_t *input,
1516 size_t input_length);
1517
Gilles Peskinedcd14942018-07-12 00:30:52 +02001518/** Finish the calculation of the MAC of a message.
1519 *
1520 * The application must call psa_mac_sign_setup() before calling this function.
1521 * This function calculates the MAC of the message formed by concatenating
1522 * the inputs passed to preceding calls to psa_mac_update().
1523 *
1524 * When this function returns, the operation becomes inactive.
1525 *
1526 * \warning Applications should not call this function if they expect
1527 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1528 * Beware that comparing integrity or authenticity data such as
1529 * MAC values with a function such as \c memcmp is risky
1530 * because the time taken by the comparison may leak information
1531 * about the MAC value which could allow an attacker to guess
1532 * a valid MAC and thereby bypass security controls.
1533 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001534 * \param[in,out] operation Active MAC operation.
1535 * \param[out] mac Buffer where the MAC value is to be written.
1536 * \param mac_size Size of the \p mac buffer in bytes.
1537 * \param[out] mac_length On success, the number of bytes
1538 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001539 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001540 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001541 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001542 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001543 *
1544 * \retval #PSA_SUCCESS
1545 * Success.
1546 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001547 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001548 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001549 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001550 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1551 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1552 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1553 * \retval #PSA_ERROR_HARDWARE_FAILURE
1554 * \retval #PSA_ERROR_TAMPERING_DETECTED
1555 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001556psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1557 uint8_t *mac,
1558 size_t mac_size,
1559 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001560
Gilles Peskinedcd14942018-07-12 00:30:52 +02001561/** Finish the calculation of the MAC of a message and compare it with
1562 * an expected value.
1563 *
1564 * The application must call psa_mac_verify_setup() before calling this function.
1565 * This function calculates the MAC of the message formed by concatenating
1566 * the inputs passed to preceding calls to psa_mac_update(). It then
1567 * compares the calculated MAC with the expected MAC passed as a
1568 * parameter to this function.
1569 *
1570 * When this function returns, the operation becomes inactive.
1571 *
1572 * \note Implementations shall make the best effort to ensure that the
1573 * comparison between the actual MAC and the expected MAC is performed
1574 * in constant time.
1575 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001576 * \param[in,out] operation Active MAC operation.
1577 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001578 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001579 *
1580 * \retval #PSA_SUCCESS
1581 * The expected MAC is identical to the actual MAC of the message.
1582 * \retval #PSA_ERROR_INVALID_SIGNATURE
1583 * The MAC of the message was calculated successfully, but it
1584 * differs from the expected MAC.
1585 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001586 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001587 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1588 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1589 * \retval #PSA_ERROR_HARDWARE_FAILURE
1590 * \retval #PSA_ERROR_TAMPERING_DETECTED
1591 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001592psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1593 const uint8_t *mac,
1594 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001595
Gilles Peskinedcd14942018-07-12 00:30:52 +02001596/** Abort a MAC operation.
1597 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001598 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001599 * \p operation structure itself. Once aborted, the operation object
1600 * can be reused for another operation by calling
1601 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001602 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001603 * You may call this function any time after the operation object has
1604 * been initialized by any of the following methods:
1605 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1606 * it succeeds or not.
1607 * - Initializing the \c struct to all-bits-zero.
1608 * - Initializing the \c struct to logical zeros, e.g.
1609 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001610 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001611 * In particular, calling psa_mac_abort() after the operation has been
1612 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1613 * psa_mac_verify_finish() is safe and has no effect.
1614 *
1615 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001616 *
1617 * \retval #PSA_SUCCESS
1618 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001619 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001620 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1621 * \retval #PSA_ERROR_HARDWARE_FAILURE
1622 * \retval #PSA_ERROR_TAMPERING_DETECTED
1623 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001624psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1625
1626/**@}*/
1627
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001628/** \defgroup cipher Symmetric ciphers
1629 * @{
1630 */
1631
Gilles Peskine69647a42019-01-14 20:18:12 +01001632/** Encrypt a message using a symmetric cipher.
1633 *
1634 * This function encrypts a message with a random IV (initialization
1635 * vector).
1636 *
1637 * \param handle Handle to the key to use for the operation.
1638 * It must remain valid until the operation
1639 * terminates.
1640 * \param alg The cipher algorithm to compute
1641 * (\c PSA_ALG_XXX value such that
1642 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1643 * \param[in] input Buffer containing the message to encrypt.
1644 * \param input_length Size of the \p input buffer in bytes.
1645 * \param[out] output Buffer where the output is to be written.
1646 * The output contains the IV followed by
1647 * the ciphertext proper.
1648 * \param output_size Size of the \p output buffer in bytes.
1649 * \param[out] output_length On success, the number of bytes
1650 * that make up the output.
1651 *
1652 * \retval #PSA_SUCCESS
1653 * Success.
1654 * \retval #PSA_ERROR_INVALID_HANDLE
1655 * \retval #PSA_ERROR_EMPTY_SLOT
1656 * \retval #PSA_ERROR_NOT_PERMITTED
1657 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001658 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001659 * \retval #PSA_ERROR_NOT_SUPPORTED
1660 * \p alg is not supported or is not a cipher algorithm.
1661 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1662 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1663 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1664 * \retval #PSA_ERROR_HARDWARE_FAILURE
1665 * \retval #PSA_ERROR_TAMPERING_DETECTED
1666 */
1667psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1668 psa_algorithm_t alg,
1669 const uint8_t *input,
1670 size_t input_length,
1671 uint8_t *output,
1672 size_t output_size,
1673 size_t *output_length);
1674
1675/** Decrypt a message using a symmetric cipher.
1676 *
1677 * This function decrypts a message encrypted with a symmetric cipher.
1678 *
1679 * \param handle Handle to the key to use for the operation.
1680 * It must remain valid until the operation
1681 * terminates.
1682 * \param alg The cipher algorithm to compute
1683 * (\c PSA_ALG_XXX value such that
1684 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1685 * \param[in] input Buffer containing the message to decrypt.
1686 * This consists of the IV followed by the
1687 * ciphertext proper.
1688 * \param input_length Size of the \p input buffer in bytes.
1689 * \param[out] output Buffer where the plaintext is to be written.
1690 * \param output_size Size of the \p output buffer in bytes.
1691 * \param[out] output_length On success, the number of bytes
1692 * that make up the output.
1693 *
1694 * \retval #PSA_SUCCESS
1695 * Success.
1696 * \retval #PSA_ERROR_INVALID_HANDLE
1697 * \retval #PSA_ERROR_EMPTY_SLOT
1698 * \retval #PSA_ERROR_NOT_PERMITTED
1699 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001700 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001701 * \retval #PSA_ERROR_NOT_SUPPORTED
1702 * \p alg is not supported or is not a cipher algorithm.
1703 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1704 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1705 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1706 * \retval #PSA_ERROR_HARDWARE_FAILURE
1707 * \retval #PSA_ERROR_TAMPERING_DETECTED
1708 */
1709psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1710 psa_algorithm_t alg,
1711 const uint8_t *input,
1712 size_t input_length,
1713 uint8_t *output,
1714 size_t output_size,
1715 size_t *output_length);
1716
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001717/** The type of the state data structure for multipart cipher operations.
1718 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001719 * Before calling any function on a cipher operation object, the application
1720 * must initialize it by any of the following means:
1721 * - Set the structure to all-bits-zero, for example:
1722 * \code
1723 * psa_cipher_operation_t operation;
1724 * memset(&operation, 0, sizeof(operation));
1725 * \endcode
1726 * - Initialize the structure to logical zero values, for example:
1727 * \code
1728 * psa_cipher_operation_t operation = {0};
1729 * \endcode
1730 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1731 * for example:
1732 * \code
1733 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1734 * \endcode
1735 * - Assign the result of the function psa_cipher_operation_init()
1736 * to the structure, for example:
1737 * \code
1738 * psa_cipher_operation_t operation;
1739 * operation = psa_cipher_operation_init();
1740 * \endcode
1741 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001742 * This is an implementation-defined \c struct. Applications should not
1743 * make any assumptions about the content of this structure except
1744 * as directed by the documentation of a specific implementation. */
1745typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1746
Jaeden Amero5bae2272019-01-04 11:48:27 +00001747/** \def PSA_CIPHER_OPERATION_INIT
1748 *
1749 * This macro returns a suitable initializer for a cipher operation object of
1750 * type #psa_cipher_operation_t.
1751 */
1752#ifdef __DOXYGEN_ONLY__
1753/* This is an example definition for documentation purposes.
1754 * Implementations should define a suitable value in `crypto_struct.h`.
1755 */
1756#define PSA_CIPHER_OPERATION_INIT {0}
1757#endif
1758
1759/** Return an initial value for a cipher operation object.
1760 */
1761static psa_cipher_operation_t psa_cipher_operation_init(void);
1762
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001763/** Set the key for a multipart symmetric encryption operation.
1764 *
1765 * The sequence of operations to encrypt a message with a symmetric cipher
1766 * is as follows:
1767 * -# Allocate an operation object which will be passed to all the functions
1768 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001769 * -# Initialize the operation object with one of the methods described in the
1770 * documentation for #psa_cipher_operation_t, e.g.
1771 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001772 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001773 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001774 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001775 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001776 * requires a specific IV value.
1777 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1778 * of the message each time.
1779 * -# Call psa_cipher_finish().
1780 *
1781 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001782 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001783 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001784 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001785 * eventually terminate the operation. The following events terminate an
1786 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001787 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001788 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001789 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001790 * \param[in,out] operation The operation object to set up. It must have
1791 * been initialized as per the documentation for
1792 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001793 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001794 * It must remain valid until the operation
1795 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001796 * \param alg The cipher algorithm to compute
1797 * (\c PSA_ALG_XXX value such that
1798 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001799 *
Gilles Peskine28538492018-07-11 17:34:00 +02001800 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001801 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001802 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001803 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001804 * \retval #PSA_ERROR_NOT_PERMITTED
1805 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001806 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001807 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001808 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001809 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1810 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1811 * \retval #PSA_ERROR_HARDWARE_FAILURE
1812 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001813 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001814 * The operation state is not valid (already set up and not
1815 * subsequently completed).
1816 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001817 * The library has not been previously initialized by psa_crypto_init().
1818 * It is implementation-dependent whether a failure to initialize
1819 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001820 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001821psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001822 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001823 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001824
1825/** Set the key for a multipart symmetric decryption operation.
1826 *
1827 * The sequence of operations to decrypt a message with a symmetric cipher
1828 * is as follows:
1829 * -# Allocate an operation object which will be passed to all the functions
1830 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001831 * -# Initialize the operation object with one of the methods described in the
1832 * documentation for #psa_cipher_operation_t, e.g.
1833 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001834 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001835 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001836 * decryption. If the IV is prepended to the ciphertext, you can call
1837 * psa_cipher_update() on a buffer containing the IV followed by the
1838 * beginning of the message.
1839 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1840 * of the message each time.
1841 * -# Call psa_cipher_finish().
1842 *
1843 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001844 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001845 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001846 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001847 * eventually terminate the operation. The following events terminate an
1848 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001849 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001850 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001851 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001852 * \param[in,out] operation The operation object to set up. It must have
1853 * been initialized as per the documentation for
1854 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001855 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001856 * It must remain valid until the operation
1857 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001858 * \param alg The cipher algorithm to compute
1859 * (\c PSA_ALG_XXX value such that
1860 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001861 *
Gilles Peskine28538492018-07-11 17:34:00 +02001862 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001863 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001864 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001865 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001866 * \retval #PSA_ERROR_NOT_PERMITTED
1867 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001868 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001869 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001870 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001871 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1872 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1873 * \retval #PSA_ERROR_HARDWARE_FAILURE
1874 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001875 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001876 * The operation state is not valid (already set up and not
1877 * subsequently completed).
1878 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001879 * The library has not been previously initialized by psa_crypto_init().
1880 * It is implementation-dependent whether a failure to initialize
1881 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001882 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001883psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001884 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001885 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001886
Gilles Peskinedcd14942018-07-12 00:30:52 +02001887/** Generate an IV for a symmetric encryption operation.
1888 *
1889 * This function generates a random IV (initialization vector), nonce
1890 * or initial counter value for the encryption operation as appropriate
1891 * for the chosen algorithm, key type and key size.
1892 *
1893 * The application must call psa_cipher_encrypt_setup() before
1894 * calling this function.
1895 *
1896 * If this function returns an error status, the operation becomes inactive.
1897 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001898 * \param[in,out] operation Active cipher operation.
1899 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001900 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001901 * \param[out] iv_length On success, the number of bytes of the
1902 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001903 *
1904 * \retval #PSA_SUCCESS
1905 * Success.
1906 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001907 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001908 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001909 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001910 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1911 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1912 * \retval #PSA_ERROR_HARDWARE_FAILURE
1913 * \retval #PSA_ERROR_TAMPERING_DETECTED
1914 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001915psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1916 unsigned char *iv,
1917 size_t iv_size,
1918 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001919
Gilles Peskinedcd14942018-07-12 00:30:52 +02001920/** Set the IV for a symmetric encryption or decryption operation.
1921 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001922 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001923 * or initial counter value for the encryption or decryption operation.
1924 *
1925 * The application must call psa_cipher_encrypt_setup() before
1926 * calling this function.
1927 *
1928 * If this function returns an error status, the operation becomes inactive.
1929 *
1930 * \note When encrypting, applications should use psa_cipher_generate_iv()
1931 * instead of this function, unless implementing a protocol that requires
1932 * a non-random IV.
1933 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001934 * \param[in,out] operation Active cipher operation.
1935 * \param[in] iv Buffer containing the IV to use.
1936 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001937 *
1938 * \retval #PSA_SUCCESS
1939 * Success.
1940 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001941 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001942 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001943 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001944 * or the chosen algorithm does not use an IV.
1945 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1946 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1947 * \retval #PSA_ERROR_HARDWARE_FAILURE
1948 * \retval #PSA_ERROR_TAMPERING_DETECTED
1949 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001950psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1951 const unsigned char *iv,
1952 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001953
Gilles Peskinedcd14942018-07-12 00:30:52 +02001954/** Encrypt or decrypt a message fragment in an active cipher operation.
1955 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001956 * Before calling this function, you must:
1957 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1958 * The choice of setup function determines whether this function
1959 * encrypts or decrypts its input.
1960 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1961 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001962 *
1963 * If this function returns an error status, the operation becomes inactive.
1964 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001965 * \param[in,out] operation Active cipher operation.
1966 * \param[in] input Buffer containing the message fragment to
1967 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001968 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001969 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001970 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001971 * \param[out] output_length On success, the number of bytes
1972 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001973 *
1974 * \retval #PSA_SUCCESS
1975 * Success.
1976 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001977 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001978 * not set, or already completed).
1979 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1980 * The size of the \p output buffer is too small.
1981 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1982 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1983 * \retval #PSA_ERROR_HARDWARE_FAILURE
1984 * \retval #PSA_ERROR_TAMPERING_DETECTED
1985 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001986psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1987 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001988 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001989 unsigned char *output,
1990 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001991 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001992
Gilles Peskinedcd14942018-07-12 00:30:52 +02001993/** Finish encrypting or decrypting a message in a cipher operation.
1994 *
1995 * The application must call psa_cipher_encrypt_setup() or
1996 * psa_cipher_decrypt_setup() before calling this function. The choice
1997 * of setup function determines whether this function encrypts or
1998 * decrypts its input.
1999 *
2000 * This function finishes the encryption or decryption of the message
2001 * formed by concatenating the inputs passed to preceding calls to
2002 * psa_cipher_update().
2003 *
2004 * When this function returns, the operation becomes inactive.
2005 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002006 * \param[in,out] operation Active cipher operation.
2007 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002008 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002009 * \param[out] output_length On success, the number of bytes
2010 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002011 *
2012 * \retval #PSA_SUCCESS
2013 * Success.
2014 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002015 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02002016 * not set, or already completed).
2017 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2018 * The size of the \p output buffer is too small.
2019 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2020 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2021 * \retval #PSA_ERROR_HARDWARE_FAILURE
2022 * \retval #PSA_ERROR_TAMPERING_DETECTED
2023 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002024psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002025 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002026 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002027 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002028
Gilles Peskinedcd14942018-07-12 00:30:52 +02002029/** Abort a cipher operation.
2030 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002031 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002032 * \p operation structure itself. Once aborted, the operation object
2033 * can be reused for another operation by calling
2034 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002035 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002036 * You may call this function any time after the operation object has
2037 * been initialized by any of the following methods:
2038 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2039 * whether it succeeds or not.
2040 * - Initializing the \c struct to all-bits-zero.
2041 * - Initializing the \c struct to logical zeros, e.g.
2042 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002043 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002044 * In particular, calling psa_cipher_abort() after the operation has been
2045 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2046 * is safe and has no effect.
2047 *
2048 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002049 *
2050 * \retval #PSA_SUCCESS
2051 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002052 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002053 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2054 * \retval #PSA_ERROR_HARDWARE_FAILURE
2055 * \retval #PSA_ERROR_TAMPERING_DETECTED
2056 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002057psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2058
2059/**@}*/
2060
Gilles Peskine3b555712018-03-03 21:27:57 +01002061/** \defgroup aead Authenticated encryption with associated data (AEAD)
2062 * @{
2063 */
2064
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002065/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002066 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002067 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002068 * \param alg The AEAD algorithm to compute
2069 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002070 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002071 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002072 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002073 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002074 * but not encrypted.
2075 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002076 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002077 * encrypted.
2078 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002079 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002080 * encrypted data. The additional data is not
2081 * part of this output. For algorithms where the
2082 * encrypted data and the authentication tag
2083 * are defined as separate outputs, the
2084 * authentication tag is appended to the
2085 * encrypted data.
2086 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2087 * This must be at least
2088 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2089 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002090 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002091 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002092 *
Gilles Peskine28538492018-07-11 17:34:00 +02002093 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002094 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002095 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002096 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002097 * \retval #PSA_ERROR_NOT_PERMITTED
2098 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002099 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002100 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002101 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002102 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2103 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2104 * \retval #PSA_ERROR_HARDWARE_FAILURE
2105 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002106 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002107 * The library has not been previously initialized by psa_crypto_init().
2108 * It is implementation-dependent whether a failure to initialize
2109 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002110 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002111psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002112 psa_algorithm_t alg,
2113 const uint8_t *nonce,
2114 size_t nonce_length,
2115 const uint8_t *additional_data,
2116 size_t additional_data_length,
2117 const uint8_t *plaintext,
2118 size_t plaintext_length,
2119 uint8_t *ciphertext,
2120 size_t ciphertext_size,
2121 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002122
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002123/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002124 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002125 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002126 * \param alg The AEAD algorithm to compute
2127 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002128 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002129 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002130 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002131 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002132 * but not encrypted.
2133 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002134 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002135 * encrypted. For algorithms where the
2136 * encrypted data and the authentication tag
2137 * are defined as separate inputs, the buffer
2138 * must contain the encrypted data followed
2139 * by the authentication tag.
2140 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002141 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002142 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2143 * This must be at least
2144 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2145 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002146 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002147 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002148 *
Gilles Peskine28538492018-07-11 17:34:00 +02002149 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002150 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002151 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002152 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002153 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002154 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002155 * \retval #PSA_ERROR_NOT_PERMITTED
2156 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002157 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002158 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002159 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002160 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2161 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2162 * \retval #PSA_ERROR_HARDWARE_FAILURE
2163 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002164 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002165 * The library has not been previously initialized by psa_crypto_init().
2166 * It is implementation-dependent whether a failure to initialize
2167 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002168 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002169psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002170 psa_algorithm_t alg,
2171 const uint8_t *nonce,
2172 size_t nonce_length,
2173 const uint8_t *additional_data,
2174 size_t additional_data_length,
2175 const uint8_t *ciphertext,
2176 size_t ciphertext_length,
2177 uint8_t *plaintext,
2178 size_t plaintext_size,
2179 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002180
Gilles Peskine30a9e412019-01-14 18:36:12 +01002181/** The type of the state data structure for multipart AEAD operations.
2182 *
2183 * Before calling any function on an AEAD operation object, the application
2184 * must initialize it by any of the following means:
2185 * - Set the structure to all-bits-zero, for example:
2186 * \code
2187 * psa_aead_operation_t operation;
2188 * memset(&operation, 0, sizeof(operation));
2189 * \endcode
2190 * - Initialize the structure to logical zero values, for example:
2191 * \code
2192 * psa_aead_operation_t operation = {0};
2193 * \endcode
2194 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2195 * for example:
2196 * \code
2197 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2198 * \endcode
2199 * - Assign the result of the function psa_aead_operation_init()
2200 * to the structure, for example:
2201 * \code
2202 * psa_aead_operation_t operation;
2203 * operation = psa_aead_operation_init();
2204 * \endcode
2205 *
2206 * This is an implementation-defined \c struct. Applications should not
2207 * make any assumptions about the content of this structure except
2208 * as directed by the documentation of a specific implementation. */
2209typedef struct psa_aead_operation_s psa_aead_operation_t;
2210
2211/** \def PSA_AEAD_OPERATION_INIT
2212 *
2213 * This macro returns a suitable initializer for an AEAD operation object of
2214 * type #psa_aead_operation_t.
2215 */
2216#ifdef __DOXYGEN_ONLY__
2217/* This is an example definition for documentation purposes.
2218 * Implementations should define a suitable value in `crypto_struct.h`.
2219 */
2220#define PSA_AEAD_OPERATION_INIT {0}
2221#endif
2222
2223/** Return an initial value for an AEAD operation object.
2224 */
2225static psa_aead_operation_t psa_aead_operation_init(void);
2226
2227/** Set the key for a multipart authenticated encryption operation.
2228 *
2229 * The sequence of operations to encrypt a message with authentication
2230 * is as follows:
2231 * -# Allocate an operation object which will be passed to all the functions
2232 * listed here.
2233 * -# Initialize the operation object with one of the methods described in the
2234 * documentation for #psa_aead_operation_t, e.g.
2235 * PSA_AEAD_OPERATION_INIT.
2236 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002237 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2238 * inputs to the subsequent calls to psa_aead_update_ad() and
2239 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2240 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002241 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2242 * generate or set the nonce. You should use
2243 * psa_aead_generate_nonce() unless the protocol you are implementing
2244 * requires a specific nonce value.
2245 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2246 * of the non-encrypted additional authenticated data each time.
2247 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002248 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002249 * -# Call psa_aead_finish().
2250 *
2251 * The application may call psa_aead_abort() at any time after the operation
2252 * has been initialized.
2253 *
2254 * After a successful call to psa_aead_encrypt_setup(), the application must
2255 * eventually terminate the operation. The following events terminate an
2256 * operation:
2257 * - A failed call to any of the \c psa_aead_xxx functions.
2258 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2259 *
2260 * \param[in,out] operation The operation object to set up. It must have
2261 * been initialized as per the documentation for
2262 * #psa_aead_operation_t and not yet in use.
2263 * \param handle Handle to the key to use for the operation.
2264 * It must remain valid until the operation
2265 * terminates.
2266 * \param alg The AEAD algorithm to compute
2267 * (\c PSA_ALG_XXX value such that
2268 * #PSA_ALG_IS_AEAD(\p alg) is true).
2269 *
2270 * \retval #PSA_SUCCESS
2271 * Success.
2272 * \retval #PSA_ERROR_INVALID_HANDLE
2273 * \retval #PSA_ERROR_EMPTY_SLOT
2274 * \retval #PSA_ERROR_NOT_PERMITTED
2275 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002276 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002277 * \retval #PSA_ERROR_NOT_SUPPORTED
2278 * \p alg is not supported or is not an AEAD algorithm.
2279 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2280 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2281 * \retval #PSA_ERROR_HARDWARE_FAILURE
2282 * \retval #PSA_ERROR_TAMPERING_DETECTED
2283 * \retval #PSA_ERROR_BAD_STATE
2284 * The library has not been previously initialized by psa_crypto_init().
2285 * It is implementation-dependent whether a failure to initialize
2286 * results in this error code.
2287 */
2288psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2289 psa_key_handle_t handle,
2290 psa_algorithm_t alg);
2291
2292/** Set the key for a multipart authenticated decryption operation.
2293 *
2294 * The sequence of operations to decrypt a message with authentication
2295 * is as follows:
2296 * -# Allocate an operation object which will be passed to all the functions
2297 * listed here.
2298 * -# Initialize the operation object with one of the methods described in the
2299 * documentation for #psa_aead_operation_t, e.g.
2300 * PSA_AEAD_OPERATION_INIT.
2301 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002302 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2303 * inputs to the subsequent calls to psa_aead_update_ad() and
2304 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2305 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002306 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2307 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2308 * of the non-encrypted additional authenticated data each time.
2309 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002310 * of the ciphertext to decrypt each time.
2311 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002312 *
2313 * The application may call psa_aead_abort() at any time after the operation
2314 * has been initialized.
2315 *
2316 * After a successful call to psa_aead_decrypt_setup(), the application must
2317 * eventually terminate the operation. The following events terminate an
2318 * operation:
2319 * - A failed call to any of the \c psa_aead_xxx functions.
2320 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2321 *
2322 * \param[in,out] operation The operation object to set up. It must have
2323 * been initialized as per the documentation for
2324 * #psa_aead_operation_t and not yet in use.
2325 * \param handle Handle to the key to use for the operation.
2326 * It must remain valid until the operation
2327 * terminates.
2328 * \param alg The AEAD algorithm to compute
2329 * (\c PSA_ALG_XXX value such that
2330 * #PSA_ALG_IS_AEAD(\p alg) is true).
2331 *
2332 * \retval #PSA_SUCCESS
2333 * Success.
2334 * \retval #PSA_ERROR_INVALID_HANDLE
2335 * \retval #PSA_ERROR_EMPTY_SLOT
2336 * \retval #PSA_ERROR_NOT_PERMITTED
2337 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002338 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002339 * \retval #PSA_ERROR_NOT_SUPPORTED
2340 * \p alg is not supported or is not an AEAD algorithm.
2341 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2342 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2343 * \retval #PSA_ERROR_HARDWARE_FAILURE
2344 * \retval #PSA_ERROR_TAMPERING_DETECTED
2345 * \retval #PSA_ERROR_BAD_STATE
2346 * The library has not been previously initialized by psa_crypto_init().
2347 * It is implementation-dependent whether a failure to initialize
2348 * results in this error code.
2349 */
2350psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2351 psa_key_handle_t handle,
2352 psa_algorithm_t alg);
2353
2354/** Generate a random nonce for an authenticated encryption operation.
2355 *
2356 * This function generates a random nonce for the authenticated encryption
2357 * operation with an appropriate size for the chosen algorithm, key type
2358 * and key size.
2359 *
2360 * The application must call psa_aead_encrypt_setup() before
2361 * calling this function.
2362 *
2363 * If this function returns an error status, the operation becomes inactive.
2364 *
2365 * \param[in,out] operation Active AEAD operation.
2366 * \param[out] nonce Buffer where the generated nonce is to be
2367 * written.
2368 * \param nonce_size Size of the \p nonce buffer in bytes.
2369 * \param[out] nonce_length On success, the number of bytes of the
2370 * generated nonce.
2371 *
2372 * \retval #PSA_SUCCESS
2373 * Success.
2374 * \retval #PSA_ERROR_BAD_STATE
2375 * The operation state is not valid (not set up, or nonce already set).
2376 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2377 * The size of the \p nonce buffer is too small.
2378 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2379 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2380 * \retval #PSA_ERROR_HARDWARE_FAILURE
2381 * \retval #PSA_ERROR_TAMPERING_DETECTED
2382 */
2383psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2384 unsigned char *nonce,
2385 size_t nonce_size,
2386 size_t *nonce_length);
2387
2388/** Set the nonce for an authenticated encryption or decryption operation.
2389 *
2390 * This function sets the nonce for the authenticated
2391 * encryption or decryption operation.
2392 *
2393 * The application must call psa_aead_encrypt_setup() before
2394 * calling this function.
2395 *
2396 * If this function returns an error status, the operation becomes inactive.
2397 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002398 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002399 * instead of this function, unless implementing a protocol that requires
2400 * a non-random IV.
2401 *
2402 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002403 * \param[in] nonce Buffer containing the nonce to use.
2404 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002405 *
2406 * \retval #PSA_SUCCESS
2407 * Success.
2408 * \retval #PSA_ERROR_BAD_STATE
2409 * The operation state is not valid (not set up, or nonce already set).
2410 * \retval #PSA_ERROR_INVALID_ARGUMENT
2411 * The size of \p nonce is not acceptable for the chosen algorithm.
2412 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2413 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2414 * \retval #PSA_ERROR_HARDWARE_FAILURE
2415 * \retval #PSA_ERROR_TAMPERING_DETECTED
2416 */
2417psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2418 const unsigned char *nonce,
2419 size_t nonce_length);
2420
Gilles Peskinebc59c852019-01-17 15:26:08 +01002421/** Declare the lengths of the message and additional data for AEAD.
2422 *
2423 * The application must call this function before calling
2424 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2425 * the operation requires it. If the algorithm does not require it,
2426 * calling this function is optional, but if this function is called
2427 * then the implementation must enforce the lengths.
2428 *
2429 * You may call this function before or after setting the nonce with
2430 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2431 *
2432 * - For #PSA_ALG_CCM, calling this function is required.
2433 * - For the other AEAD algorithms defined in this specification, calling
2434 * this function is not required.
2435 * - For vendor-defined algorithm, refer to the vendor documentation.
2436 *
2437 * \param[in,out] operation Active AEAD operation.
2438 * \param ad_length Size of the non-encrypted additional
2439 * authenticated data in bytes.
2440 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2441 *
2442 * \retval #PSA_SUCCESS
2443 * Success.
2444 * \retval #PSA_ERROR_BAD_STATE
2445 * The operation state is not valid (not set up, already completed,
2446 * or psa_aead_update_ad() or psa_aead_update() already called).
2447 * \retval #PSA_ERROR_INVALID_ARGUMENT
2448 * At least one of the lengths is not acceptable for the chosen
2449 * algorithm.
2450 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2451 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2452 * \retval #PSA_ERROR_HARDWARE_FAILURE
2453 * \retval #PSA_ERROR_TAMPERING_DETECTED
2454 */
2455psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2456 size_t ad_length,
2457 size_t plaintext_length);
2458
Gilles Peskine30a9e412019-01-14 18:36:12 +01002459/** Pass additional data to an active AEAD operation.
2460 *
2461 * Additional data is authenticated, but not encrypted.
2462 *
2463 * You may call this function multiple times to pass successive fragments
2464 * of the additional data. You may not call this function after passing
2465 * data to encrypt or decrypt with psa_aead_update().
2466 *
2467 * Before calling this function, you must:
2468 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2469 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2470 *
2471 * If this function returns an error status, the operation becomes inactive.
2472 *
2473 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2474 * there is no guarantee that the input is valid. Therefore, until
2475 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2476 * treat the input as untrusted and prepare to undo any action that
2477 * depends on the input if psa_aead_verify() returns an error status.
2478 *
2479 * \param[in,out] operation Active AEAD operation.
2480 * \param[in] input Buffer containing the fragment of
2481 * additional data.
2482 * \param input_length Size of the \p input buffer in bytes.
2483 *
2484 * \retval #PSA_SUCCESS
2485 * Success.
2486 * \retval #PSA_ERROR_BAD_STATE
2487 * The operation state is not valid (not set up, nonce not set,
2488 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002489 * \retval #PSA_ERROR_INVALID_ARGUMENT
2490 * The total input length overflows the additional data length that
2491 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002492 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2493 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2494 * \retval #PSA_ERROR_HARDWARE_FAILURE
2495 * \retval #PSA_ERROR_TAMPERING_DETECTED
2496 */
2497psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2498 const uint8_t *input,
2499 size_t input_length);
2500
2501/** Encrypt or decrypt a message fragment in an active AEAD operation.
2502 *
2503 * Before calling this function, you must:
2504 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2505 * The choice of setup function determines whether this function
2506 * encrypts or decrypts its input.
2507 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2508 * 3. Call psa_aead_update_ad() to pass all the additional data.
2509 *
2510 * If this function returns an error status, the operation becomes inactive.
2511 *
2512 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2513 * there is no guarantee that the input is valid. Therefore, until
2514 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2515 * - Do not use the output in any way other than storing it in a
2516 * confidential location. If you take any action that depends
2517 * on the tentative decrypted data, this action will need to be
2518 * undone if the input turns out not to be valid. Furthermore,
2519 * if an adversary can observe that this action took place
2520 * (for example through timing), they may be able to use this
2521 * fact as an oracle to decrypt any message encrypted with the
2522 * same key.
2523 * - In particular, do not copy the output anywhere but to a
2524 * memory or storage space that you have exclusive access to.
2525 *
2526 * \param[in,out] operation Active AEAD operation.
2527 * \param[in] input Buffer containing the message fragment to
2528 * encrypt or decrypt.
2529 * \param input_length Size of the \p input buffer in bytes.
2530 * \param[out] output Buffer where the output is to be written.
2531 * \param output_size Size of the \p output buffer in bytes.
2532 * \param[out] output_length On success, the number of bytes
2533 * that make up the returned output.
2534 *
2535 * \retval #PSA_SUCCESS
2536 * Success.
2537 * \retval #PSA_ERROR_BAD_STATE
2538 * The operation state is not valid (not set up, nonce not set
2539 * or already completed).
2540 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2541 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002542 * \retval #PSA_ERROR_INVALID_ARGUMENT
2543 * The total length of input to psa_aead_update_ad() so far is
2544 * less than the additional data length that was previously
2545 * specified with psa_aead_set_lengths().
2546 * \retval #PSA_ERROR_INVALID_ARGUMENT
2547 * The total input length overflows the plaintext length that
2548 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002549 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2550 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2551 * \retval #PSA_ERROR_HARDWARE_FAILURE
2552 * \retval #PSA_ERROR_TAMPERING_DETECTED
2553 */
2554psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2555 const uint8_t *input,
2556 size_t input_length,
2557 unsigned char *output,
2558 size_t output_size,
2559 size_t *output_length);
2560
2561/** Finish encrypting a message in an AEAD operation.
2562 *
2563 * The operation must have been set up with psa_aead_encrypt_setup().
2564 *
2565 * This function finishes the authentication of the additional data
2566 * formed by concatenating the inputs passed to preceding calls to
2567 * psa_aead_update_ad() with the plaintext formed by concatenating the
2568 * inputs passed to preceding calls to psa_aead_update().
2569 *
2570 * This function has two output buffers:
2571 * - \p ciphertext contains trailing ciphertext that was buffered from
2572 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2573 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2574 * will not contain any output and can be a 0-sized buffer.
2575 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002576 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002577 * that the operation performs.
2578 *
2579 * When this function returns, the operation becomes inactive.
2580 *
2581 * \param[in,out] operation Active AEAD operation.
2582 * \param[out] ciphertext Buffer where the last part of the ciphertext
2583 * is to be written.
2584 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2585 * \param[out] ciphertext_length On success, the number of bytes of
2586 * returned ciphertext.
2587 * \param[out] tag Buffer where the authentication tag is
2588 * to be written.
2589 * \param tag_size Size of the \p tag buffer in bytes.
2590 * \param[out] tag_length On success, the number of bytes
2591 * that make up the returned tag.
2592 *
2593 * \retval #PSA_SUCCESS
2594 * Success.
2595 * \retval #PSA_ERROR_BAD_STATE
2596 * The operation state is not valid (not set up, nonce not set,
2597 * decryption, or already completed).
2598 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002599 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002600 * \retval #PSA_ERROR_INVALID_ARGUMENT
2601 * The total length of input to psa_aead_update_ad() so far is
2602 * less than the additional data length that was previously
2603 * specified with psa_aead_set_lengths().
2604 * \retval #PSA_ERROR_INVALID_ARGUMENT
2605 * The total length of input to psa_aead_update() so far is
2606 * less than the plaintext length that was previously
2607 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002608 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2609 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2610 * \retval #PSA_ERROR_HARDWARE_FAILURE
2611 * \retval #PSA_ERROR_TAMPERING_DETECTED
2612 */
2613psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002614 uint8_t *ciphertext,
2615 size_t ciphertext_size,
2616 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002617 uint8_t *tag,
2618 size_t tag_size,
2619 size_t *tag_length);
2620
2621/** Finish authenticating and decrypting a message in an AEAD operation.
2622 *
2623 * The operation must have been set up with psa_aead_decrypt_setup().
2624 *
2625 * This function finishes the authentication of the additional data
2626 * formed by concatenating the inputs passed to preceding calls to
2627 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2628 * inputs passed to preceding calls to psa_aead_update().
2629 *
2630 * When this function returns, the operation becomes inactive.
2631 *
2632 * \param[in,out] operation Active AEAD operation.
2633 * \param[in] tag Buffer containing the authentication tag.
2634 * \param tag_length Size of the \p tag buffer in bytes.
2635 *
2636 * \retval #PSA_SUCCESS
2637 * Success.
2638 * \retval #PSA_ERROR_BAD_STATE
2639 * The operation state is not valid (not set up, nonce not set,
2640 * encryption, or already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002641 * \retval #PSA_ERROR_INVALID_ARGUMENT
2642 * The total length of input to psa_aead_update_ad() so far is
2643 * less than the additional data length that was previously
2644 * specified with psa_aead_set_lengths().
2645 * \retval #PSA_ERROR_INVALID_ARGUMENT
2646 * The total length of input to psa_aead_update() so far is
2647 * less than the plaintext length that was previously
2648 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002649 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2650 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2651 * \retval #PSA_ERROR_HARDWARE_FAILURE
2652 * \retval #PSA_ERROR_TAMPERING_DETECTED
2653 */
2654psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2655 const uint8_t *tag,
2656 size_t tag_length);
2657
2658/** Abort an AEAD operation.
2659 *
2660 * Aborting an operation frees all associated resources except for the
2661 * \p operation structure itself. Once aborted, the operation object
2662 * can be reused for another operation by calling
2663 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2664 *
2665 * You may call this function any time after the operation object has
2666 * been initialized by any of the following methods:
2667 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2668 * whether it succeeds or not.
2669 * - Initializing the \c struct to all-bits-zero.
2670 * - Initializing the \c struct to logical zeros, e.g.
2671 * `psa_aead_operation_t operation = {0}`.
2672 *
2673 * In particular, calling psa_aead_abort() after the operation has been
2674 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2675 * is safe and has no effect.
2676 *
2677 * \param[in,out] operation Initialized AEAD operation.
2678 *
2679 * \retval #PSA_SUCCESS
2680 * \retval #PSA_ERROR_BAD_STATE
2681 * \p operation is not an active AEAD operation.
2682 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2683 * \retval #PSA_ERROR_HARDWARE_FAILURE
2684 * \retval #PSA_ERROR_TAMPERING_DETECTED
2685 */
2686psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2687
Gilles Peskine3b555712018-03-03 21:27:57 +01002688/**@}*/
2689
Gilles Peskine20035e32018-02-03 22:44:14 +01002690/** \defgroup asymmetric Asymmetric cryptography
2691 * @{
2692 */
2693
2694/**
2695 * \brief Sign a hash or short message with a private key.
2696 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002697 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002698 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002699 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2700 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2701 * to determine the hash algorithm to use.
2702 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002703 * \param handle Handle to the key to use for the operation.
2704 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002705 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002706 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002707 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002708 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002709 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002710 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002711 * \param[out] signature_length On success, the number of bytes
2712 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002713 *
Gilles Peskine28538492018-07-11 17:34:00 +02002714 * \retval #PSA_SUCCESS
2715 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002716 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002717 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002718 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002719 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002720 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002721 * \retval #PSA_ERROR_NOT_SUPPORTED
2722 * \retval #PSA_ERROR_INVALID_ARGUMENT
2723 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2724 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2725 * \retval #PSA_ERROR_HARDWARE_FAILURE
2726 * \retval #PSA_ERROR_TAMPERING_DETECTED
2727 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002728 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002729 * The library has not been previously initialized by psa_crypto_init().
2730 * It is implementation-dependent whether a failure to initialize
2731 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002732 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002733psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002734 psa_algorithm_t alg,
2735 const uint8_t *hash,
2736 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002737 uint8_t *signature,
2738 size_t signature_size,
2739 size_t *signature_length);
2740
2741/**
2742 * \brief Verify the signature a hash or short message using a public key.
2743 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002744 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002745 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002746 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2747 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2748 * to determine the hash algorithm to use.
2749 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002750 * \param handle Handle to the key to use for the operation.
2751 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002752 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002753 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002754 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002755 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002756 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002757 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002758 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002759 *
Gilles Peskine28538492018-07-11 17:34:00 +02002760 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002761 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002762 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002763 * The calculation was perfomed successfully, but the passed
2764 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002765 * \retval #PSA_ERROR_NOT_SUPPORTED
2766 * \retval #PSA_ERROR_INVALID_ARGUMENT
2767 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2768 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2769 * \retval #PSA_ERROR_HARDWARE_FAILURE
2770 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002771 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002772 * The library has not been previously initialized by psa_crypto_init().
2773 * It is implementation-dependent whether a failure to initialize
2774 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002775 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002776psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002777 psa_algorithm_t alg,
2778 const uint8_t *hash,
2779 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002780 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002781 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002782
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002783/**
2784 * \brief Encrypt a short message with a public key.
2785 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002786 * \param handle Handle to the key to use for the operation.
2787 * It must be a public key or an asymmetric
2788 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002789 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002790 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002791 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002792 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002793 * \param[in] salt A salt or label, if supported by the
2794 * encryption algorithm.
2795 * If the algorithm does not support a
2796 * salt, pass \c NULL.
2797 * If the algorithm supports an optional
2798 * salt and you do not want to pass a salt,
2799 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002800 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002801 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2802 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002803 * \param salt_length Size of the \p salt buffer in bytes.
2804 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002805 * \param[out] output Buffer where the encrypted message is to
2806 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002807 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002808 * \param[out] output_length On success, the number of bytes
2809 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002810 *
Gilles Peskine28538492018-07-11 17:34:00 +02002811 * \retval #PSA_SUCCESS
2812 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002813 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002814 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002815 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002816 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002817 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002818 * \retval #PSA_ERROR_NOT_SUPPORTED
2819 * \retval #PSA_ERROR_INVALID_ARGUMENT
2820 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2821 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2822 * \retval #PSA_ERROR_HARDWARE_FAILURE
2823 * \retval #PSA_ERROR_TAMPERING_DETECTED
2824 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002825 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002826 * The library has not been previously initialized by psa_crypto_init().
2827 * It is implementation-dependent whether a failure to initialize
2828 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002829 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002830psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002831 psa_algorithm_t alg,
2832 const uint8_t *input,
2833 size_t input_length,
2834 const uint8_t *salt,
2835 size_t salt_length,
2836 uint8_t *output,
2837 size_t output_size,
2838 size_t *output_length);
2839
2840/**
2841 * \brief Decrypt a short message with a private key.
2842 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002843 * \param handle Handle to the key to use for the operation.
2844 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002845 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002846 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002847 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002848 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002849 * \param[in] salt A salt or label, if supported by the
2850 * encryption algorithm.
2851 * If the algorithm does not support a
2852 * salt, pass \c NULL.
2853 * If the algorithm supports an optional
2854 * salt and you do not want to pass a salt,
2855 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002856 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002857 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2858 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002859 * \param salt_length Size of the \p salt buffer in bytes.
2860 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002861 * \param[out] output Buffer where the decrypted message is to
2862 * be written.
2863 * \param output_size Size of the \c output buffer in bytes.
2864 * \param[out] output_length On success, the number of bytes
2865 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002866 *
Gilles Peskine28538492018-07-11 17:34:00 +02002867 * \retval #PSA_SUCCESS
2868 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002869 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002870 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002871 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002872 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002873 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002874 * \retval #PSA_ERROR_NOT_SUPPORTED
2875 * \retval #PSA_ERROR_INVALID_ARGUMENT
2876 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2877 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2878 * \retval #PSA_ERROR_HARDWARE_FAILURE
2879 * \retval #PSA_ERROR_TAMPERING_DETECTED
2880 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2881 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002882 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002883 * The library has not been previously initialized by psa_crypto_init().
2884 * It is implementation-dependent whether a failure to initialize
2885 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002886 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002887psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002888 psa_algorithm_t alg,
2889 const uint8_t *input,
2890 size_t input_length,
2891 const uint8_t *salt,
2892 size_t salt_length,
2893 uint8_t *output,
2894 size_t output_size,
2895 size_t *output_length);
2896
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002897/**@}*/
2898
Gilles Peskineedd76872018-07-20 17:42:05 +02002899/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002900 * @{
2901 */
2902
2903/** The type of the state data structure for generators.
2904 *
2905 * Before calling any function on a generator, the application must
2906 * initialize it by any of the following means:
2907 * - Set the structure to all-bits-zero, for example:
2908 * \code
2909 * psa_crypto_generator_t generator;
2910 * memset(&generator, 0, sizeof(generator));
2911 * \endcode
2912 * - Initialize the structure to logical zero values, for example:
2913 * \code
2914 * psa_crypto_generator_t generator = {0};
2915 * \endcode
2916 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2917 * for example:
2918 * \code
2919 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2920 * \endcode
2921 * - Assign the result of the function psa_crypto_generator_init()
2922 * to the structure, for example:
2923 * \code
2924 * psa_crypto_generator_t generator;
2925 * generator = psa_crypto_generator_init();
2926 * \endcode
2927 *
2928 * This is an implementation-defined \c struct. Applications should not
2929 * make any assumptions about the content of this structure except
2930 * as directed by the documentation of a specific implementation.
2931 */
2932typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2933
2934/** \def PSA_CRYPTO_GENERATOR_INIT
2935 *
2936 * This macro returns a suitable initializer for a generator object
2937 * of type #psa_crypto_generator_t.
2938 */
2939#ifdef __DOXYGEN_ONLY__
2940/* This is an example definition for documentation purposes.
2941 * Implementations should define a suitable value in `crypto_struct.h`.
2942 */
2943#define PSA_CRYPTO_GENERATOR_INIT {0}
2944#endif
2945
2946/** Return an initial value for a generator object.
2947 */
2948static psa_crypto_generator_t psa_crypto_generator_init(void);
2949
2950/** Retrieve the current capacity of a generator.
2951 *
2952 * The capacity of a generator is the maximum number of bytes that it can
2953 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2954 *
2955 * \param[in] generator The generator to query.
2956 * \param[out] capacity On success, the capacity of the generator.
2957 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002958 * \retval #PSA_SUCCESS
2959 * \retval #PSA_ERROR_BAD_STATE
2960 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002961 */
2962psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2963 size_t *capacity);
2964
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002965/** Set the maximum capacity of a generator.
2966 *
2967 * \param[in,out] generator The generator object to modify.
2968 * \param capacity The new capacity of the generator.
2969 * It must be less or equal to the generator's
2970 * current capacity.
2971 *
2972 * \retval #PSA_SUCCESS
2973 * \retval #PSA_ERROR_INVALID_ARGUMENT
2974 * \p capacity is larger than the generator's current capacity.
2975 * \retval #PSA_ERROR_BAD_STATE
2976 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2977 */
2978psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
2979 size_t capacity);
2980
Gilles Peskineeab56e42018-07-12 17:12:33 +02002981/** Read some data from a generator.
2982 *
2983 * This function reads and returns a sequence of bytes from a generator.
2984 * The data that is read is discarded from the generator. The generator's
2985 * capacity is decreased by the number of bytes read.
2986 *
2987 * \param[in,out] generator The generator object to read from.
2988 * \param[out] output Buffer where the generator output will be
2989 * written.
2990 * \param output_length Number of bytes to output.
2991 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002992 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02002993 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskineeab56e42018-07-12 17:12:33 +02002994 * There were fewer than \p output_length bytes
2995 * in the generator. Note that in this case, no
2996 * output is written to the output buffer.
2997 * The generator's capacity is set to 0, thus
2998 * subsequent calls to this function will not
2999 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003000 * \retval #PSA_ERROR_BAD_STATE
3001 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3002 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3003 * \retval #PSA_ERROR_HARDWARE_FAILURE
3004 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003005 */
3006psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
3007 uint8_t *output,
3008 size_t output_length);
3009
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003010/** Generate a key deterministically from data read from a generator.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003011 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003012 * This function uses the output of a generator to derive a key.
3013 * How much output it consumes and how the key is derived depends on the
3014 * key type.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003015 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003016 * - For key types for which the key is an arbitrary sequence of bytes
3017 * of a given size,
3018 * this function is functionally equivalent to calling #psa_generator_read
3019 * and passing the resulting output to #psa_import_key.
3020 * However, this function has a security benefit:
3021 * if the implementation provides an isolation boundary then
3022 * the key material is not exposed outside the isolation boundary.
3023 * As a consequence, for these key types, this function always consumes
3024 * exactly (\p bits / 8) bytes from the generator.
3025 * The following key types defined in this specification follow this scheme:
3026 *
3027 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003028 * - #PSA_KEY_TYPE_ARC4;
3029 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003030 * - #PSA_KEY_TYPE_DERIVE;
3031 * - #PSA_KEY_TYPE_HMAC.
3032 *
3033 * - For ECC keys on a Montgomery elliptic curve
3034 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3035 * Montgomery curve), this function always draws a byte string whose
3036 * length is determined by the curve, and sets the mandatory bits
3037 * accordingly. That is:
3038 *
3039 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3040 * and process it as specified in RFC 7748 &sect;5.
3041 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3042 * and process it as specified in RFC 7748 &sect;5.
3043 *
3044 * - For key types for which the key is represented by a single sequence of
3045 * \p bits bits with constraints as to which bit sequences are acceptable,
3046 * this function draws a byte string of length (\p bits / 8) bytes rounded
3047 * up to the nearest whole number of bytes. If the resulting byte string
3048 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3049 * This process is repeated until an acceptable byte string is drawn.
3050 * The byte string drawn from the generator is interpreted as specified
3051 * for the output produced by psa_export_key().
3052 * The following key types defined in this specification follow this scheme:
3053 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003054 * - #PSA_KEY_TYPE_DES.
3055 * Force-set the parity bits, but discard forbidden weak keys.
3056 * For 2-key and 3-key triple-DES, the three keys are generated
3057 * successively (for example, for 3-key triple-DES,
3058 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3059 * discard the first 8 bytes, use the next 8 bytes as the first key,
3060 * and continue reading output from the generator to derive the other
3061 * two keys).
3062 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR),
3063 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
3064 * ECC keys on a Weierstrass elliptic curve
3065 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3066 * Weierstrass curve).
3067 * For these key types, interpret the byte string as integer
3068 * in big-endian order. Discard it if it is not in the range
3069 * [0, *N* - 2] where *N* is the boundary of the private key domain
3070 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003071 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003072 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003073 * This method allows compliance to NIST standards, specifically
3074 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003075 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3076 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3077 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3078 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003079 *
3080 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
3081 * the way in which the generator output is consumed is
3082 * implementation-defined.
3083 *
3084 * In all cases, the data that is read is discarded from the generator.
3085 * The generator's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003086 *
Gilles Peskine20628592019-04-19 19:29:50 +02003087 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003088 * \param[out] handle On success, a handle to the newly created key.
3089 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003090 * \param[in,out] generator The generator object to read from.
3091 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003092 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003093 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003094 * If the key is persistent, the key material and the key's metadata
3095 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003096 * \retval #PSA_ERROR_ALREADY_EXISTS
3097 * This is an attempt to create a persistent key, and there is
3098 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003099 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003100 * There was not enough data to create the desired key.
3101 * Note that in this case, no output is written to the output buffer.
3102 * The generator's capacity is set to 0, thus subsequent calls to
3103 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003104 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003105 * The key type or key size is not supported, either by the
3106 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003107 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003108 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3109 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3110 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3111 * \retval #PSA_ERROR_HARDWARE_FAILURE
3112 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003113 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003114 * The library has not been previously initialized by psa_crypto_init().
3115 * It is implementation-dependent whether a failure to initialize
3116 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003117 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003118psa_status_t psa_generate_derived_key(const psa_key_attributes_t *attributes,
Gilles Peskine87a5e562019-04-17 12:28:25 +02003119 psa_key_handle_t *handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003120 psa_crypto_generator_t *generator);
3121
3122/** Abort a generator.
3123 *
3124 * Once a generator has been aborted, its capacity is zero.
3125 * Aborting a generator frees all associated resources except for the
3126 * \c generator structure itself.
3127 *
3128 * This function may be called at any time as long as the generator
3129 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
3130 * psa_crypto_generator_init() or a zero value. In particular, it is valid
3131 * to call psa_generator_abort() twice, or to call psa_generator_abort()
3132 * on a generator that has not been set up.
3133 *
3134 * Once aborted, the generator object may be called.
3135 *
3136 * \param[in,out] generator The generator to abort.
3137 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003138 * \retval #PSA_SUCCESS
3139 * \retval #PSA_ERROR_BAD_STATE
3140 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3141 * \retval #PSA_ERROR_HARDWARE_FAILURE
3142 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003143 */
3144psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3145
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003146/** Use the maximum possible capacity for a generator.
3147 *
3148 * Use this value as the capacity argument when setting up a generator
3149 * to indicate that the generator should have the maximum possible capacity.
3150 * The value of the maximum possible capacity depends on the generator
3151 * algorithm.
3152 */
3153#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
3154
Gilles Peskineeab56e42018-07-12 17:12:33 +02003155/**@}*/
3156
Gilles Peskineea0fb492018-07-12 17:17:20 +02003157/** \defgroup derivation Key derivation
3158 * @{
3159 */
3160
3161/** Set up a key derivation operation.
3162 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003163 * A key derivation algorithm takes some inputs and uses them to create
3164 * a byte generator which can be used to produce keys and other
3165 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003166 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003167 * To use a generator for key derivation:
3168 * - Start with an initialized object of type #psa_crypto_generator_t.
3169 * - Call psa_key_derivation_setup() to select the algorithm.
3170 * - Provide the inputs for the key derivation by calling
3171 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3172 * as appropriate. Which inputs are needed, in what order, and whether
3173 * they may be keys and if so of what type depends on the algorithm.
3174 * - Optionally set the generator's maximum capacity with
3175 * psa_set_generator_capacity(). You may do this before, in the middle of
3176 * or after providing inputs. For some algorithms, this step is mandatory
3177 * because the output depends on the maximum capacity.
3178 * - Generate output with psa_generator_read() or
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003179 * psa_generate_derived_key(). Successive calls to these functions
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003180 * use successive output bytes from the generator.
3181 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02003182 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003183 * \param[in,out] generator The generator object to set up. It must
3184 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003185 * \param alg The key derivation algorithm to compute
3186 * (\c PSA_ALG_XXX value such that
3187 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02003188 *
3189 * \retval #PSA_SUCCESS
3190 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003191 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003192 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003193 * \retval #PSA_ERROR_NOT_SUPPORTED
3194 * \c alg is not supported or is not a key derivation algorithm.
3195 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3196 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3197 * \retval #PSA_ERROR_HARDWARE_FAILURE
3198 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003199 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003200 */
3201psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
3202 psa_algorithm_t alg);
3203
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003204/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003205 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003206 * Which inputs are required and in what order depends on the algorithm.
3207 * Refer to the documentation of each key derivation or key agreement
3208 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003209 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003210 * This function passes direct inputs. Some inputs must be passed as keys
3211 * using psa_key_derivation_input_key() instead of this function. Refer to
3212 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003213 *
3214 * \param[in,out] generator The generator object to use. It must
3215 * have been set up with
3216 * psa_key_derivation_setup() and must not
3217 * have produced any output yet.
3218 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003219 * \param[in] data Input data to use.
3220 * \param data_length Size of the \p data buffer in bytes.
3221 *
3222 * \retval #PSA_SUCCESS
3223 * Success.
3224 * \retval #PSA_ERROR_INVALID_ARGUMENT
3225 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003226 * \retval #PSA_ERROR_INVALID_ARGUMENT
3227 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003228 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3229 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3230 * \retval #PSA_ERROR_HARDWARE_FAILURE
3231 * \retval #PSA_ERROR_TAMPERING_DETECTED
3232 * \retval #PSA_ERROR_BAD_STATE
3233 * The value of \p step is not valid given the state of \p generator.
3234 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003235 * The library has not been previously initialized by psa_crypto_init().
3236 * It is implementation-dependent whether a failure to initialize
3237 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003238 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003239psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3240 psa_key_derivation_step_t step,
3241 const uint8_t *data,
3242 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003243
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003244/** Provide an input for key derivation in the form of a key.
3245 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003246 * Which inputs are required and in what order depends on the algorithm.
3247 * Refer to the documentation of each key derivation or key agreement
3248 * algorithm for information.
3249 *
3250 * This function passes key inputs. Some inputs must be passed as keys
3251 * of the appropriate type using this function, while others must be
3252 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3253 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003254 *
3255 * \param[in,out] generator The generator object to use. It must
3256 * have been set up with
3257 * psa_key_derivation_setup() and must not
3258 * have produced any output yet.
3259 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003260 * \param handle Handle to the key. It must have an
3261 * appropriate type for \p step and must
3262 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003263 *
3264 * \retval #PSA_SUCCESS
3265 * Success.
3266 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine31351842019-04-09 12:00:00 +02003267 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003268 * \retval #PSA_ERROR_NOT_PERMITTED
3269 * \retval #PSA_ERROR_INVALID_ARGUMENT
3270 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003271 * \retval #PSA_ERROR_INVALID_ARGUMENT
3272 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003273 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3274 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3275 * \retval #PSA_ERROR_HARDWARE_FAILURE
3276 * \retval #PSA_ERROR_TAMPERING_DETECTED
3277 * \retval #PSA_ERROR_BAD_STATE
3278 * The value of \p step is not valid given the state of \p generator.
3279 * \retval #PSA_ERROR_BAD_STATE
3280 * The library has not been previously initialized by psa_crypto_init().
3281 * It is implementation-dependent whether a failure to initialize
3282 * results in this error code.
3283 */
3284psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3285 psa_key_derivation_step_t step,
3286 psa_key_handle_t handle);
3287
Gilles Peskine969c5d62019-01-16 15:53:06 +01003288/** Perform a key agreement and use the shared secret as input to a key
3289 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003290 *
3291 * A key agreement algorithm takes two inputs: a private key \p private_key
3292 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003293 * The result of this function is passed as input to a key derivation.
3294 * The output of this key derivation can be extracted by reading from the
3295 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003296 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003297 * \param[in,out] generator The generator object to use. It must
3298 * have been set up with
3299 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003300 * key agreement and derivation algorithm
3301 * \c alg (\c PSA_ALG_XXX value such that
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003302 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3303 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
Gilles Peskine6843c292019-01-18 16:44:49 +01003304 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003305 * The generator must be ready for an
3306 * input of the type given by \p step.
3307 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003308 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003309 * \param[in] peer_key Public key of the peer. The peer key must be in the
3310 * same format that psa_import_key() accepts for the
3311 * public key type corresponding to the type of
3312 * private_key. That is, this function performs the
3313 * equivalent of
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003314 * #psa_import_key(`internal_public_key_handle`,
3315 * #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(`private_key_type`),
3316 * `peer_key`, `peer_key_length`) where
Jaeden Amero8afbff82019-01-14 16:56:20 +00003317 * `private_key_type` is the type of `private_key`.
3318 * For example, for EC keys, this means that peer_key
3319 * is interpreted as a point on the curve that the
3320 * private key is on. The standard formats for public
3321 * keys are documented in the documentation of
3322 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003323 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003324 *
3325 * \retval #PSA_SUCCESS
3326 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003327 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02003328 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine01d718c2018-09-18 12:01:02 +02003329 * \retval #PSA_ERROR_NOT_PERMITTED
3330 * \retval #PSA_ERROR_INVALID_ARGUMENT
3331 * \c private_key is not compatible with \c alg,
3332 * or \p peer_key is not valid for \c alg or not compatible with
3333 * \c private_key.
3334 * \retval #PSA_ERROR_NOT_SUPPORTED
3335 * \c alg is not supported or is not a key derivation algorithm.
3336 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3337 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3338 * \retval #PSA_ERROR_HARDWARE_FAILURE
3339 * \retval #PSA_ERROR_TAMPERING_DETECTED
3340 */
3341psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003342 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003343 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003344 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003345 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003346
Gilles Peskine769c7a62019-01-18 16:42:29 +01003347/** Perform a key agreement and use the shared secret as input to a key
3348 * derivation.
3349 *
3350 * A key agreement algorithm takes two inputs: a private key \p private_key
3351 * a public key \p peer_key.
3352 *
3353 * \warning The raw result of a key agreement algorithm such as finite-field
3354 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3355 * not be used directly as key material. It should instead be passed as
3356 * input to a key derivation algorithm. To chain a key agreement with
3357 * a key derivation, use psa_key_agreement() and other functions from
3358 * the key derivation and generator interface.
3359 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003360 * \param alg The key agreement algorithm to compute
3361 * (\c PSA_ALG_XXX value such that
3362 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3363 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003364 * \param private_key Handle to the private key to use.
3365 * \param[in] peer_key Public key of the peer. It must be
3366 * in the same format that psa_import_key()
3367 * accepts. The standard formats for public
3368 * keys are documented in the documentation
3369 * of psa_export_public_key().
3370 * \param peer_key_length Size of \p peer_key in bytes.
3371 * \param[out] output Buffer where the decrypted message is to
3372 * be written.
3373 * \param output_size Size of the \c output buffer in bytes.
3374 * \param[out] output_length On success, the number of bytes
3375 * that make up the returned output.
3376 *
3377 * \retval #PSA_SUCCESS
3378 * Success.
3379 * \retval #PSA_ERROR_INVALID_HANDLE
3380 * \retval #PSA_ERROR_EMPTY_SLOT
3381 * \retval #PSA_ERROR_NOT_PERMITTED
3382 * \retval #PSA_ERROR_INVALID_ARGUMENT
3383 * \p alg is not a key agreement algorithm
3384 * \retval #PSA_ERROR_INVALID_ARGUMENT
3385 * \p private_key is not compatible with \p alg,
3386 * or \p peer_key is not valid for \p alg or not compatible with
3387 * \p private_key.
3388 * \retval #PSA_ERROR_NOT_SUPPORTED
3389 * \p alg is not a supported key agreement algorithm.
3390 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3391 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3392 * \retval #PSA_ERROR_HARDWARE_FAILURE
3393 * \retval #PSA_ERROR_TAMPERING_DETECTED
3394 */
3395psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3396 psa_key_handle_t private_key,
3397 const uint8_t *peer_key,
3398 size_t peer_key_length,
3399 uint8_t *output,
3400 size_t output_size,
3401 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003402
3403/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003404
3405/** \defgroup random Random generation
3406 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003407 */
3408
3409/**
3410 * \brief Generate random bytes.
3411 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003412 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003413 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003414 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003415 *
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003416 * \note To generate a key, use psa_generate_random_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003417 *
3418 * \param[out] output Output buffer for the generated data.
3419 * \param output_size Number of bytes to generate and output.
3420 *
3421 * \retval #PSA_SUCCESS
3422 * \retval #PSA_ERROR_NOT_SUPPORTED
3423 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3424 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3425 * \retval #PSA_ERROR_HARDWARE_FAILURE
3426 * \retval #PSA_ERROR_TAMPERING_DETECTED
3427 * \retval #PSA_ERROR_BAD_STATE
3428 * The library has not been previously initialized by psa_crypto_init().
3429 * It is implementation-dependent whether a failure to initialize
3430 * results in this error code.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003431 */
3432psa_status_t psa_generate_random(uint8_t *output,
3433 size_t output_size);
3434
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003435/**
3436 * \brief Generate a key or key pair.
3437 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003438 * The key is generated randomly.
3439 * Its location, policy, type and size are taken from \p attributes.
3440 *
3441 * If the type requires additional domain parameters, these are taken
3442 * from \p attributes as well. The following types use domain parameters:
3443 * - When generating an RSA key (#PSA_KEY_TYPE_RSA_KEYPAIR),
3444 * the default public exponent is 65537. This value is used if
3445 * \p attributes was set with psa_set_key_type() or by passing an empty
3446 * byte string as domain parameters to psa_set_key_domain_parameters().
3447 * If psa_set_key_domain_parameters() was used to set a non-empty
3448 * domain parameter string in \p attributes, this string is read as
3449 * a big-endian integer which is used as the public exponent.
3450 * - When generating a DSA key (#PSA_KEY_TYPE_DSA_KEYPAIR) or a
3451 * Diffie-Hellman key (#PSA_KEY_TYPE_DH_KEYPAIR), the domain parameters
3452 * from \p attributes are interpreted as described for
3453 * psa_set_key_domain_parameters().
3454 *
Gilles Peskine20628592019-04-19 19:29:50 +02003455 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003456 * \param[out] handle On success, a handle to the newly created key.
3457 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003458 *
Gilles Peskine28538492018-07-11 17:34:00 +02003459 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003460 * Success.
3461 * If the key is persistent, the key material and the key's metadata
3462 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003463 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003464 * This is an attempt to create a persistent key, and there is
3465 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003466 * \retval #PSA_ERROR_NOT_SUPPORTED
3467 * \retval #PSA_ERROR_INVALID_ARGUMENT
3468 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3469 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3470 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3471 * \retval #PSA_ERROR_HARDWARE_FAILURE
3472 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003473 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003474 * The library has not been previously initialized by psa_crypto_init().
3475 * It is implementation-dependent whether a failure to initialize
3476 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003477 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003478psa_status_t psa_generate_random_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003479 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003480
3481/**@}*/
3482
Gilles Peskinee59236f2018-01-27 23:32:46 +01003483#ifdef __cplusplus
3484}
3485#endif
3486
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003487/* The file "crypto_sizes.h" contains definitions for size calculation
3488 * macros whose definitions are implementation-specific. */
3489#include "crypto_sizes.h"
3490
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003491/* The file "crypto_struct.h" contains definitions for
3492 * implementation-specific structs that are declared above. */
3493#include "crypto_struct.h"
3494
3495/* The file "crypto_extra.h" contains vendor-specific definitions. This
3496 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003497#include "crypto_extra.h"
3498
3499#endif /* PSA_CRYPTO_H */