blob: 77ade6c89746a111ffdb3676470aedb125cb4a9c [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
89 * \retval #PSA_ERROR_TAMPERING_DETECTED
90 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
Gilles Peskine87a5e562019-04-17 12:28:25 +020096/** \defgroup attributes Key attributes
97 * @{
98 */
99
100/** The type of a structure containing key attributes.
101 *
102 * This is an opaque structure that can represent the metadata of a key
Gilles Peskine9c640f92019-04-28 11:36:21 +0200103 * object. Metadata that can be stored in attributes includes:
104 * - The location of the key in storage, indicated by its key identifier
105 * and its lifetime.
106 * - The key's policy, comprising usage flags and a specification of
107 * the permitted algorithm(s).
108 * - Information about the key itself: the key type, the key size, and
109 * for some key type additional domain parameters.
110 * - Implementations may define additional attributes.
Gilles Peskine87a5e562019-04-17 12:28:25 +0200111 *
112 * The actual key material is not considered an attribute of a key.
113 * Key attributes do not contain information that is generally considered
114 * highly confidential.
Gilles Peskine20628592019-04-19 19:29:50 +0200115 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200116 * An attribute structure can be a simple data structure where each function
117 * `psa_set_key_xxx` sets a field and the corresponding function
118 * `psa_get_key_xxx` retrieves the value of the corresponding field.
119 * However, implementations may report values that are equivalent to the
120 * original one, but have a different encoding. For example, an
121 * implementation may use a more compact representation for types where
122 * many bit-patterns are invalid or not supported, and store all values
123 * that it does not support as a special marker value. In such an
124 * implementation, after setting an invalid value, the corresponding
125 * get function returns an invalid value which may not be the one that
126 * was originally stored.
127 *
128 * An attribute structure may contain references to auxiliary resources,
129 * for example pointers to allocated memory or indirect references to
130 * pre-calculated values. In order to free such resources, the application
131 * must call psa_reset_key_attributes(). As an exception, calling
132 * psa_reset_key_attributes() on an attribute structure is optional if
133 * the structure has only been modified by the following functions
134 * since it was initialized or last reset with psa_reset_key_attributes():
135 * - psa_make_key_persistent()
136 * - psa_set_key_type()
137 * - psa_set_key_bits()
138 * - psa_set_key_usage_flags()
139 * - psa_set_key_algorithm()
140 *
Gilles Peskine20628592019-04-19 19:29:50 +0200141 * Before calling any function on a key attribute structure, the application
142 * must initialize it by any of the following means:
143 * - Set the structure to all-bits-zero, for example:
144 * \code
145 * psa_key_attributes_t attributes;
146 * memset(&attributes, 0, sizeof(attributes));
147 * \endcode
148 * - Initialize the structure to logical zero values, for example:
149 * \code
150 * psa_key_attributes_t attributes = {0};
151 * \endcode
152 * - Initialize the structure to the initializer #PSA_KEY_ATTRIBUTES_INIT,
153 * for example:
154 * \code
155 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
156 * \endcode
157 * - Assign the result of the function psa_key_attributes_init()
158 * to the structure, for example:
159 * \code
160 * psa_key_attributes_t attributes;
161 * attributes = psa_key_attributes_init();
162 * \endcode
163 *
164 * A freshly initialized attribute structure contains the following
165 * values:
166 *
167 * - lifetime: #PSA_KEY_LIFETIME_VOLATILE.
168 * - key identifier: unspecified.
169 * - type: \c 0, with no domain parameters.
170 * - key size: \c 0.
171 * - usage flags: \c 0.
172 * - algorithm: \c 0.
173 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200174 * A typical sequence to create a key is as follows:
175 * -# Create and initialize an attribute structure.
176 * -# If the key is persistent, call psa_make_key_persistent().
177 * -# Set the key policy with psa_set_key_usage_flags() and
178 * psa_set_key_algorithm().
179 * -# Set the key type with psa_set_key_type(). If the key type requires
180 * domain parameters, call psa_set_key_domain_parameters() instead.
181 * Skip this step if copying an existing key with psa_copy_key().
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100182 * -# When generating a random key with psa_generate_random_key() or deriving a key
183 * with psa_generate_derived_key(), set the desired key size with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200184 * psa_set_key_bits().
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100185 * -# Call a key creation function: psa_import_key(), psa_generate_random_key(),
186 * psa_generate_derived_key() or psa_copy_key(). This function reads
Gilles Peskine1ea5e442019-05-02 20:31:10 +0200187 * the attribute structure, creates a key with these attributes, and
188 * outputs a handle to the newly created key.
189 * -# The attribute structure is now no longer necessary. If you called
Gilles Peskine9c640f92019-04-28 11:36:21 +0200190 * psa_set_key_domain_parameters() earlier, you must call
191 * psa_reset_key_attributes() to free any resources used by the
192 * domain parameters. Otherwise calling psa_reset_key_attributes()
193 * is optional.
Gilles Peskine20628592019-04-19 19:29:50 +0200194 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200195 * A typical sequence to query a key's attributes is as follows:
196 * -# Call psa_get_key_attributes().
197 * -# Call `psa_get_key_xxx` functions to retrieve the attribute(s) that
198 * you are interested in.
199 * -# Call psa_reset_key_attributes() to free any resources that may be
200 * used by the attribute structure.
201 *
202 * Once a key has been created, it is impossible to change its attributes.
Gilles Peskine87a5e562019-04-17 12:28:25 +0200203 */
204typedef struct psa_key_attributes_s psa_key_attributes_t;
205
Gilles Peskine20628592019-04-19 19:29:50 +0200206/** Declare a key as persistent.
207 *
208 * This function does not access storage, it merely fills the attribute
209 * structure with given values. The persistent key will be written to
210 * storage when the attribute structure is passed to a key creation
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100211 * function such as psa_import_key(), psa_generate_random_key(),
212 * psa_generate_derived_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200213 *
214 * This function overwrites any identifier and lifetime values
215 * previously set in \p attributes.
216 *
217 * This function may be declared as `static` (i.e. without external
218 * linkage). This function may be provided as a function-like macro,
219 * but in this case it must evaluate each of its arguments exactly once.
220 *
221 * \param[out] attributes The attribute structure to write to.
222 * \param id The persistent identifier for the key.
223 * \param lifetime The lifetime for the key.
224 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
225 * key will be volatile, and \p id is ignored.
226 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200227static void psa_make_key_persistent(psa_key_attributes_t *attributes,
228 psa_key_id_t id,
229 psa_key_lifetime_t lifetime);
230
Gilles Peskine20628592019-04-19 19:29:50 +0200231/** Retrieve the key identifier from key attributes.
232 *
233 * This function may be declared as `static` (i.e. without external
234 * linkage). This function may be provided as a function-like macro,
235 * but in this case it must evaluate its argument exactly once.
236 *
237 * \param[in] attributes The key attribute structure to query.
238 *
239 * \return The persistent identifier stored in the attribute structure.
240 * This value is unspecified if the attribute structure declares
241 * the key as volatile.
242 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200243static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
244
Gilles Peskine20628592019-04-19 19:29:50 +0200245/** Retrieve the lifetime from key attributes.
246 *
247 * This function may be declared as `static` (i.e. without external
248 * linkage). This function may be provided as a function-like macro,
249 * but in this case it must evaluate its argument exactly once.
250 *
251 * \param[in] attributes The key attribute structure to query.
252 *
253 * \return The lifetime value stored in the attribute structure.
254 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200255static psa_key_lifetime_t psa_get_key_lifetime(
256 const psa_key_attributes_t *attributes);
257
Gilles Peskine20628592019-04-19 19:29:50 +0200258/** Declare usage flags for a key.
259 *
260 * Usage flags are part of a key's usage policy. They encode what
261 * kind of operations are permitted on the key. For more details,
262 * refer to the documentation of the type #psa_key_usage_t.
263 *
264 * This function overwrites any usage flags
265 * previously set in \p attributes.
266 *
267 * This function may be declared as `static` (i.e. without external
268 * linkage). This function may be provided as a function-like macro,
269 * but in this case it must evaluate each of its arguments exactly once.
270 *
271 * \param[out] attributes The attribute structure to write to.
272 * \param usage_flags The usage flags to write.
273 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200274static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
275 psa_key_usage_t usage_flags);
276
Gilles Peskine20628592019-04-19 19:29:50 +0200277/** Retrieve the usage flags from key attributes.
278 *
279 * This function may be declared as `static` (i.e. without external
280 * linkage). This function may be provided as a function-like macro,
281 * but in this case it must evaluate its argument exactly once.
282 *
283 * \param[in] attributes The key attribute structure to query.
284 *
285 * \return The usage flags stored in the attribute structure.
286 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200287static psa_key_usage_t psa_get_key_usage_flags(
288 const psa_key_attributes_t *attributes);
289
Gilles Peskine20628592019-04-19 19:29:50 +0200290/** Declare the permitted algorithm policy for a key.
291 *
292 * The permitted algorithm policy of a key encodes which algorithm or
293 * algorithms are permitted to be used with this key.
294 *
295 * This function overwrites any algorithm policy
296 * previously set in \p attributes.
297 *
298 * This function may be declared as `static` (i.e. without external
299 * linkage). This function may be provided as a function-like macro,
300 * but in this case it must evaluate each of its arguments exactly once.
301 *
302 * \param[out] attributes The attribute structure to write to.
303 * \param alg The permitted algorithm policy to write.
304 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200305static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
306 psa_algorithm_t alg);
307
Gilles Peskine20628592019-04-19 19:29:50 +0200308/** Retrieve the algorithm policy from key attributes.
309 *
310 * This function may be declared as `static` (i.e. without external
311 * linkage). This function may be provided as a function-like macro,
312 * but in this case it must evaluate its argument exactly once.
313 *
314 * \param[in] attributes The key attribute structure to query.
315 *
316 * \return The algorithm stored in the attribute structure.
317 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200318static psa_algorithm_t psa_get_key_algorithm(
319 const psa_key_attributes_t *attributes);
320
Gilles Peskine20628592019-04-19 19:29:50 +0200321/** Declare the type of a key.
322 *
323 * If a type requires domain parameters, you must call
324 * psa_set_key_domain_parameters() instead of this function.
325 *
326 * This function overwrites any key type and domain parameters
327 * previously set in \p attributes.
328 *
329 * This function may be declared as `static` (i.e. without external
330 * linkage). This function may be provided as a function-like macro,
331 * but in this case it must evaluate each of its arguments exactly once.
332 *
333 * \param[out] attributes The attribute structure to write to.
334 * \param type The key type to write.
335 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200336static void psa_set_key_type(psa_key_attributes_t *attributes,
337 psa_key_type_t type);
338
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200339/** Declare the size of a key.
340 *
341 * This function overwrites any key size previously set in \p attributes.
342 *
343 * This function may be declared as `static` (i.e. without external
344 * linkage). This function may be provided as a function-like macro,
345 * but in this case it must evaluate each of its arguments exactly once.
346 *
347 * \param[out] attributes The attribute structure to write to.
348 * \param bits The key size in bits.
349 */
350static void psa_set_key_bits(psa_key_attributes_t *attributes,
351 size_t bits);
352
Gilles Peskine20628592019-04-19 19:29:50 +0200353/** Retrieve the key type from key attributes.
354 *
355 * This function may be declared as `static` (i.e. without external
356 * linkage). This function may be provided as a function-like macro,
357 * but in this case it must evaluate its argument exactly once.
358 *
359 * \param[in] attributes The key attribute structure to query.
360 *
361 * \return The key type stored in the attribute structure.
362 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200363static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
364
Gilles Peskine20628592019-04-19 19:29:50 +0200365/** Retrieve the key size from key attributes.
366 *
367 * This function may be declared as `static` (i.e. without external
368 * linkage). This function may be provided as a function-like macro,
369 * but in this case it must evaluate its argument exactly once.
370 *
371 * \param[in] attributes The key attribute structure to query.
372 *
373 * \return The key size stored in the attribute structure, in bits.
374 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200375static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
376
Gilles Peskineb699f072019-04-26 16:06:02 +0200377/**
378 * \brief Set domain parameters for a key.
379 *
380 * Some key types require additional domain parameters in addition to
381 * the key type identifier and the key size.
382 * The format for the required domain parameters varies by the key type.
383 *
Gilles Peskinee56e8782019-04-26 17:34:02 +0200384 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEYPAIR),
385 * the domain parameter data consists of the public exponent,
Gilles Peskineb699f072019-04-26 16:06:02 +0200386 * represented as a big-endian integer with no leading zeros.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200387 * This information is used when generating an RSA key pair.
Gilles Peskineb699f072019-04-26 16:06:02 +0200388 * When importing a key, the public exponent is read from the imported
389 * key data and the exponent recorded in the attribute structure is ignored.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200390 * As an exception, the public exponent 65537 is represented by an empty
391 * byte string.
392 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEYPAIR),
Gilles Peskineb699f072019-04-26 16:06:02 +0200393 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
394 * ```
395 * Dss-Parms ::= SEQUENCE {
396 * p INTEGER,
397 * q INTEGER,
398 * g INTEGER
399 * }
400 * ```
Gilles Peskinee56e8782019-04-26 17:34:02 +0200401 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY or
402 * #PSA_KEY_TYPE_DH_KEYPAIR), the
Gilles Peskineb699f072019-04-26 16:06:02 +0200403 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
404 * ```
405 * DomainParameters ::= SEQUENCE {
406 * p INTEGER, -- odd prime, p=jq +1
407 * g INTEGER, -- generator, g
408 * q INTEGER, -- factor of p-1
409 * j INTEGER OPTIONAL, -- subgroup factor
410 * validationParms ValidationParms OPTIONAL
411 * }
412 * ValidationParms ::= SEQUENCE {
413 * seed BIT STRING,
414 * pgenCounter INTEGER
415 * }
416 * ```
417 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200418 * \note This function may allocate memory or other resources.
419 * Once you have called this function on an attribute structure,
420 * you must call psa_reset_key_attributes() to free these resources.
421 *
Gilles Peskineb699f072019-04-26 16:06:02 +0200422 * \param[in,out] attributes Attribute structure where the specified domain
423 * parameters will be stored.
424 * If this function fails, the content of
425 * \p attributes is not modified.
426 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
427 * \param[in] data Buffer containing the key domain parameters.
428 * The content of this buffer is interpreted
429 * according to \p type as described above.
430 * \param data_length Size of the \p data buffer in bytes.
431 *
432 * \retval #PSA_SUCCESS
433 * \retval #PSA_ERROR_INVALID_ARGUMENT
434 * \retval #PSA_ERROR_NOT_SUPPORTED
435 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
436 */
437psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
438 psa_key_type_t type,
439 const uint8_t *data,
440 size_t data_length);
441
442/**
443 * \brief Get domain parameters for a key.
444 *
445 * Get the domain parameters for a key with this function, if any. The format
446 * of the domain parameters written to \p data is specified in the
447 * documentation for psa_set_key_domain_parameters().
448 *
449 * \param[in] attributes The key attribute structure to query.
450 * \param[out] data On success, the key domain parameters.
451 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineaa02c172019-04-28 11:44:17 +0200452 * The buffer is guaranteed to be large
453 * enough if its size in bytes is at least
454 * the value given by
455 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
Gilles Peskineb699f072019-04-26 16:06:02 +0200456 * \param[out] data_length On success, the number of bytes
457 * that make up the key domain parameters data.
458 *
459 * \retval #PSA_SUCCESS
460 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
461 */
462psa_status_t psa_get_key_domain_parameters(
463 const psa_key_attributes_t *attributes,
464 uint8_t *data,
465 size_t data_size,
466 size_t *data_length);
467
Gilles Peskine20628592019-04-19 19:29:50 +0200468/** Retrieve the attributes of a key.
469 *
470 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200471 * psa_reset_key_attributes(). It then copies the attributes of
472 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200473 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200474 * \note This function may allocate memory or other resources.
475 * Once you have called this function on an attribute structure,
476 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200477 *
Gilles Peskine20628592019-04-19 19:29:50 +0200478 * \param[in] handle Handle to the key to query.
479 * \param[in,out] attributes On success, the attributes of the key.
480 * On failure, equivalent to a
481 * freshly-initialized structure.
482 *
483 * \retval #PSA_SUCCESS
484 * \retval #PSA_ERROR_INVALID_HANDLE
485 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
486 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
487 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200488psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
489 psa_key_attributes_t *attributes);
490
Gilles Peskine20628592019-04-19 19:29:50 +0200491/** Reset a key attribute structure to a freshly initialized state.
492 *
493 * You must initialize the attribute structure as described in the
494 * documentation of the type #psa_key_attributes_t before calling this
495 * function. Once the structure has been initialized, you may call this
496 * function at any time.
497 *
498 * This function frees any auxiliary resources that the structure
499 * may contain.
500 *
501 * \param[in,out] attributes The attribute structure to reset.
502 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200503void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200504
Gilles Peskine87a5e562019-04-17 12:28:25 +0200505/**@}*/
506
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100507/** \defgroup key_management Key management
508 * @{
509 */
510
Gilles Peskinef535eb22018-11-30 14:08:36 +0100511/** Open a handle to an existing persistent key.
512 *
513 * Open a handle to a key which was previously created with psa_create_key().
514 *
515 * \param lifetime The lifetime of the key. This designates a storage
516 * area where the key material is stored. This must not
517 * be #PSA_KEY_LIFETIME_VOLATILE.
518 * \param id The persistent identifier of the key.
519 * \param[out] handle On success, a handle to a key slot which contains
520 * the data and metadata loaded from the specified
521 * persistent location.
522 *
523 * \retval #PSA_SUCCESS
524 * Success. The application can now use the value of `*handle`
525 * to access the newly allocated key slot.
526 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200527 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100528 * \retval #PSA_ERROR_INVALID_ARGUMENT
529 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
530 * \retval #PSA_ERROR_INVALID_ARGUMENT
531 * \p id is invalid for the specified lifetime.
532 * \retval #PSA_ERROR_NOT_SUPPORTED
533 * \p lifetime is not supported.
534 * \retval #PSA_ERROR_NOT_PERMITTED
535 * The specified key exists, but the application does not have the
536 * permission to access it. Note that this specification does not
537 * define any way to create such a key, but it may be possible
538 * through implementation-specific means.
539 */
540psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
541 psa_key_id_t id,
542 psa_key_handle_t *handle);
543
Gilles Peskinef535eb22018-11-30 14:08:36 +0100544/** Close a key handle.
545 *
546 * If the handle designates a volatile key, destroy the key material and
547 * free all associated resources, just like psa_destroy_key().
548 *
549 * If the handle designates a persistent key, free all resources associated
550 * with the key in volatile memory. The key slot in persistent storage is
551 * not affected and can be opened again later with psa_open_key().
552 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100553 * If the key is currently in use in a multipart operation,
554 * the multipart operation is aborted.
555 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100556 * \param handle The key handle to close.
557 *
558 * \retval #PSA_SUCCESS
559 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100560 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100561 */
562psa_status_t psa_close_key(psa_key_handle_t handle);
563
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100564/**@}*/
565
566/** \defgroup import_export Key import and export
567 * @{
568 */
569
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100570/**
571 * \brief Import a key in binary format.
572 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100573 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100574 * documentation of psa_export_public_key() for the format of public keys
575 * and to the documentation of psa_export_key() for the format for
576 * other key types.
577 *
578 * This specification supports a single format for each key type.
579 * Implementations may support other formats as long as the standard
580 * format is supported. Implementations that support other formats
581 * should ensure that the formats are clearly unambiguous so as to
582 * minimize the risk that an invalid input is accidentally interpreted
583 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100584 *
Gilles Peskine20628592019-04-19 19:29:50 +0200585 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200586 * The key size is always determined from the
587 * \p data buffer.
588 * If the key size in \p attributes is nonzero,
589 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200590 * \param[out] handle On success, a handle to the newly created key.
591 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100592 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine20628592019-04-19 19:29:50 +0200593 * buffer is interpreted according to the type and,
594 * if applicable, domain parameters declared in
595 * \p attributes.
596 * All implementations must support at least the format
597 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100598 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200599 * the chosen type. Implementations may allow other
600 * formats, but should be conservative: implementations
601 * should err on the side of rejecting content if it
602 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200603 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100604 *
Gilles Peskine28538492018-07-11 17:34:00 +0200605 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100606 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100607 * If the key is persistent, the key material and the key's metadata
608 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200609 * \retval #PSA_ERROR_ALREADY_EXISTS
610 * This is an attempt to create a persistent key, and there is
611 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200612 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200613 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200614 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200615 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200616 * The key attributes, as a whole, are invalid.
617 * \retval #PSA_ERROR_INVALID_ARGUMENT
618 * The key data is not correctly formatted.
619 * \retval #PSA_ERROR_INVALID_ARGUMENT
620 * The size in \p attributes is nonzero and does not match the size
621 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200622 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
623 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
624 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100625 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200626 * \retval #PSA_ERROR_HARDWARE_FAILURE
627 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300628 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300629 * The library has not been previously initialized by psa_crypto_init().
630 * It is implementation-dependent whether a failure to initialize
631 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100632 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200633psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
634 psa_key_handle_t *handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100635 const uint8_t *data,
636 size_t data_length);
637
638/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100639 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200640 *
641 * This function destroys the content of the key slot from both volatile
642 * memory and, if applicable, non-volatile storage. Implementations shall
643 * make a best effort to ensure that any previous content of the slot is
644 * unrecoverable.
645 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100646 * This function also erases any metadata such as policies and frees all
647 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200648 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100649 * If the key is currently in use in a multipart operation,
650 * the multipart operation is aborted.
651 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100652 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100653 *
Gilles Peskine28538492018-07-11 17:34:00 +0200654 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200655 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200656 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200657 * The slot holds content and cannot be erased because it is
658 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100659 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200660 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200661 * There was an failure in communication with the cryptoprocessor.
662 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200663 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200664 * The storage is corrupted. Implementations shall make a best effort
665 * to erase key material even in this stage, however applications
666 * should be aware that it may be impossible to guarantee that the
667 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200668 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200669 * An unexpected condition which is not a storage corruption or
670 * a communication failure occurred. The cryptoprocessor may have
671 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300672 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300673 * The library has not been previously initialized by psa_crypto_init().
674 * It is implementation-dependent whether a failure to initialize
675 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100677psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678
679/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100680 * \brief Export a key in binary format.
681 *
682 * The output of this function can be passed to psa_import_key() to
683 * create an equivalent object.
684 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100685 * If the implementation of psa_import_key() supports other formats
686 * beyond the format specified here, the output from psa_export_key()
687 * must use the representation specified here, not the original
688 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100689 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100690 * For standard key types, the output format is as follows:
691 *
692 * - For symmetric keys (including MAC keys), the format is the
693 * raw bytes of the key.
694 * - For DES, the key data consists of 8 bytes. The parity bits must be
695 * correct.
696 * - For Triple-DES, the format is the concatenation of the
697 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100698 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200699 * is the non-encrypted DER encoding of the representation defined by
700 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
701 * ```
702 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200703 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200704 * modulus INTEGER, -- n
705 * publicExponent INTEGER, -- e
706 * privateExponent INTEGER, -- d
707 * prime1 INTEGER, -- p
708 * prime2 INTEGER, -- q
709 * exponent1 INTEGER, -- d mod (p-1)
710 * exponent2 INTEGER, -- d mod (q-1)
711 * coefficient INTEGER, -- (inverse of q) mod p
712 * }
713 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000714 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
715 * representation of the private key `x` as a big-endian byte string. The
716 * length of the byte string is the private key size in bytes (leading zeroes
717 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200718 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100719 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100720 * a representation of the private value as a `ceiling(m/8)`-byte string
721 * where `m` is the bit size associated with the curve, i.e. the bit size
722 * of the order of the curve's coordinate field. This byte string is
723 * in little-endian order for Montgomery curves (curve types
724 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
725 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
726 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100727 * This is the content of the `privateKey` field of the `ECPrivateKey`
728 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000729 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
730 * format is the representation of the private key `x` as a big-endian byte
731 * string. The length of the byte string is the private key size in bytes
732 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200733 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
734 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100735 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100736 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200737 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200738 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200739 * \param[out] data_length On success, the number of bytes
740 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100741 *
Gilles Peskine28538492018-07-11 17:34:00 +0200742 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100743 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200744 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200745 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100746 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200747 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
748 * The size of the \p data buffer is too small. You can determine a
749 * sufficient buffer size by calling
750 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
751 * where \c type is the key type
752 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200753 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
754 * \retval #PSA_ERROR_HARDWARE_FAILURE
755 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300756 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300757 * The library has not been previously initialized by psa_crypto_init().
758 * It is implementation-dependent whether a failure to initialize
759 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100760 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100761psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100762 uint8_t *data,
763 size_t data_size,
764 size_t *data_length);
765
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100766/**
767 * \brief Export a public key or the public part of a key pair in binary format.
768 *
769 * The output of this function can be passed to psa_import_key() to
770 * create an object that is equivalent to the public key.
771 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000772 * This specification supports a single format for each key type.
773 * Implementations may support other formats as long as the standard
774 * format is supported. Implementations that support other formats
775 * should ensure that the formats are clearly unambiguous so as to
776 * minimize the risk that an invalid input is accidentally interpreted
777 * according to a different format.
778 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000779 * For standard key types, the output format is as follows:
780 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
781 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
782 * ```
783 * RSAPublicKey ::= SEQUENCE {
784 * modulus INTEGER, -- n
785 * publicExponent INTEGER } -- e
786 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000787 * - For elliptic curve public keys (key types for which
788 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
789 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
790 * Let `m` be the bit size associated with the curve, i.e. the bit size of
791 * `q` for a curve over `F_q`. The representation consists of:
792 * - The byte 0x04;
793 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
794 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000795 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
796 * representation of the public key `y = g^x mod p` as a big-endian byte
797 * string. The length of the byte string is the length of the base prime `p`
798 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000799 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
800 * the format is the representation of the public key `y = g^x mod p` as a
801 * big-endian byte string. The length of the byte string is the length of the
802 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100803 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100804 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200805 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200806 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200807 * \param[out] data_length On success, the number of bytes
808 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100809 *
Gilles Peskine28538492018-07-11 17:34:00 +0200810 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100811 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200812 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200813 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200814 * The key is neither a public key nor a key pair.
815 * \retval #PSA_ERROR_NOT_SUPPORTED
816 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
817 * The size of the \p data buffer is too small. You can determine a
818 * sufficient buffer size by calling
819 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
820 * where \c type is the key type
821 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200822 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
823 * \retval #PSA_ERROR_HARDWARE_FAILURE
824 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300825 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300826 * The library has not been previously initialized by psa_crypto_init().
827 * It is implementation-dependent whether a failure to initialize
828 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100829 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100830psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100831 uint8_t *data,
832 size_t data_size,
833 size_t *data_length);
834
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100835/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100836 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100837 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000838 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100839 * This function is primarily useful to copy a key from one location
840 * to another, since it populates a key using the material from
841 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200842 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100843 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100844 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100845 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100846 *
Gilles Peskine20628592019-04-19 19:29:50 +0200847 * The resulting key may only be used in a way that conforms to
848 * both the policy of the original key and the policy specified in
849 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100850 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200851 * usage flags on the source policy and the usage flags in \p attributes.
852 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100853 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200854 * - If either of the policies allows an algorithm and the other policy
855 * allows a wildcard-based algorithm policy that includes this algorithm,
856 * the resulting key allows the same algorithm.
857 * - If the policies do not allow any algorithm in common, this function
858 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200859 *
Gilles Peskine20628592019-04-19 19:29:50 +0200860 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100861 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200862 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100863 * \param source_handle The key to copy. It must be a handle to an
864 * occupied slot.
Gilles Peskine20628592019-04-19 19:29:50 +0200865 * \param[in] attributes The attributes for the new key.
866 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200867 * - The key type and size may be 0. If either is
868 * nonzero, it must match the corresponding
869 * attribute of the source key.
870 * - If \p attributes contains domain parameters,
871 * they must match the domain parameters of
872 * the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200873 * - The key location (the lifetime and, for
874 * persistent keys, the key identifier) is
875 * used directly.
876 * - The policy constraints (usage flags and
877 * algorithm policy) are combined from
878 * the source key and \p attributes so that
879 * both sets of restrictions apply, as
880 * described in the documentation of this function.
881 * \param[out] target_handle On success, a handle to the newly created key.
882 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200883 *
884 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100885 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200886 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200887 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200888 * This is an attempt to create a persistent key, and there is
889 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200890 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200891 * The lifetime or identifier in \p attributes are invalid.
892 * \retval #PSA_ERROR_INVALID_ARGUMENT
893 * The policy constraints on the source and specified in
894 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200895 * \retval #PSA_ERROR_INVALID_ARGUMENT
896 * \p attributes specifies a key type, domain parameters or key size
897 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100898 * \retval #PSA_ERROR_NOT_PERMITTED
899 * The source key is not exportable and its lifetime does not
900 * allow copying it to the target's lifetime.
901 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
902 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200903 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
904 * \retval #PSA_ERROR_HARDWARE_FAILURE
905 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100906 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100907psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200908 const psa_key_attributes_t *attributes,
909 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100910
911/**@}*/
912
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100913/** \defgroup hash Message digests
914 * @{
915 */
916
Gilles Peskine69647a42019-01-14 20:18:12 +0100917/** Calculate the hash (digest) of a message.
918 *
919 * \note To verify the hash of a message against an
920 * expected value, use psa_hash_compare() instead.
921 *
922 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
923 * such that #PSA_ALG_IS_HASH(\p alg) is true).
924 * \param[in] input Buffer containing the message to hash.
925 * \param input_length Size of the \p input buffer in bytes.
926 * \param[out] hash Buffer where the hash is to be written.
927 * \param hash_size Size of the \p hash buffer in bytes.
928 * \param[out] hash_length On success, the number of bytes
929 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100930 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100931 *
932 * \retval #PSA_SUCCESS
933 * Success.
934 * \retval #PSA_ERROR_NOT_SUPPORTED
935 * \p alg is not supported or is not a hash algorithm.
936 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
937 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
938 * \retval #PSA_ERROR_HARDWARE_FAILURE
939 * \retval #PSA_ERROR_TAMPERING_DETECTED
940 */
941psa_status_t psa_hash_compute(psa_algorithm_t alg,
942 const uint8_t *input,
943 size_t input_length,
944 uint8_t *hash,
945 size_t hash_size,
946 size_t *hash_length);
947
948/** Calculate the hash (digest) of a message and compare it with a
949 * reference value.
950 *
951 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
952 * such that #PSA_ALG_IS_HASH(\p alg) is true).
953 * \param[in] input Buffer containing the message to hash.
954 * \param input_length Size of the \p input buffer in bytes.
955 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100956 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100957 *
958 * \retval #PSA_SUCCESS
959 * The expected hash is identical to the actual hash of the input.
960 * \retval #PSA_ERROR_INVALID_SIGNATURE
961 * The hash of the message was calculated successfully, but it
962 * differs from the expected hash.
963 * \retval #PSA_ERROR_NOT_SUPPORTED
964 * \p alg is not supported or is not a hash algorithm.
965 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
966 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
967 * \retval #PSA_ERROR_HARDWARE_FAILURE
968 * \retval #PSA_ERROR_TAMPERING_DETECTED
969 */
970psa_status_t psa_hash_compare(psa_algorithm_t alg,
971 const uint8_t *input,
972 size_t input_length,
973 const uint8_t *hash,
974 const size_t hash_length);
975
Gilles Peskine308b91d2018-02-08 09:47:44 +0100976/** The type of the state data structure for multipart hash operations.
977 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000978 * Before calling any function on a hash operation object, the application must
979 * initialize it by any of the following means:
980 * - Set the structure to all-bits-zero, for example:
981 * \code
982 * psa_hash_operation_t operation;
983 * memset(&operation, 0, sizeof(operation));
984 * \endcode
985 * - Initialize the structure to logical zero values, for example:
986 * \code
987 * psa_hash_operation_t operation = {0};
988 * \endcode
989 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
990 * for example:
991 * \code
992 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
993 * \endcode
994 * - Assign the result of the function psa_hash_operation_init()
995 * to the structure, for example:
996 * \code
997 * psa_hash_operation_t operation;
998 * operation = psa_hash_operation_init();
999 * \endcode
1000 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001001 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001002 * make any assumptions about the content of this structure except
1003 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001004typedef struct psa_hash_operation_s psa_hash_operation_t;
1005
Jaeden Amero6a25b412019-01-04 11:47:44 +00001006/** \def PSA_HASH_OPERATION_INIT
1007 *
1008 * This macro returns a suitable initializer for a hash operation object
1009 * of type #psa_hash_operation_t.
1010 */
1011#ifdef __DOXYGEN_ONLY__
1012/* This is an example definition for documentation purposes.
1013 * Implementations should define a suitable value in `crypto_struct.h`.
1014 */
1015#define PSA_HASH_OPERATION_INIT {0}
1016#endif
1017
1018/** Return an initial value for a hash operation object.
1019 */
1020static psa_hash_operation_t psa_hash_operation_init(void);
1021
Gilles Peskinef45adda2019-01-14 18:29:18 +01001022/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001023 *
1024 * The sequence of operations to calculate a hash (message digest)
1025 * is as follows:
1026 * -# Allocate an operation object which will be passed to all the functions
1027 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +00001028 * -# Initialize the operation object with one of the methods described in the
1029 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001030 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001031 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001032 * of the message each time. The hash that is calculated is the hash
1033 * of the concatenation of these messages in order.
1034 * -# To calculate the hash, call psa_hash_finish().
1035 * To compare the hash with an expected value, call psa_hash_verify().
1036 *
1037 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +00001038 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001039 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001040 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001041 * eventually terminate the operation. The following events terminate an
1042 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001043 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001044 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001045 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001046 * \param[in,out] operation The operation object to set up. It must have
1047 * been initialized as per the documentation for
1048 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001049 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1050 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001051 *
Gilles Peskine28538492018-07-11 17:34:00 +02001052 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001053 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001054 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001055 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001056 * \retval #PSA_ERROR_BAD_STATE
1057 * The operation state is not valid (already set up and not
1058 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001059 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1060 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1061 * \retval #PSA_ERROR_HARDWARE_FAILURE
1062 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001063 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001064psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001065 psa_algorithm_t alg);
1066
Gilles Peskine308b91d2018-02-08 09:47:44 +01001067/** Add a message fragment to a multipart hash operation.
1068 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001069 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001070 *
1071 * If this function returns an error status, the operation becomes inactive.
1072 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001073 * \param[in,out] operation Active hash operation.
1074 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001075 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001076 *
Gilles Peskine28538492018-07-11 17:34:00 +02001077 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001078 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001079 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001080 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001081 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1082 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1083 * \retval #PSA_ERROR_HARDWARE_FAILURE
1084 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001085 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001086psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1087 const uint8_t *input,
1088 size_t input_length);
1089
Gilles Peskine308b91d2018-02-08 09:47:44 +01001090/** Finish the calculation of the hash of a message.
1091 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001092 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001093 * This function calculates the hash of the message formed by concatenating
1094 * the inputs passed to preceding calls to psa_hash_update().
1095 *
1096 * When this function returns, the operation becomes inactive.
1097 *
1098 * \warning Applications should not call this function if they expect
1099 * a specific value for the hash. Call psa_hash_verify() instead.
1100 * Beware that comparing integrity or authenticity data such as
1101 * hash values with a function such as \c memcmp is risky
1102 * because the time taken by the comparison may leak information
1103 * about the hashed data which could allow an attacker to guess
1104 * a valid hash and thereby bypass security controls.
1105 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001106 * \param[in,out] operation Active hash operation.
1107 * \param[out] hash Buffer where the hash is to be written.
1108 * \param hash_size Size of the \p hash buffer in bytes.
1109 * \param[out] hash_length On success, the number of bytes
1110 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001111 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001112 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001113 *
Gilles Peskine28538492018-07-11 17:34:00 +02001114 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001115 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001116 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001117 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001118 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001119 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001120 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001121 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001122 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1123 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1124 * \retval #PSA_ERROR_HARDWARE_FAILURE
1125 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001126 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001127psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1128 uint8_t *hash,
1129 size_t hash_size,
1130 size_t *hash_length);
1131
Gilles Peskine308b91d2018-02-08 09:47:44 +01001132/** Finish the calculation of the hash of a message and compare it with
1133 * an expected value.
1134 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001135 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001136 * This function calculates the hash of the message formed by concatenating
1137 * the inputs passed to preceding calls to psa_hash_update(). It then
1138 * compares the calculated hash with the expected hash passed as a
1139 * parameter to this function.
1140 *
1141 * When this function returns, the operation becomes inactive.
1142 *
Gilles Peskine19067982018-03-20 17:54:53 +01001143 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001144 * comparison between the actual hash and the expected hash is performed
1145 * in constant time.
1146 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001147 * \param[in,out] operation Active hash operation.
1148 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001149 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001150 *
Gilles Peskine28538492018-07-11 17:34:00 +02001151 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001152 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001153 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001154 * The hash of the message was calculated successfully, but it
1155 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001156 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001157 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001158 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1159 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1160 * \retval #PSA_ERROR_HARDWARE_FAILURE
1161 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001162 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001163psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1164 const uint8_t *hash,
1165 size_t hash_length);
1166
Gilles Peskine308b91d2018-02-08 09:47:44 +01001167/** Abort a hash operation.
1168 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001169 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001170 * \p operation structure itself. Once aborted, the operation object
1171 * can be reused for another operation by calling
1172 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001173 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001174 * You may call this function any time after the operation object has
1175 * been initialized by any of the following methods:
1176 * - A call to psa_hash_setup(), whether it succeeds or not.
1177 * - Initializing the \c struct to all-bits-zero.
1178 * - Initializing the \c struct to logical zeros, e.g.
1179 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001180 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001181 * In particular, calling psa_hash_abort() after the operation has been
1182 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1183 * psa_hash_verify() is safe and has no effect.
1184 *
1185 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001186 *
Gilles Peskine28538492018-07-11 17:34:00 +02001187 * \retval #PSA_SUCCESS
1188 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001189 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001190 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1191 * \retval #PSA_ERROR_HARDWARE_FAILURE
1192 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001193 */
1194psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001195
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001196/** Clone a hash operation.
1197 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001198 * This function copies the state of an ongoing hash operation to
1199 * a new operation object. In other words, this function is equivalent
1200 * to calling psa_hash_setup() on \p target_operation with the same
1201 * algorithm that \p source_operation was set up for, then
1202 * psa_hash_update() on \p target_operation with the same input that
1203 * that was passed to \p source_operation. After this function returns, the
1204 * two objects are independent, i.e. subsequent calls involving one of
1205 * the objects do not affect the other object.
1206 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001207 * \param[in] source_operation The active hash operation to clone.
1208 * \param[in,out] target_operation The operation object to set up.
1209 * It must be initialized but not active.
1210 *
1211 * \retval #PSA_SUCCESS
1212 * \retval #PSA_ERROR_BAD_STATE
1213 * \p source_operation is not an active hash operation.
1214 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001215 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001216 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1217 * \retval #PSA_ERROR_HARDWARE_FAILURE
1218 * \retval #PSA_ERROR_TAMPERING_DETECTED
1219 */
1220psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1221 psa_hash_operation_t *target_operation);
1222
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001223/**@}*/
1224
Gilles Peskine8c9def32018-02-08 10:02:12 +01001225/** \defgroup MAC Message authentication codes
1226 * @{
1227 */
1228
Gilles Peskine69647a42019-01-14 20:18:12 +01001229/** Calculate the MAC (message authentication code) of a message.
1230 *
1231 * \note To verify the MAC of a message against an
1232 * expected value, use psa_mac_verify() instead.
1233 * Beware that comparing integrity or authenticity data such as
1234 * MAC values with a function such as \c memcmp is risky
1235 * because the time taken by the comparison may leak information
1236 * about the MAC value which could allow an attacker to guess
1237 * a valid MAC and thereby bypass security controls.
1238 *
1239 * \param handle Handle to the key to use for the operation.
1240 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001241 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001242 * \param[in] input Buffer containing the input message.
1243 * \param input_length Size of the \p input buffer in bytes.
1244 * \param[out] mac Buffer where the MAC value is to be written.
1245 * \param mac_size Size of the \p mac buffer in bytes.
1246 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001247 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001248 *
1249 * \retval #PSA_SUCCESS
1250 * Success.
1251 * \retval #PSA_ERROR_INVALID_HANDLE
1252 * \retval #PSA_ERROR_EMPTY_SLOT
1253 * \retval #PSA_ERROR_NOT_PERMITTED
1254 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001255 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001256 * \retval #PSA_ERROR_NOT_SUPPORTED
1257 * \p alg is not supported or is not a MAC algorithm.
1258 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1259 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1260 * \retval #PSA_ERROR_HARDWARE_FAILURE
1261 * \retval #PSA_ERROR_TAMPERING_DETECTED
1262 * \retval #PSA_ERROR_BAD_STATE
1263 * The library has not been previously initialized by psa_crypto_init().
1264 * It is implementation-dependent whether a failure to initialize
1265 * results in this error code.
1266 */
1267psa_status_t psa_mac_compute(psa_key_handle_t handle,
1268 psa_algorithm_t alg,
1269 const uint8_t *input,
1270 size_t input_length,
1271 uint8_t *mac,
1272 size_t mac_size,
1273 size_t *mac_length);
1274
1275/** Calculate the MAC of a message and compare it with a reference value.
1276 *
1277 * \param handle Handle to the key to use for the operation.
1278 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001279 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001280 * \param[in] input Buffer containing the input message.
1281 * \param input_length Size of the \p input buffer in bytes.
1282 * \param[out] mac Buffer containing the expected MAC value.
1283 * \param mac_length Size of the \p mac buffer in bytes.
1284 *
1285 * \retval #PSA_SUCCESS
1286 * The expected MAC is identical to the actual MAC of the input.
1287 * \retval #PSA_ERROR_INVALID_SIGNATURE
1288 * The MAC of the message was calculated successfully, but it
1289 * differs from the expected value.
1290 * \retval #PSA_ERROR_INVALID_HANDLE
1291 * \retval #PSA_ERROR_EMPTY_SLOT
1292 * \retval #PSA_ERROR_NOT_PERMITTED
1293 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001294 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001295 * \retval #PSA_ERROR_NOT_SUPPORTED
1296 * \p alg is not supported or is not a MAC algorithm.
1297 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1298 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1299 * \retval #PSA_ERROR_HARDWARE_FAILURE
1300 * \retval #PSA_ERROR_TAMPERING_DETECTED
1301 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001302psa_status_t psa_mac_verify(psa_key_handle_t handle,
1303 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001304 const uint8_t *input,
1305 size_t input_length,
1306 const uint8_t *mac,
1307 const size_t mac_length);
1308
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001309/** The type of the state data structure for multipart MAC operations.
1310 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001311 * Before calling any function on a MAC operation object, the application must
1312 * initialize it by any of the following means:
1313 * - Set the structure to all-bits-zero, for example:
1314 * \code
1315 * psa_mac_operation_t operation;
1316 * memset(&operation, 0, sizeof(operation));
1317 * \endcode
1318 * - Initialize the structure to logical zero values, for example:
1319 * \code
1320 * psa_mac_operation_t operation = {0};
1321 * \endcode
1322 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1323 * for example:
1324 * \code
1325 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1326 * \endcode
1327 * - Assign the result of the function psa_mac_operation_init()
1328 * to the structure, for example:
1329 * \code
1330 * psa_mac_operation_t operation;
1331 * operation = psa_mac_operation_init();
1332 * \endcode
1333 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001334 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001335 * make any assumptions about the content of this structure except
1336 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001337typedef struct psa_mac_operation_s psa_mac_operation_t;
1338
Jaeden Amero769ce272019-01-04 11:48:03 +00001339/** \def PSA_MAC_OPERATION_INIT
1340 *
1341 * This macro returns a suitable initializer for a MAC operation object of type
1342 * #psa_mac_operation_t.
1343 */
1344#ifdef __DOXYGEN_ONLY__
1345/* This is an example definition for documentation purposes.
1346 * Implementations should define a suitable value in `crypto_struct.h`.
1347 */
1348#define PSA_MAC_OPERATION_INIT {0}
1349#endif
1350
1351/** Return an initial value for a MAC operation object.
1352 */
1353static psa_mac_operation_t psa_mac_operation_init(void);
1354
Gilles Peskinef45adda2019-01-14 18:29:18 +01001355/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001356 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001357 * This function sets up the calculation of the MAC
1358 * (message authentication code) of a byte string.
1359 * To verify the MAC of a message against an
1360 * expected value, use psa_mac_verify_setup() instead.
1361 *
1362 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001363 * -# Allocate an operation object which will be passed to all the functions
1364 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001365 * -# Initialize the operation object with one of the methods described in the
1366 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001367 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001368 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1369 * of the message each time. The MAC that is calculated is the MAC
1370 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001371 * -# At the end of the message, call psa_mac_sign_finish() to finish
1372 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001373 *
1374 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001375 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001376 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001377 * After a successful call to psa_mac_sign_setup(), the application must
1378 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001379 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001380 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001381 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001382 * \param[in,out] operation The operation object to set up. It must have
1383 * been initialized as per the documentation for
1384 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001385 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001386 * It must remain valid until the operation
1387 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001388 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001389 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001390 *
Gilles Peskine28538492018-07-11 17:34:00 +02001391 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001392 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001393 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001394 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001395 * \retval #PSA_ERROR_NOT_PERMITTED
1396 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001397 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001398 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001399 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001400 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1401 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1402 * \retval #PSA_ERROR_HARDWARE_FAILURE
1403 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001404 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001405 * The operation state is not valid (already set up and not
1406 * subsequently completed).
1407 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001408 * The library has not been previously initialized by psa_crypto_init().
1409 * It is implementation-dependent whether a failure to initialize
1410 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001411 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001412psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001413 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001414 psa_algorithm_t alg);
1415
Gilles Peskinef45adda2019-01-14 18:29:18 +01001416/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001417 *
1418 * This function sets up the verification of the MAC
1419 * (message authentication code) of a byte string against an expected value.
1420 *
1421 * The sequence of operations to verify a MAC is as follows:
1422 * -# Allocate an operation object which will be passed to all the functions
1423 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001424 * -# Initialize the operation object with one of the methods described in the
1425 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001426 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001427 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1428 * of the message each time. The MAC that is calculated is the MAC
1429 * of the concatenation of these messages in order.
1430 * -# At the end of the message, call psa_mac_verify_finish() to finish
1431 * calculating the actual MAC of the message and verify it against
1432 * the expected value.
1433 *
1434 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001435 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001436 *
1437 * After a successful call to psa_mac_verify_setup(), the application must
1438 * eventually terminate the operation through one of the following methods:
1439 * - A failed call to psa_mac_update().
1440 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1441 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001442 * \param[in,out] operation The operation object to set up. It must have
1443 * been initialized as per the documentation for
1444 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001445 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001446 * It must remain valid until the operation
1447 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001448 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1449 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001450 *
Gilles Peskine28538492018-07-11 17:34:00 +02001451 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001452 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001453 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001454 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001455 * \retval #PSA_ERROR_NOT_PERMITTED
1456 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001457 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001458 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001459 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001460 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1461 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1462 * \retval #PSA_ERROR_HARDWARE_FAILURE
1463 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001464 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001465 * The operation state is not valid (already set up and not
1466 * subsequently completed).
1467 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001468 * The library has not been previously initialized by psa_crypto_init().
1469 * It is implementation-dependent whether a failure to initialize
1470 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001471 */
1472psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001473 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001474 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001475
Gilles Peskinedcd14942018-07-12 00:30:52 +02001476/** Add a message fragment to a multipart MAC operation.
1477 *
1478 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1479 * before calling this function.
1480 *
1481 * If this function returns an error status, the operation becomes inactive.
1482 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001483 * \param[in,out] operation Active MAC operation.
1484 * \param[in] input Buffer containing the message fragment to add to
1485 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001486 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001487 *
1488 * \retval #PSA_SUCCESS
1489 * Success.
1490 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001491 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001492 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1493 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1494 * \retval #PSA_ERROR_HARDWARE_FAILURE
1495 * \retval #PSA_ERROR_TAMPERING_DETECTED
1496 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001497psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1498 const uint8_t *input,
1499 size_t input_length);
1500
Gilles Peskinedcd14942018-07-12 00:30:52 +02001501/** Finish the calculation of the MAC of a message.
1502 *
1503 * The application must call psa_mac_sign_setup() before calling this function.
1504 * This function calculates the MAC of the message formed by concatenating
1505 * the inputs passed to preceding calls to psa_mac_update().
1506 *
1507 * When this function returns, the operation becomes inactive.
1508 *
1509 * \warning Applications should not call this function if they expect
1510 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1511 * Beware that comparing integrity or authenticity data such as
1512 * MAC values with a function such as \c memcmp is risky
1513 * because the time taken by the comparison may leak information
1514 * about the MAC value which could allow an attacker to guess
1515 * a valid MAC and thereby bypass security controls.
1516 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001517 * \param[in,out] operation Active MAC operation.
1518 * \param[out] mac Buffer where the MAC value is to be written.
1519 * \param mac_size Size of the \p mac buffer in bytes.
1520 * \param[out] mac_length On success, the number of bytes
1521 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001522 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001523 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001524 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001525 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001526 *
1527 * \retval #PSA_SUCCESS
1528 * Success.
1529 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001530 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001531 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001532 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001533 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1534 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1535 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1536 * \retval #PSA_ERROR_HARDWARE_FAILURE
1537 * \retval #PSA_ERROR_TAMPERING_DETECTED
1538 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001539psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1540 uint8_t *mac,
1541 size_t mac_size,
1542 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001543
Gilles Peskinedcd14942018-07-12 00:30:52 +02001544/** Finish the calculation of the MAC of a message and compare it with
1545 * an expected value.
1546 *
1547 * The application must call psa_mac_verify_setup() before calling this function.
1548 * This function calculates the MAC of the message formed by concatenating
1549 * the inputs passed to preceding calls to psa_mac_update(). It then
1550 * compares the calculated MAC with the expected MAC passed as a
1551 * parameter to this function.
1552 *
1553 * When this function returns, the operation becomes inactive.
1554 *
1555 * \note Implementations shall make the best effort to ensure that the
1556 * comparison between the actual MAC and the expected MAC is performed
1557 * in constant time.
1558 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001559 * \param[in,out] operation Active MAC operation.
1560 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001561 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001562 *
1563 * \retval #PSA_SUCCESS
1564 * The expected MAC is identical to the actual MAC of the message.
1565 * \retval #PSA_ERROR_INVALID_SIGNATURE
1566 * The MAC of the message was calculated successfully, but it
1567 * differs from the expected MAC.
1568 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001569 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001570 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1571 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1572 * \retval #PSA_ERROR_HARDWARE_FAILURE
1573 * \retval #PSA_ERROR_TAMPERING_DETECTED
1574 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001575psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1576 const uint8_t *mac,
1577 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001578
Gilles Peskinedcd14942018-07-12 00:30:52 +02001579/** Abort a MAC operation.
1580 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001581 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001582 * \p operation structure itself. Once aborted, the operation object
1583 * can be reused for another operation by calling
1584 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001585 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001586 * You may call this function any time after the operation object has
1587 * been initialized by any of the following methods:
1588 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1589 * it succeeds or not.
1590 * - Initializing the \c struct to all-bits-zero.
1591 * - Initializing the \c struct to logical zeros, e.g.
1592 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001593 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001594 * In particular, calling psa_mac_abort() after the operation has been
1595 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1596 * psa_mac_verify_finish() is safe and has no effect.
1597 *
1598 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001599 *
1600 * \retval #PSA_SUCCESS
1601 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001602 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001603 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1604 * \retval #PSA_ERROR_HARDWARE_FAILURE
1605 * \retval #PSA_ERROR_TAMPERING_DETECTED
1606 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001607psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1608
1609/**@}*/
1610
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001611/** \defgroup cipher Symmetric ciphers
1612 * @{
1613 */
1614
Gilles Peskine69647a42019-01-14 20:18:12 +01001615/** Encrypt a message using a symmetric cipher.
1616 *
1617 * This function encrypts a message with a random IV (initialization
1618 * vector).
1619 *
1620 * \param handle Handle to the key to use for the operation.
1621 * It must remain valid until the operation
1622 * terminates.
1623 * \param alg The cipher algorithm to compute
1624 * (\c PSA_ALG_XXX value such that
1625 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1626 * \param[in] input Buffer containing the message to encrypt.
1627 * \param input_length Size of the \p input buffer in bytes.
1628 * \param[out] output Buffer where the output is to be written.
1629 * The output contains the IV followed by
1630 * the ciphertext proper.
1631 * \param output_size Size of the \p output buffer in bytes.
1632 * \param[out] output_length On success, the number of bytes
1633 * that make up the output.
1634 *
1635 * \retval #PSA_SUCCESS
1636 * Success.
1637 * \retval #PSA_ERROR_INVALID_HANDLE
1638 * \retval #PSA_ERROR_EMPTY_SLOT
1639 * \retval #PSA_ERROR_NOT_PERMITTED
1640 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001641 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001642 * \retval #PSA_ERROR_NOT_SUPPORTED
1643 * \p alg is not supported or is not a cipher algorithm.
1644 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1645 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1646 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1647 * \retval #PSA_ERROR_HARDWARE_FAILURE
1648 * \retval #PSA_ERROR_TAMPERING_DETECTED
1649 */
1650psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1651 psa_algorithm_t alg,
1652 const uint8_t *input,
1653 size_t input_length,
1654 uint8_t *output,
1655 size_t output_size,
1656 size_t *output_length);
1657
1658/** Decrypt a message using a symmetric cipher.
1659 *
1660 * This function decrypts a message encrypted with a symmetric cipher.
1661 *
1662 * \param handle Handle to the key to use for the operation.
1663 * It must remain valid until the operation
1664 * terminates.
1665 * \param alg The cipher algorithm to compute
1666 * (\c PSA_ALG_XXX value such that
1667 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1668 * \param[in] input Buffer containing the message to decrypt.
1669 * This consists of the IV followed by the
1670 * ciphertext proper.
1671 * \param input_length Size of the \p input buffer in bytes.
1672 * \param[out] output Buffer where the plaintext is to be written.
1673 * \param output_size Size of the \p output buffer in bytes.
1674 * \param[out] output_length On success, the number of bytes
1675 * that make up the output.
1676 *
1677 * \retval #PSA_SUCCESS
1678 * Success.
1679 * \retval #PSA_ERROR_INVALID_HANDLE
1680 * \retval #PSA_ERROR_EMPTY_SLOT
1681 * \retval #PSA_ERROR_NOT_PERMITTED
1682 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001683 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001684 * \retval #PSA_ERROR_NOT_SUPPORTED
1685 * \p alg is not supported or is not a cipher algorithm.
1686 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1687 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1688 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1689 * \retval #PSA_ERROR_HARDWARE_FAILURE
1690 * \retval #PSA_ERROR_TAMPERING_DETECTED
1691 */
1692psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1693 psa_algorithm_t alg,
1694 const uint8_t *input,
1695 size_t input_length,
1696 uint8_t *output,
1697 size_t output_size,
1698 size_t *output_length);
1699
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001700/** The type of the state data structure for multipart cipher operations.
1701 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001702 * Before calling any function on a cipher operation object, the application
1703 * must initialize it by any of the following means:
1704 * - Set the structure to all-bits-zero, for example:
1705 * \code
1706 * psa_cipher_operation_t operation;
1707 * memset(&operation, 0, sizeof(operation));
1708 * \endcode
1709 * - Initialize the structure to logical zero values, for example:
1710 * \code
1711 * psa_cipher_operation_t operation = {0};
1712 * \endcode
1713 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1714 * for example:
1715 * \code
1716 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1717 * \endcode
1718 * - Assign the result of the function psa_cipher_operation_init()
1719 * to the structure, for example:
1720 * \code
1721 * psa_cipher_operation_t operation;
1722 * operation = psa_cipher_operation_init();
1723 * \endcode
1724 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001725 * This is an implementation-defined \c struct. Applications should not
1726 * make any assumptions about the content of this structure except
1727 * as directed by the documentation of a specific implementation. */
1728typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1729
Jaeden Amero5bae2272019-01-04 11:48:27 +00001730/** \def PSA_CIPHER_OPERATION_INIT
1731 *
1732 * This macro returns a suitable initializer for a cipher operation object of
1733 * type #psa_cipher_operation_t.
1734 */
1735#ifdef __DOXYGEN_ONLY__
1736/* This is an example definition for documentation purposes.
1737 * Implementations should define a suitable value in `crypto_struct.h`.
1738 */
1739#define PSA_CIPHER_OPERATION_INIT {0}
1740#endif
1741
1742/** Return an initial value for a cipher operation object.
1743 */
1744static psa_cipher_operation_t psa_cipher_operation_init(void);
1745
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001746/** Set the key for a multipart symmetric encryption operation.
1747 *
1748 * The sequence of operations to encrypt a message with a symmetric cipher
1749 * is as follows:
1750 * -# Allocate an operation object which will be passed to all the functions
1751 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001752 * -# Initialize the operation object with one of the methods described in the
1753 * documentation for #psa_cipher_operation_t, e.g.
1754 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001755 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001756 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001757 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001758 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001759 * requires a specific IV value.
1760 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1761 * of the message each time.
1762 * -# Call psa_cipher_finish().
1763 *
1764 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001765 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001766 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001767 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001768 * eventually terminate the operation. The following events terminate an
1769 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001770 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001771 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001772 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001773 * \param[in,out] operation The operation object to set up. It must have
1774 * been initialized as per the documentation for
1775 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001776 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001777 * It must remain valid until the operation
1778 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001779 * \param alg The cipher algorithm to compute
1780 * (\c PSA_ALG_XXX value such that
1781 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001782 *
Gilles Peskine28538492018-07-11 17:34:00 +02001783 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001784 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001785 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001786 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001787 * \retval #PSA_ERROR_NOT_PERMITTED
1788 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001789 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001790 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001791 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001792 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1793 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1794 * \retval #PSA_ERROR_HARDWARE_FAILURE
1795 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001796 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001797 * The operation state is not valid (already set up and not
1798 * subsequently completed).
1799 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001800 * The library has not been previously initialized by psa_crypto_init().
1801 * It is implementation-dependent whether a failure to initialize
1802 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001803 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001804psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001805 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001806 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001807
1808/** Set the key for a multipart symmetric decryption operation.
1809 *
1810 * The sequence of operations to decrypt a message with a symmetric cipher
1811 * is as follows:
1812 * -# Allocate an operation object which will be passed to all the functions
1813 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001814 * -# Initialize the operation object with one of the methods described in the
1815 * documentation for #psa_cipher_operation_t, e.g.
1816 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001817 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001818 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001819 * decryption. If the IV is prepended to the ciphertext, you can call
1820 * psa_cipher_update() on a buffer containing the IV followed by the
1821 * beginning of the message.
1822 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1823 * of the message each time.
1824 * -# Call psa_cipher_finish().
1825 *
1826 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001827 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001828 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001829 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001830 * eventually terminate the operation. The following events terminate an
1831 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001832 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001833 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001834 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001835 * \param[in,out] operation The operation object to set up. It must have
1836 * been initialized as per the documentation for
1837 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001838 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001839 * It must remain valid until the operation
1840 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001841 * \param alg The cipher algorithm to compute
1842 * (\c PSA_ALG_XXX value such that
1843 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001844 *
Gilles Peskine28538492018-07-11 17:34:00 +02001845 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001846 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001847 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001848 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001849 * \retval #PSA_ERROR_NOT_PERMITTED
1850 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001851 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001852 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001853 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001854 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1855 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1856 * \retval #PSA_ERROR_HARDWARE_FAILURE
1857 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001858 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001859 * The operation state is not valid (already set up and not
1860 * subsequently completed).
1861 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001862 * The library has not been previously initialized by psa_crypto_init().
1863 * It is implementation-dependent whether a failure to initialize
1864 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001865 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001866psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001867 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001868 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001869
Gilles Peskinedcd14942018-07-12 00:30:52 +02001870/** Generate an IV for a symmetric encryption operation.
1871 *
1872 * This function generates a random IV (initialization vector), nonce
1873 * or initial counter value for the encryption operation as appropriate
1874 * for the chosen algorithm, key type and key size.
1875 *
1876 * The application must call psa_cipher_encrypt_setup() before
1877 * calling this function.
1878 *
1879 * If this function returns an error status, the operation becomes inactive.
1880 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001881 * \param[in,out] operation Active cipher operation.
1882 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001883 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001884 * \param[out] iv_length On success, the number of bytes of the
1885 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001886 *
1887 * \retval #PSA_SUCCESS
1888 * Success.
1889 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001890 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001891 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001892 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001893 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1894 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1895 * \retval #PSA_ERROR_HARDWARE_FAILURE
1896 * \retval #PSA_ERROR_TAMPERING_DETECTED
1897 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001898psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1899 unsigned char *iv,
1900 size_t iv_size,
1901 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001902
Gilles Peskinedcd14942018-07-12 00:30:52 +02001903/** Set the IV for a symmetric encryption or decryption operation.
1904 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001905 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001906 * or initial counter value for the encryption or decryption operation.
1907 *
1908 * The application must call psa_cipher_encrypt_setup() before
1909 * calling this function.
1910 *
1911 * If this function returns an error status, the operation becomes inactive.
1912 *
1913 * \note When encrypting, applications should use psa_cipher_generate_iv()
1914 * instead of this function, unless implementing a protocol that requires
1915 * a non-random IV.
1916 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001917 * \param[in,out] operation Active cipher operation.
1918 * \param[in] iv Buffer containing the IV to use.
1919 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001920 *
1921 * \retval #PSA_SUCCESS
1922 * Success.
1923 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001924 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001925 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001926 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001927 * or the chosen algorithm does not use an IV.
1928 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1929 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1930 * \retval #PSA_ERROR_HARDWARE_FAILURE
1931 * \retval #PSA_ERROR_TAMPERING_DETECTED
1932 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001933psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1934 const unsigned char *iv,
1935 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001936
Gilles Peskinedcd14942018-07-12 00:30:52 +02001937/** Encrypt or decrypt a message fragment in an active cipher operation.
1938 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001939 * Before calling this function, you must:
1940 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1941 * The choice of setup function determines whether this function
1942 * encrypts or decrypts its input.
1943 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1944 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001945 *
1946 * If this function returns an error status, the operation becomes inactive.
1947 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001948 * \param[in,out] operation Active cipher operation.
1949 * \param[in] input Buffer containing the message fragment to
1950 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001951 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001952 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001953 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001954 * \param[out] output_length On success, the number of bytes
1955 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001956 *
1957 * \retval #PSA_SUCCESS
1958 * Success.
1959 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001960 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001961 * not set, or already completed).
1962 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1963 * The size of the \p output buffer is too small.
1964 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1965 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1966 * \retval #PSA_ERROR_HARDWARE_FAILURE
1967 * \retval #PSA_ERROR_TAMPERING_DETECTED
1968 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001969psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1970 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001971 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001972 unsigned char *output,
1973 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001974 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001975
Gilles Peskinedcd14942018-07-12 00:30:52 +02001976/** Finish encrypting or decrypting a message in a cipher operation.
1977 *
1978 * The application must call psa_cipher_encrypt_setup() or
1979 * psa_cipher_decrypt_setup() before calling this function. The choice
1980 * of setup function determines whether this function encrypts or
1981 * decrypts its input.
1982 *
1983 * This function finishes the encryption or decryption of the message
1984 * formed by concatenating the inputs passed to preceding calls to
1985 * psa_cipher_update().
1986 *
1987 * When this function returns, the operation becomes inactive.
1988 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001989 * \param[in,out] operation Active cipher operation.
1990 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001991 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001992 * \param[out] output_length On success, the number of bytes
1993 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001994 *
1995 * \retval #PSA_SUCCESS
1996 * Success.
1997 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001998 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001999 * not set, or already completed).
2000 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2001 * The size of the \p output buffer is too small.
2002 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2003 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2004 * \retval #PSA_ERROR_HARDWARE_FAILURE
2005 * \retval #PSA_ERROR_TAMPERING_DETECTED
2006 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002007psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002008 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002009 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002010 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002011
Gilles Peskinedcd14942018-07-12 00:30:52 +02002012/** Abort a cipher operation.
2013 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002014 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002015 * \p operation structure itself. Once aborted, the operation object
2016 * can be reused for another operation by calling
2017 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002018 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002019 * You may call this function any time after the operation object has
2020 * been initialized by any of the following methods:
2021 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2022 * whether it succeeds or not.
2023 * - Initializing the \c struct to all-bits-zero.
2024 * - Initializing the \c struct to logical zeros, e.g.
2025 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002026 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002027 * In particular, calling psa_cipher_abort() after the operation has been
2028 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2029 * is safe and has no effect.
2030 *
2031 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002032 *
2033 * \retval #PSA_SUCCESS
2034 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002035 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002036 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2037 * \retval #PSA_ERROR_HARDWARE_FAILURE
2038 * \retval #PSA_ERROR_TAMPERING_DETECTED
2039 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002040psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2041
2042/**@}*/
2043
Gilles Peskine3b555712018-03-03 21:27:57 +01002044/** \defgroup aead Authenticated encryption with associated data (AEAD)
2045 * @{
2046 */
2047
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002048/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002049 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002050 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002051 * \param alg The AEAD algorithm to compute
2052 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002053 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002054 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002055 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002056 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002057 * but not encrypted.
2058 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002059 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002060 * encrypted.
2061 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002062 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002063 * encrypted data. The additional data is not
2064 * part of this output. For algorithms where the
2065 * encrypted data and the authentication tag
2066 * are defined as separate outputs, the
2067 * authentication tag is appended to the
2068 * encrypted data.
2069 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2070 * This must be at least
2071 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2072 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002073 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002074 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002075 *
Gilles Peskine28538492018-07-11 17:34:00 +02002076 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002077 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002078 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002079 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002080 * \retval #PSA_ERROR_NOT_PERMITTED
2081 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002082 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002083 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002084 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002085 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2086 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2087 * \retval #PSA_ERROR_HARDWARE_FAILURE
2088 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002089 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002090 * The library has not been previously initialized by psa_crypto_init().
2091 * It is implementation-dependent whether a failure to initialize
2092 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002093 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002094psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002095 psa_algorithm_t alg,
2096 const uint8_t *nonce,
2097 size_t nonce_length,
2098 const uint8_t *additional_data,
2099 size_t additional_data_length,
2100 const uint8_t *plaintext,
2101 size_t plaintext_length,
2102 uint8_t *ciphertext,
2103 size_t ciphertext_size,
2104 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002105
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002106/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002107 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002108 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002109 * \param alg The AEAD algorithm to compute
2110 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002111 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002112 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002113 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002114 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002115 * but not encrypted.
2116 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002117 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002118 * encrypted. For algorithms where the
2119 * encrypted data and the authentication tag
2120 * are defined as separate inputs, the buffer
2121 * must contain the encrypted data followed
2122 * by the authentication tag.
2123 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002124 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002125 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2126 * This must be at least
2127 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2128 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002129 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002130 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002131 *
Gilles Peskine28538492018-07-11 17:34:00 +02002132 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002133 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002134 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002135 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002136 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002137 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002138 * \retval #PSA_ERROR_NOT_PERMITTED
2139 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002140 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002141 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002142 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002143 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2144 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2145 * \retval #PSA_ERROR_HARDWARE_FAILURE
2146 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002147 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002148 * The library has not been previously initialized by psa_crypto_init().
2149 * It is implementation-dependent whether a failure to initialize
2150 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002151 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002152psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002153 psa_algorithm_t alg,
2154 const uint8_t *nonce,
2155 size_t nonce_length,
2156 const uint8_t *additional_data,
2157 size_t additional_data_length,
2158 const uint8_t *ciphertext,
2159 size_t ciphertext_length,
2160 uint8_t *plaintext,
2161 size_t plaintext_size,
2162 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002163
Gilles Peskine30a9e412019-01-14 18:36:12 +01002164/** The type of the state data structure for multipart AEAD operations.
2165 *
2166 * Before calling any function on an AEAD operation object, the application
2167 * must initialize it by any of the following means:
2168 * - Set the structure to all-bits-zero, for example:
2169 * \code
2170 * psa_aead_operation_t operation;
2171 * memset(&operation, 0, sizeof(operation));
2172 * \endcode
2173 * - Initialize the structure to logical zero values, for example:
2174 * \code
2175 * psa_aead_operation_t operation = {0};
2176 * \endcode
2177 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2178 * for example:
2179 * \code
2180 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2181 * \endcode
2182 * - Assign the result of the function psa_aead_operation_init()
2183 * to the structure, for example:
2184 * \code
2185 * psa_aead_operation_t operation;
2186 * operation = psa_aead_operation_init();
2187 * \endcode
2188 *
2189 * This is an implementation-defined \c struct. Applications should not
2190 * make any assumptions about the content of this structure except
2191 * as directed by the documentation of a specific implementation. */
2192typedef struct psa_aead_operation_s psa_aead_operation_t;
2193
2194/** \def PSA_AEAD_OPERATION_INIT
2195 *
2196 * This macro returns a suitable initializer for an AEAD operation object of
2197 * type #psa_aead_operation_t.
2198 */
2199#ifdef __DOXYGEN_ONLY__
2200/* This is an example definition for documentation purposes.
2201 * Implementations should define a suitable value in `crypto_struct.h`.
2202 */
2203#define PSA_AEAD_OPERATION_INIT {0}
2204#endif
2205
2206/** Return an initial value for an AEAD operation object.
2207 */
2208static psa_aead_operation_t psa_aead_operation_init(void);
2209
2210/** Set the key for a multipart authenticated encryption operation.
2211 *
2212 * The sequence of operations to encrypt a message with authentication
2213 * is as follows:
2214 * -# Allocate an operation object which will be passed to all the functions
2215 * listed here.
2216 * -# Initialize the operation object with one of the methods described in the
2217 * documentation for #psa_aead_operation_t, e.g.
2218 * PSA_AEAD_OPERATION_INIT.
2219 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002220 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2221 * inputs to the subsequent calls to psa_aead_update_ad() and
2222 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2223 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002224 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2225 * generate or set the nonce. You should use
2226 * psa_aead_generate_nonce() unless the protocol you are implementing
2227 * requires a specific nonce value.
2228 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2229 * of the non-encrypted additional authenticated data each time.
2230 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002231 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002232 * -# Call psa_aead_finish().
2233 *
2234 * The application may call psa_aead_abort() at any time after the operation
2235 * has been initialized.
2236 *
2237 * After a successful call to psa_aead_encrypt_setup(), the application must
2238 * eventually terminate the operation. The following events terminate an
2239 * operation:
2240 * - A failed call to any of the \c psa_aead_xxx functions.
2241 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2242 *
2243 * \param[in,out] operation The operation object to set up. It must have
2244 * been initialized as per the documentation for
2245 * #psa_aead_operation_t and not yet in use.
2246 * \param handle Handle to the key to use for the operation.
2247 * It must remain valid until the operation
2248 * terminates.
2249 * \param alg The AEAD algorithm to compute
2250 * (\c PSA_ALG_XXX value such that
2251 * #PSA_ALG_IS_AEAD(\p alg) is true).
2252 *
2253 * \retval #PSA_SUCCESS
2254 * Success.
2255 * \retval #PSA_ERROR_INVALID_HANDLE
2256 * \retval #PSA_ERROR_EMPTY_SLOT
2257 * \retval #PSA_ERROR_NOT_PERMITTED
2258 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002259 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002260 * \retval #PSA_ERROR_NOT_SUPPORTED
2261 * \p alg is not supported or is not an AEAD algorithm.
2262 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2263 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2264 * \retval #PSA_ERROR_HARDWARE_FAILURE
2265 * \retval #PSA_ERROR_TAMPERING_DETECTED
2266 * \retval #PSA_ERROR_BAD_STATE
2267 * The library has not been previously initialized by psa_crypto_init().
2268 * It is implementation-dependent whether a failure to initialize
2269 * results in this error code.
2270 */
2271psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2272 psa_key_handle_t handle,
2273 psa_algorithm_t alg);
2274
2275/** Set the key for a multipart authenticated decryption operation.
2276 *
2277 * The sequence of operations to decrypt a message with authentication
2278 * is as follows:
2279 * -# Allocate an operation object which will be passed to all the functions
2280 * listed here.
2281 * -# Initialize the operation object with one of the methods described in the
2282 * documentation for #psa_aead_operation_t, e.g.
2283 * PSA_AEAD_OPERATION_INIT.
2284 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002285 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2286 * inputs to the subsequent calls to psa_aead_update_ad() and
2287 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2288 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002289 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2290 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2291 * of the non-encrypted additional authenticated data each time.
2292 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002293 * of the ciphertext to decrypt each time.
2294 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002295 *
2296 * The application may call psa_aead_abort() at any time after the operation
2297 * has been initialized.
2298 *
2299 * After a successful call to psa_aead_decrypt_setup(), the application must
2300 * eventually terminate the operation. The following events terminate an
2301 * operation:
2302 * - A failed call to any of the \c psa_aead_xxx functions.
2303 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2304 *
2305 * \param[in,out] operation The operation object to set up. It must have
2306 * been initialized as per the documentation for
2307 * #psa_aead_operation_t and not yet in use.
2308 * \param handle Handle to the key to use for the operation.
2309 * It must remain valid until the operation
2310 * terminates.
2311 * \param alg The AEAD algorithm to compute
2312 * (\c PSA_ALG_XXX value such that
2313 * #PSA_ALG_IS_AEAD(\p alg) is true).
2314 *
2315 * \retval #PSA_SUCCESS
2316 * Success.
2317 * \retval #PSA_ERROR_INVALID_HANDLE
2318 * \retval #PSA_ERROR_EMPTY_SLOT
2319 * \retval #PSA_ERROR_NOT_PERMITTED
2320 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002321 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002322 * \retval #PSA_ERROR_NOT_SUPPORTED
2323 * \p alg is not supported or is not an AEAD algorithm.
2324 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2325 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2326 * \retval #PSA_ERROR_HARDWARE_FAILURE
2327 * \retval #PSA_ERROR_TAMPERING_DETECTED
2328 * \retval #PSA_ERROR_BAD_STATE
2329 * The library has not been previously initialized by psa_crypto_init().
2330 * It is implementation-dependent whether a failure to initialize
2331 * results in this error code.
2332 */
2333psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2334 psa_key_handle_t handle,
2335 psa_algorithm_t alg);
2336
2337/** Generate a random nonce for an authenticated encryption operation.
2338 *
2339 * This function generates a random nonce for the authenticated encryption
2340 * operation with an appropriate size for the chosen algorithm, key type
2341 * and key size.
2342 *
2343 * The application must call psa_aead_encrypt_setup() before
2344 * calling this function.
2345 *
2346 * If this function returns an error status, the operation becomes inactive.
2347 *
2348 * \param[in,out] operation Active AEAD operation.
2349 * \param[out] nonce Buffer where the generated nonce is to be
2350 * written.
2351 * \param nonce_size Size of the \p nonce buffer in bytes.
2352 * \param[out] nonce_length On success, the number of bytes of the
2353 * generated nonce.
2354 *
2355 * \retval #PSA_SUCCESS
2356 * Success.
2357 * \retval #PSA_ERROR_BAD_STATE
2358 * The operation state is not valid (not set up, or nonce already set).
2359 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2360 * The size of the \p nonce buffer is too small.
2361 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2362 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2363 * \retval #PSA_ERROR_HARDWARE_FAILURE
2364 * \retval #PSA_ERROR_TAMPERING_DETECTED
2365 */
2366psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2367 unsigned char *nonce,
2368 size_t nonce_size,
2369 size_t *nonce_length);
2370
2371/** Set the nonce for an authenticated encryption or decryption operation.
2372 *
2373 * This function sets the nonce for the authenticated
2374 * encryption or decryption operation.
2375 *
2376 * The application must call psa_aead_encrypt_setup() before
2377 * calling this function.
2378 *
2379 * If this function returns an error status, the operation becomes inactive.
2380 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002381 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002382 * instead of this function, unless implementing a protocol that requires
2383 * a non-random IV.
2384 *
2385 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002386 * \param[in] nonce Buffer containing the nonce to use.
2387 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002388 *
2389 * \retval #PSA_SUCCESS
2390 * Success.
2391 * \retval #PSA_ERROR_BAD_STATE
2392 * The operation state is not valid (not set up, or nonce already set).
2393 * \retval #PSA_ERROR_INVALID_ARGUMENT
2394 * The size of \p nonce is not acceptable for the chosen algorithm.
2395 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2396 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2397 * \retval #PSA_ERROR_HARDWARE_FAILURE
2398 * \retval #PSA_ERROR_TAMPERING_DETECTED
2399 */
2400psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2401 const unsigned char *nonce,
2402 size_t nonce_length);
2403
Gilles Peskinebc59c852019-01-17 15:26:08 +01002404/** Declare the lengths of the message and additional data for AEAD.
2405 *
2406 * The application must call this function before calling
2407 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2408 * the operation requires it. If the algorithm does not require it,
2409 * calling this function is optional, but if this function is called
2410 * then the implementation must enforce the lengths.
2411 *
2412 * You may call this function before or after setting the nonce with
2413 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2414 *
2415 * - For #PSA_ALG_CCM, calling this function is required.
2416 * - For the other AEAD algorithms defined in this specification, calling
2417 * this function is not required.
2418 * - For vendor-defined algorithm, refer to the vendor documentation.
2419 *
2420 * \param[in,out] operation Active AEAD operation.
2421 * \param ad_length Size of the non-encrypted additional
2422 * authenticated data in bytes.
2423 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2424 *
2425 * \retval #PSA_SUCCESS
2426 * Success.
2427 * \retval #PSA_ERROR_BAD_STATE
2428 * The operation state is not valid (not set up, already completed,
2429 * or psa_aead_update_ad() or psa_aead_update() already called).
2430 * \retval #PSA_ERROR_INVALID_ARGUMENT
2431 * At least one of the lengths is not acceptable for the chosen
2432 * algorithm.
2433 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2434 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2435 * \retval #PSA_ERROR_HARDWARE_FAILURE
2436 * \retval #PSA_ERROR_TAMPERING_DETECTED
2437 */
2438psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2439 size_t ad_length,
2440 size_t plaintext_length);
2441
Gilles Peskine30a9e412019-01-14 18:36:12 +01002442/** Pass additional data to an active AEAD operation.
2443 *
2444 * Additional data is authenticated, but not encrypted.
2445 *
2446 * You may call this function multiple times to pass successive fragments
2447 * of the additional data. You may not call this function after passing
2448 * data to encrypt or decrypt with psa_aead_update().
2449 *
2450 * Before calling this function, you must:
2451 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2452 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2453 *
2454 * If this function returns an error status, the operation becomes inactive.
2455 *
2456 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2457 * there is no guarantee that the input is valid. Therefore, until
2458 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2459 * treat the input as untrusted and prepare to undo any action that
2460 * depends on the input if psa_aead_verify() returns an error status.
2461 *
2462 * \param[in,out] operation Active AEAD operation.
2463 * \param[in] input Buffer containing the fragment of
2464 * additional data.
2465 * \param input_length Size of the \p input buffer in bytes.
2466 *
2467 * \retval #PSA_SUCCESS
2468 * Success.
2469 * \retval #PSA_ERROR_BAD_STATE
2470 * The operation state is not valid (not set up, nonce not set,
2471 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002472 * \retval #PSA_ERROR_INVALID_ARGUMENT
2473 * The total input length overflows the additional data length that
2474 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002475 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2476 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2477 * \retval #PSA_ERROR_HARDWARE_FAILURE
2478 * \retval #PSA_ERROR_TAMPERING_DETECTED
2479 */
2480psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2481 const uint8_t *input,
2482 size_t input_length);
2483
2484/** Encrypt or decrypt a message fragment in an active AEAD operation.
2485 *
2486 * Before calling this function, you must:
2487 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2488 * The choice of setup function determines whether this function
2489 * encrypts or decrypts its input.
2490 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2491 * 3. Call psa_aead_update_ad() to pass all the additional data.
2492 *
2493 * If this function returns an error status, the operation becomes inactive.
2494 *
2495 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2496 * there is no guarantee that the input is valid. Therefore, until
2497 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2498 * - Do not use the output in any way other than storing it in a
2499 * confidential location. If you take any action that depends
2500 * on the tentative decrypted data, this action will need to be
2501 * undone if the input turns out not to be valid. Furthermore,
2502 * if an adversary can observe that this action took place
2503 * (for example through timing), they may be able to use this
2504 * fact as an oracle to decrypt any message encrypted with the
2505 * same key.
2506 * - In particular, do not copy the output anywhere but to a
2507 * memory or storage space that you have exclusive access to.
2508 *
2509 * \param[in,out] operation Active AEAD operation.
2510 * \param[in] input Buffer containing the message fragment to
2511 * encrypt or decrypt.
2512 * \param input_length Size of the \p input buffer in bytes.
2513 * \param[out] output Buffer where the output is to be written.
2514 * \param output_size Size of the \p output buffer in bytes.
2515 * \param[out] output_length On success, the number of bytes
2516 * that make up the returned output.
2517 *
2518 * \retval #PSA_SUCCESS
2519 * Success.
2520 * \retval #PSA_ERROR_BAD_STATE
2521 * The operation state is not valid (not set up, nonce not set
2522 * or already completed).
2523 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2524 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002525 * \retval #PSA_ERROR_INVALID_ARGUMENT
2526 * The total length of input to psa_aead_update_ad() so far is
2527 * less than the additional data length that was previously
2528 * specified with psa_aead_set_lengths().
2529 * \retval #PSA_ERROR_INVALID_ARGUMENT
2530 * The total input length overflows the plaintext length that
2531 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002532 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2533 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2534 * \retval #PSA_ERROR_HARDWARE_FAILURE
2535 * \retval #PSA_ERROR_TAMPERING_DETECTED
2536 */
2537psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2538 const uint8_t *input,
2539 size_t input_length,
2540 unsigned char *output,
2541 size_t output_size,
2542 size_t *output_length);
2543
2544/** Finish encrypting a message in an AEAD operation.
2545 *
2546 * The operation must have been set up with psa_aead_encrypt_setup().
2547 *
2548 * This function finishes the authentication of the additional data
2549 * formed by concatenating the inputs passed to preceding calls to
2550 * psa_aead_update_ad() with the plaintext formed by concatenating the
2551 * inputs passed to preceding calls to psa_aead_update().
2552 *
2553 * This function has two output buffers:
2554 * - \p ciphertext contains trailing ciphertext that was buffered from
2555 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2556 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2557 * will not contain any output and can be a 0-sized buffer.
2558 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002559 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002560 * that the operation performs.
2561 *
2562 * When this function returns, the operation becomes inactive.
2563 *
2564 * \param[in,out] operation Active AEAD operation.
2565 * \param[out] ciphertext Buffer where the last part of the ciphertext
2566 * is to be written.
2567 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2568 * \param[out] ciphertext_length On success, the number of bytes of
2569 * returned ciphertext.
2570 * \param[out] tag Buffer where the authentication tag is
2571 * to be written.
2572 * \param tag_size Size of the \p tag buffer in bytes.
2573 * \param[out] tag_length On success, the number of bytes
2574 * that make up the returned tag.
2575 *
2576 * \retval #PSA_SUCCESS
2577 * Success.
2578 * \retval #PSA_ERROR_BAD_STATE
2579 * The operation state is not valid (not set up, nonce not set,
2580 * decryption, or already completed).
2581 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002582 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002583 * \retval #PSA_ERROR_INVALID_ARGUMENT
2584 * The total length of input to psa_aead_update_ad() so far is
2585 * less than the additional data length that was previously
2586 * specified with psa_aead_set_lengths().
2587 * \retval #PSA_ERROR_INVALID_ARGUMENT
2588 * The total length of input to psa_aead_update() so far is
2589 * less than the plaintext length that was previously
2590 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002591 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2592 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2593 * \retval #PSA_ERROR_HARDWARE_FAILURE
2594 * \retval #PSA_ERROR_TAMPERING_DETECTED
2595 */
2596psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002597 uint8_t *ciphertext,
2598 size_t ciphertext_size,
2599 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002600 uint8_t *tag,
2601 size_t tag_size,
2602 size_t *tag_length);
2603
2604/** Finish authenticating and decrypting a message in an AEAD operation.
2605 *
2606 * The operation must have been set up with psa_aead_decrypt_setup().
2607 *
2608 * This function finishes the authentication of the additional data
2609 * formed by concatenating the inputs passed to preceding calls to
2610 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2611 * inputs passed to preceding calls to psa_aead_update().
2612 *
2613 * When this function returns, the operation becomes inactive.
2614 *
2615 * \param[in,out] operation Active AEAD operation.
2616 * \param[in] tag Buffer containing the authentication tag.
2617 * \param tag_length Size of the \p tag buffer in bytes.
2618 *
2619 * \retval #PSA_SUCCESS
2620 * Success.
2621 * \retval #PSA_ERROR_BAD_STATE
2622 * The operation state is not valid (not set up, nonce not set,
2623 * encryption, or already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002624 * \retval #PSA_ERROR_INVALID_ARGUMENT
2625 * The total length of input to psa_aead_update_ad() so far is
2626 * less than the additional data length that was previously
2627 * specified with psa_aead_set_lengths().
2628 * \retval #PSA_ERROR_INVALID_ARGUMENT
2629 * The total length of input to psa_aead_update() so far is
2630 * less than the plaintext length that was previously
2631 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002632 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2633 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2634 * \retval #PSA_ERROR_HARDWARE_FAILURE
2635 * \retval #PSA_ERROR_TAMPERING_DETECTED
2636 */
2637psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2638 const uint8_t *tag,
2639 size_t tag_length);
2640
2641/** Abort an AEAD operation.
2642 *
2643 * Aborting an operation frees all associated resources except for the
2644 * \p operation structure itself. Once aborted, the operation object
2645 * can be reused for another operation by calling
2646 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2647 *
2648 * You may call this function any time after the operation object has
2649 * been initialized by any of the following methods:
2650 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2651 * whether it succeeds or not.
2652 * - Initializing the \c struct to all-bits-zero.
2653 * - Initializing the \c struct to logical zeros, e.g.
2654 * `psa_aead_operation_t operation = {0}`.
2655 *
2656 * In particular, calling psa_aead_abort() after the operation has been
2657 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2658 * is safe and has no effect.
2659 *
2660 * \param[in,out] operation Initialized AEAD operation.
2661 *
2662 * \retval #PSA_SUCCESS
2663 * \retval #PSA_ERROR_BAD_STATE
2664 * \p operation is not an active AEAD operation.
2665 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2666 * \retval #PSA_ERROR_HARDWARE_FAILURE
2667 * \retval #PSA_ERROR_TAMPERING_DETECTED
2668 */
2669psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2670
Gilles Peskine3b555712018-03-03 21:27:57 +01002671/**@}*/
2672
Gilles Peskine20035e32018-02-03 22:44:14 +01002673/** \defgroup asymmetric Asymmetric cryptography
2674 * @{
2675 */
2676
2677/**
2678 * \brief Sign a hash or short message with a private key.
2679 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002680 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002681 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002682 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2683 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2684 * to determine the hash algorithm to use.
2685 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002686 * \param handle Handle to the key to use for the operation.
2687 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002688 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002689 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002690 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002691 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002692 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002693 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002694 * \param[out] signature_length On success, the number of bytes
2695 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002696 *
Gilles Peskine28538492018-07-11 17:34:00 +02002697 * \retval #PSA_SUCCESS
2698 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002699 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002700 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002701 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002702 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002703 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002704 * \retval #PSA_ERROR_NOT_SUPPORTED
2705 * \retval #PSA_ERROR_INVALID_ARGUMENT
2706 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2707 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2708 * \retval #PSA_ERROR_HARDWARE_FAILURE
2709 * \retval #PSA_ERROR_TAMPERING_DETECTED
2710 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002711 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002712 * The library has not been previously initialized by psa_crypto_init().
2713 * It is implementation-dependent whether a failure to initialize
2714 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002715 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002716psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002717 psa_algorithm_t alg,
2718 const uint8_t *hash,
2719 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002720 uint8_t *signature,
2721 size_t signature_size,
2722 size_t *signature_length);
2723
2724/**
2725 * \brief Verify the signature a hash or short message using a public key.
2726 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002727 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002728 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002729 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2730 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2731 * to determine the hash algorithm to use.
2732 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002733 * \param handle Handle to the key to use for the operation.
2734 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002735 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002736 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002737 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002738 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002739 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002740 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002741 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002742 *
Gilles Peskine28538492018-07-11 17:34:00 +02002743 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002744 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002745 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002746 * The calculation was perfomed successfully, but the passed
2747 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002748 * \retval #PSA_ERROR_NOT_SUPPORTED
2749 * \retval #PSA_ERROR_INVALID_ARGUMENT
2750 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2751 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2752 * \retval #PSA_ERROR_HARDWARE_FAILURE
2753 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002754 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002755 * The library has not been previously initialized by psa_crypto_init().
2756 * It is implementation-dependent whether a failure to initialize
2757 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002758 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002759psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002760 psa_algorithm_t alg,
2761 const uint8_t *hash,
2762 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002763 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002764 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002765
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002766/**
2767 * \brief Encrypt a short message with a public key.
2768 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002769 * \param handle Handle to the key to use for the operation.
2770 * It must be a public key or an asymmetric
2771 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002772 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002773 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002774 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002775 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002776 * \param[in] salt A salt or label, if supported by the
2777 * encryption algorithm.
2778 * If the algorithm does not support a
2779 * salt, pass \c NULL.
2780 * If the algorithm supports an optional
2781 * salt and you do not want to pass a salt,
2782 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002783 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002784 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2785 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002786 * \param salt_length Size of the \p salt buffer in bytes.
2787 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002788 * \param[out] output Buffer where the encrypted message is to
2789 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002790 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002791 * \param[out] output_length On success, the number of bytes
2792 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002793 *
Gilles Peskine28538492018-07-11 17:34:00 +02002794 * \retval #PSA_SUCCESS
2795 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002796 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002797 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002798 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002799 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002800 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002801 * \retval #PSA_ERROR_NOT_SUPPORTED
2802 * \retval #PSA_ERROR_INVALID_ARGUMENT
2803 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2804 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2805 * \retval #PSA_ERROR_HARDWARE_FAILURE
2806 * \retval #PSA_ERROR_TAMPERING_DETECTED
2807 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002808 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002809 * The library has not been previously initialized by psa_crypto_init().
2810 * It is implementation-dependent whether a failure to initialize
2811 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002812 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002813psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002814 psa_algorithm_t alg,
2815 const uint8_t *input,
2816 size_t input_length,
2817 const uint8_t *salt,
2818 size_t salt_length,
2819 uint8_t *output,
2820 size_t output_size,
2821 size_t *output_length);
2822
2823/**
2824 * \brief Decrypt a short message with a private key.
2825 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002826 * \param handle Handle to the key to use for the operation.
2827 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002828 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002829 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002830 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002831 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002832 * \param[in] salt A salt or label, if supported by the
2833 * encryption algorithm.
2834 * If the algorithm does not support a
2835 * salt, pass \c NULL.
2836 * If the algorithm supports an optional
2837 * salt and you do not want to pass a salt,
2838 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002839 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002840 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2841 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002842 * \param salt_length Size of the \p salt buffer in bytes.
2843 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002844 * \param[out] output Buffer where the decrypted message is to
2845 * be written.
2846 * \param output_size Size of the \c output buffer in bytes.
2847 * \param[out] output_length On success, the number of bytes
2848 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002849 *
Gilles Peskine28538492018-07-11 17:34:00 +02002850 * \retval #PSA_SUCCESS
2851 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002852 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002853 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002854 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002855 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002856 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002857 * \retval #PSA_ERROR_NOT_SUPPORTED
2858 * \retval #PSA_ERROR_INVALID_ARGUMENT
2859 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2860 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2861 * \retval #PSA_ERROR_HARDWARE_FAILURE
2862 * \retval #PSA_ERROR_TAMPERING_DETECTED
2863 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2864 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002865 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002866 * The library has not been previously initialized by psa_crypto_init().
2867 * It is implementation-dependent whether a failure to initialize
2868 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002869 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002870psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002871 psa_algorithm_t alg,
2872 const uint8_t *input,
2873 size_t input_length,
2874 const uint8_t *salt,
2875 size_t salt_length,
2876 uint8_t *output,
2877 size_t output_size,
2878 size_t *output_length);
2879
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002880/**@}*/
2881
Gilles Peskineedd76872018-07-20 17:42:05 +02002882/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002883 * @{
2884 */
2885
2886/** The type of the state data structure for generators.
2887 *
2888 * Before calling any function on a generator, the application must
2889 * initialize it by any of the following means:
2890 * - Set the structure to all-bits-zero, for example:
2891 * \code
2892 * psa_crypto_generator_t generator;
2893 * memset(&generator, 0, sizeof(generator));
2894 * \endcode
2895 * - Initialize the structure to logical zero values, for example:
2896 * \code
2897 * psa_crypto_generator_t generator = {0};
2898 * \endcode
2899 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2900 * for example:
2901 * \code
2902 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2903 * \endcode
2904 * - Assign the result of the function psa_crypto_generator_init()
2905 * to the structure, for example:
2906 * \code
2907 * psa_crypto_generator_t generator;
2908 * generator = psa_crypto_generator_init();
2909 * \endcode
2910 *
2911 * This is an implementation-defined \c struct. Applications should not
2912 * make any assumptions about the content of this structure except
2913 * as directed by the documentation of a specific implementation.
2914 */
2915typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2916
2917/** \def PSA_CRYPTO_GENERATOR_INIT
2918 *
2919 * This macro returns a suitable initializer for a generator object
2920 * of type #psa_crypto_generator_t.
2921 */
2922#ifdef __DOXYGEN_ONLY__
2923/* This is an example definition for documentation purposes.
2924 * Implementations should define a suitable value in `crypto_struct.h`.
2925 */
2926#define PSA_CRYPTO_GENERATOR_INIT {0}
2927#endif
2928
2929/** Return an initial value for a generator object.
2930 */
2931static psa_crypto_generator_t psa_crypto_generator_init(void);
2932
2933/** Retrieve the current capacity of a generator.
2934 *
2935 * The capacity of a generator is the maximum number of bytes that it can
2936 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2937 *
2938 * \param[in] generator The generator to query.
2939 * \param[out] capacity On success, the capacity of the generator.
2940 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002941 * \retval #PSA_SUCCESS
2942 * \retval #PSA_ERROR_BAD_STATE
2943 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002944 */
2945psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2946 size_t *capacity);
2947
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002948/** Set the maximum capacity of a generator.
2949 *
2950 * \param[in,out] generator The generator object to modify.
2951 * \param capacity The new capacity of the generator.
2952 * It must be less or equal to the generator's
2953 * current capacity.
2954 *
2955 * \retval #PSA_SUCCESS
2956 * \retval #PSA_ERROR_INVALID_ARGUMENT
2957 * \p capacity is larger than the generator's current capacity.
2958 * \retval #PSA_ERROR_BAD_STATE
2959 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2960 */
2961psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
2962 size_t capacity);
2963
Gilles Peskineeab56e42018-07-12 17:12:33 +02002964/** Read some data from a generator.
2965 *
2966 * This function reads and returns a sequence of bytes from a generator.
2967 * The data that is read is discarded from the generator. The generator's
2968 * capacity is decreased by the number of bytes read.
2969 *
2970 * \param[in,out] generator The generator object to read from.
2971 * \param[out] output Buffer where the generator output will be
2972 * written.
2973 * \param output_length Number of bytes to output.
2974 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002975 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02002976 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskineeab56e42018-07-12 17:12:33 +02002977 * There were fewer than \p output_length bytes
2978 * in the generator. Note that in this case, no
2979 * output is written to the output buffer.
2980 * The generator's capacity is set to 0, thus
2981 * subsequent calls to this function will not
2982 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002983 * \retval #PSA_ERROR_BAD_STATE
2984 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2985 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2986 * \retval #PSA_ERROR_HARDWARE_FAILURE
2987 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002988 */
2989psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2990 uint8_t *output,
2991 size_t output_length);
2992
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002993/** Generate a key deterministically from data read from a generator.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002994 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002995 * This function uses the output of a generator to derive a key.
2996 * How much output it consumes and how the key is derived depends on the
2997 * key type.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002998 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002999 * - For key types for which the key is an arbitrary sequence of bytes
3000 * of a given size,
3001 * this function is functionally equivalent to calling #psa_generator_read
3002 * and passing the resulting output to #psa_import_key.
3003 * However, this function has a security benefit:
3004 * if the implementation provides an isolation boundary then
3005 * the key material is not exposed outside the isolation boundary.
3006 * As a consequence, for these key types, this function always consumes
3007 * exactly (\p bits / 8) bytes from the generator.
3008 * The following key types defined in this specification follow this scheme:
3009 *
3010 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003011 * - #PSA_KEY_TYPE_ARC4;
3012 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003013 * - #PSA_KEY_TYPE_DERIVE;
3014 * - #PSA_KEY_TYPE_HMAC.
3015 *
3016 * - For ECC keys on a Montgomery elliptic curve
3017 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3018 * Montgomery curve), this function always draws a byte string whose
3019 * length is determined by the curve, and sets the mandatory bits
3020 * accordingly. That is:
3021 *
3022 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3023 * and process it as specified in RFC 7748 &sect;5.
3024 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3025 * and process it as specified in RFC 7748 &sect;5.
3026 *
3027 * - For key types for which the key is represented by a single sequence of
3028 * \p bits bits with constraints as to which bit sequences are acceptable,
3029 * this function draws a byte string of length (\p bits / 8) bytes rounded
3030 * up to the nearest whole number of bytes. If the resulting byte string
3031 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3032 * This process is repeated until an acceptable byte string is drawn.
3033 * The byte string drawn from the generator is interpreted as specified
3034 * for the output produced by psa_export_key().
3035 * The following key types defined in this specification follow this scheme:
3036 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003037 * - #PSA_KEY_TYPE_DES.
3038 * Force-set the parity bits, but discard forbidden weak keys.
3039 * For 2-key and 3-key triple-DES, the three keys are generated
3040 * successively (for example, for 3-key triple-DES,
3041 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3042 * discard the first 8 bytes, use the next 8 bytes as the first key,
3043 * and continue reading output from the generator to derive the other
3044 * two keys).
3045 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR),
3046 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
3047 * ECC keys on a Weierstrass elliptic curve
3048 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3049 * Weierstrass curve).
3050 * For these key types, interpret the byte string as integer
3051 * in big-endian order. Discard it if it is not in the range
3052 * [0, *N* - 2] where *N* is the boundary of the private key domain
3053 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003054 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003055 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003056 * This method allows compliance to NIST standards, specifically
3057 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003058 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3059 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3060 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3061 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003062 *
3063 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
3064 * the way in which the generator output is consumed is
3065 * implementation-defined.
3066 *
3067 * In all cases, the data that is read is discarded from the generator.
3068 * The generator's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003069 *
Gilles Peskine20628592019-04-19 19:29:50 +02003070 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003071 * \param[out] handle On success, a handle to the newly created key.
3072 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003073 * \param[in,out] generator The generator object to read from.
3074 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003075 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003076 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003077 * If the key is persistent, the key material and the key's metadata
3078 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003079 * \retval #PSA_ERROR_ALREADY_EXISTS
3080 * This is an attempt to create a persistent key, and there is
3081 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003082 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003083 * There was not enough data to create the desired key.
3084 * Note that in this case, no output is written to the output buffer.
3085 * The generator's capacity is set to 0, thus subsequent calls to
3086 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003087 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003088 * The key type or key size is not supported, either by the
3089 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003090 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003091 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3092 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3093 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3094 * \retval #PSA_ERROR_HARDWARE_FAILURE
3095 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003096 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003097 * The library has not been previously initialized by psa_crypto_init().
3098 * It is implementation-dependent whether a failure to initialize
3099 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003100 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003101psa_status_t psa_generate_derived_key(const psa_key_attributes_t *attributes,
Gilles Peskine87a5e562019-04-17 12:28:25 +02003102 psa_key_handle_t *handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003103 psa_crypto_generator_t *generator);
3104
3105/** Abort a generator.
3106 *
3107 * Once a generator has been aborted, its capacity is zero.
3108 * Aborting a generator frees all associated resources except for the
3109 * \c generator structure itself.
3110 *
3111 * This function may be called at any time as long as the generator
3112 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
3113 * psa_crypto_generator_init() or a zero value. In particular, it is valid
3114 * to call psa_generator_abort() twice, or to call psa_generator_abort()
3115 * on a generator that has not been set up.
3116 *
3117 * Once aborted, the generator object may be called.
3118 *
3119 * \param[in,out] generator The generator to abort.
3120 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003121 * \retval #PSA_SUCCESS
3122 * \retval #PSA_ERROR_BAD_STATE
3123 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3124 * \retval #PSA_ERROR_HARDWARE_FAILURE
3125 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003126 */
3127psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3128
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003129/** Use the maximum possible capacity for a generator.
3130 *
3131 * Use this value as the capacity argument when setting up a generator
3132 * to indicate that the generator should have the maximum possible capacity.
3133 * The value of the maximum possible capacity depends on the generator
3134 * algorithm.
3135 */
3136#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
3137
Gilles Peskineeab56e42018-07-12 17:12:33 +02003138/**@}*/
3139
Gilles Peskineea0fb492018-07-12 17:17:20 +02003140/** \defgroup derivation Key derivation
3141 * @{
3142 */
3143
3144/** Set up a key derivation operation.
3145 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003146 * A key derivation algorithm takes some inputs and uses them to create
3147 * a byte generator which can be used to produce keys and other
3148 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003149 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003150 * To use a generator for key derivation:
3151 * - Start with an initialized object of type #psa_crypto_generator_t.
3152 * - Call psa_key_derivation_setup() to select the algorithm.
3153 * - Provide the inputs for the key derivation by calling
3154 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3155 * as appropriate. Which inputs are needed, in what order, and whether
3156 * they may be keys and if so of what type depends on the algorithm.
3157 * - Optionally set the generator's maximum capacity with
3158 * psa_set_generator_capacity(). You may do this before, in the middle of
3159 * or after providing inputs. For some algorithms, this step is mandatory
3160 * because the output depends on the maximum capacity.
3161 * - Generate output with psa_generator_read() or
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003162 * psa_generate_derived_key(). Successive calls to these functions
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003163 * use successive output bytes from the generator.
3164 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02003165 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003166 * \param[in,out] generator The generator object to set up. It must
3167 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003168 * \param alg The key derivation algorithm to compute
3169 * (\c PSA_ALG_XXX value such that
3170 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02003171 *
3172 * \retval #PSA_SUCCESS
3173 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003174 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003175 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003176 * \retval #PSA_ERROR_NOT_SUPPORTED
3177 * \c alg is not supported or is not a key derivation algorithm.
3178 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3179 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3180 * \retval #PSA_ERROR_HARDWARE_FAILURE
3181 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003182 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003183 */
3184psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
3185 psa_algorithm_t alg);
3186
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003187/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003188 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003189 * Which inputs are required and in what order depends on the algorithm.
3190 * Refer to the documentation of each key derivation or key agreement
3191 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003192 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003193 * This function passes direct inputs. Some inputs must be passed as keys
3194 * using psa_key_derivation_input_key() instead of this function. Refer to
3195 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003196 *
3197 * \param[in,out] generator The generator object to use. It must
3198 * have been set up with
3199 * psa_key_derivation_setup() and must not
3200 * have produced any output yet.
3201 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003202 * \param[in] data Input data to use.
3203 * \param data_length Size of the \p data buffer in bytes.
3204 *
3205 * \retval #PSA_SUCCESS
3206 * Success.
3207 * \retval #PSA_ERROR_INVALID_ARGUMENT
3208 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003209 * \retval #PSA_ERROR_INVALID_ARGUMENT
3210 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003211 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3212 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3213 * \retval #PSA_ERROR_HARDWARE_FAILURE
3214 * \retval #PSA_ERROR_TAMPERING_DETECTED
3215 * \retval #PSA_ERROR_BAD_STATE
3216 * The value of \p step is not valid given the state of \p generator.
3217 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003218 * The library has not been previously initialized by psa_crypto_init().
3219 * It is implementation-dependent whether a failure to initialize
3220 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003221 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003222psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3223 psa_key_derivation_step_t step,
3224 const uint8_t *data,
3225 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003226
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003227/** Provide an input for key derivation in the form of a key.
3228 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003229 * Which inputs are required and in what order depends on the algorithm.
3230 * Refer to the documentation of each key derivation or key agreement
3231 * algorithm for information.
3232 *
3233 * This function passes key inputs. Some inputs must be passed as keys
3234 * of the appropriate type using this function, while others must be
3235 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3236 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003237 *
3238 * \param[in,out] generator The generator object to use. It must
3239 * have been set up with
3240 * psa_key_derivation_setup() and must not
3241 * have produced any output yet.
3242 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003243 * \param handle Handle to the key. It must have an
3244 * appropriate type for \p step and must
3245 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003246 *
3247 * \retval #PSA_SUCCESS
3248 * Success.
3249 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine31351842019-04-09 12:00:00 +02003250 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003251 * \retval #PSA_ERROR_NOT_PERMITTED
3252 * \retval #PSA_ERROR_INVALID_ARGUMENT
3253 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003254 * \retval #PSA_ERROR_INVALID_ARGUMENT
3255 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003256 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3257 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3258 * \retval #PSA_ERROR_HARDWARE_FAILURE
3259 * \retval #PSA_ERROR_TAMPERING_DETECTED
3260 * \retval #PSA_ERROR_BAD_STATE
3261 * The value of \p step is not valid given the state of \p generator.
3262 * \retval #PSA_ERROR_BAD_STATE
3263 * The library has not been previously initialized by psa_crypto_init().
3264 * It is implementation-dependent whether a failure to initialize
3265 * results in this error code.
3266 */
3267psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3268 psa_key_derivation_step_t step,
3269 psa_key_handle_t handle);
3270
Gilles Peskine969c5d62019-01-16 15:53:06 +01003271/** Perform a key agreement and use the shared secret as input to a key
3272 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003273 *
3274 * A key agreement algorithm takes two inputs: a private key \p private_key
3275 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003276 * The result of this function is passed as input to a key derivation.
3277 * The output of this key derivation can be extracted by reading from the
3278 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003279 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003280 * \param[in,out] generator The generator object to use. It must
3281 * have been set up with
3282 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003283 * key agreement and derivation algorithm
3284 * \c alg (\c PSA_ALG_XXX value such that
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003285 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3286 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
Gilles Peskine6843c292019-01-18 16:44:49 +01003287 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003288 * The generator must be ready for an
3289 * input of the type given by \p step.
3290 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003291 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003292 * \param[in] peer_key Public key of the peer. The peer key must be in the
3293 * same format that psa_import_key() accepts for the
3294 * public key type corresponding to the type of
3295 * private_key. That is, this function performs the
3296 * equivalent of
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003297 * #psa_import_key(`internal_public_key_handle`,
3298 * #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(`private_key_type`),
3299 * `peer_key`, `peer_key_length`) where
Jaeden Amero8afbff82019-01-14 16:56:20 +00003300 * `private_key_type` is the type of `private_key`.
3301 * For example, for EC keys, this means that peer_key
3302 * is interpreted as a point on the curve that the
3303 * private key is on. The standard formats for public
3304 * keys are documented in the documentation of
3305 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003306 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003307 *
3308 * \retval #PSA_SUCCESS
3309 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003310 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02003311 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine01d718c2018-09-18 12:01:02 +02003312 * \retval #PSA_ERROR_NOT_PERMITTED
3313 * \retval #PSA_ERROR_INVALID_ARGUMENT
3314 * \c private_key is not compatible with \c alg,
3315 * or \p peer_key is not valid for \c alg or not compatible with
3316 * \c private_key.
3317 * \retval #PSA_ERROR_NOT_SUPPORTED
3318 * \c alg is not supported or is not a key derivation algorithm.
3319 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3320 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3321 * \retval #PSA_ERROR_HARDWARE_FAILURE
3322 * \retval #PSA_ERROR_TAMPERING_DETECTED
3323 */
3324psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003325 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003326 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003327 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003328 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003329
Gilles Peskine769c7a62019-01-18 16:42:29 +01003330/** Perform a key agreement and use the shared secret as input to a key
3331 * derivation.
3332 *
3333 * A key agreement algorithm takes two inputs: a private key \p private_key
3334 * a public key \p peer_key.
3335 *
3336 * \warning The raw result of a key agreement algorithm such as finite-field
3337 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3338 * not be used directly as key material. It should instead be passed as
3339 * input to a key derivation algorithm. To chain a key agreement with
3340 * a key derivation, use psa_key_agreement() and other functions from
3341 * the key derivation and generator interface.
3342 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003343 * \param alg The key agreement algorithm to compute
3344 * (\c PSA_ALG_XXX value such that
3345 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3346 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003347 * \param private_key Handle to the private key to use.
3348 * \param[in] peer_key Public key of the peer. It must be
3349 * in the same format that psa_import_key()
3350 * accepts. The standard formats for public
3351 * keys are documented in the documentation
3352 * of psa_export_public_key().
3353 * \param peer_key_length Size of \p peer_key in bytes.
3354 * \param[out] output Buffer where the decrypted message is to
3355 * be written.
3356 * \param output_size Size of the \c output buffer in bytes.
3357 * \param[out] output_length On success, the number of bytes
3358 * that make up the returned output.
3359 *
3360 * \retval #PSA_SUCCESS
3361 * Success.
3362 * \retval #PSA_ERROR_INVALID_HANDLE
3363 * \retval #PSA_ERROR_EMPTY_SLOT
3364 * \retval #PSA_ERROR_NOT_PERMITTED
3365 * \retval #PSA_ERROR_INVALID_ARGUMENT
3366 * \p alg is not a key agreement algorithm
3367 * \retval #PSA_ERROR_INVALID_ARGUMENT
3368 * \p private_key is not compatible with \p alg,
3369 * or \p peer_key is not valid for \p alg or not compatible with
3370 * \p private_key.
3371 * \retval #PSA_ERROR_NOT_SUPPORTED
3372 * \p alg is not a supported key agreement algorithm.
3373 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3374 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3375 * \retval #PSA_ERROR_HARDWARE_FAILURE
3376 * \retval #PSA_ERROR_TAMPERING_DETECTED
3377 */
3378psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3379 psa_key_handle_t private_key,
3380 const uint8_t *peer_key,
3381 size_t peer_key_length,
3382 uint8_t *output,
3383 size_t output_size,
3384 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003385
3386/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003387
3388/** \defgroup random Random generation
3389 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003390 */
3391
3392/**
3393 * \brief Generate random bytes.
3394 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003395 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003396 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003397 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003398 *
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003399 * \note To generate a key, use psa_generate_random_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003400 *
3401 * \param[out] output Output buffer for the generated data.
3402 * \param output_size Number of bytes to generate and output.
3403 *
3404 * \retval #PSA_SUCCESS
3405 * \retval #PSA_ERROR_NOT_SUPPORTED
3406 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3407 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3408 * \retval #PSA_ERROR_HARDWARE_FAILURE
3409 * \retval #PSA_ERROR_TAMPERING_DETECTED
3410 * \retval #PSA_ERROR_BAD_STATE
3411 * The library has not been previously initialized by psa_crypto_init().
3412 * It is implementation-dependent whether a failure to initialize
3413 * results in this error code.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003414 */
3415psa_status_t psa_generate_random(uint8_t *output,
3416 size_t output_size);
3417
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003418/**
3419 * \brief Generate a key or key pair.
3420 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003421 * The key is generated randomly.
3422 * Its location, policy, type and size are taken from \p attributes.
3423 *
3424 * If the type requires additional domain parameters, these are taken
3425 * from \p attributes as well. The following types use domain parameters:
3426 * - When generating an RSA key (#PSA_KEY_TYPE_RSA_KEYPAIR),
3427 * the default public exponent is 65537. This value is used if
3428 * \p attributes was set with psa_set_key_type() or by passing an empty
3429 * byte string as domain parameters to psa_set_key_domain_parameters().
3430 * If psa_set_key_domain_parameters() was used to set a non-empty
3431 * domain parameter string in \p attributes, this string is read as
3432 * a big-endian integer which is used as the public exponent.
3433 * - When generating a DSA key (#PSA_KEY_TYPE_DSA_KEYPAIR) or a
3434 * Diffie-Hellman key (#PSA_KEY_TYPE_DH_KEYPAIR), the domain parameters
3435 * from \p attributes are interpreted as described for
3436 * psa_set_key_domain_parameters().
3437 *
Gilles Peskine20628592019-04-19 19:29:50 +02003438 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003439 * \param[out] handle On success, a handle to the newly created key.
3440 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003441 *
Gilles Peskine28538492018-07-11 17:34:00 +02003442 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003443 * Success.
3444 * If the key is persistent, the key material and the key's metadata
3445 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003446 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003447 * This is an attempt to create a persistent key, and there is
3448 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003449 * \retval #PSA_ERROR_NOT_SUPPORTED
3450 * \retval #PSA_ERROR_INVALID_ARGUMENT
3451 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3452 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3453 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3454 * \retval #PSA_ERROR_HARDWARE_FAILURE
3455 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003456 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003457 * The library has not been previously initialized by psa_crypto_init().
3458 * It is implementation-dependent whether a failure to initialize
3459 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003460 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003461psa_status_t psa_generate_random_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003462 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003463
3464/**@}*/
3465
Gilles Peskinee59236f2018-01-27 23:32:46 +01003466#ifdef __cplusplus
3467}
3468#endif
3469
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003470/* The file "crypto_sizes.h" contains definitions for size calculation
3471 * macros whose definitions are implementation-specific. */
3472#include "crypto_sizes.h"
3473
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003474/* The file "crypto_struct.h" contains definitions for
3475 * implementation-specific structs that are declared above. */
3476#include "crypto_struct.h"
3477
3478/* The file "crypto_extra.h" contains vendor-specific definitions. This
3479 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003480#include "crypto_extra.h"
3481
3482#endif /* PSA_CRYPTO_H */