blob: ba2692cc452d441f292c2fd6ee246bce7a9c7a9b [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
89 * \retval #PSA_ERROR_TAMPERING_DETECTED
90 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
Gilles Peskine87a5e562019-04-17 12:28:25 +020096/** \defgroup attributes Key attributes
97 * @{
98 */
99
100/** The type of a structure containing key attributes.
101 *
102 * This is an opaque structure that can represent the metadata of a key
Gilles Peskine9c640f92019-04-28 11:36:21 +0200103 * object. Metadata that can be stored in attributes includes:
104 * - The location of the key in storage, indicated by its key identifier
105 * and its lifetime.
106 * - The key's policy, comprising usage flags and a specification of
107 * the permitted algorithm(s).
108 * - Information about the key itself: the key type, the key size, and
109 * for some key type additional domain parameters.
110 * - Implementations may define additional attributes.
Gilles Peskine87a5e562019-04-17 12:28:25 +0200111 *
112 * The actual key material is not considered an attribute of a key.
113 * Key attributes do not contain information that is generally considered
114 * highly confidential.
Gilles Peskine20628592019-04-19 19:29:50 +0200115 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200116 * An attribute structure can be a simple data structure where each function
117 * `psa_set_key_xxx` sets a field and the corresponding function
118 * `psa_get_key_xxx` retrieves the value of the corresponding field.
119 * However, implementations may report values that are equivalent to the
120 * original one, but have a different encoding. For example, an
121 * implementation may use a more compact representation for types where
122 * many bit-patterns are invalid or not supported, and store all values
123 * that it does not support as a special marker value. In such an
124 * implementation, after setting an invalid value, the corresponding
125 * get function returns an invalid value which may not be the one that
126 * was originally stored.
127 *
128 * An attribute structure may contain references to auxiliary resources,
129 * for example pointers to allocated memory or indirect references to
130 * pre-calculated values. In order to free such resources, the application
131 * must call psa_reset_key_attributes(). As an exception, calling
132 * psa_reset_key_attributes() on an attribute structure is optional if
133 * the structure has only been modified by the following functions
134 * since it was initialized or last reset with psa_reset_key_attributes():
135 * - psa_make_key_persistent()
136 * - psa_set_key_type()
137 * - psa_set_key_bits()
138 * - psa_set_key_usage_flags()
139 * - psa_set_key_algorithm()
140 *
Gilles Peskine20628592019-04-19 19:29:50 +0200141 * Before calling any function on a key attribute structure, the application
142 * must initialize it by any of the following means:
143 * - Set the structure to all-bits-zero, for example:
144 * \code
145 * psa_key_attributes_t attributes;
146 * memset(&attributes, 0, sizeof(attributes));
147 * \endcode
148 * - Initialize the structure to logical zero values, for example:
149 * \code
150 * psa_key_attributes_t attributes = {0};
151 * \endcode
152 * - Initialize the structure to the initializer #PSA_KEY_ATTRIBUTES_INIT,
153 * for example:
154 * \code
155 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
156 * \endcode
157 * - Assign the result of the function psa_key_attributes_init()
158 * to the structure, for example:
159 * \code
160 * psa_key_attributes_t attributes;
161 * attributes = psa_key_attributes_init();
162 * \endcode
163 *
164 * A freshly initialized attribute structure contains the following
165 * values:
166 *
167 * - lifetime: #PSA_KEY_LIFETIME_VOLATILE.
168 * - key identifier: unspecified.
169 * - type: \c 0, with no domain parameters.
170 * - key size: \c 0.
171 * - usage flags: \c 0.
172 * - algorithm: \c 0.
173 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200174 * A typical sequence to create a key is as follows:
175 * -# Create and initialize an attribute structure.
176 * -# If the key is persistent, call psa_make_key_persistent().
177 * -# Set the key policy with psa_set_key_usage_flags() and
178 * psa_set_key_algorithm().
179 * -# Set the key type with psa_set_key_type(). If the key type requires
180 * domain parameters, call psa_set_key_domain_parameters() instead.
181 * Skip this step if copying an existing key with psa_copy_key().
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100182 * -# When generating a random key with psa_generate_random_key() or deriving a key
183 * with psa_generate_derived_key(), set the desired key size with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200184 * psa_set_key_bits().
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100185 * -# Call a key creation function: psa_import_key(), psa_generate_random_key(),
186 * psa_generate_derived_key() or psa_copy_key(). This function reads
Gilles Peskine1ea5e442019-05-02 20:31:10 +0200187 * the attribute structure, creates a key with these attributes, and
188 * outputs a handle to the newly created key.
189 * -# The attribute structure is now no longer necessary. If you called
Gilles Peskine9c640f92019-04-28 11:36:21 +0200190 * psa_set_key_domain_parameters() earlier, you must call
191 * psa_reset_key_attributes() to free any resources used by the
192 * domain parameters. Otherwise calling psa_reset_key_attributes()
193 * is optional.
Gilles Peskine20628592019-04-19 19:29:50 +0200194 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200195 * A typical sequence to query a key's attributes is as follows:
196 * -# Call psa_get_key_attributes().
197 * -# Call `psa_get_key_xxx` functions to retrieve the attribute(s) that
198 * you are interested in.
199 * -# Call psa_reset_key_attributes() to free any resources that may be
200 * used by the attribute structure.
201 *
202 * Once a key has been created, it is impossible to change its attributes.
Gilles Peskine87a5e562019-04-17 12:28:25 +0200203 */
204typedef struct psa_key_attributes_s psa_key_attributes_t;
205
Gilles Peskine20628592019-04-19 19:29:50 +0200206/** Declare a key as persistent.
207 *
208 * This function does not access storage, it merely fills the attribute
209 * structure with given values. The persistent key will be written to
210 * storage when the attribute structure is passed to a key creation
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100211 * function such as psa_import_key(), psa_generate_random_key(),
212 * psa_generate_derived_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200213 *
214 * This function overwrites any identifier and lifetime values
215 * previously set in \p attributes.
216 *
217 * This function may be declared as `static` (i.e. without external
218 * linkage). This function may be provided as a function-like macro,
219 * but in this case it must evaluate each of its arguments exactly once.
220 *
221 * \param[out] attributes The attribute structure to write to.
222 * \param id The persistent identifier for the key.
223 * \param lifetime The lifetime for the key.
224 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
225 * key will be volatile, and \p id is ignored.
226 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200227static void psa_make_key_persistent(psa_key_attributes_t *attributes,
228 psa_key_id_t id,
229 psa_key_lifetime_t lifetime);
230
Gilles Peskine20628592019-04-19 19:29:50 +0200231/** Retrieve the key identifier from key attributes.
232 *
233 * This function may be declared as `static` (i.e. without external
234 * linkage). This function may be provided as a function-like macro,
235 * but in this case it must evaluate its argument exactly once.
236 *
237 * \param[in] attributes The key attribute structure to query.
238 *
239 * \return The persistent identifier stored in the attribute structure.
240 * This value is unspecified if the attribute structure declares
241 * the key as volatile.
242 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200243static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
244
Gilles Peskine20628592019-04-19 19:29:50 +0200245/** Retrieve the lifetime from key attributes.
246 *
247 * This function may be declared as `static` (i.e. without external
248 * linkage). This function may be provided as a function-like macro,
249 * but in this case it must evaluate its argument exactly once.
250 *
251 * \param[in] attributes The key attribute structure to query.
252 *
253 * \return The lifetime value stored in the attribute structure.
254 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200255static psa_key_lifetime_t psa_get_key_lifetime(
256 const psa_key_attributes_t *attributes);
257
Gilles Peskine20628592019-04-19 19:29:50 +0200258/** Declare usage flags for a key.
259 *
260 * Usage flags are part of a key's usage policy. They encode what
261 * kind of operations are permitted on the key. For more details,
262 * refer to the documentation of the type #psa_key_usage_t.
263 *
264 * This function overwrites any usage flags
265 * previously set in \p attributes.
266 *
267 * This function may be declared as `static` (i.e. without external
268 * linkage). This function may be provided as a function-like macro,
269 * but in this case it must evaluate each of its arguments exactly once.
270 *
271 * \param[out] attributes The attribute structure to write to.
272 * \param usage_flags The usage flags to write.
273 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200274static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
275 psa_key_usage_t usage_flags);
276
Gilles Peskine20628592019-04-19 19:29:50 +0200277/** Retrieve the usage flags from key attributes.
278 *
279 * This function may be declared as `static` (i.e. without external
280 * linkage). This function may be provided as a function-like macro,
281 * but in this case it must evaluate its argument exactly once.
282 *
283 * \param[in] attributes The key attribute structure to query.
284 *
285 * \return The usage flags stored in the attribute structure.
286 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200287static psa_key_usage_t psa_get_key_usage_flags(
288 const psa_key_attributes_t *attributes);
289
Gilles Peskine20628592019-04-19 19:29:50 +0200290/** Declare the permitted algorithm policy for a key.
291 *
292 * The permitted algorithm policy of a key encodes which algorithm or
293 * algorithms are permitted to be used with this key.
294 *
295 * This function overwrites any algorithm policy
296 * previously set in \p attributes.
297 *
298 * This function may be declared as `static` (i.e. without external
299 * linkage). This function may be provided as a function-like macro,
300 * but in this case it must evaluate each of its arguments exactly once.
301 *
302 * \param[out] attributes The attribute structure to write to.
303 * \param alg The permitted algorithm policy to write.
304 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200305static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
306 psa_algorithm_t alg);
307
Gilles Peskine20628592019-04-19 19:29:50 +0200308/** Retrieve the algorithm policy from key attributes.
309 *
310 * This function may be declared as `static` (i.e. without external
311 * linkage). This function may be provided as a function-like macro,
312 * but in this case it must evaluate its argument exactly once.
313 *
314 * \param[in] attributes The key attribute structure to query.
315 *
316 * \return The algorithm stored in the attribute structure.
317 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200318static psa_algorithm_t psa_get_key_algorithm(
319 const psa_key_attributes_t *attributes);
320
Gilles Peskine20628592019-04-19 19:29:50 +0200321/** Declare the type of a key.
322 *
323 * If a type requires domain parameters, you must call
324 * psa_set_key_domain_parameters() instead of this function.
325 *
326 * This function overwrites any key type and domain parameters
327 * previously set in \p attributes.
328 *
329 * This function may be declared as `static` (i.e. without external
330 * linkage). This function may be provided as a function-like macro,
331 * but in this case it must evaluate each of its arguments exactly once.
332 *
333 * \param[out] attributes The attribute structure to write to.
334 * \param type The key type to write.
335 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200336static void psa_set_key_type(psa_key_attributes_t *attributes,
337 psa_key_type_t type);
338
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200339/** Declare the size of a key.
340 *
341 * This function overwrites any key size previously set in \p attributes.
342 *
343 * This function may be declared as `static` (i.e. without external
344 * linkage). This function may be provided as a function-like macro,
345 * but in this case it must evaluate each of its arguments exactly once.
346 *
347 * \param[out] attributes The attribute structure to write to.
348 * \param bits The key size in bits.
349 */
350static void psa_set_key_bits(psa_key_attributes_t *attributes,
351 size_t bits);
352
Gilles Peskine20628592019-04-19 19:29:50 +0200353/** Retrieve the key type from key attributes.
354 *
355 * This function may be declared as `static` (i.e. without external
356 * linkage). This function may be provided as a function-like macro,
357 * but in this case it must evaluate its argument exactly once.
358 *
359 * \param[in] attributes The key attribute structure to query.
360 *
361 * \return The key type stored in the attribute structure.
362 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200363static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
364
Gilles Peskine20628592019-04-19 19:29:50 +0200365/** Retrieve the key size from key attributes.
366 *
367 * This function may be declared as `static` (i.e. without external
368 * linkage). This function may be provided as a function-like macro,
369 * but in this case it must evaluate its argument exactly once.
370 *
371 * \param[in] attributes The key attribute structure to query.
372 *
373 * \return The key size stored in the attribute structure, in bits.
374 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200375static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
376
Gilles Peskineb699f072019-04-26 16:06:02 +0200377/**
378 * \brief Set domain parameters for a key.
379 *
380 * Some key types require additional domain parameters in addition to
381 * the key type identifier and the key size.
382 * The format for the required domain parameters varies by the key type.
383 *
Gilles Peskinee56e8782019-04-26 17:34:02 +0200384 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEYPAIR),
385 * the domain parameter data consists of the public exponent,
Gilles Peskineb699f072019-04-26 16:06:02 +0200386 * represented as a big-endian integer with no leading zeros.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200387 * This information is used when generating an RSA key pair.
Gilles Peskineb699f072019-04-26 16:06:02 +0200388 * When importing a key, the public exponent is read from the imported
389 * key data and the exponent recorded in the attribute structure is ignored.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200390 * As an exception, the public exponent 65537 is represented by an empty
391 * byte string.
392 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEYPAIR),
Gilles Peskineb699f072019-04-26 16:06:02 +0200393 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
394 * ```
395 * Dss-Parms ::= SEQUENCE {
396 * p INTEGER,
397 * q INTEGER,
398 * g INTEGER
399 * }
400 * ```
Gilles Peskinee56e8782019-04-26 17:34:02 +0200401 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY or
402 * #PSA_KEY_TYPE_DH_KEYPAIR), the
Gilles Peskineb699f072019-04-26 16:06:02 +0200403 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
404 * ```
405 * DomainParameters ::= SEQUENCE {
406 * p INTEGER, -- odd prime, p=jq +1
407 * g INTEGER, -- generator, g
408 * q INTEGER, -- factor of p-1
409 * j INTEGER OPTIONAL, -- subgroup factor
410 * validationParms ValidationParms OPTIONAL
411 * }
412 * ValidationParms ::= SEQUENCE {
413 * seed BIT STRING,
414 * pgenCounter INTEGER
415 * }
416 * ```
417 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200418 * \note This function may allocate memory or other resources.
419 * Once you have called this function on an attribute structure,
420 * you must call psa_reset_key_attributes() to free these resources.
421 *
Gilles Peskineb699f072019-04-26 16:06:02 +0200422 * \param[in,out] attributes Attribute structure where the specified domain
423 * parameters will be stored.
424 * If this function fails, the content of
425 * \p attributes is not modified.
426 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
427 * \param[in] data Buffer containing the key domain parameters.
428 * The content of this buffer is interpreted
429 * according to \p type as described above.
430 * \param data_length Size of the \p data buffer in bytes.
431 *
432 * \retval #PSA_SUCCESS
433 * \retval #PSA_ERROR_INVALID_ARGUMENT
434 * \retval #PSA_ERROR_NOT_SUPPORTED
435 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
436 */
437psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
438 psa_key_type_t type,
439 const uint8_t *data,
440 size_t data_length);
441
442/**
443 * \brief Get domain parameters for a key.
444 *
445 * Get the domain parameters for a key with this function, if any. The format
446 * of the domain parameters written to \p data is specified in the
447 * documentation for psa_set_key_domain_parameters().
448 *
449 * \param[in] attributes The key attribute structure to query.
450 * \param[out] data On success, the key domain parameters.
451 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineaa02c172019-04-28 11:44:17 +0200452 * The buffer is guaranteed to be large
453 * enough if its size in bytes is at least
454 * the value given by
455 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
Gilles Peskineb699f072019-04-26 16:06:02 +0200456 * \param[out] data_length On success, the number of bytes
457 * that make up the key domain parameters data.
458 *
459 * \retval #PSA_SUCCESS
460 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
461 */
462psa_status_t psa_get_key_domain_parameters(
463 const psa_key_attributes_t *attributes,
464 uint8_t *data,
465 size_t data_size,
466 size_t *data_length);
467
Gilles Peskine20628592019-04-19 19:29:50 +0200468/** Retrieve the attributes of a key.
469 *
470 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200471 * psa_reset_key_attributes(). It then copies the attributes of
472 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200473 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200474 * \note This function may allocate memory or other resources.
475 * Once you have called this function on an attribute structure,
476 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200477 *
Gilles Peskine20628592019-04-19 19:29:50 +0200478 * \param[in] handle Handle to the key to query.
479 * \param[in,out] attributes On success, the attributes of the key.
480 * On failure, equivalent to a
481 * freshly-initialized structure.
482 *
483 * \retval #PSA_SUCCESS
484 * \retval #PSA_ERROR_INVALID_HANDLE
485 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
486 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
487 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200488psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
489 psa_key_attributes_t *attributes);
490
Gilles Peskine20628592019-04-19 19:29:50 +0200491/** Reset a key attribute structure to a freshly initialized state.
492 *
493 * You must initialize the attribute structure as described in the
494 * documentation of the type #psa_key_attributes_t before calling this
495 * function. Once the structure has been initialized, you may call this
496 * function at any time.
497 *
498 * This function frees any auxiliary resources that the structure
499 * may contain.
500 *
501 * \param[in,out] attributes The attribute structure to reset.
502 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200503void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200504
Gilles Peskine87a5e562019-04-17 12:28:25 +0200505/**@}*/
506
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100507/** \defgroup key_management Key management
508 * @{
509 */
510
Gilles Peskinef535eb22018-11-30 14:08:36 +0100511/** Open a handle to an existing persistent key.
512 *
513 * Open a handle to a key which was previously created with psa_create_key().
514 *
515 * \param lifetime The lifetime of the key. This designates a storage
516 * area where the key material is stored. This must not
517 * be #PSA_KEY_LIFETIME_VOLATILE.
518 * \param id The persistent identifier of the key.
519 * \param[out] handle On success, a handle to a key slot which contains
520 * the data and metadata loaded from the specified
521 * persistent location.
522 *
523 * \retval #PSA_SUCCESS
524 * Success. The application can now use the value of `*handle`
525 * to access the newly allocated key slot.
526 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200527 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100528 * \retval #PSA_ERROR_INVALID_ARGUMENT
529 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
530 * \retval #PSA_ERROR_INVALID_ARGUMENT
531 * \p id is invalid for the specified lifetime.
532 * \retval #PSA_ERROR_NOT_SUPPORTED
533 * \p lifetime is not supported.
534 * \retval #PSA_ERROR_NOT_PERMITTED
535 * The specified key exists, but the application does not have the
536 * permission to access it. Note that this specification does not
537 * define any way to create such a key, but it may be possible
538 * through implementation-specific means.
539 */
540psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
541 psa_key_id_t id,
542 psa_key_handle_t *handle);
543
Gilles Peskinef535eb22018-11-30 14:08:36 +0100544/** Close a key handle.
545 *
546 * If the handle designates a volatile key, destroy the key material and
547 * free all associated resources, just like psa_destroy_key().
548 *
549 * If the handle designates a persistent key, free all resources associated
550 * with the key in volatile memory. The key slot in persistent storage is
551 * not affected and can be opened again later with psa_open_key().
552 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100553 * If the key is currently in use in a multipart operation,
554 * the multipart operation is aborted.
555 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100556 * \param handle The key handle to close.
557 *
558 * \retval #PSA_SUCCESS
559 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100560 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100561 */
562psa_status_t psa_close_key(psa_key_handle_t handle);
563
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100564/**@}*/
565
566/** \defgroup import_export Key import and export
567 * @{
568 */
569
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100570/**
571 * \brief Import a key in binary format.
572 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100573 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100574 * documentation of psa_export_public_key() for the format of public keys
575 * and to the documentation of psa_export_key() for the format for
576 * other key types.
577 *
578 * This specification supports a single format for each key type.
579 * Implementations may support other formats as long as the standard
580 * format is supported. Implementations that support other formats
581 * should ensure that the formats are clearly unambiguous so as to
582 * minimize the risk that an invalid input is accidentally interpreted
583 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100584 *
Gilles Peskine20628592019-04-19 19:29:50 +0200585 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200586 * The key size is always determined from the
587 * \p data buffer.
588 * If the key size in \p attributes is nonzero,
589 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200590 * \param[out] handle On success, a handle to the newly created key.
591 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100592 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine20628592019-04-19 19:29:50 +0200593 * buffer is interpreted according to the type and,
594 * if applicable, domain parameters declared in
595 * \p attributes.
596 * All implementations must support at least the format
597 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100598 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200599 * the chosen type. Implementations may allow other
600 * formats, but should be conservative: implementations
601 * should err on the side of rejecting content if it
602 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200603 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100604 *
Gilles Peskine28538492018-07-11 17:34:00 +0200605 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100606 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100607 * If the key is persistent, the key material and the key's metadata
608 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200609 * \retval #PSA_ERROR_ALREADY_EXISTS
610 * This is an attempt to create a persistent key, and there is
611 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200612 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200613 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200614 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200615 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200616 * The key attributes, as a whole, are invalid.
617 * \retval #PSA_ERROR_INVALID_ARGUMENT
618 * The key data is not correctly formatted.
619 * \retval #PSA_ERROR_INVALID_ARGUMENT
620 * The size in \p attributes is nonzero and does not match the size
621 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200622 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
623 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
624 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100625 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200626 * \retval #PSA_ERROR_HARDWARE_FAILURE
627 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300628 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300629 * The library has not been previously initialized by psa_crypto_init().
630 * It is implementation-dependent whether a failure to initialize
631 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100632 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200633psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
634 psa_key_handle_t *handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100635 const uint8_t *data,
636 size_t data_length);
637
638/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100639 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200640 *
641 * This function destroys the content of the key slot from both volatile
642 * memory and, if applicable, non-volatile storage. Implementations shall
643 * make a best effort to ensure that any previous content of the slot is
644 * unrecoverable.
645 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100646 * This function also erases any metadata such as policies and frees all
647 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200648 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100649 * If the key is currently in use in a multipart operation,
650 * the multipart operation is aborted.
651 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100652 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100653 *
Gilles Peskine28538492018-07-11 17:34:00 +0200654 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200655 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200656 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200657 * The slot holds content and cannot be erased because it is
658 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100659 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200660 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200661 * There was an failure in communication with the cryptoprocessor.
662 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200663 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200664 * The storage is corrupted. Implementations shall make a best effort
665 * to erase key material even in this stage, however applications
666 * should be aware that it may be impossible to guarantee that the
667 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200668 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200669 * An unexpected condition which is not a storage corruption or
670 * a communication failure occurred. The cryptoprocessor may have
671 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300672 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300673 * The library has not been previously initialized by psa_crypto_init().
674 * It is implementation-dependent whether a failure to initialize
675 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100677psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678
679/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100680 * \brief Export a key in binary format.
681 *
682 * The output of this function can be passed to psa_import_key() to
683 * create an equivalent object.
684 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100685 * If the implementation of psa_import_key() supports other formats
686 * beyond the format specified here, the output from psa_export_key()
687 * must use the representation specified here, not the original
688 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100689 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100690 * For standard key types, the output format is as follows:
691 *
692 * - For symmetric keys (including MAC keys), the format is the
693 * raw bytes of the key.
694 * - For DES, the key data consists of 8 bytes. The parity bits must be
695 * correct.
696 * - For Triple-DES, the format is the concatenation of the
697 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100698 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200699 * is the non-encrypted DER encoding of the representation defined by
700 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
701 * ```
702 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200703 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200704 * modulus INTEGER, -- n
705 * publicExponent INTEGER, -- e
706 * privateExponent INTEGER, -- d
707 * prime1 INTEGER, -- p
708 * prime2 INTEGER, -- q
709 * exponent1 INTEGER, -- d mod (p-1)
710 * exponent2 INTEGER, -- d mod (q-1)
711 * coefficient INTEGER, -- (inverse of q) mod p
712 * }
713 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000714 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
715 * representation of the private key `x` as a big-endian byte string. The
716 * length of the byte string is the private key size in bytes (leading zeroes
717 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200718 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100719 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100720 * a representation of the private value as a `ceiling(m/8)`-byte string
721 * where `m` is the bit size associated with the curve, i.e. the bit size
722 * of the order of the curve's coordinate field. This byte string is
723 * in little-endian order for Montgomery curves (curve types
724 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
725 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
726 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100727 * This is the content of the `privateKey` field of the `ECPrivateKey`
728 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000729 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
730 * format is the representation of the private key `x` as a big-endian byte
731 * string. The length of the byte string is the private key size in bytes
732 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200733 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
734 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100735 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200736 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
737 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100738 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200739 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200740 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200741 * \param[out] data_length On success, the number of bytes
742 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100743 *
Gilles Peskine28538492018-07-11 17:34:00 +0200744 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100745 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200746 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200747 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200748 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100749 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200750 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
751 * The size of the \p data buffer is too small. You can determine a
752 * sufficient buffer size by calling
753 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
754 * where \c type is the key type
755 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200756 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
757 * \retval #PSA_ERROR_HARDWARE_FAILURE
758 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300759 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300760 * The library has not been previously initialized by psa_crypto_init().
761 * It is implementation-dependent whether a failure to initialize
762 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100763 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100764psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100765 uint8_t *data,
766 size_t data_size,
767 size_t *data_length);
768
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100769/**
770 * \brief Export a public key or the public part of a key pair in binary format.
771 *
772 * The output of this function can be passed to psa_import_key() to
773 * create an object that is equivalent to the public key.
774 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000775 * This specification supports a single format for each key type.
776 * Implementations may support other formats as long as the standard
777 * format is supported. Implementations that support other formats
778 * should ensure that the formats are clearly unambiguous so as to
779 * minimize the risk that an invalid input is accidentally interpreted
780 * according to a different format.
781 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000782 * For standard key types, the output format is as follows:
783 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
784 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
785 * ```
786 * RSAPublicKey ::= SEQUENCE {
787 * modulus INTEGER, -- n
788 * publicExponent INTEGER } -- e
789 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000790 * - For elliptic curve public keys (key types for which
791 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
792 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
793 * Let `m` be the bit size associated with the curve, i.e. the bit size of
794 * `q` for a curve over `F_q`. The representation consists of:
795 * - The byte 0x04;
796 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
797 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000798 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
799 * representation of the public key `y = g^x mod p` as a big-endian byte
800 * string. The length of the byte string is the length of the base prime `p`
801 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000802 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
803 * the format is the representation of the public key `y = g^x mod p` as a
804 * big-endian byte string. The length of the byte string is the length of the
805 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100806 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200807 * Exporting a public key object or the public part of a key pair is
808 * always permitted, regardless of the key's usage flags.
809 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100810 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200811 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200812 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200813 * \param[out] data_length On success, the number of bytes
814 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100815 *
Gilles Peskine28538492018-07-11 17:34:00 +0200816 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100817 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200818 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200819 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200820 * The key is neither a public key nor a key pair.
821 * \retval #PSA_ERROR_NOT_SUPPORTED
822 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
823 * The size of the \p data buffer is too small. You can determine a
824 * sufficient buffer size by calling
825 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
826 * where \c type is the key type
827 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200828 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
829 * \retval #PSA_ERROR_HARDWARE_FAILURE
830 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300831 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300832 * The library has not been previously initialized by psa_crypto_init().
833 * It is implementation-dependent whether a failure to initialize
834 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100835 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100836psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100837 uint8_t *data,
838 size_t data_size,
839 size_t *data_length);
840
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100841/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100842 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100843 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000844 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100845 * This function is primarily useful to copy a key from one location
846 * to another, since it populates a key using the material from
847 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200848 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100849 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100850 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100851 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100852 *
Gilles Peskine20628592019-04-19 19:29:50 +0200853 * The resulting key may only be used in a way that conforms to
854 * both the policy of the original key and the policy specified in
855 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100856 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200857 * usage flags on the source policy and the usage flags in \p attributes.
858 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100859 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200860 * - If either of the policies allows an algorithm and the other policy
861 * allows a wildcard-based algorithm policy that includes this algorithm,
862 * the resulting key allows the same algorithm.
863 * - If the policies do not allow any algorithm in common, this function
864 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200865 *
Gilles Peskine20628592019-04-19 19:29:50 +0200866 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100867 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200868 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100869 * \param source_handle The key to copy. It must be a handle to an
870 * occupied slot.
Gilles Peskine20628592019-04-19 19:29:50 +0200871 * \param[in] attributes The attributes for the new key.
872 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200873 * - The key type and size may be 0. If either is
874 * nonzero, it must match the corresponding
875 * attribute of the source key.
876 * - If \p attributes contains domain parameters,
877 * they must match the domain parameters of
878 * the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200879 * - The key location (the lifetime and, for
880 * persistent keys, the key identifier) is
881 * used directly.
882 * - The policy constraints (usage flags and
883 * algorithm policy) are combined from
884 * the source key and \p attributes so that
885 * both sets of restrictions apply, as
886 * described in the documentation of this function.
887 * \param[out] target_handle On success, a handle to the newly created key.
888 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200889 *
890 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100891 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200892 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200893 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200894 * This is an attempt to create a persistent key, and there is
895 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200896 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200897 * The lifetime or identifier in \p attributes are invalid.
898 * \retval #PSA_ERROR_INVALID_ARGUMENT
899 * The policy constraints on the source and specified in
900 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200901 * \retval #PSA_ERROR_INVALID_ARGUMENT
902 * \p attributes specifies a key type, domain parameters or key size
903 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100904 * \retval #PSA_ERROR_NOT_PERMITTED
905 * The source key is not exportable and its lifetime does not
906 * allow copying it to the target's lifetime.
907 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
908 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200909 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
910 * \retval #PSA_ERROR_HARDWARE_FAILURE
911 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100912 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100913psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200914 const psa_key_attributes_t *attributes,
915 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100916
917/**@}*/
918
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100919/** \defgroup hash Message digests
920 * @{
921 */
922
Gilles Peskine69647a42019-01-14 20:18:12 +0100923/** Calculate the hash (digest) of a message.
924 *
925 * \note To verify the hash of a message against an
926 * expected value, use psa_hash_compare() instead.
927 *
928 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
929 * such that #PSA_ALG_IS_HASH(\p alg) is true).
930 * \param[in] input Buffer containing the message to hash.
931 * \param input_length Size of the \p input buffer in bytes.
932 * \param[out] hash Buffer where the hash is to be written.
933 * \param hash_size Size of the \p hash buffer in bytes.
934 * \param[out] hash_length On success, the number of bytes
935 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100936 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100937 *
938 * \retval #PSA_SUCCESS
939 * Success.
940 * \retval #PSA_ERROR_NOT_SUPPORTED
941 * \p alg is not supported or is not a hash algorithm.
942 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
943 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
944 * \retval #PSA_ERROR_HARDWARE_FAILURE
945 * \retval #PSA_ERROR_TAMPERING_DETECTED
946 */
947psa_status_t psa_hash_compute(psa_algorithm_t alg,
948 const uint8_t *input,
949 size_t input_length,
950 uint8_t *hash,
951 size_t hash_size,
952 size_t *hash_length);
953
954/** Calculate the hash (digest) of a message and compare it with a
955 * reference value.
956 *
957 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
958 * such that #PSA_ALG_IS_HASH(\p alg) is true).
959 * \param[in] input Buffer containing the message to hash.
960 * \param input_length Size of the \p input buffer in bytes.
961 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100962 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100963 *
964 * \retval #PSA_SUCCESS
965 * The expected hash is identical to the actual hash of the input.
966 * \retval #PSA_ERROR_INVALID_SIGNATURE
967 * The hash of the message was calculated successfully, but it
968 * differs from the expected hash.
969 * \retval #PSA_ERROR_NOT_SUPPORTED
970 * \p alg is not supported or is not a hash algorithm.
971 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
972 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
973 * \retval #PSA_ERROR_HARDWARE_FAILURE
974 * \retval #PSA_ERROR_TAMPERING_DETECTED
975 */
976psa_status_t psa_hash_compare(psa_algorithm_t alg,
977 const uint8_t *input,
978 size_t input_length,
979 const uint8_t *hash,
980 const size_t hash_length);
981
Gilles Peskine308b91d2018-02-08 09:47:44 +0100982/** The type of the state data structure for multipart hash operations.
983 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000984 * Before calling any function on a hash operation object, the application must
985 * initialize it by any of the following means:
986 * - Set the structure to all-bits-zero, for example:
987 * \code
988 * psa_hash_operation_t operation;
989 * memset(&operation, 0, sizeof(operation));
990 * \endcode
991 * - Initialize the structure to logical zero values, for example:
992 * \code
993 * psa_hash_operation_t operation = {0};
994 * \endcode
995 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
996 * for example:
997 * \code
998 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
999 * \endcode
1000 * - Assign the result of the function psa_hash_operation_init()
1001 * to the structure, for example:
1002 * \code
1003 * psa_hash_operation_t operation;
1004 * operation = psa_hash_operation_init();
1005 * \endcode
1006 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001007 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001008 * make any assumptions about the content of this structure except
1009 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001010typedef struct psa_hash_operation_s psa_hash_operation_t;
1011
Jaeden Amero6a25b412019-01-04 11:47:44 +00001012/** \def PSA_HASH_OPERATION_INIT
1013 *
1014 * This macro returns a suitable initializer for a hash operation object
1015 * of type #psa_hash_operation_t.
1016 */
1017#ifdef __DOXYGEN_ONLY__
1018/* This is an example definition for documentation purposes.
1019 * Implementations should define a suitable value in `crypto_struct.h`.
1020 */
1021#define PSA_HASH_OPERATION_INIT {0}
1022#endif
1023
1024/** Return an initial value for a hash operation object.
1025 */
1026static psa_hash_operation_t psa_hash_operation_init(void);
1027
Gilles Peskinef45adda2019-01-14 18:29:18 +01001028/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001029 *
1030 * The sequence of operations to calculate a hash (message digest)
1031 * is as follows:
1032 * -# Allocate an operation object which will be passed to all the functions
1033 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +00001034 * -# Initialize the operation object with one of the methods described in the
1035 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001036 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001037 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001038 * of the message each time. The hash that is calculated is the hash
1039 * of the concatenation of these messages in order.
1040 * -# To calculate the hash, call psa_hash_finish().
1041 * To compare the hash with an expected value, call psa_hash_verify().
1042 *
1043 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +00001044 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001045 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001046 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001047 * eventually terminate the operation. The following events terminate an
1048 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001049 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001050 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001051 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001052 * \param[in,out] operation The operation object to set up. It must have
1053 * been initialized as per the documentation for
1054 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001055 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1056 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001057 *
Gilles Peskine28538492018-07-11 17:34:00 +02001058 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001059 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001060 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001061 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001062 * \retval #PSA_ERROR_BAD_STATE
1063 * The operation state is not valid (already set up and not
1064 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001065 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1066 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1067 * \retval #PSA_ERROR_HARDWARE_FAILURE
1068 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001069 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001070psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001071 psa_algorithm_t alg);
1072
Gilles Peskine308b91d2018-02-08 09:47:44 +01001073/** Add a message fragment to a multipart hash operation.
1074 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001075 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001076 *
1077 * If this function returns an error status, the operation becomes inactive.
1078 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001079 * \param[in,out] operation Active hash operation.
1080 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001081 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001082 *
Gilles Peskine28538492018-07-11 17:34:00 +02001083 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001084 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001085 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001086 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001087 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1088 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1089 * \retval #PSA_ERROR_HARDWARE_FAILURE
1090 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001091 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001092psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1093 const uint8_t *input,
1094 size_t input_length);
1095
Gilles Peskine308b91d2018-02-08 09:47:44 +01001096/** Finish the calculation of the hash of a message.
1097 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001098 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001099 * This function calculates the hash of the message formed by concatenating
1100 * the inputs passed to preceding calls to psa_hash_update().
1101 *
1102 * When this function returns, the operation becomes inactive.
1103 *
1104 * \warning Applications should not call this function if they expect
1105 * a specific value for the hash. Call psa_hash_verify() instead.
1106 * Beware that comparing integrity or authenticity data such as
1107 * hash values with a function such as \c memcmp is risky
1108 * because the time taken by the comparison may leak information
1109 * about the hashed data which could allow an attacker to guess
1110 * a valid hash and thereby bypass security controls.
1111 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001112 * \param[in,out] operation Active hash operation.
1113 * \param[out] hash Buffer where the hash is to be written.
1114 * \param hash_size Size of the \p hash buffer in bytes.
1115 * \param[out] hash_length On success, the number of bytes
1116 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001117 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001118 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001119 *
Gilles Peskine28538492018-07-11 17:34:00 +02001120 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001121 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001122 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001123 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001124 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001125 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001126 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001127 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001128 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1129 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1130 * \retval #PSA_ERROR_HARDWARE_FAILURE
1131 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001132 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001133psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1134 uint8_t *hash,
1135 size_t hash_size,
1136 size_t *hash_length);
1137
Gilles Peskine308b91d2018-02-08 09:47:44 +01001138/** Finish the calculation of the hash of a message and compare it with
1139 * an expected value.
1140 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001141 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001142 * This function calculates the hash of the message formed by concatenating
1143 * the inputs passed to preceding calls to psa_hash_update(). It then
1144 * compares the calculated hash with the expected hash passed as a
1145 * parameter to this function.
1146 *
1147 * When this function returns, the operation becomes inactive.
1148 *
Gilles Peskine19067982018-03-20 17:54:53 +01001149 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001150 * comparison between the actual hash and the expected hash is performed
1151 * in constant time.
1152 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001153 * \param[in,out] operation Active hash operation.
1154 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001155 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001156 *
Gilles Peskine28538492018-07-11 17:34:00 +02001157 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001158 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001159 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001160 * The hash of the message was calculated successfully, but it
1161 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001162 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001163 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001164 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1165 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1166 * \retval #PSA_ERROR_HARDWARE_FAILURE
1167 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001168 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001169psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1170 const uint8_t *hash,
1171 size_t hash_length);
1172
Gilles Peskine308b91d2018-02-08 09:47:44 +01001173/** Abort a hash operation.
1174 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001175 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001176 * \p operation structure itself. Once aborted, the operation object
1177 * can be reused for another operation by calling
1178 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001179 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001180 * You may call this function any time after the operation object has
1181 * been initialized by any of the following methods:
1182 * - A call to psa_hash_setup(), whether it succeeds or not.
1183 * - Initializing the \c struct to all-bits-zero.
1184 * - Initializing the \c struct to logical zeros, e.g.
1185 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001186 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001187 * In particular, calling psa_hash_abort() after the operation has been
1188 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1189 * psa_hash_verify() is safe and has no effect.
1190 *
1191 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001192 *
Gilles Peskine28538492018-07-11 17:34:00 +02001193 * \retval #PSA_SUCCESS
1194 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001195 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001196 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1197 * \retval #PSA_ERROR_HARDWARE_FAILURE
1198 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001199 */
1200psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001201
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001202/** Clone a hash operation.
1203 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001204 * This function copies the state of an ongoing hash operation to
1205 * a new operation object. In other words, this function is equivalent
1206 * to calling psa_hash_setup() on \p target_operation with the same
1207 * algorithm that \p source_operation was set up for, then
1208 * psa_hash_update() on \p target_operation with the same input that
1209 * that was passed to \p source_operation. After this function returns, the
1210 * two objects are independent, i.e. subsequent calls involving one of
1211 * the objects do not affect the other object.
1212 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001213 * \param[in] source_operation The active hash operation to clone.
1214 * \param[in,out] target_operation The operation object to set up.
1215 * It must be initialized but not active.
1216 *
1217 * \retval #PSA_SUCCESS
1218 * \retval #PSA_ERROR_BAD_STATE
1219 * \p source_operation is not an active hash operation.
1220 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001221 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001222 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1223 * \retval #PSA_ERROR_HARDWARE_FAILURE
1224 * \retval #PSA_ERROR_TAMPERING_DETECTED
1225 */
1226psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1227 psa_hash_operation_t *target_operation);
1228
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001229/**@}*/
1230
Gilles Peskine8c9def32018-02-08 10:02:12 +01001231/** \defgroup MAC Message authentication codes
1232 * @{
1233 */
1234
Gilles Peskine69647a42019-01-14 20:18:12 +01001235/** Calculate the MAC (message authentication code) of a message.
1236 *
1237 * \note To verify the MAC of a message against an
1238 * expected value, use psa_mac_verify() instead.
1239 * Beware that comparing integrity or authenticity data such as
1240 * MAC values with a function such as \c memcmp is risky
1241 * because the time taken by the comparison may leak information
1242 * about the MAC value which could allow an attacker to guess
1243 * a valid MAC and thereby bypass security controls.
1244 *
1245 * \param handle Handle to the key to use for the operation.
1246 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001247 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001248 * \param[in] input Buffer containing the input message.
1249 * \param input_length Size of the \p input buffer in bytes.
1250 * \param[out] mac Buffer where the MAC value is to be written.
1251 * \param mac_size Size of the \p mac buffer in bytes.
1252 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001253 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001254 *
1255 * \retval #PSA_SUCCESS
1256 * Success.
1257 * \retval #PSA_ERROR_INVALID_HANDLE
1258 * \retval #PSA_ERROR_EMPTY_SLOT
1259 * \retval #PSA_ERROR_NOT_PERMITTED
1260 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001261 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001262 * \retval #PSA_ERROR_NOT_SUPPORTED
1263 * \p alg is not supported or is not a MAC algorithm.
1264 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1265 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1266 * \retval #PSA_ERROR_HARDWARE_FAILURE
1267 * \retval #PSA_ERROR_TAMPERING_DETECTED
1268 * \retval #PSA_ERROR_BAD_STATE
1269 * The library has not been previously initialized by psa_crypto_init().
1270 * It is implementation-dependent whether a failure to initialize
1271 * results in this error code.
1272 */
1273psa_status_t psa_mac_compute(psa_key_handle_t handle,
1274 psa_algorithm_t alg,
1275 const uint8_t *input,
1276 size_t input_length,
1277 uint8_t *mac,
1278 size_t mac_size,
1279 size_t *mac_length);
1280
1281/** Calculate the MAC of a message and compare it with a reference value.
1282 *
1283 * \param handle Handle to the key to use for the operation.
1284 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001285 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001286 * \param[in] input Buffer containing the input message.
1287 * \param input_length Size of the \p input buffer in bytes.
1288 * \param[out] mac Buffer containing the expected MAC value.
1289 * \param mac_length Size of the \p mac buffer in bytes.
1290 *
1291 * \retval #PSA_SUCCESS
1292 * The expected MAC is identical to the actual MAC of the input.
1293 * \retval #PSA_ERROR_INVALID_SIGNATURE
1294 * The MAC of the message was calculated successfully, but it
1295 * differs from the expected value.
1296 * \retval #PSA_ERROR_INVALID_HANDLE
1297 * \retval #PSA_ERROR_EMPTY_SLOT
1298 * \retval #PSA_ERROR_NOT_PERMITTED
1299 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001300 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001301 * \retval #PSA_ERROR_NOT_SUPPORTED
1302 * \p alg is not supported or is not a MAC algorithm.
1303 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1304 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1305 * \retval #PSA_ERROR_HARDWARE_FAILURE
1306 * \retval #PSA_ERROR_TAMPERING_DETECTED
1307 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001308psa_status_t psa_mac_verify(psa_key_handle_t handle,
1309 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001310 const uint8_t *input,
1311 size_t input_length,
1312 const uint8_t *mac,
1313 const size_t mac_length);
1314
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001315/** The type of the state data structure for multipart MAC operations.
1316 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001317 * Before calling any function on a MAC operation object, the application must
1318 * initialize it by any of the following means:
1319 * - Set the structure to all-bits-zero, for example:
1320 * \code
1321 * psa_mac_operation_t operation;
1322 * memset(&operation, 0, sizeof(operation));
1323 * \endcode
1324 * - Initialize the structure to logical zero values, for example:
1325 * \code
1326 * psa_mac_operation_t operation = {0};
1327 * \endcode
1328 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1329 * for example:
1330 * \code
1331 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1332 * \endcode
1333 * - Assign the result of the function psa_mac_operation_init()
1334 * to the structure, for example:
1335 * \code
1336 * psa_mac_operation_t operation;
1337 * operation = psa_mac_operation_init();
1338 * \endcode
1339 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001340 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001341 * make any assumptions about the content of this structure except
1342 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001343typedef struct psa_mac_operation_s psa_mac_operation_t;
1344
Jaeden Amero769ce272019-01-04 11:48:03 +00001345/** \def PSA_MAC_OPERATION_INIT
1346 *
1347 * This macro returns a suitable initializer for a MAC operation object of type
1348 * #psa_mac_operation_t.
1349 */
1350#ifdef __DOXYGEN_ONLY__
1351/* This is an example definition for documentation purposes.
1352 * Implementations should define a suitable value in `crypto_struct.h`.
1353 */
1354#define PSA_MAC_OPERATION_INIT {0}
1355#endif
1356
1357/** Return an initial value for a MAC operation object.
1358 */
1359static psa_mac_operation_t psa_mac_operation_init(void);
1360
Gilles Peskinef45adda2019-01-14 18:29:18 +01001361/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001362 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001363 * This function sets up the calculation of the MAC
1364 * (message authentication code) of a byte string.
1365 * To verify the MAC of a message against an
1366 * expected value, use psa_mac_verify_setup() instead.
1367 *
1368 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001369 * -# Allocate an operation object which will be passed to all the functions
1370 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001371 * -# Initialize the operation object with one of the methods described in the
1372 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001373 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001374 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1375 * of the message each time. The MAC that is calculated is the MAC
1376 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001377 * -# At the end of the message, call psa_mac_sign_finish() to finish
1378 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001379 *
1380 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001381 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001382 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001383 * After a successful call to psa_mac_sign_setup(), the application must
1384 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001385 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001386 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001387 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001388 * \param[in,out] operation The operation object to set up. It must have
1389 * been initialized as per the documentation for
1390 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001391 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001392 * It must remain valid until the operation
1393 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001394 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001395 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001396 *
Gilles Peskine28538492018-07-11 17:34:00 +02001397 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001398 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001399 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001400 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001401 * \retval #PSA_ERROR_NOT_PERMITTED
1402 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001403 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001404 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001405 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001406 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1407 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1408 * \retval #PSA_ERROR_HARDWARE_FAILURE
1409 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001410 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001411 * The operation state is not valid (already set up and not
1412 * subsequently completed).
1413 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001414 * The library has not been previously initialized by psa_crypto_init().
1415 * It is implementation-dependent whether a failure to initialize
1416 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001417 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001418psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001419 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001420 psa_algorithm_t alg);
1421
Gilles Peskinef45adda2019-01-14 18:29:18 +01001422/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001423 *
1424 * This function sets up the verification of the MAC
1425 * (message authentication code) of a byte string against an expected value.
1426 *
1427 * The sequence of operations to verify a MAC is as follows:
1428 * -# Allocate an operation object which will be passed to all the functions
1429 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001430 * -# Initialize the operation object with one of the methods described in the
1431 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001432 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001433 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1434 * of the message each time. The MAC that is calculated is the MAC
1435 * of the concatenation of these messages in order.
1436 * -# At the end of the message, call psa_mac_verify_finish() to finish
1437 * calculating the actual MAC of the message and verify it against
1438 * the expected value.
1439 *
1440 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001441 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001442 *
1443 * After a successful call to psa_mac_verify_setup(), the application must
1444 * eventually terminate the operation through one of the following methods:
1445 * - A failed call to psa_mac_update().
1446 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1447 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001448 * \param[in,out] operation The operation object to set up. It must have
1449 * been initialized as per the documentation for
1450 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001451 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001452 * It must remain valid until the operation
1453 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001454 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1455 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001456 *
Gilles Peskine28538492018-07-11 17:34:00 +02001457 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001458 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001459 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001460 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001461 * \retval #PSA_ERROR_NOT_PERMITTED
1462 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001463 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001464 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001465 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001466 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1467 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1468 * \retval #PSA_ERROR_HARDWARE_FAILURE
1469 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001470 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001471 * The operation state is not valid (already set up and not
1472 * subsequently completed).
1473 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001474 * The library has not been previously initialized by psa_crypto_init().
1475 * It is implementation-dependent whether a failure to initialize
1476 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001477 */
1478psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001479 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001480 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001481
Gilles Peskinedcd14942018-07-12 00:30:52 +02001482/** Add a message fragment to a multipart MAC operation.
1483 *
1484 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1485 * before calling this function.
1486 *
1487 * If this function returns an error status, the operation becomes inactive.
1488 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001489 * \param[in,out] operation Active MAC operation.
1490 * \param[in] input Buffer containing the message fragment to add to
1491 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001492 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001493 *
1494 * \retval #PSA_SUCCESS
1495 * Success.
1496 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001497 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001498 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1499 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1500 * \retval #PSA_ERROR_HARDWARE_FAILURE
1501 * \retval #PSA_ERROR_TAMPERING_DETECTED
1502 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001503psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1504 const uint8_t *input,
1505 size_t input_length);
1506
Gilles Peskinedcd14942018-07-12 00:30:52 +02001507/** Finish the calculation of the MAC of a message.
1508 *
1509 * The application must call psa_mac_sign_setup() before calling this function.
1510 * This function calculates the MAC of the message formed by concatenating
1511 * the inputs passed to preceding calls to psa_mac_update().
1512 *
1513 * When this function returns, the operation becomes inactive.
1514 *
1515 * \warning Applications should not call this function if they expect
1516 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1517 * Beware that comparing integrity or authenticity data such as
1518 * MAC values with a function such as \c memcmp is risky
1519 * because the time taken by the comparison may leak information
1520 * about the MAC value which could allow an attacker to guess
1521 * a valid MAC and thereby bypass security controls.
1522 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001523 * \param[in,out] operation Active MAC operation.
1524 * \param[out] mac Buffer where the MAC value is to be written.
1525 * \param mac_size Size of the \p mac buffer in bytes.
1526 * \param[out] mac_length On success, the number of bytes
1527 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001528 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001529 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001530 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001531 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001532 *
1533 * \retval #PSA_SUCCESS
1534 * Success.
1535 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001536 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001537 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001538 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001539 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1540 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1541 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1542 * \retval #PSA_ERROR_HARDWARE_FAILURE
1543 * \retval #PSA_ERROR_TAMPERING_DETECTED
1544 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001545psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1546 uint8_t *mac,
1547 size_t mac_size,
1548 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001549
Gilles Peskinedcd14942018-07-12 00:30:52 +02001550/** Finish the calculation of the MAC of a message and compare it with
1551 * an expected value.
1552 *
1553 * The application must call psa_mac_verify_setup() before calling this function.
1554 * This function calculates the MAC of the message formed by concatenating
1555 * the inputs passed to preceding calls to psa_mac_update(). It then
1556 * compares the calculated MAC with the expected MAC passed as a
1557 * parameter to this function.
1558 *
1559 * When this function returns, the operation becomes inactive.
1560 *
1561 * \note Implementations shall make the best effort to ensure that the
1562 * comparison between the actual MAC and the expected MAC is performed
1563 * in constant time.
1564 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001565 * \param[in,out] operation Active MAC operation.
1566 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001567 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001568 *
1569 * \retval #PSA_SUCCESS
1570 * The expected MAC is identical to the actual MAC of the message.
1571 * \retval #PSA_ERROR_INVALID_SIGNATURE
1572 * The MAC of the message was calculated successfully, but it
1573 * differs from the expected MAC.
1574 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001575 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001576 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1577 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1578 * \retval #PSA_ERROR_HARDWARE_FAILURE
1579 * \retval #PSA_ERROR_TAMPERING_DETECTED
1580 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001581psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1582 const uint8_t *mac,
1583 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001584
Gilles Peskinedcd14942018-07-12 00:30:52 +02001585/** Abort a MAC operation.
1586 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001587 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001588 * \p operation structure itself. Once aborted, the operation object
1589 * can be reused for another operation by calling
1590 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001591 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001592 * You may call this function any time after the operation object has
1593 * been initialized by any of the following methods:
1594 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1595 * it succeeds or not.
1596 * - Initializing the \c struct to all-bits-zero.
1597 * - Initializing the \c struct to logical zeros, e.g.
1598 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001599 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001600 * In particular, calling psa_mac_abort() after the operation has been
1601 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1602 * psa_mac_verify_finish() is safe and has no effect.
1603 *
1604 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001605 *
1606 * \retval #PSA_SUCCESS
1607 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001608 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001609 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1610 * \retval #PSA_ERROR_HARDWARE_FAILURE
1611 * \retval #PSA_ERROR_TAMPERING_DETECTED
1612 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001613psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1614
1615/**@}*/
1616
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001617/** \defgroup cipher Symmetric ciphers
1618 * @{
1619 */
1620
Gilles Peskine69647a42019-01-14 20:18:12 +01001621/** Encrypt a message using a symmetric cipher.
1622 *
1623 * This function encrypts a message with a random IV (initialization
1624 * vector).
1625 *
1626 * \param handle Handle to the key to use for the operation.
1627 * It must remain valid until the operation
1628 * terminates.
1629 * \param alg The cipher algorithm to compute
1630 * (\c PSA_ALG_XXX value such that
1631 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1632 * \param[in] input Buffer containing the message to encrypt.
1633 * \param input_length Size of the \p input buffer in bytes.
1634 * \param[out] output Buffer where the output is to be written.
1635 * The output contains the IV followed by
1636 * the ciphertext proper.
1637 * \param output_size Size of the \p output buffer in bytes.
1638 * \param[out] output_length On success, the number of bytes
1639 * that make up the output.
1640 *
1641 * \retval #PSA_SUCCESS
1642 * Success.
1643 * \retval #PSA_ERROR_INVALID_HANDLE
1644 * \retval #PSA_ERROR_EMPTY_SLOT
1645 * \retval #PSA_ERROR_NOT_PERMITTED
1646 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001647 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001648 * \retval #PSA_ERROR_NOT_SUPPORTED
1649 * \p alg is not supported or is not a cipher algorithm.
1650 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1651 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1652 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1653 * \retval #PSA_ERROR_HARDWARE_FAILURE
1654 * \retval #PSA_ERROR_TAMPERING_DETECTED
1655 */
1656psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1657 psa_algorithm_t alg,
1658 const uint8_t *input,
1659 size_t input_length,
1660 uint8_t *output,
1661 size_t output_size,
1662 size_t *output_length);
1663
1664/** Decrypt a message using a symmetric cipher.
1665 *
1666 * This function decrypts a message encrypted with a symmetric cipher.
1667 *
1668 * \param handle Handle to the key to use for the operation.
1669 * It must remain valid until the operation
1670 * terminates.
1671 * \param alg The cipher algorithm to compute
1672 * (\c PSA_ALG_XXX value such that
1673 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1674 * \param[in] input Buffer containing the message to decrypt.
1675 * This consists of the IV followed by the
1676 * ciphertext proper.
1677 * \param input_length Size of the \p input buffer in bytes.
1678 * \param[out] output Buffer where the plaintext is to be written.
1679 * \param output_size Size of the \p output buffer in bytes.
1680 * \param[out] output_length On success, the number of bytes
1681 * that make up the output.
1682 *
1683 * \retval #PSA_SUCCESS
1684 * Success.
1685 * \retval #PSA_ERROR_INVALID_HANDLE
1686 * \retval #PSA_ERROR_EMPTY_SLOT
1687 * \retval #PSA_ERROR_NOT_PERMITTED
1688 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001689 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001690 * \retval #PSA_ERROR_NOT_SUPPORTED
1691 * \p alg is not supported or is not a cipher algorithm.
1692 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1693 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1694 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1695 * \retval #PSA_ERROR_HARDWARE_FAILURE
1696 * \retval #PSA_ERROR_TAMPERING_DETECTED
1697 */
1698psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1699 psa_algorithm_t alg,
1700 const uint8_t *input,
1701 size_t input_length,
1702 uint8_t *output,
1703 size_t output_size,
1704 size_t *output_length);
1705
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001706/** The type of the state data structure for multipart cipher operations.
1707 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001708 * Before calling any function on a cipher operation object, the application
1709 * must initialize it by any of the following means:
1710 * - Set the structure to all-bits-zero, for example:
1711 * \code
1712 * psa_cipher_operation_t operation;
1713 * memset(&operation, 0, sizeof(operation));
1714 * \endcode
1715 * - Initialize the structure to logical zero values, for example:
1716 * \code
1717 * psa_cipher_operation_t operation = {0};
1718 * \endcode
1719 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1720 * for example:
1721 * \code
1722 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1723 * \endcode
1724 * - Assign the result of the function psa_cipher_operation_init()
1725 * to the structure, for example:
1726 * \code
1727 * psa_cipher_operation_t operation;
1728 * operation = psa_cipher_operation_init();
1729 * \endcode
1730 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001731 * This is an implementation-defined \c struct. Applications should not
1732 * make any assumptions about the content of this structure except
1733 * as directed by the documentation of a specific implementation. */
1734typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1735
Jaeden Amero5bae2272019-01-04 11:48:27 +00001736/** \def PSA_CIPHER_OPERATION_INIT
1737 *
1738 * This macro returns a suitable initializer for a cipher operation object of
1739 * type #psa_cipher_operation_t.
1740 */
1741#ifdef __DOXYGEN_ONLY__
1742/* This is an example definition for documentation purposes.
1743 * Implementations should define a suitable value in `crypto_struct.h`.
1744 */
1745#define PSA_CIPHER_OPERATION_INIT {0}
1746#endif
1747
1748/** Return an initial value for a cipher operation object.
1749 */
1750static psa_cipher_operation_t psa_cipher_operation_init(void);
1751
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001752/** Set the key for a multipart symmetric encryption operation.
1753 *
1754 * The sequence of operations to encrypt a message with a symmetric cipher
1755 * is as follows:
1756 * -# Allocate an operation object which will be passed to all the functions
1757 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001758 * -# Initialize the operation object with one of the methods described in the
1759 * documentation for #psa_cipher_operation_t, e.g.
1760 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001761 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001762 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001763 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001764 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001765 * requires a specific IV value.
1766 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1767 * of the message each time.
1768 * -# Call psa_cipher_finish().
1769 *
1770 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001771 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001772 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001773 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001774 * eventually terminate the operation. The following events terminate an
1775 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001776 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001777 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001778 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001779 * \param[in,out] operation The operation object to set up. It must have
1780 * been initialized as per the documentation for
1781 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001782 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001783 * It must remain valid until the operation
1784 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001785 * \param alg The cipher algorithm to compute
1786 * (\c PSA_ALG_XXX value such that
1787 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001788 *
Gilles Peskine28538492018-07-11 17:34:00 +02001789 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001790 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001791 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001792 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001793 * \retval #PSA_ERROR_NOT_PERMITTED
1794 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001795 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001796 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001797 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001798 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1799 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1800 * \retval #PSA_ERROR_HARDWARE_FAILURE
1801 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001802 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001803 * The operation state is not valid (already set up and not
1804 * subsequently completed).
1805 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001806 * The library has not been previously initialized by psa_crypto_init().
1807 * It is implementation-dependent whether a failure to initialize
1808 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001809 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001810psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001811 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001812 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001813
1814/** Set the key for a multipart symmetric decryption operation.
1815 *
1816 * The sequence of operations to decrypt a message with a symmetric cipher
1817 * is as follows:
1818 * -# Allocate an operation object which will be passed to all the functions
1819 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001820 * -# Initialize the operation object with one of the methods described in the
1821 * documentation for #psa_cipher_operation_t, e.g.
1822 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001823 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001824 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001825 * decryption. If the IV is prepended to the ciphertext, you can call
1826 * psa_cipher_update() on a buffer containing the IV followed by the
1827 * beginning of the message.
1828 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1829 * of the message each time.
1830 * -# Call psa_cipher_finish().
1831 *
1832 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001833 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001834 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001835 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001836 * eventually terminate the operation. The following events terminate an
1837 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001838 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001839 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001840 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001841 * \param[in,out] operation The operation object to set up. It must have
1842 * been initialized as per the documentation for
1843 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001844 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001845 * It must remain valid until the operation
1846 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001847 * \param alg The cipher algorithm to compute
1848 * (\c PSA_ALG_XXX value such that
1849 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001850 *
Gilles Peskine28538492018-07-11 17:34:00 +02001851 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001852 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001853 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001854 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001855 * \retval #PSA_ERROR_NOT_PERMITTED
1856 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001857 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001858 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001859 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001860 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1861 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1862 * \retval #PSA_ERROR_HARDWARE_FAILURE
1863 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001864 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001865 * The operation state is not valid (already set up and not
1866 * subsequently completed).
1867 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001868 * The library has not been previously initialized by psa_crypto_init().
1869 * It is implementation-dependent whether a failure to initialize
1870 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001871 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001872psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001873 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001874 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001875
Gilles Peskinedcd14942018-07-12 00:30:52 +02001876/** Generate an IV for a symmetric encryption operation.
1877 *
1878 * This function generates a random IV (initialization vector), nonce
1879 * or initial counter value for the encryption operation as appropriate
1880 * for the chosen algorithm, key type and key size.
1881 *
1882 * The application must call psa_cipher_encrypt_setup() before
1883 * calling this function.
1884 *
1885 * If this function returns an error status, the operation becomes inactive.
1886 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001887 * \param[in,out] operation Active cipher operation.
1888 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001889 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001890 * \param[out] iv_length On success, the number of bytes of the
1891 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001892 *
1893 * \retval #PSA_SUCCESS
1894 * Success.
1895 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001896 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001897 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001898 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001899 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1900 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1901 * \retval #PSA_ERROR_HARDWARE_FAILURE
1902 * \retval #PSA_ERROR_TAMPERING_DETECTED
1903 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001904psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1905 unsigned char *iv,
1906 size_t iv_size,
1907 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001908
Gilles Peskinedcd14942018-07-12 00:30:52 +02001909/** Set the IV for a symmetric encryption or decryption operation.
1910 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001911 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001912 * or initial counter value for the encryption or decryption operation.
1913 *
1914 * The application must call psa_cipher_encrypt_setup() before
1915 * calling this function.
1916 *
1917 * If this function returns an error status, the operation becomes inactive.
1918 *
1919 * \note When encrypting, applications should use psa_cipher_generate_iv()
1920 * instead of this function, unless implementing a protocol that requires
1921 * a non-random IV.
1922 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001923 * \param[in,out] operation Active cipher operation.
1924 * \param[in] iv Buffer containing the IV to use.
1925 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001926 *
1927 * \retval #PSA_SUCCESS
1928 * Success.
1929 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001930 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001931 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001932 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001933 * or the chosen algorithm does not use an IV.
1934 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1935 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1936 * \retval #PSA_ERROR_HARDWARE_FAILURE
1937 * \retval #PSA_ERROR_TAMPERING_DETECTED
1938 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001939psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1940 const unsigned char *iv,
1941 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001942
Gilles Peskinedcd14942018-07-12 00:30:52 +02001943/** Encrypt or decrypt a message fragment in an active cipher operation.
1944 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001945 * Before calling this function, you must:
1946 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1947 * The choice of setup function determines whether this function
1948 * encrypts or decrypts its input.
1949 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1950 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001951 *
1952 * If this function returns an error status, the operation becomes inactive.
1953 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001954 * \param[in,out] operation Active cipher operation.
1955 * \param[in] input Buffer containing the message fragment to
1956 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001957 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001958 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001959 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001960 * \param[out] output_length On success, the number of bytes
1961 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001962 *
1963 * \retval #PSA_SUCCESS
1964 * Success.
1965 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001966 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001967 * not set, or already completed).
1968 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1969 * The size of the \p output buffer is too small.
1970 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1971 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1972 * \retval #PSA_ERROR_HARDWARE_FAILURE
1973 * \retval #PSA_ERROR_TAMPERING_DETECTED
1974 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001975psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1976 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001977 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001978 unsigned char *output,
1979 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001980 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001981
Gilles Peskinedcd14942018-07-12 00:30:52 +02001982/** Finish encrypting or decrypting a message in a cipher operation.
1983 *
1984 * The application must call psa_cipher_encrypt_setup() or
1985 * psa_cipher_decrypt_setup() before calling this function. The choice
1986 * of setup function determines whether this function encrypts or
1987 * decrypts its input.
1988 *
1989 * This function finishes the encryption or decryption of the message
1990 * formed by concatenating the inputs passed to preceding calls to
1991 * psa_cipher_update().
1992 *
1993 * When this function returns, the operation becomes inactive.
1994 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001995 * \param[in,out] operation Active cipher operation.
1996 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001997 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001998 * \param[out] output_length On success, the number of bytes
1999 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002000 *
2001 * \retval #PSA_SUCCESS
2002 * Success.
2003 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002004 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02002005 * not set, or already completed).
2006 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2007 * The size of the \p output buffer is too small.
2008 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2009 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2010 * \retval #PSA_ERROR_HARDWARE_FAILURE
2011 * \retval #PSA_ERROR_TAMPERING_DETECTED
2012 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002013psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002014 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002015 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002016 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002017
Gilles Peskinedcd14942018-07-12 00:30:52 +02002018/** Abort a cipher operation.
2019 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002020 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002021 * \p operation structure itself. Once aborted, the operation object
2022 * can be reused for another operation by calling
2023 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002024 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002025 * You may call this function any time after the operation object has
2026 * been initialized by any of the following methods:
2027 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2028 * whether it succeeds or not.
2029 * - Initializing the \c struct to all-bits-zero.
2030 * - Initializing the \c struct to logical zeros, e.g.
2031 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002032 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002033 * In particular, calling psa_cipher_abort() after the operation has been
2034 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2035 * is safe and has no effect.
2036 *
2037 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002038 *
2039 * \retval #PSA_SUCCESS
2040 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002041 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002042 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2043 * \retval #PSA_ERROR_HARDWARE_FAILURE
2044 * \retval #PSA_ERROR_TAMPERING_DETECTED
2045 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002046psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2047
2048/**@}*/
2049
Gilles Peskine3b555712018-03-03 21:27:57 +01002050/** \defgroup aead Authenticated encryption with associated data (AEAD)
2051 * @{
2052 */
2053
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002054/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002055 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002056 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002057 * \param alg The AEAD algorithm to compute
2058 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002059 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002060 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002061 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002062 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002063 * but not encrypted.
2064 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002065 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002066 * encrypted.
2067 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002068 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002069 * encrypted data. The additional data is not
2070 * part of this output. For algorithms where the
2071 * encrypted data and the authentication tag
2072 * are defined as separate outputs, the
2073 * authentication tag is appended to the
2074 * encrypted data.
2075 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2076 * This must be at least
2077 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2078 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002079 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002080 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002081 *
Gilles Peskine28538492018-07-11 17:34:00 +02002082 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002083 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002084 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002085 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002086 * \retval #PSA_ERROR_NOT_PERMITTED
2087 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002088 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002089 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002090 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002091 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2092 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2093 * \retval #PSA_ERROR_HARDWARE_FAILURE
2094 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002095 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002096 * The library has not been previously initialized by psa_crypto_init().
2097 * It is implementation-dependent whether a failure to initialize
2098 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002099 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002100psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002101 psa_algorithm_t alg,
2102 const uint8_t *nonce,
2103 size_t nonce_length,
2104 const uint8_t *additional_data,
2105 size_t additional_data_length,
2106 const uint8_t *plaintext,
2107 size_t plaintext_length,
2108 uint8_t *ciphertext,
2109 size_t ciphertext_size,
2110 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002111
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002112/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002113 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002114 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002115 * \param alg The AEAD algorithm to compute
2116 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002117 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002118 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002119 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002120 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002121 * but not encrypted.
2122 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002123 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002124 * encrypted. For algorithms where the
2125 * encrypted data and the authentication tag
2126 * are defined as separate inputs, the buffer
2127 * must contain the encrypted data followed
2128 * by the authentication tag.
2129 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002130 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002131 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2132 * This must be at least
2133 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2134 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002135 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002136 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002137 *
Gilles Peskine28538492018-07-11 17:34:00 +02002138 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002139 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002140 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002141 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002142 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002143 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002144 * \retval #PSA_ERROR_NOT_PERMITTED
2145 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002146 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002147 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002148 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002149 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2150 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2151 * \retval #PSA_ERROR_HARDWARE_FAILURE
2152 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002153 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002154 * The library has not been previously initialized by psa_crypto_init().
2155 * It is implementation-dependent whether a failure to initialize
2156 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002157 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002158psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002159 psa_algorithm_t alg,
2160 const uint8_t *nonce,
2161 size_t nonce_length,
2162 const uint8_t *additional_data,
2163 size_t additional_data_length,
2164 const uint8_t *ciphertext,
2165 size_t ciphertext_length,
2166 uint8_t *plaintext,
2167 size_t plaintext_size,
2168 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002169
Gilles Peskine30a9e412019-01-14 18:36:12 +01002170/** The type of the state data structure for multipart AEAD operations.
2171 *
2172 * Before calling any function on an AEAD operation object, the application
2173 * must initialize it by any of the following means:
2174 * - Set the structure to all-bits-zero, for example:
2175 * \code
2176 * psa_aead_operation_t operation;
2177 * memset(&operation, 0, sizeof(operation));
2178 * \endcode
2179 * - Initialize the structure to logical zero values, for example:
2180 * \code
2181 * psa_aead_operation_t operation = {0};
2182 * \endcode
2183 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2184 * for example:
2185 * \code
2186 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2187 * \endcode
2188 * - Assign the result of the function psa_aead_operation_init()
2189 * to the structure, for example:
2190 * \code
2191 * psa_aead_operation_t operation;
2192 * operation = psa_aead_operation_init();
2193 * \endcode
2194 *
2195 * This is an implementation-defined \c struct. Applications should not
2196 * make any assumptions about the content of this structure except
2197 * as directed by the documentation of a specific implementation. */
2198typedef struct psa_aead_operation_s psa_aead_operation_t;
2199
2200/** \def PSA_AEAD_OPERATION_INIT
2201 *
2202 * This macro returns a suitable initializer for an AEAD operation object of
2203 * type #psa_aead_operation_t.
2204 */
2205#ifdef __DOXYGEN_ONLY__
2206/* This is an example definition for documentation purposes.
2207 * Implementations should define a suitable value in `crypto_struct.h`.
2208 */
2209#define PSA_AEAD_OPERATION_INIT {0}
2210#endif
2211
2212/** Return an initial value for an AEAD operation object.
2213 */
2214static psa_aead_operation_t psa_aead_operation_init(void);
2215
2216/** Set the key for a multipart authenticated encryption operation.
2217 *
2218 * The sequence of operations to encrypt a message with authentication
2219 * is as follows:
2220 * -# Allocate an operation object which will be passed to all the functions
2221 * listed here.
2222 * -# Initialize the operation object with one of the methods described in the
2223 * documentation for #psa_aead_operation_t, e.g.
2224 * PSA_AEAD_OPERATION_INIT.
2225 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002226 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2227 * inputs to the subsequent calls to psa_aead_update_ad() and
2228 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2229 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002230 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2231 * generate or set the nonce. You should use
2232 * psa_aead_generate_nonce() unless the protocol you are implementing
2233 * requires a specific nonce value.
2234 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2235 * of the non-encrypted additional authenticated data each time.
2236 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002237 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002238 * -# Call psa_aead_finish().
2239 *
2240 * The application may call psa_aead_abort() at any time after the operation
2241 * has been initialized.
2242 *
2243 * After a successful call to psa_aead_encrypt_setup(), the application must
2244 * eventually terminate the operation. The following events terminate an
2245 * operation:
2246 * - A failed call to any of the \c psa_aead_xxx functions.
2247 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2248 *
2249 * \param[in,out] operation The operation object to set up. It must have
2250 * been initialized as per the documentation for
2251 * #psa_aead_operation_t and not yet in use.
2252 * \param handle Handle to the key to use for the operation.
2253 * It must remain valid until the operation
2254 * terminates.
2255 * \param alg The AEAD algorithm to compute
2256 * (\c PSA_ALG_XXX value such that
2257 * #PSA_ALG_IS_AEAD(\p alg) is true).
2258 *
2259 * \retval #PSA_SUCCESS
2260 * Success.
2261 * \retval #PSA_ERROR_INVALID_HANDLE
2262 * \retval #PSA_ERROR_EMPTY_SLOT
2263 * \retval #PSA_ERROR_NOT_PERMITTED
2264 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002265 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002266 * \retval #PSA_ERROR_NOT_SUPPORTED
2267 * \p alg is not supported or is not an AEAD algorithm.
2268 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2269 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2270 * \retval #PSA_ERROR_HARDWARE_FAILURE
2271 * \retval #PSA_ERROR_TAMPERING_DETECTED
2272 * \retval #PSA_ERROR_BAD_STATE
2273 * The library has not been previously initialized by psa_crypto_init().
2274 * It is implementation-dependent whether a failure to initialize
2275 * results in this error code.
2276 */
2277psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2278 psa_key_handle_t handle,
2279 psa_algorithm_t alg);
2280
2281/** Set the key for a multipart authenticated decryption operation.
2282 *
2283 * The sequence of operations to decrypt a message with authentication
2284 * is as follows:
2285 * -# Allocate an operation object which will be passed to all the functions
2286 * listed here.
2287 * -# Initialize the operation object with one of the methods described in the
2288 * documentation for #psa_aead_operation_t, e.g.
2289 * PSA_AEAD_OPERATION_INIT.
2290 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002291 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2292 * inputs to the subsequent calls to psa_aead_update_ad() and
2293 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2294 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002295 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2296 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2297 * of the non-encrypted additional authenticated data each time.
2298 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002299 * of the ciphertext to decrypt each time.
2300 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002301 *
2302 * The application may call psa_aead_abort() at any time after the operation
2303 * has been initialized.
2304 *
2305 * After a successful call to psa_aead_decrypt_setup(), the application must
2306 * eventually terminate the operation. The following events terminate an
2307 * operation:
2308 * - A failed call to any of the \c psa_aead_xxx functions.
2309 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2310 *
2311 * \param[in,out] operation The operation object to set up. It must have
2312 * been initialized as per the documentation for
2313 * #psa_aead_operation_t and not yet in use.
2314 * \param handle Handle to the key to use for the operation.
2315 * It must remain valid until the operation
2316 * terminates.
2317 * \param alg The AEAD algorithm to compute
2318 * (\c PSA_ALG_XXX value such that
2319 * #PSA_ALG_IS_AEAD(\p alg) is true).
2320 *
2321 * \retval #PSA_SUCCESS
2322 * Success.
2323 * \retval #PSA_ERROR_INVALID_HANDLE
2324 * \retval #PSA_ERROR_EMPTY_SLOT
2325 * \retval #PSA_ERROR_NOT_PERMITTED
2326 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002327 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002328 * \retval #PSA_ERROR_NOT_SUPPORTED
2329 * \p alg is not supported or is not an AEAD algorithm.
2330 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2331 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2332 * \retval #PSA_ERROR_HARDWARE_FAILURE
2333 * \retval #PSA_ERROR_TAMPERING_DETECTED
2334 * \retval #PSA_ERROR_BAD_STATE
2335 * The library has not been previously initialized by psa_crypto_init().
2336 * It is implementation-dependent whether a failure to initialize
2337 * results in this error code.
2338 */
2339psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2340 psa_key_handle_t handle,
2341 psa_algorithm_t alg);
2342
2343/** Generate a random nonce for an authenticated encryption operation.
2344 *
2345 * This function generates a random nonce for the authenticated encryption
2346 * operation with an appropriate size for the chosen algorithm, key type
2347 * and key size.
2348 *
2349 * The application must call psa_aead_encrypt_setup() before
2350 * calling this function.
2351 *
2352 * If this function returns an error status, the operation becomes inactive.
2353 *
2354 * \param[in,out] operation Active AEAD operation.
2355 * \param[out] nonce Buffer where the generated nonce is to be
2356 * written.
2357 * \param nonce_size Size of the \p nonce buffer in bytes.
2358 * \param[out] nonce_length On success, the number of bytes of the
2359 * generated nonce.
2360 *
2361 * \retval #PSA_SUCCESS
2362 * Success.
2363 * \retval #PSA_ERROR_BAD_STATE
2364 * The operation state is not valid (not set up, or nonce already set).
2365 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2366 * The size of the \p nonce buffer is too small.
2367 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2368 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2369 * \retval #PSA_ERROR_HARDWARE_FAILURE
2370 * \retval #PSA_ERROR_TAMPERING_DETECTED
2371 */
2372psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2373 unsigned char *nonce,
2374 size_t nonce_size,
2375 size_t *nonce_length);
2376
2377/** Set the nonce for an authenticated encryption or decryption operation.
2378 *
2379 * This function sets the nonce for the authenticated
2380 * encryption or decryption operation.
2381 *
2382 * The application must call psa_aead_encrypt_setup() before
2383 * calling this function.
2384 *
2385 * If this function returns an error status, the operation becomes inactive.
2386 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002387 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002388 * instead of this function, unless implementing a protocol that requires
2389 * a non-random IV.
2390 *
2391 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002392 * \param[in] nonce Buffer containing the nonce to use.
2393 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002394 *
2395 * \retval #PSA_SUCCESS
2396 * Success.
2397 * \retval #PSA_ERROR_BAD_STATE
2398 * The operation state is not valid (not set up, or nonce already set).
2399 * \retval #PSA_ERROR_INVALID_ARGUMENT
2400 * The size of \p nonce is not acceptable for the chosen algorithm.
2401 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2402 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2403 * \retval #PSA_ERROR_HARDWARE_FAILURE
2404 * \retval #PSA_ERROR_TAMPERING_DETECTED
2405 */
2406psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2407 const unsigned char *nonce,
2408 size_t nonce_length);
2409
Gilles Peskinebc59c852019-01-17 15:26:08 +01002410/** Declare the lengths of the message and additional data for AEAD.
2411 *
2412 * The application must call this function before calling
2413 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2414 * the operation requires it. If the algorithm does not require it,
2415 * calling this function is optional, but if this function is called
2416 * then the implementation must enforce the lengths.
2417 *
2418 * You may call this function before or after setting the nonce with
2419 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2420 *
2421 * - For #PSA_ALG_CCM, calling this function is required.
2422 * - For the other AEAD algorithms defined in this specification, calling
2423 * this function is not required.
2424 * - For vendor-defined algorithm, refer to the vendor documentation.
2425 *
2426 * \param[in,out] operation Active AEAD operation.
2427 * \param ad_length Size of the non-encrypted additional
2428 * authenticated data in bytes.
2429 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2430 *
2431 * \retval #PSA_SUCCESS
2432 * Success.
2433 * \retval #PSA_ERROR_BAD_STATE
2434 * The operation state is not valid (not set up, already completed,
2435 * or psa_aead_update_ad() or psa_aead_update() already called).
2436 * \retval #PSA_ERROR_INVALID_ARGUMENT
2437 * At least one of the lengths is not acceptable for the chosen
2438 * algorithm.
2439 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2440 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2441 * \retval #PSA_ERROR_HARDWARE_FAILURE
2442 * \retval #PSA_ERROR_TAMPERING_DETECTED
2443 */
2444psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2445 size_t ad_length,
2446 size_t plaintext_length);
2447
Gilles Peskine30a9e412019-01-14 18:36:12 +01002448/** Pass additional data to an active AEAD operation.
2449 *
2450 * Additional data is authenticated, but not encrypted.
2451 *
2452 * You may call this function multiple times to pass successive fragments
2453 * of the additional data. You may not call this function after passing
2454 * data to encrypt or decrypt with psa_aead_update().
2455 *
2456 * Before calling this function, you must:
2457 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2458 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2459 *
2460 * If this function returns an error status, the operation becomes inactive.
2461 *
2462 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2463 * there is no guarantee that the input is valid. Therefore, until
2464 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2465 * treat the input as untrusted and prepare to undo any action that
2466 * depends on the input if psa_aead_verify() returns an error status.
2467 *
2468 * \param[in,out] operation Active AEAD operation.
2469 * \param[in] input Buffer containing the fragment of
2470 * additional data.
2471 * \param input_length Size of the \p input buffer in bytes.
2472 *
2473 * \retval #PSA_SUCCESS
2474 * Success.
2475 * \retval #PSA_ERROR_BAD_STATE
2476 * The operation state is not valid (not set up, nonce not set,
2477 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002478 * \retval #PSA_ERROR_INVALID_ARGUMENT
2479 * The total input length overflows the additional data length that
2480 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002481 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2482 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2483 * \retval #PSA_ERROR_HARDWARE_FAILURE
2484 * \retval #PSA_ERROR_TAMPERING_DETECTED
2485 */
2486psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2487 const uint8_t *input,
2488 size_t input_length);
2489
2490/** Encrypt or decrypt a message fragment in an active AEAD operation.
2491 *
2492 * Before calling this function, you must:
2493 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2494 * The choice of setup function determines whether this function
2495 * encrypts or decrypts its input.
2496 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2497 * 3. Call psa_aead_update_ad() to pass all the additional data.
2498 *
2499 * If this function returns an error status, the operation becomes inactive.
2500 *
2501 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2502 * there is no guarantee that the input is valid. Therefore, until
2503 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2504 * - Do not use the output in any way other than storing it in a
2505 * confidential location. If you take any action that depends
2506 * on the tentative decrypted data, this action will need to be
2507 * undone if the input turns out not to be valid. Furthermore,
2508 * if an adversary can observe that this action took place
2509 * (for example through timing), they may be able to use this
2510 * fact as an oracle to decrypt any message encrypted with the
2511 * same key.
2512 * - In particular, do not copy the output anywhere but to a
2513 * memory or storage space that you have exclusive access to.
2514 *
2515 * \param[in,out] operation Active AEAD operation.
2516 * \param[in] input Buffer containing the message fragment to
2517 * encrypt or decrypt.
2518 * \param input_length Size of the \p input buffer in bytes.
2519 * \param[out] output Buffer where the output is to be written.
2520 * \param output_size Size of the \p output buffer in bytes.
2521 * \param[out] output_length On success, the number of bytes
2522 * that make up the returned output.
2523 *
2524 * \retval #PSA_SUCCESS
2525 * Success.
2526 * \retval #PSA_ERROR_BAD_STATE
2527 * The operation state is not valid (not set up, nonce not set
2528 * or already completed).
2529 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2530 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002531 * \retval #PSA_ERROR_INVALID_ARGUMENT
2532 * The total length of input to psa_aead_update_ad() so far is
2533 * less than the additional data length that was previously
2534 * specified with psa_aead_set_lengths().
2535 * \retval #PSA_ERROR_INVALID_ARGUMENT
2536 * The total input length overflows the plaintext length that
2537 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002538 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2539 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2540 * \retval #PSA_ERROR_HARDWARE_FAILURE
2541 * \retval #PSA_ERROR_TAMPERING_DETECTED
2542 */
2543psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2544 const uint8_t *input,
2545 size_t input_length,
2546 unsigned char *output,
2547 size_t output_size,
2548 size_t *output_length);
2549
2550/** Finish encrypting a message in an AEAD operation.
2551 *
2552 * The operation must have been set up with psa_aead_encrypt_setup().
2553 *
2554 * This function finishes the authentication of the additional data
2555 * formed by concatenating the inputs passed to preceding calls to
2556 * psa_aead_update_ad() with the plaintext formed by concatenating the
2557 * inputs passed to preceding calls to psa_aead_update().
2558 *
2559 * This function has two output buffers:
2560 * - \p ciphertext contains trailing ciphertext that was buffered from
2561 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2562 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2563 * will not contain any output and can be a 0-sized buffer.
2564 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002565 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002566 * that the operation performs.
2567 *
2568 * When this function returns, the operation becomes inactive.
2569 *
2570 * \param[in,out] operation Active AEAD operation.
2571 * \param[out] ciphertext Buffer where the last part of the ciphertext
2572 * is to be written.
2573 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2574 * \param[out] ciphertext_length On success, the number of bytes of
2575 * returned ciphertext.
2576 * \param[out] tag Buffer where the authentication tag is
2577 * to be written.
2578 * \param tag_size Size of the \p tag buffer in bytes.
2579 * \param[out] tag_length On success, the number of bytes
2580 * that make up the returned tag.
2581 *
2582 * \retval #PSA_SUCCESS
2583 * Success.
2584 * \retval #PSA_ERROR_BAD_STATE
2585 * The operation state is not valid (not set up, nonce not set,
2586 * decryption, or already completed).
2587 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002588 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002589 * \retval #PSA_ERROR_INVALID_ARGUMENT
2590 * The total length of input to psa_aead_update_ad() so far is
2591 * less than the additional data length that was previously
2592 * specified with psa_aead_set_lengths().
2593 * \retval #PSA_ERROR_INVALID_ARGUMENT
2594 * The total length of input to psa_aead_update() so far is
2595 * less than the plaintext length that was previously
2596 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002597 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2598 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2599 * \retval #PSA_ERROR_HARDWARE_FAILURE
2600 * \retval #PSA_ERROR_TAMPERING_DETECTED
2601 */
2602psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002603 uint8_t *ciphertext,
2604 size_t ciphertext_size,
2605 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002606 uint8_t *tag,
2607 size_t tag_size,
2608 size_t *tag_length);
2609
2610/** Finish authenticating and decrypting a message in an AEAD operation.
2611 *
2612 * The operation must have been set up with psa_aead_decrypt_setup().
2613 *
2614 * This function finishes the authentication of the additional data
2615 * formed by concatenating the inputs passed to preceding calls to
2616 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2617 * inputs passed to preceding calls to psa_aead_update().
2618 *
2619 * When this function returns, the operation becomes inactive.
2620 *
2621 * \param[in,out] operation Active AEAD operation.
2622 * \param[in] tag Buffer containing the authentication tag.
2623 * \param tag_length Size of the \p tag buffer in bytes.
2624 *
2625 * \retval #PSA_SUCCESS
2626 * Success.
2627 * \retval #PSA_ERROR_BAD_STATE
2628 * The operation state is not valid (not set up, nonce not set,
2629 * encryption, or already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002630 * \retval #PSA_ERROR_INVALID_ARGUMENT
2631 * The total length of input to psa_aead_update_ad() so far is
2632 * less than the additional data length that was previously
2633 * specified with psa_aead_set_lengths().
2634 * \retval #PSA_ERROR_INVALID_ARGUMENT
2635 * The total length of input to psa_aead_update() so far is
2636 * less than the plaintext length that was previously
2637 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002638 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2639 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2640 * \retval #PSA_ERROR_HARDWARE_FAILURE
2641 * \retval #PSA_ERROR_TAMPERING_DETECTED
2642 */
2643psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2644 const uint8_t *tag,
2645 size_t tag_length);
2646
2647/** Abort an AEAD operation.
2648 *
2649 * Aborting an operation frees all associated resources except for the
2650 * \p operation structure itself. Once aborted, the operation object
2651 * can be reused for another operation by calling
2652 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2653 *
2654 * You may call this function any time after the operation object has
2655 * been initialized by any of the following methods:
2656 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2657 * whether it succeeds or not.
2658 * - Initializing the \c struct to all-bits-zero.
2659 * - Initializing the \c struct to logical zeros, e.g.
2660 * `psa_aead_operation_t operation = {0}`.
2661 *
2662 * In particular, calling psa_aead_abort() after the operation has been
2663 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2664 * is safe and has no effect.
2665 *
2666 * \param[in,out] operation Initialized AEAD operation.
2667 *
2668 * \retval #PSA_SUCCESS
2669 * \retval #PSA_ERROR_BAD_STATE
2670 * \p operation is not an active AEAD operation.
2671 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2672 * \retval #PSA_ERROR_HARDWARE_FAILURE
2673 * \retval #PSA_ERROR_TAMPERING_DETECTED
2674 */
2675psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2676
Gilles Peskine3b555712018-03-03 21:27:57 +01002677/**@}*/
2678
Gilles Peskine20035e32018-02-03 22:44:14 +01002679/** \defgroup asymmetric Asymmetric cryptography
2680 * @{
2681 */
2682
2683/**
2684 * \brief Sign a hash or short message with a private key.
2685 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002686 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002687 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002688 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2689 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2690 * to determine the hash algorithm to use.
2691 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002692 * \param handle Handle to the key to use for the operation.
2693 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002694 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002695 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002696 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002697 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002698 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002699 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002700 * \param[out] signature_length On success, the number of bytes
2701 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002702 *
Gilles Peskine28538492018-07-11 17:34:00 +02002703 * \retval #PSA_SUCCESS
2704 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002705 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002706 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002707 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002708 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002709 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002710 * \retval #PSA_ERROR_NOT_SUPPORTED
2711 * \retval #PSA_ERROR_INVALID_ARGUMENT
2712 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2713 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2714 * \retval #PSA_ERROR_HARDWARE_FAILURE
2715 * \retval #PSA_ERROR_TAMPERING_DETECTED
2716 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002717 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002718 * The library has not been previously initialized by psa_crypto_init().
2719 * It is implementation-dependent whether a failure to initialize
2720 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002721 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002722psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002723 psa_algorithm_t alg,
2724 const uint8_t *hash,
2725 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002726 uint8_t *signature,
2727 size_t signature_size,
2728 size_t *signature_length);
2729
2730/**
2731 * \brief Verify the signature a hash or short message using a public key.
2732 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002733 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002734 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002735 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2736 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2737 * to determine the hash algorithm to use.
2738 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002739 * \param handle Handle to the key to use for the operation.
2740 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002741 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002742 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002743 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002744 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002745 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002746 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002747 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002748 *
Gilles Peskine28538492018-07-11 17:34:00 +02002749 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002750 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002751 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002752 * The calculation was perfomed successfully, but the passed
2753 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002754 * \retval #PSA_ERROR_NOT_SUPPORTED
2755 * \retval #PSA_ERROR_INVALID_ARGUMENT
2756 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2757 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2758 * \retval #PSA_ERROR_HARDWARE_FAILURE
2759 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002760 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002761 * The library has not been previously initialized by psa_crypto_init().
2762 * It is implementation-dependent whether a failure to initialize
2763 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002764 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002765psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002766 psa_algorithm_t alg,
2767 const uint8_t *hash,
2768 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002769 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002770 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002771
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002772/**
2773 * \brief Encrypt a short message with a public key.
2774 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002775 * \param handle Handle to the key to use for the operation.
2776 * It must be a public key or an asymmetric
2777 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002778 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002779 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002780 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002781 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002782 * \param[in] salt A salt or label, if supported by the
2783 * encryption algorithm.
2784 * If the algorithm does not support a
2785 * salt, pass \c NULL.
2786 * If the algorithm supports an optional
2787 * salt and you do not want to pass a salt,
2788 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002789 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002790 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2791 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002792 * \param salt_length Size of the \p salt buffer in bytes.
2793 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002794 * \param[out] output Buffer where the encrypted message is to
2795 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002796 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002797 * \param[out] output_length On success, the number of bytes
2798 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002799 *
Gilles Peskine28538492018-07-11 17:34:00 +02002800 * \retval #PSA_SUCCESS
2801 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002802 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002803 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002804 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002805 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002806 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002807 * \retval #PSA_ERROR_NOT_SUPPORTED
2808 * \retval #PSA_ERROR_INVALID_ARGUMENT
2809 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2810 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2811 * \retval #PSA_ERROR_HARDWARE_FAILURE
2812 * \retval #PSA_ERROR_TAMPERING_DETECTED
2813 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002814 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002815 * The library has not been previously initialized by psa_crypto_init().
2816 * It is implementation-dependent whether a failure to initialize
2817 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002818 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002819psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002820 psa_algorithm_t alg,
2821 const uint8_t *input,
2822 size_t input_length,
2823 const uint8_t *salt,
2824 size_t salt_length,
2825 uint8_t *output,
2826 size_t output_size,
2827 size_t *output_length);
2828
2829/**
2830 * \brief Decrypt a short message with a private key.
2831 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002832 * \param handle Handle to the key to use for the operation.
2833 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002834 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002835 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002836 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002837 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002838 * \param[in] salt A salt or label, if supported by the
2839 * encryption algorithm.
2840 * If the algorithm does not support a
2841 * salt, pass \c NULL.
2842 * If the algorithm supports an optional
2843 * salt and you do not want to pass a salt,
2844 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002845 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002846 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2847 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002848 * \param salt_length Size of the \p salt buffer in bytes.
2849 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002850 * \param[out] output Buffer where the decrypted message is to
2851 * be written.
2852 * \param output_size Size of the \c output buffer in bytes.
2853 * \param[out] output_length On success, the number of bytes
2854 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002855 *
Gilles Peskine28538492018-07-11 17:34:00 +02002856 * \retval #PSA_SUCCESS
2857 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002858 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002859 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002860 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002861 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002862 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002863 * \retval #PSA_ERROR_NOT_SUPPORTED
2864 * \retval #PSA_ERROR_INVALID_ARGUMENT
2865 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2866 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2867 * \retval #PSA_ERROR_HARDWARE_FAILURE
2868 * \retval #PSA_ERROR_TAMPERING_DETECTED
2869 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2870 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002871 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002872 * The library has not been previously initialized by psa_crypto_init().
2873 * It is implementation-dependent whether a failure to initialize
2874 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002875 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002876psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002877 psa_algorithm_t alg,
2878 const uint8_t *input,
2879 size_t input_length,
2880 const uint8_t *salt,
2881 size_t salt_length,
2882 uint8_t *output,
2883 size_t output_size,
2884 size_t *output_length);
2885
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002886/**@}*/
2887
Gilles Peskineedd76872018-07-20 17:42:05 +02002888/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002889 * @{
2890 */
2891
2892/** The type of the state data structure for generators.
2893 *
2894 * Before calling any function on a generator, the application must
2895 * initialize it by any of the following means:
2896 * - Set the structure to all-bits-zero, for example:
2897 * \code
2898 * psa_crypto_generator_t generator;
2899 * memset(&generator, 0, sizeof(generator));
2900 * \endcode
2901 * - Initialize the structure to logical zero values, for example:
2902 * \code
2903 * psa_crypto_generator_t generator = {0};
2904 * \endcode
2905 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2906 * for example:
2907 * \code
2908 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2909 * \endcode
2910 * - Assign the result of the function psa_crypto_generator_init()
2911 * to the structure, for example:
2912 * \code
2913 * psa_crypto_generator_t generator;
2914 * generator = psa_crypto_generator_init();
2915 * \endcode
2916 *
2917 * This is an implementation-defined \c struct. Applications should not
2918 * make any assumptions about the content of this structure except
2919 * as directed by the documentation of a specific implementation.
2920 */
2921typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2922
2923/** \def PSA_CRYPTO_GENERATOR_INIT
2924 *
2925 * This macro returns a suitable initializer for a generator object
2926 * of type #psa_crypto_generator_t.
2927 */
2928#ifdef __DOXYGEN_ONLY__
2929/* This is an example definition for documentation purposes.
2930 * Implementations should define a suitable value in `crypto_struct.h`.
2931 */
2932#define PSA_CRYPTO_GENERATOR_INIT {0}
2933#endif
2934
2935/** Return an initial value for a generator object.
2936 */
2937static psa_crypto_generator_t psa_crypto_generator_init(void);
2938
2939/** Retrieve the current capacity of a generator.
2940 *
2941 * The capacity of a generator is the maximum number of bytes that it can
2942 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2943 *
2944 * \param[in] generator The generator to query.
2945 * \param[out] capacity On success, the capacity of the generator.
2946 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002947 * \retval #PSA_SUCCESS
2948 * \retval #PSA_ERROR_BAD_STATE
2949 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002950 */
2951psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2952 size_t *capacity);
2953
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002954/** Set the maximum capacity of a generator.
2955 *
2956 * \param[in,out] generator The generator object to modify.
2957 * \param capacity The new capacity of the generator.
2958 * It must be less or equal to the generator's
2959 * current capacity.
2960 *
2961 * \retval #PSA_SUCCESS
2962 * \retval #PSA_ERROR_INVALID_ARGUMENT
2963 * \p capacity is larger than the generator's current capacity.
2964 * \retval #PSA_ERROR_BAD_STATE
2965 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2966 */
2967psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
2968 size_t capacity);
2969
Gilles Peskineeab56e42018-07-12 17:12:33 +02002970/** Read some data from a generator.
2971 *
2972 * This function reads and returns a sequence of bytes from a generator.
2973 * The data that is read is discarded from the generator. The generator's
2974 * capacity is decreased by the number of bytes read.
2975 *
2976 * \param[in,out] generator The generator object to read from.
2977 * \param[out] output Buffer where the generator output will be
2978 * written.
2979 * \param output_length Number of bytes to output.
2980 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002981 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02002982 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskineeab56e42018-07-12 17:12:33 +02002983 * There were fewer than \p output_length bytes
2984 * in the generator. Note that in this case, no
2985 * output is written to the output buffer.
2986 * The generator's capacity is set to 0, thus
2987 * subsequent calls to this function will not
2988 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002989 * \retval #PSA_ERROR_BAD_STATE
2990 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2991 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2992 * \retval #PSA_ERROR_HARDWARE_FAILURE
2993 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002994 */
2995psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2996 uint8_t *output,
2997 size_t output_length);
2998
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002999/** Generate a key deterministically from data read from a generator.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003000 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003001 * This function uses the output of a generator to derive a key.
3002 * How much output it consumes and how the key is derived depends on the
3003 * key type.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003004 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003005 * - For key types for which the key is an arbitrary sequence of bytes
3006 * of a given size,
3007 * this function is functionally equivalent to calling #psa_generator_read
3008 * and passing the resulting output to #psa_import_key.
3009 * However, this function has a security benefit:
3010 * if the implementation provides an isolation boundary then
3011 * the key material is not exposed outside the isolation boundary.
3012 * As a consequence, for these key types, this function always consumes
3013 * exactly (\p bits / 8) bytes from the generator.
3014 * The following key types defined in this specification follow this scheme:
3015 *
3016 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003017 * - #PSA_KEY_TYPE_ARC4;
3018 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003019 * - #PSA_KEY_TYPE_DERIVE;
3020 * - #PSA_KEY_TYPE_HMAC.
3021 *
3022 * - For ECC keys on a Montgomery elliptic curve
3023 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3024 * Montgomery curve), this function always draws a byte string whose
3025 * length is determined by the curve, and sets the mandatory bits
3026 * accordingly. That is:
3027 *
3028 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3029 * and process it as specified in RFC 7748 &sect;5.
3030 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3031 * and process it as specified in RFC 7748 &sect;5.
3032 *
3033 * - For key types for which the key is represented by a single sequence of
3034 * \p bits bits with constraints as to which bit sequences are acceptable,
3035 * this function draws a byte string of length (\p bits / 8) bytes rounded
3036 * up to the nearest whole number of bytes. If the resulting byte string
3037 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3038 * This process is repeated until an acceptable byte string is drawn.
3039 * The byte string drawn from the generator is interpreted as specified
3040 * for the output produced by psa_export_key().
3041 * The following key types defined in this specification follow this scheme:
3042 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003043 * - #PSA_KEY_TYPE_DES.
3044 * Force-set the parity bits, but discard forbidden weak keys.
3045 * For 2-key and 3-key triple-DES, the three keys are generated
3046 * successively (for example, for 3-key triple-DES,
3047 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3048 * discard the first 8 bytes, use the next 8 bytes as the first key,
3049 * and continue reading output from the generator to derive the other
3050 * two keys).
3051 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR),
3052 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
3053 * ECC keys on a Weierstrass elliptic curve
3054 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3055 * Weierstrass curve).
3056 * For these key types, interpret the byte string as integer
3057 * in big-endian order. Discard it if it is not in the range
3058 * [0, *N* - 2] where *N* is the boundary of the private key domain
3059 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003060 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003061 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003062 * This method allows compliance to NIST standards, specifically
3063 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003064 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3065 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3066 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3067 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003068 *
3069 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
3070 * the way in which the generator output is consumed is
3071 * implementation-defined.
3072 *
3073 * In all cases, the data that is read is discarded from the generator.
3074 * The generator's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003075 *
Gilles Peskine20628592019-04-19 19:29:50 +02003076 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003077 * \param[out] handle On success, a handle to the newly created key.
3078 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003079 * \param[in,out] generator The generator object to read from.
3080 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003081 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003082 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003083 * If the key is persistent, the key material and the key's metadata
3084 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003085 * \retval #PSA_ERROR_ALREADY_EXISTS
3086 * This is an attempt to create a persistent key, and there is
3087 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003088 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003089 * There was not enough data to create the desired key.
3090 * Note that in this case, no output is written to the output buffer.
3091 * The generator's capacity is set to 0, thus subsequent calls to
3092 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003093 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003094 * The key type or key size is not supported, either by the
3095 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003096 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003097 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3098 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3099 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3100 * \retval #PSA_ERROR_HARDWARE_FAILURE
3101 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003102 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003103 * The library has not been previously initialized by psa_crypto_init().
3104 * It is implementation-dependent whether a failure to initialize
3105 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003106 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003107psa_status_t psa_generate_derived_key(const psa_key_attributes_t *attributes,
Gilles Peskine87a5e562019-04-17 12:28:25 +02003108 psa_key_handle_t *handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003109 psa_crypto_generator_t *generator);
3110
3111/** Abort a generator.
3112 *
3113 * Once a generator has been aborted, its capacity is zero.
3114 * Aborting a generator frees all associated resources except for the
3115 * \c generator structure itself.
3116 *
3117 * This function may be called at any time as long as the generator
3118 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
3119 * psa_crypto_generator_init() or a zero value. In particular, it is valid
3120 * to call psa_generator_abort() twice, or to call psa_generator_abort()
3121 * on a generator that has not been set up.
3122 *
3123 * Once aborted, the generator object may be called.
3124 *
3125 * \param[in,out] generator The generator to abort.
3126 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003127 * \retval #PSA_SUCCESS
3128 * \retval #PSA_ERROR_BAD_STATE
3129 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3130 * \retval #PSA_ERROR_HARDWARE_FAILURE
3131 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003132 */
3133psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3134
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003135/** Use the maximum possible capacity for a generator.
3136 *
3137 * Use this value as the capacity argument when setting up a generator
3138 * to indicate that the generator should have the maximum possible capacity.
3139 * The value of the maximum possible capacity depends on the generator
3140 * algorithm.
3141 */
3142#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
3143
Gilles Peskineeab56e42018-07-12 17:12:33 +02003144/**@}*/
3145
Gilles Peskineea0fb492018-07-12 17:17:20 +02003146/** \defgroup derivation Key derivation
3147 * @{
3148 */
3149
3150/** Set up a key derivation operation.
3151 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003152 * A key derivation algorithm takes some inputs and uses them to create
3153 * a byte generator which can be used to produce keys and other
3154 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003155 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003156 * To use a generator for key derivation:
3157 * - Start with an initialized object of type #psa_crypto_generator_t.
3158 * - Call psa_key_derivation_setup() to select the algorithm.
3159 * - Provide the inputs for the key derivation by calling
3160 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3161 * as appropriate. Which inputs are needed, in what order, and whether
3162 * they may be keys and if so of what type depends on the algorithm.
3163 * - Optionally set the generator's maximum capacity with
3164 * psa_set_generator_capacity(). You may do this before, in the middle of
3165 * or after providing inputs. For some algorithms, this step is mandatory
3166 * because the output depends on the maximum capacity.
3167 * - Generate output with psa_generator_read() or
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003168 * psa_generate_derived_key(). Successive calls to these functions
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003169 * use successive output bytes from the generator.
3170 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02003171 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003172 * \param[in,out] generator The generator object to set up. It must
3173 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003174 * \param alg The key derivation algorithm to compute
3175 * (\c PSA_ALG_XXX value such that
3176 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02003177 *
3178 * \retval #PSA_SUCCESS
3179 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003180 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003181 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003182 * \retval #PSA_ERROR_NOT_SUPPORTED
3183 * \c alg is not supported or is not a key derivation algorithm.
3184 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3185 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3186 * \retval #PSA_ERROR_HARDWARE_FAILURE
3187 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003188 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003189 */
3190psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
3191 psa_algorithm_t alg);
3192
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003193/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003194 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003195 * Which inputs are required and in what order depends on the algorithm.
3196 * Refer to the documentation of each key derivation or key agreement
3197 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003198 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003199 * This function passes direct inputs. Some inputs must be passed as keys
3200 * using psa_key_derivation_input_key() instead of this function. Refer to
3201 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003202 *
3203 * \param[in,out] generator The generator object to use. It must
3204 * have been set up with
3205 * psa_key_derivation_setup() and must not
3206 * have produced any output yet.
3207 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003208 * \param[in] data Input data to use.
3209 * \param data_length Size of the \p data buffer in bytes.
3210 *
3211 * \retval #PSA_SUCCESS
3212 * Success.
3213 * \retval #PSA_ERROR_INVALID_ARGUMENT
3214 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003215 * \retval #PSA_ERROR_INVALID_ARGUMENT
3216 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003217 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3218 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3219 * \retval #PSA_ERROR_HARDWARE_FAILURE
3220 * \retval #PSA_ERROR_TAMPERING_DETECTED
3221 * \retval #PSA_ERROR_BAD_STATE
3222 * The value of \p step is not valid given the state of \p generator.
3223 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003224 * The library has not been previously initialized by psa_crypto_init().
3225 * It is implementation-dependent whether a failure to initialize
3226 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003227 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003228psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3229 psa_key_derivation_step_t step,
3230 const uint8_t *data,
3231 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003232
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003233/** Provide an input for key derivation in the form of a key.
3234 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003235 * Which inputs are required and in what order depends on the algorithm.
3236 * Refer to the documentation of each key derivation or key agreement
3237 * algorithm for information.
3238 *
3239 * This function passes key inputs. Some inputs must be passed as keys
3240 * of the appropriate type using this function, while others must be
3241 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3242 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003243 *
3244 * \param[in,out] generator The generator object to use. It must
3245 * have been set up with
3246 * psa_key_derivation_setup() and must not
3247 * have produced any output yet.
3248 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003249 * \param handle Handle to the key. It must have an
3250 * appropriate type for \p step and must
3251 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003252 *
3253 * \retval #PSA_SUCCESS
3254 * Success.
3255 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine31351842019-04-09 12:00:00 +02003256 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003257 * \retval #PSA_ERROR_NOT_PERMITTED
3258 * \retval #PSA_ERROR_INVALID_ARGUMENT
3259 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003260 * \retval #PSA_ERROR_INVALID_ARGUMENT
3261 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003262 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3263 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3264 * \retval #PSA_ERROR_HARDWARE_FAILURE
3265 * \retval #PSA_ERROR_TAMPERING_DETECTED
3266 * \retval #PSA_ERROR_BAD_STATE
3267 * The value of \p step is not valid given the state of \p generator.
3268 * \retval #PSA_ERROR_BAD_STATE
3269 * The library has not been previously initialized by psa_crypto_init().
3270 * It is implementation-dependent whether a failure to initialize
3271 * results in this error code.
3272 */
3273psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3274 psa_key_derivation_step_t step,
3275 psa_key_handle_t handle);
3276
Gilles Peskine969c5d62019-01-16 15:53:06 +01003277/** Perform a key agreement and use the shared secret as input to a key
3278 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003279 *
3280 * A key agreement algorithm takes two inputs: a private key \p private_key
3281 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003282 * The result of this function is passed as input to a key derivation.
3283 * The output of this key derivation can be extracted by reading from the
3284 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003285 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003286 * \param[in,out] generator The generator object to use. It must
3287 * have been set up with
3288 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003289 * key agreement and derivation algorithm
3290 * \c alg (\c PSA_ALG_XXX value such that
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003291 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3292 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
Gilles Peskine6843c292019-01-18 16:44:49 +01003293 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003294 * The generator must be ready for an
3295 * input of the type given by \p step.
3296 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003297 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003298 * \param[in] peer_key Public key of the peer. The peer key must be in the
3299 * same format that psa_import_key() accepts for the
3300 * public key type corresponding to the type of
3301 * private_key. That is, this function performs the
3302 * equivalent of
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003303 * #psa_import_key(`internal_public_key_handle`,
3304 * #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(`private_key_type`),
3305 * `peer_key`, `peer_key_length`) where
Jaeden Amero8afbff82019-01-14 16:56:20 +00003306 * `private_key_type` is the type of `private_key`.
3307 * For example, for EC keys, this means that peer_key
3308 * is interpreted as a point on the curve that the
3309 * private key is on. The standard formats for public
3310 * keys are documented in the documentation of
3311 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003312 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003313 *
3314 * \retval #PSA_SUCCESS
3315 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003316 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02003317 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine01d718c2018-09-18 12:01:02 +02003318 * \retval #PSA_ERROR_NOT_PERMITTED
3319 * \retval #PSA_ERROR_INVALID_ARGUMENT
3320 * \c private_key is not compatible with \c alg,
3321 * or \p peer_key is not valid for \c alg or not compatible with
3322 * \c private_key.
3323 * \retval #PSA_ERROR_NOT_SUPPORTED
3324 * \c alg is not supported or is not a key derivation algorithm.
3325 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3326 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3327 * \retval #PSA_ERROR_HARDWARE_FAILURE
3328 * \retval #PSA_ERROR_TAMPERING_DETECTED
3329 */
3330psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003331 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003332 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003333 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003334 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003335
Gilles Peskine769c7a62019-01-18 16:42:29 +01003336/** Perform a key agreement and use the shared secret as input to a key
3337 * derivation.
3338 *
3339 * A key agreement algorithm takes two inputs: a private key \p private_key
3340 * a public key \p peer_key.
3341 *
3342 * \warning The raw result of a key agreement algorithm such as finite-field
3343 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3344 * not be used directly as key material. It should instead be passed as
3345 * input to a key derivation algorithm. To chain a key agreement with
3346 * a key derivation, use psa_key_agreement() and other functions from
3347 * the key derivation and generator interface.
3348 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003349 * \param alg The key agreement algorithm to compute
3350 * (\c PSA_ALG_XXX value such that
3351 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3352 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003353 * \param private_key Handle to the private key to use.
3354 * \param[in] peer_key Public key of the peer. It must be
3355 * in the same format that psa_import_key()
3356 * accepts. The standard formats for public
3357 * keys are documented in the documentation
3358 * of psa_export_public_key().
3359 * \param peer_key_length Size of \p peer_key in bytes.
3360 * \param[out] output Buffer where the decrypted message is to
3361 * be written.
3362 * \param output_size Size of the \c output buffer in bytes.
3363 * \param[out] output_length On success, the number of bytes
3364 * that make up the returned output.
3365 *
3366 * \retval #PSA_SUCCESS
3367 * Success.
3368 * \retval #PSA_ERROR_INVALID_HANDLE
3369 * \retval #PSA_ERROR_EMPTY_SLOT
3370 * \retval #PSA_ERROR_NOT_PERMITTED
3371 * \retval #PSA_ERROR_INVALID_ARGUMENT
3372 * \p alg is not a key agreement algorithm
3373 * \retval #PSA_ERROR_INVALID_ARGUMENT
3374 * \p private_key is not compatible with \p alg,
3375 * or \p peer_key is not valid for \p alg or not compatible with
3376 * \p private_key.
3377 * \retval #PSA_ERROR_NOT_SUPPORTED
3378 * \p alg is not a supported key agreement algorithm.
3379 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3380 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3381 * \retval #PSA_ERROR_HARDWARE_FAILURE
3382 * \retval #PSA_ERROR_TAMPERING_DETECTED
3383 */
3384psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3385 psa_key_handle_t private_key,
3386 const uint8_t *peer_key,
3387 size_t peer_key_length,
3388 uint8_t *output,
3389 size_t output_size,
3390 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003391
3392/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003393
3394/** \defgroup random Random generation
3395 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003396 */
3397
3398/**
3399 * \brief Generate random bytes.
3400 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003401 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003402 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003403 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003404 *
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003405 * \note To generate a key, use psa_generate_random_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003406 *
3407 * \param[out] output Output buffer for the generated data.
3408 * \param output_size Number of bytes to generate and output.
3409 *
3410 * \retval #PSA_SUCCESS
3411 * \retval #PSA_ERROR_NOT_SUPPORTED
3412 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3413 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3414 * \retval #PSA_ERROR_HARDWARE_FAILURE
3415 * \retval #PSA_ERROR_TAMPERING_DETECTED
3416 * \retval #PSA_ERROR_BAD_STATE
3417 * The library has not been previously initialized by psa_crypto_init().
3418 * It is implementation-dependent whether a failure to initialize
3419 * results in this error code.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003420 */
3421psa_status_t psa_generate_random(uint8_t *output,
3422 size_t output_size);
3423
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003424/**
3425 * \brief Generate a key or key pair.
3426 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003427 * The key is generated randomly.
3428 * Its location, policy, type and size are taken from \p attributes.
3429 *
3430 * If the type requires additional domain parameters, these are taken
3431 * from \p attributes as well. The following types use domain parameters:
3432 * - When generating an RSA key (#PSA_KEY_TYPE_RSA_KEYPAIR),
3433 * the default public exponent is 65537. This value is used if
3434 * \p attributes was set with psa_set_key_type() or by passing an empty
3435 * byte string as domain parameters to psa_set_key_domain_parameters().
3436 * If psa_set_key_domain_parameters() was used to set a non-empty
3437 * domain parameter string in \p attributes, this string is read as
3438 * a big-endian integer which is used as the public exponent.
3439 * - When generating a DSA key (#PSA_KEY_TYPE_DSA_KEYPAIR) or a
3440 * Diffie-Hellman key (#PSA_KEY_TYPE_DH_KEYPAIR), the domain parameters
3441 * from \p attributes are interpreted as described for
3442 * psa_set_key_domain_parameters().
3443 *
Gilles Peskine20628592019-04-19 19:29:50 +02003444 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003445 * \param[out] handle On success, a handle to the newly created key.
3446 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003447 *
Gilles Peskine28538492018-07-11 17:34:00 +02003448 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003449 * Success.
3450 * If the key is persistent, the key material and the key's metadata
3451 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003452 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003453 * This is an attempt to create a persistent key, and there is
3454 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003455 * \retval #PSA_ERROR_NOT_SUPPORTED
3456 * \retval #PSA_ERROR_INVALID_ARGUMENT
3457 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3458 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3459 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3460 * \retval #PSA_ERROR_HARDWARE_FAILURE
3461 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003462 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003463 * The library has not been previously initialized by psa_crypto_init().
3464 * It is implementation-dependent whether a failure to initialize
3465 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003466 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003467psa_status_t psa_generate_random_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003468 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003469
3470/**@}*/
3471
Gilles Peskinee59236f2018-01-27 23:32:46 +01003472#ifdef __cplusplus
3473}
3474#endif
3475
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003476/* The file "crypto_sizes.h" contains definitions for size calculation
3477 * macros whose definitions are implementation-specific. */
3478#include "crypto_sizes.h"
3479
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003480/* The file "crypto_struct.h" contains definitions for
3481 * implementation-specific structs that are declared above. */
3482#include "crypto_struct.h"
3483
3484/* The file "crypto_extra.h" contains vendor-specific definitions. This
3485 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003486#include "crypto_extra.h"
3487
3488#endif /* PSA_CRYPTO_H */