blob: 424c16e31703f7770c493f22c8170d30d174e62d [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
89 * \retval #PSA_ERROR_TAMPERING_DETECTED
90 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
Gilles Peskine87a5e562019-04-17 12:28:25 +020096/** \defgroup attributes Key attributes
97 * @{
98 */
99
100/** The type of a structure containing key attributes.
101 *
102 * This is an opaque structure that can represent the metadata of a key
Gilles Peskine9c640f92019-04-28 11:36:21 +0200103 * object. Metadata that can be stored in attributes includes:
104 * - The location of the key in storage, indicated by its key identifier
105 * and its lifetime.
106 * - The key's policy, comprising usage flags and a specification of
107 * the permitted algorithm(s).
108 * - Information about the key itself: the key type, the key size, and
109 * for some key type additional domain parameters.
110 * - Implementations may define additional attributes.
Gilles Peskine87a5e562019-04-17 12:28:25 +0200111 *
112 * The actual key material is not considered an attribute of a key.
113 * Key attributes do not contain information that is generally considered
114 * highly confidential.
Gilles Peskine20628592019-04-19 19:29:50 +0200115 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200116 * An attribute structure can be a simple data structure where each function
117 * `psa_set_key_xxx` sets a field and the corresponding function
118 * `psa_get_key_xxx` retrieves the value of the corresponding field.
119 * However, implementations may report values that are equivalent to the
120 * original one, but have a different encoding. For example, an
121 * implementation may use a more compact representation for types where
122 * many bit-patterns are invalid or not supported, and store all values
123 * that it does not support as a special marker value. In such an
124 * implementation, after setting an invalid value, the corresponding
125 * get function returns an invalid value which may not be the one that
126 * was originally stored.
127 *
128 * An attribute structure may contain references to auxiliary resources,
129 * for example pointers to allocated memory or indirect references to
130 * pre-calculated values. In order to free such resources, the application
131 * must call psa_reset_key_attributes(). As an exception, calling
132 * psa_reset_key_attributes() on an attribute structure is optional if
133 * the structure has only been modified by the following functions
134 * since it was initialized or last reset with psa_reset_key_attributes():
135 * - psa_make_key_persistent()
136 * - psa_set_key_type()
137 * - psa_set_key_bits()
138 * - psa_set_key_usage_flags()
139 * - psa_set_key_algorithm()
140 *
Gilles Peskine20628592019-04-19 19:29:50 +0200141 * Before calling any function on a key attribute structure, the application
142 * must initialize it by any of the following means:
143 * - Set the structure to all-bits-zero, for example:
144 * \code
145 * psa_key_attributes_t attributes;
146 * memset(&attributes, 0, sizeof(attributes));
147 * \endcode
148 * - Initialize the structure to logical zero values, for example:
149 * \code
150 * psa_key_attributes_t attributes = {0};
151 * \endcode
152 * - Initialize the structure to the initializer #PSA_KEY_ATTRIBUTES_INIT,
153 * for example:
154 * \code
155 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
156 * \endcode
157 * - Assign the result of the function psa_key_attributes_init()
158 * to the structure, for example:
159 * \code
160 * psa_key_attributes_t attributes;
161 * attributes = psa_key_attributes_init();
162 * \endcode
163 *
164 * A freshly initialized attribute structure contains the following
165 * values:
166 *
167 * - lifetime: #PSA_KEY_LIFETIME_VOLATILE.
168 * - key identifier: unspecified.
169 * - type: \c 0, with no domain parameters.
170 * - key size: \c 0.
171 * - usage flags: \c 0.
172 * - algorithm: \c 0.
173 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200174 * A typical sequence to create a key is as follows:
175 * -# Create and initialize an attribute structure.
176 * -# If the key is persistent, call psa_make_key_persistent().
177 * -# Set the key policy with psa_set_key_usage_flags() and
178 * psa_set_key_algorithm().
179 * -# Set the key type with psa_set_key_type(). If the key type requires
180 * domain parameters, call psa_set_key_domain_parameters() instead.
181 * Skip this step if copying an existing key with psa_copy_key().
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100182 * -# When generating a random key with psa_generate_random_key() or deriving a key
183 * with psa_generate_derived_key(), set the desired key size with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200184 * psa_set_key_bits().
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100185 * -# Call a key creation function: psa_import_key(), psa_generate_random_key(),
186 * psa_generate_derived_key() or psa_copy_key(). This function reads
Gilles Peskine1ea5e442019-05-02 20:31:10 +0200187 * the attribute structure, creates a key with these attributes, and
188 * outputs a handle to the newly created key.
189 * -# The attribute structure is now no longer necessary. If you called
Gilles Peskine9c640f92019-04-28 11:36:21 +0200190 * psa_set_key_domain_parameters() earlier, you must call
191 * psa_reset_key_attributes() to free any resources used by the
192 * domain parameters. Otherwise calling psa_reset_key_attributes()
193 * is optional.
Gilles Peskine20628592019-04-19 19:29:50 +0200194 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200195 * A typical sequence to query a key's attributes is as follows:
196 * -# Call psa_get_key_attributes().
197 * -# Call `psa_get_key_xxx` functions to retrieve the attribute(s) that
198 * you are interested in.
199 * -# Call psa_reset_key_attributes() to free any resources that may be
200 * used by the attribute structure.
201 *
202 * Once a key has been created, it is impossible to change its attributes.
Gilles Peskine87a5e562019-04-17 12:28:25 +0200203 */
204typedef struct psa_key_attributes_s psa_key_attributes_t;
205
Gilles Peskine20628592019-04-19 19:29:50 +0200206/** Declare a key as persistent.
207 *
208 * This function does not access storage, it merely fills the attribute
209 * structure with given values. The persistent key will be written to
210 * storage when the attribute structure is passed to a key creation
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100211 * function such as psa_import_key(), psa_generate_random_key(),
212 * psa_generate_derived_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200213 *
214 * This function overwrites any identifier and lifetime values
215 * previously set in \p attributes.
216 *
217 * This function may be declared as `static` (i.e. without external
218 * linkage). This function may be provided as a function-like macro,
219 * but in this case it must evaluate each of its arguments exactly once.
220 *
221 * \param[out] attributes The attribute structure to write to.
222 * \param id The persistent identifier for the key.
223 * \param lifetime The lifetime for the key.
224 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
225 * key will be volatile, and \p id is ignored.
226 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200227static void psa_make_key_persistent(psa_key_attributes_t *attributes,
228 psa_key_id_t id,
229 psa_key_lifetime_t lifetime);
230
Gilles Peskine20628592019-04-19 19:29:50 +0200231/** Retrieve the key identifier from key attributes.
232 *
233 * This function may be declared as `static` (i.e. without external
234 * linkage). This function may be provided as a function-like macro,
235 * but in this case it must evaluate its argument exactly once.
236 *
237 * \param[in] attributes The key attribute structure to query.
238 *
239 * \return The persistent identifier stored in the attribute structure.
240 * This value is unspecified if the attribute structure declares
241 * the key as volatile.
242 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200243static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
244
Gilles Peskine20628592019-04-19 19:29:50 +0200245/** Retrieve the lifetime from key attributes.
246 *
247 * This function may be declared as `static` (i.e. without external
248 * linkage). This function may be provided as a function-like macro,
249 * but in this case it must evaluate its argument exactly once.
250 *
251 * \param[in] attributes The key attribute structure to query.
252 *
253 * \return The lifetime value stored in the attribute structure.
254 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200255static psa_key_lifetime_t psa_get_key_lifetime(
256 const psa_key_attributes_t *attributes);
257
Gilles Peskine20628592019-04-19 19:29:50 +0200258/** Declare usage flags for a key.
259 *
260 * Usage flags are part of a key's usage policy. They encode what
261 * kind of operations are permitted on the key. For more details,
262 * refer to the documentation of the type #psa_key_usage_t.
263 *
264 * This function overwrites any usage flags
265 * previously set in \p attributes.
266 *
267 * This function may be declared as `static` (i.e. without external
268 * linkage). This function may be provided as a function-like macro,
269 * but in this case it must evaluate each of its arguments exactly once.
270 *
271 * \param[out] attributes The attribute structure to write to.
272 * \param usage_flags The usage flags to write.
273 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200274static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
275 psa_key_usage_t usage_flags);
276
Gilles Peskine20628592019-04-19 19:29:50 +0200277/** Retrieve the usage flags from key attributes.
278 *
279 * This function may be declared as `static` (i.e. without external
280 * linkage). This function may be provided as a function-like macro,
281 * but in this case it must evaluate its argument exactly once.
282 *
283 * \param[in] attributes The key attribute structure to query.
284 *
285 * \return The usage flags stored in the attribute structure.
286 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200287static psa_key_usage_t psa_get_key_usage_flags(
288 const psa_key_attributes_t *attributes);
289
Gilles Peskine20628592019-04-19 19:29:50 +0200290/** Declare the permitted algorithm policy for a key.
291 *
292 * The permitted algorithm policy of a key encodes which algorithm or
293 * algorithms are permitted to be used with this key.
294 *
295 * This function overwrites any algorithm policy
296 * previously set in \p attributes.
297 *
298 * This function may be declared as `static` (i.e. without external
299 * linkage). This function may be provided as a function-like macro,
300 * but in this case it must evaluate each of its arguments exactly once.
301 *
302 * \param[out] attributes The attribute structure to write to.
303 * \param alg The permitted algorithm policy to write.
304 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200305static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
306 psa_algorithm_t alg);
307
Gilles Peskine20628592019-04-19 19:29:50 +0200308/** Retrieve the algorithm policy from key attributes.
309 *
310 * This function may be declared as `static` (i.e. without external
311 * linkage). This function may be provided as a function-like macro,
312 * but in this case it must evaluate its argument exactly once.
313 *
314 * \param[in] attributes The key attribute structure to query.
315 *
316 * \return The algorithm stored in the attribute structure.
317 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200318static psa_algorithm_t psa_get_key_algorithm(
319 const psa_key_attributes_t *attributes);
320
Gilles Peskine20628592019-04-19 19:29:50 +0200321/** Declare the type of a key.
322 *
323 * If a type requires domain parameters, you must call
324 * psa_set_key_domain_parameters() instead of this function.
325 *
326 * This function overwrites any key type and domain parameters
327 * previously set in \p attributes.
328 *
329 * This function may be declared as `static` (i.e. without external
330 * linkage). This function may be provided as a function-like macro,
331 * but in this case it must evaluate each of its arguments exactly once.
332 *
333 * \param[out] attributes The attribute structure to write to.
334 * \param type The key type to write.
335 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200336static void psa_set_key_type(psa_key_attributes_t *attributes,
337 psa_key_type_t type);
338
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200339/** Declare the size of a key.
340 *
341 * This function overwrites any key size previously set in \p attributes.
342 *
343 * This function may be declared as `static` (i.e. without external
344 * linkage). This function may be provided as a function-like macro,
345 * but in this case it must evaluate each of its arguments exactly once.
346 *
347 * \param[out] attributes The attribute structure to write to.
348 * \param bits The key size in bits.
349 */
350static void psa_set_key_bits(psa_key_attributes_t *attributes,
351 size_t bits);
352
Gilles Peskine20628592019-04-19 19:29:50 +0200353/** Retrieve the key type from key attributes.
354 *
355 * This function may be declared as `static` (i.e. without external
356 * linkage). This function may be provided as a function-like macro,
357 * but in this case it must evaluate its argument exactly once.
358 *
359 * \param[in] attributes The key attribute structure to query.
360 *
361 * \return The key type stored in the attribute structure.
362 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200363static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
364
Gilles Peskine20628592019-04-19 19:29:50 +0200365/** Retrieve the key size from key attributes.
366 *
367 * This function may be declared as `static` (i.e. without external
368 * linkage). This function may be provided as a function-like macro,
369 * but in this case it must evaluate its argument exactly once.
370 *
371 * \param[in] attributes The key attribute structure to query.
372 *
373 * \return The key size stored in the attribute structure, in bits.
374 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200375static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
376
Gilles Peskineb699f072019-04-26 16:06:02 +0200377/**
378 * \brief Set domain parameters for a key.
379 *
380 * Some key types require additional domain parameters in addition to
381 * the key type identifier and the key size.
382 * The format for the required domain parameters varies by the key type.
383 *
Gilles Peskinee56e8782019-04-26 17:34:02 +0200384 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEYPAIR),
385 * the domain parameter data consists of the public exponent,
Gilles Peskineb699f072019-04-26 16:06:02 +0200386 * represented as a big-endian integer with no leading zeros.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200387 * This information is used when generating an RSA key pair.
Gilles Peskineb699f072019-04-26 16:06:02 +0200388 * When importing a key, the public exponent is read from the imported
389 * key data and the exponent recorded in the attribute structure is ignored.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200390 * As an exception, the public exponent 65537 is represented by an empty
391 * byte string.
392 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEYPAIR),
Gilles Peskineb699f072019-04-26 16:06:02 +0200393 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
394 * ```
395 * Dss-Parms ::= SEQUENCE {
396 * p INTEGER,
397 * q INTEGER,
398 * g INTEGER
399 * }
400 * ```
Gilles Peskinee56e8782019-04-26 17:34:02 +0200401 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY or
402 * #PSA_KEY_TYPE_DH_KEYPAIR), the
Gilles Peskineb699f072019-04-26 16:06:02 +0200403 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
404 * ```
405 * DomainParameters ::= SEQUENCE {
406 * p INTEGER, -- odd prime, p=jq +1
407 * g INTEGER, -- generator, g
408 * q INTEGER, -- factor of p-1
409 * j INTEGER OPTIONAL, -- subgroup factor
410 * validationParms ValidationParms OPTIONAL
411 * }
412 * ValidationParms ::= SEQUENCE {
413 * seed BIT STRING,
414 * pgenCounter INTEGER
415 * }
416 * ```
417 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200418 * \note This function may allocate memory or other resources.
419 * Once you have called this function on an attribute structure,
420 * you must call psa_reset_key_attributes() to free these resources.
421 *
Gilles Peskineb699f072019-04-26 16:06:02 +0200422 * \param[in,out] attributes Attribute structure where the specified domain
423 * parameters will be stored.
424 * If this function fails, the content of
425 * \p attributes is not modified.
426 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
427 * \param[in] data Buffer containing the key domain parameters.
428 * The content of this buffer is interpreted
429 * according to \p type as described above.
430 * \param data_length Size of the \p data buffer in bytes.
431 *
432 * \retval #PSA_SUCCESS
433 * \retval #PSA_ERROR_INVALID_ARGUMENT
434 * \retval #PSA_ERROR_NOT_SUPPORTED
435 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
436 */
437psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
438 psa_key_type_t type,
439 const uint8_t *data,
440 size_t data_length);
441
442/**
443 * \brief Get domain parameters for a key.
444 *
445 * Get the domain parameters for a key with this function, if any. The format
446 * of the domain parameters written to \p data is specified in the
447 * documentation for psa_set_key_domain_parameters().
448 *
449 * \param[in] attributes The key attribute structure to query.
450 * \param[out] data On success, the key domain parameters.
451 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineaa02c172019-04-28 11:44:17 +0200452 * The buffer is guaranteed to be large
453 * enough if its size in bytes is at least
454 * the value given by
455 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
Gilles Peskineb699f072019-04-26 16:06:02 +0200456 * \param[out] data_length On success, the number of bytes
457 * that make up the key domain parameters data.
458 *
459 * \retval #PSA_SUCCESS
460 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
461 */
462psa_status_t psa_get_key_domain_parameters(
463 const psa_key_attributes_t *attributes,
464 uint8_t *data,
465 size_t data_size,
466 size_t *data_length);
467
Gilles Peskine20628592019-04-19 19:29:50 +0200468/** Retrieve the attributes of a key.
469 *
470 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200471 * psa_reset_key_attributes(). It then copies the attributes of
472 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200473 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200474 * \note This function may allocate memory or other resources.
475 * Once you have called this function on an attribute structure,
476 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200477 *
Gilles Peskine20628592019-04-19 19:29:50 +0200478 * \param[in] handle Handle to the key to query.
479 * \param[in,out] attributes On success, the attributes of the key.
480 * On failure, equivalent to a
481 * freshly-initialized structure.
482 *
483 * \retval #PSA_SUCCESS
484 * \retval #PSA_ERROR_INVALID_HANDLE
485 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
486 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
487 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200488psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
489 psa_key_attributes_t *attributes);
490
Gilles Peskine20628592019-04-19 19:29:50 +0200491/** Reset a key attribute structure to a freshly initialized state.
492 *
493 * You must initialize the attribute structure as described in the
494 * documentation of the type #psa_key_attributes_t before calling this
495 * function. Once the structure has been initialized, you may call this
496 * function at any time.
497 *
498 * This function frees any auxiliary resources that the structure
499 * may contain.
500 *
501 * \param[in,out] attributes The attribute structure to reset.
502 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200503void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200504
Gilles Peskine87a5e562019-04-17 12:28:25 +0200505/**@}*/
506
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100507/** \defgroup key_management Key management
508 * @{
509 */
510
Gilles Peskinef535eb22018-11-30 14:08:36 +0100511/** Open a handle to an existing persistent key.
512 *
513 * Open a handle to a key which was previously created with psa_create_key().
514 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100515 * \param id The persistent identifier of the key.
516 * \param[out] handle On success, a handle to a key slot which contains
517 * the data and metadata loaded from the specified
518 * persistent location.
519 *
520 * \retval #PSA_SUCCESS
521 * Success. The application can now use the value of `*handle`
522 * to access the newly allocated key slot.
523 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200524 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100525 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine225010f2019-05-06 18:44:55 +0200526 * \p id is invalid.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100527 * \retval #PSA_ERROR_NOT_PERMITTED
528 * The specified key exists, but the application does not have the
529 * permission to access it. Note that this specification does not
530 * define any way to create such a key, but it may be possible
531 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200532 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
533 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100534 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200535psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100536 psa_key_handle_t *handle);
537
Gilles Peskinef535eb22018-11-30 14:08:36 +0100538/** Close a key handle.
539 *
540 * If the handle designates a volatile key, destroy the key material and
541 * free all associated resources, just like psa_destroy_key().
542 *
543 * If the handle designates a persistent key, free all resources associated
544 * with the key in volatile memory. The key slot in persistent storage is
545 * not affected and can be opened again later with psa_open_key().
546 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100547 * If the key is currently in use in a multipart operation,
548 * the multipart operation is aborted.
549 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100550 * \param handle The key handle to close.
551 *
552 * \retval #PSA_SUCCESS
553 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100554 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100555 */
556psa_status_t psa_close_key(psa_key_handle_t handle);
557
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100558/**@}*/
559
560/** \defgroup import_export Key import and export
561 * @{
562 */
563
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100564/**
565 * \brief Import a key in binary format.
566 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100567 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100568 * documentation of psa_export_public_key() for the format of public keys
569 * and to the documentation of psa_export_key() for the format for
570 * other key types.
571 *
572 * This specification supports a single format for each key type.
573 * Implementations may support other formats as long as the standard
574 * format is supported. Implementations that support other formats
575 * should ensure that the formats are clearly unambiguous so as to
576 * minimize the risk that an invalid input is accidentally interpreted
577 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100578 *
Gilles Peskine20628592019-04-19 19:29:50 +0200579 * \param[in] attributes The attributes for the new key.
580 * The key size field in \p attributes is
581 * ignored; the actual key size is determined
582 * from the \p data buffer.
583 * \param[out] handle On success, a handle to the newly created key.
584 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100585 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine20628592019-04-19 19:29:50 +0200586 * buffer is interpreted according to the type and,
587 * if applicable, domain parameters declared in
588 * \p attributes.
589 * All implementations must support at least the format
590 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100591 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200592 * the chosen type. Implementations may allow other
593 * formats, but should be conservative: implementations
594 * should err on the side of rejecting content if it
595 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200596 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100597 *
Gilles Peskine28538492018-07-11 17:34:00 +0200598 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100599 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100600 * If the key is persistent, the key material and the key's metadata
601 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200602 * \retval #PSA_ERROR_ALREADY_EXISTS
603 * This is an attempt to create a persistent key, and there is
604 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200605 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200606 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200607 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200608 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200609 * The key attributes, as a whole, are invalid,
Gilles Peskine308b91d2018-02-08 09:47:44 +0100610 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200611 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
612 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
613 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100614 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200615 * \retval #PSA_ERROR_HARDWARE_FAILURE
616 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300617 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300618 * The library has not been previously initialized by psa_crypto_init().
619 * It is implementation-dependent whether a failure to initialize
620 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100621 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200622psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
623 psa_key_handle_t *handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100624 const uint8_t *data,
625 size_t data_length);
626
627/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100628 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200629 *
630 * This function destroys the content of the key slot from both volatile
631 * memory and, if applicable, non-volatile storage. Implementations shall
632 * make a best effort to ensure that any previous content of the slot is
633 * unrecoverable.
634 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100635 * This function also erases any metadata such as policies and frees all
636 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200637 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100638 * If the key is currently in use in a multipart operation,
639 * the multipart operation is aborted.
640 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100641 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100642 *
Gilles Peskine28538492018-07-11 17:34:00 +0200643 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200644 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200645 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200646 * The slot holds content and cannot be erased because it is
647 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100648 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200649 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200650 * There was an failure in communication with the cryptoprocessor.
651 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200652 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200653 * The storage is corrupted. Implementations shall make a best effort
654 * to erase key material even in this stage, however applications
655 * should be aware that it may be impossible to guarantee that the
656 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200657 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200658 * An unexpected condition which is not a storage corruption or
659 * a communication failure occurred. The cryptoprocessor may have
660 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300661 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300662 * The library has not been previously initialized by psa_crypto_init().
663 * It is implementation-dependent whether a failure to initialize
664 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100665 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100666psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100667
668/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100669 * \brief Export a key in binary format.
670 *
671 * The output of this function can be passed to psa_import_key() to
672 * create an equivalent object.
673 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100674 * If the implementation of psa_import_key() supports other formats
675 * beyond the format specified here, the output from psa_export_key()
676 * must use the representation specified here, not the original
677 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100679 * For standard key types, the output format is as follows:
680 *
681 * - For symmetric keys (including MAC keys), the format is the
682 * raw bytes of the key.
683 * - For DES, the key data consists of 8 bytes. The parity bits must be
684 * correct.
685 * - For Triple-DES, the format is the concatenation of the
686 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100687 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200688 * is the non-encrypted DER encoding of the representation defined by
689 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
690 * ```
691 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200692 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200693 * modulus INTEGER, -- n
694 * publicExponent INTEGER, -- e
695 * privateExponent INTEGER, -- d
696 * prime1 INTEGER, -- p
697 * prime2 INTEGER, -- q
698 * exponent1 INTEGER, -- d mod (p-1)
699 * exponent2 INTEGER, -- d mod (q-1)
700 * coefficient INTEGER, -- (inverse of q) mod p
701 * }
702 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000703 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
704 * representation of the private key `x` as a big-endian byte string. The
705 * length of the byte string is the private key size in bytes (leading zeroes
706 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200707 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100708 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100709 * a representation of the private value as a `ceiling(m/8)`-byte string
710 * where `m` is the bit size associated with the curve, i.e. the bit size
711 * of the order of the curve's coordinate field. This byte string is
712 * in little-endian order for Montgomery curves (curve types
713 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
714 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
715 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100716 * This is the content of the `privateKey` field of the `ECPrivateKey`
717 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000718 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
719 * format is the representation of the private key `x` as a big-endian byte
720 * string. The length of the byte string is the private key size in bytes
721 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200722 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
723 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100724 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100725 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200726 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200727 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200728 * \param[out] data_length On success, the number of bytes
729 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100730 *
Gilles Peskine28538492018-07-11 17:34:00 +0200731 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100732 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200733 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200734 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100735 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200736 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
737 * The size of the \p data buffer is too small. You can determine a
738 * sufficient buffer size by calling
739 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
740 * where \c type is the key type
741 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200742 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
743 * \retval #PSA_ERROR_HARDWARE_FAILURE
744 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300745 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300746 * The library has not been previously initialized by psa_crypto_init().
747 * It is implementation-dependent whether a failure to initialize
748 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100749 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100750psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100751 uint8_t *data,
752 size_t data_size,
753 size_t *data_length);
754
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100755/**
756 * \brief Export a public key or the public part of a key pair in binary format.
757 *
758 * The output of this function can be passed to psa_import_key() to
759 * create an object that is equivalent to the public key.
760 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000761 * This specification supports a single format for each key type.
762 * Implementations may support other formats as long as the standard
763 * format is supported. Implementations that support other formats
764 * should ensure that the formats are clearly unambiguous so as to
765 * minimize the risk that an invalid input is accidentally interpreted
766 * according to a different format.
767 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000768 * For standard key types, the output format is as follows:
769 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
770 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
771 * ```
772 * RSAPublicKey ::= SEQUENCE {
773 * modulus INTEGER, -- n
774 * publicExponent INTEGER } -- e
775 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000776 * - For elliptic curve public keys (key types for which
777 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
778 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
779 * Let `m` be the bit size associated with the curve, i.e. the bit size of
780 * `q` for a curve over `F_q`. The representation consists of:
781 * - The byte 0x04;
782 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
783 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000784 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
785 * representation of the public key `y = g^x mod p` as a big-endian byte
786 * string. The length of the byte string is the length of the base prime `p`
787 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000788 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
789 * the format is the representation of the public key `y = g^x mod p` as a
790 * big-endian byte string. The length of the byte string is the length of the
791 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100792 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100793 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200794 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200795 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200796 * \param[out] data_length On success, the number of bytes
797 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100798 *
Gilles Peskine28538492018-07-11 17:34:00 +0200799 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100800 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200801 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200802 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200803 * The key is neither a public key nor a key pair.
804 * \retval #PSA_ERROR_NOT_SUPPORTED
805 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
806 * The size of the \p data buffer is too small. You can determine a
807 * sufficient buffer size by calling
808 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
809 * where \c type is the key type
810 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200811 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
812 * \retval #PSA_ERROR_HARDWARE_FAILURE
813 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300814 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300815 * The library has not been previously initialized by psa_crypto_init().
816 * It is implementation-dependent whether a failure to initialize
817 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100818 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100819psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100820 uint8_t *data,
821 size_t data_size,
822 size_t *data_length);
823
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100824/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100825 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100826 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000827 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100828 * This function is primarily useful to copy a key from one location
829 * to another, since it populates a key using the material from
830 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200831 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100832 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100833 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100834 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100835 *
Gilles Peskine20628592019-04-19 19:29:50 +0200836 * The resulting key may only be used in a way that conforms to
837 * both the policy of the original key and the policy specified in
838 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100839 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200840 * usage flags on the source policy and the usage flags in \p attributes.
841 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100842 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200843 * - If either of the policies allows an algorithm and the other policy
844 * allows a wildcard-based algorithm policy that includes this algorithm,
845 * the resulting key allows the same algorithm.
846 * - If the policies do not allow any algorithm in common, this function
847 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200848 *
Gilles Peskine20628592019-04-19 19:29:50 +0200849 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100850 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200851 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100852 * \param source_handle The key to copy. It must be a handle to an
853 * occupied slot.
Gilles Peskine20628592019-04-19 19:29:50 +0200854 * \param[in] attributes The attributes for the new key.
855 * They are used as follows:
856 * - The key type, key size and domain parameters
857 * are ignored. This information is copied
858 * from the source key.
859 * - The key location (the lifetime and, for
860 * persistent keys, the key identifier) is
861 * used directly.
862 * - The policy constraints (usage flags and
863 * algorithm policy) are combined from
864 * the source key and \p attributes so that
865 * both sets of restrictions apply, as
866 * described in the documentation of this function.
867 * \param[out] target_handle On success, a handle to the newly created key.
868 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200869 *
870 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100871 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200872 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200873 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200874 * This is an attempt to create a persistent key, and there is
875 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200876 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200877 * The lifetime or identifier in \p attributes are invalid.
878 * \retval #PSA_ERROR_INVALID_ARGUMENT
879 * The policy constraints on the source and specified in
880 * \p attributes are incompatible.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100881 * \retval #PSA_ERROR_NOT_PERMITTED
882 * The source key is not exportable and its lifetime does not
883 * allow copying it to the target's lifetime.
884 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
885 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200886 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
887 * \retval #PSA_ERROR_HARDWARE_FAILURE
888 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100889 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100890psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200891 const psa_key_attributes_t *attributes,
892 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100893
894/**@}*/
895
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100896/** \defgroup hash Message digests
897 * @{
898 */
899
Gilles Peskine69647a42019-01-14 20:18:12 +0100900/** Calculate the hash (digest) of a message.
901 *
902 * \note To verify the hash of a message against an
903 * expected value, use psa_hash_compare() instead.
904 *
905 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
906 * such that #PSA_ALG_IS_HASH(\p alg) is true).
907 * \param[in] input Buffer containing the message to hash.
908 * \param input_length Size of the \p input buffer in bytes.
909 * \param[out] hash Buffer where the hash is to be written.
910 * \param hash_size Size of the \p hash buffer in bytes.
911 * \param[out] hash_length On success, the number of bytes
912 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100913 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100914 *
915 * \retval #PSA_SUCCESS
916 * Success.
917 * \retval #PSA_ERROR_NOT_SUPPORTED
918 * \p alg is not supported or is not a hash algorithm.
919 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
920 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
921 * \retval #PSA_ERROR_HARDWARE_FAILURE
922 * \retval #PSA_ERROR_TAMPERING_DETECTED
923 */
924psa_status_t psa_hash_compute(psa_algorithm_t alg,
925 const uint8_t *input,
926 size_t input_length,
927 uint8_t *hash,
928 size_t hash_size,
929 size_t *hash_length);
930
931/** Calculate the hash (digest) of a message and compare it with a
932 * reference value.
933 *
934 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
935 * such that #PSA_ALG_IS_HASH(\p alg) is true).
936 * \param[in] input Buffer containing the message to hash.
937 * \param input_length Size of the \p input buffer in bytes.
938 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100939 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100940 *
941 * \retval #PSA_SUCCESS
942 * The expected hash is identical to the actual hash of the input.
943 * \retval #PSA_ERROR_INVALID_SIGNATURE
944 * The hash of the message was calculated successfully, but it
945 * differs from the expected hash.
946 * \retval #PSA_ERROR_NOT_SUPPORTED
947 * \p alg is not supported or is not a hash algorithm.
948 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
949 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
950 * \retval #PSA_ERROR_HARDWARE_FAILURE
951 * \retval #PSA_ERROR_TAMPERING_DETECTED
952 */
953psa_status_t psa_hash_compare(psa_algorithm_t alg,
954 const uint8_t *input,
955 size_t input_length,
956 const uint8_t *hash,
957 const size_t hash_length);
958
Gilles Peskine308b91d2018-02-08 09:47:44 +0100959/** The type of the state data structure for multipart hash operations.
960 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000961 * Before calling any function on a hash operation object, the application must
962 * initialize it by any of the following means:
963 * - Set the structure to all-bits-zero, for example:
964 * \code
965 * psa_hash_operation_t operation;
966 * memset(&operation, 0, sizeof(operation));
967 * \endcode
968 * - Initialize the structure to logical zero values, for example:
969 * \code
970 * psa_hash_operation_t operation = {0};
971 * \endcode
972 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
973 * for example:
974 * \code
975 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
976 * \endcode
977 * - Assign the result of the function psa_hash_operation_init()
978 * to the structure, for example:
979 * \code
980 * psa_hash_operation_t operation;
981 * operation = psa_hash_operation_init();
982 * \endcode
983 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100984 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100985 * make any assumptions about the content of this structure except
986 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100987typedef struct psa_hash_operation_s psa_hash_operation_t;
988
Jaeden Amero6a25b412019-01-04 11:47:44 +0000989/** \def PSA_HASH_OPERATION_INIT
990 *
991 * This macro returns a suitable initializer for a hash operation object
992 * of type #psa_hash_operation_t.
993 */
994#ifdef __DOXYGEN_ONLY__
995/* This is an example definition for documentation purposes.
996 * Implementations should define a suitable value in `crypto_struct.h`.
997 */
998#define PSA_HASH_OPERATION_INIT {0}
999#endif
1000
1001/** Return an initial value for a hash operation object.
1002 */
1003static psa_hash_operation_t psa_hash_operation_init(void);
1004
Gilles Peskinef45adda2019-01-14 18:29:18 +01001005/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001006 *
1007 * The sequence of operations to calculate a hash (message digest)
1008 * is as follows:
1009 * -# Allocate an operation object which will be passed to all the functions
1010 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +00001011 * -# Initialize the operation object with one of the methods described in the
1012 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001013 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001014 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001015 * of the message each time. The hash that is calculated is the hash
1016 * of the concatenation of these messages in order.
1017 * -# To calculate the hash, call psa_hash_finish().
1018 * To compare the hash with an expected value, call psa_hash_verify().
1019 *
1020 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +00001021 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001022 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001023 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001024 * eventually terminate the operation. The following events terminate an
1025 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001026 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001027 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001028 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001029 * \param[in,out] operation The operation object to set up. It must have
1030 * been initialized as per the documentation for
1031 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001032 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1033 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001034 *
Gilles Peskine28538492018-07-11 17:34:00 +02001035 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001036 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001037 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001038 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001039 * \retval #PSA_ERROR_BAD_STATE
1040 * The operation state is not valid (already set up and not
1041 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001042 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1043 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1044 * \retval #PSA_ERROR_HARDWARE_FAILURE
1045 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001046 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001047psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001048 psa_algorithm_t alg);
1049
Gilles Peskine308b91d2018-02-08 09:47:44 +01001050/** Add a message fragment to a multipart hash operation.
1051 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001052 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001053 *
1054 * If this function returns an error status, the operation becomes inactive.
1055 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001056 * \param[in,out] operation Active hash operation.
1057 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001058 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001059 *
Gilles Peskine28538492018-07-11 17:34:00 +02001060 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001061 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001062 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001063 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001064 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1065 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1066 * \retval #PSA_ERROR_HARDWARE_FAILURE
1067 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001068 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001069psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1070 const uint8_t *input,
1071 size_t input_length);
1072
Gilles Peskine308b91d2018-02-08 09:47:44 +01001073/** Finish the calculation of the hash of a message.
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 * This function calculates the hash of the message formed by concatenating
1077 * the inputs passed to preceding calls to psa_hash_update().
1078 *
1079 * When this function returns, the operation becomes inactive.
1080 *
1081 * \warning Applications should not call this function if they expect
1082 * a specific value for the hash. Call psa_hash_verify() instead.
1083 * Beware that comparing integrity or authenticity data such as
1084 * hash values with a function such as \c memcmp is risky
1085 * because the time taken by the comparison may leak information
1086 * about the hashed data which could allow an attacker to guess
1087 * a valid hash and thereby bypass security controls.
1088 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001089 * \param[in,out] operation Active hash operation.
1090 * \param[out] hash Buffer where the hash is to be written.
1091 * \param hash_size Size of the \p hash buffer in bytes.
1092 * \param[out] hash_length On success, the number of bytes
1093 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001094 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001095 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001096 *
Gilles Peskine28538492018-07-11 17:34:00 +02001097 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001098 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001099 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001100 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001101 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001102 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001103 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001104 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001105 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1106 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1107 * \retval #PSA_ERROR_HARDWARE_FAILURE
1108 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001109 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001110psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1111 uint8_t *hash,
1112 size_t hash_size,
1113 size_t *hash_length);
1114
Gilles Peskine308b91d2018-02-08 09:47:44 +01001115/** Finish the calculation of the hash of a message and compare it with
1116 * an expected value.
1117 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001118 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001119 * This function calculates the hash of the message formed by concatenating
1120 * the inputs passed to preceding calls to psa_hash_update(). It then
1121 * compares the calculated hash with the expected hash passed as a
1122 * parameter to this function.
1123 *
1124 * When this function returns, the operation becomes inactive.
1125 *
Gilles Peskine19067982018-03-20 17:54:53 +01001126 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001127 * comparison between the actual hash and the expected hash is performed
1128 * in constant time.
1129 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001130 * \param[in,out] operation Active hash operation.
1131 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001132 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001133 *
Gilles Peskine28538492018-07-11 17:34:00 +02001134 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001135 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001136 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001137 * The hash of the message was calculated successfully, but it
1138 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001139 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001140 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001141 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1142 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1143 * \retval #PSA_ERROR_HARDWARE_FAILURE
1144 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001145 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001146psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1147 const uint8_t *hash,
1148 size_t hash_length);
1149
Gilles Peskine308b91d2018-02-08 09:47:44 +01001150/** Abort a hash operation.
1151 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001152 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001153 * \p operation structure itself. Once aborted, the operation object
1154 * can be reused for another operation by calling
1155 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001156 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001157 * You may call this function any time after the operation object has
1158 * been initialized by any of the following methods:
1159 * - A call to psa_hash_setup(), whether it succeeds or not.
1160 * - Initializing the \c struct to all-bits-zero.
1161 * - Initializing the \c struct to logical zeros, e.g.
1162 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001163 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001164 * In particular, calling psa_hash_abort() after the operation has been
1165 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1166 * psa_hash_verify() is safe and has no effect.
1167 *
1168 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001169 *
Gilles Peskine28538492018-07-11 17:34:00 +02001170 * \retval #PSA_SUCCESS
1171 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001172 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001173 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1174 * \retval #PSA_ERROR_HARDWARE_FAILURE
1175 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001176 */
1177psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001178
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001179/** Clone a hash operation.
1180 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001181 * This function copies the state of an ongoing hash operation to
1182 * a new operation object. In other words, this function is equivalent
1183 * to calling psa_hash_setup() on \p target_operation with the same
1184 * algorithm that \p source_operation was set up for, then
1185 * psa_hash_update() on \p target_operation with the same input that
1186 * that was passed to \p source_operation. After this function returns, the
1187 * two objects are independent, i.e. subsequent calls involving one of
1188 * the objects do not affect the other object.
1189 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001190 * \param[in] source_operation The active hash operation to clone.
1191 * \param[in,out] target_operation The operation object to set up.
1192 * It must be initialized but not active.
1193 *
1194 * \retval #PSA_SUCCESS
1195 * \retval #PSA_ERROR_BAD_STATE
1196 * \p source_operation is not an active hash operation.
1197 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001198 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001199 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1200 * \retval #PSA_ERROR_HARDWARE_FAILURE
1201 * \retval #PSA_ERROR_TAMPERING_DETECTED
1202 */
1203psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1204 psa_hash_operation_t *target_operation);
1205
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001206/**@}*/
1207
Gilles Peskine8c9def32018-02-08 10:02:12 +01001208/** \defgroup MAC Message authentication codes
1209 * @{
1210 */
1211
Gilles Peskine69647a42019-01-14 20:18:12 +01001212/** Calculate the MAC (message authentication code) of a message.
1213 *
1214 * \note To verify the MAC of a message against an
1215 * expected value, use psa_mac_verify() instead.
1216 * Beware that comparing integrity or authenticity data such as
1217 * MAC values with a function such as \c memcmp is risky
1218 * because the time taken by the comparison may leak information
1219 * about the MAC value which could allow an attacker to guess
1220 * a valid MAC and thereby bypass security controls.
1221 *
1222 * \param handle Handle to the key to use for the operation.
1223 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001224 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001225 * \param[in] input Buffer containing the input message.
1226 * \param input_length Size of the \p input buffer in bytes.
1227 * \param[out] mac Buffer where the MAC value is to be written.
1228 * \param mac_size Size of the \p mac buffer in bytes.
1229 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001230 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001231 *
1232 * \retval #PSA_SUCCESS
1233 * Success.
1234 * \retval #PSA_ERROR_INVALID_HANDLE
1235 * \retval #PSA_ERROR_EMPTY_SLOT
1236 * \retval #PSA_ERROR_NOT_PERMITTED
1237 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001238 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001239 * \retval #PSA_ERROR_NOT_SUPPORTED
1240 * \p alg is not supported or is not a MAC algorithm.
1241 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1242 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1243 * \retval #PSA_ERROR_HARDWARE_FAILURE
1244 * \retval #PSA_ERROR_TAMPERING_DETECTED
1245 * \retval #PSA_ERROR_BAD_STATE
1246 * The library has not been previously initialized by psa_crypto_init().
1247 * It is implementation-dependent whether a failure to initialize
1248 * results in this error code.
1249 */
1250psa_status_t psa_mac_compute(psa_key_handle_t handle,
1251 psa_algorithm_t alg,
1252 const uint8_t *input,
1253 size_t input_length,
1254 uint8_t *mac,
1255 size_t mac_size,
1256 size_t *mac_length);
1257
1258/** Calculate the MAC of a message and compare it with a reference value.
1259 *
1260 * \param handle Handle to the key to use for the operation.
1261 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001262 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001263 * \param[in] input Buffer containing the input message.
1264 * \param input_length Size of the \p input buffer in bytes.
1265 * \param[out] mac Buffer containing the expected MAC value.
1266 * \param mac_length Size of the \p mac buffer in bytes.
1267 *
1268 * \retval #PSA_SUCCESS
1269 * The expected MAC is identical to the actual MAC of the input.
1270 * \retval #PSA_ERROR_INVALID_SIGNATURE
1271 * The MAC of the message was calculated successfully, but it
1272 * differs from the expected value.
1273 * \retval #PSA_ERROR_INVALID_HANDLE
1274 * \retval #PSA_ERROR_EMPTY_SLOT
1275 * \retval #PSA_ERROR_NOT_PERMITTED
1276 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001277 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001278 * \retval #PSA_ERROR_NOT_SUPPORTED
1279 * \p alg is not supported or is not a MAC algorithm.
1280 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1281 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1282 * \retval #PSA_ERROR_HARDWARE_FAILURE
1283 * \retval #PSA_ERROR_TAMPERING_DETECTED
1284 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001285psa_status_t psa_mac_verify(psa_key_handle_t handle,
1286 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001287 const uint8_t *input,
1288 size_t input_length,
1289 const uint8_t *mac,
1290 const size_t mac_length);
1291
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001292/** The type of the state data structure for multipart MAC operations.
1293 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001294 * Before calling any function on a MAC operation object, the application must
1295 * initialize it by any of the following means:
1296 * - Set the structure to all-bits-zero, for example:
1297 * \code
1298 * psa_mac_operation_t operation;
1299 * memset(&operation, 0, sizeof(operation));
1300 * \endcode
1301 * - Initialize the structure to logical zero values, for example:
1302 * \code
1303 * psa_mac_operation_t operation = {0};
1304 * \endcode
1305 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1306 * for example:
1307 * \code
1308 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1309 * \endcode
1310 * - Assign the result of the function psa_mac_operation_init()
1311 * to the structure, for example:
1312 * \code
1313 * psa_mac_operation_t operation;
1314 * operation = psa_mac_operation_init();
1315 * \endcode
1316 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001317 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001318 * make any assumptions about the content of this structure except
1319 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001320typedef struct psa_mac_operation_s psa_mac_operation_t;
1321
Jaeden Amero769ce272019-01-04 11:48:03 +00001322/** \def PSA_MAC_OPERATION_INIT
1323 *
1324 * This macro returns a suitable initializer for a MAC operation object of type
1325 * #psa_mac_operation_t.
1326 */
1327#ifdef __DOXYGEN_ONLY__
1328/* This is an example definition for documentation purposes.
1329 * Implementations should define a suitable value in `crypto_struct.h`.
1330 */
1331#define PSA_MAC_OPERATION_INIT {0}
1332#endif
1333
1334/** Return an initial value for a MAC operation object.
1335 */
1336static psa_mac_operation_t psa_mac_operation_init(void);
1337
Gilles Peskinef45adda2019-01-14 18:29:18 +01001338/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001339 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001340 * This function sets up the calculation of the MAC
1341 * (message authentication code) of a byte string.
1342 * To verify the MAC of a message against an
1343 * expected value, use psa_mac_verify_setup() instead.
1344 *
1345 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001346 * -# Allocate an operation object which will be passed to all the functions
1347 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001348 * -# Initialize the operation object with one of the methods described in the
1349 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001350 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001351 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1352 * of the message each time. The MAC that is calculated is the MAC
1353 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001354 * -# At the end of the message, call psa_mac_sign_finish() to finish
1355 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001356 *
1357 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001358 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001359 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001360 * After a successful call to psa_mac_sign_setup(), the application must
1361 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001362 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001363 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001364 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001365 * \param[in,out] operation The operation object to set up. It must have
1366 * been initialized as per the documentation for
1367 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001368 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001369 * It must remain valid until the operation
1370 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001371 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001372 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001373 *
Gilles Peskine28538492018-07-11 17:34:00 +02001374 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001375 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001376 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001377 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001378 * \retval #PSA_ERROR_NOT_PERMITTED
1379 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001380 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001381 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001382 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001383 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1384 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1385 * \retval #PSA_ERROR_HARDWARE_FAILURE
1386 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001387 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001388 * The operation state is not valid (already set up and not
1389 * subsequently completed).
1390 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001391 * The library has not been previously initialized by psa_crypto_init().
1392 * It is implementation-dependent whether a failure to initialize
1393 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001394 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001395psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001396 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001397 psa_algorithm_t alg);
1398
Gilles Peskinef45adda2019-01-14 18:29:18 +01001399/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001400 *
1401 * This function sets up the verification of the MAC
1402 * (message authentication code) of a byte string against an expected value.
1403 *
1404 * The sequence of operations to verify a MAC is as follows:
1405 * -# Allocate an operation object which will be passed to all the functions
1406 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001407 * -# Initialize the operation object with one of the methods described in the
1408 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001409 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001410 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1411 * of the message each time. The MAC that is calculated is the MAC
1412 * of the concatenation of these messages in order.
1413 * -# At the end of the message, call psa_mac_verify_finish() to finish
1414 * calculating the actual MAC of the message and verify it against
1415 * the expected value.
1416 *
1417 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001418 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001419 *
1420 * After a successful call to psa_mac_verify_setup(), the application must
1421 * eventually terminate the operation through one of the following methods:
1422 * - A failed call to psa_mac_update().
1423 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1424 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001425 * \param[in,out] operation The operation object to set up. It must have
1426 * been initialized as per the documentation for
1427 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001428 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001429 * It must remain valid until the operation
1430 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001431 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1432 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001433 *
Gilles Peskine28538492018-07-11 17:34:00 +02001434 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001435 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001436 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001437 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001438 * \retval #PSA_ERROR_NOT_PERMITTED
1439 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001440 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001441 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001442 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001443 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1444 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1445 * \retval #PSA_ERROR_HARDWARE_FAILURE
1446 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001447 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001448 * The operation state is not valid (already set up and not
1449 * subsequently completed).
1450 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001451 * The library has not been previously initialized by psa_crypto_init().
1452 * It is implementation-dependent whether a failure to initialize
1453 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001454 */
1455psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001456 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001457 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001458
Gilles Peskinedcd14942018-07-12 00:30:52 +02001459/** Add a message fragment to a multipart MAC operation.
1460 *
1461 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1462 * before calling this function.
1463 *
1464 * If this function returns an error status, the operation becomes inactive.
1465 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001466 * \param[in,out] operation Active MAC operation.
1467 * \param[in] input Buffer containing the message fragment to add to
1468 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001469 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001470 *
1471 * \retval #PSA_SUCCESS
1472 * Success.
1473 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001474 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001475 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1476 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1477 * \retval #PSA_ERROR_HARDWARE_FAILURE
1478 * \retval #PSA_ERROR_TAMPERING_DETECTED
1479 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001480psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1481 const uint8_t *input,
1482 size_t input_length);
1483
Gilles Peskinedcd14942018-07-12 00:30:52 +02001484/** Finish the calculation of the MAC of a message.
1485 *
1486 * The application must call psa_mac_sign_setup() before calling this function.
1487 * This function calculates the MAC of the message formed by concatenating
1488 * the inputs passed to preceding calls to psa_mac_update().
1489 *
1490 * When this function returns, the operation becomes inactive.
1491 *
1492 * \warning Applications should not call this function if they expect
1493 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1494 * Beware that comparing integrity or authenticity data such as
1495 * MAC values with a function such as \c memcmp is risky
1496 * because the time taken by the comparison may leak information
1497 * about the MAC value which could allow an attacker to guess
1498 * a valid MAC and thereby bypass security controls.
1499 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001500 * \param[in,out] operation Active MAC operation.
1501 * \param[out] mac Buffer where the MAC value is to be written.
1502 * \param mac_size Size of the \p mac buffer in bytes.
1503 * \param[out] mac_length On success, the number of bytes
1504 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001505 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001506 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001507 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001508 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001509 *
1510 * \retval #PSA_SUCCESS
1511 * Success.
1512 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001513 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001514 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001515 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001516 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1517 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1518 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1519 * \retval #PSA_ERROR_HARDWARE_FAILURE
1520 * \retval #PSA_ERROR_TAMPERING_DETECTED
1521 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001522psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1523 uint8_t *mac,
1524 size_t mac_size,
1525 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001526
Gilles Peskinedcd14942018-07-12 00:30:52 +02001527/** Finish the calculation of the MAC of a message and compare it with
1528 * an expected value.
1529 *
1530 * The application must call psa_mac_verify_setup() before calling this function.
1531 * This function calculates the MAC of the message formed by concatenating
1532 * the inputs passed to preceding calls to psa_mac_update(). It then
1533 * compares the calculated MAC with the expected MAC passed as a
1534 * parameter to this function.
1535 *
1536 * When this function returns, the operation becomes inactive.
1537 *
1538 * \note Implementations shall make the best effort to ensure that the
1539 * comparison between the actual MAC and the expected MAC is performed
1540 * in constant time.
1541 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001542 * \param[in,out] operation Active MAC operation.
1543 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001544 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001545 *
1546 * \retval #PSA_SUCCESS
1547 * The expected MAC is identical to the actual MAC of the message.
1548 * \retval #PSA_ERROR_INVALID_SIGNATURE
1549 * The MAC of the message was calculated successfully, but it
1550 * differs from the expected MAC.
1551 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001552 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001553 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1554 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1555 * \retval #PSA_ERROR_HARDWARE_FAILURE
1556 * \retval #PSA_ERROR_TAMPERING_DETECTED
1557 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001558psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1559 const uint8_t *mac,
1560 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001561
Gilles Peskinedcd14942018-07-12 00:30:52 +02001562/** Abort a MAC operation.
1563 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001564 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001565 * \p operation structure itself. Once aborted, the operation object
1566 * can be reused for another operation by calling
1567 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001568 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001569 * You may call this function any time after the operation object has
1570 * been initialized by any of the following methods:
1571 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1572 * it succeeds or not.
1573 * - Initializing the \c struct to all-bits-zero.
1574 * - Initializing the \c struct to logical zeros, e.g.
1575 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001576 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001577 * In particular, calling psa_mac_abort() after the operation has been
1578 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1579 * psa_mac_verify_finish() is safe and has no effect.
1580 *
1581 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001582 *
1583 * \retval #PSA_SUCCESS
1584 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001585 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001586 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1587 * \retval #PSA_ERROR_HARDWARE_FAILURE
1588 * \retval #PSA_ERROR_TAMPERING_DETECTED
1589 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001590psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1591
1592/**@}*/
1593
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001594/** \defgroup cipher Symmetric ciphers
1595 * @{
1596 */
1597
Gilles Peskine69647a42019-01-14 20:18:12 +01001598/** Encrypt a message using a symmetric cipher.
1599 *
1600 * This function encrypts a message with a random IV (initialization
1601 * vector).
1602 *
1603 * \param handle Handle to the key to use for the operation.
1604 * It must remain valid until the operation
1605 * terminates.
1606 * \param alg The cipher algorithm to compute
1607 * (\c PSA_ALG_XXX value such that
1608 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1609 * \param[in] input Buffer containing the message to encrypt.
1610 * \param input_length Size of the \p input buffer in bytes.
1611 * \param[out] output Buffer where the output is to be written.
1612 * The output contains the IV followed by
1613 * the ciphertext proper.
1614 * \param output_size Size of the \p output buffer in bytes.
1615 * \param[out] output_length On success, the number of bytes
1616 * that make up the output.
1617 *
1618 * \retval #PSA_SUCCESS
1619 * Success.
1620 * \retval #PSA_ERROR_INVALID_HANDLE
1621 * \retval #PSA_ERROR_EMPTY_SLOT
1622 * \retval #PSA_ERROR_NOT_PERMITTED
1623 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001624 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001625 * \retval #PSA_ERROR_NOT_SUPPORTED
1626 * \p alg is not supported or is not a cipher algorithm.
1627 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1628 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1629 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1630 * \retval #PSA_ERROR_HARDWARE_FAILURE
1631 * \retval #PSA_ERROR_TAMPERING_DETECTED
1632 */
1633psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1634 psa_algorithm_t alg,
1635 const uint8_t *input,
1636 size_t input_length,
1637 uint8_t *output,
1638 size_t output_size,
1639 size_t *output_length);
1640
1641/** Decrypt a message using a symmetric cipher.
1642 *
1643 * This function decrypts a message encrypted with a symmetric cipher.
1644 *
1645 * \param handle Handle to the key to use for the operation.
1646 * It must remain valid until the operation
1647 * terminates.
1648 * \param alg The cipher algorithm to compute
1649 * (\c PSA_ALG_XXX value such that
1650 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1651 * \param[in] input Buffer containing the message to decrypt.
1652 * This consists of the IV followed by the
1653 * ciphertext proper.
1654 * \param input_length Size of the \p input buffer in bytes.
1655 * \param[out] output Buffer where the plaintext is to be written.
1656 * \param output_size Size of the \p output buffer in bytes.
1657 * \param[out] output_length On success, the number of bytes
1658 * that make up the output.
1659 *
1660 * \retval #PSA_SUCCESS
1661 * Success.
1662 * \retval #PSA_ERROR_INVALID_HANDLE
1663 * \retval #PSA_ERROR_EMPTY_SLOT
1664 * \retval #PSA_ERROR_NOT_PERMITTED
1665 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001666 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001667 * \retval #PSA_ERROR_NOT_SUPPORTED
1668 * \p alg is not supported or is not a cipher algorithm.
1669 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1670 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1671 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1672 * \retval #PSA_ERROR_HARDWARE_FAILURE
1673 * \retval #PSA_ERROR_TAMPERING_DETECTED
1674 */
1675psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1676 psa_algorithm_t alg,
1677 const uint8_t *input,
1678 size_t input_length,
1679 uint8_t *output,
1680 size_t output_size,
1681 size_t *output_length);
1682
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001683/** The type of the state data structure for multipart cipher operations.
1684 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001685 * Before calling any function on a cipher operation object, the application
1686 * must initialize it by any of the following means:
1687 * - Set the structure to all-bits-zero, for example:
1688 * \code
1689 * psa_cipher_operation_t operation;
1690 * memset(&operation, 0, sizeof(operation));
1691 * \endcode
1692 * - Initialize the structure to logical zero values, for example:
1693 * \code
1694 * psa_cipher_operation_t operation = {0};
1695 * \endcode
1696 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1697 * for example:
1698 * \code
1699 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1700 * \endcode
1701 * - Assign the result of the function psa_cipher_operation_init()
1702 * to the structure, for example:
1703 * \code
1704 * psa_cipher_operation_t operation;
1705 * operation = psa_cipher_operation_init();
1706 * \endcode
1707 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001708 * This is an implementation-defined \c struct. Applications should not
1709 * make any assumptions about the content of this structure except
1710 * as directed by the documentation of a specific implementation. */
1711typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1712
Jaeden Amero5bae2272019-01-04 11:48:27 +00001713/** \def PSA_CIPHER_OPERATION_INIT
1714 *
1715 * This macro returns a suitable initializer for a cipher operation object of
1716 * type #psa_cipher_operation_t.
1717 */
1718#ifdef __DOXYGEN_ONLY__
1719/* This is an example definition for documentation purposes.
1720 * Implementations should define a suitable value in `crypto_struct.h`.
1721 */
1722#define PSA_CIPHER_OPERATION_INIT {0}
1723#endif
1724
1725/** Return an initial value for a cipher operation object.
1726 */
1727static psa_cipher_operation_t psa_cipher_operation_init(void);
1728
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001729/** Set the key for a multipart symmetric encryption operation.
1730 *
1731 * The sequence of operations to encrypt a message with a symmetric cipher
1732 * is as follows:
1733 * -# Allocate an operation object which will be passed to all the functions
1734 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001735 * -# Initialize the operation object with one of the methods described in the
1736 * documentation for #psa_cipher_operation_t, e.g.
1737 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001738 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001739 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001740 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001741 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001742 * requires a specific IV value.
1743 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1744 * of the message each time.
1745 * -# Call psa_cipher_finish().
1746 *
1747 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001748 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001749 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001750 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001751 * eventually terminate the operation. The following events terminate an
1752 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001753 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001754 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001755 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001756 * \param[in,out] operation The operation object to set up. It must have
1757 * been initialized as per the documentation for
1758 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001759 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001760 * It must remain valid until the operation
1761 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001762 * \param alg The cipher algorithm to compute
1763 * (\c PSA_ALG_XXX value such that
1764 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001765 *
Gilles Peskine28538492018-07-11 17:34:00 +02001766 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001767 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001768 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001769 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001770 * \retval #PSA_ERROR_NOT_PERMITTED
1771 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001772 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001773 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001774 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001775 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1776 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1777 * \retval #PSA_ERROR_HARDWARE_FAILURE
1778 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001779 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001780 * The operation state is not valid (already set up and not
1781 * subsequently completed).
1782 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001783 * The library has not been previously initialized by psa_crypto_init().
1784 * It is implementation-dependent whether a failure to initialize
1785 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001786 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001787psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001788 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001789 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001790
1791/** Set the key for a multipart symmetric decryption operation.
1792 *
1793 * The sequence of operations to decrypt a message with a symmetric cipher
1794 * is as follows:
1795 * -# Allocate an operation object which will be passed to all the functions
1796 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001797 * -# Initialize the operation object with one of the methods described in the
1798 * documentation for #psa_cipher_operation_t, e.g.
1799 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001800 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001801 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001802 * decryption. If the IV is prepended to the ciphertext, you can call
1803 * psa_cipher_update() on a buffer containing the IV followed by the
1804 * beginning of the message.
1805 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1806 * of the message each time.
1807 * -# Call psa_cipher_finish().
1808 *
1809 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001810 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001811 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001812 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001813 * eventually terminate the operation. The following events terminate an
1814 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001815 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001816 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001817 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001818 * \param[in,out] operation The operation object to set up. It must have
1819 * been initialized as per the documentation for
1820 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001821 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001822 * It must remain valid until the operation
1823 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001824 * \param alg The cipher algorithm to compute
1825 * (\c PSA_ALG_XXX value such that
1826 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001827 *
Gilles Peskine28538492018-07-11 17:34:00 +02001828 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001829 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001830 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001831 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001832 * \retval #PSA_ERROR_NOT_PERMITTED
1833 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001834 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001835 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001836 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001837 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1838 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1839 * \retval #PSA_ERROR_HARDWARE_FAILURE
1840 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001841 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001842 * The operation state is not valid (already set up and not
1843 * subsequently completed).
1844 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001845 * The library has not been previously initialized by psa_crypto_init().
1846 * It is implementation-dependent whether a failure to initialize
1847 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001848 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001849psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001850 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001851 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001852
Gilles Peskinedcd14942018-07-12 00:30:52 +02001853/** Generate an IV for a symmetric encryption operation.
1854 *
1855 * This function generates a random IV (initialization vector), nonce
1856 * or initial counter value for the encryption operation as appropriate
1857 * for the chosen algorithm, key type and key size.
1858 *
1859 * The application must call psa_cipher_encrypt_setup() before
1860 * calling this function.
1861 *
1862 * If this function returns an error status, the operation becomes inactive.
1863 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001864 * \param[in,out] operation Active cipher operation.
1865 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001866 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001867 * \param[out] iv_length On success, the number of bytes of the
1868 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001869 *
1870 * \retval #PSA_SUCCESS
1871 * Success.
1872 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001873 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001874 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001875 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001876 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1877 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1878 * \retval #PSA_ERROR_HARDWARE_FAILURE
1879 * \retval #PSA_ERROR_TAMPERING_DETECTED
1880 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001881psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1882 unsigned char *iv,
1883 size_t iv_size,
1884 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001885
Gilles Peskinedcd14942018-07-12 00:30:52 +02001886/** Set the IV for a symmetric encryption or decryption operation.
1887 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001888 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001889 * or initial counter value for the encryption or decryption operation.
1890 *
1891 * The application must call psa_cipher_encrypt_setup() before
1892 * calling this function.
1893 *
1894 * If this function returns an error status, the operation becomes inactive.
1895 *
1896 * \note When encrypting, applications should use psa_cipher_generate_iv()
1897 * instead of this function, unless implementing a protocol that requires
1898 * a non-random IV.
1899 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001900 * \param[in,out] operation Active cipher operation.
1901 * \param[in] iv Buffer containing the IV to use.
1902 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001903 *
1904 * \retval #PSA_SUCCESS
1905 * Success.
1906 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001907 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001908 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001909 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001910 * or the chosen algorithm does not use an IV.
1911 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1912 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1913 * \retval #PSA_ERROR_HARDWARE_FAILURE
1914 * \retval #PSA_ERROR_TAMPERING_DETECTED
1915 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001916psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1917 const unsigned char *iv,
1918 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001919
Gilles Peskinedcd14942018-07-12 00:30:52 +02001920/** Encrypt or decrypt a message fragment in an active cipher operation.
1921 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001922 * Before calling this function, you must:
1923 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1924 * The choice of setup function determines whether this function
1925 * encrypts or decrypts its input.
1926 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1927 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001928 *
1929 * If this function returns an error status, the operation becomes inactive.
1930 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001931 * \param[in,out] operation Active cipher operation.
1932 * \param[in] input Buffer containing the message fragment to
1933 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001934 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001935 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001936 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001937 * \param[out] output_length On success, the number of bytes
1938 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001939 *
1940 * \retval #PSA_SUCCESS
1941 * Success.
1942 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001943 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001944 * not set, or already completed).
1945 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1946 * The size of the \p output buffer is too small.
1947 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1948 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1949 * \retval #PSA_ERROR_HARDWARE_FAILURE
1950 * \retval #PSA_ERROR_TAMPERING_DETECTED
1951 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001952psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1953 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001954 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001955 unsigned char *output,
1956 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001957 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001958
Gilles Peskinedcd14942018-07-12 00:30:52 +02001959/** Finish encrypting or decrypting a message in a cipher operation.
1960 *
1961 * The application must call psa_cipher_encrypt_setup() or
1962 * psa_cipher_decrypt_setup() before calling this function. The choice
1963 * of setup function determines whether this function encrypts or
1964 * decrypts its input.
1965 *
1966 * This function finishes the encryption or decryption of the message
1967 * formed by concatenating the inputs passed to preceding calls to
1968 * psa_cipher_update().
1969 *
1970 * When this function returns, the operation becomes inactive.
1971 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001972 * \param[in,out] operation Active cipher operation.
1973 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001974 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001975 * \param[out] output_length On success, the number of bytes
1976 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001977 *
1978 * \retval #PSA_SUCCESS
1979 * Success.
1980 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001981 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001982 * not set, or already completed).
1983 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1984 * The size of the \p output buffer is too small.
1985 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1986 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1987 * \retval #PSA_ERROR_HARDWARE_FAILURE
1988 * \retval #PSA_ERROR_TAMPERING_DETECTED
1989 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001990psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001991 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001992 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001993 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001994
Gilles Peskinedcd14942018-07-12 00:30:52 +02001995/** Abort a cipher operation.
1996 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001997 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001998 * \p operation structure itself. Once aborted, the operation object
1999 * can be reused for another operation by calling
2000 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002001 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002002 * You may call this function any time after the operation object has
2003 * been initialized by any of the following methods:
2004 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2005 * whether it succeeds or not.
2006 * - Initializing the \c struct to all-bits-zero.
2007 * - Initializing the \c struct to logical zeros, e.g.
2008 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002009 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002010 * In particular, calling psa_cipher_abort() after the operation has been
2011 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2012 * is safe and has no effect.
2013 *
2014 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002015 *
2016 * \retval #PSA_SUCCESS
2017 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002018 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002019 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2020 * \retval #PSA_ERROR_HARDWARE_FAILURE
2021 * \retval #PSA_ERROR_TAMPERING_DETECTED
2022 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002023psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2024
2025/**@}*/
2026
Gilles Peskine3b555712018-03-03 21:27:57 +01002027/** \defgroup aead Authenticated encryption with associated data (AEAD)
2028 * @{
2029 */
2030
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002031/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002032 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002033 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002034 * \param alg The AEAD algorithm to compute
2035 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002036 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002037 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002038 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002039 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002040 * but not encrypted.
2041 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002042 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002043 * encrypted.
2044 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002045 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002046 * encrypted data. The additional data is not
2047 * part of this output. For algorithms where the
2048 * encrypted data and the authentication tag
2049 * are defined as separate outputs, the
2050 * authentication tag is appended to the
2051 * encrypted data.
2052 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2053 * This must be at least
2054 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2055 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002056 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002057 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002058 *
Gilles Peskine28538492018-07-11 17:34:00 +02002059 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002060 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002061 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002062 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002063 * \retval #PSA_ERROR_NOT_PERMITTED
2064 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002065 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002066 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002067 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002068 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2069 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2070 * \retval #PSA_ERROR_HARDWARE_FAILURE
2071 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002072 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002073 * The library has not been previously initialized by psa_crypto_init().
2074 * It is implementation-dependent whether a failure to initialize
2075 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002076 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002077psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002078 psa_algorithm_t alg,
2079 const uint8_t *nonce,
2080 size_t nonce_length,
2081 const uint8_t *additional_data,
2082 size_t additional_data_length,
2083 const uint8_t *plaintext,
2084 size_t plaintext_length,
2085 uint8_t *ciphertext,
2086 size_t ciphertext_size,
2087 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002088
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002089/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002090 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002091 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002092 * \param alg The AEAD algorithm to compute
2093 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002094 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002095 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002096 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002097 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002098 * but not encrypted.
2099 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002100 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002101 * encrypted. For algorithms where the
2102 * encrypted data and the authentication tag
2103 * are defined as separate inputs, the buffer
2104 * must contain the encrypted data followed
2105 * by the authentication tag.
2106 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002107 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002108 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2109 * This must be at least
2110 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2111 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002112 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002113 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002114 *
Gilles Peskine28538492018-07-11 17:34:00 +02002115 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002116 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002117 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002118 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002119 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002120 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002121 * \retval #PSA_ERROR_NOT_PERMITTED
2122 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002123 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002124 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002125 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002126 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2127 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2128 * \retval #PSA_ERROR_HARDWARE_FAILURE
2129 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002130 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002131 * The library has not been previously initialized by psa_crypto_init().
2132 * It is implementation-dependent whether a failure to initialize
2133 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002134 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002135psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002136 psa_algorithm_t alg,
2137 const uint8_t *nonce,
2138 size_t nonce_length,
2139 const uint8_t *additional_data,
2140 size_t additional_data_length,
2141 const uint8_t *ciphertext,
2142 size_t ciphertext_length,
2143 uint8_t *plaintext,
2144 size_t plaintext_size,
2145 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002146
Gilles Peskine30a9e412019-01-14 18:36:12 +01002147/** The type of the state data structure for multipart AEAD operations.
2148 *
2149 * Before calling any function on an AEAD operation object, the application
2150 * must initialize it by any of the following means:
2151 * - Set the structure to all-bits-zero, for example:
2152 * \code
2153 * psa_aead_operation_t operation;
2154 * memset(&operation, 0, sizeof(operation));
2155 * \endcode
2156 * - Initialize the structure to logical zero values, for example:
2157 * \code
2158 * psa_aead_operation_t operation = {0};
2159 * \endcode
2160 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2161 * for example:
2162 * \code
2163 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2164 * \endcode
2165 * - Assign the result of the function psa_aead_operation_init()
2166 * to the structure, for example:
2167 * \code
2168 * psa_aead_operation_t operation;
2169 * operation = psa_aead_operation_init();
2170 * \endcode
2171 *
2172 * This is an implementation-defined \c struct. Applications should not
2173 * make any assumptions about the content of this structure except
2174 * as directed by the documentation of a specific implementation. */
2175typedef struct psa_aead_operation_s psa_aead_operation_t;
2176
2177/** \def PSA_AEAD_OPERATION_INIT
2178 *
2179 * This macro returns a suitable initializer for an AEAD operation object of
2180 * type #psa_aead_operation_t.
2181 */
2182#ifdef __DOXYGEN_ONLY__
2183/* This is an example definition for documentation purposes.
2184 * Implementations should define a suitable value in `crypto_struct.h`.
2185 */
2186#define PSA_AEAD_OPERATION_INIT {0}
2187#endif
2188
2189/** Return an initial value for an AEAD operation object.
2190 */
2191static psa_aead_operation_t psa_aead_operation_init(void);
2192
2193/** Set the key for a multipart authenticated encryption operation.
2194 *
2195 * The sequence of operations to encrypt a message with authentication
2196 * is as follows:
2197 * -# Allocate an operation object which will be passed to all the functions
2198 * listed here.
2199 * -# Initialize the operation object with one of the methods described in the
2200 * documentation for #psa_aead_operation_t, e.g.
2201 * PSA_AEAD_OPERATION_INIT.
2202 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002203 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2204 * inputs to the subsequent calls to psa_aead_update_ad() and
2205 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2206 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002207 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2208 * generate or set the nonce. You should use
2209 * psa_aead_generate_nonce() unless the protocol you are implementing
2210 * requires a specific nonce value.
2211 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2212 * of the non-encrypted additional authenticated data each time.
2213 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002214 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002215 * -# Call psa_aead_finish().
2216 *
2217 * The application may call psa_aead_abort() at any time after the operation
2218 * has been initialized.
2219 *
2220 * After a successful call to psa_aead_encrypt_setup(), the application must
2221 * eventually terminate the operation. The following events terminate an
2222 * operation:
2223 * - A failed call to any of the \c psa_aead_xxx functions.
2224 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2225 *
2226 * \param[in,out] operation The operation object to set up. It must have
2227 * been initialized as per the documentation for
2228 * #psa_aead_operation_t and not yet in use.
2229 * \param handle Handle to the key to use for the operation.
2230 * It must remain valid until the operation
2231 * terminates.
2232 * \param alg The AEAD algorithm to compute
2233 * (\c PSA_ALG_XXX value such that
2234 * #PSA_ALG_IS_AEAD(\p alg) is true).
2235 *
2236 * \retval #PSA_SUCCESS
2237 * Success.
2238 * \retval #PSA_ERROR_INVALID_HANDLE
2239 * \retval #PSA_ERROR_EMPTY_SLOT
2240 * \retval #PSA_ERROR_NOT_PERMITTED
2241 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002242 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002243 * \retval #PSA_ERROR_NOT_SUPPORTED
2244 * \p alg is not supported or is not an AEAD algorithm.
2245 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2246 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2247 * \retval #PSA_ERROR_HARDWARE_FAILURE
2248 * \retval #PSA_ERROR_TAMPERING_DETECTED
2249 * \retval #PSA_ERROR_BAD_STATE
2250 * The library has not been previously initialized by psa_crypto_init().
2251 * It is implementation-dependent whether a failure to initialize
2252 * results in this error code.
2253 */
2254psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2255 psa_key_handle_t handle,
2256 psa_algorithm_t alg);
2257
2258/** Set the key for a multipart authenticated decryption operation.
2259 *
2260 * The sequence of operations to decrypt a message with authentication
2261 * is as follows:
2262 * -# Allocate an operation object which will be passed to all the functions
2263 * listed here.
2264 * -# Initialize the operation object with one of the methods described in the
2265 * documentation for #psa_aead_operation_t, e.g.
2266 * PSA_AEAD_OPERATION_INIT.
2267 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002268 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2269 * inputs to the subsequent calls to psa_aead_update_ad() and
2270 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2271 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002272 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2273 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2274 * of the non-encrypted additional authenticated data each time.
2275 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002276 * of the ciphertext to decrypt each time.
2277 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002278 *
2279 * The application may call psa_aead_abort() at any time after the operation
2280 * has been initialized.
2281 *
2282 * After a successful call to psa_aead_decrypt_setup(), the application must
2283 * eventually terminate the operation. The following events terminate an
2284 * operation:
2285 * - A failed call to any of the \c psa_aead_xxx functions.
2286 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2287 *
2288 * \param[in,out] operation The operation object to set up. It must have
2289 * been initialized as per the documentation for
2290 * #psa_aead_operation_t and not yet in use.
2291 * \param handle Handle to the key to use for the operation.
2292 * It must remain valid until the operation
2293 * terminates.
2294 * \param alg The AEAD algorithm to compute
2295 * (\c PSA_ALG_XXX value such that
2296 * #PSA_ALG_IS_AEAD(\p alg) is true).
2297 *
2298 * \retval #PSA_SUCCESS
2299 * Success.
2300 * \retval #PSA_ERROR_INVALID_HANDLE
2301 * \retval #PSA_ERROR_EMPTY_SLOT
2302 * \retval #PSA_ERROR_NOT_PERMITTED
2303 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002304 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002305 * \retval #PSA_ERROR_NOT_SUPPORTED
2306 * \p alg is not supported or is not an AEAD algorithm.
2307 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2308 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2309 * \retval #PSA_ERROR_HARDWARE_FAILURE
2310 * \retval #PSA_ERROR_TAMPERING_DETECTED
2311 * \retval #PSA_ERROR_BAD_STATE
2312 * The library has not been previously initialized by psa_crypto_init().
2313 * It is implementation-dependent whether a failure to initialize
2314 * results in this error code.
2315 */
2316psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2317 psa_key_handle_t handle,
2318 psa_algorithm_t alg);
2319
2320/** Generate a random nonce for an authenticated encryption operation.
2321 *
2322 * This function generates a random nonce for the authenticated encryption
2323 * operation with an appropriate size for the chosen algorithm, key type
2324 * and key size.
2325 *
2326 * The application must call psa_aead_encrypt_setup() before
2327 * calling this function.
2328 *
2329 * If this function returns an error status, the operation becomes inactive.
2330 *
2331 * \param[in,out] operation Active AEAD operation.
2332 * \param[out] nonce Buffer where the generated nonce is to be
2333 * written.
2334 * \param nonce_size Size of the \p nonce buffer in bytes.
2335 * \param[out] nonce_length On success, the number of bytes of the
2336 * generated nonce.
2337 *
2338 * \retval #PSA_SUCCESS
2339 * Success.
2340 * \retval #PSA_ERROR_BAD_STATE
2341 * The operation state is not valid (not set up, or nonce already set).
2342 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2343 * The size of the \p nonce buffer is too small.
2344 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2345 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2346 * \retval #PSA_ERROR_HARDWARE_FAILURE
2347 * \retval #PSA_ERROR_TAMPERING_DETECTED
2348 */
2349psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2350 unsigned char *nonce,
2351 size_t nonce_size,
2352 size_t *nonce_length);
2353
2354/** Set the nonce for an authenticated encryption or decryption operation.
2355 *
2356 * This function sets the nonce for the authenticated
2357 * encryption or decryption operation.
2358 *
2359 * The application must call psa_aead_encrypt_setup() before
2360 * calling this function.
2361 *
2362 * If this function returns an error status, the operation becomes inactive.
2363 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002364 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002365 * instead of this function, unless implementing a protocol that requires
2366 * a non-random IV.
2367 *
2368 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002369 * \param[in] nonce Buffer containing the nonce to use.
2370 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002371 *
2372 * \retval #PSA_SUCCESS
2373 * Success.
2374 * \retval #PSA_ERROR_BAD_STATE
2375 * The operation state is not valid (not set up, or nonce already set).
2376 * \retval #PSA_ERROR_INVALID_ARGUMENT
2377 * The size of \p nonce is not acceptable for the chosen algorithm.
2378 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2379 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2380 * \retval #PSA_ERROR_HARDWARE_FAILURE
2381 * \retval #PSA_ERROR_TAMPERING_DETECTED
2382 */
2383psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2384 const unsigned char *nonce,
2385 size_t nonce_length);
2386
Gilles Peskinebc59c852019-01-17 15:26:08 +01002387/** Declare the lengths of the message and additional data for AEAD.
2388 *
2389 * The application must call this function before calling
2390 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2391 * the operation requires it. If the algorithm does not require it,
2392 * calling this function is optional, but if this function is called
2393 * then the implementation must enforce the lengths.
2394 *
2395 * You may call this function before or after setting the nonce with
2396 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2397 *
2398 * - For #PSA_ALG_CCM, calling this function is required.
2399 * - For the other AEAD algorithms defined in this specification, calling
2400 * this function is not required.
2401 * - For vendor-defined algorithm, refer to the vendor documentation.
2402 *
2403 * \param[in,out] operation Active AEAD operation.
2404 * \param ad_length Size of the non-encrypted additional
2405 * authenticated data in bytes.
2406 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2407 *
2408 * \retval #PSA_SUCCESS
2409 * Success.
2410 * \retval #PSA_ERROR_BAD_STATE
2411 * The operation state is not valid (not set up, already completed,
2412 * or psa_aead_update_ad() or psa_aead_update() already called).
2413 * \retval #PSA_ERROR_INVALID_ARGUMENT
2414 * At least one of the lengths is not acceptable for the chosen
2415 * algorithm.
2416 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2417 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2418 * \retval #PSA_ERROR_HARDWARE_FAILURE
2419 * \retval #PSA_ERROR_TAMPERING_DETECTED
2420 */
2421psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2422 size_t ad_length,
2423 size_t plaintext_length);
2424
Gilles Peskine30a9e412019-01-14 18:36:12 +01002425/** Pass additional data to an active AEAD operation.
2426 *
2427 * Additional data is authenticated, but not encrypted.
2428 *
2429 * You may call this function multiple times to pass successive fragments
2430 * of the additional data. You may not call this function after passing
2431 * data to encrypt or decrypt with psa_aead_update().
2432 *
2433 * Before calling this function, you must:
2434 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2435 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2436 *
2437 * If this function returns an error status, the operation becomes inactive.
2438 *
2439 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2440 * there is no guarantee that the input is valid. Therefore, until
2441 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2442 * treat the input as untrusted and prepare to undo any action that
2443 * depends on the input if psa_aead_verify() returns an error status.
2444 *
2445 * \param[in,out] operation Active AEAD operation.
2446 * \param[in] input Buffer containing the fragment of
2447 * additional data.
2448 * \param input_length Size of the \p input buffer in bytes.
2449 *
2450 * \retval #PSA_SUCCESS
2451 * Success.
2452 * \retval #PSA_ERROR_BAD_STATE
2453 * The operation state is not valid (not set up, nonce not set,
2454 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002455 * \retval #PSA_ERROR_INVALID_ARGUMENT
2456 * The total input length overflows the additional data length that
2457 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002458 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2459 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2460 * \retval #PSA_ERROR_HARDWARE_FAILURE
2461 * \retval #PSA_ERROR_TAMPERING_DETECTED
2462 */
2463psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2464 const uint8_t *input,
2465 size_t input_length);
2466
2467/** Encrypt or decrypt a message fragment in an active AEAD operation.
2468 *
2469 * Before calling this function, you must:
2470 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2471 * The choice of setup function determines whether this function
2472 * encrypts or decrypts its input.
2473 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2474 * 3. Call psa_aead_update_ad() to pass all the additional data.
2475 *
2476 * If this function returns an error status, the operation becomes inactive.
2477 *
2478 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2479 * there is no guarantee that the input is valid. Therefore, until
2480 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2481 * - Do not use the output in any way other than storing it in a
2482 * confidential location. If you take any action that depends
2483 * on the tentative decrypted data, this action will need to be
2484 * undone if the input turns out not to be valid. Furthermore,
2485 * if an adversary can observe that this action took place
2486 * (for example through timing), they may be able to use this
2487 * fact as an oracle to decrypt any message encrypted with the
2488 * same key.
2489 * - In particular, do not copy the output anywhere but to a
2490 * memory or storage space that you have exclusive access to.
2491 *
2492 * \param[in,out] operation Active AEAD operation.
2493 * \param[in] input Buffer containing the message fragment to
2494 * encrypt or decrypt.
2495 * \param input_length Size of the \p input buffer in bytes.
2496 * \param[out] output Buffer where the output is to be written.
2497 * \param output_size Size of the \p output buffer in bytes.
2498 * \param[out] output_length On success, the number of bytes
2499 * that make up the returned output.
2500 *
2501 * \retval #PSA_SUCCESS
2502 * Success.
2503 * \retval #PSA_ERROR_BAD_STATE
2504 * The operation state is not valid (not set up, nonce not set
2505 * or already completed).
2506 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2507 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002508 * \retval #PSA_ERROR_INVALID_ARGUMENT
2509 * The total length of input to psa_aead_update_ad() so far is
2510 * less than the additional data length that was previously
2511 * specified with psa_aead_set_lengths().
2512 * \retval #PSA_ERROR_INVALID_ARGUMENT
2513 * The total input length overflows the plaintext length that
2514 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002515 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2516 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2517 * \retval #PSA_ERROR_HARDWARE_FAILURE
2518 * \retval #PSA_ERROR_TAMPERING_DETECTED
2519 */
2520psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2521 const uint8_t *input,
2522 size_t input_length,
2523 unsigned char *output,
2524 size_t output_size,
2525 size_t *output_length);
2526
2527/** Finish encrypting a message in an AEAD operation.
2528 *
2529 * The operation must have been set up with psa_aead_encrypt_setup().
2530 *
2531 * This function finishes the authentication of the additional data
2532 * formed by concatenating the inputs passed to preceding calls to
2533 * psa_aead_update_ad() with the plaintext formed by concatenating the
2534 * inputs passed to preceding calls to psa_aead_update().
2535 *
2536 * This function has two output buffers:
2537 * - \p ciphertext contains trailing ciphertext that was buffered from
2538 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2539 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2540 * will not contain any output and can be a 0-sized buffer.
2541 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002542 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002543 * that the operation performs.
2544 *
2545 * When this function returns, the operation becomes inactive.
2546 *
2547 * \param[in,out] operation Active AEAD operation.
2548 * \param[out] ciphertext Buffer where the last part of the ciphertext
2549 * is to be written.
2550 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2551 * \param[out] ciphertext_length On success, the number of bytes of
2552 * returned ciphertext.
2553 * \param[out] tag Buffer where the authentication tag is
2554 * to be written.
2555 * \param tag_size Size of the \p tag buffer in bytes.
2556 * \param[out] tag_length On success, the number of bytes
2557 * that make up the returned tag.
2558 *
2559 * \retval #PSA_SUCCESS
2560 * Success.
2561 * \retval #PSA_ERROR_BAD_STATE
2562 * The operation state is not valid (not set up, nonce not set,
2563 * decryption, or already completed).
2564 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002565 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002566 * \retval #PSA_ERROR_INVALID_ARGUMENT
2567 * The total length of input to psa_aead_update_ad() so far is
2568 * less than the additional data length that was previously
2569 * specified with psa_aead_set_lengths().
2570 * \retval #PSA_ERROR_INVALID_ARGUMENT
2571 * The total length of input to psa_aead_update() so far is
2572 * less than the plaintext length that was previously
2573 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002574 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2575 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2576 * \retval #PSA_ERROR_HARDWARE_FAILURE
2577 * \retval #PSA_ERROR_TAMPERING_DETECTED
2578 */
2579psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002580 uint8_t *ciphertext,
2581 size_t ciphertext_size,
2582 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002583 uint8_t *tag,
2584 size_t tag_size,
2585 size_t *tag_length);
2586
2587/** Finish authenticating and decrypting a message in an AEAD operation.
2588 *
2589 * The operation must have been set up with psa_aead_decrypt_setup().
2590 *
2591 * This function finishes the authentication of the additional data
2592 * formed by concatenating the inputs passed to preceding calls to
2593 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2594 * inputs passed to preceding calls to psa_aead_update().
2595 *
2596 * When this function returns, the operation becomes inactive.
2597 *
2598 * \param[in,out] operation Active AEAD operation.
2599 * \param[in] tag Buffer containing the authentication tag.
2600 * \param tag_length Size of the \p tag buffer in bytes.
2601 *
2602 * \retval #PSA_SUCCESS
2603 * Success.
2604 * \retval #PSA_ERROR_BAD_STATE
2605 * The operation state is not valid (not set up, nonce not set,
2606 * encryption, or already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002607 * \retval #PSA_ERROR_INVALID_ARGUMENT
2608 * The total length of input to psa_aead_update_ad() so far is
2609 * less than the additional data length that was previously
2610 * specified with psa_aead_set_lengths().
2611 * \retval #PSA_ERROR_INVALID_ARGUMENT
2612 * The total length of input to psa_aead_update() so far is
2613 * less than the plaintext length that was previously
2614 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002615 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2616 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2617 * \retval #PSA_ERROR_HARDWARE_FAILURE
2618 * \retval #PSA_ERROR_TAMPERING_DETECTED
2619 */
2620psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2621 const uint8_t *tag,
2622 size_t tag_length);
2623
2624/** Abort an AEAD operation.
2625 *
2626 * Aborting an operation frees all associated resources except for the
2627 * \p operation structure itself. Once aborted, the operation object
2628 * can be reused for another operation by calling
2629 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2630 *
2631 * You may call this function any time after the operation object has
2632 * been initialized by any of the following methods:
2633 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2634 * whether it succeeds or not.
2635 * - Initializing the \c struct to all-bits-zero.
2636 * - Initializing the \c struct to logical zeros, e.g.
2637 * `psa_aead_operation_t operation = {0}`.
2638 *
2639 * In particular, calling psa_aead_abort() after the operation has been
2640 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2641 * is safe and has no effect.
2642 *
2643 * \param[in,out] operation Initialized AEAD operation.
2644 *
2645 * \retval #PSA_SUCCESS
2646 * \retval #PSA_ERROR_BAD_STATE
2647 * \p operation is not an active AEAD operation.
2648 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2649 * \retval #PSA_ERROR_HARDWARE_FAILURE
2650 * \retval #PSA_ERROR_TAMPERING_DETECTED
2651 */
2652psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2653
Gilles Peskine3b555712018-03-03 21:27:57 +01002654/**@}*/
2655
Gilles Peskine20035e32018-02-03 22:44:14 +01002656/** \defgroup asymmetric Asymmetric cryptography
2657 * @{
2658 */
2659
2660/**
2661 * \brief Sign a hash or short message with a private key.
2662 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002663 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002664 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002665 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2666 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2667 * to determine the hash algorithm to use.
2668 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002669 * \param handle Handle to the key to use for the operation.
2670 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002671 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002672 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002673 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002674 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002675 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002676 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002677 * \param[out] signature_length On success, the number of bytes
2678 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002679 *
Gilles Peskine28538492018-07-11 17:34:00 +02002680 * \retval #PSA_SUCCESS
2681 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002682 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002683 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002684 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002685 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002686 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002687 * \retval #PSA_ERROR_NOT_SUPPORTED
2688 * \retval #PSA_ERROR_INVALID_ARGUMENT
2689 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2690 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2691 * \retval #PSA_ERROR_HARDWARE_FAILURE
2692 * \retval #PSA_ERROR_TAMPERING_DETECTED
2693 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002694 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002695 * The library has not been previously initialized by psa_crypto_init().
2696 * It is implementation-dependent whether a failure to initialize
2697 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002698 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002699psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002700 psa_algorithm_t alg,
2701 const uint8_t *hash,
2702 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002703 uint8_t *signature,
2704 size_t signature_size,
2705 size_t *signature_length);
2706
2707/**
2708 * \brief Verify the signature a hash or short message using a public key.
2709 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002710 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002711 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002712 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2713 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2714 * to determine the hash algorithm to use.
2715 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002716 * \param handle Handle to the key to use for the operation.
2717 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002718 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002719 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002720 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002721 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002722 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002723 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002724 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002725 *
Gilles Peskine28538492018-07-11 17:34:00 +02002726 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002727 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002728 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002729 * The calculation was perfomed successfully, but the passed
2730 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002731 * \retval #PSA_ERROR_NOT_SUPPORTED
2732 * \retval #PSA_ERROR_INVALID_ARGUMENT
2733 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2734 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2735 * \retval #PSA_ERROR_HARDWARE_FAILURE
2736 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002737 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002738 * The library has not been previously initialized by psa_crypto_init().
2739 * It is implementation-dependent whether a failure to initialize
2740 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002741 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002742psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002743 psa_algorithm_t alg,
2744 const uint8_t *hash,
2745 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002746 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002747 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002748
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002749/**
2750 * \brief Encrypt a short message with a public key.
2751 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002752 * \param handle Handle to the key to use for the operation.
2753 * It must be a public key or an asymmetric
2754 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002755 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002756 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002757 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002758 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002759 * \param[in] salt A salt or label, if supported by the
2760 * encryption algorithm.
2761 * If the algorithm does not support a
2762 * salt, pass \c NULL.
2763 * If the algorithm supports an optional
2764 * salt and you do not want to pass a salt,
2765 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002766 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002767 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2768 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002769 * \param salt_length Size of the \p salt buffer in bytes.
2770 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002771 * \param[out] output Buffer where the encrypted message is to
2772 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002773 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002774 * \param[out] output_length On success, the number of bytes
2775 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002776 *
Gilles Peskine28538492018-07-11 17:34:00 +02002777 * \retval #PSA_SUCCESS
2778 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002779 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002780 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002781 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002782 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002783 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002784 * \retval #PSA_ERROR_NOT_SUPPORTED
2785 * \retval #PSA_ERROR_INVALID_ARGUMENT
2786 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2787 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2788 * \retval #PSA_ERROR_HARDWARE_FAILURE
2789 * \retval #PSA_ERROR_TAMPERING_DETECTED
2790 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002791 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002792 * The library has not been previously initialized by psa_crypto_init().
2793 * It is implementation-dependent whether a failure to initialize
2794 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002795 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002796psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002797 psa_algorithm_t alg,
2798 const uint8_t *input,
2799 size_t input_length,
2800 const uint8_t *salt,
2801 size_t salt_length,
2802 uint8_t *output,
2803 size_t output_size,
2804 size_t *output_length);
2805
2806/**
2807 * \brief Decrypt a short message with a private key.
2808 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002809 * \param handle Handle to the key to use for the operation.
2810 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002811 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002812 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002813 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002814 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002815 * \param[in] salt A salt or label, if supported by the
2816 * encryption algorithm.
2817 * If the algorithm does not support a
2818 * salt, pass \c NULL.
2819 * If the algorithm supports an optional
2820 * salt and you do not want to pass a salt,
2821 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002822 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002823 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2824 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002825 * \param salt_length Size of the \p salt buffer in bytes.
2826 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002827 * \param[out] output Buffer where the decrypted message is to
2828 * be written.
2829 * \param output_size Size of the \c output buffer in bytes.
2830 * \param[out] output_length On success, the number of bytes
2831 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002832 *
Gilles Peskine28538492018-07-11 17:34:00 +02002833 * \retval #PSA_SUCCESS
2834 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002835 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002836 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002837 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002838 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002839 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002840 * \retval #PSA_ERROR_NOT_SUPPORTED
2841 * \retval #PSA_ERROR_INVALID_ARGUMENT
2842 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2843 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2844 * \retval #PSA_ERROR_HARDWARE_FAILURE
2845 * \retval #PSA_ERROR_TAMPERING_DETECTED
2846 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2847 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002848 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002849 * The library has not been previously initialized by psa_crypto_init().
2850 * It is implementation-dependent whether a failure to initialize
2851 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002852 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002853psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002854 psa_algorithm_t alg,
2855 const uint8_t *input,
2856 size_t input_length,
2857 const uint8_t *salt,
2858 size_t salt_length,
2859 uint8_t *output,
2860 size_t output_size,
2861 size_t *output_length);
2862
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002863/**@}*/
2864
Gilles Peskineedd76872018-07-20 17:42:05 +02002865/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002866 * @{
2867 */
2868
2869/** The type of the state data structure for generators.
2870 *
2871 * Before calling any function on a generator, the application must
2872 * initialize it by any of the following means:
2873 * - Set the structure to all-bits-zero, for example:
2874 * \code
2875 * psa_crypto_generator_t generator;
2876 * memset(&generator, 0, sizeof(generator));
2877 * \endcode
2878 * - Initialize the structure to logical zero values, for example:
2879 * \code
2880 * psa_crypto_generator_t generator = {0};
2881 * \endcode
2882 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2883 * for example:
2884 * \code
2885 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2886 * \endcode
2887 * - Assign the result of the function psa_crypto_generator_init()
2888 * to the structure, for example:
2889 * \code
2890 * psa_crypto_generator_t generator;
2891 * generator = psa_crypto_generator_init();
2892 * \endcode
2893 *
2894 * This is an implementation-defined \c struct. Applications should not
2895 * make any assumptions about the content of this structure except
2896 * as directed by the documentation of a specific implementation.
2897 */
2898typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2899
2900/** \def PSA_CRYPTO_GENERATOR_INIT
2901 *
2902 * This macro returns a suitable initializer for a generator object
2903 * of type #psa_crypto_generator_t.
2904 */
2905#ifdef __DOXYGEN_ONLY__
2906/* This is an example definition for documentation purposes.
2907 * Implementations should define a suitable value in `crypto_struct.h`.
2908 */
2909#define PSA_CRYPTO_GENERATOR_INIT {0}
2910#endif
2911
2912/** Return an initial value for a generator object.
2913 */
2914static psa_crypto_generator_t psa_crypto_generator_init(void);
2915
2916/** Retrieve the current capacity of a generator.
2917 *
2918 * The capacity of a generator is the maximum number of bytes that it can
2919 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2920 *
2921 * \param[in] generator The generator to query.
2922 * \param[out] capacity On success, the capacity of the generator.
2923 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002924 * \retval #PSA_SUCCESS
2925 * \retval #PSA_ERROR_BAD_STATE
2926 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002927 */
2928psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2929 size_t *capacity);
2930
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002931/** Set the maximum capacity of a generator.
2932 *
2933 * \param[in,out] generator The generator object to modify.
2934 * \param capacity The new capacity of the generator.
2935 * It must be less or equal to the generator's
2936 * current capacity.
2937 *
2938 * \retval #PSA_SUCCESS
2939 * \retval #PSA_ERROR_INVALID_ARGUMENT
2940 * \p capacity is larger than the generator's current capacity.
2941 * \retval #PSA_ERROR_BAD_STATE
2942 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2943 */
2944psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
2945 size_t capacity);
2946
Gilles Peskineeab56e42018-07-12 17:12:33 +02002947/** Read some data from a generator.
2948 *
2949 * This function reads and returns a sequence of bytes from a generator.
2950 * The data that is read is discarded from the generator. The generator's
2951 * capacity is decreased by the number of bytes read.
2952 *
2953 * \param[in,out] generator The generator object to read from.
2954 * \param[out] output Buffer where the generator output will be
2955 * written.
2956 * \param output_length Number of bytes to output.
2957 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002958 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02002959 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskineeab56e42018-07-12 17:12:33 +02002960 * There were fewer than \p output_length bytes
2961 * in the generator. Note that in this case, no
2962 * output is written to the output buffer.
2963 * The generator's capacity is set to 0, thus
2964 * subsequent calls to this function will not
2965 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002966 * \retval #PSA_ERROR_BAD_STATE
2967 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2968 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2969 * \retval #PSA_ERROR_HARDWARE_FAILURE
2970 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002971 */
2972psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2973 uint8_t *output,
2974 size_t output_length);
2975
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002976/** Generate a key deterministically from data read from a generator.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002977 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002978 * This function uses the output of a generator to derive a key.
2979 * How much output it consumes and how the key is derived depends on the
2980 * key type.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002981 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002982 * - For key types for which the key is an arbitrary sequence of bytes
2983 * of a given size,
2984 * this function is functionally equivalent to calling #psa_generator_read
2985 * and passing the resulting output to #psa_import_key.
2986 * However, this function has a security benefit:
2987 * if the implementation provides an isolation boundary then
2988 * the key material is not exposed outside the isolation boundary.
2989 * As a consequence, for these key types, this function always consumes
2990 * exactly (\p bits / 8) bytes from the generator.
2991 * The following key types defined in this specification follow this scheme:
2992 *
2993 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002994 * - #PSA_KEY_TYPE_ARC4;
2995 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002996 * - #PSA_KEY_TYPE_DERIVE;
2997 * - #PSA_KEY_TYPE_HMAC.
2998 *
2999 * - For ECC keys on a Montgomery elliptic curve
3000 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3001 * Montgomery curve), this function always draws a byte string whose
3002 * length is determined by the curve, and sets the mandatory bits
3003 * accordingly. That is:
3004 *
3005 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3006 * and process it as specified in RFC 7748 &sect;5.
3007 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3008 * and process it as specified in RFC 7748 &sect;5.
3009 *
3010 * - For key types for which the key is represented by a single sequence of
3011 * \p bits bits with constraints as to which bit sequences are acceptable,
3012 * this function draws a byte string of length (\p bits / 8) bytes rounded
3013 * up to the nearest whole number of bytes. If the resulting byte string
3014 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3015 * This process is repeated until an acceptable byte string is drawn.
3016 * The byte string drawn from the generator is interpreted as specified
3017 * for the output produced by psa_export_key().
3018 * The following key types defined in this specification follow this scheme:
3019 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003020 * - #PSA_KEY_TYPE_DES.
3021 * Force-set the parity bits, but discard forbidden weak keys.
3022 * For 2-key and 3-key triple-DES, the three keys are generated
3023 * successively (for example, for 3-key triple-DES,
3024 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3025 * discard the first 8 bytes, use the next 8 bytes as the first key,
3026 * and continue reading output from the generator to derive the other
3027 * two keys).
3028 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR),
3029 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
3030 * ECC keys on a Weierstrass elliptic curve
3031 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3032 * Weierstrass curve).
3033 * For these key types, interpret the byte string as integer
3034 * in big-endian order. Discard it if it is not in the range
3035 * [0, *N* - 2] where *N* is the boundary of the private key domain
3036 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003037 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003038 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003039 * This method allows compliance to NIST standards, specifically
3040 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003041 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3042 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3043 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3044 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003045 *
3046 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
3047 * the way in which the generator output is consumed is
3048 * implementation-defined.
3049 *
3050 * In all cases, the data that is read is discarded from the generator.
3051 * The generator's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003052 *
Gilles Peskine20628592019-04-19 19:29:50 +02003053 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003054 * \param[out] handle On success, a handle to the newly created key.
3055 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003056 * \param[in,out] generator The generator object to read from.
3057 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003058 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003059 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003060 * If the key is persistent, the key material and the key's metadata
3061 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003062 * \retval #PSA_ERROR_ALREADY_EXISTS
3063 * This is an attempt to create a persistent key, and there is
3064 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003065 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003066 * There was not enough data to create the desired key.
3067 * Note that in this case, no output is written to the output buffer.
3068 * The generator's capacity is set to 0, thus subsequent calls to
3069 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003070 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003071 * The key type or key size is not supported, either by the
3072 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003073 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003074 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3075 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3076 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3077 * \retval #PSA_ERROR_HARDWARE_FAILURE
3078 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003079 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003080 * The library has not been previously initialized by psa_crypto_init().
3081 * It is implementation-dependent whether a failure to initialize
3082 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003083 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003084psa_status_t psa_generate_derived_key(const psa_key_attributes_t *attributes,
Gilles Peskine87a5e562019-04-17 12:28:25 +02003085 psa_key_handle_t *handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003086 psa_crypto_generator_t *generator);
3087
3088/** Abort a generator.
3089 *
3090 * Once a generator has been aborted, its capacity is zero.
3091 * Aborting a generator frees all associated resources except for the
3092 * \c generator structure itself.
3093 *
3094 * This function may be called at any time as long as the generator
3095 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
3096 * psa_crypto_generator_init() or a zero value. In particular, it is valid
3097 * to call psa_generator_abort() twice, or to call psa_generator_abort()
3098 * on a generator that has not been set up.
3099 *
3100 * Once aborted, the generator object may be called.
3101 *
3102 * \param[in,out] generator The generator to abort.
3103 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003104 * \retval #PSA_SUCCESS
3105 * \retval #PSA_ERROR_BAD_STATE
3106 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3107 * \retval #PSA_ERROR_HARDWARE_FAILURE
3108 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003109 */
3110psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3111
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003112/** Use the maximum possible capacity for a generator.
3113 *
3114 * Use this value as the capacity argument when setting up a generator
3115 * to indicate that the generator should have the maximum possible capacity.
3116 * The value of the maximum possible capacity depends on the generator
3117 * algorithm.
3118 */
3119#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
3120
Gilles Peskineeab56e42018-07-12 17:12:33 +02003121/**@}*/
3122
Gilles Peskineea0fb492018-07-12 17:17:20 +02003123/** \defgroup derivation Key derivation
3124 * @{
3125 */
3126
3127/** Set up a key derivation operation.
3128 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003129 * A key derivation algorithm takes some inputs and uses them to create
3130 * a byte generator which can be used to produce keys and other
3131 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003132 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003133 * To use a generator for key derivation:
3134 * - Start with an initialized object of type #psa_crypto_generator_t.
3135 * - Call psa_key_derivation_setup() to select the algorithm.
3136 * - Provide the inputs for the key derivation by calling
3137 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3138 * as appropriate. Which inputs are needed, in what order, and whether
3139 * they may be keys and if so of what type depends on the algorithm.
3140 * - Optionally set the generator's maximum capacity with
3141 * psa_set_generator_capacity(). You may do this before, in the middle of
3142 * or after providing inputs. For some algorithms, this step is mandatory
3143 * because the output depends on the maximum capacity.
3144 * - Generate output with psa_generator_read() or
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003145 * psa_generate_derived_key(). Successive calls to these functions
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003146 * use successive output bytes from the generator.
3147 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02003148 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003149 * \param[in,out] generator The generator object to set up. It must
3150 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003151 * \param alg The key derivation algorithm to compute
3152 * (\c PSA_ALG_XXX value such that
3153 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02003154 *
3155 * \retval #PSA_SUCCESS
3156 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003157 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003158 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003159 * \retval #PSA_ERROR_NOT_SUPPORTED
3160 * \c alg is not supported or is not a key derivation algorithm.
3161 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3162 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3163 * \retval #PSA_ERROR_HARDWARE_FAILURE
3164 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003165 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003166 */
3167psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
3168 psa_algorithm_t alg);
3169
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003170/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003171 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003172 * Which inputs are required and in what order depends on the algorithm.
3173 * Refer to the documentation of each key derivation or key agreement
3174 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003175 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003176 * This function passes direct inputs. Some inputs must be passed as keys
3177 * using psa_key_derivation_input_key() instead of this function. Refer to
3178 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003179 *
3180 * \param[in,out] generator The generator object to use. It must
3181 * have been set up with
3182 * psa_key_derivation_setup() and must not
3183 * have produced any output yet.
3184 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003185 * \param[in] data Input data to use.
3186 * \param data_length Size of the \p data buffer in bytes.
3187 *
3188 * \retval #PSA_SUCCESS
3189 * Success.
3190 * \retval #PSA_ERROR_INVALID_ARGUMENT
3191 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003192 * \retval #PSA_ERROR_INVALID_ARGUMENT
3193 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003194 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3195 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3196 * \retval #PSA_ERROR_HARDWARE_FAILURE
3197 * \retval #PSA_ERROR_TAMPERING_DETECTED
3198 * \retval #PSA_ERROR_BAD_STATE
3199 * The value of \p step is not valid given the state of \p generator.
3200 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003201 * The library has not been previously initialized by psa_crypto_init().
3202 * It is implementation-dependent whether a failure to initialize
3203 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003204 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003205psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3206 psa_key_derivation_step_t step,
3207 const uint8_t *data,
3208 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003209
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003210/** Provide an input for key derivation in the form of a key.
3211 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003212 * Which inputs are required and in what order depends on the algorithm.
3213 * Refer to the documentation of each key derivation or key agreement
3214 * algorithm for information.
3215 *
3216 * This function passes key inputs. Some inputs must be passed as keys
3217 * of the appropriate type using this function, while others must be
3218 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3219 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003220 *
3221 * \param[in,out] generator The generator object to use. It must
3222 * have been set up with
3223 * psa_key_derivation_setup() and must not
3224 * have produced any output yet.
3225 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003226 * \param handle Handle to the key. It must have an
3227 * appropriate type for \p step and must
3228 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003229 *
3230 * \retval #PSA_SUCCESS
3231 * Success.
3232 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine31351842019-04-09 12:00:00 +02003233 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003234 * \retval #PSA_ERROR_NOT_PERMITTED
3235 * \retval #PSA_ERROR_INVALID_ARGUMENT
3236 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003237 * \retval #PSA_ERROR_INVALID_ARGUMENT
3238 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003239 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3240 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3241 * \retval #PSA_ERROR_HARDWARE_FAILURE
3242 * \retval #PSA_ERROR_TAMPERING_DETECTED
3243 * \retval #PSA_ERROR_BAD_STATE
3244 * The value of \p step is not valid given the state of \p generator.
3245 * \retval #PSA_ERROR_BAD_STATE
3246 * The library has not been previously initialized by psa_crypto_init().
3247 * It is implementation-dependent whether a failure to initialize
3248 * results in this error code.
3249 */
3250psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3251 psa_key_derivation_step_t step,
3252 psa_key_handle_t handle);
3253
Gilles Peskine969c5d62019-01-16 15:53:06 +01003254/** Perform a key agreement and use the shared secret as input to a key
3255 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003256 *
3257 * A key agreement algorithm takes two inputs: a private key \p private_key
3258 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003259 * The result of this function is passed as input to a key derivation.
3260 * The output of this key derivation can be extracted by reading from the
3261 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003262 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003263 * \param[in,out] generator The generator object to use. It must
3264 * have been set up with
3265 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003266 * key agreement and derivation algorithm
3267 * \c alg (\c PSA_ALG_XXX value such that
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003268 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3269 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
Gilles Peskine6843c292019-01-18 16:44:49 +01003270 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003271 * The generator must be ready for an
3272 * input of the type given by \p step.
3273 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003274 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003275 * \param[in] peer_key Public key of the peer. The peer key must be in the
3276 * same format that psa_import_key() accepts for the
3277 * public key type corresponding to the type of
3278 * private_key. That is, this function performs the
3279 * equivalent of
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003280 * #psa_import_key(`internal_public_key_handle`,
3281 * #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(`private_key_type`),
3282 * `peer_key`, `peer_key_length`) where
Jaeden Amero8afbff82019-01-14 16:56:20 +00003283 * `private_key_type` is the type of `private_key`.
3284 * For example, for EC keys, this means that peer_key
3285 * is interpreted as a point on the curve that the
3286 * private key is on. The standard formats for public
3287 * keys are documented in the documentation of
3288 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003289 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003290 *
3291 * \retval #PSA_SUCCESS
3292 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003293 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02003294 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine01d718c2018-09-18 12:01:02 +02003295 * \retval #PSA_ERROR_NOT_PERMITTED
3296 * \retval #PSA_ERROR_INVALID_ARGUMENT
3297 * \c private_key is not compatible with \c alg,
3298 * or \p peer_key is not valid for \c alg or not compatible with
3299 * \c private_key.
3300 * \retval #PSA_ERROR_NOT_SUPPORTED
3301 * \c alg is not supported or is not a key derivation algorithm.
3302 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3303 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3304 * \retval #PSA_ERROR_HARDWARE_FAILURE
3305 * \retval #PSA_ERROR_TAMPERING_DETECTED
3306 */
3307psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003308 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003309 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003310 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003311 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003312
Gilles Peskine769c7a62019-01-18 16:42:29 +01003313/** Perform a key agreement and use the shared secret as input to a key
3314 * derivation.
3315 *
3316 * A key agreement algorithm takes two inputs: a private key \p private_key
3317 * a public key \p peer_key.
3318 *
3319 * \warning The raw result of a key agreement algorithm such as finite-field
3320 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3321 * not be used directly as key material. It should instead be passed as
3322 * input to a key derivation algorithm. To chain a key agreement with
3323 * a key derivation, use psa_key_agreement() and other functions from
3324 * the key derivation and generator interface.
3325 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003326 * \param alg The key agreement algorithm to compute
3327 * (\c PSA_ALG_XXX value such that
3328 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3329 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003330 * \param private_key Handle to the private key to use.
3331 * \param[in] peer_key Public key of the peer. It must be
3332 * in the same format that psa_import_key()
3333 * accepts. The standard formats for public
3334 * keys are documented in the documentation
3335 * of psa_export_public_key().
3336 * \param peer_key_length Size of \p peer_key in bytes.
3337 * \param[out] output Buffer where the decrypted message is to
3338 * be written.
3339 * \param output_size Size of the \c output buffer in bytes.
3340 * \param[out] output_length On success, the number of bytes
3341 * that make up the returned output.
3342 *
3343 * \retval #PSA_SUCCESS
3344 * Success.
3345 * \retval #PSA_ERROR_INVALID_HANDLE
3346 * \retval #PSA_ERROR_EMPTY_SLOT
3347 * \retval #PSA_ERROR_NOT_PERMITTED
3348 * \retval #PSA_ERROR_INVALID_ARGUMENT
3349 * \p alg is not a key agreement algorithm
3350 * \retval #PSA_ERROR_INVALID_ARGUMENT
3351 * \p private_key is not compatible with \p alg,
3352 * or \p peer_key is not valid for \p alg or not compatible with
3353 * \p private_key.
3354 * \retval #PSA_ERROR_NOT_SUPPORTED
3355 * \p alg is not a supported key agreement algorithm.
3356 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3357 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3358 * \retval #PSA_ERROR_HARDWARE_FAILURE
3359 * \retval #PSA_ERROR_TAMPERING_DETECTED
3360 */
3361psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3362 psa_key_handle_t private_key,
3363 const uint8_t *peer_key,
3364 size_t peer_key_length,
3365 uint8_t *output,
3366 size_t output_size,
3367 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003368
3369/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003370
3371/** \defgroup random Random generation
3372 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003373 */
3374
3375/**
3376 * \brief Generate random bytes.
3377 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003378 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003379 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003380 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003381 *
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003382 * \note To generate a key, use psa_generate_random_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003383 *
3384 * \param[out] output Output buffer for the generated data.
3385 * \param output_size Number of bytes to generate and output.
3386 *
3387 * \retval #PSA_SUCCESS
3388 * \retval #PSA_ERROR_NOT_SUPPORTED
3389 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3390 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3391 * \retval #PSA_ERROR_HARDWARE_FAILURE
3392 * \retval #PSA_ERROR_TAMPERING_DETECTED
3393 * \retval #PSA_ERROR_BAD_STATE
3394 * The library has not been previously initialized by psa_crypto_init().
3395 * It is implementation-dependent whether a failure to initialize
3396 * results in this error code.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003397 */
3398psa_status_t psa_generate_random(uint8_t *output,
3399 size_t output_size);
3400
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003401/**
3402 * \brief Generate a key or key pair.
3403 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003404 * The key is generated randomly.
3405 * Its location, policy, type and size are taken from \p attributes.
3406 *
3407 * If the type requires additional domain parameters, these are taken
3408 * from \p attributes as well. The following types use domain parameters:
3409 * - When generating an RSA key (#PSA_KEY_TYPE_RSA_KEYPAIR),
3410 * the default public exponent is 65537. This value is used if
3411 * \p attributes was set with psa_set_key_type() or by passing an empty
3412 * byte string as domain parameters to psa_set_key_domain_parameters().
3413 * If psa_set_key_domain_parameters() was used to set a non-empty
3414 * domain parameter string in \p attributes, this string is read as
3415 * a big-endian integer which is used as the public exponent.
3416 * - When generating a DSA key (#PSA_KEY_TYPE_DSA_KEYPAIR) or a
3417 * Diffie-Hellman key (#PSA_KEY_TYPE_DH_KEYPAIR), the domain parameters
3418 * from \p attributes are interpreted as described for
3419 * psa_set_key_domain_parameters().
3420 *
Gilles Peskine20628592019-04-19 19:29:50 +02003421 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003422 * \param[out] handle On success, a handle to the newly created key.
3423 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003424 *
Gilles Peskine28538492018-07-11 17:34:00 +02003425 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003426 * Success.
3427 * If the key is persistent, the key material and the key's metadata
3428 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003429 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003430 * This is an attempt to create a persistent key, and there is
3431 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003432 * \retval #PSA_ERROR_NOT_SUPPORTED
3433 * \retval #PSA_ERROR_INVALID_ARGUMENT
3434 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3435 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3436 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3437 * \retval #PSA_ERROR_HARDWARE_FAILURE
3438 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003439 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003440 * The library has not been previously initialized by psa_crypto_init().
3441 * It is implementation-dependent whether a failure to initialize
3442 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003443 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003444psa_status_t psa_generate_random_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003445 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003446
3447/**@}*/
3448
Gilles Peskinee59236f2018-01-27 23:32:46 +01003449#ifdef __cplusplus
3450}
3451#endif
3452
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003453/* The file "crypto_sizes.h" contains definitions for size calculation
3454 * macros whose definitions are implementation-specific. */
3455#include "crypto_sizes.h"
3456
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003457/* The file "crypto_struct.h" contains definitions for
3458 * implementation-specific structs that are declared above. */
3459#include "crypto_struct.h"
3460
3461/* The file "crypto_extra.h" contains vendor-specific definitions. This
3462 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003463#include "crypto_extra.h"
3464
3465#endif /* PSA_CRYPTO_H */