blob: e43a301a0a728526f7523b4debd14b70f2313c5a [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
186 * with psa_generate_derived_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(),
189 * psa_generate_derived_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.
210 *
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(),
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100220 * psa_generate_derived_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 Peskinedc8219a2019-05-15 16:11:15 +0200245 * psa_generate_derived_key() or psa_copy_key().
246 *
247 * This function may be declared as `static` (i.e. without external
248 * linkage). This function may be provided as a function-like macro,
249 * but in this case it must evaluate 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
Gilles Peskine20628592019-04-19 19:29:50 +0200337/** Retrieve the algorithm policy from key attributes.
338 *
339 * This function may be declared as `static` (i.e. without external
340 * linkage). This function may be provided as a function-like macro,
341 * but in this case it must evaluate its argument exactly once.
342 *
343 * \param[in] attributes The key attribute structure to query.
344 *
345 * \return The algorithm stored in the attribute structure.
346 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200347static psa_algorithm_t psa_get_key_algorithm(
348 const psa_key_attributes_t *attributes);
349
Gilles Peskine20628592019-04-19 19:29:50 +0200350/** Declare the type of a key.
351 *
352 * If a type requires domain parameters, you must call
353 * psa_set_key_domain_parameters() instead of this function.
354 *
355 * This function overwrites any key type and domain parameters
356 * previously set in \p attributes.
357 *
358 * This function may be declared as `static` (i.e. without external
359 * linkage). This function may be provided as a function-like macro,
360 * but in this case it must evaluate each of its arguments exactly once.
361 *
362 * \param[out] attributes The attribute structure to write to.
363 * \param type The key type to write.
364 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200365static void psa_set_key_type(psa_key_attributes_t *attributes,
366 psa_key_type_t type);
367
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200368/** Declare the size of a key.
369 *
370 * This function overwrites any key size previously set in \p attributes.
371 *
372 * This function may be declared as `static` (i.e. without external
373 * linkage). This function may be provided as a function-like macro,
374 * but in this case it must evaluate each of its arguments exactly once.
375 *
376 * \param[out] attributes The attribute structure to write to.
377 * \param bits The key size in bits.
378 */
379static void psa_set_key_bits(psa_key_attributes_t *attributes,
380 size_t bits);
381
Gilles Peskine20628592019-04-19 19:29:50 +0200382/** Retrieve the key type from key attributes.
383 *
384 * This function may be declared as `static` (i.e. without external
385 * linkage). This function may be provided as a function-like macro,
386 * but in this case it must evaluate its argument exactly once.
387 *
388 * \param[in] attributes The key attribute structure to query.
389 *
390 * \return The key type stored in the attribute structure.
391 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200392static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
393
Gilles Peskine20628592019-04-19 19:29:50 +0200394/** Retrieve the key size from key attributes.
395 *
396 * This function may be declared as `static` (i.e. without external
397 * linkage). This function may be provided as a function-like macro,
398 * but in this case it must evaluate its argument exactly once.
399 *
400 * \param[in] attributes The key attribute structure to query.
401 *
402 * \return The key size stored in the attribute structure, in bits.
403 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200404static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
405
Gilles Peskineb699f072019-04-26 16:06:02 +0200406/**
407 * \brief Set domain parameters for a key.
408 *
409 * Some key types require additional domain parameters in addition to
410 * the key type identifier and the key size.
411 * The format for the required domain parameters varies by the key type.
412 *
Gilles Peskinee56e8782019-04-26 17:34:02 +0200413 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEYPAIR),
414 * the domain parameter data consists of the public exponent,
Gilles Peskineb699f072019-04-26 16:06:02 +0200415 * represented as a big-endian integer with no leading zeros.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200416 * This information is used when generating an RSA key pair.
Gilles Peskineb699f072019-04-26 16:06:02 +0200417 * When importing a key, the public exponent is read from the imported
418 * key data and the exponent recorded in the attribute structure is ignored.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200419 * As an exception, the public exponent 65537 is represented by an empty
420 * byte string.
421 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEYPAIR),
Gilles Peskineb699f072019-04-26 16:06:02 +0200422 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
423 * ```
424 * Dss-Parms ::= SEQUENCE {
425 * p INTEGER,
426 * q INTEGER,
427 * g INTEGER
428 * }
429 * ```
Gilles Peskinee56e8782019-04-26 17:34:02 +0200430 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY or
431 * #PSA_KEY_TYPE_DH_KEYPAIR), the
Gilles Peskineb699f072019-04-26 16:06:02 +0200432 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
433 * ```
434 * DomainParameters ::= SEQUENCE {
435 * p INTEGER, -- odd prime, p=jq +1
436 * g INTEGER, -- generator, g
437 * q INTEGER, -- factor of p-1
438 * j INTEGER OPTIONAL, -- subgroup factor
439 * validationParms ValidationParms OPTIONAL
440 * }
441 * ValidationParms ::= SEQUENCE {
442 * seed BIT STRING,
443 * pgenCounter INTEGER
444 * }
445 * ```
446 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200447 * \note This function may allocate memory or other resources.
448 * Once you have called this function on an attribute structure,
449 * you must call psa_reset_key_attributes() to free these resources.
450 *
Gilles Peskineb699f072019-04-26 16:06:02 +0200451 * \param[in,out] attributes Attribute structure where the specified domain
452 * parameters will be stored.
453 * If this function fails, the content of
454 * \p attributes is not modified.
455 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
456 * \param[in] data Buffer containing the key domain parameters.
457 * The content of this buffer is interpreted
458 * according to \p type as described above.
459 * \param data_length Size of the \p data buffer in bytes.
460 *
461 * \retval #PSA_SUCCESS
462 * \retval #PSA_ERROR_INVALID_ARGUMENT
463 * \retval #PSA_ERROR_NOT_SUPPORTED
464 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
465 */
466psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
467 psa_key_type_t type,
468 const uint8_t *data,
469 size_t data_length);
470
471/**
472 * \brief Get domain parameters for a key.
473 *
474 * Get the domain parameters for a key with this function, if any. The format
475 * of the domain parameters written to \p data is specified in the
476 * documentation for psa_set_key_domain_parameters().
477 *
478 * \param[in] attributes The key attribute structure to query.
479 * \param[out] data On success, the key domain parameters.
480 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineaa02c172019-04-28 11:44:17 +0200481 * The buffer is guaranteed to be large
482 * enough if its size in bytes is at least
483 * the value given by
484 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
Gilles Peskineb699f072019-04-26 16:06:02 +0200485 * \param[out] data_length On success, the number of bytes
486 * that make up the key domain parameters data.
487 *
488 * \retval #PSA_SUCCESS
489 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
490 */
491psa_status_t psa_get_key_domain_parameters(
492 const psa_key_attributes_t *attributes,
493 uint8_t *data,
494 size_t data_size,
495 size_t *data_length);
496
Gilles Peskine20628592019-04-19 19:29:50 +0200497/** Retrieve the attributes of a key.
498 *
499 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200500 * psa_reset_key_attributes(). It then copies the attributes of
501 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200502 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200503 * \note This function may allocate memory or other resources.
504 * Once you have called this function on an attribute structure,
505 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200506 *
Gilles Peskine20628592019-04-19 19:29:50 +0200507 * \param[in] handle Handle to the key to query.
508 * \param[in,out] attributes On success, the attributes of the key.
509 * On failure, equivalent to a
510 * freshly-initialized structure.
511 *
512 * \retval #PSA_SUCCESS
513 * \retval #PSA_ERROR_INVALID_HANDLE
514 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
515 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
516 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200517psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
518 psa_key_attributes_t *attributes);
519
Gilles Peskine20628592019-04-19 19:29:50 +0200520/** Reset a key attribute structure to a freshly initialized state.
521 *
522 * You must initialize the attribute structure as described in the
523 * documentation of the type #psa_key_attributes_t before calling this
524 * function. Once the structure has been initialized, you may call this
525 * function at any time.
526 *
527 * This function frees any auxiliary resources that the structure
528 * may contain.
529 *
530 * \param[in,out] attributes The attribute structure to reset.
531 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200532void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200533
Gilles Peskine87a5e562019-04-17 12:28:25 +0200534/**@}*/
535
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100536/** \defgroup key_management Key management
537 * @{
538 */
539
Gilles Peskinef535eb22018-11-30 14:08:36 +0100540/** Open a handle to an existing persistent key.
541 *
542 * Open a handle to a key which was previously created with psa_create_key().
543 *
544 * \param lifetime The lifetime of the key. This designates a storage
545 * area where the key material is stored. This must not
546 * be #PSA_KEY_LIFETIME_VOLATILE.
547 * \param id The persistent identifier of the key.
548 * \param[out] handle On success, a handle to a key slot which contains
549 * the data and metadata loaded from the specified
550 * persistent location.
551 *
552 * \retval #PSA_SUCCESS
553 * Success. The application can now use the value of `*handle`
554 * to access the newly allocated key slot.
555 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200556 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100557 * \retval #PSA_ERROR_INVALID_ARGUMENT
558 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
559 * \retval #PSA_ERROR_INVALID_ARGUMENT
560 * \p id is invalid for the specified lifetime.
561 * \retval #PSA_ERROR_NOT_SUPPORTED
562 * \p lifetime is not supported.
563 * \retval #PSA_ERROR_NOT_PERMITTED
564 * The specified key exists, but the application does not have the
565 * permission to access it. Note that this specification does not
566 * define any way to create such a key, but it may be possible
567 * through implementation-specific means.
568 */
569psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
570 psa_key_id_t id,
571 psa_key_handle_t *handle);
572
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
579 * with the key in volatile memory. The key slot in persistent storage is
580 * 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 *
Gilles Peskine20628592019-04-19 19:29:50 +0200614 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200615 * The key size is always determined from the
616 * \p data buffer.
617 * If the key size in \p attributes is nonzero,
618 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200619 * \param[out] handle On success, a handle to the newly created key.
620 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100621 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine20628592019-04-19 19:29:50 +0200622 * buffer is interpreted according to the type and,
623 * if applicable, domain parameters declared in
624 * \p attributes.
625 * All implementations must support at least the format
626 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100627 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200628 * the chosen type. Implementations may allow other
629 * formats, but should be conservative: implementations
630 * should err on the side of rejecting content if it
631 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200632 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100633 *
Gilles Peskine28538492018-07-11 17:34:00 +0200634 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100635 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100636 * If the key is persistent, the key material and the key's metadata
637 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200638 * \retval #PSA_ERROR_ALREADY_EXISTS
639 * This is an attempt to create a persistent key, and there is
640 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200641 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200642 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200643 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200644 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200645 * The key attributes, as a whole, are invalid.
646 * \retval #PSA_ERROR_INVALID_ARGUMENT
647 * The key data is not correctly formatted.
648 * \retval #PSA_ERROR_INVALID_ARGUMENT
649 * The size in \p attributes is nonzero and does not match the size
650 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200651 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
652 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
653 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100654 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200655 * \retval #PSA_ERROR_HARDWARE_FAILURE
656 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300657 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300658 * The library has not been previously initialized by psa_crypto_init().
659 * It is implementation-dependent whether a failure to initialize
660 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100661 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200662psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
663 psa_key_handle_t *handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100664 const uint8_t *data,
665 size_t data_length);
666
667/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100668 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200669 *
670 * This function destroys the content of the key slot from both volatile
671 * memory and, if applicable, non-volatile storage. Implementations shall
672 * make a best effort to ensure that any previous content of the slot is
673 * unrecoverable.
674 *
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 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100678 * If the key is currently in use in a multipart operation,
679 * the multipart operation is aborted.
680 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100681 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100682 *
Gilles Peskine28538492018-07-11 17:34:00 +0200683 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200684 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200685 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200686 * The slot holds content and cannot be erased because it is
687 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100688 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200689 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200690 * There was an failure in communication with the cryptoprocessor.
691 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200692 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200693 * The storage is corrupted. Implementations shall make a best effort
694 * to erase key material even in this stage, however applications
695 * should be aware that it may be impossible to guarantee that the
696 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200697 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200698 * An unexpected condition which is not a storage corruption or
699 * a communication failure occurred. The cryptoprocessor may have
700 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300701 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300702 * The library has not been previously initialized by psa_crypto_init().
703 * It is implementation-dependent whether a failure to initialize
704 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100705 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100706psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100707
708/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100709 * \brief Export a key in binary format.
710 *
711 * The output of this function can be passed to psa_import_key() to
712 * create an equivalent object.
713 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100714 * If the implementation of psa_import_key() supports other formats
715 * beyond the format specified here, the output from psa_export_key()
716 * must use the representation specified here, not the original
717 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100718 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100719 * For standard key types, the output format is as follows:
720 *
721 * - For symmetric keys (including MAC keys), the format is the
722 * raw bytes of the key.
723 * - For DES, the key data consists of 8 bytes. The parity bits must be
724 * correct.
725 * - For Triple-DES, the format is the concatenation of the
726 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100727 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200728 * is the non-encrypted DER encoding of the representation defined by
729 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
730 * ```
731 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200732 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200733 * modulus INTEGER, -- n
734 * publicExponent INTEGER, -- e
735 * privateExponent INTEGER, -- d
736 * prime1 INTEGER, -- p
737 * prime2 INTEGER, -- q
738 * exponent1 INTEGER, -- d mod (p-1)
739 * exponent2 INTEGER, -- d mod (q-1)
740 * coefficient INTEGER, -- (inverse of q) mod p
741 * }
742 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000743 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
744 * representation of the private key `x` as a big-endian byte string. The
745 * length of the byte string is the private key size in bytes (leading zeroes
746 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200747 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100748 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100749 * a representation of the private value as a `ceiling(m/8)`-byte string
750 * where `m` is the bit size associated with the curve, i.e. the bit size
751 * of the order of the curve's coordinate field. This byte string is
752 * in little-endian order for Montgomery curves (curve types
753 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
754 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
755 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100756 * This is the content of the `privateKey` field of the `ECPrivateKey`
757 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000758 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
759 * format is the representation of the private key `x` as a big-endian byte
760 * string. The length of the byte string is the private key size in bytes
761 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200762 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
763 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100764 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100765 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200766 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200767 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200768 * \param[out] data_length On success, the number of bytes
769 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100770 *
Gilles Peskine28538492018-07-11 17:34:00 +0200771 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100772 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200773 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200774 * \retval #PSA_ERROR_NOT_PERMITTED
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 Peskineae32aac2018-11-30 14:39:32 +0100833 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200834 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200835 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200836 * \param[out] data_length On success, the number of bytes
837 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100838 *
Gilles Peskine28538492018-07-11 17:34:00 +0200839 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100840 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200841 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200842 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200843 * The key is neither a public key nor a key pair.
844 * \retval #PSA_ERROR_NOT_SUPPORTED
845 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
846 * The size of the \p data buffer is too small. You can determine a
847 * sufficient buffer size by calling
848 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
849 * where \c type is the key type
850 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200851 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
852 * \retval #PSA_ERROR_HARDWARE_FAILURE
853 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300854 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300855 * The library has not been previously initialized by psa_crypto_init().
856 * It is implementation-dependent whether a failure to initialize
857 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100858 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100859psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100860 uint8_t *data,
861 size_t data_size,
862 size_t *data_length);
863
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100864/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100865 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100866 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000867 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100868 * This function is primarily useful to copy a key from one location
869 * to another, since it populates a key using the material from
870 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200871 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100872 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100873 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100874 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100875 *
Gilles Peskine20628592019-04-19 19:29:50 +0200876 * The resulting key may only be used in a way that conforms to
877 * both the policy of the original key and the policy specified in
878 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100879 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200880 * usage flags on the source policy and the usage flags in \p attributes.
881 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100882 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200883 * - If either of the policies allows an algorithm and the other policy
884 * allows a wildcard-based algorithm policy that includes this algorithm,
885 * the resulting key allows the same algorithm.
886 * - If the policies do not allow any algorithm in common, this function
887 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200888 *
Gilles Peskine20628592019-04-19 19:29:50 +0200889 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100890 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200891 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100892 * \param source_handle The key to copy. It must be a handle to an
893 * occupied slot.
Gilles Peskine20628592019-04-19 19:29:50 +0200894 * \param[in] attributes The attributes for the new key.
895 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200896 * - The key type and size may be 0. If either is
897 * nonzero, it must match the corresponding
898 * attribute of the source key.
899 * - If \p attributes contains domain parameters,
900 * they must match the domain parameters of
901 * the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200902 * - The key location (the lifetime and, for
903 * persistent keys, the key identifier) is
904 * used directly.
905 * - The policy constraints (usage flags and
906 * algorithm policy) are combined from
907 * the source key and \p attributes so that
908 * both sets of restrictions apply, as
909 * described in the documentation of this function.
910 * \param[out] target_handle On success, a handle to the newly created key.
911 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200912 *
913 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100914 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200915 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200916 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200917 * This is an attempt to create a persistent key, and there is
918 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200919 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200920 * The lifetime or identifier in \p attributes are invalid.
921 * \retval #PSA_ERROR_INVALID_ARGUMENT
922 * The policy constraints on the source and specified in
923 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200924 * \retval #PSA_ERROR_INVALID_ARGUMENT
925 * \p attributes specifies a key type, domain parameters or key size
926 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100927 * \retval #PSA_ERROR_NOT_PERMITTED
928 * The source key is not exportable and its lifetime does not
929 * allow copying it to the target's lifetime.
930 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
931 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200932 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
933 * \retval #PSA_ERROR_HARDWARE_FAILURE
934 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100935 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100936psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200937 const psa_key_attributes_t *attributes,
938 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100939
940/**@}*/
941
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100942/** \defgroup hash Message digests
943 * @{
944 */
945
Gilles Peskine69647a42019-01-14 20:18:12 +0100946/** Calculate the hash (digest) of a message.
947 *
948 * \note To verify the hash of a message against an
949 * expected value, use psa_hash_compare() instead.
950 *
951 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
952 * such that #PSA_ALG_IS_HASH(\p alg) is true).
953 * \param[in] input Buffer containing the message to hash.
954 * \param input_length Size of the \p input buffer in bytes.
955 * \param[out] hash Buffer where the hash is to be written.
956 * \param hash_size Size of the \p hash buffer in bytes.
957 * \param[out] hash_length On success, the number of bytes
958 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100959 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100960 *
961 * \retval #PSA_SUCCESS
962 * Success.
963 * \retval #PSA_ERROR_NOT_SUPPORTED
964 * \p alg is not supported or is not a hash algorithm.
965 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
966 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
967 * \retval #PSA_ERROR_HARDWARE_FAILURE
968 * \retval #PSA_ERROR_TAMPERING_DETECTED
969 */
970psa_status_t psa_hash_compute(psa_algorithm_t alg,
971 const uint8_t *input,
972 size_t input_length,
973 uint8_t *hash,
974 size_t hash_size,
975 size_t *hash_length);
976
977/** Calculate the hash (digest) of a message and compare it with a
978 * reference value.
979 *
980 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
981 * such that #PSA_ALG_IS_HASH(\p alg) is true).
982 * \param[in] input Buffer containing the message to hash.
983 * \param input_length Size of the \p input buffer in bytes.
984 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100985 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100986 *
987 * \retval #PSA_SUCCESS
988 * The expected hash is identical to the actual hash of the input.
989 * \retval #PSA_ERROR_INVALID_SIGNATURE
990 * The hash of the message was calculated successfully, but it
991 * differs from the expected hash.
992 * \retval #PSA_ERROR_NOT_SUPPORTED
993 * \p alg is not supported or is not a hash algorithm.
994 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
995 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
996 * \retval #PSA_ERROR_HARDWARE_FAILURE
997 * \retval #PSA_ERROR_TAMPERING_DETECTED
998 */
999psa_status_t psa_hash_compare(psa_algorithm_t alg,
1000 const uint8_t *input,
1001 size_t input_length,
1002 const uint8_t *hash,
1003 const size_t hash_length);
1004
Gilles Peskine308b91d2018-02-08 09:47:44 +01001005/** The type of the state data structure for multipart hash operations.
1006 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001007 * Before calling any function on a hash operation object, the application must
1008 * initialize it by any of the following means:
1009 * - Set the structure to all-bits-zero, for example:
1010 * \code
1011 * psa_hash_operation_t operation;
1012 * memset(&operation, 0, sizeof(operation));
1013 * \endcode
1014 * - Initialize the structure to logical zero values, for example:
1015 * \code
1016 * psa_hash_operation_t operation = {0};
1017 * \endcode
1018 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
1019 * for example:
1020 * \code
1021 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
1022 * \endcode
1023 * - Assign the result of the function psa_hash_operation_init()
1024 * to the structure, for example:
1025 * \code
1026 * psa_hash_operation_t operation;
1027 * operation = psa_hash_operation_init();
1028 * \endcode
1029 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001030 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001031 * make any assumptions about the content of this structure except
1032 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001033typedef struct psa_hash_operation_s psa_hash_operation_t;
1034
Jaeden Amero6a25b412019-01-04 11:47:44 +00001035/** \def PSA_HASH_OPERATION_INIT
1036 *
1037 * This macro returns a suitable initializer for a hash operation object
1038 * of type #psa_hash_operation_t.
1039 */
1040#ifdef __DOXYGEN_ONLY__
1041/* This is an example definition for documentation purposes.
1042 * Implementations should define a suitable value in `crypto_struct.h`.
1043 */
1044#define PSA_HASH_OPERATION_INIT {0}
1045#endif
1046
1047/** Return an initial value for a hash operation object.
1048 */
1049static psa_hash_operation_t psa_hash_operation_init(void);
1050
Gilles Peskinef45adda2019-01-14 18:29:18 +01001051/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001052 *
1053 * The sequence of operations to calculate a hash (message digest)
1054 * is as follows:
1055 * -# Allocate an operation object which will be passed to all the functions
1056 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +00001057 * -# Initialize the operation object with one of the methods described in the
1058 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001059 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001060 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001061 * of the message each time. The hash that is calculated is the hash
1062 * of the concatenation of these messages in order.
1063 * -# To calculate the hash, call psa_hash_finish().
1064 * To compare the hash with an expected value, call psa_hash_verify().
1065 *
1066 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +00001067 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001068 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001069 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001070 * eventually terminate the operation. The following events terminate an
1071 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001072 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001073 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001075 * \param[in,out] operation The operation object to set up. It must have
1076 * been initialized as per the documentation for
1077 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001078 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1079 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001080 *
Gilles Peskine28538492018-07-11 17:34:00 +02001081 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001082 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001083 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001084 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001085 * \retval #PSA_ERROR_BAD_STATE
1086 * The operation state is not valid (already set up and not
1087 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001088 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1089 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1090 * \retval #PSA_ERROR_HARDWARE_FAILURE
1091 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001092 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001093psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001094 psa_algorithm_t alg);
1095
Gilles Peskine308b91d2018-02-08 09:47:44 +01001096/** Add a message fragment to a multipart hash operation.
1097 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001098 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001099 *
1100 * If this function returns an error status, the operation becomes inactive.
1101 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001102 * \param[in,out] operation Active hash operation.
1103 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001104 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001105 *
Gilles Peskine28538492018-07-11 17:34:00 +02001106 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001107 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001108 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001109 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001110 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1111 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1112 * \retval #PSA_ERROR_HARDWARE_FAILURE
1113 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001114 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001115psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1116 const uint8_t *input,
1117 size_t input_length);
1118
Gilles Peskine308b91d2018-02-08 09:47:44 +01001119/** Finish the calculation of the hash of a message.
1120 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001121 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001122 * This function calculates the hash of the message formed by concatenating
1123 * the inputs passed to preceding calls to psa_hash_update().
1124 *
1125 * When this function returns, the operation becomes inactive.
1126 *
1127 * \warning Applications should not call this function if they expect
1128 * a specific value for the hash. Call psa_hash_verify() instead.
1129 * Beware that comparing integrity or authenticity data such as
1130 * hash values with a function such as \c memcmp is risky
1131 * because the time taken by the comparison may leak information
1132 * about the hashed data which could allow an attacker to guess
1133 * a valid hash and thereby bypass security controls.
1134 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001135 * \param[in,out] operation Active hash operation.
1136 * \param[out] hash Buffer where the hash is to be written.
1137 * \param hash_size Size of the \p hash buffer in bytes.
1138 * \param[out] hash_length On success, the number of bytes
1139 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001140 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001141 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001142 *
Gilles Peskine28538492018-07-11 17:34:00 +02001143 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001144 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001145 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001146 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001147 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001148 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001149 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001150 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001151 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1152 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1153 * \retval #PSA_ERROR_HARDWARE_FAILURE
1154 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001155 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001156psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1157 uint8_t *hash,
1158 size_t hash_size,
1159 size_t *hash_length);
1160
Gilles Peskine308b91d2018-02-08 09:47:44 +01001161/** Finish the calculation of the hash of a message and compare it with
1162 * an expected value.
1163 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001164 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001165 * This function calculates the hash of the message formed by concatenating
1166 * the inputs passed to preceding calls to psa_hash_update(). It then
1167 * compares the calculated hash with the expected hash passed as a
1168 * parameter to this function.
1169 *
1170 * When this function returns, the operation becomes inactive.
1171 *
Gilles Peskine19067982018-03-20 17:54:53 +01001172 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001173 * comparison between the actual hash and the expected hash is performed
1174 * in constant time.
1175 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001176 * \param[in,out] operation Active hash operation.
1177 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001178 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001179 *
Gilles Peskine28538492018-07-11 17:34:00 +02001180 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001181 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001182 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001183 * The hash of the message was calculated successfully, but it
1184 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001185 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001186 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001187 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1188 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1189 * \retval #PSA_ERROR_HARDWARE_FAILURE
1190 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001191 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001192psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1193 const uint8_t *hash,
1194 size_t hash_length);
1195
Gilles Peskine308b91d2018-02-08 09:47:44 +01001196/** Abort a hash operation.
1197 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001198 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001199 * \p operation structure itself. Once aborted, the operation object
1200 * can be reused for another operation by calling
1201 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001202 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001203 * You may call this function any time after the operation object has
1204 * been initialized by any of the following methods:
1205 * - A call to psa_hash_setup(), whether it succeeds or not.
1206 * - Initializing the \c struct to all-bits-zero.
1207 * - Initializing the \c struct to logical zeros, e.g.
1208 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001209 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001210 * In particular, calling psa_hash_abort() after the operation has been
1211 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1212 * psa_hash_verify() is safe and has no effect.
1213 *
1214 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001215 *
Gilles Peskine28538492018-07-11 17:34:00 +02001216 * \retval #PSA_SUCCESS
1217 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001218 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001219 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1220 * \retval #PSA_ERROR_HARDWARE_FAILURE
1221 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001222 */
1223psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001224
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001225/** Clone a hash operation.
1226 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001227 * This function copies the state of an ongoing hash operation to
1228 * a new operation object. In other words, this function is equivalent
1229 * to calling psa_hash_setup() on \p target_operation with the same
1230 * algorithm that \p source_operation was set up for, then
1231 * psa_hash_update() on \p target_operation with the same input that
1232 * that was passed to \p source_operation. After this function returns, the
1233 * two objects are independent, i.e. subsequent calls involving one of
1234 * the objects do not affect the other object.
1235 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001236 * \param[in] source_operation The active hash operation to clone.
1237 * \param[in,out] target_operation The operation object to set up.
1238 * It must be initialized but not active.
1239 *
1240 * \retval #PSA_SUCCESS
1241 * \retval #PSA_ERROR_BAD_STATE
1242 * \p source_operation is not an active hash operation.
1243 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001244 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001245 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1246 * \retval #PSA_ERROR_HARDWARE_FAILURE
1247 * \retval #PSA_ERROR_TAMPERING_DETECTED
1248 */
1249psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1250 psa_hash_operation_t *target_operation);
1251
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001252/**@}*/
1253
Gilles Peskine8c9def32018-02-08 10:02:12 +01001254/** \defgroup MAC Message authentication codes
1255 * @{
1256 */
1257
Gilles Peskine69647a42019-01-14 20:18:12 +01001258/** Calculate the MAC (message authentication code) of a message.
1259 *
1260 * \note To verify the MAC of a message against an
1261 * expected value, use psa_mac_verify() instead.
1262 * Beware that comparing integrity or authenticity data such as
1263 * MAC values with a function such as \c memcmp is risky
1264 * because the time taken by the comparison may leak information
1265 * about the MAC value which could allow an attacker to guess
1266 * a valid MAC and thereby bypass security controls.
1267 *
1268 * \param handle Handle to the key to use for the operation.
1269 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001270 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001271 * \param[in] input Buffer containing the input message.
1272 * \param input_length Size of the \p input buffer in bytes.
1273 * \param[out] mac Buffer where the MAC value is to be written.
1274 * \param mac_size Size of the \p mac buffer in bytes.
1275 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001276 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001277 *
1278 * \retval #PSA_SUCCESS
1279 * Success.
1280 * \retval #PSA_ERROR_INVALID_HANDLE
1281 * \retval #PSA_ERROR_EMPTY_SLOT
1282 * \retval #PSA_ERROR_NOT_PERMITTED
1283 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001284 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001285 * \retval #PSA_ERROR_NOT_SUPPORTED
1286 * \p alg is not supported or is not a MAC algorithm.
1287 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1288 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1289 * \retval #PSA_ERROR_HARDWARE_FAILURE
1290 * \retval #PSA_ERROR_TAMPERING_DETECTED
1291 * \retval #PSA_ERROR_BAD_STATE
1292 * The library has not been previously initialized by psa_crypto_init().
1293 * It is implementation-dependent whether a failure to initialize
1294 * results in this error code.
1295 */
1296psa_status_t psa_mac_compute(psa_key_handle_t handle,
1297 psa_algorithm_t alg,
1298 const uint8_t *input,
1299 size_t input_length,
1300 uint8_t *mac,
1301 size_t mac_size,
1302 size_t *mac_length);
1303
1304/** Calculate the MAC of a message and compare it with a reference value.
1305 *
1306 * \param handle Handle to the key to use for the operation.
1307 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001308 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001309 * \param[in] input Buffer containing the input message.
1310 * \param input_length Size of the \p input buffer in bytes.
1311 * \param[out] mac Buffer containing the expected MAC value.
1312 * \param mac_length Size of the \p mac buffer in bytes.
1313 *
1314 * \retval #PSA_SUCCESS
1315 * The expected MAC is identical to the actual MAC of the input.
1316 * \retval #PSA_ERROR_INVALID_SIGNATURE
1317 * The MAC of the message was calculated successfully, but it
1318 * differs from the expected value.
1319 * \retval #PSA_ERROR_INVALID_HANDLE
1320 * \retval #PSA_ERROR_EMPTY_SLOT
1321 * \retval #PSA_ERROR_NOT_PERMITTED
1322 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001323 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001324 * \retval #PSA_ERROR_NOT_SUPPORTED
1325 * \p alg is not supported or is not a MAC algorithm.
1326 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1327 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1328 * \retval #PSA_ERROR_HARDWARE_FAILURE
1329 * \retval #PSA_ERROR_TAMPERING_DETECTED
1330 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001331psa_status_t psa_mac_verify(psa_key_handle_t handle,
1332 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001333 const uint8_t *input,
1334 size_t input_length,
1335 const uint8_t *mac,
1336 const size_t mac_length);
1337
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001338/** The type of the state data structure for multipart MAC operations.
1339 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001340 * Before calling any function on a MAC operation object, the application must
1341 * initialize it by any of the following means:
1342 * - Set the structure to all-bits-zero, for example:
1343 * \code
1344 * psa_mac_operation_t operation;
1345 * memset(&operation, 0, sizeof(operation));
1346 * \endcode
1347 * - Initialize the structure to logical zero values, for example:
1348 * \code
1349 * psa_mac_operation_t operation = {0};
1350 * \endcode
1351 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1352 * for example:
1353 * \code
1354 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1355 * \endcode
1356 * - Assign the result of the function psa_mac_operation_init()
1357 * to the structure, for example:
1358 * \code
1359 * psa_mac_operation_t operation;
1360 * operation = psa_mac_operation_init();
1361 * \endcode
1362 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001363 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001364 * make any assumptions about the content of this structure except
1365 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001366typedef struct psa_mac_operation_s psa_mac_operation_t;
1367
Jaeden Amero769ce272019-01-04 11:48:03 +00001368/** \def PSA_MAC_OPERATION_INIT
1369 *
1370 * This macro returns a suitable initializer for a MAC operation object of type
1371 * #psa_mac_operation_t.
1372 */
1373#ifdef __DOXYGEN_ONLY__
1374/* This is an example definition for documentation purposes.
1375 * Implementations should define a suitable value in `crypto_struct.h`.
1376 */
1377#define PSA_MAC_OPERATION_INIT {0}
1378#endif
1379
1380/** Return an initial value for a MAC operation object.
1381 */
1382static psa_mac_operation_t psa_mac_operation_init(void);
1383
Gilles Peskinef45adda2019-01-14 18:29:18 +01001384/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001385 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001386 * This function sets up the calculation of the MAC
1387 * (message authentication code) of a byte string.
1388 * To verify the MAC of a message against an
1389 * expected value, use psa_mac_verify_setup() instead.
1390 *
1391 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001392 * -# Allocate an operation object which will be passed to all the functions
1393 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001394 * -# Initialize the operation object with one of the methods described in the
1395 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001396 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001397 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1398 * of the message each time. The MAC that is calculated is the MAC
1399 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001400 * -# At the end of the message, call psa_mac_sign_finish() to finish
1401 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001402 *
1403 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001404 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001405 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001406 * After a successful call to psa_mac_sign_setup(), the application must
1407 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001408 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001409 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001410 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001411 * \param[in,out] operation The operation object to set up. It must have
1412 * been initialized as per the documentation for
1413 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001414 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001415 * It must remain valid until the operation
1416 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001417 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001418 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001419 *
Gilles Peskine28538492018-07-11 17:34:00 +02001420 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001421 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001422 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001423 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001424 * \retval #PSA_ERROR_NOT_PERMITTED
1425 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001426 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001427 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001428 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001429 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1430 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1431 * \retval #PSA_ERROR_HARDWARE_FAILURE
1432 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001433 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001434 * The operation state is not valid (already set up and not
1435 * subsequently completed).
1436 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001437 * The library has not been previously initialized by psa_crypto_init().
1438 * It is implementation-dependent whether a failure to initialize
1439 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001440 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001441psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001442 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001443 psa_algorithm_t alg);
1444
Gilles Peskinef45adda2019-01-14 18:29:18 +01001445/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001446 *
1447 * This function sets up the verification of the MAC
1448 * (message authentication code) of a byte string against an expected value.
1449 *
1450 * The sequence of operations to verify a MAC is as follows:
1451 * -# Allocate an operation object which will be passed to all the functions
1452 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001453 * -# Initialize the operation object with one of the methods described in the
1454 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001455 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001456 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1457 * of the message each time. The MAC that is calculated is the MAC
1458 * of the concatenation of these messages in order.
1459 * -# At the end of the message, call psa_mac_verify_finish() to finish
1460 * calculating the actual MAC of the message and verify it against
1461 * the expected value.
1462 *
1463 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001464 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001465 *
1466 * After a successful call to psa_mac_verify_setup(), the application must
1467 * eventually terminate the operation through one of the following methods:
1468 * - A failed call to psa_mac_update().
1469 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1470 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001471 * \param[in,out] operation The operation object to set up. It must have
1472 * been initialized as per the documentation for
1473 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001474 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001475 * It must remain valid until the operation
1476 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001477 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1478 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001479 *
Gilles Peskine28538492018-07-11 17:34:00 +02001480 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001481 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001482 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001483 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001484 * \retval #PSA_ERROR_NOT_PERMITTED
1485 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001486 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001487 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001488 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001489 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1490 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1491 * \retval #PSA_ERROR_HARDWARE_FAILURE
1492 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001493 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001494 * The operation state is not valid (already set up and not
1495 * subsequently completed).
1496 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001497 * The library has not been previously initialized by psa_crypto_init().
1498 * It is implementation-dependent whether a failure to initialize
1499 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001500 */
1501psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001502 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001503 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001504
Gilles Peskinedcd14942018-07-12 00:30:52 +02001505/** Add a message fragment to a multipart MAC operation.
1506 *
1507 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1508 * before calling this function.
1509 *
1510 * If this function returns an error status, the operation becomes inactive.
1511 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001512 * \param[in,out] operation Active MAC operation.
1513 * \param[in] input Buffer containing the message fragment to add to
1514 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001515 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001516 *
1517 * \retval #PSA_SUCCESS
1518 * Success.
1519 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001520 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001521 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1522 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1523 * \retval #PSA_ERROR_HARDWARE_FAILURE
1524 * \retval #PSA_ERROR_TAMPERING_DETECTED
1525 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001526psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1527 const uint8_t *input,
1528 size_t input_length);
1529
Gilles Peskinedcd14942018-07-12 00:30:52 +02001530/** Finish the calculation of the MAC of a message.
1531 *
1532 * The application must call psa_mac_sign_setup() before calling this function.
1533 * This function calculates the MAC of the message formed by concatenating
1534 * the inputs passed to preceding calls to psa_mac_update().
1535 *
1536 * When this function returns, the operation becomes inactive.
1537 *
1538 * \warning Applications should not call this function if they expect
1539 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1540 * Beware that comparing integrity or authenticity data such as
1541 * MAC values with a function such as \c memcmp is risky
1542 * because the time taken by the comparison may leak information
1543 * about the MAC value which could allow an attacker to guess
1544 * a valid MAC and thereby bypass security controls.
1545 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001546 * \param[in,out] operation Active MAC operation.
1547 * \param[out] mac Buffer where the MAC value is to be written.
1548 * \param mac_size Size of the \p mac buffer in bytes.
1549 * \param[out] mac_length On success, the number of bytes
1550 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001551 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001552 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001553 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001554 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001555 *
1556 * \retval #PSA_SUCCESS
1557 * Success.
1558 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001559 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001560 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001561 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001562 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1563 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1564 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1565 * \retval #PSA_ERROR_HARDWARE_FAILURE
1566 * \retval #PSA_ERROR_TAMPERING_DETECTED
1567 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001568psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1569 uint8_t *mac,
1570 size_t mac_size,
1571 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001572
Gilles Peskinedcd14942018-07-12 00:30:52 +02001573/** Finish the calculation of the MAC of a message and compare it with
1574 * an expected value.
1575 *
1576 * The application must call psa_mac_verify_setup() before calling this function.
1577 * This function calculates the MAC of the message formed by concatenating
1578 * the inputs passed to preceding calls to psa_mac_update(). It then
1579 * compares the calculated MAC with the expected MAC passed as a
1580 * parameter to this function.
1581 *
1582 * When this function returns, the operation becomes inactive.
1583 *
1584 * \note Implementations shall make the best effort to ensure that the
1585 * comparison between the actual MAC and the expected MAC is performed
1586 * in constant time.
1587 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001588 * \param[in,out] operation Active MAC operation.
1589 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001590 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001591 *
1592 * \retval #PSA_SUCCESS
1593 * The expected MAC is identical to the actual MAC of the message.
1594 * \retval #PSA_ERROR_INVALID_SIGNATURE
1595 * The MAC of the message was calculated successfully, but it
1596 * differs from the expected MAC.
1597 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001598 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001599 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1600 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1601 * \retval #PSA_ERROR_HARDWARE_FAILURE
1602 * \retval #PSA_ERROR_TAMPERING_DETECTED
1603 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001604psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1605 const uint8_t *mac,
1606 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001607
Gilles Peskinedcd14942018-07-12 00:30:52 +02001608/** Abort a MAC operation.
1609 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001610 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001611 * \p operation structure itself. Once aborted, the operation object
1612 * can be reused for another operation by calling
1613 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001614 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001615 * You may call this function any time after the operation object has
1616 * been initialized by any of the following methods:
1617 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1618 * it succeeds or not.
1619 * - Initializing the \c struct to all-bits-zero.
1620 * - Initializing the \c struct to logical zeros, e.g.
1621 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001622 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001623 * In particular, calling psa_mac_abort() after the operation has been
1624 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1625 * psa_mac_verify_finish() is safe and has no effect.
1626 *
1627 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001628 *
1629 * \retval #PSA_SUCCESS
1630 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001631 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001632 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1633 * \retval #PSA_ERROR_HARDWARE_FAILURE
1634 * \retval #PSA_ERROR_TAMPERING_DETECTED
1635 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001636psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1637
1638/**@}*/
1639
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001640/** \defgroup cipher Symmetric ciphers
1641 * @{
1642 */
1643
Gilles Peskine69647a42019-01-14 20:18:12 +01001644/** Encrypt a message using a symmetric cipher.
1645 *
1646 * This function encrypts a message with a random IV (initialization
1647 * vector).
1648 *
1649 * \param handle Handle to the key to use for the operation.
1650 * It must remain valid until the operation
1651 * terminates.
1652 * \param alg The cipher algorithm to compute
1653 * (\c PSA_ALG_XXX value such that
1654 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1655 * \param[in] input Buffer containing the message to encrypt.
1656 * \param input_length Size of the \p input buffer in bytes.
1657 * \param[out] output Buffer where the output is to be written.
1658 * The output contains the IV followed by
1659 * the ciphertext proper.
1660 * \param output_size Size of the \p output buffer in bytes.
1661 * \param[out] output_length On success, the number of bytes
1662 * that make up the output.
1663 *
1664 * \retval #PSA_SUCCESS
1665 * Success.
1666 * \retval #PSA_ERROR_INVALID_HANDLE
1667 * \retval #PSA_ERROR_EMPTY_SLOT
1668 * \retval #PSA_ERROR_NOT_PERMITTED
1669 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001670 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001671 * \retval #PSA_ERROR_NOT_SUPPORTED
1672 * \p alg is not supported or is not a cipher algorithm.
1673 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1674 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1675 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1676 * \retval #PSA_ERROR_HARDWARE_FAILURE
1677 * \retval #PSA_ERROR_TAMPERING_DETECTED
1678 */
1679psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1680 psa_algorithm_t alg,
1681 const uint8_t *input,
1682 size_t input_length,
1683 uint8_t *output,
1684 size_t output_size,
1685 size_t *output_length);
1686
1687/** Decrypt a message using a symmetric cipher.
1688 *
1689 * This function decrypts a message encrypted with a symmetric cipher.
1690 *
1691 * \param handle Handle to the key to use for the operation.
1692 * It must remain valid until the operation
1693 * terminates.
1694 * \param alg The cipher algorithm to compute
1695 * (\c PSA_ALG_XXX value such that
1696 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1697 * \param[in] input Buffer containing the message to decrypt.
1698 * This consists of the IV followed by the
1699 * ciphertext proper.
1700 * \param input_length Size of the \p input buffer in bytes.
1701 * \param[out] output Buffer where the plaintext is to be written.
1702 * \param output_size Size of the \p output buffer in bytes.
1703 * \param[out] output_length On success, the number of bytes
1704 * that make up the output.
1705 *
1706 * \retval #PSA_SUCCESS
1707 * Success.
1708 * \retval #PSA_ERROR_INVALID_HANDLE
1709 * \retval #PSA_ERROR_EMPTY_SLOT
1710 * \retval #PSA_ERROR_NOT_PERMITTED
1711 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001712 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001713 * \retval #PSA_ERROR_NOT_SUPPORTED
1714 * \p alg is not supported or is not a cipher algorithm.
1715 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1716 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1717 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1718 * \retval #PSA_ERROR_HARDWARE_FAILURE
1719 * \retval #PSA_ERROR_TAMPERING_DETECTED
1720 */
1721psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1722 psa_algorithm_t alg,
1723 const uint8_t *input,
1724 size_t input_length,
1725 uint8_t *output,
1726 size_t output_size,
1727 size_t *output_length);
1728
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001729/** The type of the state data structure for multipart cipher operations.
1730 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001731 * Before calling any function on a cipher operation object, the application
1732 * must initialize it by any of the following means:
1733 * - Set the structure to all-bits-zero, for example:
1734 * \code
1735 * psa_cipher_operation_t operation;
1736 * memset(&operation, 0, sizeof(operation));
1737 * \endcode
1738 * - Initialize the structure to logical zero values, for example:
1739 * \code
1740 * psa_cipher_operation_t operation = {0};
1741 * \endcode
1742 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1743 * for example:
1744 * \code
1745 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1746 * \endcode
1747 * - Assign the result of the function psa_cipher_operation_init()
1748 * to the structure, for example:
1749 * \code
1750 * psa_cipher_operation_t operation;
1751 * operation = psa_cipher_operation_init();
1752 * \endcode
1753 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001754 * This is an implementation-defined \c struct. Applications should not
1755 * make any assumptions about the content of this structure except
1756 * as directed by the documentation of a specific implementation. */
1757typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1758
Jaeden Amero5bae2272019-01-04 11:48:27 +00001759/** \def PSA_CIPHER_OPERATION_INIT
1760 *
1761 * This macro returns a suitable initializer for a cipher operation object of
1762 * type #psa_cipher_operation_t.
1763 */
1764#ifdef __DOXYGEN_ONLY__
1765/* This is an example definition for documentation purposes.
1766 * Implementations should define a suitable value in `crypto_struct.h`.
1767 */
1768#define PSA_CIPHER_OPERATION_INIT {0}
1769#endif
1770
1771/** Return an initial value for a cipher operation object.
1772 */
1773static psa_cipher_operation_t psa_cipher_operation_init(void);
1774
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001775/** Set the key for a multipart symmetric encryption operation.
1776 *
1777 * The sequence of operations to encrypt a message with a symmetric cipher
1778 * is as follows:
1779 * -# Allocate an operation object which will be passed to all the functions
1780 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001781 * -# Initialize the operation object with one of the methods described in the
1782 * documentation for #psa_cipher_operation_t, e.g.
1783 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001784 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001785 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001786 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001787 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001788 * requires a specific IV value.
1789 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1790 * of the message each time.
1791 * -# Call psa_cipher_finish().
1792 *
1793 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001794 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001795 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001796 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001797 * eventually terminate the operation. The following events terminate an
1798 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001799 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001800 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001801 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001802 * \param[in,out] operation The operation object to set up. It must have
1803 * been initialized as per the documentation for
1804 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001805 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001806 * It must remain valid until the operation
1807 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001808 * \param alg The cipher algorithm to compute
1809 * (\c PSA_ALG_XXX value such that
1810 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001811 *
Gilles Peskine28538492018-07-11 17:34:00 +02001812 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001813 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001814 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001815 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001816 * \retval #PSA_ERROR_NOT_PERMITTED
1817 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001818 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001819 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001820 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001821 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1822 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1823 * \retval #PSA_ERROR_HARDWARE_FAILURE
1824 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001825 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001826 * The operation state is not valid (already set up and not
1827 * subsequently completed).
1828 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001829 * The library has not been previously initialized by psa_crypto_init().
1830 * It is implementation-dependent whether a failure to initialize
1831 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001832 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001833psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001834 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001835 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001836
1837/** Set the key for a multipart symmetric decryption operation.
1838 *
1839 * The sequence of operations to decrypt a message with a symmetric cipher
1840 * is as follows:
1841 * -# Allocate an operation object which will be passed to all the functions
1842 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001843 * -# Initialize the operation object with one of the methods described in the
1844 * documentation for #psa_cipher_operation_t, e.g.
1845 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001846 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001847 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001848 * decryption. If the IV is prepended to the ciphertext, you can call
1849 * psa_cipher_update() on a buffer containing the IV followed by the
1850 * beginning of the message.
1851 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1852 * of the message each time.
1853 * -# Call psa_cipher_finish().
1854 *
1855 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001856 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001857 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001858 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001859 * eventually terminate the operation. The following events terminate an
1860 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001861 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001862 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001863 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001864 * \param[in,out] operation The operation object to set up. It must have
1865 * been initialized as per the documentation for
1866 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001867 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001868 * It must remain valid until the operation
1869 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001870 * \param alg The cipher algorithm to compute
1871 * (\c PSA_ALG_XXX value such that
1872 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001873 *
Gilles Peskine28538492018-07-11 17:34:00 +02001874 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001875 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001876 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001877 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001878 * \retval #PSA_ERROR_NOT_PERMITTED
1879 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001880 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001881 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001882 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001883 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1884 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1885 * \retval #PSA_ERROR_HARDWARE_FAILURE
1886 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001887 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001888 * The operation state is not valid (already set up and not
1889 * subsequently completed).
1890 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001891 * The library has not been previously initialized by psa_crypto_init().
1892 * It is implementation-dependent whether a failure to initialize
1893 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001894 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001895psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001896 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001897 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001898
Gilles Peskinedcd14942018-07-12 00:30:52 +02001899/** Generate an IV for a symmetric encryption operation.
1900 *
1901 * This function generates a random IV (initialization vector), nonce
1902 * or initial counter value for the encryption operation as appropriate
1903 * for the chosen algorithm, key type and key size.
1904 *
1905 * The application must call psa_cipher_encrypt_setup() before
1906 * calling this function.
1907 *
1908 * If this function returns an error status, the operation becomes inactive.
1909 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001910 * \param[in,out] operation Active cipher operation.
1911 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001912 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001913 * \param[out] iv_length On success, the number of bytes of the
1914 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001915 *
1916 * \retval #PSA_SUCCESS
1917 * Success.
1918 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001919 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001920 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001921 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001922 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1923 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1924 * \retval #PSA_ERROR_HARDWARE_FAILURE
1925 * \retval #PSA_ERROR_TAMPERING_DETECTED
1926 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001927psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1928 unsigned char *iv,
1929 size_t iv_size,
1930 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001931
Gilles Peskinedcd14942018-07-12 00:30:52 +02001932/** Set the IV for a symmetric encryption or decryption operation.
1933 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001934 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001935 * or initial counter value for the encryption or decryption operation.
1936 *
1937 * The application must call psa_cipher_encrypt_setup() before
1938 * calling this function.
1939 *
1940 * If this function returns an error status, the operation becomes inactive.
1941 *
1942 * \note When encrypting, applications should use psa_cipher_generate_iv()
1943 * instead of this function, unless implementing a protocol that requires
1944 * a non-random IV.
1945 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001946 * \param[in,out] operation Active cipher operation.
1947 * \param[in] iv Buffer containing the IV to use.
1948 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001949 *
1950 * \retval #PSA_SUCCESS
1951 * Success.
1952 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001953 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001954 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001955 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001956 * or the chosen algorithm does not use an IV.
1957 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1958 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1959 * \retval #PSA_ERROR_HARDWARE_FAILURE
1960 * \retval #PSA_ERROR_TAMPERING_DETECTED
1961 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001962psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1963 const unsigned char *iv,
1964 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001965
Gilles Peskinedcd14942018-07-12 00:30:52 +02001966/** Encrypt or decrypt a message fragment in an active cipher operation.
1967 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001968 * Before calling this function, you must:
1969 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1970 * The choice of setup function determines whether this function
1971 * encrypts or decrypts its input.
1972 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1973 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001974 *
1975 * If this function returns an error status, the operation becomes inactive.
1976 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001977 * \param[in,out] operation Active cipher operation.
1978 * \param[in] input Buffer containing the message fragment to
1979 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001980 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001981 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001982 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001983 * \param[out] output_length On success, the number of bytes
1984 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001985 *
1986 * \retval #PSA_SUCCESS
1987 * Success.
1988 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001989 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001990 * not set, or already completed).
1991 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1992 * The size of the \p output buffer is too small.
1993 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1994 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1995 * \retval #PSA_ERROR_HARDWARE_FAILURE
1996 * \retval #PSA_ERROR_TAMPERING_DETECTED
1997 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001998psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1999 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002000 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002001 unsigned char *output,
2002 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002003 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002004
Gilles Peskinedcd14942018-07-12 00:30:52 +02002005/** Finish encrypting or decrypting a message in a cipher operation.
2006 *
2007 * The application must call psa_cipher_encrypt_setup() or
2008 * psa_cipher_decrypt_setup() before calling this function. The choice
2009 * of setup function determines whether this function encrypts or
2010 * decrypts its input.
2011 *
2012 * This function finishes the encryption or decryption of the message
2013 * formed by concatenating the inputs passed to preceding calls to
2014 * psa_cipher_update().
2015 *
2016 * When this function returns, the operation becomes inactive.
2017 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002018 * \param[in,out] operation Active cipher operation.
2019 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002020 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002021 * \param[out] output_length On success, the number of bytes
2022 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002023 *
2024 * \retval #PSA_SUCCESS
2025 * Success.
2026 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002027 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02002028 * not set, or already completed).
2029 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2030 * The size of the \p output buffer is too small.
2031 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2032 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2033 * \retval #PSA_ERROR_HARDWARE_FAILURE
2034 * \retval #PSA_ERROR_TAMPERING_DETECTED
2035 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002036psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002037 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002038 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002039 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002040
Gilles Peskinedcd14942018-07-12 00:30:52 +02002041/** Abort a cipher operation.
2042 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002043 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002044 * \p operation structure itself. Once aborted, the operation object
2045 * can be reused for another operation by calling
2046 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002047 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002048 * You may call this function any time after the operation object has
2049 * been initialized by any of the following methods:
2050 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2051 * whether it succeeds or not.
2052 * - Initializing the \c struct to all-bits-zero.
2053 * - Initializing the \c struct to logical zeros, e.g.
2054 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002055 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002056 * In particular, calling psa_cipher_abort() after the operation has been
2057 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2058 * is safe and has no effect.
2059 *
2060 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002061 *
2062 * \retval #PSA_SUCCESS
2063 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002064 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002065 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2066 * \retval #PSA_ERROR_HARDWARE_FAILURE
2067 * \retval #PSA_ERROR_TAMPERING_DETECTED
2068 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002069psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2070
2071/**@}*/
2072
Gilles Peskine3b555712018-03-03 21:27:57 +01002073/** \defgroup aead Authenticated encryption with associated data (AEAD)
2074 * @{
2075 */
2076
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002077/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002078 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002079 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002080 * \param alg The AEAD algorithm to compute
2081 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002082 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002083 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002084 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002085 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002086 * but not encrypted.
2087 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002088 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002089 * encrypted.
2090 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002091 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002092 * encrypted data. The additional data is not
2093 * part of this output. For algorithms where the
2094 * encrypted data and the authentication tag
2095 * are defined as separate outputs, the
2096 * authentication tag is appended to the
2097 * encrypted data.
2098 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2099 * This must be at least
2100 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2101 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002102 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002103 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002104 *
Gilles Peskine28538492018-07-11 17:34:00 +02002105 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002106 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002107 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002108 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002109 * \retval #PSA_ERROR_NOT_PERMITTED
2110 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002111 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002112 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002113 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002114 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2115 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2116 * \retval #PSA_ERROR_HARDWARE_FAILURE
2117 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002118 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002119 * The library has not been previously initialized by psa_crypto_init().
2120 * It is implementation-dependent whether a failure to initialize
2121 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002122 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002123psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002124 psa_algorithm_t alg,
2125 const uint8_t *nonce,
2126 size_t nonce_length,
2127 const uint8_t *additional_data,
2128 size_t additional_data_length,
2129 const uint8_t *plaintext,
2130 size_t plaintext_length,
2131 uint8_t *ciphertext,
2132 size_t ciphertext_size,
2133 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002134
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002135/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002136 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002137 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002138 * \param alg The AEAD algorithm to compute
2139 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002140 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002141 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002142 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002143 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002144 * but not encrypted.
2145 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002146 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002147 * encrypted. For algorithms where the
2148 * encrypted data and the authentication tag
2149 * are defined as separate inputs, the buffer
2150 * must contain the encrypted data followed
2151 * by the authentication tag.
2152 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002153 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002154 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2155 * This must be at least
2156 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2157 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002158 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002159 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002160 *
Gilles Peskine28538492018-07-11 17:34:00 +02002161 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002162 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002163 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002164 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002165 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002166 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002167 * \retval #PSA_ERROR_NOT_PERMITTED
2168 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002169 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002170 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002171 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002172 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2173 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2174 * \retval #PSA_ERROR_HARDWARE_FAILURE
2175 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002176 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002177 * The library has not been previously initialized by psa_crypto_init().
2178 * It is implementation-dependent whether a failure to initialize
2179 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002180 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002181psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002182 psa_algorithm_t alg,
2183 const uint8_t *nonce,
2184 size_t nonce_length,
2185 const uint8_t *additional_data,
2186 size_t additional_data_length,
2187 const uint8_t *ciphertext,
2188 size_t ciphertext_length,
2189 uint8_t *plaintext,
2190 size_t plaintext_size,
2191 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002192
Gilles Peskine30a9e412019-01-14 18:36:12 +01002193/** The type of the state data structure for multipart AEAD operations.
2194 *
2195 * Before calling any function on an AEAD operation object, the application
2196 * must initialize it by any of the following means:
2197 * - Set the structure to all-bits-zero, for example:
2198 * \code
2199 * psa_aead_operation_t operation;
2200 * memset(&operation, 0, sizeof(operation));
2201 * \endcode
2202 * - Initialize the structure to logical zero values, for example:
2203 * \code
2204 * psa_aead_operation_t operation = {0};
2205 * \endcode
2206 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2207 * for example:
2208 * \code
2209 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2210 * \endcode
2211 * - Assign the result of the function psa_aead_operation_init()
2212 * to the structure, for example:
2213 * \code
2214 * psa_aead_operation_t operation;
2215 * operation = psa_aead_operation_init();
2216 * \endcode
2217 *
2218 * This is an implementation-defined \c struct. Applications should not
2219 * make any assumptions about the content of this structure except
2220 * as directed by the documentation of a specific implementation. */
2221typedef struct psa_aead_operation_s psa_aead_operation_t;
2222
2223/** \def PSA_AEAD_OPERATION_INIT
2224 *
2225 * This macro returns a suitable initializer for an AEAD operation object of
2226 * type #psa_aead_operation_t.
2227 */
2228#ifdef __DOXYGEN_ONLY__
2229/* This is an example definition for documentation purposes.
2230 * Implementations should define a suitable value in `crypto_struct.h`.
2231 */
2232#define PSA_AEAD_OPERATION_INIT {0}
2233#endif
2234
2235/** Return an initial value for an AEAD operation object.
2236 */
2237static psa_aead_operation_t psa_aead_operation_init(void);
2238
2239/** Set the key for a multipart authenticated encryption operation.
2240 *
2241 * The sequence of operations to encrypt a message with authentication
2242 * is as follows:
2243 * -# Allocate an operation object which will be passed to all the functions
2244 * listed here.
2245 * -# Initialize the operation object with one of the methods described in the
2246 * documentation for #psa_aead_operation_t, e.g.
2247 * PSA_AEAD_OPERATION_INIT.
2248 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002249 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2250 * inputs to the subsequent calls to psa_aead_update_ad() and
2251 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2252 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002253 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2254 * generate or set the nonce. You should use
2255 * psa_aead_generate_nonce() unless the protocol you are implementing
2256 * requires a specific nonce value.
2257 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2258 * of the non-encrypted additional authenticated data each time.
2259 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002260 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002261 * -# Call psa_aead_finish().
2262 *
2263 * The application may call psa_aead_abort() at any time after the operation
2264 * has been initialized.
2265 *
2266 * After a successful call to psa_aead_encrypt_setup(), the application must
2267 * eventually terminate the operation. The following events terminate an
2268 * operation:
2269 * - A failed call to any of the \c psa_aead_xxx functions.
2270 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2271 *
2272 * \param[in,out] operation The operation object to set up. It must have
2273 * been initialized as per the documentation for
2274 * #psa_aead_operation_t and not yet in use.
2275 * \param handle Handle to the key to use for the operation.
2276 * It must remain valid until the operation
2277 * terminates.
2278 * \param alg The AEAD algorithm to compute
2279 * (\c PSA_ALG_XXX value such that
2280 * #PSA_ALG_IS_AEAD(\p alg) is true).
2281 *
2282 * \retval #PSA_SUCCESS
2283 * Success.
2284 * \retval #PSA_ERROR_INVALID_HANDLE
2285 * \retval #PSA_ERROR_EMPTY_SLOT
2286 * \retval #PSA_ERROR_NOT_PERMITTED
2287 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002288 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002289 * \retval #PSA_ERROR_NOT_SUPPORTED
2290 * \p alg is not supported or is not an AEAD algorithm.
2291 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2292 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2293 * \retval #PSA_ERROR_HARDWARE_FAILURE
2294 * \retval #PSA_ERROR_TAMPERING_DETECTED
2295 * \retval #PSA_ERROR_BAD_STATE
2296 * The library has not been previously initialized by psa_crypto_init().
2297 * It is implementation-dependent whether a failure to initialize
2298 * results in this error code.
2299 */
2300psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2301 psa_key_handle_t handle,
2302 psa_algorithm_t alg);
2303
2304/** Set the key for a multipart authenticated decryption operation.
2305 *
2306 * The sequence of operations to decrypt a message with authentication
2307 * is as follows:
2308 * -# Allocate an operation object which will be passed to all the functions
2309 * listed here.
2310 * -# Initialize the operation object with one of the methods described in the
2311 * documentation for #psa_aead_operation_t, e.g.
2312 * PSA_AEAD_OPERATION_INIT.
2313 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002314 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2315 * inputs to the subsequent calls to psa_aead_update_ad() and
2316 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2317 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002318 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2319 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2320 * of the non-encrypted additional authenticated data each time.
2321 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002322 * of the ciphertext to decrypt each time.
2323 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002324 *
2325 * The application may call psa_aead_abort() at any time after the operation
2326 * has been initialized.
2327 *
2328 * After a successful call to psa_aead_decrypt_setup(), the application must
2329 * eventually terminate the operation. The following events terminate an
2330 * operation:
2331 * - A failed call to any of the \c psa_aead_xxx functions.
2332 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2333 *
2334 * \param[in,out] operation The operation object to set up. It must have
2335 * been initialized as per the documentation for
2336 * #psa_aead_operation_t and not yet in use.
2337 * \param handle Handle to the key to use for the operation.
2338 * It must remain valid until the operation
2339 * terminates.
2340 * \param alg The AEAD algorithm to compute
2341 * (\c PSA_ALG_XXX value such that
2342 * #PSA_ALG_IS_AEAD(\p alg) is true).
2343 *
2344 * \retval #PSA_SUCCESS
2345 * Success.
2346 * \retval #PSA_ERROR_INVALID_HANDLE
2347 * \retval #PSA_ERROR_EMPTY_SLOT
2348 * \retval #PSA_ERROR_NOT_PERMITTED
2349 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002350 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002351 * \retval #PSA_ERROR_NOT_SUPPORTED
2352 * \p alg is not supported or is not an AEAD algorithm.
2353 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2354 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2355 * \retval #PSA_ERROR_HARDWARE_FAILURE
2356 * \retval #PSA_ERROR_TAMPERING_DETECTED
2357 * \retval #PSA_ERROR_BAD_STATE
2358 * The library has not been previously initialized by psa_crypto_init().
2359 * It is implementation-dependent whether a failure to initialize
2360 * results in this error code.
2361 */
2362psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2363 psa_key_handle_t handle,
2364 psa_algorithm_t alg);
2365
2366/** Generate a random nonce for an authenticated encryption operation.
2367 *
2368 * This function generates a random nonce for the authenticated encryption
2369 * operation with an appropriate size for the chosen algorithm, key type
2370 * and key size.
2371 *
2372 * The application must call psa_aead_encrypt_setup() before
2373 * calling this function.
2374 *
2375 * If this function returns an error status, the operation becomes inactive.
2376 *
2377 * \param[in,out] operation Active AEAD operation.
2378 * \param[out] nonce Buffer where the generated nonce is to be
2379 * written.
2380 * \param nonce_size Size of the \p nonce buffer in bytes.
2381 * \param[out] nonce_length On success, the number of bytes of the
2382 * generated nonce.
2383 *
2384 * \retval #PSA_SUCCESS
2385 * Success.
2386 * \retval #PSA_ERROR_BAD_STATE
2387 * The operation state is not valid (not set up, or nonce already set).
2388 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2389 * The size of the \p nonce buffer is too small.
2390 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2391 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2392 * \retval #PSA_ERROR_HARDWARE_FAILURE
2393 * \retval #PSA_ERROR_TAMPERING_DETECTED
2394 */
2395psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2396 unsigned char *nonce,
2397 size_t nonce_size,
2398 size_t *nonce_length);
2399
2400/** Set the nonce for an authenticated encryption or decryption operation.
2401 *
2402 * This function sets the nonce for the authenticated
2403 * encryption or decryption operation.
2404 *
2405 * The application must call psa_aead_encrypt_setup() before
2406 * calling this function.
2407 *
2408 * If this function returns an error status, the operation becomes inactive.
2409 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002410 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002411 * instead of this function, unless implementing a protocol that requires
2412 * a non-random IV.
2413 *
2414 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002415 * \param[in] nonce Buffer containing the nonce to use.
2416 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002417 *
2418 * \retval #PSA_SUCCESS
2419 * Success.
2420 * \retval #PSA_ERROR_BAD_STATE
2421 * The operation state is not valid (not set up, or nonce already set).
2422 * \retval #PSA_ERROR_INVALID_ARGUMENT
2423 * The size of \p nonce is not acceptable for the chosen algorithm.
2424 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2425 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2426 * \retval #PSA_ERROR_HARDWARE_FAILURE
2427 * \retval #PSA_ERROR_TAMPERING_DETECTED
2428 */
2429psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2430 const unsigned char *nonce,
2431 size_t nonce_length);
2432
Gilles Peskinebc59c852019-01-17 15:26:08 +01002433/** Declare the lengths of the message and additional data for AEAD.
2434 *
2435 * The application must call this function before calling
2436 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2437 * the operation requires it. If the algorithm does not require it,
2438 * calling this function is optional, but if this function is called
2439 * then the implementation must enforce the lengths.
2440 *
2441 * You may call this function before or after setting the nonce with
2442 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2443 *
2444 * - For #PSA_ALG_CCM, calling this function is required.
2445 * - For the other AEAD algorithms defined in this specification, calling
2446 * this function is not required.
2447 * - For vendor-defined algorithm, refer to the vendor documentation.
2448 *
2449 * \param[in,out] operation Active AEAD operation.
2450 * \param ad_length Size of the non-encrypted additional
2451 * authenticated data in bytes.
2452 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2453 *
2454 * \retval #PSA_SUCCESS
2455 * Success.
2456 * \retval #PSA_ERROR_BAD_STATE
2457 * The operation state is not valid (not set up, already completed,
2458 * or psa_aead_update_ad() or psa_aead_update() already called).
2459 * \retval #PSA_ERROR_INVALID_ARGUMENT
2460 * At least one of the lengths is not acceptable for the chosen
2461 * algorithm.
2462 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2463 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2464 * \retval #PSA_ERROR_HARDWARE_FAILURE
2465 * \retval #PSA_ERROR_TAMPERING_DETECTED
2466 */
2467psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2468 size_t ad_length,
2469 size_t plaintext_length);
2470
Gilles Peskine30a9e412019-01-14 18:36:12 +01002471/** Pass additional data to an active AEAD operation.
2472 *
2473 * Additional data is authenticated, but not encrypted.
2474 *
2475 * You may call this function multiple times to pass successive fragments
2476 * of the additional data. You may not call this function after passing
2477 * data to encrypt or decrypt with psa_aead_update().
2478 *
2479 * Before calling this function, you must:
2480 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2481 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2482 *
2483 * If this function returns an error status, the operation becomes inactive.
2484 *
2485 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2486 * there is no guarantee that the input is valid. Therefore, until
2487 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2488 * treat the input as untrusted and prepare to undo any action that
2489 * depends on the input if psa_aead_verify() returns an error status.
2490 *
2491 * \param[in,out] operation Active AEAD operation.
2492 * \param[in] input Buffer containing the fragment of
2493 * additional data.
2494 * \param input_length Size of the \p input buffer in bytes.
2495 *
2496 * \retval #PSA_SUCCESS
2497 * Success.
2498 * \retval #PSA_ERROR_BAD_STATE
2499 * The operation state is not valid (not set up, nonce not set,
2500 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002501 * \retval #PSA_ERROR_INVALID_ARGUMENT
2502 * The total input length overflows the additional data length that
2503 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002504 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2505 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2506 * \retval #PSA_ERROR_HARDWARE_FAILURE
2507 * \retval #PSA_ERROR_TAMPERING_DETECTED
2508 */
2509psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2510 const uint8_t *input,
2511 size_t input_length);
2512
2513/** Encrypt or decrypt a message fragment in an active AEAD operation.
2514 *
2515 * Before calling this function, you must:
2516 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2517 * The choice of setup function determines whether this function
2518 * encrypts or decrypts its input.
2519 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2520 * 3. Call psa_aead_update_ad() to pass all the additional data.
2521 *
2522 * If this function returns an error status, the operation becomes inactive.
2523 *
2524 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2525 * there is no guarantee that the input is valid. Therefore, until
2526 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2527 * - Do not use the output in any way other than storing it in a
2528 * confidential location. If you take any action that depends
2529 * on the tentative decrypted data, this action will need to be
2530 * undone if the input turns out not to be valid. Furthermore,
2531 * if an adversary can observe that this action took place
2532 * (for example through timing), they may be able to use this
2533 * fact as an oracle to decrypt any message encrypted with the
2534 * same key.
2535 * - In particular, do not copy the output anywhere but to a
2536 * memory or storage space that you have exclusive access to.
2537 *
2538 * \param[in,out] operation Active AEAD operation.
2539 * \param[in] input Buffer containing the message fragment to
2540 * encrypt or decrypt.
2541 * \param input_length Size of the \p input buffer in bytes.
2542 * \param[out] output Buffer where the output is to be written.
2543 * \param output_size Size of the \p output buffer in bytes.
2544 * \param[out] output_length On success, the number of bytes
2545 * that make up the returned output.
2546 *
2547 * \retval #PSA_SUCCESS
2548 * Success.
2549 * \retval #PSA_ERROR_BAD_STATE
2550 * The operation state is not valid (not set up, nonce not set
2551 * or already completed).
2552 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2553 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002554 * \retval #PSA_ERROR_INVALID_ARGUMENT
2555 * The total length of input to psa_aead_update_ad() so far is
2556 * less than the additional data length that was previously
2557 * specified with psa_aead_set_lengths().
2558 * \retval #PSA_ERROR_INVALID_ARGUMENT
2559 * The total input length overflows the plaintext length that
2560 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002561 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2562 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2563 * \retval #PSA_ERROR_HARDWARE_FAILURE
2564 * \retval #PSA_ERROR_TAMPERING_DETECTED
2565 */
2566psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2567 const uint8_t *input,
2568 size_t input_length,
2569 unsigned char *output,
2570 size_t output_size,
2571 size_t *output_length);
2572
2573/** Finish encrypting a message in an AEAD operation.
2574 *
2575 * The operation must have been set up with psa_aead_encrypt_setup().
2576 *
2577 * This function finishes the authentication of the additional data
2578 * formed by concatenating the inputs passed to preceding calls to
2579 * psa_aead_update_ad() with the plaintext formed by concatenating the
2580 * inputs passed to preceding calls to psa_aead_update().
2581 *
2582 * This function has two output buffers:
2583 * - \p ciphertext contains trailing ciphertext that was buffered from
2584 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2585 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2586 * will not contain any output and can be a 0-sized buffer.
2587 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002588 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002589 * that the operation performs.
2590 *
2591 * When this function returns, the operation becomes inactive.
2592 *
2593 * \param[in,out] operation Active AEAD operation.
2594 * \param[out] ciphertext Buffer where the last part of the ciphertext
2595 * is to be written.
2596 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2597 * \param[out] ciphertext_length On success, the number of bytes of
2598 * returned ciphertext.
2599 * \param[out] tag Buffer where the authentication tag is
2600 * to be written.
2601 * \param tag_size Size of the \p tag buffer in bytes.
2602 * \param[out] tag_length On success, the number of bytes
2603 * that make up the returned tag.
2604 *
2605 * \retval #PSA_SUCCESS
2606 * Success.
2607 * \retval #PSA_ERROR_BAD_STATE
2608 * The operation state is not valid (not set up, nonce not set,
2609 * decryption, or already completed).
2610 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002611 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002612 * \retval #PSA_ERROR_INVALID_ARGUMENT
2613 * The total length of input to psa_aead_update_ad() so far is
2614 * less than the additional data length that was previously
2615 * specified with psa_aead_set_lengths().
2616 * \retval #PSA_ERROR_INVALID_ARGUMENT
2617 * The total length of input to psa_aead_update() so far is
2618 * less than the plaintext length that was previously
2619 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002620 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2621 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2622 * \retval #PSA_ERROR_HARDWARE_FAILURE
2623 * \retval #PSA_ERROR_TAMPERING_DETECTED
2624 */
2625psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002626 uint8_t *ciphertext,
2627 size_t ciphertext_size,
2628 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002629 uint8_t *tag,
2630 size_t tag_size,
2631 size_t *tag_length);
2632
2633/** Finish authenticating and decrypting a message in an AEAD operation.
2634 *
2635 * The operation must have been set up with psa_aead_decrypt_setup().
2636 *
2637 * This function finishes the authentication of the additional data
2638 * formed by concatenating the inputs passed to preceding calls to
2639 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2640 * inputs passed to preceding calls to psa_aead_update().
2641 *
2642 * When this function returns, the operation becomes inactive.
2643 *
2644 * \param[in,out] operation Active AEAD operation.
2645 * \param[in] tag Buffer containing the authentication tag.
2646 * \param tag_length Size of the \p tag buffer in bytes.
2647 *
2648 * \retval #PSA_SUCCESS
2649 * Success.
2650 * \retval #PSA_ERROR_BAD_STATE
2651 * The operation state is not valid (not set up, nonce not set,
2652 * encryption, or already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002653 * \retval #PSA_ERROR_INVALID_ARGUMENT
2654 * The total length of input to psa_aead_update_ad() so far is
2655 * less than the additional data length that was previously
2656 * specified with psa_aead_set_lengths().
2657 * \retval #PSA_ERROR_INVALID_ARGUMENT
2658 * The total length of input to psa_aead_update() so far is
2659 * less than the plaintext length that was previously
2660 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002661 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2662 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2663 * \retval #PSA_ERROR_HARDWARE_FAILURE
2664 * \retval #PSA_ERROR_TAMPERING_DETECTED
2665 */
2666psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2667 const uint8_t *tag,
2668 size_t tag_length);
2669
2670/** Abort an AEAD operation.
2671 *
2672 * Aborting an operation frees all associated resources except for the
2673 * \p operation structure itself. Once aborted, the operation object
2674 * can be reused for another operation by calling
2675 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2676 *
2677 * You may call this function any time after the operation object has
2678 * been initialized by any of the following methods:
2679 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2680 * whether it succeeds or not.
2681 * - Initializing the \c struct to all-bits-zero.
2682 * - Initializing the \c struct to logical zeros, e.g.
2683 * `psa_aead_operation_t operation = {0}`.
2684 *
2685 * In particular, calling psa_aead_abort() after the operation has been
2686 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2687 * is safe and has no effect.
2688 *
2689 * \param[in,out] operation Initialized AEAD operation.
2690 *
2691 * \retval #PSA_SUCCESS
2692 * \retval #PSA_ERROR_BAD_STATE
2693 * \p operation is not an active AEAD operation.
2694 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2695 * \retval #PSA_ERROR_HARDWARE_FAILURE
2696 * \retval #PSA_ERROR_TAMPERING_DETECTED
2697 */
2698psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2699
Gilles Peskine3b555712018-03-03 21:27:57 +01002700/**@}*/
2701
Gilles Peskine20035e32018-02-03 22:44:14 +01002702/** \defgroup asymmetric Asymmetric cryptography
2703 * @{
2704 */
2705
2706/**
2707 * \brief Sign a hash or short message with a private key.
2708 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002709 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002710 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002711 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2712 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2713 * to determine the hash algorithm to use.
2714 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002715 * \param handle Handle to the key to use for the operation.
2716 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002717 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002718 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002719 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002720 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002721 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002722 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002723 * \param[out] signature_length On success, the number of bytes
2724 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002725 *
Gilles Peskine28538492018-07-11 17:34:00 +02002726 * \retval #PSA_SUCCESS
2727 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002728 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002729 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002730 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002731 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002732 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002733 * \retval #PSA_ERROR_NOT_SUPPORTED
2734 * \retval #PSA_ERROR_INVALID_ARGUMENT
2735 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2736 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2737 * \retval #PSA_ERROR_HARDWARE_FAILURE
2738 * \retval #PSA_ERROR_TAMPERING_DETECTED
2739 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002740 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002741 * The library has not been previously initialized by psa_crypto_init().
2742 * It is implementation-dependent whether a failure to initialize
2743 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002744 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002745psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002746 psa_algorithm_t alg,
2747 const uint8_t *hash,
2748 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002749 uint8_t *signature,
2750 size_t signature_size,
2751 size_t *signature_length);
2752
2753/**
2754 * \brief Verify the signature a hash or short message using a public key.
2755 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002756 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002757 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002758 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2759 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2760 * to determine the hash algorithm to use.
2761 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002762 * \param handle Handle to the key to use for the operation.
2763 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002764 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002765 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002766 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002767 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002768 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002769 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002770 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002771 *
Gilles Peskine28538492018-07-11 17:34:00 +02002772 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002773 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002774 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002775 * The calculation was perfomed successfully, but the passed
2776 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002777 * \retval #PSA_ERROR_NOT_SUPPORTED
2778 * \retval #PSA_ERROR_INVALID_ARGUMENT
2779 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2780 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2781 * \retval #PSA_ERROR_HARDWARE_FAILURE
2782 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002783 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002784 * The library has not been previously initialized by psa_crypto_init().
2785 * It is implementation-dependent whether a failure to initialize
2786 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002787 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002788psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002789 psa_algorithm_t alg,
2790 const uint8_t *hash,
2791 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002792 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002793 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002794
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002795/**
2796 * \brief Encrypt a short message with a public key.
2797 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002798 * \param handle Handle to the key to use for the operation.
2799 * It must be a public key or an asymmetric
2800 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002801 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002802 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002803 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002804 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002805 * \param[in] salt A salt or label, if supported by the
2806 * encryption algorithm.
2807 * If the algorithm does not support a
2808 * salt, pass \c NULL.
2809 * If the algorithm supports an optional
2810 * salt and you do not want to pass a salt,
2811 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002812 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002813 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2814 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002815 * \param salt_length Size of the \p salt buffer in bytes.
2816 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002817 * \param[out] output Buffer where the encrypted message is to
2818 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002819 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002820 * \param[out] output_length On success, the number of bytes
2821 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002822 *
Gilles Peskine28538492018-07-11 17:34:00 +02002823 * \retval #PSA_SUCCESS
2824 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002825 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002826 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002827 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002828 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002829 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002830 * \retval #PSA_ERROR_NOT_SUPPORTED
2831 * \retval #PSA_ERROR_INVALID_ARGUMENT
2832 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2833 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2834 * \retval #PSA_ERROR_HARDWARE_FAILURE
2835 * \retval #PSA_ERROR_TAMPERING_DETECTED
2836 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002837 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002838 * The library has not been previously initialized by psa_crypto_init().
2839 * It is implementation-dependent whether a failure to initialize
2840 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002841 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002842psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002843 psa_algorithm_t alg,
2844 const uint8_t *input,
2845 size_t input_length,
2846 const uint8_t *salt,
2847 size_t salt_length,
2848 uint8_t *output,
2849 size_t output_size,
2850 size_t *output_length);
2851
2852/**
2853 * \brief Decrypt a short message with a private key.
2854 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002855 * \param handle Handle to the key to use for the operation.
2856 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002857 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002858 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002859 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002860 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002861 * \param[in] salt A salt or label, if supported by the
2862 * encryption algorithm.
2863 * If the algorithm does not support a
2864 * salt, pass \c NULL.
2865 * If the algorithm supports an optional
2866 * salt and you do not want to pass a salt,
2867 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002868 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002869 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2870 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002871 * \param salt_length Size of the \p salt buffer in bytes.
2872 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002873 * \param[out] output Buffer where the decrypted message is to
2874 * be written.
2875 * \param output_size Size of the \c output buffer in bytes.
2876 * \param[out] output_length On success, the number of bytes
2877 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002878 *
Gilles Peskine28538492018-07-11 17:34:00 +02002879 * \retval #PSA_SUCCESS
2880 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002881 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002882 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002883 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002884 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002885 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002886 * \retval #PSA_ERROR_NOT_SUPPORTED
2887 * \retval #PSA_ERROR_INVALID_ARGUMENT
2888 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2889 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2890 * \retval #PSA_ERROR_HARDWARE_FAILURE
2891 * \retval #PSA_ERROR_TAMPERING_DETECTED
2892 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2893 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002894 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002895 * The library has not been previously initialized by psa_crypto_init().
2896 * It is implementation-dependent whether a failure to initialize
2897 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002898 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002899psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002900 psa_algorithm_t alg,
2901 const uint8_t *input,
2902 size_t input_length,
2903 const uint8_t *salt,
2904 size_t salt_length,
2905 uint8_t *output,
2906 size_t output_size,
2907 size_t *output_length);
2908
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002909/**@}*/
2910
Gilles Peskineedd76872018-07-20 17:42:05 +02002911/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002912 * @{
2913 */
2914
2915/** The type of the state data structure for generators.
2916 *
2917 * Before calling any function on a generator, the application must
2918 * initialize it by any of the following means:
2919 * - Set the structure to all-bits-zero, for example:
2920 * \code
2921 * psa_crypto_generator_t generator;
2922 * memset(&generator, 0, sizeof(generator));
2923 * \endcode
2924 * - Initialize the structure to logical zero values, for example:
2925 * \code
2926 * psa_crypto_generator_t generator = {0};
2927 * \endcode
2928 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2929 * for example:
2930 * \code
2931 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2932 * \endcode
2933 * - Assign the result of the function psa_crypto_generator_init()
2934 * to the structure, for example:
2935 * \code
2936 * psa_crypto_generator_t generator;
2937 * generator = psa_crypto_generator_init();
2938 * \endcode
2939 *
2940 * This is an implementation-defined \c struct. Applications should not
2941 * make any assumptions about the content of this structure except
2942 * as directed by the documentation of a specific implementation.
2943 */
2944typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2945
2946/** \def PSA_CRYPTO_GENERATOR_INIT
2947 *
2948 * This macro returns a suitable initializer for a generator object
2949 * of type #psa_crypto_generator_t.
2950 */
2951#ifdef __DOXYGEN_ONLY__
2952/* This is an example definition for documentation purposes.
2953 * Implementations should define a suitable value in `crypto_struct.h`.
2954 */
2955#define PSA_CRYPTO_GENERATOR_INIT {0}
2956#endif
2957
2958/** Return an initial value for a generator object.
2959 */
2960static psa_crypto_generator_t psa_crypto_generator_init(void);
2961
2962/** Retrieve the current capacity of a generator.
2963 *
2964 * The capacity of a generator is the maximum number of bytes that it can
2965 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2966 *
2967 * \param[in] generator The generator to query.
2968 * \param[out] capacity On success, the capacity of the generator.
2969 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002970 * \retval #PSA_SUCCESS
2971 * \retval #PSA_ERROR_BAD_STATE
2972 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002973 */
2974psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2975 size_t *capacity);
2976
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002977/** Set the maximum capacity of a generator.
2978 *
2979 * \param[in,out] generator The generator object to modify.
2980 * \param capacity The new capacity of the generator.
2981 * It must be less or equal to the generator's
2982 * current capacity.
2983 *
2984 * \retval #PSA_SUCCESS
2985 * \retval #PSA_ERROR_INVALID_ARGUMENT
2986 * \p capacity is larger than the generator's current capacity.
2987 * \retval #PSA_ERROR_BAD_STATE
2988 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2989 */
2990psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
2991 size_t capacity);
2992
Gilles Peskineeab56e42018-07-12 17:12:33 +02002993/** Read some data from a generator.
2994 *
2995 * This function reads and returns a sequence of bytes from a generator.
2996 * The data that is read is discarded from the generator. The generator's
2997 * capacity is decreased by the number of bytes read.
2998 *
2999 * \param[in,out] generator The generator object to read from.
3000 * \param[out] output Buffer where the generator output will be
3001 * written.
3002 * \param output_length Number of bytes to output.
3003 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003004 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003005 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskineeab56e42018-07-12 17:12:33 +02003006 * There were fewer than \p output_length bytes
3007 * in the generator. Note that in this case, no
3008 * output is written to the output buffer.
3009 * The generator's capacity is set to 0, thus
3010 * subsequent calls to this function will not
3011 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003012 * \retval #PSA_ERROR_BAD_STATE
3013 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3014 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3015 * \retval #PSA_ERROR_HARDWARE_FAILURE
3016 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003017 */
3018psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
3019 uint8_t *output,
3020 size_t output_length);
3021
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003022/** Generate a key deterministically from data read from a generator.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003023 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003024 * This function uses the output of a generator to derive a key.
3025 * How much output it consumes and how the key is derived depends on the
3026 * key type.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003027 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003028 * - For key types for which the key is an arbitrary sequence of bytes
3029 * of a given size,
3030 * this function is functionally equivalent to calling #psa_generator_read
3031 * and passing the resulting output to #psa_import_key.
3032 * However, this function has a security benefit:
3033 * if the implementation provides an isolation boundary then
3034 * the key material is not exposed outside the isolation boundary.
3035 * As a consequence, for these key types, this function always consumes
3036 * exactly (\p bits / 8) bytes from the generator.
3037 * The following key types defined in this specification follow this scheme:
3038 *
3039 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003040 * - #PSA_KEY_TYPE_ARC4;
3041 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003042 * - #PSA_KEY_TYPE_DERIVE;
3043 * - #PSA_KEY_TYPE_HMAC.
3044 *
3045 * - For ECC keys on a Montgomery elliptic curve
3046 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3047 * Montgomery curve), this function always draws a byte string whose
3048 * length is determined by the curve, and sets the mandatory bits
3049 * accordingly. That is:
3050 *
3051 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3052 * and process it as specified in RFC 7748 &sect;5.
3053 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3054 * and process it as specified in RFC 7748 &sect;5.
3055 *
3056 * - For key types for which the key is represented by a single sequence of
3057 * \p bits bits with constraints as to which bit sequences are acceptable,
3058 * this function draws a byte string of length (\p bits / 8) bytes rounded
3059 * up to the nearest whole number of bytes. If the resulting byte string
3060 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3061 * This process is repeated until an acceptable byte string is drawn.
3062 * The byte string drawn from the generator is interpreted as specified
3063 * for the output produced by psa_export_key().
3064 * The following key types defined in this specification follow this scheme:
3065 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003066 * - #PSA_KEY_TYPE_DES.
3067 * Force-set the parity bits, but discard forbidden weak keys.
3068 * For 2-key and 3-key triple-DES, the three keys are generated
3069 * successively (for example, for 3-key triple-DES,
3070 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3071 * discard the first 8 bytes, use the next 8 bytes as the first key,
3072 * and continue reading output from the generator to derive the other
3073 * two keys).
3074 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR),
3075 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
3076 * ECC keys on a Weierstrass elliptic curve
3077 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3078 * Weierstrass curve).
3079 * For these key types, interpret the byte string as integer
3080 * in big-endian order. Discard it if it is not in the range
3081 * [0, *N* - 2] where *N* is the boundary of the private key domain
3082 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003083 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003084 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003085 * This method allows compliance to NIST standards, specifically
3086 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003087 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3088 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3089 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3090 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003091 *
3092 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
3093 * the way in which the generator output is consumed is
3094 * implementation-defined.
3095 *
3096 * In all cases, the data that is read is discarded from the generator.
3097 * The generator's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003098 *
Gilles Peskine20628592019-04-19 19:29:50 +02003099 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003100 * \param[out] handle On success, a handle to the newly created key.
3101 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003102 * \param[in,out] generator The generator object to read from.
3103 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003104 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003105 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003106 * If the key is persistent, the key material and the key's metadata
3107 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003108 * \retval #PSA_ERROR_ALREADY_EXISTS
3109 * This is an attempt to create a persistent key, and there is
3110 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003111 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003112 * There was not enough data to create the desired key.
3113 * Note that in this case, no output is written to the output buffer.
3114 * The generator's capacity is set to 0, thus subsequent calls to
3115 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003116 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003117 * The key type or key size is not supported, either by the
3118 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003119 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003120 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3121 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3122 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3123 * \retval #PSA_ERROR_HARDWARE_FAILURE
3124 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003125 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003126 * The library has not been previously initialized by psa_crypto_init().
3127 * It is implementation-dependent whether a failure to initialize
3128 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003129 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003130psa_status_t psa_generate_derived_key(const psa_key_attributes_t *attributes,
Gilles Peskine87a5e562019-04-17 12:28:25 +02003131 psa_key_handle_t *handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003132 psa_crypto_generator_t *generator);
3133
3134/** Abort a generator.
3135 *
3136 * Once a generator has been aborted, its capacity is zero.
3137 * Aborting a generator frees all associated resources except for the
3138 * \c generator structure itself.
3139 *
3140 * This function may be called at any time as long as the generator
3141 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
3142 * psa_crypto_generator_init() or a zero value. In particular, it is valid
3143 * to call psa_generator_abort() twice, or to call psa_generator_abort()
3144 * on a generator that has not been set up.
3145 *
3146 * Once aborted, the generator object may be called.
3147 *
3148 * \param[in,out] generator The generator to abort.
3149 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003150 * \retval #PSA_SUCCESS
3151 * \retval #PSA_ERROR_BAD_STATE
3152 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3153 * \retval #PSA_ERROR_HARDWARE_FAILURE
3154 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003155 */
3156psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3157
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003158/** Use the maximum possible capacity for a generator.
3159 *
3160 * Use this value as the capacity argument when setting up a generator
3161 * to indicate that the generator should have the maximum possible capacity.
3162 * The value of the maximum possible capacity depends on the generator
3163 * algorithm.
3164 */
3165#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
3166
Gilles Peskineeab56e42018-07-12 17:12:33 +02003167/**@}*/
3168
Gilles Peskineea0fb492018-07-12 17:17:20 +02003169/** \defgroup derivation Key derivation
3170 * @{
3171 */
3172
3173/** Set up a key derivation operation.
3174 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003175 * A key derivation algorithm takes some inputs and uses them to create
3176 * a byte generator which can be used to produce keys and other
3177 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003178 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003179 * To use a generator for key derivation:
3180 * - Start with an initialized object of type #psa_crypto_generator_t.
3181 * - Call psa_key_derivation_setup() to select the algorithm.
3182 * - Provide the inputs for the key derivation by calling
3183 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3184 * as appropriate. Which inputs are needed, in what order, and whether
3185 * they may be keys and if so of what type depends on the algorithm.
3186 * - Optionally set the generator's maximum capacity with
3187 * psa_set_generator_capacity(). You may do this before, in the middle of
3188 * or after providing inputs. For some algorithms, this step is mandatory
3189 * because the output depends on the maximum capacity.
3190 * - Generate output with psa_generator_read() or
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003191 * psa_generate_derived_key(). Successive calls to these functions
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003192 * use successive output bytes from the generator.
3193 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02003194 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003195 * \param[in,out] generator The generator object to set up. It must
3196 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003197 * \param alg The key derivation algorithm to compute
3198 * (\c PSA_ALG_XXX value such that
3199 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02003200 *
3201 * \retval #PSA_SUCCESS
3202 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003203 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003204 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003205 * \retval #PSA_ERROR_NOT_SUPPORTED
3206 * \c alg is not supported or is not a key derivation algorithm.
3207 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3208 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3209 * \retval #PSA_ERROR_HARDWARE_FAILURE
3210 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003211 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003212 */
3213psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
3214 psa_algorithm_t alg);
3215
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003216/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003217 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003218 * Which inputs are required and in what order depends on the algorithm.
3219 * Refer to the documentation of each key derivation or key agreement
3220 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003221 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003222 * This function passes direct inputs. Some inputs must be passed as keys
3223 * using psa_key_derivation_input_key() instead of this function. Refer to
3224 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003225 *
3226 * \param[in,out] generator The generator object to use. It must
3227 * have been set up with
3228 * psa_key_derivation_setup() and must not
3229 * have produced any output yet.
3230 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003231 * \param[in] data Input data to use.
3232 * \param data_length Size of the \p data buffer in bytes.
3233 *
3234 * \retval #PSA_SUCCESS
3235 * Success.
3236 * \retval #PSA_ERROR_INVALID_ARGUMENT
3237 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003238 * \retval #PSA_ERROR_INVALID_ARGUMENT
3239 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003240 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3241 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3242 * \retval #PSA_ERROR_HARDWARE_FAILURE
3243 * \retval #PSA_ERROR_TAMPERING_DETECTED
3244 * \retval #PSA_ERROR_BAD_STATE
3245 * The value of \p step is not valid given the state of \p generator.
3246 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003247 * The library has not been previously initialized by psa_crypto_init().
3248 * It is implementation-dependent whether a failure to initialize
3249 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003250 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003251psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3252 psa_key_derivation_step_t step,
3253 const uint8_t *data,
3254 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003255
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003256/** Provide an input for key derivation in the form of a key.
3257 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003258 * Which inputs are required and in what order depends on the algorithm.
3259 * Refer to the documentation of each key derivation or key agreement
3260 * algorithm for information.
3261 *
3262 * This function passes key inputs. Some inputs must be passed as keys
3263 * of the appropriate type using this function, while others must be
3264 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3265 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003266 *
3267 * \param[in,out] generator The generator object to use. It must
3268 * have been set up with
3269 * psa_key_derivation_setup() and must not
3270 * have produced any output yet.
3271 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003272 * \param handle Handle to the key. It must have an
3273 * appropriate type for \p step and must
3274 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003275 *
3276 * \retval #PSA_SUCCESS
3277 * Success.
3278 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine31351842019-04-09 12:00:00 +02003279 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003280 * \retval #PSA_ERROR_NOT_PERMITTED
3281 * \retval #PSA_ERROR_INVALID_ARGUMENT
3282 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003283 * \retval #PSA_ERROR_INVALID_ARGUMENT
3284 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003285 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3286 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3287 * \retval #PSA_ERROR_HARDWARE_FAILURE
3288 * \retval #PSA_ERROR_TAMPERING_DETECTED
3289 * \retval #PSA_ERROR_BAD_STATE
3290 * The value of \p step is not valid given the state of \p generator.
3291 * \retval #PSA_ERROR_BAD_STATE
3292 * The library has not been previously initialized by psa_crypto_init().
3293 * It is implementation-dependent whether a failure to initialize
3294 * results in this error code.
3295 */
3296psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3297 psa_key_derivation_step_t step,
3298 psa_key_handle_t handle);
3299
Gilles Peskine969c5d62019-01-16 15:53:06 +01003300/** Perform a key agreement and use the shared secret as input to a key
3301 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003302 *
3303 * A key agreement algorithm takes two inputs: a private key \p private_key
3304 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003305 * The result of this function is passed as input to a key derivation.
3306 * The output of this key derivation can be extracted by reading from the
3307 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003308 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003309 * \param[in,out] generator The generator object to use. It must
3310 * have been set up with
3311 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003312 * key agreement and derivation algorithm
3313 * \c alg (\c PSA_ALG_XXX value such that
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003314 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3315 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
Gilles Peskine6843c292019-01-18 16:44:49 +01003316 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003317 * The generator must be ready for an
3318 * input of the type given by \p step.
3319 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003320 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003321 * \param[in] peer_key Public key of the peer. The peer key must be in the
3322 * same format that psa_import_key() accepts for the
3323 * public key type corresponding to the type of
3324 * private_key. That is, this function performs the
3325 * equivalent of
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003326 * #psa_import_key(`internal_public_key_handle`,
3327 * #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(`private_key_type`),
3328 * `peer_key`, `peer_key_length`) where
Jaeden Amero8afbff82019-01-14 16:56:20 +00003329 * `private_key_type` is the type of `private_key`.
3330 * For example, for EC keys, this means that peer_key
3331 * is interpreted as a point on the curve that the
3332 * private key is on. The standard formats for public
3333 * keys are documented in the documentation of
3334 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003335 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003336 *
3337 * \retval #PSA_SUCCESS
3338 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003339 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02003340 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine01d718c2018-09-18 12:01:02 +02003341 * \retval #PSA_ERROR_NOT_PERMITTED
3342 * \retval #PSA_ERROR_INVALID_ARGUMENT
3343 * \c private_key is not compatible with \c alg,
3344 * or \p peer_key is not valid for \c alg or not compatible with
3345 * \c private_key.
3346 * \retval #PSA_ERROR_NOT_SUPPORTED
3347 * \c alg is not supported or is not a key derivation algorithm.
3348 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3349 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3350 * \retval #PSA_ERROR_HARDWARE_FAILURE
3351 * \retval #PSA_ERROR_TAMPERING_DETECTED
3352 */
3353psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003354 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003355 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003356 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003357 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003358
Gilles Peskine769c7a62019-01-18 16:42:29 +01003359/** Perform a key agreement and use the shared secret as input to a key
3360 * derivation.
3361 *
3362 * A key agreement algorithm takes two inputs: a private key \p private_key
3363 * a public key \p peer_key.
3364 *
3365 * \warning The raw result of a key agreement algorithm such as finite-field
3366 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3367 * not be used directly as key material. It should instead be passed as
3368 * input to a key derivation algorithm. To chain a key agreement with
3369 * a key derivation, use psa_key_agreement() and other functions from
3370 * the key derivation and generator interface.
3371 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003372 * \param alg The key agreement algorithm to compute
3373 * (\c PSA_ALG_XXX value such that
3374 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3375 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003376 * \param private_key Handle to the private key to use.
3377 * \param[in] peer_key Public key of the peer. It must be
3378 * in the same format that psa_import_key()
3379 * accepts. The standard formats for public
3380 * keys are documented in the documentation
3381 * of psa_export_public_key().
3382 * \param peer_key_length Size of \p peer_key in bytes.
3383 * \param[out] output Buffer where the decrypted message is to
3384 * be written.
3385 * \param output_size Size of the \c output buffer in bytes.
3386 * \param[out] output_length On success, the number of bytes
3387 * that make up the returned output.
3388 *
3389 * \retval #PSA_SUCCESS
3390 * Success.
3391 * \retval #PSA_ERROR_INVALID_HANDLE
3392 * \retval #PSA_ERROR_EMPTY_SLOT
3393 * \retval #PSA_ERROR_NOT_PERMITTED
3394 * \retval #PSA_ERROR_INVALID_ARGUMENT
3395 * \p alg is not a key agreement algorithm
3396 * \retval #PSA_ERROR_INVALID_ARGUMENT
3397 * \p private_key is not compatible with \p alg,
3398 * or \p peer_key is not valid for \p alg or not compatible with
3399 * \p private_key.
3400 * \retval #PSA_ERROR_NOT_SUPPORTED
3401 * \p alg is not a supported key agreement algorithm.
3402 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3403 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3404 * \retval #PSA_ERROR_HARDWARE_FAILURE
3405 * \retval #PSA_ERROR_TAMPERING_DETECTED
3406 */
3407psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3408 psa_key_handle_t private_key,
3409 const uint8_t *peer_key,
3410 size_t peer_key_length,
3411 uint8_t *output,
3412 size_t output_size,
3413 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003414
3415/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003416
3417/** \defgroup random Random generation
3418 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003419 */
3420
3421/**
3422 * \brief Generate random bytes.
3423 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003424 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003425 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003426 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003427 *
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003428 * \note To generate a key, use psa_generate_random_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003429 *
3430 * \param[out] output Output buffer for the generated data.
3431 * \param output_size Number of bytes to generate and output.
3432 *
3433 * \retval #PSA_SUCCESS
3434 * \retval #PSA_ERROR_NOT_SUPPORTED
3435 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3436 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3437 * \retval #PSA_ERROR_HARDWARE_FAILURE
3438 * \retval #PSA_ERROR_TAMPERING_DETECTED
3439 * \retval #PSA_ERROR_BAD_STATE
3440 * The library has not been previously initialized by psa_crypto_init().
3441 * It is implementation-dependent whether a failure to initialize
3442 * results in this error code.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003443 */
3444psa_status_t psa_generate_random(uint8_t *output,
3445 size_t output_size);
3446
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003447/**
3448 * \brief Generate a key or key pair.
3449 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003450 * The key is generated randomly.
3451 * Its location, policy, type and size are taken from \p attributes.
3452 *
3453 * If the type requires additional domain parameters, these are taken
3454 * from \p attributes as well. The following types use domain parameters:
3455 * - When generating an RSA key (#PSA_KEY_TYPE_RSA_KEYPAIR),
3456 * the default public exponent is 65537. This value is used if
3457 * \p attributes was set with psa_set_key_type() or by passing an empty
3458 * byte string as domain parameters to psa_set_key_domain_parameters().
3459 * If psa_set_key_domain_parameters() was used to set a non-empty
3460 * domain parameter string in \p attributes, this string is read as
3461 * a big-endian integer which is used as the public exponent.
3462 * - When generating a DSA key (#PSA_KEY_TYPE_DSA_KEYPAIR) or a
3463 * Diffie-Hellman key (#PSA_KEY_TYPE_DH_KEYPAIR), the domain parameters
3464 * from \p attributes are interpreted as described for
3465 * psa_set_key_domain_parameters().
3466 *
Gilles Peskine20628592019-04-19 19:29:50 +02003467 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003468 * \param[out] handle On success, a handle to the newly created key.
3469 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003470 *
Gilles Peskine28538492018-07-11 17:34:00 +02003471 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003472 * Success.
3473 * If the key is persistent, the key material and the key's metadata
3474 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003475 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003476 * This is an attempt to create a persistent key, and there is
3477 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003478 * \retval #PSA_ERROR_NOT_SUPPORTED
3479 * \retval #PSA_ERROR_INVALID_ARGUMENT
3480 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3481 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3482 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3483 * \retval #PSA_ERROR_HARDWARE_FAILURE
3484 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003485 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003486 * The library has not been previously initialized by psa_crypto_init().
3487 * It is implementation-dependent whether a failure to initialize
3488 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003489 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003490psa_status_t psa_generate_random_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003491 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003492
3493/**@}*/
3494
Gilles Peskinee59236f2018-01-27 23:32:46 +01003495#ifdef __cplusplus
3496}
3497#endif
3498
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003499/* The file "crypto_sizes.h" contains definitions for size calculation
3500 * macros whose definitions are implementation-specific. */
3501#include "crypto_sizes.h"
3502
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003503/* The file "crypto_struct.h" contains definitions for
3504 * implementation-specific structs that are declared above. */
3505#include "crypto_struct.h"
3506
3507/* The file "crypto_extra.h" contains vendor-specific definitions. This
3508 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003509#include "crypto_extra.h"
3510
3511#endif /* PSA_CRYPTO_H */