blob: 043db3288e9a0c3f52a86e8f8130b97241383c5f [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 *
Gilles Peskine4a231b82019-05-06 18:56:14 +0200515 * Implementations may provide additional keys that can be opened with
516 * psa_open_key(). Such keys have a key identifier in the vendor range,
517 * as documented in the description of #psa_key_id_t.
518 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100519 * \param id The persistent identifier of the key.
520 * \param[out] handle On success, a handle to a key slot which contains
521 * the data and metadata loaded from the specified
522 * persistent location.
523 *
524 * \retval #PSA_SUCCESS
525 * Success. The application can now use the value of `*handle`
526 * to access the newly allocated key slot.
527 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200528 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100529 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine225010f2019-05-06 18:44:55 +0200530 * \p id is invalid.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100531 * \retval #PSA_ERROR_NOT_PERMITTED
532 * The specified key exists, but the application does not have the
533 * permission to access it. Note that this specification does not
534 * define any way to create such a key, but it may be possible
535 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200536 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
537 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100538 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200539psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100540 psa_key_handle_t *handle);
541
Gilles Peskinef535eb22018-11-30 14:08:36 +0100542/** Close a key handle.
543 *
544 * If the handle designates a volatile key, destroy the key material and
545 * free all associated resources, just like psa_destroy_key().
546 *
547 * If the handle designates a persistent key, free all resources associated
548 * with the key in volatile memory. The key slot in persistent storage is
549 * not affected and can be opened again later with psa_open_key().
550 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100551 * If the key is currently in use in a multipart operation,
552 * the multipart operation is aborted.
553 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100554 * \param handle The key handle to close.
555 *
556 * \retval #PSA_SUCCESS
557 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100558 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100559 */
560psa_status_t psa_close_key(psa_key_handle_t handle);
561
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100562/**@}*/
563
564/** \defgroup import_export Key import and export
565 * @{
566 */
567
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568/**
569 * \brief Import a key in binary format.
570 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100571 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100572 * documentation of psa_export_public_key() for the format of public keys
573 * and to the documentation of psa_export_key() for the format for
574 * other key types.
575 *
576 * This specification supports a single format for each key type.
577 * Implementations may support other formats as long as the standard
578 * format is supported. Implementations that support other formats
579 * should ensure that the formats are clearly unambiguous so as to
580 * minimize the risk that an invalid input is accidentally interpreted
581 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100582 *
Gilles Peskine20628592019-04-19 19:29:50 +0200583 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200584 * The key size is always determined from the
585 * \p data buffer.
586 * If the key size in \p attributes is nonzero,
587 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200588 * \param[out] handle On success, a handle to the newly created key.
589 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100590 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine20628592019-04-19 19:29:50 +0200591 * buffer is interpreted according to the type and,
592 * if applicable, domain parameters declared in
593 * \p attributes.
594 * All implementations must support at least the format
595 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100596 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200597 * the chosen type. Implementations may allow other
598 * formats, but should be conservative: implementations
599 * should err on the side of rejecting content if it
600 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200601 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100602 *
Gilles Peskine28538492018-07-11 17:34:00 +0200603 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100604 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100605 * If the key is persistent, the key material and the key's metadata
606 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200607 * \retval #PSA_ERROR_ALREADY_EXISTS
608 * This is an attempt to create a persistent key, and there is
609 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200610 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200611 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200612 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200613 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200614 * The key attributes, as a whole, are invalid.
615 * \retval #PSA_ERROR_INVALID_ARGUMENT
616 * The key data is not correctly formatted.
617 * \retval #PSA_ERROR_INVALID_ARGUMENT
618 * The size in \p attributes is nonzero and does not match the size
619 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200620 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
621 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
622 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100623 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200624 * \retval #PSA_ERROR_HARDWARE_FAILURE
625 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300626 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300627 * The library has not been previously initialized by psa_crypto_init().
628 * It is implementation-dependent whether a failure to initialize
629 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100630 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200631psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100632 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200633 size_t data_length,
634 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100635
636/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100637 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200638 *
639 * This function destroys the content of the key slot from both volatile
640 * memory and, if applicable, non-volatile storage. Implementations shall
641 * make a best effort to ensure that any previous content of the slot is
642 * unrecoverable.
643 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100644 * This function also erases any metadata such as policies and frees all
645 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200646 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100647 * If the key is currently in use in a multipart operation,
648 * the multipart operation is aborted.
649 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100650 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100651 *
Gilles Peskine28538492018-07-11 17:34:00 +0200652 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200653 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200654 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200655 * The slot holds content and cannot be erased because it is
656 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100657 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200658 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200659 * There was an failure in communication with the cryptoprocessor.
660 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200661 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200662 * The storage is corrupted. Implementations shall make a best effort
663 * to erase key material even in this stage, however applications
664 * should be aware that it may be impossible to guarantee that the
665 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200666 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200667 * An unexpected condition which is not a storage corruption or
668 * a communication failure occurred. The cryptoprocessor may have
669 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300670 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300671 * The library has not been previously initialized by psa_crypto_init().
672 * It is implementation-dependent whether a failure to initialize
673 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100675psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676
677/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678 * \brief Export a key in binary format.
679 *
680 * The output of this function can be passed to psa_import_key() to
681 * create an equivalent object.
682 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100683 * If the implementation of psa_import_key() supports other formats
684 * beyond the format specified here, the output from psa_export_key()
685 * must use the representation specified here, not the original
686 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100687 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100688 * For standard key types, the output format is as follows:
689 *
690 * - For symmetric keys (including MAC keys), the format is the
691 * raw bytes of the key.
692 * - For DES, the key data consists of 8 bytes. The parity bits must be
693 * correct.
694 * - For Triple-DES, the format is the concatenation of the
695 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100696 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200697 * is the non-encrypted DER encoding of the representation defined by
698 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
699 * ```
700 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200701 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200702 * modulus INTEGER, -- n
703 * publicExponent INTEGER, -- e
704 * privateExponent INTEGER, -- d
705 * prime1 INTEGER, -- p
706 * prime2 INTEGER, -- q
707 * exponent1 INTEGER, -- d mod (p-1)
708 * exponent2 INTEGER, -- d mod (q-1)
709 * coefficient INTEGER, -- (inverse of q) mod p
710 * }
711 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000712 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
713 * representation of the private key `x` as a big-endian byte string. The
714 * length of the byte string is the private key size in bytes (leading zeroes
715 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200716 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100717 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100718 * a representation of the private value as a `ceiling(m/8)`-byte string
719 * where `m` is the bit size associated with the curve, i.e. the bit size
720 * of the order of the curve's coordinate field. This byte string is
721 * in little-endian order for Montgomery curves (curve types
722 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
723 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
724 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100725 * This is the content of the `privateKey` field of the `ECPrivateKey`
726 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000727 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
728 * format is the representation of the private key `x` as a big-endian byte
729 * string. The length of the byte string is the private key size in bytes
730 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200731 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
732 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100733 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200734 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
735 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100736 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200737 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200738 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200739 * \param[out] data_length On success, the number of bytes
740 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100741 *
Gilles Peskine28538492018-07-11 17:34:00 +0200742 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100743 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200744 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200745 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200746 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100747 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200748 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
749 * The size of the \p data buffer is too small. You can determine a
750 * sufficient buffer size by calling
751 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
752 * where \c type is the key type
753 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200754 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
755 * \retval #PSA_ERROR_HARDWARE_FAILURE
756 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300757 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300758 * The library has not been previously initialized by psa_crypto_init().
759 * It is implementation-dependent whether a failure to initialize
760 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100761 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100762psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100763 uint8_t *data,
764 size_t data_size,
765 size_t *data_length);
766
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100767/**
768 * \brief Export a public key or the public part of a key pair in binary format.
769 *
770 * The output of this function can be passed to psa_import_key() to
771 * create an object that is equivalent to the public key.
772 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000773 * This specification supports a single format for each key type.
774 * Implementations may support other formats as long as the standard
775 * format is supported. Implementations that support other formats
776 * should ensure that the formats are clearly unambiguous so as to
777 * minimize the risk that an invalid input is accidentally interpreted
778 * according to a different format.
779 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000780 * For standard key types, the output format is as follows:
781 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
782 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
783 * ```
784 * RSAPublicKey ::= SEQUENCE {
785 * modulus INTEGER, -- n
786 * publicExponent INTEGER } -- e
787 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000788 * - For elliptic curve public keys (key types for which
789 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
790 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
791 * Let `m` be the bit size associated with the curve, i.e. the bit size of
792 * `q` for a curve over `F_q`. The representation consists of:
793 * - The byte 0x04;
794 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
795 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000796 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
797 * representation of the public key `y = g^x mod p` as a big-endian byte
798 * string. The length of the byte string is the length of the base prime `p`
799 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000800 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
801 * the format is the representation of the public key `y = g^x mod p` as a
802 * big-endian byte string. The length of the byte string is the length of the
803 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100804 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200805 * Exporting a public key object or the public part of a key pair is
806 * always permitted, regardless of the key's usage flags.
807 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100808 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200809 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200810 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200811 * \param[out] data_length On success, the number of bytes
812 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100813 *
Gilles Peskine28538492018-07-11 17:34:00 +0200814 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100815 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200816 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200817 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200818 * The key is neither a public key nor a key pair.
819 * \retval #PSA_ERROR_NOT_SUPPORTED
820 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
821 * The size of the \p data buffer is too small. You can determine a
822 * sufficient buffer size by calling
823 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
824 * where \c type is the key type
825 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200826 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
827 * \retval #PSA_ERROR_HARDWARE_FAILURE
828 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300829 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300830 * The library has not been previously initialized by psa_crypto_init().
831 * It is implementation-dependent whether a failure to initialize
832 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100833 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100834psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100835 uint8_t *data,
836 size_t data_size,
837 size_t *data_length);
838
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100839/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100840 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100841 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000842 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100843 * This function is primarily useful to copy a key from one location
844 * to another, since it populates a key using the material from
845 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200846 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100847 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100848 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100849 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100850 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200851 * The policy on the source key must have the usage flag
852 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200853 * This flag is sufficient to permit the copy if the key has the lifetime
854 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
855 * Some secure elements do not provide a way to copy a key without
856 * making it extractable from the secure element. If a key is located
857 * in such a secure element, then the key must have both usage flags
858 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
859 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200860 *
Gilles Peskine20628592019-04-19 19:29:50 +0200861 * The resulting key may only be used in a way that conforms to
862 * both the policy of the original key and the policy specified in
863 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100864 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200865 * usage flags on the source policy and the usage flags in \p attributes.
866 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100867 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200868 * - If either of the policies allows an algorithm and the other policy
869 * allows a wildcard-based algorithm policy that includes this algorithm,
870 * the resulting key allows the same algorithm.
871 * - If the policies do not allow any algorithm in common, this function
872 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200873 *
Gilles Peskine20628592019-04-19 19:29:50 +0200874 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100875 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200876 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100877 * \param source_handle The key to copy. It must be a handle to an
878 * occupied slot.
Gilles Peskine20628592019-04-19 19:29:50 +0200879 * \param[in] attributes The attributes for the new key.
880 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200881 * - The key type and size may be 0. If either is
882 * nonzero, it must match the corresponding
883 * attribute of the source key.
884 * - If \p attributes contains domain parameters,
885 * they must match the domain parameters of
886 * the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200887 * - The key location (the lifetime and, for
888 * persistent keys, the key identifier) is
889 * used directly.
890 * - The policy constraints (usage flags and
891 * algorithm policy) are combined from
892 * the source key and \p attributes so that
893 * both sets of restrictions apply, as
894 * described in the documentation of this function.
895 * \param[out] target_handle On success, a handle to the newly created key.
896 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200897 *
898 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100899 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200900 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200901 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200902 * This is an attempt to create a persistent key, and there is
903 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200904 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200905 * The lifetime or identifier in \p attributes are invalid.
906 * \retval #PSA_ERROR_INVALID_ARGUMENT
907 * The policy constraints on the source and specified in
908 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200909 * \retval #PSA_ERROR_INVALID_ARGUMENT
910 * \p attributes specifies a key type, domain parameters or key size
911 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100912 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200913 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
914 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100915 * The source key is not exportable and its lifetime does not
916 * allow copying it to the target's lifetime.
917 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
918 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200919 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
920 * \retval #PSA_ERROR_HARDWARE_FAILURE
921 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100922 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100923psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200924 const psa_key_attributes_t *attributes,
925 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100926
927/**@}*/
928
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100929/** \defgroup hash Message digests
930 * @{
931 */
932
Gilles Peskine69647a42019-01-14 20:18:12 +0100933/** Calculate the hash (digest) of a message.
934 *
935 * \note To verify the hash of a message against an
936 * expected value, use psa_hash_compare() instead.
937 *
938 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
939 * such that #PSA_ALG_IS_HASH(\p alg) is true).
940 * \param[in] input Buffer containing the message to hash.
941 * \param input_length Size of the \p input buffer in bytes.
942 * \param[out] hash Buffer where the hash is to be written.
943 * \param hash_size Size of the \p hash buffer in bytes.
944 * \param[out] hash_length On success, the number of bytes
945 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100946 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100947 *
948 * \retval #PSA_SUCCESS
949 * Success.
950 * \retval #PSA_ERROR_NOT_SUPPORTED
951 * \p alg is not supported or is not a hash algorithm.
952 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
953 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
954 * \retval #PSA_ERROR_HARDWARE_FAILURE
955 * \retval #PSA_ERROR_TAMPERING_DETECTED
956 */
957psa_status_t psa_hash_compute(psa_algorithm_t alg,
958 const uint8_t *input,
959 size_t input_length,
960 uint8_t *hash,
961 size_t hash_size,
962 size_t *hash_length);
963
964/** Calculate the hash (digest) of a message and compare it with a
965 * reference value.
966 *
967 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
968 * such that #PSA_ALG_IS_HASH(\p alg) is true).
969 * \param[in] input Buffer containing the message to hash.
970 * \param input_length Size of the \p input buffer in bytes.
971 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100972 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100973 *
974 * \retval #PSA_SUCCESS
975 * The expected hash is identical to the actual hash of the input.
976 * \retval #PSA_ERROR_INVALID_SIGNATURE
977 * The hash of the message was calculated successfully, but it
978 * differs from the expected hash.
979 * \retval #PSA_ERROR_NOT_SUPPORTED
980 * \p alg is not supported or is not a hash algorithm.
981 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
982 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
983 * \retval #PSA_ERROR_HARDWARE_FAILURE
984 * \retval #PSA_ERROR_TAMPERING_DETECTED
985 */
986psa_status_t psa_hash_compare(psa_algorithm_t alg,
987 const uint8_t *input,
988 size_t input_length,
989 const uint8_t *hash,
990 const size_t hash_length);
991
Gilles Peskine308b91d2018-02-08 09:47:44 +0100992/** The type of the state data structure for multipart hash operations.
993 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000994 * Before calling any function on a hash operation object, the application must
995 * initialize it by any of the following means:
996 * - Set the structure to all-bits-zero, for example:
997 * \code
998 * psa_hash_operation_t operation;
999 * memset(&operation, 0, sizeof(operation));
1000 * \endcode
1001 * - Initialize the structure to logical zero values, for example:
1002 * \code
1003 * psa_hash_operation_t operation = {0};
1004 * \endcode
1005 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
1006 * for example:
1007 * \code
1008 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
1009 * \endcode
1010 * - Assign the result of the function psa_hash_operation_init()
1011 * to the structure, for example:
1012 * \code
1013 * psa_hash_operation_t operation;
1014 * operation = psa_hash_operation_init();
1015 * \endcode
1016 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001017 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001018 * make any assumptions about the content of this structure except
1019 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001020typedef struct psa_hash_operation_s psa_hash_operation_t;
1021
Jaeden Amero6a25b412019-01-04 11:47:44 +00001022/** \def PSA_HASH_OPERATION_INIT
1023 *
1024 * This macro returns a suitable initializer for a hash operation object
1025 * of type #psa_hash_operation_t.
1026 */
1027#ifdef __DOXYGEN_ONLY__
1028/* This is an example definition for documentation purposes.
1029 * Implementations should define a suitable value in `crypto_struct.h`.
1030 */
1031#define PSA_HASH_OPERATION_INIT {0}
1032#endif
1033
1034/** Return an initial value for a hash operation object.
1035 */
1036static psa_hash_operation_t psa_hash_operation_init(void);
1037
Gilles Peskinef45adda2019-01-14 18:29:18 +01001038/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001039 *
1040 * The sequence of operations to calculate a hash (message digest)
1041 * is as follows:
1042 * -# Allocate an operation object which will be passed to all the functions
1043 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +00001044 * -# Initialize the operation object with one of the methods described in the
1045 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001046 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001047 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001048 * of the message each time. The hash that is calculated is the hash
1049 * of the concatenation of these messages in order.
1050 * -# To calculate the hash, call psa_hash_finish().
1051 * To compare the hash with an expected value, call psa_hash_verify().
1052 *
1053 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +00001054 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001055 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001056 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001057 * eventually terminate the operation. The following events terminate an
1058 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001059 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001060 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001061 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001062 * \param[in,out] operation The operation object to set up. It must have
1063 * been initialized as per the documentation for
1064 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001065 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1066 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001067 *
Gilles Peskine28538492018-07-11 17:34:00 +02001068 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001069 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001070 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001071 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001072 * \retval #PSA_ERROR_BAD_STATE
1073 * The operation state is not valid (already set up and not
1074 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001075 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1076 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1077 * \retval #PSA_ERROR_HARDWARE_FAILURE
1078 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001079 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001080psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001081 psa_algorithm_t alg);
1082
Gilles Peskine308b91d2018-02-08 09:47:44 +01001083/** Add a message fragment to a multipart hash operation.
1084 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001085 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001086 *
1087 * If this function returns an error status, the operation becomes inactive.
1088 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001089 * \param[in,out] operation Active hash operation.
1090 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001091 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001092 *
Gilles Peskine28538492018-07-11 17:34:00 +02001093 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001094 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001095 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001096 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001097 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1098 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1099 * \retval #PSA_ERROR_HARDWARE_FAILURE
1100 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001101 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001102psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1103 const uint8_t *input,
1104 size_t input_length);
1105
Gilles Peskine308b91d2018-02-08 09:47:44 +01001106/** Finish the calculation of the hash of a message.
1107 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001108 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001109 * This function calculates the hash of the message formed by concatenating
1110 * the inputs passed to preceding calls to psa_hash_update().
1111 *
1112 * When this function returns, the operation becomes inactive.
1113 *
1114 * \warning Applications should not call this function if they expect
1115 * a specific value for the hash. Call psa_hash_verify() instead.
1116 * Beware that comparing integrity or authenticity data such as
1117 * hash values with a function such as \c memcmp is risky
1118 * because the time taken by the comparison may leak information
1119 * about the hashed data which could allow an attacker to guess
1120 * a valid hash and thereby bypass security controls.
1121 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001122 * \param[in,out] operation Active hash operation.
1123 * \param[out] hash Buffer where the hash is to be written.
1124 * \param hash_size Size of the \p hash buffer in bytes.
1125 * \param[out] hash_length On success, the number of bytes
1126 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001127 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001128 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001129 *
Gilles Peskine28538492018-07-11 17:34:00 +02001130 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001131 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001132 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001133 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001134 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001135 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001136 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001137 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001138 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1139 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1140 * \retval #PSA_ERROR_HARDWARE_FAILURE
1141 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001142 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001143psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1144 uint8_t *hash,
1145 size_t hash_size,
1146 size_t *hash_length);
1147
Gilles Peskine308b91d2018-02-08 09:47:44 +01001148/** Finish the calculation of the hash of a message and compare it with
1149 * an expected value.
1150 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001151 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001152 * This function calculates the hash of the message formed by concatenating
1153 * the inputs passed to preceding calls to psa_hash_update(). It then
1154 * compares the calculated hash with the expected hash passed as a
1155 * parameter to this function.
1156 *
1157 * When this function returns, the operation becomes inactive.
1158 *
Gilles Peskine19067982018-03-20 17:54:53 +01001159 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001160 * comparison between the actual hash and the expected hash is performed
1161 * in constant time.
1162 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001163 * \param[in,out] operation Active hash operation.
1164 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001165 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001166 *
Gilles Peskine28538492018-07-11 17:34:00 +02001167 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001168 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001169 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001170 * The hash of the message was calculated successfully, but it
1171 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001172 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001173 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001174 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1175 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1176 * \retval #PSA_ERROR_HARDWARE_FAILURE
1177 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001178 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001179psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1180 const uint8_t *hash,
1181 size_t hash_length);
1182
Gilles Peskine308b91d2018-02-08 09:47:44 +01001183/** Abort a hash operation.
1184 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001185 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001186 * \p operation structure itself. Once aborted, the operation object
1187 * can be reused for another operation by calling
1188 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001189 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001190 * You may call this function any time after the operation object has
1191 * been initialized by any of the following methods:
1192 * - A call to psa_hash_setup(), whether it succeeds or not.
1193 * - Initializing the \c struct to all-bits-zero.
1194 * - Initializing the \c struct to logical zeros, e.g.
1195 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001196 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001197 * In particular, calling psa_hash_abort() after the operation has been
1198 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1199 * psa_hash_verify() is safe and has no effect.
1200 *
1201 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001202 *
Gilles Peskine28538492018-07-11 17:34:00 +02001203 * \retval #PSA_SUCCESS
1204 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001205 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001206 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1207 * \retval #PSA_ERROR_HARDWARE_FAILURE
1208 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001209 */
1210psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001211
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001212/** Clone a hash operation.
1213 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001214 * This function copies the state of an ongoing hash operation to
1215 * a new operation object. In other words, this function is equivalent
1216 * to calling psa_hash_setup() on \p target_operation with the same
1217 * algorithm that \p source_operation was set up for, then
1218 * psa_hash_update() on \p target_operation with the same input that
1219 * that was passed to \p source_operation. After this function returns, the
1220 * two objects are independent, i.e. subsequent calls involving one of
1221 * the objects do not affect the other object.
1222 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001223 * \param[in] source_operation The active hash operation to clone.
1224 * \param[in,out] target_operation The operation object to set up.
1225 * It must be initialized but not active.
1226 *
1227 * \retval #PSA_SUCCESS
1228 * \retval #PSA_ERROR_BAD_STATE
1229 * \p source_operation is not an active hash operation.
1230 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001231 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001232 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1233 * \retval #PSA_ERROR_HARDWARE_FAILURE
1234 * \retval #PSA_ERROR_TAMPERING_DETECTED
1235 */
1236psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1237 psa_hash_operation_t *target_operation);
1238
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001239/**@}*/
1240
Gilles Peskine8c9def32018-02-08 10:02:12 +01001241/** \defgroup MAC Message authentication codes
1242 * @{
1243 */
1244
Gilles Peskine69647a42019-01-14 20:18:12 +01001245/** Calculate the MAC (message authentication code) of a message.
1246 *
1247 * \note To verify the MAC of a message against an
1248 * expected value, use psa_mac_verify() instead.
1249 * Beware that comparing integrity or authenticity data such as
1250 * MAC values with a function such as \c memcmp is risky
1251 * because the time taken by the comparison may leak information
1252 * about the MAC value which could allow an attacker to guess
1253 * a valid MAC and thereby bypass security controls.
1254 *
1255 * \param handle Handle to the key to use for the operation.
1256 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001257 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001258 * \param[in] input Buffer containing the input message.
1259 * \param input_length Size of the \p input buffer in bytes.
1260 * \param[out] mac Buffer where the MAC value is to be written.
1261 * \param mac_size Size of the \p mac buffer in bytes.
1262 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001263 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001264 *
1265 * \retval #PSA_SUCCESS
1266 * Success.
1267 * \retval #PSA_ERROR_INVALID_HANDLE
1268 * \retval #PSA_ERROR_EMPTY_SLOT
1269 * \retval #PSA_ERROR_NOT_PERMITTED
1270 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001271 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001272 * \retval #PSA_ERROR_NOT_SUPPORTED
1273 * \p alg is not supported or is not a MAC algorithm.
1274 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1275 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1276 * \retval #PSA_ERROR_HARDWARE_FAILURE
1277 * \retval #PSA_ERROR_TAMPERING_DETECTED
1278 * \retval #PSA_ERROR_BAD_STATE
1279 * The library has not been previously initialized by psa_crypto_init().
1280 * It is implementation-dependent whether a failure to initialize
1281 * results in this error code.
1282 */
1283psa_status_t psa_mac_compute(psa_key_handle_t handle,
1284 psa_algorithm_t alg,
1285 const uint8_t *input,
1286 size_t input_length,
1287 uint8_t *mac,
1288 size_t mac_size,
1289 size_t *mac_length);
1290
1291/** Calculate the MAC of a message and compare it with a reference value.
1292 *
1293 * \param handle Handle to the key to use for the operation.
1294 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001295 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001296 * \param[in] input Buffer containing the input message.
1297 * \param input_length Size of the \p input buffer in bytes.
1298 * \param[out] mac Buffer containing the expected MAC value.
1299 * \param mac_length Size of the \p mac buffer in bytes.
1300 *
1301 * \retval #PSA_SUCCESS
1302 * The expected MAC is identical to the actual MAC of the input.
1303 * \retval #PSA_ERROR_INVALID_SIGNATURE
1304 * The MAC of the message was calculated successfully, but it
1305 * differs from the expected value.
1306 * \retval #PSA_ERROR_INVALID_HANDLE
1307 * \retval #PSA_ERROR_EMPTY_SLOT
1308 * \retval #PSA_ERROR_NOT_PERMITTED
1309 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001310 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001311 * \retval #PSA_ERROR_NOT_SUPPORTED
1312 * \p alg is not supported or is not a MAC algorithm.
1313 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1314 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1315 * \retval #PSA_ERROR_HARDWARE_FAILURE
1316 * \retval #PSA_ERROR_TAMPERING_DETECTED
1317 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001318psa_status_t psa_mac_verify(psa_key_handle_t handle,
1319 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001320 const uint8_t *input,
1321 size_t input_length,
1322 const uint8_t *mac,
1323 const size_t mac_length);
1324
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001325/** The type of the state data structure for multipart MAC operations.
1326 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001327 * Before calling any function on a MAC operation object, the application must
1328 * initialize it by any of the following means:
1329 * - Set the structure to all-bits-zero, for example:
1330 * \code
1331 * psa_mac_operation_t operation;
1332 * memset(&operation, 0, sizeof(operation));
1333 * \endcode
1334 * - Initialize the structure to logical zero values, for example:
1335 * \code
1336 * psa_mac_operation_t operation = {0};
1337 * \endcode
1338 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1339 * for example:
1340 * \code
1341 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1342 * \endcode
1343 * - Assign the result of the function psa_mac_operation_init()
1344 * to the structure, for example:
1345 * \code
1346 * psa_mac_operation_t operation;
1347 * operation = psa_mac_operation_init();
1348 * \endcode
1349 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001350 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001351 * make any assumptions about the content of this structure except
1352 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001353typedef struct psa_mac_operation_s psa_mac_operation_t;
1354
Jaeden Amero769ce272019-01-04 11:48:03 +00001355/** \def PSA_MAC_OPERATION_INIT
1356 *
1357 * This macro returns a suitable initializer for a MAC operation object of type
1358 * #psa_mac_operation_t.
1359 */
1360#ifdef __DOXYGEN_ONLY__
1361/* This is an example definition for documentation purposes.
1362 * Implementations should define a suitable value in `crypto_struct.h`.
1363 */
1364#define PSA_MAC_OPERATION_INIT {0}
1365#endif
1366
1367/** Return an initial value for a MAC operation object.
1368 */
1369static psa_mac_operation_t psa_mac_operation_init(void);
1370
Gilles Peskinef45adda2019-01-14 18:29:18 +01001371/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001372 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001373 * This function sets up the calculation of the MAC
1374 * (message authentication code) of a byte string.
1375 * To verify the MAC of a message against an
1376 * expected value, use psa_mac_verify_setup() instead.
1377 *
1378 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001379 * -# Allocate an operation object which will be passed to all the functions
1380 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001381 * -# Initialize the operation object with one of the methods described in the
1382 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001383 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001384 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1385 * of the message each time. The MAC that is calculated is the MAC
1386 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001387 * -# At the end of the message, call psa_mac_sign_finish() to finish
1388 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001389 *
1390 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001391 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001392 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001393 * After a successful call to psa_mac_sign_setup(), the application must
1394 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001395 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001396 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001397 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001398 * \param[in,out] operation The operation object to set up. It must have
1399 * been initialized as per the documentation for
1400 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001401 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001402 * It must remain valid until the operation
1403 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001404 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001405 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001406 *
Gilles Peskine28538492018-07-11 17:34:00 +02001407 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001408 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001409 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001410 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001411 * \retval #PSA_ERROR_NOT_PERMITTED
1412 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001413 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001414 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001415 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001416 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1417 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1418 * \retval #PSA_ERROR_HARDWARE_FAILURE
1419 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001420 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001421 * The operation state is not valid (already set up and not
1422 * subsequently completed).
1423 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001424 * The library has not been previously initialized by psa_crypto_init().
1425 * It is implementation-dependent whether a failure to initialize
1426 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001427 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001428psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001429 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001430 psa_algorithm_t alg);
1431
Gilles Peskinef45adda2019-01-14 18:29:18 +01001432/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001433 *
1434 * This function sets up the verification of the MAC
1435 * (message authentication code) of a byte string against an expected value.
1436 *
1437 * The sequence of operations to verify a MAC is as follows:
1438 * -# Allocate an operation object which will be passed to all the functions
1439 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001440 * -# Initialize the operation object with one of the methods described in the
1441 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001442 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001443 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1444 * of the message each time. The MAC that is calculated is the MAC
1445 * of the concatenation of these messages in order.
1446 * -# At the end of the message, call psa_mac_verify_finish() to finish
1447 * calculating the actual MAC of the message and verify it against
1448 * the expected value.
1449 *
1450 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001451 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001452 *
1453 * After a successful call to psa_mac_verify_setup(), the application must
1454 * eventually terminate the operation through one of the following methods:
1455 * - A failed call to psa_mac_update().
1456 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1457 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001458 * \param[in,out] operation The operation object to set up. It must have
1459 * been initialized as per the documentation for
1460 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001461 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001462 * It must remain valid until the operation
1463 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001464 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1465 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001466 *
Gilles Peskine28538492018-07-11 17:34:00 +02001467 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001468 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001469 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001470 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001471 * \retval #PSA_ERROR_NOT_PERMITTED
1472 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001473 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001474 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001475 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001476 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1477 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1478 * \retval #PSA_ERROR_HARDWARE_FAILURE
1479 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001480 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001481 * The operation state is not valid (already set up and not
1482 * subsequently completed).
1483 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001484 * The library has not been previously initialized by psa_crypto_init().
1485 * It is implementation-dependent whether a failure to initialize
1486 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001487 */
1488psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001489 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001490 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001491
Gilles Peskinedcd14942018-07-12 00:30:52 +02001492/** Add a message fragment to a multipart MAC operation.
1493 *
1494 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1495 * before calling this function.
1496 *
1497 * If this function returns an error status, the operation becomes inactive.
1498 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001499 * \param[in,out] operation Active MAC operation.
1500 * \param[in] input Buffer containing the message fragment to add to
1501 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001502 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001503 *
1504 * \retval #PSA_SUCCESS
1505 * Success.
1506 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001507 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001508 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1509 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1510 * \retval #PSA_ERROR_HARDWARE_FAILURE
1511 * \retval #PSA_ERROR_TAMPERING_DETECTED
1512 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001513psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1514 const uint8_t *input,
1515 size_t input_length);
1516
Gilles Peskinedcd14942018-07-12 00:30:52 +02001517/** Finish the calculation of the MAC of a message.
1518 *
1519 * The application must call psa_mac_sign_setup() before calling this function.
1520 * This function calculates the MAC of the message formed by concatenating
1521 * the inputs passed to preceding calls to psa_mac_update().
1522 *
1523 * When this function returns, the operation becomes inactive.
1524 *
1525 * \warning Applications should not call this function if they expect
1526 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1527 * Beware that comparing integrity or authenticity data such as
1528 * MAC values with a function such as \c memcmp is risky
1529 * because the time taken by the comparison may leak information
1530 * about the MAC value which could allow an attacker to guess
1531 * a valid MAC and thereby bypass security controls.
1532 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001533 * \param[in,out] operation Active MAC operation.
1534 * \param[out] mac Buffer where the MAC value is to be written.
1535 * \param mac_size Size of the \p mac buffer in bytes.
1536 * \param[out] mac_length On success, the number of bytes
1537 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001538 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001539 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001540 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001541 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001542 *
1543 * \retval #PSA_SUCCESS
1544 * Success.
1545 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001546 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001547 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001548 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001549 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1550 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1551 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1552 * \retval #PSA_ERROR_HARDWARE_FAILURE
1553 * \retval #PSA_ERROR_TAMPERING_DETECTED
1554 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001555psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1556 uint8_t *mac,
1557 size_t mac_size,
1558 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001559
Gilles Peskinedcd14942018-07-12 00:30:52 +02001560/** Finish the calculation of the MAC of a message and compare it with
1561 * an expected value.
1562 *
1563 * The application must call psa_mac_verify_setup() before calling this function.
1564 * This function calculates the MAC of the message formed by concatenating
1565 * the inputs passed to preceding calls to psa_mac_update(). It then
1566 * compares the calculated MAC with the expected MAC passed as a
1567 * parameter to this function.
1568 *
1569 * When this function returns, the operation becomes inactive.
1570 *
1571 * \note Implementations shall make the best effort to ensure that the
1572 * comparison between the actual MAC and the expected MAC is performed
1573 * in constant time.
1574 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001575 * \param[in,out] operation Active MAC operation.
1576 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001577 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001578 *
1579 * \retval #PSA_SUCCESS
1580 * The expected MAC is identical to the actual MAC of the message.
1581 * \retval #PSA_ERROR_INVALID_SIGNATURE
1582 * The MAC of the message was calculated successfully, but it
1583 * differs from the expected MAC.
1584 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001585 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001586 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1587 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1588 * \retval #PSA_ERROR_HARDWARE_FAILURE
1589 * \retval #PSA_ERROR_TAMPERING_DETECTED
1590 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001591psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1592 const uint8_t *mac,
1593 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001594
Gilles Peskinedcd14942018-07-12 00:30:52 +02001595/** Abort a MAC operation.
1596 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001597 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001598 * \p operation structure itself. Once aborted, the operation object
1599 * can be reused for another operation by calling
1600 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001601 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001602 * You may call this function any time after the operation object has
1603 * been initialized by any of the following methods:
1604 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1605 * it succeeds or not.
1606 * - Initializing the \c struct to all-bits-zero.
1607 * - Initializing the \c struct to logical zeros, e.g.
1608 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001609 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001610 * In particular, calling psa_mac_abort() after the operation has been
1611 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1612 * psa_mac_verify_finish() is safe and has no effect.
1613 *
1614 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001615 *
1616 * \retval #PSA_SUCCESS
1617 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001618 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001619 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1620 * \retval #PSA_ERROR_HARDWARE_FAILURE
1621 * \retval #PSA_ERROR_TAMPERING_DETECTED
1622 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001623psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1624
1625/**@}*/
1626
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001627/** \defgroup cipher Symmetric ciphers
1628 * @{
1629 */
1630
Gilles Peskine69647a42019-01-14 20:18:12 +01001631/** Encrypt a message using a symmetric cipher.
1632 *
1633 * This function encrypts a message with a random IV (initialization
1634 * vector).
1635 *
1636 * \param handle Handle to the key to use for the operation.
1637 * It must remain valid until the operation
1638 * terminates.
1639 * \param alg The cipher algorithm to compute
1640 * (\c PSA_ALG_XXX value such that
1641 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1642 * \param[in] input Buffer containing the message to encrypt.
1643 * \param input_length Size of the \p input buffer in bytes.
1644 * \param[out] output Buffer where the output is to be written.
1645 * The output contains the IV followed by
1646 * the ciphertext proper.
1647 * \param output_size Size of the \p output buffer in bytes.
1648 * \param[out] output_length On success, the number of bytes
1649 * that make up the output.
1650 *
1651 * \retval #PSA_SUCCESS
1652 * Success.
1653 * \retval #PSA_ERROR_INVALID_HANDLE
1654 * \retval #PSA_ERROR_EMPTY_SLOT
1655 * \retval #PSA_ERROR_NOT_PERMITTED
1656 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001657 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001658 * \retval #PSA_ERROR_NOT_SUPPORTED
1659 * \p alg is not supported or is not a cipher algorithm.
1660 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1661 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1662 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1663 * \retval #PSA_ERROR_HARDWARE_FAILURE
1664 * \retval #PSA_ERROR_TAMPERING_DETECTED
1665 */
1666psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1667 psa_algorithm_t alg,
1668 const uint8_t *input,
1669 size_t input_length,
1670 uint8_t *output,
1671 size_t output_size,
1672 size_t *output_length);
1673
1674/** Decrypt a message using a symmetric cipher.
1675 *
1676 * This function decrypts a message encrypted with a symmetric cipher.
1677 *
1678 * \param handle Handle to the key to use for the operation.
1679 * It must remain valid until the operation
1680 * terminates.
1681 * \param alg The cipher algorithm to compute
1682 * (\c PSA_ALG_XXX value such that
1683 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1684 * \param[in] input Buffer containing the message to decrypt.
1685 * This consists of the IV followed by the
1686 * ciphertext proper.
1687 * \param input_length Size of the \p input buffer in bytes.
1688 * \param[out] output Buffer where the plaintext is to be written.
1689 * \param output_size Size of the \p output buffer in bytes.
1690 * \param[out] output_length On success, the number of bytes
1691 * that make up the output.
1692 *
1693 * \retval #PSA_SUCCESS
1694 * Success.
1695 * \retval #PSA_ERROR_INVALID_HANDLE
1696 * \retval #PSA_ERROR_EMPTY_SLOT
1697 * \retval #PSA_ERROR_NOT_PERMITTED
1698 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001699 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001700 * \retval #PSA_ERROR_NOT_SUPPORTED
1701 * \p alg is not supported or is not a cipher algorithm.
1702 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1703 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1704 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1705 * \retval #PSA_ERROR_HARDWARE_FAILURE
1706 * \retval #PSA_ERROR_TAMPERING_DETECTED
1707 */
1708psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1709 psa_algorithm_t alg,
1710 const uint8_t *input,
1711 size_t input_length,
1712 uint8_t *output,
1713 size_t output_size,
1714 size_t *output_length);
1715
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001716/** The type of the state data structure for multipart cipher operations.
1717 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001718 * Before calling any function on a cipher operation object, the application
1719 * must initialize it by any of the following means:
1720 * - Set the structure to all-bits-zero, for example:
1721 * \code
1722 * psa_cipher_operation_t operation;
1723 * memset(&operation, 0, sizeof(operation));
1724 * \endcode
1725 * - Initialize the structure to logical zero values, for example:
1726 * \code
1727 * psa_cipher_operation_t operation = {0};
1728 * \endcode
1729 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1730 * for example:
1731 * \code
1732 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1733 * \endcode
1734 * - Assign the result of the function psa_cipher_operation_init()
1735 * to the structure, for example:
1736 * \code
1737 * psa_cipher_operation_t operation;
1738 * operation = psa_cipher_operation_init();
1739 * \endcode
1740 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001741 * This is an implementation-defined \c struct. Applications should not
1742 * make any assumptions about the content of this structure except
1743 * as directed by the documentation of a specific implementation. */
1744typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1745
Jaeden Amero5bae2272019-01-04 11:48:27 +00001746/** \def PSA_CIPHER_OPERATION_INIT
1747 *
1748 * This macro returns a suitable initializer for a cipher operation object of
1749 * type #psa_cipher_operation_t.
1750 */
1751#ifdef __DOXYGEN_ONLY__
1752/* This is an example definition for documentation purposes.
1753 * Implementations should define a suitable value in `crypto_struct.h`.
1754 */
1755#define PSA_CIPHER_OPERATION_INIT {0}
1756#endif
1757
1758/** Return an initial value for a cipher operation object.
1759 */
1760static psa_cipher_operation_t psa_cipher_operation_init(void);
1761
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001762/** Set the key for a multipart symmetric encryption operation.
1763 *
1764 * The sequence of operations to encrypt a message with a symmetric cipher
1765 * is as follows:
1766 * -# Allocate an operation object which will be passed to all the functions
1767 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001768 * -# Initialize the operation object with one of the methods described in the
1769 * documentation for #psa_cipher_operation_t, e.g.
1770 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001771 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001772 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001773 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001774 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001775 * requires a specific IV value.
1776 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1777 * of the message each time.
1778 * -# Call psa_cipher_finish().
1779 *
1780 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001781 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001782 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001783 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001784 * eventually terminate the operation. The following events terminate an
1785 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001786 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001787 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001788 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001789 * \param[in,out] operation The operation object to set up. It must have
1790 * been initialized as per the documentation for
1791 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001792 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001793 * It must remain valid until the operation
1794 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001795 * \param alg The cipher algorithm to compute
1796 * (\c PSA_ALG_XXX value such that
1797 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001798 *
Gilles Peskine28538492018-07-11 17:34:00 +02001799 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001800 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001801 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001802 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001803 * \retval #PSA_ERROR_NOT_PERMITTED
1804 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001805 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001806 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001807 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001808 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1809 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1810 * \retval #PSA_ERROR_HARDWARE_FAILURE
1811 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001812 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001813 * The operation state is not valid (already set up and not
1814 * subsequently completed).
1815 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001816 * The library has not been previously initialized by psa_crypto_init().
1817 * It is implementation-dependent whether a failure to initialize
1818 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001819 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001820psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001821 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001822 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001823
1824/** Set the key for a multipart symmetric decryption operation.
1825 *
1826 * The sequence of operations to decrypt a message with a symmetric cipher
1827 * is as follows:
1828 * -# Allocate an operation object which will be passed to all the functions
1829 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001830 * -# Initialize the operation object with one of the methods described in the
1831 * documentation for #psa_cipher_operation_t, e.g.
1832 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001833 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001834 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001835 * decryption. If the IV is prepended to the ciphertext, you can call
1836 * psa_cipher_update() on a buffer containing the IV followed by the
1837 * beginning of the message.
1838 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1839 * of the message each time.
1840 * -# Call psa_cipher_finish().
1841 *
1842 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001843 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001844 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001845 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001846 * eventually terminate the operation. The following events terminate an
1847 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001848 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001849 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001850 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001851 * \param[in,out] operation The operation object to set up. It must have
1852 * been initialized as per the documentation for
1853 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001854 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001855 * It must remain valid until the operation
1856 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001857 * \param alg The cipher algorithm to compute
1858 * (\c PSA_ALG_XXX value such that
1859 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001860 *
Gilles Peskine28538492018-07-11 17:34:00 +02001861 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001862 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001863 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001864 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001865 * \retval #PSA_ERROR_NOT_PERMITTED
1866 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001867 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001868 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001869 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001870 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1871 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1872 * \retval #PSA_ERROR_HARDWARE_FAILURE
1873 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001874 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001875 * The operation state is not valid (already set up and not
1876 * subsequently completed).
1877 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001878 * The library has not been previously initialized by psa_crypto_init().
1879 * It is implementation-dependent whether a failure to initialize
1880 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001881 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001882psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001883 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001884 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001885
Gilles Peskinedcd14942018-07-12 00:30:52 +02001886/** Generate an IV for a symmetric encryption operation.
1887 *
1888 * This function generates a random IV (initialization vector), nonce
1889 * or initial counter value for the encryption operation as appropriate
1890 * for the chosen algorithm, key type and key size.
1891 *
1892 * The application must call psa_cipher_encrypt_setup() before
1893 * calling this function.
1894 *
1895 * If this function returns an error status, the operation becomes inactive.
1896 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001897 * \param[in,out] operation Active cipher operation.
1898 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001899 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001900 * \param[out] iv_length On success, the number of bytes of the
1901 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001902 *
1903 * \retval #PSA_SUCCESS
1904 * Success.
1905 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001906 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001907 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001908 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001909 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1910 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1911 * \retval #PSA_ERROR_HARDWARE_FAILURE
1912 * \retval #PSA_ERROR_TAMPERING_DETECTED
1913 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001914psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1915 unsigned char *iv,
1916 size_t iv_size,
1917 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001918
Gilles Peskinedcd14942018-07-12 00:30:52 +02001919/** Set the IV for a symmetric encryption or decryption operation.
1920 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001921 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001922 * or initial counter value for the encryption or decryption operation.
1923 *
1924 * The application must call psa_cipher_encrypt_setup() before
1925 * calling this function.
1926 *
1927 * If this function returns an error status, the operation becomes inactive.
1928 *
1929 * \note When encrypting, applications should use psa_cipher_generate_iv()
1930 * instead of this function, unless implementing a protocol that requires
1931 * a non-random IV.
1932 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001933 * \param[in,out] operation Active cipher operation.
1934 * \param[in] iv Buffer containing the IV to use.
1935 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001936 *
1937 * \retval #PSA_SUCCESS
1938 * Success.
1939 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001940 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001941 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001942 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001943 * or the chosen algorithm does not use an IV.
1944 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1945 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1946 * \retval #PSA_ERROR_HARDWARE_FAILURE
1947 * \retval #PSA_ERROR_TAMPERING_DETECTED
1948 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001949psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1950 const unsigned char *iv,
1951 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001952
Gilles Peskinedcd14942018-07-12 00:30:52 +02001953/** Encrypt or decrypt a message fragment in an active cipher operation.
1954 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001955 * Before calling this function, you must:
1956 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1957 * The choice of setup function determines whether this function
1958 * encrypts or decrypts its input.
1959 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1960 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001961 *
1962 * If this function returns an error status, the operation becomes inactive.
1963 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001964 * \param[in,out] operation Active cipher operation.
1965 * \param[in] input Buffer containing the message fragment to
1966 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001967 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001968 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001969 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001970 * \param[out] output_length On success, the number of bytes
1971 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001972 *
1973 * \retval #PSA_SUCCESS
1974 * Success.
1975 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001976 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001977 * not set, or already completed).
1978 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1979 * The size of the \p output buffer is too small.
1980 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1981 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1982 * \retval #PSA_ERROR_HARDWARE_FAILURE
1983 * \retval #PSA_ERROR_TAMPERING_DETECTED
1984 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001985psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1986 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001987 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001988 unsigned char *output,
1989 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001990 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001991
Gilles Peskinedcd14942018-07-12 00:30:52 +02001992/** Finish encrypting or decrypting a message in a cipher operation.
1993 *
1994 * The application must call psa_cipher_encrypt_setup() or
1995 * psa_cipher_decrypt_setup() before calling this function. The choice
1996 * of setup function determines whether this function encrypts or
1997 * decrypts its input.
1998 *
1999 * This function finishes the encryption or decryption of the message
2000 * formed by concatenating the inputs passed to preceding calls to
2001 * psa_cipher_update().
2002 *
2003 * When this function returns, the operation becomes inactive.
2004 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002005 * \param[in,out] operation Active cipher operation.
2006 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002007 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002008 * \param[out] output_length On success, the number of bytes
2009 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002010 *
2011 * \retval #PSA_SUCCESS
2012 * Success.
2013 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002014 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02002015 * not set, or already completed).
2016 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2017 * The size of the \p output buffer is too small.
2018 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2019 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2020 * \retval #PSA_ERROR_HARDWARE_FAILURE
2021 * \retval #PSA_ERROR_TAMPERING_DETECTED
2022 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002023psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002024 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002025 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002026 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002027
Gilles Peskinedcd14942018-07-12 00:30:52 +02002028/** Abort a cipher operation.
2029 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002030 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002031 * \p operation structure itself. Once aborted, the operation object
2032 * can be reused for another operation by calling
2033 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002034 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002035 * You may call this function any time after the operation object has
2036 * been initialized by any of the following methods:
2037 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2038 * whether it succeeds or not.
2039 * - Initializing the \c struct to all-bits-zero.
2040 * - Initializing the \c struct to logical zeros, e.g.
2041 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002042 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002043 * In particular, calling psa_cipher_abort() after the operation has been
2044 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2045 * is safe and has no effect.
2046 *
2047 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002048 *
2049 * \retval #PSA_SUCCESS
2050 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002051 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002052 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2053 * \retval #PSA_ERROR_HARDWARE_FAILURE
2054 * \retval #PSA_ERROR_TAMPERING_DETECTED
2055 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002056psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2057
2058/**@}*/
2059
Gilles Peskine3b555712018-03-03 21:27:57 +01002060/** \defgroup aead Authenticated encryption with associated data (AEAD)
2061 * @{
2062 */
2063
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002064/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002065 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002066 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002067 * \param alg The AEAD algorithm to compute
2068 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002069 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002070 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002071 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002072 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002073 * but not encrypted.
2074 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002075 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002076 * encrypted.
2077 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002078 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002079 * encrypted data. The additional data is not
2080 * part of this output. For algorithms where the
2081 * encrypted data and the authentication tag
2082 * are defined as separate outputs, the
2083 * authentication tag is appended to the
2084 * encrypted data.
2085 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2086 * This must be at least
2087 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2088 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002089 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002090 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002091 *
Gilles Peskine28538492018-07-11 17:34:00 +02002092 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002093 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002094 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002095 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002096 * \retval #PSA_ERROR_NOT_PERMITTED
2097 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002098 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002099 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002100 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002101 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2102 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2103 * \retval #PSA_ERROR_HARDWARE_FAILURE
2104 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002105 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002106 * The library has not been previously initialized by psa_crypto_init().
2107 * It is implementation-dependent whether a failure to initialize
2108 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002109 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002110psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002111 psa_algorithm_t alg,
2112 const uint8_t *nonce,
2113 size_t nonce_length,
2114 const uint8_t *additional_data,
2115 size_t additional_data_length,
2116 const uint8_t *plaintext,
2117 size_t plaintext_length,
2118 uint8_t *ciphertext,
2119 size_t ciphertext_size,
2120 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002121
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002122/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002123 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002124 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002125 * \param alg The AEAD algorithm to compute
2126 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002127 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002128 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002129 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002130 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002131 * but not encrypted.
2132 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002133 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002134 * encrypted. For algorithms where the
2135 * encrypted data and the authentication tag
2136 * are defined as separate inputs, the buffer
2137 * must contain the encrypted data followed
2138 * by the authentication tag.
2139 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002140 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002141 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2142 * This must be at least
2143 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2144 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002145 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002146 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002147 *
Gilles Peskine28538492018-07-11 17:34:00 +02002148 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002149 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002150 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002151 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002152 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002153 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002154 * \retval #PSA_ERROR_NOT_PERMITTED
2155 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002156 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002157 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002158 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002159 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2160 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2161 * \retval #PSA_ERROR_HARDWARE_FAILURE
2162 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002163 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002164 * The library has not been previously initialized by psa_crypto_init().
2165 * It is implementation-dependent whether a failure to initialize
2166 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002167 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002168psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002169 psa_algorithm_t alg,
2170 const uint8_t *nonce,
2171 size_t nonce_length,
2172 const uint8_t *additional_data,
2173 size_t additional_data_length,
2174 const uint8_t *ciphertext,
2175 size_t ciphertext_length,
2176 uint8_t *plaintext,
2177 size_t plaintext_size,
2178 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002179
Gilles Peskine30a9e412019-01-14 18:36:12 +01002180/** The type of the state data structure for multipart AEAD operations.
2181 *
2182 * Before calling any function on an AEAD operation object, the application
2183 * must initialize it by any of the following means:
2184 * - Set the structure to all-bits-zero, for example:
2185 * \code
2186 * psa_aead_operation_t operation;
2187 * memset(&operation, 0, sizeof(operation));
2188 * \endcode
2189 * - Initialize the structure to logical zero values, for example:
2190 * \code
2191 * psa_aead_operation_t operation = {0};
2192 * \endcode
2193 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2194 * for example:
2195 * \code
2196 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2197 * \endcode
2198 * - Assign the result of the function psa_aead_operation_init()
2199 * to the structure, for example:
2200 * \code
2201 * psa_aead_operation_t operation;
2202 * operation = psa_aead_operation_init();
2203 * \endcode
2204 *
2205 * This is an implementation-defined \c struct. Applications should not
2206 * make any assumptions about the content of this structure except
2207 * as directed by the documentation of a specific implementation. */
2208typedef struct psa_aead_operation_s psa_aead_operation_t;
2209
2210/** \def PSA_AEAD_OPERATION_INIT
2211 *
2212 * This macro returns a suitable initializer for an AEAD operation object of
2213 * type #psa_aead_operation_t.
2214 */
2215#ifdef __DOXYGEN_ONLY__
2216/* This is an example definition for documentation purposes.
2217 * Implementations should define a suitable value in `crypto_struct.h`.
2218 */
2219#define PSA_AEAD_OPERATION_INIT {0}
2220#endif
2221
2222/** Return an initial value for an AEAD operation object.
2223 */
2224static psa_aead_operation_t psa_aead_operation_init(void);
2225
2226/** Set the key for a multipart authenticated encryption operation.
2227 *
2228 * The sequence of operations to encrypt a message with authentication
2229 * is as follows:
2230 * -# Allocate an operation object which will be passed to all the functions
2231 * listed here.
2232 * -# Initialize the operation object with one of the methods described in the
2233 * documentation for #psa_aead_operation_t, e.g.
2234 * PSA_AEAD_OPERATION_INIT.
2235 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002236 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2237 * inputs to the subsequent calls to psa_aead_update_ad() and
2238 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2239 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002240 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2241 * generate or set the nonce. You should use
2242 * psa_aead_generate_nonce() unless the protocol you are implementing
2243 * requires a specific nonce value.
2244 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2245 * of the non-encrypted additional authenticated data each time.
2246 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002247 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002248 * -# Call psa_aead_finish().
2249 *
2250 * The application may call psa_aead_abort() at any time after the operation
2251 * has been initialized.
2252 *
2253 * After a successful call to psa_aead_encrypt_setup(), the application must
2254 * eventually terminate the operation. The following events terminate an
2255 * operation:
2256 * - A failed call to any of the \c psa_aead_xxx functions.
2257 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2258 *
2259 * \param[in,out] operation The operation object to set up. It must have
2260 * been initialized as per the documentation for
2261 * #psa_aead_operation_t and not yet in use.
2262 * \param handle Handle to the key to use for the operation.
2263 * It must remain valid until the operation
2264 * terminates.
2265 * \param alg The AEAD algorithm to compute
2266 * (\c PSA_ALG_XXX value such that
2267 * #PSA_ALG_IS_AEAD(\p alg) is true).
2268 *
2269 * \retval #PSA_SUCCESS
2270 * Success.
2271 * \retval #PSA_ERROR_INVALID_HANDLE
2272 * \retval #PSA_ERROR_EMPTY_SLOT
2273 * \retval #PSA_ERROR_NOT_PERMITTED
2274 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002275 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002276 * \retval #PSA_ERROR_NOT_SUPPORTED
2277 * \p alg is not supported or is not an AEAD algorithm.
2278 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2279 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2280 * \retval #PSA_ERROR_HARDWARE_FAILURE
2281 * \retval #PSA_ERROR_TAMPERING_DETECTED
2282 * \retval #PSA_ERROR_BAD_STATE
2283 * The library has not been previously initialized by psa_crypto_init().
2284 * It is implementation-dependent whether a failure to initialize
2285 * results in this error code.
2286 */
2287psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2288 psa_key_handle_t handle,
2289 psa_algorithm_t alg);
2290
2291/** Set the key for a multipart authenticated decryption operation.
2292 *
2293 * The sequence of operations to decrypt a message with authentication
2294 * is as follows:
2295 * -# Allocate an operation object which will be passed to all the functions
2296 * listed here.
2297 * -# Initialize the operation object with one of the methods described in the
2298 * documentation for #psa_aead_operation_t, e.g.
2299 * PSA_AEAD_OPERATION_INIT.
2300 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002301 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2302 * inputs to the subsequent calls to psa_aead_update_ad() and
2303 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2304 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002305 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2306 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2307 * of the non-encrypted additional authenticated data each time.
2308 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002309 * of the ciphertext to decrypt each time.
2310 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002311 *
2312 * The application may call psa_aead_abort() at any time after the operation
2313 * has been initialized.
2314 *
2315 * After a successful call to psa_aead_decrypt_setup(), the application must
2316 * eventually terminate the operation. The following events terminate an
2317 * operation:
2318 * - A failed call to any of the \c psa_aead_xxx functions.
2319 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2320 *
2321 * \param[in,out] operation The operation object to set up. It must have
2322 * been initialized as per the documentation for
2323 * #psa_aead_operation_t and not yet in use.
2324 * \param handle Handle to the key to use for the operation.
2325 * It must remain valid until the operation
2326 * terminates.
2327 * \param alg The AEAD algorithm to compute
2328 * (\c PSA_ALG_XXX value such that
2329 * #PSA_ALG_IS_AEAD(\p alg) is true).
2330 *
2331 * \retval #PSA_SUCCESS
2332 * Success.
2333 * \retval #PSA_ERROR_INVALID_HANDLE
2334 * \retval #PSA_ERROR_EMPTY_SLOT
2335 * \retval #PSA_ERROR_NOT_PERMITTED
2336 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002337 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002338 * \retval #PSA_ERROR_NOT_SUPPORTED
2339 * \p alg is not supported or is not an AEAD algorithm.
2340 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2341 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2342 * \retval #PSA_ERROR_HARDWARE_FAILURE
2343 * \retval #PSA_ERROR_TAMPERING_DETECTED
2344 * \retval #PSA_ERROR_BAD_STATE
2345 * The library has not been previously initialized by psa_crypto_init().
2346 * It is implementation-dependent whether a failure to initialize
2347 * results in this error code.
2348 */
2349psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2350 psa_key_handle_t handle,
2351 psa_algorithm_t alg);
2352
2353/** Generate a random nonce for an authenticated encryption operation.
2354 *
2355 * This function generates a random nonce for the authenticated encryption
2356 * operation with an appropriate size for the chosen algorithm, key type
2357 * and key size.
2358 *
2359 * The application must call psa_aead_encrypt_setup() before
2360 * calling this function.
2361 *
2362 * If this function returns an error status, the operation becomes inactive.
2363 *
2364 * \param[in,out] operation Active AEAD operation.
2365 * \param[out] nonce Buffer where the generated nonce is to be
2366 * written.
2367 * \param nonce_size Size of the \p nonce buffer in bytes.
2368 * \param[out] nonce_length On success, the number of bytes of the
2369 * generated nonce.
2370 *
2371 * \retval #PSA_SUCCESS
2372 * Success.
2373 * \retval #PSA_ERROR_BAD_STATE
2374 * The operation state is not valid (not set up, or nonce already set).
2375 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2376 * The size of the \p nonce buffer is too small.
2377 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2378 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2379 * \retval #PSA_ERROR_HARDWARE_FAILURE
2380 * \retval #PSA_ERROR_TAMPERING_DETECTED
2381 */
2382psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2383 unsigned char *nonce,
2384 size_t nonce_size,
2385 size_t *nonce_length);
2386
2387/** Set the nonce for an authenticated encryption or decryption operation.
2388 *
2389 * This function sets the nonce for the authenticated
2390 * encryption or decryption operation.
2391 *
2392 * The application must call psa_aead_encrypt_setup() before
2393 * calling this function.
2394 *
2395 * If this function returns an error status, the operation becomes inactive.
2396 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002397 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002398 * instead of this function, unless implementing a protocol that requires
2399 * a non-random IV.
2400 *
2401 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002402 * \param[in] nonce Buffer containing the nonce to use.
2403 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002404 *
2405 * \retval #PSA_SUCCESS
2406 * Success.
2407 * \retval #PSA_ERROR_BAD_STATE
2408 * The operation state is not valid (not set up, or nonce already set).
2409 * \retval #PSA_ERROR_INVALID_ARGUMENT
2410 * The size of \p nonce is not acceptable for the chosen algorithm.
2411 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2412 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2413 * \retval #PSA_ERROR_HARDWARE_FAILURE
2414 * \retval #PSA_ERROR_TAMPERING_DETECTED
2415 */
2416psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2417 const unsigned char *nonce,
2418 size_t nonce_length);
2419
Gilles Peskinebc59c852019-01-17 15:26:08 +01002420/** Declare the lengths of the message and additional data for AEAD.
2421 *
2422 * The application must call this function before calling
2423 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2424 * the operation requires it. If the algorithm does not require it,
2425 * calling this function is optional, but if this function is called
2426 * then the implementation must enforce the lengths.
2427 *
2428 * You may call this function before or after setting the nonce with
2429 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2430 *
2431 * - For #PSA_ALG_CCM, calling this function is required.
2432 * - For the other AEAD algorithms defined in this specification, calling
2433 * this function is not required.
2434 * - For vendor-defined algorithm, refer to the vendor documentation.
2435 *
2436 * \param[in,out] operation Active AEAD operation.
2437 * \param ad_length Size of the non-encrypted additional
2438 * authenticated data in bytes.
2439 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2440 *
2441 * \retval #PSA_SUCCESS
2442 * Success.
2443 * \retval #PSA_ERROR_BAD_STATE
2444 * The operation state is not valid (not set up, already completed,
2445 * or psa_aead_update_ad() or psa_aead_update() already called).
2446 * \retval #PSA_ERROR_INVALID_ARGUMENT
2447 * At least one of the lengths is not acceptable for the chosen
2448 * algorithm.
2449 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2450 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2451 * \retval #PSA_ERROR_HARDWARE_FAILURE
2452 * \retval #PSA_ERROR_TAMPERING_DETECTED
2453 */
2454psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2455 size_t ad_length,
2456 size_t plaintext_length);
2457
Gilles Peskine30a9e412019-01-14 18:36:12 +01002458/** Pass additional data to an active AEAD operation.
2459 *
2460 * Additional data is authenticated, but not encrypted.
2461 *
2462 * You may call this function multiple times to pass successive fragments
2463 * of the additional data. You may not call this function after passing
2464 * data to encrypt or decrypt with psa_aead_update().
2465 *
2466 * Before calling this function, you must:
2467 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2468 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2469 *
2470 * If this function returns an error status, the operation becomes inactive.
2471 *
2472 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2473 * there is no guarantee that the input is valid. Therefore, until
2474 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2475 * treat the input as untrusted and prepare to undo any action that
2476 * depends on the input if psa_aead_verify() returns an error status.
2477 *
2478 * \param[in,out] operation Active AEAD operation.
2479 * \param[in] input Buffer containing the fragment of
2480 * additional data.
2481 * \param input_length Size of the \p input buffer in bytes.
2482 *
2483 * \retval #PSA_SUCCESS
2484 * Success.
2485 * \retval #PSA_ERROR_BAD_STATE
2486 * The operation state is not valid (not set up, nonce not set,
2487 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002488 * \retval #PSA_ERROR_INVALID_ARGUMENT
2489 * The total input length overflows the additional data length that
2490 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002491 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2492 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2493 * \retval #PSA_ERROR_HARDWARE_FAILURE
2494 * \retval #PSA_ERROR_TAMPERING_DETECTED
2495 */
2496psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2497 const uint8_t *input,
2498 size_t input_length);
2499
2500/** Encrypt or decrypt a message fragment in an active AEAD operation.
2501 *
2502 * Before calling this function, you must:
2503 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2504 * The choice of setup function determines whether this function
2505 * encrypts or decrypts its input.
2506 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2507 * 3. Call psa_aead_update_ad() to pass all the additional data.
2508 *
2509 * If this function returns an error status, the operation becomes inactive.
2510 *
2511 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2512 * there is no guarantee that the input is valid. Therefore, until
2513 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2514 * - Do not use the output in any way other than storing it in a
2515 * confidential location. If you take any action that depends
2516 * on the tentative decrypted data, this action will need to be
2517 * undone if the input turns out not to be valid. Furthermore,
2518 * if an adversary can observe that this action took place
2519 * (for example through timing), they may be able to use this
2520 * fact as an oracle to decrypt any message encrypted with the
2521 * same key.
2522 * - In particular, do not copy the output anywhere but to a
2523 * memory or storage space that you have exclusive access to.
2524 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002525 * This function does not require the input to be aligned to any
2526 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002527 * a whole block at a time, it must consume all the input provided, but
2528 * it may delay the end of the corresponding output until a subsequent
2529 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2530 * provides sufficient input. The amount of data that can be delayed
2531 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002532 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002533 * \param[in,out] operation Active AEAD operation.
2534 * \param[in] input Buffer containing the message fragment to
2535 * encrypt or decrypt.
2536 * \param input_length Size of the \p input buffer in bytes.
2537 * \param[out] output Buffer where the output is to be written.
2538 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002539 * This must be at least
2540 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2541 * \p input_length) where \c alg is the
2542 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002543 * \param[out] output_length On success, the number of bytes
2544 * that make up the returned output.
2545 *
2546 * \retval #PSA_SUCCESS
2547 * Success.
2548 * \retval #PSA_ERROR_BAD_STATE
2549 * The operation state is not valid (not set up, nonce not set
2550 * or already completed).
2551 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2552 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002553 * You can determine a sufficient buffer size by calling
2554 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2555 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002556 * \retval #PSA_ERROR_INVALID_ARGUMENT
2557 * The total length of input to psa_aead_update_ad() so far is
2558 * less than the additional data length that was previously
2559 * specified with psa_aead_set_lengths().
2560 * \retval #PSA_ERROR_INVALID_ARGUMENT
2561 * The total input length overflows the plaintext length that
2562 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002563 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2564 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2565 * \retval #PSA_ERROR_HARDWARE_FAILURE
2566 * \retval #PSA_ERROR_TAMPERING_DETECTED
2567 */
2568psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2569 const uint8_t *input,
2570 size_t input_length,
2571 unsigned char *output,
2572 size_t output_size,
2573 size_t *output_length);
2574
2575/** Finish encrypting a message in an AEAD operation.
2576 *
2577 * The operation must have been set up with psa_aead_encrypt_setup().
2578 *
2579 * This function finishes the authentication of the additional data
2580 * formed by concatenating the inputs passed to preceding calls to
2581 * psa_aead_update_ad() with the plaintext formed by concatenating the
2582 * inputs passed to preceding calls to psa_aead_update().
2583 *
2584 * This function has two output buffers:
2585 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002586 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002587 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002588 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002589 * that the operation performs.
2590 *
2591 * When this function returns, the operation becomes inactive.
2592 *
2593 * \param[in,out] operation Active AEAD operation.
2594 * \param[out] ciphertext Buffer where the last part of the ciphertext
2595 * is to be written.
2596 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002597 * This must be at least
2598 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2599 * \c alg is the algorithm that is being
2600 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002601 * \param[out] ciphertext_length On success, the number of bytes of
2602 * returned ciphertext.
2603 * \param[out] tag Buffer where the authentication tag is
2604 * to be written.
2605 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002606 * This must be at least
2607 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2608 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002609 * \param[out] tag_length On success, the number of bytes
2610 * that make up the returned tag.
2611 *
2612 * \retval #PSA_SUCCESS
2613 * Success.
2614 * \retval #PSA_ERROR_BAD_STATE
2615 * The operation state is not valid (not set up, nonce not set,
2616 * decryption, or already completed).
2617 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002618 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002619 * You can determine a sufficient buffer size for \p ciphertext by
2620 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2621 * where \c alg is the algorithm that is being calculated.
2622 * You can determine a sufficient buffer size for \p tag by
2623 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002624 * \retval #PSA_ERROR_INVALID_ARGUMENT
2625 * The total length of input to psa_aead_update_ad() so far is
2626 * less than the additional data length that was previously
2627 * specified with psa_aead_set_lengths().
2628 * \retval #PSA_ERROR_INVALID_ARGUMENT
2629 * The total length of input to psa_aead_update() so far is
2630 * less than the plaintext length that was previously
2631 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002632 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2633 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2634 * \retval #PSA_ERROR_HARDWARE_FAILURE
2635 * \retval #PSA_ERROR_TAMPERING_DETECTED
2636 */
2637psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002638 uint8_t *ciphertext,
2639 size_t ciphertext_size,
2640 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002641 uint8_t *tag,
2642 size_t tag_size,
2643 size_t *tag_length);
2644
2645/** Finish authenticating and decrypting a message in an AEAD operation.
2646 *
2647 * The operation must have been set up with psa_aead_decrypt_setup().
2648 *
2649 * This function finishes the authentication of the additional data
2650 * formed by concatenating the inputs passed to preceding calls to
2651 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2652 * inputs passed to preceding calls to psa_aead_update().
2653 *
2654 * When this function returns, the operation becomes inactive.
2655 *
2656 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002657 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002658 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002659 * from previous calls to psa_aead_update()
2660 * that could not be processed until the end
2661 * of the input.
2662 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002663 * This must be at least
2664 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2665 * \c alg is the algorithm that is being
2666 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002667 * \param[out] plaintext_length On success, the number of bytes of
2668 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002669 * \param[in] tag Buffer containing the authentication tag.
2670 * \param tag_length Size of the \p tag buffer in bytes.
2671 *
2672 * \retval #PSA_SUCCESS
2673 * Success.
2674 * \retval #PSA_ERROR_BAD_STATE
2675 * The operation state is not valid (not set up, nonce not set,
2676 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002677 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2678 * The size of the \p plaintext buffer is too small.
2679 * You can determine a sufficient buffer size for \p plaintext by
2680 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2681 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002682 * \retval #PSA_ERROR_INVALID_ARGUMENT
2683 * The total length of input to psa_aead_update_ad() so far is
2684 * less than the additional data length that was previously
2685 * specified with psa_aead_set_lengths().
2686 * \retval #PSA_ERROR_INVALID_ARGUMENT
2687 * The total length of input to psa_aead_update() so far is
2688 * less than the plaintext length that was previously
2689 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002690 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2691 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2692 * \retval #PSA_ERROR_HARDWARE_FAILURE
2693 * \retval #PSA_ERROR_TAMPERING_DETECTED
2694 */
2695psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002696 uint8_t *plaintext,
2697 size_t plaintext_size,
2698 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002699 const uint8_t *tag,
2700 size_t tag_length);
2701
2702/** Abort an AEAD operation.
2703 *
2704 * Aborting an operation frees all associated resources except for the
2705 * \p operation structure itself. Once aborted, the operation object
2706 * can be reused for another operation by calling
2707 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2708 *
2709 * You may call this function any time after the operation object has
2710 * been initialized by any of the following methods:
2711 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2712 * whether it succeeds or not.
2713 * - Initializing the \c struct to all-bits-zero.
2714 * - Initializing the \c struct to logical zeros, e.g.
2715 * `psa_aead_operation_t operation = {0}`.
2716 *
2717 * In particular, calling psa_aead_abort() after the operation has been
2718 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2719 * is safe and has no effect.
2720 *
2721 * \param[in,out] operation Initialized AEAD operation.
2722 *
2723 * \retval #PSA_SUCCESS
2724 * \retval #PSA_ERROR_BAD_STATE
2725 * \p operation is not an active AEAD operation.
2726 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2727 * \retval #PSA_ERROR_HARDWARE_FAILURE
2728 * \retval #PSA_ERROR_TAMPERING_DETECTED
2729 */
2730psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2731
Gilles Peskine3b555712018-03-03 21:27:57 +01002732/**@}*/
2733
Gilles Peskine20035e32018-02-03 22:44:14 +01002734/** \defgroup asymmetric Asymmetric cryptography
2735 * @{
2736 */
2737
2738/**
2739 * \brief Sign a hash or short message with a private key.
2740 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002741 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002742 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002743 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2744 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2745 * to determine the hash algorithm to use.
2746 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002747 * \param handle Handle to the key to use for the operation.
2748 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002749 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002750 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002751 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002752 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002753 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002754 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002755 * \param[out] signature_length On success, the number of bytes
2756 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002757 *
Gilles Peskine28538492018-07-11 17:34:00 +02002758 * \retval #PSA_SUCCESS
2759 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002760 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002761 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002762 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002763 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002764 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002765 * \retval #PSA_ERROR_NOT_SUPPORTED
2766 * \retval #PSA_ERROR_INVALID_ARGUMENT
2767 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2768 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2769 * \retval #PSA_ERROR_HARDWARE_FAILURE
2770 * \retval #PSA_ERROR_TAMPERING_DETECTED
2771 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002772 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002773 * The library has not been previously initialized by psa_crypto_init().
2774 * It is implementation-dependent whether a failure to initialize
2775 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002776 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002777psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002778 psa_algorithm_t alg,
2779 const uint8_t *hash,
2780 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002781 uint8_t *signature,
2782 size_t signature_size,
2783 size_t *signature_length);
2784
2785/**
2786 * \brief Verify the signature a hash or short message using a public key.
2787 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002788 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002789 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002790 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2791 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2792 * to determine the hash algorithm to use.
2793 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002794 * \param handle Handle to the key to use for the operation.
2795 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002796 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002797 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002798 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002799 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002800 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002801 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002802 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002803 *
Gilles Peskine28538492018-07-11 17:34:00 +02002804 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002805 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002806 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002807 * The calculation was perfomed successfully, but the passed
2808 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002809 * \retval #PSA_ERROR_NOT_SUPPORTED
2810 * \retval #PSA_ERROR_INVALID_ARGUMENT
2811 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2812 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2813 * \retval #PSA_ERROR_HARDWARE_FAILURE
2814 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002815 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002816 * The library has not been previously initialized by psa_crypto_init().
2817 * It is implementation-dependent whether a failure to initialize
2818 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002819 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002820psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002821 psa_algorithm_t alg,
2822 const uint8_t *hash,
2823 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002824 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002825 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002826
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002827/**
2828 * \brief Encrypt a short message with a public key.
2829 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002830 * \param handle Handle to the key to use for the operation.
2831 * It must be a public key or an asymmetric
2832 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002833 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002834 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002835 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002836 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002837 * \param[in] salt A salt or label, if supported by the
2838 * encryption algorithm.
2839 * If the algorithm does not support a
2840 * salt, pass \c NULL.
2841 * If the algorithm supports an optional
2842 * salt and you do not want to pass a salt,
2843 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002844 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002845 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2846 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002847 * \param salt_length Size of the \p salt buffer in bytes.
2848 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002849 * \param[out] output Buffer where the encrypted message is to
2850 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002851 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002852 * \param[out] output_length On success, the number of bytes
2853 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002854 *
Gilles Peskine28538492018-07-11 17:34:00 +02002855 * \retval #PSA_SUCCESS
2856 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002857 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002858 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002859 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002860 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002861 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002862 * \retval #PSA_ERROR_NOT_SUPPORTED
2863 * \retval #PSA_ERROR_INVALID_ARGUMENT
2864 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2865 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2866 * \retval #PSA_ERROR_HARDWARE_FAILURE
2867 * \retval #PSA_ERROR_TAMPERING_DETECTED
2868 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002869 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002870 * The library has not been previously initialized by psa_crypto_init().
2871 * It is implementation-dependent whether a failure to initialize
2872 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002873 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002874psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002875 psa_algorithm_t alg,
2876 const uint8_t *input,
2877 size_t input_length,
2878 const uint8_t *salt,
2879 size_t salt_length,
2880 uint8_t *output,
2881 size_t output_size,
2882 size_t *output_length);
2883
2884/**
2885 * \brief Decrypt a short message with a private key.
2886 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002887 * \param handle Handle to the key to use for the operation.
2888 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002889 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002890 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002891 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002892 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002893 * \param[in] salt A salt or label, if supported by the
2894 * encryption algorithm.
2895 * If the algorithm does not support a
2896 * salt, pass \c NULL.
2897 * If the algorithm supports an optional
2898 * salt and you do not want to pass a salt,
2899 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002900 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002901 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2902 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002903 * \param salt_length Size of the \p salt buffer in bytes.
2904 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002905 * \param[out] output Buffer where the decrypted message is to
2906 * be written.
2907 * \param output_size Size of the \c output buffer in bytes.
2908 * \param[out] output_length On success, the number of bytes
2909 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002910 *
Gilles Peskine28538492018-07-11 17:34:00 +02002911 * \retval #PSA_SUCCESS
2912 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002913 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002914 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002915 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002916 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002917 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002918 * \retval #PSA_ERROR_NOT_SUPPORTED
2919 * \retval #PSA_ERROR_INVALID_ARGUMENT
2920 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2921 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2922 * \retval #PSA_ERROR_HARDWARE_FAILURE
2923 * \retval #PSA_ERROR_TAMPERING_DETECTED
2924 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2925 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002926 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002927 * The library has not been previously initialized by psa_crypto_init().
2928 * It is implementation-dependent whether a failure to initialize
2929 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002930 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002931psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002932 psa_algorithm_t alg,
2933 const uint8_t *input,
2934 size_t input_length,
2935 const uint8_t *salt,
2936 size_t salt_length,
2937 uint8_t *output,
2938 size_t output_size,
2939 size_t *output_length);
2940
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002941/**@}*/
2942
Gilles Peskineedd76872018-07-20 17:42:05 +02002943/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002944 * @{
2945 */
2946
2947/** The type of the state data structure for generators.
2948 *
2949 * Before calling any function on a generator, the application must
2950 * initialize it by any of the following means:
2951 * - Set the structure to all-bits-zero, for example:
2952 * \code
2953 * psa_crypto_generator_t generator;
2954 * memset(&generator, 0, sizeof(generator));
2955 * \endcode
2956 * - Initialize the structure to logical zero values, for example:
2957 * \code
2958 * psa_crypto_generator_t generator = {0};
2959 * \endcode
2960 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2961 * for example:
2962 * \code
2963 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2964 * \endcode
2965 * - Assign the result of the function psa_crypto_generator_init()
2966 * to the structure, for example:
2967 * \code
2968 * psa_crypto_generator_t generator;
2969 * generator = psa_crypto_generator_init();
2970 * \endcode
2971 *
2972 * This is an implementation-defined \c struct. Applications should not
2973 * make any assumptions about the content of this structure except
2974 * as directed by the documentation of a specific implementation.
2975 */
2976typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2977
2978/** \def PSA_CRYPTO_GENERATOR_INIT
2979 *
2980 * This macro returns a suitable initializer for a generator object
2981 * of type #psa_crypto_generator_t.
2982 */
2983#ifdef __DOXYGEN_ONLY__
2984/* This is an example definition for documentation purposes.
2985 * Implementations should define a suitable value in `crypto_struct.h`.
2986 */
2987#define PSA_CRYPTO_GENERATOR_INIT {0}
2988#endif
2989
2990/** Return an initial value for a generator object.
2991 */
2992static psa_crypto_generator_t psa_crypto_generator_init(void);
2993
2994/** Retrieve the current capacity of a generator.
2995 *
2996 * The capacity of a generator is the maximum number of bytes that it can
2997 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2998 *
2999 * \param[in] generator The generator to query.
3000 * \param[out] capacity On success, the capacity of the generator.
3001 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003002 * \retval #PSA_SUCCESS
3003 * \retval #PSA_ERROR_BAD_STATE
3004 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02003005 */
3006psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3007 size_t *capacity);
3008
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003009/** Set the maximum capacity of a generator.
3010 *
3011 * \param[in,out] generator The generator object to modify.
3012 * \param capacity The new capacity of the generator.
3013 * It must be less or equal to the generator's
3014 * current capacity.
3015 *
3016 * \retval #PSA_SUCCESS
3017 * \retval #PSA_ERROR_INVALID_ARGUMENT
3018 * \p capacity is larger than the generator's current capacity.
3019 * \retval #PSA_ERROR_BAD_STATE
3020 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3021 */
3022psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
3023 size_t capacity);
3024
Gilles Peskineeab56e42018-07-12 17:12:33 +02003025/** Read some data from a generator.
3026 *
3027 * This function reads and returns a sequence of bytes from a generator.
3028 * The data that is read is discarded from the generator. The generator's
3029 * capacity is decreased by the number of bytes read.
3030 *
3031 * \param[in,out] generator The generator object to read from.
3032 * \param[out] output Buffer where the generator output will be
3033 * written.
3034 * \param output_length Number of bytes to output.
3035 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003036 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003037 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskineeab56e42018-07-12 17:12:33 +02003038 * There were fewer than \p output_length bytes
3039 * in the generator. Note that in this case, no
3040 * output is written to the output buffer.
3041 * The generator's capacity is set to 0, thus
3042 * subsequent calls to this function will not
3043 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003044 * \retval #PSA_ERROR_BAD_STATE
3045 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3046 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3047 * \retval #PSA_ERROR_HARDWARE_FAILURE
3048 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003049 */
3050psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
3051 uint8_t *output,
3052 size_t output_length);
3053
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003054/** Generate a key deterministically from data read from a generator.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003055 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003056 * This function uses the output of a generator to derive a key.
3057 * How much output it consumes and how the key is derived depends on the
3058 * key type.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003059 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003060 * - For key types for which the key is an arbitrary sequence of bytes
3061 * of a given size,
3062 * this function is functionally equivalent to calling #psa_generator_read
3063 * and passing the resulting output to #psa_import_key.
3064 * However, this function has a security benefit:
3065 * if the implementation provides an isolation boundary then
3066 * the key material is not exposed outside the isolation boundary.
3067 * As a consequence, for these key types, this function always consumes
3068 * exactly (\p bits / 8) bytes from the generator.
3069 * The following key types defined in this specification follow this scheme:
3070 *
3071 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003072 * - #PSA_KEY_TYPE_ARC4;
3073 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003074 * - #PSA_KEY_TYPE_DERIVE;
3075 * - #PSA_KEY_TYPE_HMAC.
3076 *
3077 * - For ECC keys on a Montgomery elliptic curve
3078 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3079 * Montgomery curve), this function always draws a byte string whose
3080 * length is determined by the curve, and sets the mandatory bits
3081 * accordingly. That is:
3082 *
3083 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3084 * and process it as specified in RFC 7748 &sect;5.
3085 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3086 * and process it as specified in RFC 7748 &sect;5.
3087 *
3088 * - For key types for which the key is represented by a single sequence of
3089 * \p bits bits with constraints as to which bit sequences are acceptable,
3090 * this function draws a byte string of length (\p bits / 8) bytes rounded
3091 * up to the nearest whole number of bytes. If the resulting byte string
3092 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3093 * This process is repeated until an acceptable byte string is drawn.
3094 * The byte string drawn from the generator is interpreted as specified
3095 * for the output produced by psa_export_key().
3096 * The following key types defined in this specification follow this scheme:
3097 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003098 * - #PSA_KEY_TYPE_DES.
3099 * Force-set the parity bits, but discard forbidden weak keys.
3100 * For 2-key and 3-key triple-DES, the three keys are generated
3101 * successively (for example, for 3-key triple-DES,
3102 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3103 * discard the first 8 bytes, use the next 8 bytes as the first key,
3104 * and continue reading output from the generator to derive the other
3105 * two keys).
3106 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR),
3107 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
3108 * ECC keys on a Weierstrass elliptic curve
3109 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3110 * Weierstrass curve).
3111 * For these key types, interpret the byte string as integer
3112 * in big-endian order. Discard it if it is not in the range
3113 * [0, *N* - 2] where *N* is the boundary of the private key domain
3114 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003115 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003116 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003117 * This method allows compliance to NIST standards, specifically
3118 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003119 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3120 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3121 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3122 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003123 *
3124 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
3125 * the way in which the generator output is consumed is
3126 * implementation-defined.
3127 *
3128 * In all cases, the data that is read is discarded from the generator.
3129 * The generator's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003130 *
Gilles Peskine20628592019-04-19 19:29:50 +02003131 * \param[in] attributes The attributes for the new key.
Gilles Peskine98dd7792019-05-15 19:43:49 +02003132 * \param[in,out] generator The generator object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003133 * \param[out] handle On success, a handle to the newly created key.
3134 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003135 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003136 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003137 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003138 * If the key is persistent, the key material and the key's metadata
3139 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003140 * \retval #PSA_ERROR_ALREADY_EXISTS
3141 * This is an attempt to create a persistent key, and there is
3142 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003143 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003144 * There was not enough data to create the desired key.
3145 * Note that in this case, no output is written to the output buffer.
3146 * The generator's capacity is set to 0, thus subsequent calls to
3147 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003148 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003149 * The key type or key size is not supported, either by the
3150 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003151 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003152 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3153 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3154 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3155 * \retval #PSA_ERROR_HARDWARE_FAILURE
3156 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003157 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003158 * The library has not been previously initialized by psa_crypto_init().
3159 * It is implementation-dependent whether a failure to initialize
3160 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003161 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003162psa_status_t psa_generate_derived_key(const psa_key_attributes_t *attributes,
Gilles Peskine98dd7792019-05-15 19:43:49 +02003163 psa_crypto_generator_t *generator,
3164 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003165
3166/** Abort a generator.
3167 *
3168 * Once a generator has been aborted, its capacity is zero.
3169 * Aborting a generator frees all associated resources except for the
3170 * \c generator structure itself.
3171 *
3172 * This function may be called at any time as long as the generator
3173 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
3174 * psa_crypto_generator_init() or a zero value. In particular, it is valid
3175 * to call psa_generator_abort() twice, or to call psa_generator_abort()
3176 * on a generator that has not been set up.
3177 *
3178 * Once aborted, the generator object may be called.
3179 *
3180 * \param[in,out] generator The generator to abort.
3181 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003182 * \retval #PSA_SUCCESS
3183 * \retval #PSA_ERROR_BAD_STATE
3184 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3185 * \retval #PSA_ERROR_HARDWARE_FAILURE
3186 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003187 */
3188psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3189
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003190/** Use the maximum possible capacity for a generator.
3191 *
3192 * Use this value as the capacity argument when setting up a generator
3193 * to indicate that the generator should have the maximum possible capacity.
3194 * The value of the maximum possible capacity depends on the generator
3195 * algorithm.
3196 */
3197#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
3198
Gilles Peskineeab56e42018-07-12 17:12:33 +02003199/**@}*/
3200
Gilles Peskineea0fb492018-07-12 17:17:20 +02003201/** \defgroup derivation Key derivation
3202 * @{
3203 */
3204
3205/** Set up a key derivation operation.
3206 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003207 * A key derivation algorithm takes some inputs and uses them to create
3208 * a byte generator which can be used to produce keys and other
3209 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003210 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003211 * To use a generator for key derivation:
3212 * - Start with an initialized object of type #psa_crypto_generator_t.
3213 * - Call psa_key_derivation_setup() to select the algorithm.
3214 * - Provide the inputs for the key derivation by calling
3215 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3216 * as appropriate. Which inputs are needed, in what order, and whether
3217 * they may be keys and if so of what type depends on the algorithm.
3218 * - Optionally set the generator's maximum capacity with
3219 * psa_set_generator_capacity(). You may do this before, in the middle of
3220 * or after providing inputs. For some algorithms, this step is mandatory
3221 * because the output depends on the maximum capacity.
3222 * - Generate output with psa_generator_read() or
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003223 * psa_generate_derived_key(). Successive calls to these functions
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003224 * use successive output bytes from the generator.
3225 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02003226 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003227 * \param[in,out] generator The generator object to set up. It must
3228 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003229 * \param alg The key derivation algorithm to compute
3230 * (\c PSA_ALG_XXX value such that
3231 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02003232 *
3233 * \retval #PSA_SUCCESS
3234 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003235 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003236 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003237 * \retval #PSA_ERROR_NOT_SUPPORTED
3238 * \c alg is not supported or is not a key derivation algorithm.
3239 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3240 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3241 * \retval #PSA_ERROR_HARDWARE_FAILURE
3242 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003243 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003244 */
3245psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
3246 psa_algorithm_t alg);
3247
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003248/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003249 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003250 * Which inputs are required and in what order depends on the algorithm.
3251 * Refer to the documentation of each key derivation or key agreement
3252 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003253 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003254 * This function passes direct inputs. Some inputs must be passed as keys
3255 * using psa_key_derivation_input_key() instead of this function. Refer to
3256 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003257 *
3258 * \param[in,out] generator The generator object to use. It must
3259 * have been set up with
3260 * psa_key_derivation_setup() and must not
3261 * have produced any output yet.
3262 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003263 * \param[in] data Input data to use.
3264 * \param data_length Size of the \p data buffer in bytes.
3265 *
3266 * \retval #PSA_SUCCESS
3267 * Success.
3268 * \retval #PSA_ERROR_INVALID_ARGUMENT
3269 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003270 * \retval #PSA_ERROR_INVALID_ARGUMENT
3271 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003272 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3273 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3274 * \retval #PSA_ERROR_HARDWARE_FAILURE
3275 * \retval #PSA_ERROR_TAMPERING_DETECTED
3276 * \retval #PSA_ERROR_BAD_STATE
3277 * The value of \p step is not valid given the state of \p generator.
3278 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003279 * The library has not been previously initialized by psa_crypto_init().
3280 * It is implementation-dependent whether a failure to initialize
3281 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003282 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003283psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3284 psa_key_derivation_step_t step,
3285 const uint8_t *data,
3286 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003287
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003288/** Provide an input for key derivation in the form of a key.
3289 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003290 * Which inputs are required and in what order depends on the algorithm.
3291 * Refer to the documentation of each key derivation or key agreement
3292 * algorithm for information.
3293 *
3294 * This function passes key inputs. Some inputs must be passed as keys
3295 * of the appropriate type using this function, while others must be
3296 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3297 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003298 *
3299 * \param[in,out] generator The generator object to use. It must
3300 * have been set up with
3301 * psa_key_derivation_setup() and must not
3302 * have produced any output yet.
3303 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003304 * \param handle Handle to the key. It must have an
3305 * appropriate type for \p step and must
3306 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003307 *
3308 * \retval #PSA_SUCCESS
3309 * Success.
3310 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine31351842019-04-09 12:00:00 +02003311 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003312 * \retval #PSA_ERROR_NOT_PERMITTED
3313 * \retval #PSA_ERROR_INVALID_ARGUMENT
3314 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003315 * \retval #PSA_ERROR_INVALID_ARGUMENT
3316 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003317 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3318 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3319 * \retval #PSA_ERROR_HARDWARE_FAILURE
3320 * \retval #PSA_ERROR_TAMPERING_DETECTED
3321 * \retval #PSA_ERROR_BAD_STATE
3322 * The value of \p step is not valid given the state of \p generator.
3323 * \retval #PSA_ERROR_BAD_STATE
3324 * The library has not been previously initialized by psa_crypto_init().
3325 * It is implementation-dependent whether a failure to initialize
3326 * results in this error code.
3327 */
3328psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3329 psa_key_derivation_step_t step,
3330 psa_key_handle_t handle);
3331
Gilles Peskine969c5d62019-01-16 15:53:06 +01003332/** Perform a key agreement and use the shared secret as input to a key
3333 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003334 *
3335 * A key agreement algorithm takes two inputs: a private key \p private_key
3336 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003337 * The result of this function is passed as input to a key derivation.
3338 * The output of this key derivation can be extracted by reading from the
3339 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003340 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003341 * \param[in,out] generator The generator object to use. It must
3342 * have been set up with
3343 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003344 * key agreement and derivation algorithm
3345 * \c alg (\c PSA_ALG_XXX value such that
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003346 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3347 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
Gilles Peskine6843c292019-01-18 16:44:49 +01003348 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003349 * The generator must be ready for an
3350 * input of the type given by \p step.
3351 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003352 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003353 * \param[in] peer_key Public key of the peer. The peer key must be in the
3354 * same format that psa_import_key() accepts for the
3355 * public key type corresponding to the type of
3356 * private_key. That is, this function performs the
3357 * equivalent of
Gilles Peskine806051f2019-05-15 19:50:17 +02003358 * #psa_import_key(...,
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003359 * `peer_key`, `peer_key_length`) where
Gilles Peskine806051f2019-05-15 19:50:17 +02003360 * with key attributes indicating the public key
3361 * type corresponding to the type of `private_key`.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003362 * For example, for EC keys, this means that peer_key
3363 * is interpreted as a point on the curve that the
3364 * private key is on. The standard formats for public
3365 * keys are documented in the documentation of
3366 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003367 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003368 *
3369 * \retval #PSA_SUCCESS
3370 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003371 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02003372 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine01d718c2018-09-18 12:01:02 +02003373 * \retval #PSA_ERROR_NOT_PERMITTED
3374 * \retval #PSA_ERROR_INVALID_ARGUMENT
3375 * \c private_key is not compatible with \c alg,
3376 * or \p peer_key is not valid for \c alg or not compatible with
3377 * \c private_key.
3378 * \retval #PSA_ERROR_NOT_SUPPORTED
3379 * \c alg is not supported or is not a key derivation algorithm.
3380 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3381 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3382 * \retval #PSA_ERROR_HARDWARE_FAILURE
3383 * \retval #PSA_ERROR_TAMPERING_DETECTED
3384 */
3385psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003386 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003387 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003388 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003389 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003390
Gilles Peskine769c7a62019-01-18 16:42:29 +01003391/** Perform a key agreement and use the shared secret as input to a key
3392 * derivation.
3393 *
3394 * A key agreement algorithm takes two inputs: a private key \p private_key
3395 * a public key \p peer_key.
3396 *
3397 * \warning The raw result of a key agreement algorithm such as finite-field
3398 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3399 * not be used directly as key material. It should instead be passed as
3400 * input to a key derivation algorithm. To chain a key agreement with
3401 * a key derivation, use psa_key_agreement() and other functions from
3402 * the key derivation and generator interface.
3403 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003404 * \param alg The key agreement algorithm to compute
3405 * (\c PSA_ALG_XXX value such that
3406 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3407 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003408 * \param private_key Handle to the private key to use.
3409 * \param[in] peer_key Public key of the peer. It must be
3410 * in the same format that psa_import_key()
3411 * accepts. The standard formats for public
3412 * keys are documented in the documentation
3413 * of psa_export_public_key().
3414 * \param peer_key_length Size of \p peer_key in bytes.
3415 * \param[out] output Buffer where the decrypted message is to
3416 * be written.
3417 * \param output_size Size of the \c output buffer in bytes.
3418 * \param[out] output_length On success, the number of bytes
3419 * that make up the returned output.
3420 *
3421 * \retval #PSA_SUCCESS
3422 * Success.
3423 * \retval #PSA_ERROR_INVALID_HANDLE
3424 * \retval #PSA_ERROR_EMPTY_SLOT
3425 * \retval #PSA_ERROR_NOT_PERMITTED
3426 * \retval #PSA_ERROR_INVALID_ARGUMENT
3427 * \p alg is not a key agreement algorithm
3428 * \retval #PSA_ERROR_INVALID_ARGUMENT
3429 * \p private_key is not compatible with \p alg,
3430 * or \p peer_key is not valid for \p alg or not compatible with
3431 * \p private_key.
3432 * \retval #PSA_ERROR_NOT_SUPPORTED
3433 * \p alg is not a supported key agreement algorithm.
3434 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3435 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3436 * \retval #PSA_ERROR_HARDWARE_FAILURE
3437 * \retval #PSA_ERROR_TAMPERING_DETECTED
3438 */
3439psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3440 psa_key_handle_t private_key,
3441 const uint8_t *peer_key,
3442 size_t peer_key_length,
3443 uint8_t *output,
3444 size_t output_size,
3445 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003446
3447/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003448
3449/** \defgroup random Random generation
3450 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003451 */
3452
3453/**
3454 * \brief Generate random bytes.
3455 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003456 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003457 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003458 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003459 *
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003460 * \note To generate a key, use psa_generate_random_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003461 *
3462 * \param[out] output Output buffer for the generated data.
3463 * \param output_size Number of bytes to generate and output.
3464 *
3465 * \retval #PSA_SUCCESS
3466 * \retval #PSA_ERROR_NOT_SUPPORTED
3467 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3468 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3469 * \retval #PSA_ERROR_HARDWARE_FAILURE
3470 * \retval #PSA_ERROR_TAMPERING_DETECTED
3471 * \retval #PSA_ERROR_BAD_STATE
3472 * The library has not been previously initialized by psa_crypto_init().
3473 * It is implementation-dependent whether a failure to initialize
3474 * results in this error code.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003475 */
3476psa_status_t psa_generate_random(uint8_t *output,
3477 size_t output_size);
3478
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003479/**
3480 * \brief Generate a key or key pair.
3481 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003482 * The key is generated randomly.
3483 * Its location, policy, type and size are taken from \p attributes.
3484 *
3485 * If the type requires additional domain parameters, these are taken
3486 * from \p attributes as well. The following types use domain parameters:
3487 * - When generating an RSA key (#PSA_KEY_TYPE_RSA_KEYPAIR),
3488 * the default public exponent is 65537. This value is used if
3489 * \p attributes was set with psa_set_key_type() or by passing an empty
3490 * byte string as domain parameters to psa_set_key_domain_parameters().
3491 * If psa_set_key_domain_parameters() was used to set a non-empty
3492 * domain parameter string in \p attributes, this string is read as
3493 * a big-endian integer which is used as the public exponent.
3494 * - When generating a DSA key (#PSA_KEY_TYPE_DSA_KEYPAIR) or a
3495 * Diffie-Hellman key (#PSA_KEY_TYPE_DH_KEYPAIR), the domain parameters
3496 * from \p attributes are interpreted as described for
3497 * psa_set_key_domain_parameters().
3498 *
Gilles Peskine20628592019-04-19 19:29:50 +02003499 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003500 * \param[out] handle On success, a handle to the newly created key.
3501 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003502 *
Gilles Peskine28538492018-07-11 17:34:00 +02003503 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003504 * Success.
3505 * If the key is persistent, the key material and the key's metadata
3506 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003507 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003508 * This is an attempt to create a persistent key, and there is
3509 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003510 * \retval #PSA_ERROR_NOT_SUPPORTED
3511 * \retval #PSA_ERROR_INVALID_ARGUMENT
3512 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3513 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3514 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3515 * \retval #PSA_ERROR_HARDWARE_FAILURE
3516 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003517 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003518 * The library has not been previously initialized by psa_crypto_init().
3519 * It is implementation-dependent whether a failure to initialize
3520 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003521 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003522psa_status_t psa_generate_random_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003523 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003524
3525/**@}*/
3526
Gilles Peskinee59236f2018-01-27 23:32:46 +01003527#ifdef __cplusplus
3528}
3529#endif
3530
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003531/* The file "crypto_sizes.h" contains definitions for size calculation
3532 * macros whose definitions are implementation-specific. */
3533#include "crypto_sizes.h"
3534
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003535/* The file "crypto_struct.h" contains definitions for
3536 * implementation-specific structs that are declared above. */
3537#include "crypto_struct.h"
3538
3539/* The file "crypto_extra.h" contains vendor-specific definitions. This
3540 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003541#include "crypto_extra.h"
3542
3543#endif /* PSA_CRYPTO_H */