blob: 22dea8feb3b309e4f237a1c09a5a0bb180ab53fc [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():
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200135 * - psa_set_key_id()
136 * - psa_set_key_lifetime()
Gilles Peskine9c640f92019-04-28 11:36:21 +0200137 * - psa_set_key_type()
138 * - psa_set_key_bits()
139 * - psa_set_key_usage_flags()
140 * - psa_set_key_algorithm()
141 *
Gilles Peskine20628592019-04-19 19:29:50 +0200142 * Before calling any function on a key attribute structure, the application
143 * must initialize it by any of the following means:
144 * - Set the structure to all-bits-zero, for example:
145 * \code
146 * psa_key_attributes_t attributes;
147 * memset(&attributes, 0, sizeof(attributes));
148 * \endcode
149 * - Initialize the structure to logical zero values, for example:
150 * \code
151 * psa_key_attributes_t attributes = {0};
152 * \endcode
153 * - Initialize the structure to the initializer #PSA_KEY_ATTRIBUTES_INIT,
154 * for example:
155 * \code
156 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
157 * \endcode
158 * - Assign the result of the function psa_key_attributes_init()
159 * to the structure, for example:
160 * \code
161 * psa_key_attributes_t attributes;
162 * attributes = psa_key_attributes_init();
163 * \endcode
164 *
165 * A freshly initialized attribute structure contains the following
166 * values:
167 *
168 * - lifetime: #PSA_KEY_LIFETIME_VOLATILE.
169 * - key identifier: unspecified.
170 * - type: \c 0, with no domain parameters.
171 * - key size: \c 0.
172 * - usage flags: \c 0.
173 * - algorithm: \c 0.
174 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200175 * A typical sequence to create a key is as follows:
176 * -# Create and initialize an attribute structure.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200177 * -# If the key is persistent, call psa_set_key_id().
178 * Also call psa_set_key_lifetime() to place the key in a non-default
179 * location.
Gilles Peskine9c640f92019-04-28 11:36:21 +0200180 * -# Set the key policy with psa_set_key_usage_flags() and
181 * psa_set_key_algorithm().
182 * -# Set the key type with psa_set_key_type(). If the key type requires
183 * domain parameters, call psa_set_key_domain_parameters() instead.
184 * Skip this step if copying an existing key with psa_copy_key().
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100185 * -# When generating a random key with psa_generate_random_key() or deriving a key
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200186 * with psa_key_derivation_output_key(), set the desired key size with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200187 * psa_set_key_bits().
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100188 * -# Call a key creation function: psa_import_key(), psa_generate_random_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200189 * psa_key_derivation_output_key() or psa_copy_key(). This function reads
Gilles Peskine1ea5e442019-05-02 20:31:10 +0200190 * the attribute structure, creates a key with these attributes, and
191 * outputs a handle to the newly created key.
192 * -# The attribute structure is now no longer necessary. If you called
Gilles Peskine9c640f92019-04-28 11:36:21 +0200193 * psa_set_key_domain_parameters() earlier, you must call
194 * psa_reset_key_attributes() to free any resources used by the
195 * domain parameters. Otherwise calling psa_reset_key_attributes()
196 * is optional.
Gilles Peskine20628592019-04-19 19:29:50 +0200197 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200198 * A typical sequence to query a key's attributes is as follows:
199 * -# Call psa_get_key_attributes().
200 * -# Call `psa_get_key_xxx` functions to retrieve the attribute(s) that
201 * you are interested in.
202 * -# Call psa_reset_key_attributes() to free any resources that may be
203 * used by the attribute structure.
204 *
205 * Once a key has been created, it is impossible to change its attributes.
Gilles Peskine87a5e562019-04-17 12:28:25 +0200206 */
207typedef struct psa_key_attributes_s psa_key_attributes_t;
208
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200209/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200210 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200211 * If the attribute structure currently declares the key as volatile (which
212 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200213 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200214 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200215 * This function does not access storage, it merely stores the given
216 * value in the structure.
217 * The persistent key will be written to storage when the attribute
218 * structure is passed to a key creation function such as
219 * psa_import_key(), psa_generate_random_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200220 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200221 *
Gilles Peskine20628592019-04-19 19:29:50 +0200222 * This function may be declared as `static` (i.e. without external
223 * linkage). This function may be provided as a function-like macro,
224 * but in this case it must evaluate each of its arguments exactly once.
225 *
226 * \param[out] attributes The attribute structure to write to.
227 * \param id The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200228 */
229static void psa_set_key_id(psa_key_attributes_t *attributes,
230 psa_key_id_t id);
231
232/** Set the location of a persistent key.
233 *
234 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200235 * with psa_set_key_id(). By default, a key that has a persistent identifier
236 * is stored in the default storage area identifier by
237 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
238 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200239 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200240 * This function does not access storage, it merely stores the given
241 * value in the structure.
242 * The persistent key will be written to storage when the attribute
243 * structure is passed to a key creation function such as
244 * psa_import_key(), psa_generate_random_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200245 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200246 *
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 each of its arguments exactly once.
250 *
251 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200252 * \param lifetime The lifetime for the key.
253 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200254 * key will be volatile, and the key identifier
255 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200256 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200257static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
258 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200259
Gilles Peskine20628592019-04-19 19:29:50 +0200260/** Retrieve the key identifier from key attributes.
261 *
262 * This function may be declared as `static` (i.e. without external
263 * linkage). This function may be provided as a function-like macro,
264 * but in this case it must evaluate its argument exactly once.
265 *
266 * \param[in] attributes The key attribute structure to query.
267 *
268 * \return The persistent identifier stored in the attribute structure.
269 * This value is unspecified if the attribute structure declares
270 * the key as volatile.
271 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200272static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
273
Gilles Peskine20628592019-04-19 19:29:50 +0200274/** Retrieve the lifetime from key attributes.
275 *
276 * This function may be declared as `static` (i.e. without external
277 * linkage). This function may be provided as a function-like macro,
278 * but in this case it must evaluate its argument exactly once.
279 *
280 * \param[in] attributes The key attribute structure to query.
281 *
282 * \return The lifetime value stored in the attribute structure.
283 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200284static psa_key_lifetime_t psa_get_key_lifetime(
285 const psa_key_attributes_t *attributes);
286
Gilles Peskine20628592019-04-19 19:29:50 +0200287/** Declare usage flags for a key.
288 *
289 * Usage flags are part of a key's usage policy. They encode what
290 * kind of operations are permitted on the key. For more details,
291 * refer to the documentation of the type #psa_key_usage_t.
292 *
293 * This function overwrites any usage flags
294 * previously set in \p attributes.
295 *
296 * This function may be declared as `static` (i.e. without external
297 * linkage). This function may be provided as a function-like macro,
298 * but in this case it must evaluate each of its arguments exactly once.
299 *
300 * \param[out] attributes The attribute structure to write to.
301 * \param usage_flags The usage flags to write.
302 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200303static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
304 psa_key_usage_t usage_flags);
305
Gilles Peskine20628592019-04-19 19:29:50 +0200306/** Retrieve the usage flags from key attributes.
307 *
308 * This function may be declared as `static` (i.e. without external
309 * linkage). This function may be provided as a function-like macro,
310 * but in this case it must evaluate its argument exactly once.
311 *
312 * \param[in] attributes The key attribute structure to query.
313 *
314 * \return The usage flags stored in the attribute structure.
315 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200316static psa_key_usage_t psa_get_key_usage_flags(
317 const psa_key_attributes_t *attributes);
318
Gilles Peskine20628592019-04-19 19:29:50 +0200319/** Declare the permitted algorithm policy for a key.
320 *
321 * The permitted algorithm policy of a key encodes which algorithm or
322 * algorithms are permitted to be used with this key.
323 *
324 * This function overwrites any algorithm policy
325 * previously set in \p attributes.
326 *
327 * This function may be declared as `static` (i.e. without external
328 * linkage). This function may be provided as a function-like macro,
329 * but in this case it must evaluate each of its arguments exactly once.
330 *
331 * \param[out] attributes The attribute structure to write to.
332 * \param alg The permitted algorithm policy to write.
333 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200334static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
335 psa_algorithm_t alg);
336
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100337
Gilles Peskine20628592019-04-19 19:29:50 +0200338/** Retrieve the algorithm policy from key attributes.
339 *
340 * This function may be declared as `static` (i.e. without external
341 * linkage). This function may be provided as a function-like macro,
342 * but in this case it must evaluate its argument exactly once.
343 *
344 * \param[in] attributes The key attribute structure to query.
345 *
346 * \return The algorithm stored in the attribute structure.
347 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200348static psa_algorithm_t psa_get_key_algorithm(
349 const psa_key_attributes_t *attributes);
350
Gilles Peskine20628592019-04-19 19:29:50 +0200351/** Declare the type of a key.
352 *
353 * If a type requires domain parameters, you must call
354 * psa_set_key_domain_parameters() instead of this function.
355 *
356 * This function overwrites any key type and domain parameters
357 * previously set in \p attributes.
358 *
359 * This function may be declared as `static` (i.e. without external
360 * linkage). This function may be provided as a function-like macro,
361 * but in this case it must evaluate each of its arguments exactly once.
362 *
363 * \param[out] attributes The attribute structure to write to.
364 * \param type The key type to write.
365 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200366static void psa_set_key_type(psa_key_attributes_t *attributes,
367 psa_key_type_t type);
368
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100369
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200370/** Declare the size of a key.
371 *
372 * This function overwrites any key size previously set in \p attributes.
373 *
374 * This function may be declared as `static` (i.e. without external
375 * linkage). This function may be provided as a function-like macro,
376 * but in this case it must evaluate each of its arguments exactly once.
377 *
378 * \param[out] attributes The attribute structure to write to.
379 * \param bits The key size in bits.
380 */
381static void psa_set_key_bits(psa_key_attributes_t *attributes,
382 size_t bits);
383
Gilles Peskine20628592019-04-19 19:29:50 +0200384/** Retrieve the key type from key attributes.
385 *
386 * This function may be declared as `static` (i.e. without external
387 * linkage). This function may be provided as a function-like macro,
388 * but in this case it must evaluate its argument exactly once.
389 *
390 * \param[in] attributes The key attribute structure to query.
391 *
392 * \return The key type stored in the attribute structure.
393 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200394static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
395
Gilles Peskine20628592019-04-19 19:29:50 +0200396/** Retrieve the key size from key attributes.
397 *
398 * This function may be declared as `static` (i.e. without external
399 * linkage). This function may be provided as a function-like macro,
400 * but in this case it must evaluate its argument exactly once.
401 *
402 * \param[in] attributes The key attribute structure to query.
403 *
404 * \return The key size stored in the attribute structure, in bits.
405 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200406static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
407
Gilles Peskineb699f072019-04-26 16:06:02 +0200408/**
409 * \brief Set domain parameters for a key.
410 *
411 * Some key types require additional domain parameters in addition to
412 * the key type identifier and the key size.
413 * The format for the required domain parameters varies by the key type.
414 *
Gilles Peskinee56e8782019-04-26 17:34:02 +0200415 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEYPAIR),
416 * the domain parameter data consists of the public exponent,
Gilles Peskineb699f072019-04-26 16:06:02 +0200417 * represented as a big-endian integer with no leading zeros.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200418 * This information is used when generating an RSA key pair.
Gilles Peskineb699f072019-04-26 16:06:02 +0200419 * When importing a key, the public exponent is read from the imported
420 * key data and the exponent recorded in the attribute structure is ignored.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200421 * As an exception, the public exponent 65537 is represented by an empty
422 * byte string.
423 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEYPAIR),
Gilles Peskineb699f072019-04-26 16:06:02 +0200424 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
425 * ```
426 * Dss-Parms ::= SEQUENCE {
427 * p INTEGER,
428 * q INTEGER,
429 * g INTEGER
430 * }
431 * ```
Gilles Peskinee56e8782019-04-26 17:34:02 +0200432 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY or
433 * #PSA_KEY_TYPE_DH_KEYPAIR), the
Gilles Peskineb699f072019-04-26 16:06:02 +0200434 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
435 * ```
436 * DomainParameters ::= SEQUENCE {
437 * p INTEGER, -- odd prime, p=jq +1
438 * g INTEGER, -- generator, g
439 * q INTEGER, -- factor of p-1
440 * j INTEGER OPTIONAL, -- subgroup factor
441 * validationParms ValidationParms OPTIONAL
442 * }
443 * ValidationParms ::= SEQUENCE {
444 * seed BIT STRING,
445 * pgenCounter INTEGER
446 * }
447 * ```
448 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200449 * \note This function may allocate memory or other resources.
450 * Once you have called this function on an attribute structure,
451 * you must call psa_reset_key_attributes() to free these resources.
452 *
Gilles Peskineb699f072019-04-26 16:06:02 +0200453 * \param[in,out] attributes Attribute structure where the specified domain
454 * parameters will be stored.
455 * If this function fails, the content of
456 * \p attributes is not modified.
457 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
458 * \param[in] data Buffer containing the key domain parameters.
459 * The content of this buffer is interpreted
460 * according to \p type as described above.
461 * \param data_length Size of the \p data buffer in bytes.
462 *
463 * \retval #PSA_SUCCESS
464 * \retval #PSA_ERROR_INVALID_ARGUMENT
465 * \retval #PSA_ERROR_NOT_SUPPORTED
466 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
467 */
468psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
469 psa_key_type_t type,
470 const uint8_t *data,
471 size_t data_length);
472
473/**
474 * \brief Get domain parameters for a key.
475 *
476 * Get the domain parameters for a key with this function, if any. The format
477 * of the domain parameters written to \p data is specified in the
478 * documentation for psa_set_key_domain_parameters().
479 *
480 * \param[in] attributes The key attribute structure to query.
481 * \param[out] data On success, the key domain parameters.
482 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineaa02c172019-04-28 11:44:17 +0200483 * The buffer is guaranteed to be large
484 * enough if its size in bytes is at least
485 * the value given by
486 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
Gilles Peskineb699f072019-04-26 16:06:02 +0200487 * \param[out] data_length On success, the number of bytes
488 * that make up the key domain parameters data.
489 *
490 * \retval #PSA_SUCCESS
491 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
492 */
493psa_status_t psa_get_key_domain_parameters(
494 const psa_key_attributes_t *attributes,
495 uint8_t *data,
496 size_t data_size,
497 size_t *data_length);
498
Gilles Peskine20628592019-04-19 19:29:50 +0200499/** Retrieve the attributes of a key.
500 *
501 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200502 * psa_reset_key_attributes(). It then copies the attributes of
503 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200504 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200505 * \note This function may allocate memory or other resources.
506 * Once you have called this function on an attribute structure,
507 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200508 *
Gilles Peskine20628592019-04-19 19:29:50 +0200509 * \param[in] handle Handle to the key to query.
510 * \param[in,out] attributes On success, the attributes of the key.
511 * On failure, equivalent to a
512 * freshly-initialized structure.
513 *
514 * \retval #PSA_SUCCESS
515 * \retval #PSA_ERROR_INVALID_HANDLE
516 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
517 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
518 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200519psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
520 psa_key_attributes_t *attributes);
521
Gilles Peskine20628592019-04-19 19:29:50 +0200522/** Reset a key attribute structure to a freshly initialized state.
523 *
524 * You must initialize the attribute structure as described in the
525 * documentation of the type #psa_key_attributes_t before calling this
526 * function. Once the structure has been initialized, you may call this
527 * function at any time.
528 *
529 * This function frees any auxiliary resources that the structure
530 * may contain.
531 *
532 * \param[in,out] attributes The attribute structure to reset.
533 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200534void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200535
Gilles Peskine87a5e562019-04-17 12:28:25 +0200536/**@}*/
537
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100538/** \defgroup key_management Key management
539 * @{
540 */
541
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100542/** Open a handle to an existing persistent key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100543 *
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100544 * Open a handle to a key which was previously created with
545 * psa_make_key_persistent() when setting its attributes.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100546 *
Gilles Peskine4a231b82019-05-06 18:56:14 +0200547 * Implementations may provide additional keys that can be opened with
548 * psa_open_key(). Such keys have a key identifier in the vendor range,
549 * as documented in the description of #psa_key_id_t.
550 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100551 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100552 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100553 *
554 * \retval #PSA_SUCCESS
555 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100556 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100557 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200558 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100559 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine225010f2019-05-06 18:44:55 +0200560 * \p id is invalid.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100561 * \retval #PSA_ERROR_NOT_PERMITTED
562 * The specified key exists, but the application does not have the
563 * permission to access it. Note that this specification does not
564 * define any way to create such a key, but it may be possible
565 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200566 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
567 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100568 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200569psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100570 psa_key_handle_t *handle);
571
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100572
Gilles Peskinef535eb22018-11-30 14:08:36 +0100573/** Close a key handle.
574 *
575 * If the handle designates a volatile key, destroy the key material and
576 * free all associated resources, just like psa_destroy_key().
577 *
578 * If the handle designates a persistent key, free all resources associated
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100579 * with the key in volatile memory. The key in persistent storage is
Gilles Peskinef535eb22018-11-30 14:08:36 +0100580 * not affected and can be opened again later with psa_open_key().
581 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100582 * If the key is currently in use in a multipart operation,
583 * the multipart operation is aborted.
584 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100585 * \param handle The key handle to close.
586 *
587 * \retval #PSA_SUCCESS
588 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100589 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100590 */
591psa_status_t psa_close_key(psa_key_handle_t handle);
592
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100593/**@}*/
594
595/** \defgroup import_export Key import and export
596 * @{
597 */
598
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100599/**
600 * \brief Import a key in binary format.
601 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100602 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100603 * documentation of psa_export_public_key() for the format of public keys
604 * and to the documentation of psa_export_key() for the format for
605 * other key types.
606 *
607 * This specification supports a single format for each key type.
608 * Implementations may support other formats as long as the standard
609 * format is supported. Implementations that support other formats
610 * should ensure that the formats are clearly unambiguous so as to
611 * minimize the risk that an invalid input is accidentally interpreted
612 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100613 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100614
Gilles Peskine20628592019-04-19 19:29:50 +0200615 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200616 * The key size is always determined from the
617 * \p data buffer.
618 * If the key size in \p attributes is nonzero,
619 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200620 * \param[out] handle On success, a handle to the newly created key.
621 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100622 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine20628592019-04-19 19:29:50 +0200623 * buffer is interpreted according to the type and,
624 * if applicable, domain parameters declared in
625 * \p attributes.
626 * All implementations must support at least the format
627 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100628 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200629 * the chosen type. Implementations may allow other
630 * formats, but should be conservative: implementations
631 * should err on the side of rejecting content if it
632 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200633 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100634 *
Gilles Peskine28538492018-07-11 17:34:00 +0200635 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100636 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100637 * If the key is persistent, the key material and the key's metadata
638 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200639 * \retval #PSA_ERROR_ALREADY_EXISTS
640 * This is an attempt to create a persistent key, and there is
641 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200642 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200643 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200644 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200645 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200646 * The key attributes, as a whole, are invalid.
647 * \retval #PSA_ERROR_INVALID_ARGUMENT
648 * The key data is not correctly formatted.
649 * \retval #PSA_ERROR_INVALID_ARGUMENT
650 * The size in \p attributes is nonzero and does not match the size
651 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200652 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
653 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
654 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100655 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200656 * \retval #PSA_ERROR_HARDWARE_FAILURE
657 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300658 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300659 * The library has not been previously initialized by psa_crypto_init().
660 * It is implementation-dependent whether a failure to initialize
661 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100662 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200663psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100664 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200665 size_t data_length,
666 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100667
668/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100669 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200670 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100671 * This function destroys a key from both volatile
Gilles Peskine154bd952018-04-19 08:38:16 +0200672 * memory and, if applicable, non-volatile storage. Implementations shall
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100673 * make a best effort to ensure that that the key material cannot be recovered.
Gilles Peskine154bd952018-04-19 08:38:16 +0200674 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100675 * This function also erases any metadata such as policies and frees all
676 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200677 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100678 * \param handle Handle to the key to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100679 *
Gilles Peskine28538492018-07-11 17:34:00 +0200680 * \retval #PSA_SUCCESS
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100681 * The key material has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200682 * \retval #PSA_ERROR_NOT_PERMITTED
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100683 * The handle holds content and cannot be erased because it is
Gilles Peskine65eb8582018-04-19 08:28:58 +0200684 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100685 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200686 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200687 * There was an failure in communication with the cryptoprocessor.
688 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200689 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200690 * The storage is corrupted. Implementations shall make a best effort
691 * to erase key material even in this stage, however applications
692 * should be aware that it may be impossible to guarantee that the
693 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200694 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200695 * An unexpected condition which is not a storage corruption or
696 * a communication failure occurred. The cryptoprocessor may have
697 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300698 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300699 * The library has not been previously initialized by psa_crypto_init().
700 * It is implementation-dependent whether a failure to initialize
701 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100702 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100703psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100704
705/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706 * \brief Export a key in binary format.
707 *
708 * The output of this function can be passed to psa_import_key() to
709 * create an equivalent object.
710 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100711 * If the implementation of psa_import_key() supports other formats
712 * beyond the format specified here, the output from psa_export_key()
713 * must use the representation specified here, not the original
714 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100715 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100716 * For standard key types, the output format is as follows:
717 *
718 * - For symmetric keys (including MAC keys), the format is the
719 * raw bytes of the key.
720 * - For DES, the key data consists of 8 bytes. The parity bits must be
721 * correct.
722 * - For Triple-DES, the format is the concatenation of the
723 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100724 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200725 * is the non-encrypted DER encoding of the representation defined by
726 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
727 * ```
728 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200729 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200730 * modulus INTEGER, -- n
731 * publicExponent INTEGER, -- e
732 * privateExponent INTEGER, -- d
733 * prime1 INTEGER, -- p
734 * prime2 INTEGER, -- q
735 * exponent1 INTEGER, -- d mod (p-1)
736 * exponent2 INTEGER, -- d mod (q-1)
737 * coefficient INTEGER, -- (inverse of q) mod p
738 * }
739 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000740 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
741 * representation of the private key `x` as a big-endian byte string. The
742 * length of the byte string is the private key size in bytes (leading zeroes
743 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200744 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100745 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100746 * a representation of the private value as a `ceiling(m/8)`-byte string
747 * where `m` is the bit size associated with the curve, i.e. the bit size
748 * of the order of the curve's coordinate field. This byte string is
749 * in little-endian order for Montgomery curves (curve types
750 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
751 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
752 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100753 * This is the content of the `privateKey` field of the `ECPrivateKey`
754 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000755 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
756 * format is the representation of the private key `x` as a big-endian byte
757 * string. The length of the byte string is the private key size in bytes
758 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200759 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
760 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100761 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200762 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
763 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100764 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200765 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200766 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200767 * \param[out] data_length On success, the number of bytes
768 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100769 *
Gilles Peskine28538492018-07-11 17:34:00 +0200770 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100771 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200772 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200773 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200774 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100775 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200776 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
777 * The size of the \p data buffer is too small. You can determine a
778 * sufficient buffer size by calling
779 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
780 * where \c type is the key type
781 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200782 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
783 * \retval #PSA_ERROR_HARDWARE_FAILURE
784 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300785 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300786 * The library has not been previously initialized by psa_crypto_init().
787 * It is implementation-dependent whether a failure to initialize
788 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100789 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100790psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100791 uint8_t *data,
792 size_t data_size,
793 size_t *data_length);
794
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100795/**
796 * \brief Export a public key or the public part of a key pair in binary format.
797 *
798 * The output of this function can be passed to psa_import_key() to
799 * create an object that is equivalent to the public key.
800 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000801 * This specification supports a single format for each key type.
802 * Implementations may support other formats as long as the standard
803 * format is supported. Implementations that support other formats
804 * should ensure that the formats are clearly unambiguous so as to
805 * minimize the risk that an invalid input is accidentally interpreted
806 * according to a different format.
807 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000808 * For standard key types, the output format is as follows:
809 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
810 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
811 * ```
812 * RSAPublicKey ::= SEQUENCE {
813 * modulus INTEGER, -- n
814 * publicExponent INTEGER } -- e
815 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000816 * - For elliptic curve public keys (key types for which
817 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
818 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
819 * Let `m` be the bit size associated with the curve, i.e. the bit size of
820 * `q` for a curve over `F_q`. The representation consists of:
821 * - The byte 0x04;
822 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
823 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000824 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
825 * representation of the public key `y = g^x mod p` as a big-endian byte
826 * string. The length of the byte string is the length of the base prime `p`
827 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000828 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
829 * the format is the representation of the public key `y = g^x mod p` as a
830 * big-endian byte string. The length of the byte string is the length of the
831 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100832 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200833 * Exporting a public key object or the public part of a key pair is
834 * always permitted, regardless of the key's usage flags.
835 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100836 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200837 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200838 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200839 * \param[out] data_length On success, the number of bytes
840 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100841 *
Gilles Peskine28538492018-07-11 17:34:00 +0200842 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100843 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200844 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200845 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200846 * The key is neither a public key nor a key pair.
847 * \retval #PSA_ERROR_NOT_SUPPORTED
848 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
849 * The size of the \p data buffer is too small. You can determine a
850 * sufficient buffer size by calling
851 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
852 * where \c type is the key type
853 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200854 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
855 * \retval #PSA_ERROR_HARDWARE_FAILURE
856 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300857 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300858 * The library has not been previously initialized by psa_crypto_init().
859 * It is implementation-dependent whether a failure to initialize
860 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100861 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100862psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100863 uint8_t *data,
864 size_t data_size,
865 size_t *data_length);
866
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100867/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100868 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100869 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000870 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100871 * This function is primarily useful to copy a key from one location
872 * to another, since it populates a key using the material from
873 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200874 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100875 * In an implementation where handles have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100876 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100877 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100878 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200879 * The policy on the source key must have the usage flag
880 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200881 * This flag is sufficient to permit the copy if the key has the lifetime
882 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
883 * Some secure elements do not provide a way to copy a key without
884 * making it extractable from the secure element. If a key is located
885 * in such a secure element, then the key must have both usage flags
886 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
887 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200888 *
Gilles Peskine20628592019-04-19 19:29:50 +0200889 * The resulting key may only be used in a way that conforms to
890 * both the policy of the original key and the policy specified in
891 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100892 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200893 * usage flags on the source policy and the usage flags in \p attributes.
894 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100895 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200896 * - If either of the policies allows an algorithm and the other policy
897 * allows a wildcard-based algorithm policy that includes this algorithm,
898 * the resulting key allows the same algorithm.
899 * - If the policies do not allow any algorithm in common, this function
900 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200901 *
Gilles Peskine20628592019-04-19 19:29:50 +0200902 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100903 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200904 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100905 * \param source_handle The key to copy. It must be a valid key handle.
Gilles Peskine20628592019-04-19 19:29:50 +0200906 * \param[in] attributes The attributes for the new key.
907 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200908 * - The key type and size may be 0. If either is
909 * nonzero, it must match the corresponding
910 * attribute of the source key.
911 * - If \p attributes contains domain parameters,
912 * they must match the domain parameters of
913 * the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200914 * - The key location (the lifetime and, for
915 * persistent keys, the key identifier) is
916 * used directly.
917 * - The policy constraints (usage flags and
918 * algorithm policy) are combined from
919 * the source key and \p attributes so that
920 * both sets of restrictions apply, as
921 * described in the documentation of this function.
922 * \param[out] target_handle On success, a handle to the newly created key.
923 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200924 *
925 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100926 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200927 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200928 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200929 * This is an attempt to create a persistent key, and there is
930 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200931 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200932 * The lifetime or identifier in \p attributes are invalid.
933 * \retval #PSA_ERROR_INVALID_ARGUMENT
934 * The policy constraints on the source and specified in
935 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200936 * \retval #PSA_ERROR_INVALID_ARGUMENT
937 * \p attributes specifies a key type, domain parameters or key size
938 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100939 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200940 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
941 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100942 * The source key is not exportable and its lifetime does not
943 * allow copying it to the target's lifetime.
944 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
945 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200946 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
947 * \retval #PSA_ERROR_HARDWARE_FAILURE
948 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100949 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100950psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200951 const psa_key_attributes_t *attributes,
952 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100953
954/**@}*/
955
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100956/** \defgroup hash Message digests
957 * @{
958 */
959
Gilles Peskine69647a42019-01-14 20:18:12 +0100960/** Calculate the hash (digest) of a message.
961 *
962 * \note To verify the hash of a message against an
963 * expected value, use psa_hash_compare() instead.
964 *
965 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
966 * such that #PSA_ALG_IS_HASH(\p alg) is true).
967 * \param[in] input Buffer containing the message to hash.
968 * \param input_length Size of the \p input buffer in bytes.
969 * \param[out] hash Buffer where the hash is to be written.
970 * \param hash_size Size of the \p hash buffer in bytes.
971 * \param[out] hash_length On success, the number of bytes
972 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100973 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100974 *
975 * \retval #PSA_SUCCESS
976 * Success.
977 * \retval #PSA_ERROR_NOT_SUPPORTED
978 * \p alg is not supported or is not a hash algorithm.
979 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
980 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
981 * \retval #PSA_ERROR_HARDWARE_FAILURE
982 * \retval #PSA_ERROR_TAMPERING_DETECTED
983 */
984psa_status_t psa_hash_compute(psa_algorithm_t alg,
985 const uint8_t *input,
986 size_t input_length,
987 uint8_t *hash,
988 size_t hash_size,
989 size_t *hash_length);
990
991/** Calculate the hash (digest) of a message and compare it with a
992 * reference value.
993 *
994 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
995 * such that #PSA_ALG_IS_HASH(\p alg) is true).
996 * \param[in] input Buffer containing the message to hash.
997 * \param input_length Size of the \p input buffer in bytes.
998 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100999 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +01001000 *
1001 * \retval #PSA_SUCCESS
1002 * The expected hash is identical to the actual hash of the input.
1003 * \retval #PSA_ERROR_INVALID_SIGNATURE
1004 * The hash of the message was calculated successfully, but it
1005 * differs from the expected hash.
1006 * \retval #PSA_ERROR_NOT_SUPPORTED
1007 * \p alg is not supported or is not a hash algorithm.
1008 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1009 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1010 * \retval #PSA_ERROR_HARDWARE_FAILURE
1011 * \retval #PSA_ERROR_TAMPERING_DETECTED
1012 */
1013psa_status_t psa_hash_compare(psa_algorithm_t alg,
1014 const uint8_t *input,
1015 size_t input_length,
1016 const uint8_t *hash,
1017 const size_t hash_length);
1018
Gilles Peskine308b91d2018-02-08 09:47:44 +01001019/** The type of the state data structure for multipart hash operations.
1020 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001021 * Before calling any function on a hash operation object, the application must
1022 * initialize it by any of the following means:
1023 * - Set the structure to all-bits-zero, for example:
1024 * \code
1025 * psa_hash_operation_t operation;
1026 * memset(&operation, 0, sizeof(operation));
1027 * \endcode
1028 * - Initialize the structure to logical zero values, for example:
1029 * \code
1030 * psa_hash_operation_t operation = {0};
1031 * \endcode
1032 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
1033 * for example:
1034 * \code
1035 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
1036 * \endcode
1037 * - Assign the result of the function psa_hash_operation_init()
1038 * to the structure, for example:
1039 * \code
1040 * psa_hash_operation_t operation;
1041 * operation = psa_hash_operation_init();
1042 * \endcode
1043 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001044 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001045 * make any assumptions about the content of this structure except
1046 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001047typedef struct psa_hash_operation_s psa_hash_operation_t;
1048
Jaeden Amero6a25b412019-01-04 11:47:44 +00001049/** \def PSA_HASH_OPERATION_INIT
1050 *
1051 * This macro returns a suitable initializer for a hash operation object
1052 * of type #psa_hash_operation_t.
1053 */
1054#ifdef __DOXYGEN_ONLY__
1055/* This is an example definition for documentation purposes.
1056 * Implementations should define a suitable value in `crypto_struct.h`.
1057 */
1058#define PSA_HASH_OPERATION_INIT {0}
1059#endif
1060
1061/** Return an initial value for a hash operation object.
1062 */
1063static psa_hash_operation_t psa_hash_operation_init(void);
1064
Gilles Peskinef45adda2019-01-14 18:29:18 +01001065/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001066 *
1067 * The sequence of operations to calculate a hash (message digest)
1068 * is as follows:
1069 * -# Allocate an operation object which will be passed to all the functions
1070 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +00001071 * -# Initialize the operation object with one of the methods described in the
1072 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001073 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001074 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001075 * of the message each time. The hash that is calculated is the hash
1076 * of the concatenation of these messages in order.
1077 * -# To calculate the hash, call psa_hash_finish().
1078 * To compare the hash with an expected value, call psa_hash_verify().
1079 *
1080 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +00001081 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001082 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001083 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001084 * eventually terminate the operation. The following events terminate an
1085 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001086 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001087 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001088 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001089 * \param[in,out] operation The operation object to set up. It must have
1090 * been initialized as per the documentation for
1091 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001092 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1093 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001094 *
Gilles Peskine28538492018-07-11 17:34:00 +02001095 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001096 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001097 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001098 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001099 * \retval #PSA_ERROR_BAD_STATE
1100 * The operation state is not valid (already set up and not
1101 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001102 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1103 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1104 * \retval #PSA_ERROR_HARDWARE_FAILURE
1105 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001106 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001107psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001108 psa_algorithm_t alg);
1109
Gilles Peskine308b91d2018-02-08 09:47:44 +01001110/** Add a message fragment to a multipart hash operation.
1111 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001112 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001113 *
1114 * If this function returns an error status, the operation becomes inactive.
1115 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001116 * \param[in,out] operation Active hash operation.
1117 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001118 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001119 *
Gilles Peskine28538492018-07-11 17:34:00 +02001120 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001121 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001122 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001123 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001124 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1125 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1126 * \retval #PSA_ERROR_HARDWARE_FAILURE
1127 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001128 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001129psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1130 const uint8_t *input,
1131 size_t input_length);
1132
Gilles Peskine308b91d2018-02-08 09:47:44 +01001133/** Finish the calculation of the hash of a message.
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().
1138 *
1139 * When this function returns, the operation becomes inactive.
1140 *
1141 * \warning Applications should not call this function if they expect
1142 * a specific value for the hash. Call psa_hash_verify() instead.
1143 * Beware that comparing integrity or authenticity data such as
1144 * hash values with a function such as \c memcmp is risky
1145 * because the time taken by the comparison may leak information
1146 * about the hashed data which could allow an attacker to guess
1147 * a valid hash and thereby bypass security controls.
1148 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001149 * \param[in,out] operation Active hash operation.
1150 * \param[out] hash Buffer where the hash is to be written.
1151 * \param hash_size Size of the \p hash buffer in bytes.
1152 * \param[out] hash_length On success, the number of bytes
1153 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001154 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001155 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001156 *
Gilles Peskine28538492018-07-11 17:34:00 +02001157 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001158 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001159 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001160 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001161 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001162 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001163 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001164 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001165 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1166 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1167 * \retval #PSA_ERROR_HARDWARE_FAILURE
1168 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001169 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001170psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1171 uint8_t *hash,
1172 size_t hash_size,
1173 size_t *hash_length);
1174
Gilles Peskine308b91d2018-02-08 09:47:44 +01001175/** Finish the calculation of the hash of a message and compare it with
1176 * an expected value.
1177 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001178 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001179 * This function calculates the hash of the message formed by concatenating
1180 * the inputs passed to preceding calls to psa_hash_update(). It then
1181 * compares the calculated hash with the expected hash passed as a
1182 * parameter to this function.
1183 *
1184 * When this function returns, the operation becomes inactive.
1185 *
Gilles Peskine19067982018-03-20 17:54:53 +01001186 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001187 * comparison between the actual hash and the expected hash is performed
1188 * in constant time.
1189 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001190 * \param[in,out] operation Active hash operation.
1191 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001192 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001193 *
Gilles Peskine28538492018-07-11 17:34:00 +02001194 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001195 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001196 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001197 * The hash of the message was calculated successfully, but it
1198 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001199 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001200 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001201 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1202 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1203 * \retval #PSA_ERROR_HARDWARE_FAILURE
1204 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001205 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001206psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1207 const uint8_t *hash,
1208 size_t hash_length);
1209
Gilles Peskine308b91d2018-02-08 09:47:44 +01001210/** Abort a hash operation.
1211 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001212 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001213 * \p operation structure itself. Once aborted, the operation object
1214 * can be reused for another operation by calling
1215 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001216 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001217 * You may call this function any time after the operation object has
1218 * been initialized by any of the following methods:
1219 * - A call to psa_hash_setup(), whether it succeeds or not.
1220 * - Initializing the \c struct to all-bits-zero.
1221 * - Initializing the \c struct to logical zeros, e.g.
1222 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001223 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001224 * In particular, calling psa_hash_abort() after the operation has been
1225 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1226 * psa_hash_verify() is safe and has no effect.
1227 *
1228 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001229 *
Gilles Peskine28538492018-07-11 17:34:00 +02001230 * \retval #PSA_SUCCESS
1231 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001232 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001233 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1234 * \retval #PSA_ERROR_HARDWARE_FAILURE
1235 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001236 */
1237psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001238
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001239/** Clone a hash operation.
1240 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001241 * This function copies the state of an ongoing hash operation to
1242 * a new operation object. In other words, this function is equivalent
1243 * to calling psa_hash_setup() on \p target_operation with the same
1244 * algorithm that \p source_operation was set up for, then
1245 * psa_hash_update() on \p target_operation with the same input that
1246 * that was passed to \p source_operation. After this function returns, the
1247 * two objects are independent, i.e. subsequent calls involving one of
1248 * the objects do not affect the other object.
1249 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001250 * \param[in] source_operation The active hash operation to clone.
1251 * \param[in,out] target_operation The operation object to set up.
1252 * It must be initialized but not active.
1253 *
1254 * \retval #PSA_SUCCESS
1255 * \retval #PSA_ERROR_BAD_STATE
1256 * \p source_operation is not an active hash operation.
1257 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001258 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001259 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1260 * \retval #PSA_ERROR_HARDWARE_FAILURE
1261 * \retval #PSA_ERROR_TAMPERING_DETECTED
1262 */
1263psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1264 psa_hash_operation_t *target_operation);
1265
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001266/**@}*/
1267
Gilles Peskine8c9def32018-02-08 10:02:12 +01001268/** \defgroup MAC Message authentication codes
1269 * @{
1270 */
1271
Gilles Peskine69647a42019-01-14 20:18:12 +01001272/** Calculate the MAC (message authentication code) of a message.
1273 *
1274 * \note To verify the MAC of a message against an
1275 * expected value, use psa_mac_verify() instead.
1276 * Beware that comparing integrity or authenticity data such as
1277 * MAC values with a function such as \c memcmp is risky
1278 * because the time taken by the comparison may leak information
1279 * about the MAC value which could allow an attacker to guess
1280 * a valid MAC and thereby bypass security controls.
1281 *
1282 * \param handle Handle to the key to use for the operation.
1283 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001284 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001285 * \param[in] input Buffer containing the input message.
1286 * \param input_length Size of the \p input buffer in bytes.
1287 * \param[out] mac Buffer where the MAC value is to be written.
1288 * \param mac_size Size of the \p mac buffer in bytes.
1289 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001290 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001291 *
1292 * \retval #PSA_SUCCESS
1293 * Success.
1294 * \retval #PSA_ERROR_INVALID_HANDLE
1295 * \retval #PSA_ERROR_EMPTY_SLOT
1296 * \retval #PSA_ERROR_NOT_PERMITTED
1297 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001298 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001299 * \retval #PSA_ERROR_NOT_SUPPORTED
1300 * \p alg is not supported or is not a MAC algorithm.
1301 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1302 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1303 * \retval #PSA_ERROR_HARDWARE_FAILURE
1304 * \retval #PSA_ERROR_TAMPERING_DETECTED
1305 * \retval #PSA_ERROR_BAD_STATE
1306 * The library has not been previously initialized by psa_crypto_init().
1307 * It is implementation-dependent whether a failure to initialize
1308 * results in this error code.
1309 */
1310psa_status_t psa_mac_compute(psa_key_handle_t handle,
1311 psa_algorithm_t alg,
1312 const uint8_t *input,
1313 size_t input_length,
1314 uint8_t *mac,
1315 size_t mac_size,
1316 size_t *mac_length);
1317
1318/** Calculate the MAC of a message and compare it with a reference value.
1319 *
1320 * \param handle Handle to the key to use for the operation.
1321 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001322 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001323 * \param[in] input Buffer containing the input message.
1324 * \param input_length Size of the \p input buffer in bytes.
1325 * \param[out] mac Buffer containing the expected MAC value.
1326 * \param mac_length Size of the \p mac buffer in bytes.
1327 *
1328 * \retval #PSA_SUCCESS
1329 * The expected MAC is identical to the actual MAC of the input.
1330 * \retval #PSA_ERROR_INVALID_SIGNATURE
1331 * The MAC of the message was calculated successfully, but it
1332 * differs from the expected value.
1333 * \retval #PSA_ERROR_INVALID_HANDLE
1334 * \retval #PSA_ERROR_EMPTY_SLOT
1335 * \retval #PSA_ERROR_NOT_PERMITTED
1336 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001337 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001338 * \retval #PSA_ERROR_NOT_SUPPORTED
1339 * \p alg is not supported or is not a MAC algorithm.
1340 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1341 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1342 * \retval #PSA_ERROR_HARDWARE_FAILURE
1343 * \retval #PSA_ERROR_TAMPERING_DETECTED
1344 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001345psa_status_t psa_mac_verify(psa_key_handle_t handle,
1346 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001347 const uint8_t *input,
1348 size_t input_length,
1349 const uint8_t *mac,
1350 const size_t mac_length);
1351
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001352/** The type of the state data structure for multipart MAC operations.
1353 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001354 * Before calling any function on a MAC operation object, the application must
1355 * initialize it by any of the following means:
1356 * - Set the structure to all-bits-zero, for example:
1357 * \code
1358 * psa_mac_operation_t operation;
1359 * memset(&operation, 0, sizeof(operation));
1360 * \endcode
1361 * - Initialize the structure to logical zero values, for example:
1362 * \code
1363 * psa_mac_operation_t operation = {0};
1364 * \endcode
1365 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1366 * for example:
1367 * \code
1368 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1369 * \endcode
1370 * - Assign the result of the function psa_mac_operation_init()
1371 * to the structure, for example:
1372 * \code
1373 * psa_mac_operation_t operation;
1374 * operation = psa_mac_operation_init();
1375 * \endcode
1376 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001377 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001378 * make any assumptions about the content of this structure except
1379 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001380typedef struct psa_mac_operation_s psa_mac_operation_t;
1381
Jaeden Amero769ce272019-01-04 11:48:03 +00001382/** \def PSA_MAC_OPERATION_INIT
1383 *
1384 * This macro returns a suitable initializer for a MAC operation object of type
1385 * #psa_mac_operation_t.
1386 */
1387#ifdef __DOXYGEN_ONLY__
1388/* This is an example definition for documentation purposes.
1389 * Implementations should define a suitable value in `crypto_struct.h`.
1390 */
1391#define PSA_MAC_OPERATION_INIT {0}
1392#endif
1393
1394/** Return an initial value for a MAC operation object.
1395 */
1396static psa_mac_operation_t psa_mac_operation_init(void);
1397
Gilles Peskinef45adda2019-01-14 18:29:18 +01001398/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001399 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001400 * This function sets up the calculation of the MAC
1401 * (message authentication code) of a byte string.
1402 * To verify the MAC of a message against an
1403 * expected value, use psa_mac_verify_setup() instead.
1404 *
1405 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001406 * -# Allocate an operation object which will be passed to all the functions
1407 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001408 * -# Initialize the operation object with one of the methods described in the
1409 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001410 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001411 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1412 * of the message each time. The MAC that is calculated is the MAC
1413 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001414 * -# At the end of the message, call psa_mac_sign_finish() to finish
1415 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001416 *
1417 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001418 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001419 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001420 * After a successful call to psa_mac_sign_setup(), the application must
1421 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001422 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001423 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001424 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001425 * \param[in,out] operation The operation object to set up. It must have
1426 * been initialized as per the documentation for
1427 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001428 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001429 * It must remain valid until the operation
1430 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001431 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001432 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001433 *
Gilles Peskine28538492018-07-11 17:34:00 +02001434 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001435 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001436 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001437 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001438 * \retval #PSA_ERROR_NOT_PERMITTED
1439 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001440 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001441 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001442 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001443 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1444 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1445 * \retval #PSA_ERROR_HARDWARE_FAILURE
1446 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001447 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001448 * The operation state is not valid (already set up and not
1449 * subsequently completed).
1450 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001451 * The library has not been previously initialized by psa_crypto_init().
1452 * It is implementation-dependent whether a failure to initialize
1453 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001454 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001455psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001456 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001457 psa_algorithm_t alg);
1458
Gilles Peskinef45adda2019-01-14 18:29:18 +01001459/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001460 *
1461 * This function sets up the verification of the MAC
1462 * (message authentication code) of a byte string against an expected value.
1463 *
1464 * The sequence of operations to verify a MAC is as follows:
1465 * -# Allocate an operation object which will be passed to all the functions
1466 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001467 * -# Initialize the operation object with one of the methods described in the
1468 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001469 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001470 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1471 * of the message each time. The MAC that is calculated is the MAC
1472 * of the concatenation of these messages in order.
1473 * -# At the end of the message, call psa_mac_verify_finish() to finish
1474 * calculating the actual MAC of the message and verify it against
1475 * the expected value.
1476 *
1477 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001478 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001479 *
1480 * After a successful call to psa_mac_verify_setup(), the application must
1481 * eventually terminate the operation through one of the following methods:
1482 * - A failed call to psa_mac_update().
1483 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1484 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001485 * \param[in,out] operation The operation object to set up. It must have
1486 * been initialized as per the documentation for
1487 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001488 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001489 * It must remain valid until the operation
1490 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001491 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1492 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001493 *
Gilles Peskine28538492018-07-11 17:34:00 +02001494 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001495 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001496 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001497 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001498 * \retval #PSA_ERROR_NOT_PERMITTED
1499 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001500 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001501 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001502 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001503 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1504 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1505 * \retval #PSA_ERROR_HARDWARE_FAILURE
1506 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001507 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001508 * The operation state is not valid (already set up and not
1509 * subsequently completed).
1510 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001511 * The library has not been previously initialized by psa_crypto_init().
1512 * It is implementation-dependent whether a failure to initialize
1513 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001514 */
1515psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001516 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001517 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001518
Gilles Peskinedcd14942018-07-12 00:30:52 +02001519/** Add a message fragment to a multipart MAC operation.
1520 *
1521 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1522 * before calling this function.
1523 *
1524 * If this function returns an error status, the operation becomes inactive.
1525 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001526 * \param[in,out] operation Active MAC operation.
1527 * \param[in] input Buffer containing the message fragment to add to
1528 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001529 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001530 *
1531 * \retval #PSA_SUCCESS
1532 * Success.
1533 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001534 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001535 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1536 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1537 * \retval #PSA_ERROR_HARDWARE_FAILURE
1538 * \retval #PSA_ERROR_TAMPERING_DETECTED
1539 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001540psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1541 const uint8_t *input,
1542 size_t input_length);
1543
Gilles Peskinedcd14942018-07-12 00:30:52 +02001544/** Finish the calculation of the MAC of a message.
1545 *
1546 * The application must call psa_mac_sign_setup() before calling this function.
1547 * This function calculates the MAC of the message formed by concatenating
1548 * the inputs passed to preceding calls to psa_mac_update().
1549 *
1550 * When this function returns, the operation becomes inactive.
1551 *
1552 * \warning Applications should not call this function if they expect
1553 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1554 * Beware that comparing integrity or authenticity data such as
1555 * MAC values with a function such as \c memcmp is risky
1556 * because the time taken by the comparison may leak information
1557 * about the MAC value which could allow an attacker to guess
1558 * a valid MAC and thereby bypass security controls.
1559 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001560 * \param[in,out] operation Active MAC operation.
1561 * \param[out] mac Buffer where the MAC value is to be written.
1562 * \param mac_size Size of the \p mac buffer in bytes.
1563 * \param[out] mac_length On success, the number of bytes
1564 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001565 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001566 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001567 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001568 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001569 *
1570 * \retval #PSA_SUCCESS
1571 * Success.
1572 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001573 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001574 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001575 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001576 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1577 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1578 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1579 * \retval #PSA_ERROR_HARDWARE_FAILURE
1580 * \retval #PSA_ERROR_TAMPERING_DETECTED
1581 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001582psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1583 uint8_t *mac,
1584 size_t mac_size,
1585 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001586
Gilles Peskinedcd14942018-07-12 00:30:52 +02001587/** Finish the calculation of the MAC of a message and compare it with
1588 * an expected value.
1589 *
1590 * The application must call psa_mac_verify_setup() before calling this function.
1591 * This function calculates the MAC of the message formed by concatenating
1592 * the inputs passed to preceding calls to psa_mac_update(). It then
1593 * compares the calculated MAC with the expected MAC passed as a
1594 * parameter to this function.
1595 *
1596 * When this function returns, the operation becomes inactive.
1597 *
1598 * \note Implementations shall make the best effort to ensure that the
1599 * comparison between the actual MAC and the expected MAC is performed
1600 * in constant time.
1601 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001602 * \param[in,out] operation Active MAC operation.
1603 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001604 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001605 *
1606 * \retval #PSA_SUCCESS
1607 * The expected MAC is identical to the actual MAC of the message.
1608 * \retval #PSA_ERROR_INVALID_SIGNATURE
1609 * The MAC of the message was calculated successfully, but it
1610 * differs from the expected MAC.
1611 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001612 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001613 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1614 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1615 * \retval #PSA_ERROR_HARDWARE_FAILURE
1616 * \retval #PSA_ERROR_TAMPERING_DETECTED
1617 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001618psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1619 const uint8_t *mac,
1620 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001621
Gilles Peskinedcd14942018-07-12 00:30:52 +02001622/** Abort a MAC operation.
1623 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001624 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001625 * \p operation structure itself. Once aborted, the operation object
1626 * can be reused for another operation by calling
1627 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001628 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001629 * You may call this function any time after the operation object has
1630 * been initialized by any of the following methods:
1631 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1632 * it succeeds or not.
1633 * - Initializing the \c struct to all-bits-zero.
1634 * - Initializing the \c struct to logical zeros, e.g.
1635 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001636 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001637 * In particular, calling psa_mac_abort() after the operation has been
1638 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1639 * psa_mac_verify_finish() is safe and has no effect.
1640 *
1641 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001642 *
1643 * \retval #PSA_SUCCESS
1644 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001645 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001646 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1647 * \retval #PSA_ERROR_HARDWARE_FAILURE
1648 * \retval #PSA_ERROR_TAMPERING_DETECTED
1649 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001650psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1651
1652/**@}*/
1653
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001654/** \defgroup cipher Symmetric ciphers
1655 * @{
1656 */
1657
Gilles Peskine69647a42019-01-14 20:18:12 +01001658/** Encrypt a message using a symmetric cipher.
1659 *
1660 * This function encrypts a message with a random IV (initialization
1661 * vector).
1662 *
1663 * \param handle Handle to the key to use for the operation.
1664 * It must remain valid until the operation
1665 * terminates.
1666 * \param alg The cipher algorithm to compute
1667 * (\c PSA_ALG_XXX value such that
1668 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1669 * \param[in] input Buffer containing the message to encrypt.
1670 * \param input_length Size of the \p input buffer in bytes.
1671 * \param[out] output Buffer where the output is to be written.
1672 * The output contains the IV followed by
1673 * the ciphertext proper.
1674 * \param output_size Size of the \p output buffer in bytes.
1675 * \param[out] output_length On success, the number of bytes
1676 * that make up the output.
1677 *
1678 * \retval #PSA_SUCCESS
1679 * Success.
1680 * \retval #PSA_ERROR_INVALID_HANDLE
1681 * \retval #PSA_ERROR_EMPTY_SLOT
1682 * \retval #PSA_ERROR_NOT_PERMITTED
1683 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001684 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001685 * \retval #PSA_ERROR_NOT_SUPPORTED
1686 * \p alg is not supported or is not a cipher algorithm.
1687 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1688 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1689 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1690 * \retval #PSA_ERROR_HARDWARE_FAILURE
1691 * \retval #PSA_ERROR_TAMPERING_DETECTED
1692 */
1693psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1694 psa_algorithm_t alg,
1695 const uint8_t *input,
1696 size_t input_length,
1697 uint8_t *output,
1698 size_t output_size,
1699 size_t *output_length);
1700
1701/** Decrypt a message using a symmetric cipher.
1702 *
1703 * This function decrypts a message encrypted with a symmetric cipher.
1704 *
1705 * \param handle Handle to the key to use for the operation.
1706 * It must remain valid until the operation
1707 * terminates.
1708 * \param alg The cipher algorithm to compute
1709 * (\c PSA_ALG_XXX value such that
1710 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1711 * \param[in] input Buffer containing the message to decrypt.
1712 * This consists of the IV followed by the
1713 * ciphertext proper.
1714 * \param input_length Size of the \p input buffer in bytes.
1715 * \param[out] output Buffer where the plaintext is to be written.
1716 * \param output_size Size of the \p output buffer in bytes.
1717 * \param[out] output_length On success, the number of bytes
1718 * that make up the output.
1719 *
1720 * \retval #PSA_SUCCESS
1721 * Success.
1722 * \retval #PSA_ERROR_INVALID_HANDLE
1723 * \retval #PSA_ERROR_EMPTY_SLOT
1724 * \retval #PSA_ERROR_NOT_PERMITTED
1725 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001726 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001727 * \retval #PSA_ERROR_NOT_SUPPORTED
1728 * \p alg is not supported or is not a cipher algorithm.
1729 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1730 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1731 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1732 * \retval #PSA_ERROR_HARDWARE_FAILURE
1733 * \retval #PSA_ERROR_TAMPERING_DETECTED
1734 */
1735psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1736 psa_algorithm_t alg,
1737 const uint8_t *input,
1738 size_t input_length,
1739 uint8_t *output,
1740 size_t output_size,
1741 size_t *output_length);
1742
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001743/** The type of the state data structure for multipart cipher operations.
1744 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001745 * Before calling any function on a cipher operation object, the application
1746 * must initialize it by any of the following means:
1747 * - Set the structure to all-bits-zero, for example:
1748 * \code
1749 * psa_cipher_operation_t operation;
1750 * memset(&operation, 0, sizeof(operation));
1751 * \endcode
1752 * - Initialize the structure to logical zero values, for example:
1753 * \code
1754 * psa_cipher_operation_t operation = {0};
1755 * \endcode
1756 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1757 * for example:
1758 * \code
1759 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1760 * \endcode
1761 * - Assign the result of the function psa_cipher_operation_init()
1762 * to the structure, for example:
1763 * \code
1764 * psa_cipher_operation_t operation;
1765 * operation = psa_cipher_operation_init();
1766 * \endcode
1767 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001768 * This is an implementation-defined \c struct. Applications should not
1769 * make any assumptions about the content of this structure except
1770 * as directed by the documentation of a specific implementation. */
1771typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1772
Jaeden Amero5bae2272019-01-04 11:48:27 +00001773/** \def PSA_CIPHER_OPERATION_INIT
1774 *
1775 * This macro returns a suitable initializer for a cipher operation object of
1776 * type #psa_cipher_operation_t.
1777 */
1778#ifdef __DOXYGEN_ONLY__
1779/* This is an example definition for documentation purposes.
1780 * Implementations should define a suitable value in `crypto_struct.h`.
1781 */
1782#define PSA_CIPHER_OPERATION_INIT {0}
1783#endif
1784
1785/** Return an initial value for a cipher operation object.
1786 */
1787static psa_cipher_operation_t psa_cipher_operation_init(void);
1788
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001789/** Set the key for a multipart symmetric encryption operation.
1790 *
1791 * The sequence of operations to encrypt a message with a symmetric cipher
1792 * is as follows:
1793 * -# Allocate an operation object which will be passed to all the functions
1794 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001795 * -# Initialize the operation object with one of the methods described in the
1796 * documentation for #psa_cipher_operation_t, e.g.
1797 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001798 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001799 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001800 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001801 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001802 * requires a specific IV value.
1803 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1804 * of the message each time.
1805 * -# Call psa_cipher_finish().
1806 *
1807 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001808 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001809 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001810 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001811 * eventually terminate the operation. The following events terminate an
1812 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001813 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001814 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001815 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001816 * \param[in,out] operation The operation object to set up. It must have
1817 * been initialized as per the documentation for
1818 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001819 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001820 * It must remain valid until the operation
1821 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001822 * \param alg The cipher algorithm to compute
1823 * (\c PSA_ALG_XXX value such that
1824 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001825 *
Gilles Peskine28538492018-07-11 17:34:00 +02001826 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001827 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001828 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001829 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001830 * \retval #PSA_ERROR_NOT_PERMITTED
1831 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001832 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001833 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001834 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001835 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1836 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1837 * \retval #PSA_ERROR_HARDWARE_FAILURE
1838 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001839 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001840 * The operation state is not valid (already set up and not
1841 * subsequently completed).
1842 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001843 * The library has not been previously initialized by psa_crypto_init().
1844 * It is implementation-dependent whether a failure to initialize
1845 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001846 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001847psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001848 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001849 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001850
1851/** Set the key for a multipart symmetric decryption operation.
1852 *
1853 * The sequence of operations to decrypt a message with a symmetric cipher
1854 * is as follows:
1855 * -# Allocate an operation object which will be passed to all the functions
1856 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001857 * -# Initialize the operation object with one of the methods described in the
1858 * documentation for #psa_cipher_operation_t, e.g.
1859 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001860 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001861 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001862 * decryption. If the IV is prepended to the ciphertext, you can call
1863 * psa_cipher_update() on a buffer containing the IV followed by the
1864 * beginning of the message.
1865 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1866 * of the message each time.
1867 * -# Call psa_cipher_finish().
1868 *
1869 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001870 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001871 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001872 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001873 * eventually terminate the operation. The following events terminate an
1874 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001875 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001876 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001877 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001878 * \param[in,out] operation The operation object to set up. It must have
1879 * been initialized as per the documentation for
1880 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001881 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001882 * It must remain valid until the operation
1883 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001884 * \param alg The cipher algorithm to compute
1885 * (\c PSA_ALG_XXX value such that
1886 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001887 *
Gilles Peskine28538492018-07-11 17:34:00 +02001888 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001889 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001890 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001891 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001892 * \retval #PSA_ERROR_NOT_PERMITTED
1893 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001894 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001895 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001896 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001897 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1898 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1899 * \retval #PSA_ERROR_HARDWARE_FAILURE
1900 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001901 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001902 * The operation state is not valid (already set up and not
1903 * subsequently completed).
1904 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001905 * The library has not been previously initialized by psa_crypto_init().
1906 * It is implementation-dependent whether a failure to initialize
1907 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001908 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001909psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001910 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001911 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001912
Gilles Peskinedcd14942018-07-12 00:30:52 +02001913/** Generate an IV for a symmetric encryption operation.
1914 *
1915 * This function generates a random IV (initialization vector), nonce
1916 * or initial counter value for the encryption operation as appropriate
1917 * for the chosen algorithm, key type and key size.
1918 *
1919 * The application must call psa_cipher_encrypt_setup() before
1920 * calling this function.
1921 *
1922 * If this function returns an error status, the operation becomes inactive.
1923 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001924 * \param[in,out] operation Active cipher operation.
1925 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001926 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001927 * \param[out] iv_length On success, the number of bytes of the
1928 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001929 *
1930 * \retval #PSA_SUCCESS
1931 * Success.
1932 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001933 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001934 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001935 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001936 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1937 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1938 * \retval #PSA_ERROR_HARDWARE_FAILURE
1939 * \retval #PSA_ERROR_TAMPERING_DETECTED
1940 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001941psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1942 unsigned char *iv,
1943 size_t iv_size,
1944 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001945
Gilles Peskinedcd14942018-07-12 00:30:52 +02001946/** Set the IV for a symmetric encryption or decryption operation.
1947 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001948 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001949 * or initial counter value for the encryption or decryption operation.
1950 *
1951 * The application must call psa_cipher_encrypt_setup() before
1952 * calling this function.
1953 *
1954 * If this function returns an error status, the operation becomes inactive.
1955 *
1956 * \note When encrypting, applications should use psa_cipher_generate_iv()
1957 * instead of this function, unless implementing a protocol that requires
1958 * a non-random IV.
1959 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001960 * \param[in,out] operation Active cipher operation.
1961 * \param[in] iv Buffer containing the IV to use.
1962 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001963 *
1964 * \retval #PSA_SUCCESS
1965 * Success.
1966 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001967 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001968 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001969 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001970 * or the chosen algorithm does not use an IV.
1971 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1972 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1973 * \retval #PSA_ERROR_HARDWARE_FAILURE
1974 * \retval #PSA_ERROR_TAMPERING_DETECTED
1975 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001976psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1977 const unsigned char *iv,
1978 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001979
Gilles Peskinedcd14942018-07-12 00:30:52 +02001980/** Encrypt or decrypt a message fragment in an active cipher operation.
1981 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001982 * Before calling this function, you must:
1983 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1984 * The choice of setup function determines whether this function
1985 * encrypts or decrypts its input.
1986 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1987 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001988 *
1989 * If this function returns an error status, the operation becomes inactive.
1990 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001991 * \param[in,out] operation Active cipher operation.
1992 * \param[in] input Buffer containing the message fragment to
1993 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001994 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001995 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001996 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001997 * \param[out] output_length On success, the number of bytes
1998 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001999 *
2000 * \retval #PSA_SUCCESS
2001 * Success.
2002 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002003 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02002004 * not set, or already completed).
2005 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2006 * The size of the \p output buffer is too small.
2007 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2008 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2009 * \retval #PSA_ERROR_HARDWARE_FAILURE
2010 * \retval #PSA_ERROR_TAMPERING_DETECTED
2011 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002012psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2013 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002014 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002015 unsigned char *output,
2016 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002017 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002018
Gilles Peskinedcd14942018-07-12 00:30:52 +02002019/** Finish encrypting or decrypting a message in a cipher operation.
2020 *
2021 * The application must call psa_cipher_encrypt_setup() or
2022 * psa_cipher_decrypt_setup() before calling this function. The choice
2023 * of setup function determines whether this function encrypts or
2024 * decrypts its input.
2025 *
2026 * This function finishes the encryption or decryption of the message
2027 * formed by concatenating the inputs passed to preceding calls to
2028 * psa_cipher_update().
2029 *
2030 * When this function returns, the operation becomes inactive.
2031 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002032 * \param[in,out] operation Active cipher operation.
2033 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002034 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002035 * \param[out] output_length On success, the number of bytes
2036 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002037 *
2038 * \retval #PSA_SUCCESS
2039 * Success.
2040 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002041 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02002042 * not set, or already completed).
2043 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2044 * The size of the \p output buffer is too small.
2045 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2046 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2047 * \retval #PSA_ERROR_HARDWARE_FAILURE
2048 * \retval #PSA_ERROR_TAMPERING_DETECTED
2049 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002050psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002051 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002052 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002053 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002054
Gilles Peskinedcd14942018-07-12 00:30:52 +02002055/** Abort a cipher operation.
2056 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002057 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002058 * \p operation structure itself. Once aborted, the operation object
2059 * can be reused for another operation by calling
2060 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002061 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002062 * You may call this function any time after the operation object has
2063 * been initialized by any of the following methods:
2064 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2065 * whether it succeeds or not.
2066 * - Initializing the \c struct to all-bits-zero.
2067 * - Initializing the \c struct to logical zeros, e.g.
2068 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002069 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002070 * In particular, calling psa_cipher_abort() after the operation has been
2071 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2072 * is safe and has no effect.
2073 *
2074 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002075 *
2076 * \retval #PSA_SUCCESS
2077 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002078 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002079 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2080 * \retval #PSA_ERROR_HARDWARE_FAILURE
2081 * \retval #PSA_ERROR_TAMPERING_DETECTED
2082 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002083psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2084
2085/**@}*/
2086
Gilles Peskine3b555712018-03-03 21:27:57 +01002087/** \defgroup aead Authenticated encryption with associated data (AEAD)
2088 * @{
2089 */
2090
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002091/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002092 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002093 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002094 * \param alg The AEAD algorithm to compute
2095 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002096 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002097 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002098 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002099 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002100 * but not encrypted.
2101 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002102 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002103 * encrypted.
2104 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002105 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002106 * encrypted data. The additional data is not
2107 * part of this output. For algorithms where the
2108 * encrypted data and the authentication tag
2109 * are defined as separate outputs, the
2110 * authentication tag is appended to the
2111 * encrypted data.
2112 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2113 * This must be at least
2114 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2115 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002116 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002117 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002118 *
Gilles Peskine28538492018-07-11 17:34:00 +02002119 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002120 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002121 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002122 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002123 * \retval #PSA_ERROR_NOT_PERMITTED
2124 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002125 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002126 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002127 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002128 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2129 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2130 * \retval #PSA_ERROR_HARDWARE_FAILURE
2131 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002132 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002133 * The library has not been previously initialized by psa_crypto_init().
2134 * It is implementation-dependent whether a failure to initialize
2135 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002136 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002137psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002138 psa_algorithm_t alg,
2139 const uint8_t *nonce,
2140 size_t nonce_length,
2141 const uint8_t *additional_data,
2142 size_t additional_data_length,
2143 const uint8_t *plaintext,
2144 size_t plaintext_length,
2145 uint8_t *ciphertext,
2146 size_t ciphertext_size,
2147 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002148
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002149/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002150 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002151 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002152 * \param alg The AEAD algorithm to compute
2153 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002154 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002155 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002156 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002157 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002158 * but not encrypted.
2159 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002160 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002161 * encrypted. For algorithms where the
2162 * encrypted data and the authentication tag
2163 * are defined as separate inputs, the buffer
2164 * must contain the encrypted data followed
2165 * by the authentication tag.
2166 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002167 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002168 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2169 * This must be at least
2170 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2171 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002172 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002173 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002174 *
Gilles Peskine28538492018-07-11 17:34:00 +02002175 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002176 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002177 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002178 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002179 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002180 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002181 * \retval #PSA_ERROR_NOT_PERMITTED
2182 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002183 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002184 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002185 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002186 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2187 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2188 * \retval #PSA_ERROR_HARDWARE_FAILURE
2189 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002190 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002191 * The library has not been previously initialized by psa_crypto_init().
2192 * It is implementation-dependent whether a failure to initialize
2193 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002194 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002195psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002196 psa_algorithm_t alg,
2197 const uint8_t *nonce,
2198 size_t nonce_length,
2199 const uint8_t *additional_data,
2200 size_t additional_data_length,
2201 const uint8_t *ciphertext,
2202 size_t ciphertext_length,
2203 uint8_t *plaintext,
2204 size_t plaintext_size,
2205 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002206
Gilles Peskine30a9e412019-01-14 18:36:12 +01002207/** The type of the state data structure for multipart AEAD operations.
2208 *
2209 * Before calling any function on an AEAD operation object, the application
2210 * must initialize it by any of the following means:
2211 * - Set the structure to all-bits-zero, for example:
2212 * \code
2213 * psa_aead_operation_t operation;
2214 * memset(&operation, 0, sizeof(operation));
2215 * \endcode
2216 * - Initialize the structure to logical zero values, for example:
2217 * \code
2218 * psa_aead_operation_t operation = {0};
2219 * \endcode
2220 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2221 * for example:
2222 * \code
2223 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2224 * \endcode
2225 * - Assign the result of the function psa_aead_operation_init()
2226 * to the structure, for example:
2227 * \code
2228 * psa_aead_operation_t operation;
2229 * operation = psa_aead_operation_init();
2230 * \endcode
2231 *
2232 * This is an implementation-defined \c struct. Applications should not
2233 * make any assumptions about the content of this structure except
2234 * as directed by the documentation of a specific implementation. */
2235typedef struct psa_aead_operation_s psa_aead_operation_t;
2236
2237/** \def PSA_AEAD_OPERATION_INIT
2238 *
2239 * This macro returns a suitable initializer for an AEAD operation object of
2240 * type #psa_aead_operation_t.
2241 */
2242#ifdef __DOXYGEN_ONLY__
2243/* This is an example definition for documentation purposes.
2244 * Implementations should define a suitable value in `crypto_struct.h`.
2245 */
2246#define PSA_AEAD_OPERATION_INIT {0}
2247#endif
2248
2249/** Return an initial value for an AEAD operation object.
2250 */
2251static psa_aead_operation_t psa_aead_operation_init(void);
2252
2253/** Set the key for a multipart authenticated encryption operation.
2254 *
2255 * The sequence of operations to encrypt a message with authentication
2256 * is as follows:
2257 * -# Allocate an operation object which will be passed to all the functions
2258 * listed here.
2259 * -# Initialize the operation object with one of the methods described in the
2260 * documentation for #psa_aead_operation_t, e.g.
2261 * PSA_AEAD_OPERATION_INIT.
2262 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002263 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2264 * inputs to the subsequent calls to psa_aead_update_ad() and
2265 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2266 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002267 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2268 * generate or set the nonce. You should use
2269 * psa_aead_generate_nonce() unless the protocol you are implementing
2270 * requires a specific nonce value.
2271 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2272 * of the non-encrypted additional authenticated data each time.
2273 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002274 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002275 * -# Call psa_aead_finish().
2276 *
2277 * The application may call psa_aead_abort() at any time after the operation
2278 * has been initialized.
2279 *
2280 * After a successful call to psa_aead_encrypt_setup(), the application must
2281 * eventually terminate the operation. The following events terminate an
2282 * operation:
2283 * - A failed call to any of the \c psa_aead_xxx functions.
2284 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2285 *
2286 * \param[in,out] operation The operation object to set up. It must have
2287 * been initialized as per the documentation for
2288 * #psa_aead_operation_t and not yet in use.
2289 * \param handle Handle to the key to use for the operation.
2290 * It must remain valid until the operation
2291 * terminates.
2292 * \param alg The AEAD algorithm to compute
2293 * (\c PSA_ALG_XXX value such that
2294 * #PSA_ALG_IS_AEAD(\p alg) is true).
2295 *
2296 * \retval #PSA_SUCCESS
2297 * Success.
2298 * \retval #PSA_ERROR_INVALID_HANDLE
2299 * \retval #PSA_ERROR_EMPTY_SLOT
2300 * \retval #PSA_ERROR_NOT_PERMITTED
2301 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002302 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002303 * \retval #PSA_ERROR_NOT_SUPPORTED
2304 * \p alg is not supported or is not an AEAD algorithm.
2305 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2306 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2307 * \retval #PSA_ERROR_HARDWARE_FAILURE
2308 * \retval #PSA_ERROR_TAMPERING_DETECTED
2309 * \retval #PSA_ERROR_BAD_STATE
2310 * The library has not been previously initialized by psa_crypto_init().
2311 * It is implementation-dependent whether a failure to initialize
2312 * results in this error code.
2313 */
2314psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2315 psa_key_handle_t handle,
2316 psa_algorithm_t alg);
2317
2318/** Set the key for a multipart authenticated decryption operation.
2319 *
2320 * The sequence of operations to decrypt a message with authentication
2321 * is as follows:
2322 * -# Allocate an operation object which will be passed to all the functions
2323 * listed here.
2324 * -# Initialize the operation object with one of the methods described in the
2325 * documentation for #psa_aead_operation_t, e.g.
2326 * PSA_AEAD_OPERATION_INIT.
2327 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002328 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2329 * inputs to the subsequent calls to psa_aead_update_ad() and
2330 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2331 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002332 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2333 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2334 * of the non-encrypted additional authenticated data each time.
2335 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002336 * of the ciphertext to decrypt each time.
2337 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002338 *
2339 * The application may call psa_aead_abort() at any time after the operation
2340 * has been initialized.
2341 *
2342 * After a successful call to psa_aead_decrypt_setup(), the application must
2343 * eventually terminate the operation. The following events terminate an
2344 * operation:
2345 * - A failed call to any of the \c psa_aead_xxx functions.
2346 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2347 *
2348 * \param[in,out] operation The operation object to set up. It must have
2349 * been initialized as per the documentation for
2350 * #psa_aead_operation_t and not yet in use.
2351 * \param handle Handle to the key to use for the operation.
2352 * It must remain valid until the operation
2353 * terminates.
2354 * \param alg The AEAD algorithm to compute
2355 * (\c PSA_ALG_XXX value such that
2356 * #PSA_ALG_IS_AEAD(\p alg) is true).
2357 *
2358 * \retval #PSA_SUCCESS
2359 * Success.
2360 * \retval #PSA_ERROR_INVALID_HANDLE
2361 * \retval #PSA_ERROR_EMPTY_SLOT
2362 * \retval #PSA_ERROR_NOT_PERMITTED
2363 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002364 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002365 * \retval #PSA_ERROR_NOT_SUPPORTED
2366 * \p alg is not supported or is not an AEAD algorithm.
2367 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2368 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2369 * \retval #PSA_ERROR_HARDWARE_FAILURE
2370 * \retval #PSA_ERROR_TAMPERING_DETECTED
2371 * \retval #PSA_ERROR_BAD_STATE
2372 * The library has not been previously initialized by psa_crypto_init().
2373 * It is implementation-dependent whether a failure to initialize
2374 * results in this error code.
2375 */
2376psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2377 psa_key_handle_t handle,
2378 psa_algorithm_t alg);
2379
2380/** Generate a random nonce for an authenticated encryption operation.
2381 *
2382 * This function generates a random nonce for the authenticated encryption
2383 * operation with an appropriate size for the chosen algorithm, key type
2384 * and key size.
2385 *
2386 * The application must call psa_aead_encrypt_setup() before
2387 * calling this function.
2388 *
2389 * If this function returns an error status, the operation becomes inactive.
2390 *
2391 * \param[in,out] operation Active AEAD operation.
2392 * \param[out] nonce Buffer where the generated nonce is to be
2393 * written.
2394 * \param nonce_size Size of the \p nonce buffer in bytes.
2395 * \param[out] nonce_length On success, the number of bytes of the
2396 * generated nonce.
2397 *
2398 * \retval #PSA_SUCCESS
2399 * Success.
2400 * \retval #PSA_ERROR_BAD_STATE
2401 * The operation state is not valid (not set up, or nonce already set).
2402 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2403 * The size of the \p nonce buffer is too small.
2404 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2405 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2406 * \retval #PSA_ERROR_HARDWARE_FAILURE
2407 * \retval #PSA_ERROR_TAMPERING_DETECTED
2408 */
2409psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2410 unsigned char *nonce,
2411 size_t nonce_size,
2412 size_t *nonce_length);
2413
2414/** Set the nonce for an authenticated encryption or decryption operation.
2415 *
2416 * This function sets the nonce for the authenticated
2417 * encryption or decryption operation.
2418 *
2419 * The application must call psa_aead_encrypt_setup() before
2420 * calling this function.
2421 *
2422 * If this function returns an error status, the operation becomes inactive.
2423 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002424 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002425 * instead of this function, unless implementing a protocol that requires
2426 * a non-random IV.
2427 *
2428 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002429 * \param[in] nonce Buffer containing the nonce to use.
2430 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002431 *
2432 * \retval #PSA_SUCCESS
2433 * Success.
2434 * \retval #PSA_ERROR_BAD_STATE
2435 * The operation state is not valid (not set up, or nonce already set).
2436 * \retval #PSA_ERROR_INVALID_ARGUMENT
2437 * The size of \p nonce is not acceptable for the chosen algorithm.
2438 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2439 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2440 * \retval #PSA_ERROR_HARDWARE_FAILURE
2441 * \retval #PSA_ERROR_TAMPERING_DETECTED
2442 */
2443psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2444 const unsigned char *nonce,
2445 size_t nonce_length);
2446
Gilles Peskinebc59c852019-01-17 15:26:08 +01002447/** Declare the lengths of the message and additional data for AEAD.
2448 *
2449 * The application must call this function before calling
2450 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2451 * the operation requires it. If the algorithm does not require it,
2452 * calling this function is optional, but if this function is called
2453 * then the implementation must enforce the lengths.
2454 *
2455 * You may call this function before or after setting the nonce with
2456 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2457 *
2458 * - For #PSA_ALG_CCM, calling this function is required.
2459 * - For the other AEAD algorithms defined in this specification, calling
2460 * this function is not required.
2461 * - For vendor-defined algorithm, refer to the vendor documentation.
2462 *
2463 * \param[in,out] operation Active AEAD operation.
2464 * \param ad_length Size of the non-encrypted additional
2465 * authenticated data in bytes.
2466 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2467 *
2468 * \retval #PSA_SUCCESS
2469 * Success.
2470 * \retval #PSA_ERROR_BAD_STATE
2471 * The operation state is not valid (not set up, already completed,
2472 * or psa_aead_update_ad() or psa_aead_update() already called).
2473 * \retval #PSA_ERROR_INVALID_ARGUMENT
2474 * At least one of the lengths is not acceptable for the chosen
2475 * algorithm.
2476 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2477 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2478 * \retval #PSA_ERROR_HARDWARE_FAILURE
2479 * \retval #PSA_ERROR_TAMPERING_DETECTED
2480 */
2481psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2482 size_t ad_length,
2483 size_t plaintext_length);
2484
Gilles Peskine30a9e412019-01-14 18:36:12 +01002485/** Pass additional data to an active AEAD operation.
2486 *
2487 * Additional data is authenticated, but not encrypted.
2488 *
2489 * You may call this function multiple times to pass successive fragments
2490 * of the additional data. You may not call this function after passing
2491 * data to encrypt or decrypt with psa_aead_update().
2492 *
2493 * Before calling this function, you must:
2494 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2495 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2496 *
2497 * If this function returns an error status, the operation becomes inactive.
2498 *
2499 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2500 * there is no guarantee that the input is valid. Therefore, until
2501 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2502 * treat the input as untrusted and prepare to undo any action that
2503 * depends on the input if psa_aead_verify() returns an error status.
2504 *
2505 * \param[in,out] operation Active AEAD operation.
2506 * \param[in] input Buffer containing the fragment of
2507 * additional data.
2508 * \param input_length Size of the \p input buffer in bytes.
2509 *
2510 * \retval #PSA_SUCCESS
2511 * Success.
2512 * \retval #PSA_ERROR_BAD_STATE
2513 * The operation state is not valid (not set up, nonce not set,
2514 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002515 * \retval #PSA_ERROR_INVALID_ARGUMENT
2516 * The total input length overflows the additional data length that
2517 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002518 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2519 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2520 * \retval #PSA_ERROR_HARDWARE_FAILURE
2521 * \retval #PSA_ERROR_TAMPERING_DETECTED
2522 */
2523psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2524 const uint8_t *input,
2525 size_t input_length);
2526
2527/** Encrypt or decrypt a message fragment in an active AEAD operation.
2528 *
2529 * Before calling this function, you must:
2530 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2531 * The choice of setup function determines whether this function
2532 * encrypts or decrypts its input.
2533 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2534 * 3. Call psa_aead_update_ad() to pass all the additional data.
2535 *
2536 * If this function returns an error status, the operation becomes inactive.
2537 *
2538 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2539 * there is no guarantee that the input is valid. Therefore, until
2540 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2541 * - Do not use the output in any way other than storing it in a
2542 * confidential location. If you take any action that depends
2543 * on the tentative decrypted data, this action will need to be
2544 * undone if the input turns out not to be valid. Furthermore,
2545 * if an adversary can observe that this action took place
2546 * (for example through timing), they may be able to use this
2547 * fact as an oracle to decrypt any message encrypted with the
2548 * same key.
2549 * - In particular, do not copy the output anywhere but to a
2550 * memory or storage space that you have exclusive access to.
2551 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002552 * This function does not require the input to be aligned to any
2553 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002554 * a whole block at a time, it must consume all the input provided, but
2555 * it may delay the end of the corresponding output until a subsequent
2556 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2557 * provides sufficient input. The amount of data that can be delayed
2558 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002559 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002560 * \param[in,out] operation Active AEAD operation.
2561 * \param[in] input Buffer containing the message fragment to
2562 * encrypt or decrypt.
2563 * \param input_length Size of the \p input buffer in bytes.
2564 * \param[out] output Buffer where the output is to be written.
2565 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002566 * This must be at least
2567 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2568 * \p input_length) where \c alg is the
2569 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002570 * \param[out] output_length On success, the number of bytes
2571 * that make up the returned output.
2572 *
2573 * \retval #PSA_SUCCESS
2574 * Success.
2575 * \retval #PSA_ERROR_BAD_STATE
2576 * The operation state is not valid (not set up, nonce not set
2577 * or already completed).
2578 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2579 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002580 * You can determine a sufficient buffer size by calling
2581 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2582 * where \c alg is the algorithm that is being calculated.
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 input length overflows the plaintext length that
2589 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002590 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2591 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2592 * \retval #PSA_ERROR_HARDWARE_FAILURE
2593 * \retval #PSA_ERROR_TAMPERING_DETECTED
2594 */
2595psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2596 const uint8_t *input,
2597 size_t input_length,
2598 unsigned char *output,
2599 size_t output_size,
2600 size_t *output_length);
2601
2602/** Finish encrypting a message in an AEAD operation.
2603 *
2604 * The operation must have been set up with psa_aead_encrypt_setup().
2605 *
2606 * This function finishes the authentication of the additional data
2607 * formed by concatenating the inputs passed to preceding calls to
2608 * psa_aead_update_ad() with the plaintext formed by concatenating the
2609 * inputs passed to preceding calls to psa_aead_update().
2610 *
2611 * This function has two output buffers:
2612 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002613 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002614 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002615 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002616 * that the operation performs.
2617 *
2618 * When this function returns, the operation becomes inactive.
2619 *
2620 * \param[in,out] operation Active AEAD operation.
2621 * \param[out] ciphertext Buffer where the last part of the ciphertext
2622 * is to be written.
2623 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002624 * This must be at least
2625 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2626 * \c alg is the algorithm that is being
2627 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002628 * \param[out] ciphertext_length On success, the number of bytes of
2629 * returned ciphertext.
2630 * \param[out] tag Buffer where the authentication tag is
2631 * to be written.
2632 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002633 * This must be at least
2634 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2635 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002636 * \param[out] tag_length On success, the number of bytes
2637 * that make up the returned tag.
2638 *
2639 * \retval #PSA_SUCCESS
2640 * Success.
2641 * \retval #PSA_ERROR_BAD_STATE
2642 * The operation state is not valid (not set up, nonce not set,
2643 * decryption, or already completed).
2644 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002645 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002646 * You can determine a sufficient buffer size for \p ciphertext by
2647 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2648 * where \c alg is the algorithm that is being calculated.
2649 * You can determine a sufficient buffer size for \p tag by
2650 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002651 * \retval #PSA_ERROR_INVALID_ARGUMENT
2652 * The total length of input to psa_aead_update_ad() so far is
2653 * less than the additional data length that was previously
2654 * specified with psa_aead_set_lengths().
2655 * \retval #PSA_ERROR_INVALID_ARGUMENT
2656 * The total length of input to psa_aead_update() so far is
2657 * less than the plaintext length that was previously
2658 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002659 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2660 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2661 * \retval #PSA_ERROR_HARDWARE_FAILURE
2662 * \retval #PSA_ERROR_TAMPERING_DETECTED
2663 */
2664psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002665 uint8_t *ciphertext,
2666 size_t ciphertext_size,
2667 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002668 uint8_t *tag,
2669 size_t tag_size,
2670 size_t *tag_length);
2671
2672/** Finish authenticating and decrypting a message in an AEAD operation.
2673 *
2674 * The operation must have been set up with psa_aead_decrypt_setup().
2675 *
2676 * This function finishes the authentication of the additional data
2677 * formed by concatenating the inputs passed to preceding calls to
2678 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2679 * inputs passed to preceding calls to psa_aead_update().
2680 *
2681 * When this function returns, the operation becomes inactive.
2682 *
2683 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002684 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002685 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002686 * from previous calls to psa_aead_update()
2687 * that could not be processed until the end
2688 * of the input.
2689 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002690 * This must be at least
2691 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2692 * \c alg is the algorithm that is being
2693 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002694 * \param[out] plaintext_length On success, the number of bytes of
2695 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002696 * \param[in] tag Buffer containing the authentication tag.
2697 * \param tag_length Size of the \p tag buffer in bytes.
2698 *
2699 * \retval #PSA_SUCCESS
2700 * Success.
2701 * \retval #PSA_ERROR_BAD_STATE
2702 * The operation state is not valid (not set up, nonce not set,
2703 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002704 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2705 * The size of the \p plaintext buffer is too small.
2706 * You can determine a sufficient buffer size for \p plaintext by
2707 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2708 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002709 * \retval #PSA_ERROR_INVALID_ARGUMENT
2710 * The total length of input to psa_aead_update_ad() so far is
2711 * less than the additional data length that was previously
2712 * specified with psa_aead_set_lengths().
2713 * \retval #PSA_ERROR_INVALID_ARGUMENT
2714 * The total length of input to psa_aead_update() so far is
2715 * less than the plaintext length that was previously
2716 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002717 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2718 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2719 * \retval #PSA_ERROR_HARDWARE_FAILURE
2720 * \retval #PSA_ERROR_TAMPERING_DETECTED
2721 */
2722psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002723 uint8_t *plaintext,
2724 size_t plaintext_size,
2725 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002726 const uint8_t *tag,
2727 size_t tag_length);
2728
2729/** Abort an AEAD operation.
2730 *
2731 * Aborting an operation frees all associated resources except for the
2732 * \p operation structure itself. Once aborted, the operation object
2733 * can be reused for another operation by calling
2734 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2735 *
2736 * You may call this function any time after the operation object has
2737 * been initialized by any of the following methods:
2738 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2739 * whether it succeeds or not.
2740 * - Initializing the \c struct to all-bits-zero.
2741 * - Initializing the \c struct to logical zeros, e.g.
2742 * `psa_aead_operation_t operation = {0}`.
2743 *
2744 * In particular, calling psa_aead_abort() after the operation has been
2745 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2746 * is safe and has no effect.
2747 *
2748 * \param[in,out] operation Initialized AEAD operation.
2749 *
2750 * \retval #PSA_SUCCESS
2751 * \retval #PSA_ERROR_BAD_STATE
2752 * \p operation is not an active AEAD operation.
2753 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2754 * \retval #PSA_ERROR_HARDWARE_FAILURE
2755 * \retval #PSA_ERROR_TAMPERING_DETECTED
2756 */
2757psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2758
Gilles Peskine3b555712018-03-03 21:27:57 +01002759/**@}*/
2760
Gilles Peskine20035e32018-02-03 22:44:14 +01002761/** \defgroup asymmetric Asymmetric cryptography
2762 * @{
2763 */
2764
2765/**
2766 * \brief Sign a hash or short message with a private key.
2767 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002768 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002769 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002770 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2771 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2772 * to determine the hash algorithm to use.
2773 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002774 * \param handle Handle to the key to use for the operation.
2775 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002776 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002777 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002778 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002779 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002780 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002781 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002782 * \param[out] signature_length On success, the number of bytes
2783 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002784 *
Gilles Peskine28538492018-07-11 17:34:00 +02002785 * \retval #PSA_SUCCESS
2786 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002787 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002788 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002789 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002790 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002791 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002792 * \retval #PSA_ERROR_NOT_SUPPORTED
2793 * \retval #PSA_ERROR_INVALID_ARGUMENT
2794 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2795 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2796 * \retval #PSA_ERROR_HARDWARE_FAILURE
2797 * \retval #PSA_ERROR_TAMPERING_DETECTED
2798 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002799 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002800 * The library has not been previously initialized by psa_crypto_init().
2801 * It is implementation-dependent whether a failure to initialize
2802 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002803 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002804psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002805 psa_algorithm_t alg,
2806 const uint8_t *hash,
2807 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002808 uint8_t *signature,
2809 size_t signature_size,
2810 size_t *signature_length);
2811
2812/**
2813 * \brief Verify the signature a hash or short message using a public key.
2814 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002815 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002816 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002817 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2818 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2819 * to determine the hash algorithm to use.
2820 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002821 * \param handle Handle to the key to use for the operation.
2822 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002823 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002824 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002825 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002826 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002827 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002828 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002829 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002830 *
Gilles Peskine28538492018-07-11 17:34:00 +02002831 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002832 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002833 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002834 * The calculation was perfomed successfully, but the passed
2835 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002836 * \retval #PSA_ERROR_NOT_SUPPORTED
2837 * \retval #PSA_ERROR_INVALID_ARGUMENT
2838 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2839 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2840 * \retval #PSA_ERROR_HARDWARE_FAILURE
2841 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002842 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002843 * The library has not been previously initialized by psa_crypto_init().
2844 * It is implementation-dependent whether a failure to initialize
2845 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002846 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002847psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002848 psa_algorithm_t alg,
2849 const uint8_t *hash,
2850 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002851 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002852 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002853
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002854/**
2855 * \brief Encrypt a short message with a public key.
2856 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002857 * \param handle Handle to the key to use for the operation.
2858 * It must be a public key or an asymmetric
2859 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002860 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002861 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002862 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002863 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002864 * \param[in] salt A salt or label, if supported by the
2865 * encryption algorithm.
2866 * If the algorithm does not support a
2867 * salt, pass \c NULL.
2868 * If the algorithm supports an optional
2869 * salt and you do not want to pass a salt,
2870 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002871 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002872 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2873 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002874 * \param salt_length Size of the \p salt buffer in bytes.
2875 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002876 * \param[out] output Buffer where the encrypted message is to
2877 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002878 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002879 * \param[out] output_length On success, the number of bytes
2880 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002881 *
Gilles Peskine28538492018-07-11 17:34:00 +02002882 * \retval #PSA_SUCCESS
2883 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002884 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002885 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002886 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002887 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002888 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002889 * \retval #PSA_ERROR_NOT_SUPPORTED
2890 * \retval #PSA_ERROR_INVALID_ARGUMENT
2891 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2892 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2893 * \retval #PSA_ERROR_HARDWARE_FAILURE
2894 * \retval #PSA_ERROR_TAMPERING_DETECTED
2895 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002896 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002897 * The library has not been previously initialized by psa_crypto_init().
2898 * It is implementation-dependent whether a failure to initialize
2899 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002900 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002901psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002902 psa_algorithm_t alg,
2903 const uint8_t *input,
2904 size_t input_length,
2905 const uint8_t *salt,
2906 size_t salt_length,
2907 uint8_t *output,
2908 size_t output_size,
2909 size_t *output_length);
2910
2911/**
2912 * \brief Decrypt a short message with a private key.
2913 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002914 * \param handle Handle to the key to use for the operation.
2915 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002916 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002917 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002918 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002919 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002920 * \param[in] salt A salt or label, if supported by the
2921 * encryption algorithm.
2922 * If the algorithm does not support a
2923 * salt, pass \c NULL.
2924 * If the algorithm supports an optional
2925 * salt and you do not want to pass a salt,
2926 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002927 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002928 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2929 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002930 * \param salt_length Size of the \p salt buffer in bytes.
2931 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002932 * \param[out] output Buffer where the decrypted message is to
2933 * be written.
2934 * \param output_size Size of the \c output buffer in bytes.
2935 * \param[out] output_length On success, the number of bytes
2936 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002937 *
Gilles Peskine28538492018-07-11 17:34:00 +02002938 * \retval #PSA_SUCCESS
2939 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002940 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002941 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002942 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002943 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002944 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002945 * \retval #PSA_ERROR_NOT_SUPPORTED
2946 * \retval #PSA_ERROR_INVALID_ARGUMENT
2947 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2948 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2949 * \retval #PSA_ERROR_HARDWARE_FAILURE
2950 * \retval #PSA_ERROR_TAMPERING_DETECTED
2951 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2952 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002953 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002954 * The library has not been previously initialized by psa_crypto_init().
2955 * It is implementation-dependent whether a failure to initialize
2956 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002957 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002958psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002959 psa_algorithm_t alg,
2960 const uint8_t *input,
2961 size_t input_length,
2962 const uint8_t *salt,
2963 size_t salt_length,
2964 uint8_t *output,
2965 size_t output_size,
2966 size_t *output_length);
2967
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002968/**@}*/
2969
Gilles Peskine35675b62019-05-16 17:26:11 +02002970/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002971 * @{
2972 */
2973
Gilles Peskine35675b62019-05-16 17:26:11 +02002974/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002975 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002976 * Before calling any function on a key derivation operation object, the
2977 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002978 * - Set the structure to all-bits-zero, for example:
2979 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002980 * psa_key_derivation_operation_t operation;
2981 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002982 * \endcode
2983 * - Initialize the structure to logical zero values, for example:
2984 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002985 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002986 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002987 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002988 * for example:
2989 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002990 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002991 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002992 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002993 * to the structure, for example:
2994 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002995 * psa_key_derivation_operation_t operation;
2996 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002997 * \endcode
2998 *
2999 * This is an implementation-defined \c struct. Applications should not
3000 * make any assumptions about the content of this structure except
3001 * as directed by the documentation of a specific implementation.
3002 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003003typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003004
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003005/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003006 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003007 * This macro returns a suitable initializer for a key derivation operation
3008 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003009 */
3010#ifdef __DOXYGEN_ONLY__
3011/* This is an example definition for documentation purposes.
3012 * Implementations should define a suitable value in `crypto_struct.h`.
3013 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003014#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003015#endif
3016
Gilles Peskine35675b62019-05-16 17:26:11 +02003017/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003018 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003019static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003020
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003021/** Set up a key derivation operation.
3022 *
3023 * A key derivation algorithm takes some inputs and uses them to generate
3024 * a byte stream in a deterministic way.
3025 * This byte stream can be used to produce keys and other
3026 * cryptographic material.
3027 *
3028 * To derive a key:
3029 * - Start with an initialized object of type #psa_key_derivation_operation_t.
3030 * - Call psa_key_derivation_setup() to select the algorithm.
3031 * - Provide the inputs for the key derivation by calling
3032 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3033 * as appropriate. Which inputs are needed, in what order, and whether
3034 * they may be keys and if so of what type depends on the algorithm.
3035 * - Optionally set the operation's maximum capacity with
3036 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3037 * of or after providing inputs. For some algorithms, this step is mandatory
3038 * because the output depends on the maximum capacity.
3039 * - To derive a key, call psa_key_derivation_output_key().
3040 * To derive a byte string for a different purpose, call
3041 * - psa_key_derivation_output_bytes().
3042 * Successive calls to these functions use successive output bytes
3043 * calculated by the key derivation algorithm.
3044 * - Clean up the key derivation operation object with
3045 * psa_key_derivation_abort().
3046 *
3047 * \param[in,out] operation The key derivation operation object
3048 * to set up. It must
3049 * have been initialized but not set up yet.
3050 * \param alg The key derivation algorithm to compute
3051 * (\c PSA_ALG_XXX value such that
3052 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3053 *
3054 * \retval #PSA_SUCCESS
3055 * Success.
3056 * \retval #PSA_ERROR_INVALID_ARGUMENT
3057 * \c alg is not a key derivation algorithm.
3058 * \retval #PSA_ERROR_NOT_SUPPORTED
3059 * \c alg is not supported or is not a key derivation algorithm.
3060 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3061 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3062 * \retval #PSA_ERROR_HARDWARE_FAILURE
3063 * \retval #PSA_ERROR_TAMPERING_DETECTED
3064 * \retval #PSA_ERROR_BAD_STATE
3065 */
3066psa_status_t psa_key_derivation_setup(
3067 psa_key_derivation_operation_t *operation,
3068 psa_algorithm_t alg);
3069
Gilles Peskine35675b62019-05-16 17:26:11 +02003070/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003071 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003072 * The capacity of a key derivation is the maximum number of bytes that it can
3073 * return. When you get *N* bytes of output from a key derivation operation,
3074 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003075 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003076 * \param[in] operation The operation to query.
3077 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003078 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003079 * \retval #PSA_SUCCESS
3080 * \retval #PSA_ERROR_BAD_STATE
3081 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02003082 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003083psa_status_t psa_key_derivation_get_capacity(
3084 const psa_key_derivation_operation_t *operation,
3085 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003086
Gilles Peskine35675b62019-05-16 17:26:11 +02003087/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003088 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003089 * The capacity of a key derivation operation is the maximum number of bytes
3090 * that the key derivation operation can return from this point onwards.
3091 *
3092 * \param[in,out] operation The key derivation operation object to modify.
3093 * \param capacity The new capacity of the operation.
3094 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003095 * current capacity.
3096 *
3097 * \retval #PSA_SUCCESS
3098 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003099 * \p capacity is larger than the operation's current capacity.
3100 * In this case, the operation object remains valid and its capacity
3101 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003102 * \retval #PSA_ERROR_BAD_STATE
3103 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3104 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003105psa_status_t psa_key_derivation_set_capacity(
3106 psa_key_derivation_operation_t *operation,
3107 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003108
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003109/** Use the maximum possible capacity for a key derivation operation.
3110 *
3111 * Use this value as the capacity argument when setting up a key derivation
3112 * to indicate that the operation should have the maximum possible capacity.
3113 * The value of the maximum possible capacity depends on the key derivation
3114 * algorithm.
3115 */
3116#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3117
3118/** Provide an input for key derivation or key agreement.
3119 *
3120 * Which inputs are required and in what order depends on the algorithm.
3121 * Refer to the documentation of each key derivation or key agreement
3122 * algorithm for information.
3123 *
3124 * This function passes direct inputs. Some inputs must be passed as keys
3125 * using psa_key_derivation_input_key() instead of this function. Refer to
3126 * the documentation of individual step types for information.
3127 *
3128 * \param[in,out] operation The key derivation operation object to use.
3129 * It must have been set up with
3130 * psa_key_derivation_setup() and must not
3131 * have produced any output yet.
3132 * \param step Which step the input data is for.
3133 * \param[in] data Input data to use.
3134 * \param data_length Size of the \p data buffer in bytes.
3135 *
3136 * \retval #PSA_SUCCESS
3137 * Success.
3138 * \retval #PSA_ERROR_INVALID_ARGUMENT
3139 * \c step is not compatible with the operation's algorithm.
3140 * \retval #PSA_ERROR_INVALID_ARGUMENT
3141 * \c step does not allow direct inputs.
3142 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3143 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3144 * \retval #PSA_ERROR_HARDWARE_FAILURE
3145 * \retval #PSA_ERROR_TAMPERING_DETECTED
3146 * \retval #PSA_ERROR_BAD_STATE
3147 * The value of \p step is not valid given the state of \p operation.
3148 * \retval #PSA_ERROR_BAD_STATE
3149 * The library has not been previously initialized by psa_crypto_init().
3150 * It is implementation-dependent whether a failure to initialize
3151 * results in this error code.
3152 */
3153psa_status_t psa_key_derivation_input_bytes(
3154 psa_key_derivation_operation_t *operation,
3155 psa_key_derivation_step_t step,
3156 const uint8_t *data,
3157 size_t data_length);
3158
3159/** Provide an input for key derivation in the form of a key.
3160 *
3161 * Which inputs are required and in what order depends on the algorithm.
3162 * Refer to the documentation of each key derivation or key agreement
3163 * algorithm for information.
3164 *
3165 * This function passes key inputs. Some inputs must be passed as keys
3166 * of the appropriate type using this function, while others must be
3167 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3168 * the documentation of individual step types for information.
3169 *
3170 * \param[in,out] operation The key derivation operation object to use.
3171 * It must have been set up with
3172 * psa_key_derivation_setup() and must not
3173 * have produced any output yet.
3174 * \param step Which step the input data is for.
3175 * \param handle Handle to the key. It must have an
3176 * appropriate type for \p step and must
3177 * allow the usage #PSA_KEY_USAGE_DERIVE.
3178 *
3179 * \retval #PSA_SUCCESS
3180 * Success.
3181 * \retval #PSA_ERROR_INVALID_HANDLE
3182 * \retval #PSA_ERROR_DOES_NOT_EXIST
3183 * \retval #PSA_ERROR_NOT_PERMITTED
3184 * \retval #PSA_ERROR_INVALID_ARGUMENT
3185 * \c step is not compatible with the operation's algorithm.
3186 * \retval #PSA_ERROR_INVALID_ARGUMENT
3187 * \c step does not allow key inputs.
3188 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3189 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3190 * \retval #PSA_ERROR_HARDWARE_FAILURE
3191 * \retval #PSA_ERROR_TAMPERING_DETECTED
3192 * \retval #PSA_ERROR_BAD_STATE
3193 * The value of \p step is not valid given the state of \p operation.
3194 * \retval #PSA_ERROR_BAD_STATE
3195 * The library has not been previously initialized by psa_crypto_init().
3196 * It is implementation-dependent whether a failure to initialize
3197 * results in this error code.
3198 */
3199psa_status_t psa_key_derivation_input_key(
3200 psa_key_derivation_operation_t *operation,
3201 psa_key_derivation_step_t step,
3202 psa_key_handle_t handle);
3203
3204/** Perform a key agreement and use the shared secret as input to a key
3205 * derivation.
3206 *
3207 * A key agreement algorithm takes two inputs: a private key \p private_key
3208 * a public key \p peer_key.
3209 * The result of this function is passed as input to a key derivation.
3210 * The output of this key derivation can be extracted by reading from the
3211 * resulting operation to produce keys and other cryptographic material.
3212 *
3213 * \param[in,out] operation The key derivation operation object to use.
3214 * It must have been set up with
3215 * psa_key_derivation_setup() with a
3216 * key agreement and derivation algorithm
3217 * \c alg (\c PSA_ALG_XXX value such that
3218 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3219 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3220 * is false).
3221 * The operation must be ready for an
3222 * input of the type given by \p step.
3223 * \param step Which step the input data is for.
3224 * \param private_key Handle to the private key to use.
3225 * \param[in] peer_key Public key of the peer. The peer key must be in the
3226 * same format that psa_import_key() accepts for the
3227 * public key type corresponding to the type of
3228 * private_key. That is, this function performs the
3229 * equivalent of
3230 * #psa_import_key(...,
3231 * `peer_key`, `peer_key_length`) where
3232 * with key attributes indicating the public key
3233 * type corresponding to the type of `private_key`.
3234 * For example, for EC keys, this means that peer_key
3235 * is interpreted as a point on the curve that the
3236 * private key is on. The standard formats for public
3237 * keys are documented in the documentation of
3238 * psa_export_public_key().
3239 * \param peer_key_length Size of \p peer_key in bytes.
3240 *
3241 * \retval #PSA_SUCCESS
3242 * Success.
3243 * \retval #PSA_ERROR_INVALID_HANDLE
3244 * \retval #PSA_ERROR_DOES_NOT_EXIST
3245 * \retval #PSA_ERROR_NOT_PERMITTED
3246 * \retval #PSA_ERROR_INVALID_ARGUMENT
3247 * \c private_key is not compatible with \c alg,
3248 * or \p peer_key is not valid for \c alg or not compatible with
3249 * \c private_key.
3250 * \retval #PSA_ERROR_NOT_SUPPORTED
3251 * \c alg is not supported or is not a key derivation algorithm.
3252 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3253 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3254 * \retval #PSA_ERROR_HARDWARE_FAILURE
3255 * \retval #PSA_ERROR_TAMPERING_DETECTED
3256 */
3257psa_status_t psa_key_derivation_key_agreement(
3258 psa_key_derivation_operation_t *operation,
3259 psa_key_derivation_step_t step,
3260 psa_key_handle_t private_key,
3261 const uint8_t *peer_key,
3262 size_t peer_key_length);
3263
Gilles Peskine35675b62019-05-16 17:26:11 +02003264/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003265 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003266 * This function calculates output bytes from a key derivation algorithm and
3267 * return those bytes.
3268 * If you view the key derivation's output as a stream of bytes, this
3269 * function destructively reads the requested number of bytes from the
3270 * stream.
3271 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003272 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003273 * \param[in,out] operation The key derivation operation object to read from.
3274 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003275 * \param output_length Number of bytes to output.
3276 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003277 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003278 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003279 * The operation's capacity was less than
3280 * \p output_length bytes. Note that in this case,
3281 * no output is written to the output buffer.
3282 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003283 * subsequent calls to this function will not
3284 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003285 * \retval #PSA_ERROR_BAD_STATE
3286 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3287 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3288 * \retval #PSA_ERROR_HARDWARE_FAILURE
3289 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003290 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003291psa_status_t psa_key_derivation_output_bytes(
3292 psa_key_derivation_operation_t *operation,
3293 uint8_t *output,
3294 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003295
Gilles Peskine35675b62019-05-16 17:26:11 +02003296/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003297 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003298 * This function calculates output bytes from a key derivation algorithm
3299 * and uses those bytes to generate a key deterministically.
3300 * If you view the key derivation's output as a stream of bytes, this
3301 * function destructively reads as many bytes as required from the
3302 * stream.
3303 * The operation's capacity decreases by the number of bytes read.
3304 *
3305 * How much output is produced and consumed from the operation, and how
3306 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003307 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003308 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003309 * of a given size, this function is functionally equivalent to
3310 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003311 * and passing the resulting output to #psa_import_key.
3312 * However, this function has a security benefit:
3313 * if the implementation provides an isolation boundary then
3314 * the key material is not exposed outside the isolation boundary.
3315 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003316 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003317 * The following key types defined in this specification follow this scheme:
3318 *
3319 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003320 * - #PSA_KEY_TYPE_ARC4;
3321 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003322 * - #PSA_KEY_TYPE_DERIVE;
3323 * - #PSA_KEY_TYPE_HMAC.
3324 *
3325 * - For ECC keys on a Montgomery elliptic curve
3326 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3327 * Montgomery curve), this function always draws a byte string whose
3328 * length is determined by the curve, and sets the mandatory bits
3329 * accordingly. That is:
3330 *
3331 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3332 * and process it as specified in RFC 7748 &sect;5.
3333 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3334 * and process it as specified in RFC 7748 &sect;5.
3335 *
3336 * - For key types for which the key is represented by a single sequence of
3337 * \p bits bits with constraints as to which bit sequences are acceptable,
3338 * this function draws a byte string of length (\p bits / 8) bytes rounded
3339 * up to the nearest whole number of bytes. If the resulting byte string
3340 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3341 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003342 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003343 * for the output produced by psa_export_key().
3344 * The following key types defined in this specification follow this scheme:
3345 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003346 * - #PSA_KEY_TYPE_DES.
3347 * Force-set the parity bits, but discard forbidden weak keys.
3348 * For 2-key and 3-key triple-DES, the three keys are generated
3349 * successively (for example, for 3-key triple-DES,
3350 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3351 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003352 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003353 * two keys).
3354 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR),
3355 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
3356 * ECC keys on a Weierstrass elliptic curve
3357 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3358 * Weierstrass curve).
3359 * For these key types, interpret the byte string as integer
3360 * in big-endian order. Discard it if it is not in the range
3361 * [0, *N* - 2] where *N* is the boundary of the private key domain
3362 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003363 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003364 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003365 * This method allows compliance to NIST standards, specifically
3366 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003367 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3368 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3369 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3370 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003371 *
3372 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003373 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003374 * implementation-defined.
3375 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003376 * In all cases, the data that is read is discarded from the operation.
3377 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003378 *
Gilles Peskine20628592019-04-19 19:29:50 +02003379 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003380 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003381 * \param[out] handle On success, a handle to the newly created key.
3382 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003383 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003384 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003385 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003386 * If the key is persistent, the key material and the key's metadata
3387 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003388 * \retval #PSA_ERROR_ALREADY_EXISTS
3389 * This is an attempt to create a persistent key, and there is
3390 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003391 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003392 * There was not enough data to create the desired key.
3393 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003394 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003395 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003396 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003397 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003398 * implementation in general or in this particular location.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003399 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003400 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3401 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3402 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3403 * \retval #PSA_ERROR_HARDWARE_FAILURE
3404 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003405 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003406 * The library has not been previously initialized by psa_crypto_init().
3407 * It is implementation-dependent whether a failure to initialize
3408 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003409 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003410psa_status_t psa_key_derivation_output_key(
3411 const psa_key_attributes_t *attributes,
3412 psa_key_derivation_operation_t *operation,
3413 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003414
Gilles Peskine35675b62019-05-16 17:26:11 +02003415/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003416 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003417 * Once a key derivation operation has been aborted, its capacity is zero.
3418 * Aborting an operation frees all associated resources except for the
3419 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003420 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003421 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003422 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003423 * psa_key_derivation_operation_init() or a zero value. In particular,
3424 * it is valid to call psa_key_derivation_abort() twice, or to call
3425 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003426 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003427 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003428 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003429 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003430 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003431 * \retval #PSA_SUCCESS
3432 * \retval #PSA_ERROR_BAD_STATE
3433 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3434 * \retval #PSA_ERROR_HARDWARE_FAILURE
3435 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003436 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003437psa_status_t psa_key_derivation_abort(
3438 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003439
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003440/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003441 *
3442 * \warning The raw result of a key agreement algorithm such as finite-field
3443 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3444 * not be used directly as key material. It should instead be passed as
3445 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003446 * a key derivation, use psa_key_derivation_key_agreement() and other
3447 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003448 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003449 * \param alg The key agreement algorithm to compute
3450 * (\c PSA_ALG_XXX value such that
3451 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3452 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003453 * \param private_key Handle to the private key to use.
3454 * \param[in] peer_key Public key of the peer. It must be
3455 * in the same format that psa_import_key()
3456 * accepts. The standard formats for public
3457 * keys are documented in the documentation
3458 * of psa_export_public_key().
3459 * \param peer_key_length Size of \p peer_key in bytes.
3460 * \param[out] output Buffer where the decrypted message is to
3461 * be written.
3462 * \param output_size Size of the \c output buffer in bytes.
3463 * \param[out] output_length On success, the number of bytes
3464 * that make up the returned output.
3465 *
3466 * \retval #PSA_SUCCESS
3467 * Success.
3468 * \retval #PSA_ERROR_INVALID_HANDLE
3469 * \retval #PSA_ERROR_EMPTY_SLOT
3470 * \retval #PSA_ERROR_NOT_PERMITTED
3471 * \retval #PSA_ERROR_INVALID_ARGUMENT
3472 * \p alg is not a key agreement algorithm
3473 * \retval #PSA_ERROR_INVALID_ARGUMENT
3474 * \p private_key is not compatible with \p alg,
3475 * or \p peer_key is not valid for \p alg or not compatible with
3476 * \p private_key.
3477 * \retval #PSA_ERROR_NOT_SUPPORTED
3478 * \p alg is not a supported key agreement algorithm.
3479 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3480 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3481 * \retval #PSA_ERROR_HARDWARE_FAILURE
3482 * \retval #PSA_ERROR_TAMPERING_DETECTED
3483 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003484psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3485 psa_key_handle_t private_key,
3486 const uint8_t *peer_key,
3487 size_t peer_key_length,
3488 uint8_t *output,
3489 size_t output_size,
3490 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003491
Gilles Peskineea0fb492018-07-12 17:17:20 +02003492/**@}*/
3493
Gilles Peskineedd76872018-07-20 17:42:05 +02003494/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003495 * @{
3496 */
3497
3498/**
3499 * \brief Generate random bytes.
3500 *
3501 * \warning This function **can** fail! Callers MUST check the return status
3502 * and MUST NOT use the content of the output buffer if the return
3503 * status is not #PSA_SUCCESS.
3504 *
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003505 * \note To generate a key, use psa_generate_random_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003506 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003507 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003508 * \param output_size Number of bytes to generate and output.
3509 *
Gilles Peskine28538492018-07-11 17:34:00 +02003510 * \retval #PSA_SUCCESS
3511 * \retval #PSA_ERROR_NOT_SUPPORTED
3512 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3513 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3514 * \retval #PSA_ERROR_HARDWARE_FAILURE
3515 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003516 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003517 * The library has not been previously initialized by psa_crypto_init().
3518 * It is implementation-dependent whether a failure to initialize
3519 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003520 */
3521psa_status_t psa_generate_random(uint8_t *output,
3522 size_t output_size);
3523
3524/**
3525 * \brief Generate a key or key pair.
3526 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003527 * The key is generated randomly.
3528 * Its location, policy, type and size are taken from \p attributes.
3529 *
3530 * If the type requires additional domain parameters, these are taken
3531 * from \p attributes as well. The following types use domain parameters:
3532 * - When generating an RSA key (#PSA_KEY_TYPE_RSA_KEYPAIR),
3533 * the default public exponent is 65537. This value is used if
3534 * \p attributes was set with psa_set_key_type() or by passing an empty
3535 * byte string as domain parameters to psa_set_key_domain_parameters().
3536 * If psa_set_key_domain_parameters() was used to set a non-empty
3537 * domain parameter string in \p attributes, this string is read as
3538 * a big-endian integer which is used as the public exponent.
3539 * - When generating a DSA key (#PSA_KEY_TYPE_DSA_KEYPAIR) or a
3540 * Diffie-Hellman key (#PSA_KEY_TYPE_DH_KEYPAIR), the domain parameters
3541 * from \p attributes are interpreted as described for
3542 * psa_set_key_domain_parameters().
3543 *
Gilles Peskine20628592019-04-19 19:29:50 +02003544 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003545 * \param[out] handle On success, a handle to the newly created key.
3546 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003547 *
Gilles Peskine28538492018-07-11 17:34:00 +02003548 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003549 * Success.
3550 * If the key is persistent, the key material and the key's metadata
3551 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003552 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003553 * This is an attempt to create a persistent key, and there is
3554 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003555 * \retval #PSA_ERROR_NOT_SUPPORTED
3556 * \retval #PSA_ERROR_INVALID_ARGUMENT
3557 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3558 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3559 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3560 * \retval #PSA_ERROR_HARDWARE_FAILURE
3561 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003562 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003563 * The library has not been previously initialized by psa_crypto_init().
3564 * It is implementation-dependent whether a failure to initialize
3565 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003566 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003567psa_status_t psa_generate_random_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003568 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003569
3570/**@}*/
3571
Gilles Peskinee59236f2018-01-27 23:32:46 +01003572#ifdef __cplusplus
3573}
3574#endif
3575
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003576/* The file "crypto_sizes.h" contains definitions for size calculation
3577 * macros whose definitions are implementation-specific. */
3578#include "crypto_sizes.h"
3579
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003580/* The file "crypto_struct.h" contains definitions for
3581 * implementation-specific structs that are declared above. */
3582#include "crypto_struct.h"
3583
3584/* The file "crypto_extra.h" contains vendor-specific definitions. This
3585 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003586#include "crypto_extra.h"
3587
3588#endif /* PSA_CRYPTO_H */