blob: ece9fd072850d1d0d7b374925b9ff7bab2543c75 [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.
586 * The key size field in \p attributes is
587 * ignored; the actual key size is determined
588 * from the \p data buffer.
589 * \param[out] handle On success, a handle to the newly created key.
590 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100591 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine20628592019-04-19 19:29:50 +0200592 * buffer is interpreted according to the type and,
593 * if applicable, domain parameters declared in
594 * \p attributes.
595 * All implementations must support at least the format
596 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100597 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200598 * the chosen type. Implementations may allow other
599 * formats, but should be conservative: implementations
600 * should err on the side of rejecting content if it
601 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200602 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100603 *
Gilles Peskine28538492018-07-11 17:34:00 +0200604 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100605 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100606 * If the key is persistent, the key material and the key's metadata
607 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200608 * \retval #PSA_ERROR_ALREADY_EXISTS
609 * This is an attempt to create a persistent key, and there is
610 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200611 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200612 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200613 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200614 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200615 * The key attributes, as a whole, are invalid,
Gilles Peskine308b91d2018-02-08 09:47:44 +0100616 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200617 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
618 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
619 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100620 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200621 * \retval #PSA_ERROR_HARDWARE_FAILURE
622 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300623 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300624 * The library has not been previously initialized by psa_crypto_init().
625 * It is implementation-dependent whether a failure to initialize
626 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100627 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200628psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
629 psa_key_handle_t *handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100630 const uint8_t *data,
631 size_t data_length);
632
633/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100634 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200635 *
636 * This function destroys the content of the key slot from both volatile
637 * memory and, if applicable, non-volatile storage. Implementations shall
638 * make a best effort to ensure that any previous content of the slot is
639 * unrecoverable.
640 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100641 * This function also erases any metadata such as policies and frees all
642 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200643 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100644 * If the key is currently in use in a multipart operation,
645 * the multipart operation is aborted.
646 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100647 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100648 *
Gilles Peskine28538492018-07-11 17:34:00 +0200649 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200650 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200651 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200652 * The slot holds content and cannot be erased because it is
653 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100654 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200655 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200656 * There was an failure in communication with the cryptoprocessor.
657 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200658 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200659 * The storage is corrupted. Implementations shall make a best effort
660 * to erase key material even in this stage, however applications
661 * should be aware that it may be impossible to guarantee that the
662 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200663 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200664 * An unexpected condition which is not a storage corruption or
665 * a communication failure occurred. The cryptoprocessor may have
666 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300667 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300668 * The library has not been previously initialized by psa_crypto_init().
669 * It is implementation-dependent whether a failure to initialize
670 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100671 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100672psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673
674/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675 * \brief Export a key in binary format.
676 *
677 * The output of this function can be passed to psa_import_key() to
678 * create an equivalent object.
679 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100680 * If the implementation of psa_import_key() supports other formats
681 * beyond the format specified here, the output from psa_export_key()
682 * must use the representation specified here, not the original
683 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100684 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100685 * For standard key types, the output format is as follows:
686 *
687 * - For symmetric keys (including MAC keys), the format is the
688 * raw bytes of the key.
689 * - For DES, the key data consists of 8 bytes. The parity bits must be
690 * correct.
691 * - For Triple-DES, the format is the concatenation of the
692 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100693 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200694 * is the non-encrypted DER encoding of the representation defined by
695 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
696 * ```
697 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200698 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200699 * modulus INTEGER, -- n
700 * publicExponent INTEGER, -- e
701 * privateExponent INTEGER, -- d
702 * prime1 INTEGER, -- p
703 * prime2 INTEGER, -- q
704 * exponent1 INTEGER, -- d mod (p-1)
705 * exponent2 INTEGER, -- d mod (q-1)
706 * coefficient INTEGER, -- (inverse of q) mod p
707 * }
708 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000709 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
710 * representation of the private key `x` as a big-endian byte string. The
711 * length of the byte string is the private key size in bytes (leading zeroes
712 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200713 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100714 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100715 * a representation of the private value as a `ceiling(m/8)`-byte string
716 * where `m` is the bit size associated with the curve, i.e. the bit size
717 * of the order of the curve's coordinate field. This byte string is
718 * in little-endian order for Montgomery curves (curve types
719 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
720 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
721 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100722 * This is the content of the `privateKey` field of the `ECPrivateKey`
723 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000724 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
725 * format is the representation of the private key `x` as a big-endian byte
726 * string. The length of the byte string is the private key size in bytes
727 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200728 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
729 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100730 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100731 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200732 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200733 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200734 * \param[out] data_length On success, the number of bytes
735 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100736 *
Gilles Peskine28538492018-07-11 17:34:00 +0200737 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100738 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200739 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200740 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100741 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200742 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
743 * The size of the \p data buffer is too small. You can determine a
744 * sufficient buffer size by calling
745 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
746 * where \c type is the key type
747 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200748 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
749 * \retval #PSA_ERROR_HARDWARE_FAILURE
750 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300751 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300752 * The library has not been previously initialized by psa_crypto_init().
753 * It is implementation-dependent whether a failure to initialize
754 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100755 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100756psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100757 uint8_t *data,
758 size_t data_size,
759 size_t *data_length);
760
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100761/**
762 * \brief Export a public key or the public part of a key pair in binary format.
763 *
764 * The output of this function can be passed to psa_import_key() to
765 * create an object that is equivalent to the public key.
766 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000767 * This specification supports a single format for each key type.
768 * Implementations may support other formats as long as the standard
769 * format is supported. Implementations that support other formats
770 * should ensure that the formats are clearly unambiguous so as to
771 * minimize the risk that an invalid input is accidentally interpreted
772 * according to a different format.
773 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000774 * For standard key types, the output format is as follows:
775 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
776 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
777 * ```
778 * RSAPublicKey ::= SEQUENCE {
779 * modulus INTEGER, -- n
780 * publicExponent INTEGER } -- e
781 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000782 * - For elliptic curve public keys (key types for which
783 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
784 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
785 * Let `m` be the bit size associated with the curve, i.e. the bit size of
786 * `q` for a curve over `F_q`. The representation consists of:
787 * - The byte 0x04;
788 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
789 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000790 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
791 * representation of the public key `y = g^x mod p` as a big-endian byte
792 * string. The length of the byte string is the length of the base prime `p`
793 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000794 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
795 * the format is the representation of the public key `y = g^x mod p` as a
796 * big-endian byte string. The length of the byte string is the length of the
797 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100798 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100799 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200800 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200801 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200802 * \param[out] data_length On success, the number of bytes
803 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100804 *
Gilles Peskine28538492018-07-11 17:34:00 +0200805 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100806 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200807 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200808 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200809 * The key is neither a public key nor a key pair.
810 * \retval #PSA_ERROR_NOT_SUPPORTED
811 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
812 * The size of the \p data buffer is too small. You can determine a
813 * sufficient buffer size by calling
814 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
815 * where \c type is the key type
816 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200817 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
818 * \retval #PSA_ERROR_HARDWARE_FAILURE
819 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300820 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300821 * The library has not been previously initialized by psa_crypto_init().
822 * It is implementation-dependent whether a failure to initialize
823 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100824 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100825psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100826 uint8_t *data,
827 size_t data_size,
828 size_t *data_length);
829
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100830/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100831 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100832 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000833 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100834 * This function is primarily useful to copy a key from one location
835 * to another, since it populates a key using the material from
836 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200837 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100838 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100839 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100840 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100841 *
Gilles Peskine20628592019-04-19 19:29:50 +0200842 * The resulting key may only be used in a way that conforms to
843 * both the policy of the original key and the policy specified in
844 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100845 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200846 * usage flags on the source policy and the usage flags in \p attributes.
847 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100848 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200849 * - If either of the policies allows an algorithm and the other policy
850 * allows a wildcard-based algorithm policy that includes this algorithm,
851 * the resulting key allows the same algorithm.
852 * - If the policies do not allow any algorithm in common, this function
853 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200854 *
Gilles Peskine20628592019-04-19 19:29:50 +0200855 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100856 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200857 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100858 * \param source_handle The key to copy. It must be a handle to an
859 * occupied slot.
Gilles Peskine20628592019-04-19 19:29:50 +0200860 * \param[in] attributes The attributes for the new key.
861 * They are used as follows:
862 * - The key type, key size and domain parameters
863 * are ignored. This information is copied
864 * from the source key.
865 * - The key location (the lifetime and, for
866 * persistent keys, the key identifier) is
867 * used directly.
868 * - The policy constraints (usage flags and
869 * algorithm policy) are combined from
870 * the source key and \p attributes so that
871 * both sets of restrictions apply, as
872 * described in the documentation of this function.
873 * \param[out] target_handle On success, a handle to the newly created key.
874 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200875 *
876 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100877 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200878 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200879 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200880 * This is an attempt to create a persistent key, and there is
881 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200882 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200883 * The lifetime or identifier in \p attributes are invalid.
884 * \retval #PSA_ERROR_INVALID_ARGUMENT
885 * The policy constraints on the source and specified in
886 * \p attributes are incompatible.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100887 * \retval #PSA_ERROR_NOT_PERMITTED
888 * The source key is not exportable and its lifetime does not
889 * allow copying it to the target's lifetime.
890 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
891 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200892 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
893 * \retval #PSA_ERROR_HARDWARE_FAILURE
894 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100895 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100896psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200897 const psa_key_attributes_t *attributes,
898 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100899
900/**@}*/
901
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100902/** \defgroup hash Message digests
903 * @{
904 */
905
Gilles Peskine69647a42019-01-14 20:18:12 +0100906/** Calculate the hash (digest) of a message.
907 *
908 * \note To verify the hash of a message against an
909 * expected value, use psa_hash_compare() instead.
910 *
911 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
912 * such that #PSA_ALG_IS_HASH(\p alg) is true).
913 * \param[in] input Buffer containing the message to hash.
914 * \param input_length Size of the \p input buffer in bytes.
915 * \param[out] hash Buffer where the hash is to be written.
916 * \param hash_size Size of the \p hash buffer in bytes.
917 * \param[out] hash_length On success, the number of bytes
918 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100919 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100920 *
921 * \retval #PSA_SUCCESS
922 * Success.
923 * \retval #PSA_ERROR_NOT_SUPPORTED
924 * \p alg is not supported or is not a hash algorithm.
925 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
926 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
927 * \retval #PSA_ERROR_HARDWARE_FAILURE
928 * \retval #PSA_ERROR_TAMPERING_DETECTED
929 */
930psa_status_t psa_hash_compute(psa_algorithm_t alg,
931 const uint8_t *input,
932 size_t input_length,
933 uint8_t *hash,
934 size_t hash_size,
935 size_t *hash_length);
936
937/** Calculate the hash (digest) of a message and compare it with a
938 * reference value.
939 *
940 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
941 * such that #PSA_ALG_IS_HASH(\p alg) is true).
942 * \param[in] input Buffer containing the message to hash.
943 * \param input_length Size of the \p input buffer in bytes.
944 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100945 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100946 *
947 * \retval #PSA_SUCCESS
948 * The expected hash is identical to the actual hash of the input.
949 * \retval #PSA_ERROR_INVALID_SIGNATURE
950 * The hash of the message was calculated successfully, but it
951 * differs from the expected hash.
952 * \retval #PSA_ERROR_NOT_SUPPORTED
953 * \p alg is not supported or is not a hash algorithm.
954 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
955 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
956 * \retval #PSA_ERROR_HARDWARE_FAILURE
957 * \retval #PSA_ERROR_TAMPERING_DETECTED
958 */
959psa_status_t psa_hash_compare(psa_algorithm_t alg,
960 const uint8_t *input,
961 size_t input_length,
962 const uint8_t *hash,
963 const size_t hash_length);
964
Gilles Peskine308b91d2018-02-08 09:47:44 +0100965/** The type of the state data structure for multipart hash operations.
966 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000967 * Before calling any function on a hash operation object, the application must
968 * initialize it by any of the following means:
969 * - Set the structure to all-bits-zero, for example:
970 * \code
971 * psa_hash_operation_t operation;
972 * memset(&operation, 0, sizeof(operation));
973 * \endcode
974 * - Initialize the structure to logical zero values, for example:
975 * \code
976 * psa_hash_operation_t operation = {0};
977 * \endcode
978 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
979 * for example:
980 * \code
981 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
982 * \endcode
983 * - Assign the result of the function psa_hash_operation_init()
984 * to the structure, for example:
985 * \code
986 * psa_hash_operation_t operation;
987 * operation = psa_hash_operation_init();
988 * \endcode
989 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100990 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100991 * make any assumptions about the content of this structure except
992 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100993typedef struct psa_hash_operation_s psa_hash_operation_t;
994
Jaeden Amero6a25b412019-01-04 11:47:44 +0000995/** \def PSA_HASH_OPERATION_INIT
996 *
997 * This macro returns a suitable initializer for a hash operation object
998 * of type #psa_hash_operation_t.
999 */
1000#ifdef __DOXYGEN_ONLY__
1001/* This is an example definition for documentation purposes.
1002 * Implementations should define a suitable value in `crypto_struct.h`.
1003 */
1004#define PSA_HASH_OPERATION_INIT {0}
1005#endif
1006
1007/** Return an initial value for a hash operation object.
1008 */
1009static psa_hash_operation_t psa_hash_operation_init(void);
1010
Gilles Peskinef45adda2019-01-14 18:29:18 +01001011/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001012 *
1013 * The sequence of operations to calculate a hash (message digest)
1014 * is as follows:
1015 * -# Allocate an operation object which will be passed to all the functions
1016 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +00001017 * -# Initialize the operation object with one of the methods described in the
1018 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001019 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001020 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001021 * of the message each time. The hash that is calculated is the hash
1022 * of the concatenation of these messages in order.
1023 * -# To calculate the hash, call psa_hash_finish().
1024 * To compare the hash with an expected value, call psa_hash_verify().
1025 *
1026 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +00001027 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001028 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001029 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001030 * eventually terminate the operation. The following events terminate an
1031 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001032 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001033 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001034 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001035 * \param[in,out] operation The operation object to set up. It must have
1036 * been initialized as per the documentation for
1037 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001038 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1039 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001040 *
Gilles Peskine28538492018-07-11 17:34:00 +02001041 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001042 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001043 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001044 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001045 * \retval #PSA_ERROR_BAD_STATE
1046 * The operation state is not valid (already set up and not
1047 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001048 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1049 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1050 * \retval #PSA_ERROR_HARDWARE_FAILURE
1051 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001052 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001053psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001054 psa_algorithm_t alg);
1055
Gilles Peskine308b91d2018-02-08 09:47:44 +01001056/** Add a message fragment to a multipart hash operation.
1057 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001058 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001059 *
1060 * If this function returns an error status, the operation becomes inactive.
1061 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001062 * \param[in,out] operation Active hash operation.
1063 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001064 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001065 *
Gilles Peskine28538492018-07-11 17:34:00 +02001066 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001067 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001068 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001069 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001070 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1071 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1072 * \retval #PSA_ERROR_HARDWARE_FAILURE
1073 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001075psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1076 const uint8_t *input,
1077 size_t input_length);
1078
Gilles Peskine308b91d2018-02-08 09:47:44 +01001079/** Finish the calculation of the hash of a message.
1080 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001081 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001082 * This function calculates the hash of the message formed by concatenating
1083 * the inputs passed to preceding calls to psa_hash_update().
1084 *
1085 * When this function returns, the operation becomes inactive.
1086 *
1087 * \warning Applications should not call this function if they expect
1088 * a specific value for the hash. Call psa_hash_verify() instead.
1089 * Beware that comparing integrity or authenticity data such as
1090 * hash values with a function such as \c memcmp is risky
1091 * because the time taken by the comparison may leak information
1092 * about the hashed data which could allow an attacker to guess
1093 * a valid hash and thereby bypass security controls.
1094 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001095 * \param[in,out] operation Active hash operation.
1096 * \param[out] hash Buffer where the hash is to be written.
1097 * \param hash_size Size of the \p hash buffer in bytes.
1098 * \param[out] hash_length On success, the number of bytes
1099 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001100 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001101 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001102 *
Gilles Peskine28538492018-07-11 17:34:00 +02001103 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001104 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001105 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001106 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001107 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001108 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001109 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001110 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001111 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1112 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1113 * \retval #PSA_ERROR_HARDWARE_FAILURE
1114 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001115 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001116psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1117 uint8_t *hash,
1118 size_t hash_size,
1119 size_t *hash_length);
1120
Gilles Peskine308b91d2018-02-08 09:47:44 +01001121/** Finish the calculation of the hash of a message and compare it with
1122 * an expected value.
1123 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001124 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001125 * This function calculates the hash of the message formed by concatenating
1126 * the inputs passed to preceding calls to psa_hash_update(). It then
1127 * compares the calculated hash with the expected hash passed as a
1128 * parameter to this function.
1129 *
1130 * When this function returns, the operation becomes inactive.
1131 *
Gilles Peskine19067982018-03-20 17:54:53 +01001132 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001133 * comparison between the actual hash and the expected hash is performed
1134 * in constant time.
1135 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001136 * \param[in,out] operation Active hash operation.
1137 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001138 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001139 *
Gilles Peskine28538492018-07-11 17:34:00 +02001140 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001141 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001142 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001143 * The hash of the message was calculated successfully, but it
1144 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001145 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001146 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001147 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1148 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1149 * \retval #PSA_ERROR_HARDWARE_FAILURE
1150 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001151 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001152psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1153 const uint8_t *hash,
1154 size_t hash_length);
1155
Gilles Peskine308b91d2018-02-08 09:47:44 +01001156/** Abort a hash operation.
1157 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001158 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001159 * \p operation structure itself. Once aborted, the operation object
1160 * can be reused for another operation by calling
1161 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001162 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001163 * You may call this function any time after the operation object has
1164 * been initialized by any of the following methods:
1165 * - A call to psa_hash_setup(), whether it succeeds or not.
1166 * - Initializing the \c struct to all-bits-zero.
1167 * - Initializing the \c struct to logical zeros, e.g.
1168 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001169 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001170 * In particular, calling psa_hash_abort() after the operation has been
1171 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1172 * psa_hash_verify() is safe and has no effect.
1173 *
1174 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001175 *
Gilles Peskine28538492018-07-11 17:34:00 +02001176 * \retval #PSA_SUCCESS
1177 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001178 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001179 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1180 * \retval #PSA_ERROR_HARDWARE_FAILURE
1181 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001182 */
1183psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001184
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001185/** Clone a hash operation.
1186 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001187 * This function copies the state of an ongoing hash operation to
1188 * a new operation object. In other words, this function is equivalent
1189 * to calling psa_hash_setup() on \p target_operation with the same
1190 * algorithm that \p source_operation was set up for, then
1191 * psa_hash_update() on \p target_operation with the same input that
1192 * that was passed to \p source_operation. After this function returns, the
1193 * two objects are independent, i.e. subsequent calls involving one of
1194 * the objects do not affect the other object.
1195 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001196 * \param[in] source_operation The active hash operation to clone.
1197 * \param[in,out] target_operation The operation object to set up.
1198 * It must be initialized but not active.
1199 *
1200 * \retval #PSA_SUCCESS
1201 * \retval #PSA_ERROR_BAD_STATE
1202 * \p source_operation is not an active hash operation.
1203 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001204 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001205 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1206 * \retval #PSA_ERROR_HARDWARE_FAILURE
1207 * \retval #PSA_ERROR_TAMPERING_DETECTED
1208 */
1209psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1210 psa_hash_operation_t *target_operation);
1211
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001212/**@}*/
1213
Gilles Peskine8c9def32018-02-08 10:02:12 +01001214/** \defgroup MAC Message authentication codes
1215 * @{
1216 */
1217
Gilles Peskine69647a42019-01-14 20:18:12 +01001218/** Calculate the MAC (message authentication code) of a message.
1219 *
1220 * \note To verify the MAC of a message against an
1221 * expected value, use psa_mac_verify() instead.
1222 * Beware that comparing integrity or authenticity data such as
1223 * MAC values with a function such as \c memcmp is risky
1224 * because the time taken by the comparison may leak information
1225 * about the MAC value which could allow an attacker to guess
1226 * a valid MAC and thereby bypass security controls.
1227 *
1228 * \param handle Handle to the key to use for the operation.
1229 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001230 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001231 * \param[in] input Buffer containing the input message.
1232 * \param input_length Size of the \p input buffer in bytes.
1233 * \param[out] mac Buffer where the MAC value is to be written.
1234 * \param mac_size Size of the \p mac buffer in bytes.
1235 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001236 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001237 *
1238 * \retval #PSA_SUCCESS
1239 * Success.
1240 * \retval #PSA_ERROR_INVALID_HANDLE
1241 * \retval #PSA_ERROR_EMPTY_SLOT
1242 * \retval #PSA_ERROR_NOT_PERMITTED
1243 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001244 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001245 * \retval #PSA_ERROR_NOT_SUPPORTED
1246 * \p alg is not supported or is not a MAC algorithm.
1247 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1248 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1249 * \retval #PSA_ERROR_HARDWARE_FAILURE
1250 * \retval #PSA_ERROR_TAMPERING_DETECTED
1251 * \retval #PSA_ERROR_BAD_STATE
1252 * The library has not been previously initialized by psa_crypto_init().
1253 * It is implementation-dependent whether a failure to initialize
1254 * results in this error code.
1255 */
1256psa_status_t psa_mac_compute(psa_key_handle_t handle,
1257 psa_algorithm_t alg,
1258 const uint8_t *input,
1259 size_t input_length,
1260 uint8_t *mac,
1261 size_t mac_size,
1262 size_t *mac_length);
1263
1264/** Calculate the MAC of a message and compare it with a reference value.
1265 *
1266 * \param handle Handle to the key to use for the operation.
1267 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001268 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001269 * \param[in] input Buffer containing the input message.
1270 * \param input_length Size of the \p input buffer in bytes.
1271 * \param[out] mac Buffer containing the expected MAC value.
1272 * \param mac_length Size of the \p mac buffer in bytes.
1273 *
1274 * \retval #PSA_SUCCESS
1275 * The expected MAC is identical to the actual MAC of the input.
1276 * \retval #PSA_ERROR_INVALID_SIGNATURE
1277 * The MAC of the message was calculated successfully, but it
1278 * differs from the expected value.
1279 * \retval #PSA_ERROR_INVALID_HANDLE
1280 * \retval #PSA_ERROR_EMPTY_SLOT
1281 * \retval #PSA_ERROR_NOT_PERMITTED
1282 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001283 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001284 * \retval #PSA_ERROR_NOT_SUPPORTED
1285 * \p alg is not supported or is not a MAC algorithm.
1286 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1287 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1288 * \retval #PSA_ERROR_HARDWARE_FAILURE
1289 * \retval #PSA_ERROR_TAMPERING_DETECTED
1290 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001291psa_status_t psa_mac_verify(psa_key_handle_t handle,
1292 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001293 const uint8_t *input,
1294 size_t input_length,
1295 const uint8_t *mac,
1296 const size_t mac_length);
1297
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001298/** The type of the state data structure for multipart MAC operations.
1299 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001300 * Before calling any function on a MAC operation object, the application must
1301 * initialize it by any of the following means:
1302 * - Set the structure to all-bits-zero, for example:
1303 * \code
1304 * psa_mac_operation_t operation;
1305 * memset(&operation, 0, sizeof(operation));
1306 * \endcode
1307 * - Initialize the structure to logical zero values, for example:
1308 * \code
1309 * psa_mac_operation_t operation = {0};
1310 * \endcode
1311 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1312 * for example:
1313 * \code
1314 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1315 * \endcode
1316 * - Assign the result of the function psa_mac_operation_init()
1317 * to the structure, for example:
1318 * \code
1319 * psa_mac_operation_t operation;
1320 * operation = psa_mac_operation_init();
1321 * \endcode
1322 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001323 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001324 * make any assumptions about the content of this structure except
1325 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001326typedef struct psa_mac_operation_s psa_mac_operation_t;
1327
Jaeden Amero769ce272019-01-04 11:48:03 +00001328/** \def PSA_MAC_OPERATION_INIT
1329 *
1330 * This macro returns a suitable initializer for a MAC operation object of type
1331 * #psa_mac_operation_t.
1332 */
1333#ifdef __DOXYGEN_ONLY__
1334/* This is an example definition for documentation purposes.
1335 * Implementations should define a suitable value in `crypto_struct.h`.
1336 */
1337#define PSA_MAC_OPERATION_INIT {0}
1338#endif
1339
1340/** Return an initial value for a MAC operation object.
1341 */
1342static psa_mac_operation_t psa_mac_operation_init(void);
1343
Gilles Peskinef45adda2019-01-14 18:29:18 +01001344/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001345 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001346 * This function sets up the calculation of the MAC
1347 * (message authentication code) of a byte string.
1348 * To verify the MAC of a message against an
1349 * expected value, use psa_mac_verify_setup() instead.
1350 *
1351 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001352 * -# Allocate an operation object which will be passed to all the functions
1353 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001354 * -# Initialize the operation object with one of the methods described in the
1355 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001356 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001357 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1358 * of the message each time. The MAC that is calculated is the MAC
1359 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001360 * -# At the end of the message, call psa_mac_sign_finish() to finish
1361 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001362 *
1363 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001364 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001365 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001366 * After a successful call to psa_mac_sign_setup(), the application must
1367 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001368 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001369 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001370 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001371 * \param[in,out] operation The operation object to set up. It must have
1372 * been initialized as per the documentation for
1373 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001374 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001375 * It must remain valid until the operation
1376 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001377 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001378 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001379 *
Gilles Peskine28538492018-07-11 17:34:00 +02001380 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001381 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001382 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001383 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001384 * \retval #PSA_ERROR_NOT_PERMITTED
1385 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001386 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001387 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001388 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001389 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1390 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1391 * \retval #PSA_ERROR_HARDWARE_FAILURE
1392 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001393 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001394 * The operation state is not valid (already set up and not
1395 * subsequently completed).
1396 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001397 * The library has not been previously initialized by psa_crypto_init().
1398 * It is implementation-dependent whether a failure to initialize
1399 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001400 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001401psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001402 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001403 psa_algorithm_t alg);
1404
Gilles Peskinef45adda2019-01-14 18:29:18 +01001405/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001406 *
1407 * This function sets up the verification of the MAC
1408 * (message authentication code) of a byte string against an expected value.
1409 *
1410 * The sequence of operations to verify a MAC is as follows:
1411 * -# Allocate an operation object which will be passed to all the functions
1412 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001413 * -# Initialize the operation object with one of the methods described in the
1414 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001415 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001416 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1417 * of the message each time. The MAC that is calculated is the MAC
1418 * of the concatenation of these messages in order.
1419 * -# At the end of the message, call psa_mac_verify_finish() to finish
1420 * calculating the actual MAC of the message and verify it against
1421 * the expected value.
1422 *
1423 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001424 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001425 *
1426 * After a successful call to psa_mac_verify_setup(), the application must
1427 * eventually terminate the operation through one of the following methods:
1428 * - A failed call to psa_mac_update().
1429 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1430 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001431 * \param[in,out] operation The operation object to set up. It must have
1432 * been initialized as per the documentation for
1433 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001434 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001435 * It must remain valid until the operation
1436 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001437 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1438 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001439 *
Gilles Peskine28538492018-07-11 17:34:00 +02001440 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001441 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001442 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001443 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001444 * \retval #PSA_ERROR_NOT_PERMITTED
1445 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001446 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001447 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001448 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001449 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1450 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1451 * \retval #PSA_ERROR_HARDWARE_FAILURE
1452 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001453 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001454 * The operation state is not valid (already set up and not
1455 * subsequently completed).
1456 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001457 * The library has not been previously initialized by psa_crypto_init().
1458 * It is implementation-dependent whether a failure to initialize
1459 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001460 */
1461psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001462 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001463 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001464
Gilles Peskinedcd14942018-07-12 00:30:52 +02001465/** Add a message fragment to a multipart MAC operation.
1466 *
1467 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1468 * before calling this function.
1469 *
1470 * If this function returns an error status, the operation becomes inactive.
1471 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001472 * \param[in,out] operation Active MAC operation.
1473 * \param[in] input Buffer containing the message fragment to add to
1474 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001475 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001476 *
1477 * \retval #PSA_SUCCESS
1478 * Success.
1479 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001480 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001481 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1482 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1483 * \retval #PSA_ERROR_HARDWARE_FAILURE
1484 * \retval #PSA_ERROR_TAMPERING_DETECTED
1485 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001486psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1487 const uint8_t *input,
1488 size_t input_length);
1489
Gilles Peskinedcd14942018-07-12 00:30:52 +02001490/** Finish the calculation of the MAC of a message.
1491 *
1492 * The application must call psa_mac_sign_setup() before calling this function.
1493 * This function calculates the MAC of the message formed by concatenating
1494 * the inputs passed to preceding calls to psa_mac_update().
1495 *
1496 * When this function returns, the operation becomes inactive.
1497 *
1498 * \warning Applications should not call this function if they expect
1499 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1500 * Beware that comparing integrity or authenticity data such as
1501 * MAC values with a function such as \c memcmp is risky
1502 * because the time taken by the comparison may leak information
1503 * about the MAC value which could allow an attacker to guess
1504 * a valid MAC and thereby bypass security controls.
1505 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001506 * \param[in,out] operation Active MAC operation.
1507 * \param[out] mac Buffer where the MAC value is to be written.
1508 * \param mac_size Size of the \p mac buffer in bytes.
1509 * \param[out] mac_length On success, the number of bytes
1510 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001511 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001512 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001513 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001514 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001515 *
1516 * \retval #PSA_SUCCESS
1517 * Success.
1518 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001519 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001520 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001521 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001522 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1523 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1524 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1525 * \retval #PSA_ERROR_HARDWARE_FAILURE
1526 * \retval #PSA_ERROR_TAMPERING_DETECTED
1527 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001528psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1529 uint8_t *mac,
1530 size_t mac_size,
1531 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001532
Gilles Peskinedcd14942018-07-12 00:30:52 +02001533/** Finish the calculation of the MAC of a message and compare it with
1534 * an expected value.
1535 *
1536 * The application must call psa_mac_verify_setup() before calling this function.
1537 * This function calculates the MAC of the message formed by concatenating
1538 * the inputs passed to preceding calls to psa_mac_update(). It then
1539 * compares the calculated MAC with the expected MAC passed as a
1540 * parameter to this function.
1541 *
1542 * When this function returns, the operation becomes inactive.
1543 *
1544 * \note Implementations shall make the best effort to ensure that the
1545 * comparison between the actual MAC and the expected MAC is performed
1546 * in constant time.
1547 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001548 * \param[in,out] operation Active MAC operation.
1549 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001550 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001551 *
1552 * \retval #PSA_SUCCESS
1553 * The expected MAC is identical to the actual MAC of the message.
1554 * \retval #PSA_ERROR_INVALID_SIGNATURE
1555 * The MAC of the message was calculated successfully, but it
1556 * differs from the expected MAC.
1557 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001558 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001559 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1560 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1561 * \retval #PSA_ERROR_HARDWARE_FAILURE
1562 * \retval #PSA_ERROR_TAMPERING_DETECTED
1563 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001564psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1565 const uint8_t *mac,
1566 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001567
Gilles Peskinedcd14942018-07-12 00:30:52 +02001568/** Abort a MAC operation.
1569 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001570 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001571 * \p operation structure itself. Once aborted, the operation object
1572 * can be reused for another operation by calling
1573 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001574 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001575 * You may call this function any time after the operation object has
1576 * been initialized by any of the following methods:
1577 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1578 * it succeeds or not.
1579 * - Initializing the \c struct to all-bits-zero.
1580 * - Initializing the \c struct to logical zeros, e.g.
1581 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001582 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001583 * In particular, calling psa_mac_abort() after the operation has been
1584 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1585 * psa_mac_verify_finish() is safe and has no effect.
1586 *
1587 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001588 *
1589 * \retval #PSA_SUCCESS
1590 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001591 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001592 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1593 * \retval #PSA_ERROR_HARDWARE_FAILURE
1594 * \retval #PSA_ERROR_TAMPERING_DETECTED
1595 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001596psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1597
1598/**@}*/
1599
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001600/** \defgroup cipher Symmetric ciphers
1601 * @{
1602 */
1603
Gilles Peskine69647a42019-01-14 20:18:12 +01001604/** Encrypt a message using a symmetric cipher.
1605 *
1606 * This function encrypts a message with a random IV (initialization
1607 * vector).
1608 *
1609 * \param handle Handle to the key to use for the operation.
1610 * It must remain valid until the operation
1611 * terminates.
1612 * \param alg The cipher algorithm to compute
1613 * (\c PSA_ALG_XXX value such that
1614 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1615 * \param[in] input Buffer containing the message to encrypt.
1616 * \param input_length Size of the \p input buffer in bytes.
1617 * \param[out] output Buffer where the output is to be written.
1618 * The output contains the IV followed by
1619 * the ciphertext proper.
1620 * \param output_size Size of the \p output buffer in bytes.
1621 * \param[out] output_length On success, the number of bytes
1622 * that make up the output.
1623 *
1624 * \retval #PSA_SUCCESS
1625 * Success.
1626 * \retval #PSA_ERROR_INVALID_HANDLE
1627 * \retval #PSA_ERROR_EMPTY_SLOT
1628 * \retval #PSA_ERROR_NOT_PERMITTED
1629 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001630 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001631 * \retval #PSA_ERROR_NOT_SUPPORTED
1632 * \p alg is not supported or is not a cipher algorithm.
1633 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1634 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1635 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1636 * \retval #PSA_ERROR_HARDWARE_FAILURE
1637 * \retval #PSA_ERROR_TAMPERING_DETECTED
1638 */
1639psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1640 psa_algorithm_t alg,
1641 const uint8_t *input,
1642 size_t input_length,
1643 uint8_t *output,
1644 size_t output_size,
1645 size_t *output_length);
1646
1647/** Decrypt a message using a symmetric cipher.
1648 *
1649 * This function decrypts a message encrypted with a symmetric cipher.
1650 *
1651 * \param handle Handle to the key to use for the operation.
1652 * It must remain valid until the operation
1653 * terminates.
1654 * \param alg The cipher algorithm to compute
1655 * (\c PSA_ALG_XXX value such that
1656 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1657 * \param[in] input Buffer containing the message to decrypt.
1658 * This consists of the IV followed by the
1659 * ciphertext proper.
1660 * \param input_length Size of the \p input buffer in bytes.
1661 * \param[out] output Buffer where the plaintext is to be written.
1662 * \param output_size Size of the \p output buffer in bytes.
1663 * \param[out] output_length On success, the number of bytes
1664 * that make up the output.
1665 *
1666 * \retval #PSA_SUCCESS
1667 * Success.
1668 * \retval #PSA_ERROR_INVALID_HANDLE
1669 * \retval #PSA_ERROR_EMPTY_SLOT
1670 * \retval #PSA_ERROR_NOT_PERMITTED
1671 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001672 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001673 * \retval #PSA_ERROR_NOT_SUPPORTED
1674 * \p alg is not supported or is not a cipher algorithm.
1675 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1676 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1677 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1678 * \retval #PSA_ERROR_HARDWARE_FAILURE
1679 * \retval #PSA_ERROR_TAMPERING_DETECTED
1680 */
1681psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1682 psa_algorithm_t alg,
1683 const uint8_t *input,
1684 size_t input_length,
1685 uint8_t *output,
1686 size_t output_size,
1687 size_t *output_length);
1688
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001689/** The type of the state data structure for multipart cipher operations.
1690 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001691 * Before calling any function on a cipher operation object, the application
1692 * must initialize it by any of the following means:
1693 * - Set the structure to all-bits-zero, for example:
1694 * \code
1695 * psa_cipher_operation_t operation;
1696 * memset(&operation, 0, sizeof(operation));
1697 * \endcode
1698 * - Initialize the structure to logical zero values, for example:
1699 * \code
1700 * psa_cipher_operation_t operation = {0};
1701 * \endcode
1702 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1703 * for example:
1704 * \code
1705 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1706 * \endcode
1707 * - Assign the result of the function psa_cipher_operation_init()
1708 * to the structure, for example:
1709 * \code
1710 * psa_cipher_operation_t operation;
1711 * operation = psa_cipher_operation_init();
1712 * \endcode
1713 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001714 * This is an implementation-defined \c struct. Applications should not
1715 * make any assumptions about the content of this structure except
1716 * as directed by the documentation of a specific implementation. */
1717typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1718
Jaeden Amero5bae2272019-01-04 11:48:27 +00001719/** \def PSA_CIPHER_OPERATION_INIT
1720 *
1721 * This macro returns a suitable initializer for a cipher operation object of
1722 * type #psa_cipher_operation_t.
1723 */
1724#ifdef __DOXYGEN_ONLY__
1725/* This is an example definition for documentation purposes.
1726 * Implementations should define a suitable value in `crypto_struct.h`.
1727 */
1728#define PSA_CIPHER_OPERATION_INIT {0}
1729#endif
1730
1731/** Return an initial value for a cipher operation object.
1732 */
1733static psa_cipher_operation_t psa_cipher_operation_init(void);
1734
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001735/** Set the key for a multipart symmetric encryption operation.
1736 *
1737 * The sequence of operations to encrypt a message with a symmetric cipher
1738 * is as follows:
1739 * -# Allocate an operation object which will be passed to all the functions
1740 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001741 * -# Initialize the operation object with one of the methods described in the
1742 * documentation for #psa_cipher_operation_t, e.g.
1743 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001744 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001745 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001746 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001747 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001748 * requires a specific IV value.
1749 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1750 * of the message each time.
1751 * -# Call psa_cipher_finish().
1752 *
1753 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001754 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001755 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001756 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001757 * eventually terminate the operation. The following events terminate an
1758 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001759 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001760 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001761 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001762 * \param[in,out] operation The operation object to set up. It must have
1763 * been initialized as per the documentation for
1764 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001765 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001766 * It must remain valid until the operation
1767 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001768 * \param alg The cipher algorithm to compute
1769 * (\c PSA_ALG_XXX value such that
1770 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001771 *
Gilles Peskine28538492018-07-11 17:34:00 +02001772 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001773 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001774 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001775 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001776 * \retval #PSA_ERROR_NOT_PERMITTED
1777 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001778 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001779 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001780 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001781 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1782 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1783 * \retval #PSA_ERROR_HARDWARE_FAILURE
1784 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001785 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001786 * The operation state is not valid (already set up and not
1787 * subsequently completed).
1788 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001789 * The library has not been previously initialized by psa_crypto_init().
1790 * It is implementation-dependent whether a failure to initialize
1791 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001792 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001793psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001794 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001795 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001796
1797/** Set the key for a multipart symmetric decryption operation.
1798 *
1799 * The sequence of operations to decrypt a message with a symmetric cipher
1800 * is as follows:
1801 * -# Allocate an operation object which will be passed to all the functions
1802 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001803 * -# Initialize the operation object with one of the methods described in the
1804 * documentation for #psa_cipher_operation_t, e.g.
1805 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001806 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001807 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001808 * decryption. If the IV is prepended to the ciphertext, you can call
1809 * psa_cipher_update() on a buffer containing the IV followed by the
1810 * beginning of the message.
1811 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1812 * of the message each time.
1813 * -# Call psa_cipher_finish().
1814 *
1815 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001816 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001817 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001818 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001819 * eventually terminate the operation. The following events terminate an
1820 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001821 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001822 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001823 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001824 * \param[in,out] operation The operation object to set up. It must have
1825 * been initialized as per the documentation for
1826 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001827 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001828 * It must remain valid until the operation
1829 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001830 * \param alg The cipher algorithm to compute
1831 * (\c PSA_ALG_XXX value such that
1832 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001833 *
Gilles Peskine28538492018-07-11 17:34:00 +02001834 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001835 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001836 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001837 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001838 * \retval #PSA_ERROR_NOT_PERMITTED
1839 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001840 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001841 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001842 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001843 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1844 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1845 * \retval #PSA_ERROR_HARDWARE_FAILURE
1846 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001847 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001848 * The operation state is not valid (already set up and not
1849 * subsequently completed).
1850 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001851 * The library has not been previously initialized by psa_crypto_init().
1852 * It is implementation-dependent whether a failure to initialize
1853 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001854 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001855psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001856 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001857 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001858
Gilles Peskinedcd14942018-07-12 00:30:52 +02001859/** Generate an IV for a symmetric encryption operation.
1860 *
1861 * This function generates a random IV (initialization vector), nonce
1862 * or initial counter value for the encryption operation as appropriate
1863 * for the chosen algorithm, key type and key size.
1864 *
1865 * The application must call psa_cipher_encrypt_setup() before
1866 * calling this function.
1867 *
1868 * If this function returns an error status, the operation becomes inactive.
1869 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001870 * \param[in,out] operation Active cipher operation.
1871 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001872 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001873 * \param[out] iv_length On success, the number of bytes of the
1874 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001875 *
1876 * \retval #PSA_SUCCESS
1877 * Success.
1878 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001879 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001880 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001881 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001882 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1883 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1884 * \retval #PSA_ERROR_HARDWARE_FAILURE
1885 * \retval #PSA_ERROR_TAMPERING_DETECTED
1886 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001887psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1888 unsigned char *iv,
1889 size_t iv_size,
1890 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001891
Gilles Peskinedcd14942018-07-12 00:30:52 +02001892/** Set the IV for a symmetric encryption or decryption operation.
1893 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001894 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001895 * or initial counter value for the encryption or decryption operation.
1896 *
1897 * The application must call psa_cipher_encrypt_setup() before
1898 * calling this function.
1899 *
1900 * If this function returns an error status, the operation becomes inactive.
1901 *
1902 * \note When encrypting, applications should use psa_cipher_generate_iv()
1903 * instead of this function, unless implementing a protocol that requires
1904 * a non-random IV.
1905 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001906 * \param[in,out] operation Active cipher operation.
1907 * \param[in] iv Buffer containing the IV to use.
1908 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001909 *
1910 * \retval #PSA_SUCCESS
1911 * Success.
1912 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001913 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001914 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001915 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001916 * or the chosen algorithm does not use an IV.
1917 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1918 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1919 * \retval #PSA_ERROR_HARDWARE_FAILURE
1920 * \retval #PSA_ERROR_TAMPERING_DETECTED
1921 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001922psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1923 const unsigned char *iv,
1924 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001925
Gilles Peskinedcd14942018-07-12 00:30:52 +02001926/** Encrypt or decrypt a message fragment in an active cipher operation.
1927 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001928 * Before calling this function, you must:
1929 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1930 * The choice of setup function determines whether this function
1931 * encrypts or decrypts its input.
1932 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1933 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001934 *
1935 * If this function returns an error status, the operation becomes inactive.
1936 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001937 * \param[in,out] operation Active cipher operation.
1938 * \param[in] input Buffer containing the message fragment to
1939 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001940 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001941 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001942 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001943 * \param[out] output_length On success, the number of bytes
1944 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001945 *
1946 * \retval #PSA_SUCCESS
1947 * Success.
1948 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001949 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001950 * not set, or already completed).
1951 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1952 * The size of the \p output buffer is too small.
1953 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1954 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1955 * \retval #PSA_ERROR_HARDWARE_FAILURE
1956 * \retval #PSA_ERROR_TAMPERING_DETECTED
1957 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001958psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1959 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001960 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001961 unsigned char *output,
1962 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001963 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001964
Gilles Peskinedcd14942018-07-12 00:30:52 +02001965/** Finish encrypting or decrypting a message in a cipher operation.
1966 *
1967 * The application must call psa_cipher_encrypt_setup() or
1968 * psa_cipher_decrypt_setup() before calling this function. The choice
1969 * of setup function determines whether this function encrypts or
1970 * decrypts its input.
1971 *
1972 * This function finishes the encryption or decryption of the message
1973 * formed by concatenating the inputs passed to preceding calls to
1974 * psa_cipher_update().
1975 *
1976 * When this function returns, the operation becomes inactive.
1977 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001978 * \param[in,out] operation Active cipher operation.
1979 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001980 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001981 * \param[out] output_length On success, the number of bytes
1982 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001983 *
1984 * \retval #PSA_SUCCESS
1985 * Success.
1986 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001987 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001988 * not set, or already completed).
1989 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1990 * The size of the \p output buffer is too small.
1991 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1992 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1993 * \retval #PSA_ERROR_HARDWARE_FAILURE
1994 * \retval #PSA_ERROR_TAMPERING_DETECTED
1995 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001996psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001997 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001998 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001999 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002000
Gilles Peskinedcd14942018-07-12 00:30:52 +02002001/** Abort a cipher operation.
2002 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002003 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002004 * \p operation structure itself. Once aborted, the operation object
2005 * can be reused for another operation by calling
2006 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002007 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002008 * You may call this function any time after the operation object has
2009 * been initialized by any of the following methods:
2010 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2011 * whether it succeeds or not.
2012 * - Initializing the \c struct to all-bits-zero.
2013 * - Initializing the \c struct to logical zeros, e.g.
2014 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002015 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002016 * In particular, calling psa_cipher_abort() after the operation has been
2017 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2018 * is safe and has no effect.
2019 *
2020 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002021 *
2022 * \retval #PSA_SUCCESS
2023 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002024 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002025 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2026 * \retval #PSA_ERROR_HARDWARE_FAILURE
2027 * \retval #PSA_ERROR_TAMPERING_DETECTED
2028 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002029psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2030
2031/**@}*/
2032
Gilles Peskine3b555712018-03-03 21:27:57 +01002033/** \defgroup aead Authenticated encryption with associated data (AEAD)
2034 * @{
2035 */
2036
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002037/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002038 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002039 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002040 * \param alg The AEAD algorithm to compute
2041 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002042 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002043 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002044 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002045 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002046 * but not encrypted.
2047 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002048 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002049 * encrypted.
2050 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002051 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002052 * encrypted data. The additional data is not
2053 * part of this output. For algorithms where the
2054 * encrypted data and the authentication tag
2055 * are defined as separate outputs, the
2056 * authentication tag is appended to the
2057 * encrypted data.
2058 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2059 * This must be at least
2060 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2061 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002062 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002063 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002064 *
Gilles Peskine28538492018-07-11 17:34:00 +02002065 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002066 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002067 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002068 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002069 * \retval #PSA_ERROR_NOT_PERMITTED
2070 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002071 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002072 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002073 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002074 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2075 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2076 * \retval #PSA_ERROR_HARDWARE_FAILURE
2077 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002078 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002079 * The library has not been previously initialized by psa_crypto_init().
2080 * It is implementation-dependent whether a failure to initialize
2081 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002082 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002083psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002084 psa_algorithm_t alg,
2085 const uint8_t *nonce,
2086 size_t nonce_length,
2087 const uint8_t *additional_data,
2088 size_t additional_data_length,
2089 const uint8_t *plaintext,
2090 size_t plaintext_length,
2091 uint8_t *ciphertext,
2092 size_t ciphertext_size,
2093 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002094
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002095/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002096 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002097 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002098 * \param alg The AEAD algorithm to compute
2099 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002100 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002101 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002102 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002103 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002104 * but not encrypted.
2105 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002106 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002107 * encrypted. For algorithms where the
2108 * encrypted data and the authentication tag
2109 * are defined as separate inputs, the buffer
2110 * must contain the encrypted data followed
2111 * by the authentication tag.
2112 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002113 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002114 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2115 * This must be at least
2116 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2117 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002118 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002119 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002120 *
Gilles Peskine28538492018-07-11 17:34:00 +02002121 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002122 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002123 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002124 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002125 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002126 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002127 * \retval #PSA_ERROR_NOT_PERMITTED
2128 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002129 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002130 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002131 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002132 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2133 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2134 * \retval #PSA_ERROR_HARDWARE_FAILURE
2135 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002136 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002137 * The library has not been previously initialized by psa_crypto_init().
2138 * It is implementation-dependent whether a failure to initialize
2139 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002140 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002141psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002142 psa_algorithm_t alg,
2143 const uint8_t *nonce,
2144 size_t nonce_length,
2145 const uint8_t *additional_data,
2146 size_t additional_data_length,
2147 const uint8_t *ciphertext,
2148 size_t ciphertext_length,
2149 uint8_t *plaintext,
2150 size_t plaintext_size,
2151 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002152
Gilles Peskine30a9e412019-01-14 18:36:12 +01002153/** The type of the state data structure for multipart AEAD operations.
2154 *
2155 * Before calling any function on an AEAD operation object, the application
2156 * must initialize it by any of the following means:
2157 * - Set the structure to all-bits-zero, for example:
2158 * \code
2159 * psa_aead_operation_t operation;
2160 * memset(&operation, 0, sizeof(operation));
2161 * \endcode
2162 * - Initialize the structure to logical zero values, for example:
2163 * \code
2164 * psa_aead_operation_t operation = {0};
2165 * \endcode
2166 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2167 * for example:
2168 * \code
2169 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2170 * \endcode
2171 * - Assign the result of the function psa_aead_operation_init()
2172 * to the structure, for example:
2173 * \code
2174 * psa_aead_operation_t operation;
2175 * operation = psa_aead_operation_init();
2176 * \endcode
2177 *
2178 * This is an implementation-defined \c struct. Applications should not
2179 * make any assumptions about the content of this structure except
2180 * as directed by the documentation of a specific implementation. */
2181typedef struct psa_aead_operation_s psa_aead_operation_t;
2182
2183/** \def PSA_AEAD_OPERATION_INIT
2184 *
2185 * This macro returns a suitable initializer for an AEAD operation object of
2186 * type #psa_aead_operation_t.
2187 */
2188#ifdef __DOXYGEN_ONLY__
2189/* This is an example definition for documentation purposes.
2190 * Implementations should define a suitable value in `crypto_struct.h`.
2191 */
2192#define PSA_AEAD_OPERATION_INIT {0}
2193#endif
2194
2195/** Return an initial value for an AEAD operation object.
2196 */
2197static psa_aead_operation_t psa_aead_operation_init(void);
2198
2199/** Set the key for a multipart authenticated encryption operation.
2200 *
2201 * The sequence of operations to encrypt a message with authentication
2202 * is as follows:
2203 * -# Allocate an operation object which will be passed to all the functions
2204 * listed here.
2205 * -# Initialize the operation object with one of the methods described in the
2206 * documentation for #psa_aead_operation_t, e.g.
2207 * PSA_AEAD_OPERATION_INIT.
2208 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002209 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2210 * inputs to the subsequent calls to psa_aead_update_ad() and
2211 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2212 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002213 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2214 * generate or set the nonce. You should use
2215 * psa_aead_generate_nonce() unless the protocol you are implementing
2216 * requires a specific nonce value.
2217 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2218 * of the non-encrypted additional authenticated data each time.
2219 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002220 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002221 * -# Call psa_aead_finish().
2222 *
2223 * The application may call psa_aead_abort() at any time after the operation
2224 * has been initialized.
2225 *
2226 * After a successful call to psa_aead_encrypt_setup(), the application must
2227 * eventually terminate the operation. The following events terminate an
2228 * operation:
2229 * - A failed call to any of the \c psa_aead_xxx functions.
2230 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2231 *
2232 * \param[in,out] operation The operation object to set up. It must have
2233 * been initialized as per the documentation for
2234 * #psa_aead_operation_t and not yet in use.
2235 * \param handle Handle to the key to use for the operation.
2236 * It must remain valid until the operation
2237 * terminates.
2238 * \param alg The AEAD algorithm to compute
2239 * (\c PSA_ALG_XXX value such that
2240 * #PSA_ALG_IS_AEAD(\p alg) is true).
2241 *
2242 * \retval #PSA_SUCCESS
2243 * Success.
2244 * \retval #PSA_ERROR_INVALID_HANDLE
2245 * \retval #PSA_ERROR_EMPTY_SLOT
2246 * \retval #PSA_ERROR_NOT_PERMITTED
2247 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002248 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002249 * \retval #PSA_ERROR_NOT_SUPPORTED
2250 * \p alg is not supported or is not an AEAD algorithm.
2251 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2252 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2253 * \retval #PSA_ERROR_HARDWARE_FAILURE
2254 * \retval #PSA_ERROR_TAMPERING_DETECTED
2255 * \retval #PSA_ERROR_BAD_STATE
2256 * The library has not been previously initialized by psa_crypto_init().
2257 * It is implementation-dependent whether a failure to initialize
2258 * results in this error code.
2259 */
2260psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2261 psa_key_handle_t handle,
2262 psa_algorithm_t alg);
2263
2264/** Set the key for a multipart authenticated decryption operation.
2265 *
2266 * The sequence of operations to decrypt a message with authentication
2267 * is as follows:
2268 * -# Allocate an operation object which will be passed to all the functions
2269 * listed here.
2270 * -# Initialize the operation object with one of the methods described in the
2271 * documentation for #psa_aead_operation_t, e.g.
2272 * PSA_AEAD_OPERATION_INIT.
2273 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002274 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2275 * inputs to the subsequent calls to psa_aead_update_ad() and
2276 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2277 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002278 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2279 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2280 * of the non-encrypted additional authenticated data each time.
2281 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002282 * of the ciphertext to decrypt each time.
2283 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002284 *
2285 * The application may call psa_aead_abort() at any time after the operation
2286 * has been initialized.
2287 *
2288 * After a successful call to psa_aead_decrypt_setup(), the application must
2289 * eventually terminate the operation. The following events terminate an
2290 * operation:
2291 * - A failed call to any of the \c psa_aead_xxx functions.
2292 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2293 *
2294 * \param[in,out] operation The operation object to set up. It must have
2295 * been initialized as per the documentation for
2296 * #psa_aead_operation_t and not yet in use.
2297 * \param handle Handle to the key to use for the operation.
2298 * It must remain valid until the operation
2299 * terminates.
2300 * \param alg The AEAD algorithm to compute
2301 * (\c PSA_ALG_XXX value such that
2302 * #PSA_ALG_IS_AEAD(\p alg) is true).
2303 *
2304 * \retval #PSA_SUCCESS
2305 * Success.
2306 * \retval #PSA_ERROR_INVALID_HANDLE
2307 * \retval #PSA_ERROR_EMPTY_SLOT
2308 * \retval #PSA_ERROR_NOT_PERMITTED
2309 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002310 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002311 * \retval #PSA_ERROR_NOT_SUPPORTED
2312 * \p alg is not supported or is not an AEAD algorithm.
2313 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2314 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2315 * \retval #PSA_ERROR_HARDWARE_FAILURE
2316 * \retval #PSA_ERROR_TAMPERING_DETECTED
2317 * \retval #PSA_ERROR_BAD_STATE
2318 * The library has not been previously initialized by psa_crypto_init().
2319 * It is implementation-dependent whether a failure to initialize
2320 * results in this error code.
2321 */
2322psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2323 psa_key_handle_t handle,
2324 psa_algorithm_t alg);
2325
2326/** Generate a random nonce for an authenticated encryption operation.
2327 *
2328 * This function generates a random nonce for the authenticated encryption
2329 * operation with an appropriate size for the chosen algorithm, key type
2330 * and key size.
2331 *
2332 * The application must call psa_aead_encrypt_setup() before
2333 * calling this function.
2334 *
2335 * If this function returns an error status, the operation becomes inactive.
2336 *
2337 * \param[in,out] operation Active AEAD operation.
2338 * \param[out] nonce Buffer where the generated nonce is to be
2339 * written.
2340 * \param nonce_size Size of the \p nonce buffer in bytes.
2341 * \param[out] nonce_length On success, the number of bytes of the
2342 * generated nonce.
2343 *
2344 * \retval #PSA_SUCCESS
2345 * Success.
2346 * \retval #PSA_ERROR_BAD_STATE
2347 * The operation state is not valid (not set up, or nonce already set).
2348 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2349 * The size of the \p nonce buffer is too small.
2350 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2351 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2352 * \retval #PSA_ERROR_HARDWARE_FAILURE
2353 * \retval #PSA_ERROR_TAMPERING_DETECTED
2354 */
2355psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2356 unsigned char *nonce,
2357 size_t nonce_size,
2358 size_t *nonce_length);
2359
2360/** Set the nonce for an authenticated encryption or decryption operation.
2361 *
2362 * This function sets the nonce for the authenticated
2363 * encryption or decryption operation.
2364 *
2365 * The application must call psa_aead_encrypt_setup() before
2366 * calling this function.
2367 *
2368 * If this function returns an error status, the operation becomes inactive.
2369 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002370 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002371 * instead of this function, unless implementing a protocol that requires
2372 * a non-random IV.
2373 *
2374 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002375 * \param[in] nonce Buffer containing the nonce to use.
2376 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002377 *
2378 * \retval #PSA_SUCCESS
2379 * Success.
2380 * \retval #PSA_ERROR_BAD_STATE
2381 * The operation state is not valid (not set up, or nonce already set).
2382 * \retval #PSA_ERROR_INVALID_ARGUMENT
2383 * The size of \p nonce is not acceptable for the chosen algorithm.
2384 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2385 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2386 * \retval #PSA_ERROR_HARDWARE_FAILURE
2387 * \retval #PSA_ERROR_TAMPERING_DETECTED
2388 */
2389psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2390 const unsigned char *nonce,
2391 size_t nonce_length);
2392
Gilles Peskinebc59c852019-01-17 15:26:08 +01002393/** Declare the lengths of the message and additional data for AEAD.
2394 *
2395 * The application must call this function before calling
2396 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2397 * the operation requires it. If the algorithm does not require it,
2398 * calling this function is optional, but if this function is called
2399 * then the implementation must enforce the lengths.
2400 *
2401 * You may call this function before or after setting the nonce with
2402 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2403 *
2404 * - For #PSA_ALG_CCM, calling this function is required.
2405 * - For the other AEAD algorithms defined in this specification, calling
2406 * this function is not required.
2407 * - For vendor-defined algorithm, refer to the vendor documentation.
2408 *
2409 * \param[in,out] operation Active AEAD operation.
2410 * \param ad_length Size of the non-encrypted additional
2411 * authenticated data in bytes.
2412 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2413 *
2414 * \retval #PSA_SUCCESS
2415 * Success.
2416 * \retval #PSA_ERROR_BAD_STATE
2417 * The operation state is not valid (not set up, already completed,
2418 * or psa_aead_update_ad() or psa_aead_update() already called).
2419 * \retval #PSA_ERROR_INVALID_ARGUMENT
2420 * At least one of the lengths is not acceptable for the chosen
2421 * algorithm.
2422 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2423 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2424 * \retval #PSA_ERROR_HARDWARE_FAILURE
2425 * \retval #PSA_ERROR_TAMPERING_DETECTED
2426 */
2427psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2428 size_t ad_length,
2429 size_t plaintext_length);
2430
Gilles Peskine30a9e412019-01-14 18:36:12 +01002431/** Pass additional data to an active AEAD operation.
2432 *
2433 * Additional data is authenticated, but not encrypted.
2434 *
2435 * You may call this function multiple times to pass successive fragments
2436 * of the additional data. You may not call this function after passing
2437 * data to encrypt or decrypt with psa_aead_update().
2438 *
2439 * Before calling this function, you must:
2440 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2441 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2442 *
2443 * If this function returns an error status, the operation becomes inactive.
2444 *
2445 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2446 * there is no guarantee that the input is valid. Therefore, until
2447 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2448 * treat the input as untrusted and prepare to undo any action that
2449 * depends on the input if psa_aead_verify() returns an error status.
2450 *
2451 * \param[in,out] operation Active AEAD operation.
2452 * \param[in] input Buffer containing the fragment of
2453 * additional data.
2454 * \param input_length Size of the \p input buffer in bytes.
2455 *
2456 * \retval #PSA_SUCCESS
2457 * Success.
2458 * \retval #PSA_ERROR_BAD_STATE
2459 * The operation state is not valid (not set up, nonce not set,
2460 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002461 * \retval #PSA_ERROR_INVALID_ARGUMENT
2462 * The total input length overflows the additional data length that
2463 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002464 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2465 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2466 * \retval #PSA_ERROR_HARDWARE_FAILURE
2467 * \retval #PSA_ERROR_TAMPERING_DETECTED
2468 */
2469psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2470 const uint8_t *input,
2471 size_t input_length);
2472
2473/** Encrypt or decrypt a message fragment in an active AEAD operation.
2474 *
2475 * Before calling this function, you must:
2476 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2477 * The choice of setup function determines whether this function
2478 * encrypts or decrypts its input.
2479 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2480 * 3. Call psa_aead_update_ad() to pass all the additional data.
2481 *
2482 * If this function returns an error status, the operation becomes inactive.
2483 *
2484 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2485 * there is no guarantee that the input is valid. Therefore, until
2486 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2487 * - Do not use the output in any way other than storing it in a
2488 * confidential location. If you take any action that depends
2489 * on the tentative decrypted data, this action will need to be
2490 * undone if the input turns out not to be valid. Furthermore,
2491 * if an adversary can observe that this action took place
2492 * (for example through timing), they may be able to use this
2493 * fact as an oracle to decrypt any message encrypted with the
2494 * same key.
2495 * - In particular, do not copy the output anywhere but to a
2496 * memory or storage space that you have exclusive access to.
2497 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002498 * This function does not require the input to be aligned to any
2499 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002500 * a whole block at a time, it must consume all the input provided, but
2501 * it may delay the end of the corresponding output until a subsequent
2502 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2503 * provides sufficient input. The amount of data that can be delayed
2504 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002505 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002506 * \param[in,out] operation Active AEAD operation.
2507 * \param[in] input Buffer containing the message fragment to
2508 * encrypt or decrypt.
2509 * \param input_length Size of the \p input buffer in bytes.
2510 * \param[out] output Buffer where the output is to be written.
2511 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002512 * This must be at least
2513 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2514 * \p input_length) where \c alg is the
2515 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002516 * \param[out] output_length On success, the number of bytes
2517 * that make up the returned output.
2518 *
2519 * \retval #PSA_SUCCESS
2520 * Success.
2521 * \retval #PSA_ERROR_BAD_STATE
2522 * The operation state is not valid (not set up, nonce not set
2523 * or already completed).
2524 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2525 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002526 * You can determine a sufficient buffer size by calling
2527 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2528 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002529 * \retval #PSA_ERROR_INVALID_ARGUMENT
2530 * The total length of input to psa_aead_update_ad() so far is
2531 * less than the additional data length that was previously
2532 * specified with psa_aead_set_lengths().
2533 * \retval #PSA_ERROR_INVALID_ARGUMENT
2534 * The total input length overflows the plaintext length that
2535 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002536 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2537 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2538 * \retval #PSA_ERROR_HARDWARE_FAILURE
2539 * \retval #PSA_ERROR_TAMPERING_DETECTED
2540 */
2541psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2542 const uint8_t *input,
2543 size_t input_length,
2544 unsigned char *output,
2545 size_t output_size,
2546 size_t *output_length);
2547
2548/** Finish encrypting a message in an AEAD operation.
2549 *
2550 * The operation must have been set up with psa_aead_encrypt_setup().
2551 *
2552 * This function finishes the authentication of the additional data
2553 * formed by concatenating the inputs passed to preceding calls to
2554 * psa_aead_update_ad() with the plaintext formed by concatenating the
2555 * inputs passed to preceding calls to psa_aead_update().
2556 *
2557 * This function has two output buffers:
2558 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002559 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002560 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002561 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002562 * that the operation performs.
2563 *
2564 * When this function returns, the operation becomes inactive.
2565 *
2566 * \param[in,out] operation Active AEAD operation.
2567 * \param[out] ciphertext Buffer where the last part of the ciphertext
2568 * is to be written.
2569 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002570 * This must be at least
2571 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2572 * \c alg is the algorithm that is being
2573 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002574 * \param[out] ciphertext_length On success, the number of bytes of
2575 * returned ciphertext.
2576 * \param[out] tag Buffer where the authentication tag is
2577 * to be written.
2578 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002579 * This must be at least
2580 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2581 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002582 * \param[out] tag_length On success, the number of bytes
2583 * that make up the returned tag.
2584 *
2585 * \retval #PSA_SUCCESS
2586 * Success.
2587 * \retval #PSA_ERROR_BAD_STATE
2588 * The operation state is not valid (not set up, nonce not set,
2589 * decryption, or already completed).
2590 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002591 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002592 * You can determine a sufficient buffer size for \p ciphertext by
2593 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2594 * where \c alg is the algorithm that is being calculated.
2595 * You can determine a sufficient buffer size for \p tag by
2596 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002597 * \retval #PSA_ERROR_INVALID_ARGUMENT
2598 * The total length of input to psa_aead_update_ad() so far is
2599 * less than the additional data length that was previously
2600 * specified with psa_aead_set_lengths().
2601 * \retval #PSA_ERROR_INVALID_ARGUMENT
2602 * The total length of input to psa_aead_update() so far is
2603 * less than the plaintext length that was previously
2604 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002605 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2606 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2607 * \retval #PSA_ERROR_HARDWARE_FAILURE
2608 * \retval #PSA_ERROR_TAMPERING_DETECTED
2609 */
2610psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002611 uint8_t *ciphertext,
2612 size_t ciphertext_size,
2613 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002614 uint8_t *tag,
2615 size_t tag_size,
2616 size_t *tag_length);
2617
2618/** Finish authenticating and decrypting a message in an AEAD operation.
2619 *
2620 * The operation must have been set up with psa_aead_decrypt_setup().
2621 *
2622 * This function finishes the authentication of the additional data
2623 * formed by concatenating the inputs passed to preceding calls to
2624 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2625 * inputs passed to preceding calls to psa_aead_update().
2626 *
2627 * When this function returns, the operation becomes inactive.
2628 *
2629 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002630 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002631 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002632 * from previous calls to psa_aead_update()
2633 * that could not be processed until the end
2634 * of the input.
2635 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002636 * This must be at least
2637 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2638 * \c alg is the algorithm that is being
2639 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002640 * \param[out] plaintext_length On success, the number of bytes of
2641 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002642 * \param[in] tag Buffer containing the authentication tag.
2643 * \param tag_length Size of the \p tag buffer in bytes.
2644 *
2645 * \retval #PSA_SUCCESS
2646 * Success.
2647 * \retval #PSA_ERROR_BAD_STATE
2648 * The operation state is not valid (not set up, nonce not set,
2649 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002650 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2651 * The size of the \p plaintext buffer is too small.
2652 * You can determine a sufficient buffer size for \p plaintext by
2653 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2654 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002655 * \retval #PSA_ERROR_INVALID_ARGUMENT
2656 * The total length of input to psa_aead_update_ad() so far is
2657 * less than the additional data length that was previously
2658 * specified with psa_aead_set_lengths().
2659 * \retval #PSA_ERROR_INVALID_ARGUMENT
2660 * The total length of input to psa_aead_update() so far is
2661 * less than the plaintext length that was previously
2662 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002663 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2664 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2665 * \retval #PSA_ERROR_HARDWARE_FAILURE
2666 * \retval #PSA_ERROR_TAMPERING_DETECTED
2667 */
2668psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002669 uint8_t *plaintext,
2670 size_t plaintext_size,
2671 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002672 const uint8_t *tag,
2673 size_t tag_length);
2674
2675/** Abort an AEAD operation.
2676 *
2677 * Aborting an operation frees all associated resources except for the
2678 * \p operation structure itself. Once aborted, the operation object
2679 * can be reused for another operation by calling
2680 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2681 *
2682 * You may call this function any time after the operation object has
2683 * been initialized by any of the following methods:
2684 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2685 * whether it succeeds or not.
2686 * - Initializing the \c struct to all-bits-zero.
2687 * - Initializing the \c struct to logical zeros, e.g.
2688 * `psa_aead_operation_t operation = {0}`.
2689 *
2690 * In particular, calling psa_aead_abort() after the operation has been
2691 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2692 * is safe and has no effect.
2693 *
2694 * \param[in,out] operation Initialized AEAD operation.
2695 *
2696 * \retval #PSA_SUCCESS
2697 * \retval #PSA_ERROR_BAD_STATE
2698 * \p operation is not an active AEAD operation.
2699 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2700 * \retval #PSA_ERROR_HARDWARE_FAILURE
2701 * \retval #PSA_ERROR_TAMPERING_DETECTED
2702 */
2703psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2704
Gilles Peskine3b555712018-03-03 21:27:57 +01002705/**@}*/
2706
Gilles Peskine20035e32018-02-03 22:44:14 +01002707/** \defgroup asymmetric Asymmetric cryptography
2708 * @{
2709 */
2710
2711/**
2712 * \brief Sign a hash or short message with a private key.
2713 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002714 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002715 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002716 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2717 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2718 * to determine the hash algorithm to use.
2719 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002720 * \param handle Handle to the key to use for the operation.
2721 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002722 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002723 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002724 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002725 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002726 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002727 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002728 * \param[out] signature_length On success, the number of bytes
2729 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002730 *
Gilles Peskine28538492018-07-11 17:34:00 +02002731 * \retval #PSA_SUCCESS
2732 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002733 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002734 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002735 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002736 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002737 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002738 * \retval #PSA_ERROR_NOT_SUPPORTED
2739 * \retval #PSA_ERROR_INVALID_ARGUMENT
2740 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2741 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2742 * \retval #PSA_ERROR_HARDWARE_FAILURE
2743 * \retval #PSA_ERROR_TAMPERING_DETECTED
2744 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002745 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002746 * The library has not been previously initialized by psa_crypto_init().
2747 * It is implementation-dependent whether a failure to initialize
2748 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002749 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002750psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002751 psa_algorithm_t alg,
2752 const uint8_t *hash,
2753 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002754 uint8_t *signature,
2755 size_t signature_size,
2756 size_t *signature_length);
2757
2758/**
2759 * \brief Verify the signature a hash or short message using a public key.
2760 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002761 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002762 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002763 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2764 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2765 * to determine the hash algorithm to use.
2766 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002767 * \param handle Handle to the key to use for the operation.
2768 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002769 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002770 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002771 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002772 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002773 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002774 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002775 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002776 *
Gilles Peskine28538492018-07-11 17:34:00 +02002777 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002778 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002779 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002780 * The calculation was perfomed successfully, but the passed
2781 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002782 * \retval #PSA_ERROR_NOT_SUPPORTED
2783 * \retval #PSA_ERROR_INVALID_ARGUMENT
2784 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2785 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2786 * \retval #PSA_ERROR_HARDWARE_FAILURE
2787 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002788 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002789 * The library has not been previously initialized by psa_crypto_init().
2790 * It is implementation-dependent whether a failure to initialize
2791 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002792 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002793psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002794 psa_algorithm_t alg,
2795 const uint8_t *hash,
2796 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002797 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002798 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002799
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002800/**
2801 * \brief Encrypt a short message with a public key.
2802 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002803 * \param handle Handle to the key to use for the operation.
2804 * It must be a public key or an asymmetric
2805 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002806 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002807 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002808 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002809 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002810 * \param[in] salt A salt or label, if supported by the
2811 * encryption algorithm.
2812 * If the algorithm does not support a
2813 * salt, pass \c NULL.
2814 * If the algorithm supports an optional
2815 * salt and you do not want to pass a salt,
2816 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002817 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002818 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2819 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002820 * \param salt_length Size of the \p salt buffer in bytes.
2821 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002822 * \param[out] output Buffer where the encrypted message is to
2823 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002824 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002825 * \param[out] output_length On success, the number of bytes
2826 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002827 *
Gilles Peskine28538492018-07-11 17:34:00 +02002828 * \retval #PSA_SUCCESS
2829 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002830 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002831 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002832 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002833 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002834 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002835 * \retval #PSA_ERROR_NOT_SUPPORTED
2836 * \retval #PSA_ERROR_INVALID_ARGUMENT
2837 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2838 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2839 * \retval #PSA_ERROR_HARDWARE_FAILURE
2840 * \retval #PSA_ERROR_TAMPERING_DETECTED
2841 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002842 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002843 * The library has not been previously initialized by psa_crypto_init().
2844 * It is implementation-dependent whether a failure to initialize
2845 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002846 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002847psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002848 psa_algorithm_t alg,
2849 const uint8_t *input,
2850 size_t input_length,
2851 const uint8_t *salt,
2852 size_t salt_length,
2853 uint8_t *output,
2854 size_t output_size,
2855 size_t *output_length);
2856
2857/**
2858 * \brief Decrypt a short message with a private key.
2859 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002860 * \param handle Handle to the key to use for the operation.
2861 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002862 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002863 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002864 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002865 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002866 * \param[in] salt A salt or label, if supported by the
2867 * encryption algorithm.
2868 * If the algorithm does not support a
2869 * salt, pass \c NULL.
2870 * If the algorithm supports an optional
2871 * salt and you do not want to pass a salt,
2872 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002873 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002874 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2875 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002876 * \param salt_length Size of the \p salt buffer in bytes.
2877 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002878 * \param[out] output Buffer where the decrypted message is to
2879 * be written.
2880 * \param output_size Size of the \c output buffer in bytes.
2881 * \param[out] output_length On success, the number of bytes
2882 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002883 *
Gilles Peskine28538492018-07-11 17:34:00 +02002884 * \retval #PSA_SUCCESS
2885 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002886 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002887 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002888 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002889 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002890 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002891 * \retval #PSA_ERROR_NOT_SUPPORTED
2892 * \retval #PSA_ERROR_INVALID_ARGUMENT
2893 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2894 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2895 * \retval #PSA_ERROR_HARDWARE_FAILURE
2896 * \retval #PSA_ERROR_TAMPERING_DETECTED
2897 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2898 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002899 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002900 * The library has not been previously initialized by psa_crypto_init().
2901 * It is implementation-dependent whether a failure to initialize
2902 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002903 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002904psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002905 psa_algorithm_t alg,
2906 const uint8_t *input,
2907 size_t input_length,
2908 const uint8_t *salt,
2909 size_t salt_length,
2910 uint8_t *output,
2911 size_t output_size,
2912 size_t *output_length);
2913
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002914/**@}*/
2915
Gilles Peskineedd76872018-07-20 17:42:05 +02002916/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002917 * @{
2918 */
2919
2920/** The type of the state data structure for generators.
2921 *
2922 * Before calling any function on a generator, the application must
2923 * initialize it by any of the following means:
2924 * - Set the structure to all-bits-zero, for example:
2925 * \code
2926 * psa_crypto_generator_t generator;
2927 * memset(&generator, 0, sizeof(generator));
2928 * \endcode
2929 * - Initialize the structure to logical zero values, for example:
2930 * \code
2931 * psa_crypto_generator_t generator = {0};
2932 * \endcode
2933 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2934 * for example:
2935 * \code
2936 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2937 * \endcode
2938 * - Assign the result of the function psa_crypto_generator_init()
2939 * to the structure, for example:
2940 * \code
2941 * psa_crypto_generator_t generator;
2942 * generator = psa_crypto_generator_init();
2943 * \endcode
2944 *
2945 * This is an implementation-defined \c struct. Applications should not
2946 * make any assumptions about the content of this structure except
2947 * as directed by the documentation of a specific implementation.
2948 */
2949typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2950
2951/** \def PSA_CRYPTO_GENERATOR_INIT
2952 *
2953 * This macro returns a suitable initializer for a generator object
2954 * of type #psa_crypto_generator_t.
2955 */
2956#ifdef __DOXYGEN_ONLY__
2957/* This is an example definition for documentation purposes.
2958 * Implementations should define a suitable value in `crypto_struct.h`.
2959 */
2960#define PSA_CRYPTO_GENERATOR_INIT {0}
2961#endif
2962
2963/** Return an initial value for a generator object.
2964 */
2965static psa_crypto_generator_t psa_crypto_generator_init(void);
2966
2967/** Retrieve the current capacity of a generator.
2968 *
2969 * The capacity of a generator is the maximum number of bytes that it can
2970 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2971 *
2972 * \param[in] generator The generator to query.
2973 * \param[out] capacity On success, the capacity of the generator.
2974 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002975 * \retval #PSA_SUCCESS
2976 * \retval #PSA_ERROR_BAD_STATE
2977 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002978 */
2979psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2980 size_t *capacity);
2981
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002982/** Set the maximum capacity of a generator.
2983 *
2984 * \param[in,out] generator The generator object to modify.
2985 * \param capacity The new capacity of the generator.
2986 * It must be less or equal to the generator's
2987 * current capacity.
2988 *
2989 * \retval #PSA_SUCCESS
2990 * \retval #PSA_ERROR_INVALID_ARGUMENT
2991 * \p capacity is larger than the generator's current capacity.
2992 * \retval #PSA_ERROR_BAD_STATE
2993 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2994 */
2995psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
2996 size_t capacity);
2997
Gilles Peskineeab56e42018-07-12 17:12:33 +02002998/** Read some data from a generator.
2999 *
3000 * This function reads and returns a sequence of bytes from a generator.
3001 * The data that is read is discarded from the generator. The generator's
3002 * capacity is decreased by the number of bytes read.
3003 *
3004 * \param[in,out] generator The generator object to read from.
3005 * \param[out] output Buffer where the generator output will be
3006 * written.
3007 * \param output_length Number of bytes to output.
3008 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003009 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003010 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskineeab56e42018-07-12 17:12:33 +02003011 * There were fewer than \p output_length bytes
3012 * in the generator. Note that in this case, no
3013 * output is written to the output buffer.
3014 * The generator's capacity is set to 0, thus
3015 * subsequent calls to this function will not
3016 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003017 * \retval #PSA_ERROR_BAD_STATE
3018 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3019 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3020 * \retval #PSA_ERROR_HARDWARE_FAILURE
3021 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003022 */
3023psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
3024 uint8_t *output,
3025 size_t output_length);
3026
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003027/** Generate a key deterministically from data read from a generator.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003028 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003029 * This function uses the output of a generator to derive a key.
3030 * How much output it consumes and how the key is derived depends on the
3031 * key type.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003032 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003033 * - For key types for which the key is an arbitrary sequence of bytes
3034 * of a given size,
3035 * this function is functionally equivalent to calling #psa_generator_read
3036 * and passing the resulting output to #psa_import_key.
3037 * However, this function has a security benefit:
3038 * if the implementation provides an isolation boundary then
3039 * the key material is not exposed outside the isolation boundary.
3040 * As a consequence, for these key types, this function always consumes
3041 * exactly (\p bits / 8) bytes from the generator.
3042 * The following key types defined in this specification follow this scheme:
3043 *
3044 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003045 * - #PSA_KEY_TYPE_ARC4;
3046 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003047 * - #PSA_KEY_TYPE_DERIVE;
3048 * - #PSA_KEY_TYPE_HMAC.
3049 *
3050 * - For ECC keys on a Montgomery elliptic curve
3051 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3052 * Montgomery curve), this function always draws a byte string whose
3053 * length is determined by the curve, and sets the mandatory bits
3054 * accordingly. That is:
3055 *
3056 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3057 * and process it as specified in RFC 7748 &sect;5.
3058 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3059 * and process it as specified in RFC 7748 &sect;5.
3060 *
3061 * - For key types for which the key is represented by a single sequence of
3062 * \p bits bits with constraints as to which bit sequences are acceptable,
3063 * this function draws a byte string of length (\p bits / 8) bytes rounded
3064 * up to the nearest whole number of bytes. If the resulting byte string
3065 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3066 * This process is repeated until an acceptable byte string is drawn.
3067 * The byte string drawn from the generator is interpreted as specified
3068 * for the output produced by psa_export_key().
3069 * The following key types defined in this specification follow this scheme:
3070 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003071 * - #PSA_KEY_TYPE_DES.
3072 * Force-set the parity bits, but discard forbidden weak keys.
3073 * For 2-key and 3-key triple-DES, the three keys are generated
3074 * successively (for example, for 3-key triple-DES,
3075 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3076 * discard the first 8 bytes, use the next 8 bytes as the first key,
3077 * and continue reading output from the generator to derive the other
3078 * two keys).
3079 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR),
3080 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
3081 * ECC keys on a Weierstrass elliptic curve
3082 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3083 * Weierstrass curve).
3084 * For these key types, interpret the byte string as integer
3085 * in big-endian order. Discard it if it is not in the range
3086 * [0, *N* - 2] where *N* is the boundary of the private key domain
3087 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003088 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003089 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003090 * This method allows compliance to NIST standards, specifically
3091 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003092 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3093 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3094 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3095 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003096 *
3097 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
3098 * the way in which the generator output is consumed is
3099 * implementation-defined.
3100 *
3101 * In all cases, the data that is read is discarded from the generator.
3102 * The generator's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003103 *
Gilles Peskine20628592019-04-19 19:29:50 +02003104 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003105 * \param[out] handle On success, a handle to the newly created key.
3106 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003107 * \param[in,out] generator The generator object to read from.
3108 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003109 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003110 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003111 * If the key is persistent, the key material and the key's metadata
3112 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003113 * \retval #PSA_ERROR_ALREADY_EXISTS
3114 * This is an attempt to create a persistent key, and there is
3115 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003116 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003117 * There was not enough data to create the desired key.
3118 * Note that in this case, no output is written to the output buffer.
3119 * The generator's capacity is set to 0, thus subsequent calls to
3120 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003121 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003122 * The key type or key size is not supported, either by the
3123 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003124 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003125 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3126 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3127 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3128 * \retval #PSA_ERROR_HARDWARE_FAILURE
3129 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003130 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003131 * The library has not been previously initialized by psa_crypto_init().
3132 * It is implementation-dependent whether a failure to initialize
3133 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003134 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003135psa_status_t psa_generate_derived_key(const psa_key_attributes_t *attributes,
Gilles Peskine87a5e562019-04-17 12:28:25 +02003136 psa_key_handle_t *handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003137 psa_crypto_generator_t *generator);
3138
3139/** Abort a generator.
3140 *
3141 * Once a generator has been aborted, its capacity is zero.
3142 * Aborting a generator frees all associated resources except for the
3143 * \c generator structure itself.
3144 *
3145 * This function may be called at any time as long as the generator
3146 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
3147 * psa_crypto_generator_init() or a zero value. In particular, it is valid
3148 * to call psa_generator_abort() twice, or to call psa_generator_abort()
3149 * on a generator that has not been set up.
3150 *
3151 * Once aborted, the generator object may be called.
3152 *
3153 * \param[in,out] generator The generator to abort.
3154 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003155 * \retval #PSA_SUCCESS
3156 * \retval #PSA_ERROR_BAD_STATE
3157 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3158 * \retval #PSA_ERROR_HARDWARE_FAILURE
3159 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003160 */
3161psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3162
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003163/** Use the maximum possible capacity for a generator.
3164 *
3165 * Use this value as the capacity argument when setting up a generator
3166 * to indicate that the generator should have the maximum possible capacity.
3167 * The value of the maximum possible capacity depends on the generator
3168 * algorithm.
3169 */
3170#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
3171
Gilles Peskineeab56e42018-07-12 17:12:33 +02003172/**@}*/
3173
Gilles Peskineea0fb492018-07-12 17:17:20 +02003174/** \defgroup derivation Key derivation
3175 * @{
3176 */
3177
3178/** Set up a key derivation operation.
3179 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003180 * A key derivation algorithm takes some inputs and uses them to create
3181 * a byte generator which can be used to produce keys and other
3182 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003183 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003184 * To use a generator for key derivation:
3185 * - Start with an initialized object of type #psa_crypto_generator_t.
3186 * - Call psa_key_derivation_setup() to select the algorithm.
3187 * - Provide the inputs for the key derivation by calling
3188 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3189 * as appropriate. Which inputs are needed, in what order, and whether
3190 * they may be keys and if so of what type depends on the algorithm.
3191 * - Optionally set the generator's maximum capacity with
3192 * psa_set_generator_capacity(). You may do this before, in the middle of
3193 * or after providing inputs. For some algorithms, this step is mandatory
3194 * because the output depends on the maximum capacity.
3195 * - Generate output with psa_generator_read() or
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003196 * psa_generate_derived_key(). Successive calls to these functions
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003197 * use successive output bytes from the generator.
3198 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02003199 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003200 * \param[in,out] generator The generator object to set up. It must
3201 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003202 * \param alg The key derivation algorithm to compute
3203 * (\c PSA_ALG_XXX value such that
3204 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02003205 *
3206 * \retval #PSA_SUCCESS
3207 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003208 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003209 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003210 * \retval #PSA_ERROR_NOT_SUPPORTED
3211 * \c alg is not supported or is not a key derivation algorithm.
3212 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3213 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3214 * \retval #PSA_ERROR_HARDWARE_FAILURE
3215 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003216 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003217 */
3218psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
3219 psa_algorithm_t alg);
3220
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003221/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003222 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003223 * Which inputs are required and in what order depends on the algorithm.
3224 * Refer to the documentation of each key derivation or key agreement
3225 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003226 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003227 * This function passes direct inputs. Some inputs must be passed as keys
3228 * using psa_key_derivation_input_key() instead of this function. Refer to
3229 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003230 *
3231 * \param[in,out] generator The generator object to use. It must
3232 * have been set up with
3233 * psa_key_derivation_setup() and must not
3234 * have produced any output yet.
3235 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003236 * \param[in] data Input data to use.
3237 * \param data_length Size of the \p data buffer in bytes.
3238 *
3239 * \retval #PSA_SUCCESS
3240 * Success.
3241 * \retval #PSA_ERROR_INVALID_ARGUMENT
3242 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003243 * \retval #PSA_ERROR_INVALID_ARGUMENT
3244 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003245 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3246 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3247 * \retval #PSA_ERROR_HARDWARE_FAILURE
3248 * \retval #PSA_ERROR_TAMPERING_DETECTED
3249 * \retval #PSA_ERROR_BAD_STATE
3250 * The value of \p step is not valid given the state of \p generator.
3251 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003252 * The library has not been previously initialized by psa_crypto_init().
3253 * It is implementation-dependent whether a failure to initialize
3254 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003255 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003256psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3257 psa_key_derivation_step_t step,
3258 const uint8_t *data,
3259 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003260
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003261/** Provide an input for key derivation in the form of a key.
3262 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003263 * Which inputs are required and in what order depends on the algorithm.
3264 * Refer to the documentation of each key derivation or key agreement
3265 * algorithm for information.
3266 *
3267 * This function passes key inputs. Some inputs must be passed as keys
3268 * of the appropriate type using this function, while others must be
3269 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3270 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003271 *
3272 * \param[in,out] generator The generator object to use. It must
3273 * have been set up with
3274 * psa_key_derivation_setup() and must not
3275 * have produced any output yet.
3276 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003277 * \param handle Handle to the key. It must have an
3278 * appropriate type for \p step and must
3279 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003280 *
3281 * \retval #PSA_SUCCESS
3282 * Success.
3283 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine31351842019-04-09 12:00:00 +02003284 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003285 * \retval #PSA_ERROR_NOT_PERMITTED
3286 * \retval #PSA_ERROR_INVALID_ARGUMENT
3287 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003288 * \retval #PSA_ERROR_INVALID_ARGUMENT
3289 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003290 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3291 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3292 * \retval #PSA_ERROR_HARDWARE_FAILURE
3293 * \retval #PSA_ERROR_TAMPERING_DETECTED
3294 * \retval #PSA_ERROR_BAD_STATE
3295 * The value of \p step is not valid given the state of \p generator.
3296 * \retval #PSA_ERROR_BAD_STATE
3297 * The library has not been previously initialized by psa_crypto_init().
3298 * It is implementation-dependent whether a failure to initialize
3299 * results in this error code.
3300 */
3301psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3302 psa_key_derivation_step_t step,
3303 psa_key_handle_t handle);
3304
Gilles Peskine969c5d62019-01-16 15:53:06 +01003305/** Perform a key agreement and use the shared secret as input to a key
3306 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003307 *
3308 * A key agreement algorithm takes two inputs: a private key \p private_key
3309 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003310 * The result of this function is passed as input to a key derivation.
3311 * The output of this key derivation can be extracted by reading from the
3312 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003313 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003314 * \param[in,out] generator The generator object to use. It must
3315 * have been set up with
3316 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003317 * key agreement and derivation algorithm
3318 * \c alg (\c PSA_ALG_XXX value such that
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003319 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3320 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
Gilles Peskine6843c292019-01-18 16:44:49 +01003321 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003322 * The generator must be ready for an
3323 * input of the type given by \p step.
3324 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003325 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003326 * \param[in] peer_key Public key of the peer. The peer key must be in the
3327 * same format that psa_import_key() accepts for the
3328 * public key type corresponding to the type of
3329 * private_key. That is, this function performs the
3330 * equivalent of
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003331 * #psa_import_key(`internal_public_key_handle`,
3332 * #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(`private_key_type`),
3333 * `peer_key`, `peer_key_length`) where
Jaeden Amero8afbff82019-01-14 16:56:20 +00003334 * `private_key_type` is the type of `private_key`.
3335 * For example, for EC keys, this means that peer_key
3336 * is interpreted as a point on the curve that the
3337 * private key is on. The standard formats for public
3338 * keys are documented in the documentation of
3339 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003340 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003341 *
3342 * \retval #PSA_SUCCESS
3343 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003344 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02003345 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine01d718c2018-09-18 12:01:02 +02003346 * \retval #PSA_ERROR_NOT_PERMITTED
3347 * \retval #PSA_ERROR_INVALID_ARGUMENT
3348 * \c private_key is not compatible with \c alg,
3349 * or \p peer_key is not valid for \c alg or not compatible with
3350 * \c private_key.
3351 * \retval #PSA_ERROR_NOT_SUPPORTED
3352 * \c alg is not supported or is not a key derivation algorithm.
3353 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3354 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3355 * \retval #PSA_ERROR_HARDWARE_FAILURE
3356 * \retval #PSA_ERROR_TAMPERING_DETECTED
3357 */
3358psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003359 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003360 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003361 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003362 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003363
Gilles Peskine769c7a62019-01-18 16:42:29 +01003364/** Perform a key agreement and use the shared secret as input to a key
3365 * derivation.
3366 *
3367 * A key agreement algorithm takes two inputs: a private key \p private_key
3368 * a public key \p peer_key.
3369 *
3370 * \warning The raw result of a key agreement algorithm such as finite-field
3371 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3372 * not be used directly as key material. It should instead be passed as
3373 * input to a key derivation algorithm. To chain a key agreement with
3374 * a key derivation, use psa_key_agreement() and other functions from
3375 * the key derivation and generator interface.
3376 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003377 * \param alg The key agreement algorithm to compute
3378 * (\c PSA_ALG_XXX value such that
3379 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3380 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003381 * \param private_key Handle to the private key to use.
3382 * \param[in] peer_key Public key of the peer. It must be
3383 * in the same format that psa_import_key()
3384 * accepts. The standard formats for public
3385 * keys are documented in the documentation
3386 * of psa_export_public_key().
3387 * \param peer_key_length Size of \p peer_key in bytes.
3388 * \param[out] output Buffer where the decrypted message is to
3389 * be written.
3390 * \param output_size Size of the \c output buffer in bytes.
3391 * \param[out] output_length On success, the number of bytes
3392 * that make up the returned output.
3393 *
3394 * \retval #PSA_SUCCESS
3395 * Success.
3396 * \retval #PSA_ERROR_INVALID_HANDLE
3397 * \retval #PSA_ERROR_EMPTY_SLOT
3398 * \retval #PSA_ERROR_NOT_PERMITTED
3399 * \retval #PSA_ERROR_INVALID_ARGUMENT
3400 * \p alg is not a key agreement algorithm
3401 * \retval #PSA_ERROR_INVALID_ARGUMENT
3402 * \p private_key is not compatible with \p alg,
3403 * or \p peer_key is not valid for \p alg or not compatible with
3404 * \p private_key.
3405 * \retval #PSA_ERROR_NOT_SUPPORTED
3406 * \p alg is not a supported key agreement algorithm.
3407 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3408 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3409 * \retval #PSA_ERROR_HARDWARE_FAILURE
3410 * \retval #PSA_ERROR_TAMPERING_DETECTED
3411 */
3412psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3413 psa_key_handle_t private_key,
3414 const uint8_t *peer_key,
3415 size_t peer_key_length,
3416 uint8_t *output,
3417 size_t output_size,
3418 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003419
3420/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003421
3422/** \defgroup random Random generation
3423 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003424 */
3425
3426/**
3427 * \brief Generate random bytes.
3428 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003429 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003430 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003431 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003432 *
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003433 * \note To generate a key, use psa_generate_random_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003434 *
3435 * \param[out] output Output buffer for the generated data.
3436 * \param output_size Number of bytes to generate and output.
3437 *
3438 * \retval #PSA_SUCCESS
3439 * \retval #PSA_ERROR_NOT_SUPPORTED
3440 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3441 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3442 * \retval #PSA_ERROR_HARDWARE_FAILURE
3443 * \retval #PSA_ERROR_TAMPERING_DETECTED
3444 * \retval #PSA_ERROR_BAD_STATE
3445 * The library has not been previously initialized by psa_crypto_init().
3446 * It is implementation-dependent whether a failure to initialize
3447 * results in this error code.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003448 */
3449psa_status_t psa_generate_random(uint8_t *output,
3450 size_t output_size);
3451
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003452/**
3453 * \brief Generate a key or key pair.
3454 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003455 * The key is generated randomly.
3456 * Its location, policy, type and size are taken from \p attributes.
3457 *
3458 * If the type requires additional domain parameters, these are taken
3459 * from \p attributes as well. The following types use domain parameters:
3460 * - When generating an RSA key (#PSA_KEY_TYPE_RSA_KEYPAIR),
3461 * the default public exponent is 65537. This value is used if
3462 * \p attributes was set with psa_set_key_type() or by passing an empty
3463 * byte string as domain parameters to psa_set_key_domain_parameters().
3464 * If psa_set_key_domain_parameters() was used to set a non-empty
3465 * domain parameter string in \p attributes, this string is read as
3466 * a big-endian integer which is used as the public exponent.
3467 * - When generating a DSA key (#PSA_KEY_TYPE_DSA_KEYPAIR) or a
3468 * Diffie-Hellman key (#PSA_KEY_TYPE_DH_KEYPAIR), the domain parameters
3469 * from \p attributes are interpreted as described for
3470 * psa_set_key_domain_parameters().
3471 *
Gilles Peskine20628592019-04-19 19:29:50 +02003472 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003473 * \param[out] handle On success, a handle to the newly created key.
3474 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003475 *
Gilles Peskine28538492018-07-11 17:34:00 +02003476 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003477 * Success.
3478 * If the key is persistent, the key material and the key's metadata
3479 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003480 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003481 * This is an attempt to create a persistent key, and there is
3482 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003483 * \retval #PSA_ERROR_NOT_SUPPORTED
3484 * \retval #PSA_ERROR_INVALID_ARGUMENT
3485 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3486 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3487 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3488 * \retval #PSA_ERROR_HARDWARE_FAILURE
3489 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003490 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003491 * The library has not been previously initialized by psa_crypto_init().
3492 * It is implementation-dependent whether a failure to initialize
3493 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003494 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003495psa_status_t psa_generate_random_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003496 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003497
3498/**@}*/
3499
Gilles Peskinee59236f2018-01-27 23:32:46 +01003500#ifdef __cplusplus
3501}
3502#endif
3503
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003504/* The file "crypto_sizes.h" contains definitions for size calculation
3505 * macros whose definitions are implementation-specific. */
3506#include "crypto_sizes.h"
3507
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003508/* The file "crypto_struct.h" contains definitions for
3509 * implementation-specific structs that are declared above. */
3510#include "crypto_struct.h"
3511
3512/* The file "crypto_extra.h" contains vendor-specific definitions. This
3513 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003514#include "crypto_extra.h"
3515
3516#endif /* PSA_CRYPTO_H */