blob: 487fce8222fb1f3fdca02f3858b6eb7174c522f9 [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).
Gilles Peskine24f10f82019-05-16 12:18:32 +0200108 * - Information about the key itself: the key type and its size.
Gilles Peskine9c640f92019-04-28 11:36:21 +0200109 * - Implementations may define additional attributes.
Gilles Peskine87a5e562019-04-17 12:28:25 +0200110 *
111 * The actual key material is not considered an attribute of a key.
112 * Key attributes do not contain information that is generally considered
113 * highly confidential.
Gilles Peskine20628592019-04-19 19:29:50 +0200114 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200115 * An attribute structure can be a simple data structure where each function
116 * `psa_set_key_xxx` sets a field and the corresponding function
117 * `psa_get_key_xxx` retrieves the value of the corresponding field.
118 * However, implementations may report values that are equivalent to the
119 * original one, but have a different encoding. For example, an
120 * implementation may use a more compact representation for types where
121 * many bit-patterns are invalid or not supported, and store all values
122 * that it does not support as a special marker value. In such an
123 * implementation, after setting an invalid value, the corresponding
124 * get function returns an invalid value which may not be the one that
125 * was originally stored.
126 *
127 * An attribute structure may contain references to auxiliary resources,
128 * for example pointers to allocated memory or indirect references to
129 * pre-calculated values. In order to free such resources, the application
130 * must call psa_reset_key_attributes(). As an exception, calling
131 * psa_reset_key_attributes() on an attribute structure is optional if
132 * the structure has only been modified by the following functions
133 * since it was initialized or last reset with psa_reset_key_attributes():
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200134 * - psa_set_key_id()
135 * - psa_set_key_lifetime()
Gilles Peskine9c640f92019-04-28 11:36:21 +0200136 * - psa_set_key_type()
137 * - psa_set_key_bits()
138 * - psa_set_key_usage_flags()
139 * - psa_set_key_algorithm()
140 *
Gilles Peskine20628592019-04-19 19:29:50 +0200141 * Before calling any function on a key attribute structure, the application
142 * must initialize it by any of the following means:
143 * - Set the structure to all-bits-zero, for example:
144 * \code
145 * psa_key_attributes_t attributes;
146 * memset(&attributes, 0, sizeof(attributes));
147 * \endcode
148 * - Initialize the structure to logical zero values, for example:
149 * \code
150 * psa_key_attributes_t attributes = {0};
151 * \endcode
152 * - Initialize the structure to the initializer #PSA_KEY_ATTRIBUTES_INIT,
153 * for example:
154 * \code
155 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
156 * \endcode
157 * - Assign the result of the function psa_key_attributes_init()
158 * to the structure, for example:
159 * \code
160 * psa_key_attributes_t attributes;
161 * attributes = psa_key_attributes_init();
162 * \endcode
163 *
164 * A freshly initialized attribute structure contains the following
165 * values:
166 *
167 * - lifetime: #PSA_KEY_LIFETIME_VOLATILE.
168 * - key identifier: unspecified.
Gilles Peskine24f10f82019-05-16 12:18:32 +0200169 * - type: \c 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200170 * - key size: \c 0.
171 * - usage flags: \c 0.
172 * - algorithm: \c 0.
173 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200174 * A typical sequence to create a key is as follows:
175 * -# Create and initialize an attribute structure.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200176 * -# If the key is persistent, call psa_set_key_id().
177 * Also call psa_set_key_lifetime() to place the key in a non-default
178 * location.
Gilles Peskine9c640f92019-04-28 11:36:21 +0200179 * -# Set the key policy with psa_set_key_usage_flags() and
180 * psa_set_key_algorithm().
Gilles Peskine24f10f82019-05-16 12:18:32 +0200181 * -# Set the key type with psa_set_key_type().
Gilles Peskine9c640f92019-04-28 11:36:21 +0200182 * Skip this step if copying an existing key with psa_copy_key().
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100183 * -# When generating a random key with psa_generate_random_key() or deriving a key
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200184 * with psa_key_derivation_output_key(), set the desired key size with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200185 * psa_set_key_bits().
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100186 * -# Call a key creation function: psa_import_key(), psa_generate_random_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200187 * psa_key_derivation_output_key() or psa_copy_key(). This function reads
Gilles Peskine1ea5e442019-05-02 20:31:10 +0200188 * the attribute structure, creates a key with these attributes, and
189 * outputs a handle to the newly created key.
Gilles Peskine24f10f82019-05-16 12:18:32 +0200190 * -# The attribute structure is now no longer necessary.
191 * You may call psa_reset_key_attributes(), although this is optional
192 * with the workflow presented here because the attributes currently
193 * defined in this specification do not require any additional resources
194 * beyond the structure itself.
Gilles Peskine20628592019-04-19 19:29:50 +0200195 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200196 * A typical sequence to query a key's attributes is as follows:
197 * -# Call psa_get_key_attributes().
198 * -# Call `psa_get_key_xxx` functions to retrieve the attribute(s) that
199 * you are interested in.
200 * -# Call psa_reset_key_attributes() to free any resources that may be
201 * used by the attribute structure.
202 *
203 * Once a key has been created, it is impossible to change its attributes.
Gilles Peskine87a5e562019-04-17 12:28:25 +0200204 */
205typedef struct psa_key_attributes_s psa_key_attributes_t;
206
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200207/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200208 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200209 * If the attribute structure currently declares the key as volatile (which
210 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200211 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200212 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200213 * This function does not access storage, it merely stores the given
214 * value in the structure.
215 * The persistent key will be written to storage when the attribute
216 * structure is passed to a key creation function such as
217 * psa_import_key(), psa_generate_random_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200218 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200219 *
Gilles Peskine20628592019-04-19 19:29:50 +0200220 * This function may be declared as `static` (i.e. without external
221 * linkage). This function may be provided as a function-like macro,
222 * but in this case it must evaluate each of its arguments exactly once.
223 *
224 * \param[out] attributes The attribute structure to write to.
225 * \param id The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200226 */
227static void psa_set_key_id(psa_key_attributes_t *attributes,
228 psa_key_id_t id);
229
230/** Set the location of a persistent key.
231 *
232 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200233 * with psa_set_key_id(). By default, a key that has a persistent identifier
234 * is stored in the default storage area identifier by
235 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
236 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200237 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200238 * This function does not access storage, it merely stores the given
239 * value in the structure.
240 * The persistent key will be written to storage when the attribute
241 * structure is passed to a key creation function such as
242 * psa_import_key(), psa_generate_random_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200243 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200244 *
245 * This function may be declared as `static` (i.e. without external
246 * linkage). This function may be provided as a function-like macro,
247 * but in this case it must evaluate each of its arguments exactly once.
248 *
249 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200250 * \param lifetime The lifetime for the key.
251 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200252 * key will be volatile, and the key identifier
253 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200254 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200255static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
256 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200257
Gilles Peskine20628592019-04-19 19:29:50 +0200258/** Retrieve the key identifier from key attributes.
259 *
260 * This function may be declared as `static` (i.e. without external
261 * linkage). This function may be provided as a function-like macro,
262 * but in this case it must evaluate its argument exactly once.
263 *
264 * \param[in] attributes The key attribute structure to query.
265 *
266 * \return The persistent identifier stored in the attribute structure.
267 * This value is unspecified if the attribute structure declares
268 * the key as volatile.
269 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200270static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
271
Gilles Peskine20628592019-04-19 19:29:50 +0200272/** Retrieve the lifetime from key attributes.
273 *
274 * This function may be declared as `static` (i.e. without external
275 * linkage). This function may be provided as a function-like macro,
276 * but in this case it must evaluate its argument exactly once.
277 *
278 * \param[in] attributes The key attribute structure to query.
279 *
280 * \return The lifetime value stored in the attribute structure.
281 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200282static psa_key_lifetime_t psa_get_key_lifetime(
283 const psa_key_attributes_t *attributes);
284
Gilles Peskine20628592019-04-19 19:29:50 +0200285/** Declare usage flags for a key.
286 *
287 * Usage flags are part of a key's usage policy. They encode what
288 * kind of operations are permitted on the key. For more details,
289 * refer to the documentation of the type #psa_key_usage_t.
290 *
291 * This function overwrites any usage flags
292 * previously set in \p attributes.
293 *
294 * This function may be declared as `static` (i.e. without external
295 * linkage). This function may be provided as a function-like macro,
296 * but in this case it must evaluate each of its arguments exactly once.
297 *
298 * \param[out] attributes The attribute structure to write to.
299 * \param usage_flags The usage flags to write.
300 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200301static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
302 psa_key_usage_t usage_flags);
303
Gilles Peskine20628592019-04-19 19:29:50 +0200304/** Retrieve the usage flags from key attributes.
305 *
306 * This function may be declared as `static` (i.e. without external
307 * linkage). This function may be provided as a function-like macro,
308 * but in this case it must evaluate its argument exactly once.
309 *
310 * \param[in] attributes The key attribute structure to query.
311 *
312 * \return The usage flags stored in the attribute structure.
313 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200314static psa_key_usage_t psa_get_key_usage_flags(
315 const psa_key_attributes_t *attributes);
316
Gilles Peskine20628592019-04-19 19:29:50 +0200317/** Declare the permitted algorithm policy for a key.
318 *
319 * The permitted algorithm policy of a key encodes which algorithm or
320 * algorithms are permitted to be used with this key.
321 *
322 * This function overwrites any algorithm policy
323 * previously set in \p attributes.
324 *
325 * This function may be declared as `static` (i.e. without external
326 * linkage). This function may be provided as a function-like macro,
327 * but in this case it must evaluate each of its arguments exactly once.
328 *
329 * \param[out] attributes The attribute structure to write to.
330 * \param alg The permitted algorithm policy to write.
331 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200332static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
333 psa_algorithm_t alg);
334
Gilles Peskine20628592019-04-19 19:29:50 +0200335/** Retrieve the algorithm policy from key attributes.
336 *
337 * This function may be declared as `static` (i.e. without external
338 * linkage). This function may be provided as a function-like macro,
339 * but in this case it must evaluate its argument exactly once.
340 *
341 * \param[in] attributes The key attribute structure to query.
342 *
343 * \return The algorithm stored in the attribute structure.
344 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200345static psa_algorithm_t psa_get_key_algorithm(
346 const psa_key_attributes_t *attributes);
347
Gilles Peskine20628592019-04-19 19:29:50 +0200348/** Declare the type of a key.
349 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200350 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200351 * previously set in \p attributes.
352 *
353 * This function may be declared as `static` (i.e. without external
354 * linkage). This function may be provided as a function-like macro,
355 * but in this case it must evaluate each of its arguments exactly once.
356 *
357 * \param[out] attributes The attribute structure to write to.
358 * \param type The key type to write.
359 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200360static void psa_set_key_type(psa_key_attributes_t *attributes,
361 psa_key_type_t type);
362
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200363/** Declare the size of a key.
364 *
365 * This function overwrites any key size previously set in \p attributes.
366 *
367 * This function may be declared as `static` (i.e. without external
368 * linkage). This function may be provided as a function-like macro,
369 * but in this case it must evaluate each of its arguments exactly once.
370 *
371 * \param[out] attributes The attribute structure to write to.
372 * \param bits The key size in bits.
373 */
374static void psa_set_key_bits(psa_key_attributes_t *attributes,
375 size_t bits);
376
Gilles Peskine20628592019-04-19 19:29:50 +0200377/** Retrieve the key type from key attributes.
378 *
379 * This function may be declared as `static` (i.e. without external
380 * linkage). This function may be provided as a function-like macro,
381 * but in this case it must evaluate its argument exactly once.
382 *
383 * \param[in] attributes The key attribute structure to query.
384 *
385 * \return The key type stored in the attribute structure.
386 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200387static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
388
Gilles Peskine20628592019-04-19 19:29:50 +0200389/** Retrieve the key size from key attributes.
390 *
391 * This function may be declared as `static` (i.e. without external
392 * linkage). This function may be provided as a function-like macro,
393 * but in this case it must evaluate its argument exactly once.
394 *
395 * \param[in] attributes The key attribute structure to query.
396 *
397 * \return The key size stored in the attribute structure, in bits.
398 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200399static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
400
Gilles Peskine20628592019-04-19 19:29:50 +0200401/** Retrieve the attributes of a key.
402 *
403 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200404 * psa_reset_key_attributes(). It then copies the attributes of
405 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200406 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200407 * \note This function may allocate memory or other resources.
408 * Once you have called this function on an attribute structure,
409 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200410 *
Gilles Peskine20628592019-04-19 19:29:50 +0200411 * \param[in] handle Handle to the key to query.
412 * \param[in,out] attributes On success, the attributes of the key.
413 * On failure, equivalent to a
414 * freshly-initialized structure.
415 *
416 * \retval #PSA_SUCCESS
417 * \retval #PSA_ERROR_INVALID_HANDLE
418 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
419 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
420 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200421psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
422 psa_key_attributes_t *attributes);
423
Gilles Peskine20628592019-04-19 19:29:50 +0200424/** Reset a key attribute structure to a freshly initialized state.
425 *
426 * You must initialize the attribute structure as described in the
427 * documentation of the type #psa_key_attributes_t before calling this
428 * function. Once the structure has been initialized, you may call this
429 * function at any time.
430 *
431 * This function frees any auxiliary resources that the structure
432 * may contain.
433 *
434 * \param[in,out] attributes The attribute structure to reset.
435 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200436void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200437
Gilles Peskine87a5e562019-04-17 12:28:25 +0200438/**@}*/
439
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100440/** \defgroup key_management Key management
441 * @{
442 */
443
Gilles Peskinef535eb22018-11-30 14:08:36 +0100444/** Open a handle to an existing persistent key.
445 *
446 * Open a handle to a key which was previously created with psa_create_key().
447 *
Gilles Peskine4a231b82019-05-06 18:56:14 +0200448 * Implementations may provide additional keys that can be opened with
449 * psa_open_key(). Such keys have a key identifier in the vendor range,
450 * as documented in the description of #psa_key_id_t.
451 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100452 * \param id The persistent identifier of the key.
453 * \param[out] handle On success, a handle to a key slot which contains
454 * the data and metadata loaded from the specified
455 * persistent location.
456 *
457 * \retval #PSA_SUCCESS
458 * Success. The application can now use the value of `*handle`
459 * to access the newly allocated key slot.
460 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200461 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100462 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine225010f2019-05-06 18:44:55 +0200463 * \p id is invalid.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100464 * \retval #PSA_ERROR_NOT_PERMITTED
465 * The specified key exists, but the application does not have the
466 * permission to access it. Note that this specification does not
467 * define any way to create such a key, but it may be possible
468 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200469 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
470 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100471 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200472psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100473 psa_key_handle_t *handle);
474
Gilles Peskinef535eb22018-11-30 14:08:36 +0100475/** Close a key handle.
476 *
477 * If the handle designates a volatile key, destroy the key material and
478 * free all associated resources, just like psa_destroy_key().
479 *
480 * If the handle designates a persistent key, free all resources associated
481 * with the key in volatile memory. The key slot in persistent storage is
482 * not affected and can be opened again later with psa_open_key().
483 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100484 * If the key is currently in use in a multipart operation,
485 * the multipart operation is aborted.
486 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100487 * \param handle The key handle to close.
488 *
489 * \retval #PSA_SUCCESS
490 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100491 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100492 */
493psa_status_t psa_close_key(psa_key_handle_t handle);
494
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100495/**@}*/
496
497/** \defgroup import_export Key import and export
498 * @{
499 */
500
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100501/**
502 * \brief Import a key in binary format.
503 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100504 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100505 * documentation of psa_export_public_key() for the format of public keys
506 * and to the documentation of psa_export_key() for the format for
507 * other key types.
508 *
509 * This specification supports a single format for each key type.
510 * Implementations may support other formats as long as the standard
511 * format is supported. Implementations that support other formats
512 * should ensure that the formats are clearly unambiguous so as to
513 * minimize the risk that an invalid input is accidentally interpreted
514 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100515 *
Gilles Peskine20628592019-04-19 19:29:50 +0200516 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200517 * The key size is always determined from the
518 * \p data buffer.
519 * If the key size in \p attributes is nonzero,
520 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200521 * \param[out] handle On success, a handle to the newly created key.
522 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100523 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200524 * buffer is interpreted according to the type declared
525 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200526 * All implementations must support at least the format
527 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100528 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200529 * the chosen type. Implementations may allow other
530 * formats, but should be conservative: implementations
531 * should err on the side of rejecting content if it
532 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200533 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100534 *
Gilles Peskine28538492018-07-11 17:34:00 +0200535 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100536 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100537 * If the key is persistent, the key material and the key's metadata
538 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200539 * \retval #PSA_ERROR_ALREADY_EXISTS
540 * This is an attempt to create a persistent key, and there is
541 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200542 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200543 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200544 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200545 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200546 * The key attributes, as a whole, are invalid.
547 * \retval #PSA_ERROR_INVALID_ARGUMENT
548 * The key data is not correctly formatted.
549 * \retval #PSA_ERROR_INVALID_ARGUMENT
550 * The size in \p attributes is nonzero and does not match the size
551 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200552 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
553 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
554 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100555 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200556 * \retval #PSA_ERROR_HARDWARE_FAILURE
557 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300558 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300559 * The library has not been previously initialized by psa_crypto_init().
560 * It is implementation-dependent whether a failure to initialize
561 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200563psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100564 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200565 size_t data_length,
566 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100567
568/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100569 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200570 *
571 * This function destroys the content of the key slot from both volatile
572 * memory and, if applicable, non-volatile storage. Implementations shall
573 * make a best effort to ensure that any previous content of the slot is
574 * unrecoverable.
575 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100576 * This function also erases any metadata such as policies and frees all
577 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200578 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100579 * If the key is currently in use in a multipart operation,
580 * the multipart operation is aborted.
581 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100582 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100583 *
Gilles Peskine28538492018-07-11 17:34:00 +0200584 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200585 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200586 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200587 * The slot holds content and cannot be erased because it is
588 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100589 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200590 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200591 * There was an failure in communication with the cryptoprocessor.
592 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200593 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200594 * The storage is corrupted. Implementations shall make a best effort
595 * to erase key material even in this stage, however applications
596 * should be aware that it may be impossible to guarantee that the
597 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200598 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200599 * An unexpected condition which is not a storage corruption or
600 * a communication failure occurred. The cryptoprocessor may have
601 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300602 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300603 * The library has not been previously initialized by psa_crypto_init().
604 * It is implementation-dependent whether a failure to initialize
605 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100606 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100607psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100608
609/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100610 * \brief Export a key in binary format.
611 *
612 * The output of this function can be passed to psa_import_key() to
613 * create an equivalent object.
614 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100615 * If the implementation of psa_import_key() supports other formats
616 * beyond the format specified here, the output from psa_export_key()
617 * must use the representation specified here, not the original
618 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100620 * For standard key types, the output format is as follows:
621 *
622 * - For symmetric keys (including MAC keys), the format is the
623 * raw bytes of the key.
624 * - For DES, the key data consists of 8 bytes. The parity bits must be
625 * correct.
626 * - For Triple-DES, the format is the concatenation of the
627 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100628 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200629 * is the non-encrypted DER encoding of the representation defined by
630 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
631 * ```
632 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200633 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200634 * modulus INTEGER, -- n
635 * publicExponent INTEGER, -- e
636 * privateExponent INTEGER, -- d
637 * prime1 INTEGER, -- p
638 * prime2 INTEGER, -- q
639 * exponent1 INTEGER, -- d mod (p-1)
640 * exponent2 INTEGER, -- d mod (q-1)
641 * coefficient INTEGER, -- (inverse of q) mod p
642 * }
643 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000644 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
645 * representation of the private key `x` as a big-endian byte string. The
646 * length of the byte string is the private key size in bytes (leading zeroes
647 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200648 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100649 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100650 * a representation of the private value as a `ceiling(m/8)`-byte string
651 * where `m` is the bit size associated with the curve, i.e. the bit size
652 * of the order of the curve's coordinate field. This byte string is
653 * in little-endian order for Montgomery curves (curve types
654 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
655 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
656 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100657 * This is the content of the `privateKey` field of the `ECPrivateKey`
658 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000659 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
660 * format is the representation of the private key `x` as a big-endian byte
661 * string. The length of the byte string is the private key size in bytes
662 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200663 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
664 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100665 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200666 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
667 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100668 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200669 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200670 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200671 * \param[out] data_length On success, the number of bytes
672 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100673 *
Gilles Peskine28538492018-07-11 17:34:00 +0200674 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100675 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200676 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200677 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200678 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100679 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200680 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
681 * The size of the \p data buffer is too small. You can determine a
682 * sufficient buffer size by calling
683 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
684 * where \c type is the key type
685 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200686 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
687 * \retval #PSA_ERROR_HARDWARE_FAILURE
688 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300689 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300690 * The library has not been previously initialized by psa_crypto_init().
691 * It is implementation-dependent whether a failure to initialize
692 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100693 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100694psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100695 uint8_t *data,
696 size_t data_size,
697 size_t *data_length);
698
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100699/**
700 * \brief Export a public key or the public part of a key pair in binary format.
701 *
702 * The output of this function can be passed to psa_import_key() to
703 * create an object that is equivalent to the public key.
704 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000705 * This specification supports a single format for each key type.
706 * Implementations may support other formats as long as the standard
707 * format is supported. Implementations that support other formats
708 * should ensure that the formats are clearly unambiguous so as to
709 * minimize the risk that an invalid input is accidentally interpreted
710 * according to a different format.
711 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000712 * For standard key types, the output format is as follows:
713 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
714 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
715 * ```
716 * RSAPublicKey ::= SEQUENCE {
717 * modulus INTEGER, -- n
718 * publicExponent INTEGER } -- e
719 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000720 * - For elliptic curve public keys (key types for which
721 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
722 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
723 * Let `m` be the bit size associated with the curve, i.e. the bit size of
724 * `q` for a curve over `F_q`. The representation consists of:
725 * - The byte 0x04;
726 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
727 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000728 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
729 * representation of the public key `y = g^x mod p` as a big-endian byte
730 * string. The length of the byte string is the length of the base prime `p`
731 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000732 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
733 * the format is the representation of the public key `y = g^x mod p` as a
734 * big-endian byte string. The length of the byte string is the length of the
735 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100736 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200737 * Exporting a public key object or the public part of a key pair is
738 * always permitted, regardless of the key's usage flags.
739 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100740 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200741 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200742 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200743 * \param[out] data_length On success, the number of bytes
744 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100745 *
Gilles Peskine28538492018-07-11 17:34:00 +0200746 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100747 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200748 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200749 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200750 * The key is neither a public key nor a key pair.
751 * \retval #PSA_ERROR_NOT_SUPPORTED
752 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
753 * The size of the \p data buffer is too small. You can determine a
754 * sufficient buffer size by calling
755 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
756 * where \c type is the key type
757 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200758 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
759 * \retval #PSA_ERROR_HARDWARE_FAILURE
760 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300761 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300762 * The library has not been previously initialized by psa_crypto_init().
763 * It is implementation-dependent whether a failure to initialize
764 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100765 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100766psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100767 uint8_t *data,
768 size_t data_size,
769 size_t *data_length);
770
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100771/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100772 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100773 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000774 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100775 * This function is primarily useful to copy a key from one location
776 * to another, since it populates a key using the material from
777 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200778 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100779 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100780 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100781 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100782 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200783 * The policy on the source key must have the usage flag
784 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200785 * This flag is sufficient to permit the copy if the key has the lifetime
786 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
787 * Some secure elements do not provide a way to copy a key without
788 * making it extractable from the secure element. If a key is located
789 * in such a secure element, then the key must have both usage flags
790 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
791 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200792 *
Gilles Peskine20628592019-04-19 19:29:50 +0200793 * The resulting key may only be used in a way that conforms to
794 * both the policy of the original key and the policy specified in
795 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100796 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200797 * usage flags on the source policy and the usage flags in \p attributes.
798 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100799 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200800 * - If either of the policies allows an algorithm and the other policy
801 * allows a wildcard-based algorithm policy that includes this algorithm,
802 * the resulting key allows the same algorithm.
803 * - If the policies do not allow any algorithm in common, this function
804 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200805 *
Gilles Peskine20628592019-04-19 19:29:50 +0200806 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100807 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200808 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100809 * \param source_handle The key to copy. It must be a handle to an
810 * occupied slot.
Gilles Peskine20628592019-04-19 19:29:50 +0200811 * \param[in] attributes The attributes for the new key.
812 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200813 * - The key type and size may be 0. If either is
814 * nonzero, it must match the corresponding
815 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200816 * - The key location (the lifetime and, for
817 * persistent keys, the key identifier) is
818 * used directly.
819 * - The policy constraints (usage flags and
820 * algorithm policy) are combined from
821 * the source key and \p attributes so that
822 * both sets of restrictions apply, as
823 * described in the documentation of this function.
824 * \param[out] target_handle On success, a handle to the newly created key.
825 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200826 *
827 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100828 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200829 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200830 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200831 * This is an attempt to create a persistent key, and there is
832 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200833 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200834 * The lifetime or identifier in \p attributes are invalid.
835 * \retval #PSA_ERROR_INVALID_ARGUMENT
836 * The policy constraints on the source and specified in
837 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200838 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200839 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200840 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100841 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200842 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
843 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100844 * The source key is not exportable and its lifetime does not
845 * allow copying it to the target's lifetime.
846 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
847 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200848 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
849 * \retval #PSA_ERROR_HARDWARE_FAILURE
850 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100851 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100852psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200853 const psa_key_attributes_t *attributes,
854 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100855
856/**@}*/
857
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100858/** \defgroup hash Message digests
859 * @{
860 */
861
Gilles Peskine69647a42019-01-14 20:18:12 +0100862/** Calculate the hash (digest) of a message.
863 *
864 * \note To verify the hash of a message against an
865 * expected value, use psa_hash_compare() instead.
866 *
867 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
868 * such that #PSA_ALG_IS_HASH(\p alg) is true).
869 * \param[in] input Buffer containing the message to hash.
870 * \param input_length Size of the \p input buffer in bytes.
871 * \param[out] hash Buffer where the hash is to be written.
872 * \param hash_size Size of the \p hash buffer in bytes.
873 * \param[out] hash_length On success, the number of bytes
874 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100875 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100876 *
877 * \retval #PSA_SUCCESS
878 * Success.
879 * \retval #PSA_ERROR_NOT_SUPPORTED
880 * \p alg is not supported or is not a hash algorithm.
881 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
882 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
883 * \retval #PSA_ERROR_HARDWARE_FAILURE
884 * \retval #PSA_ERROR_TAMPERING_DETECTED
885 */
886psa_status_t psa_hash_compute(psa_algorithm_t alg,
887 const uint8_t *input,
888 size_t input_length,
889 uint8_t *hash,
890 size_t hash_size,
891 size_t *hash_length);
892
893/** Calculate the hash (digest) of a message and compare it with a
894 * reference value.
895 *
896 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
897 * such that #PSA_ALG_IS_HASH(\p alg) is true).
898 * \param[in] input Buffer containing the message to hash.
899 * \param input_length Size of the \p input buffer in bytes.
900 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100901 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100902 *
903 * \retval #PSA_SUCCESS
904 * The expected hash is identical to the actual hash of the input.
905 * \retval #PSA_ERROR_INVALID_SIGNATURE
906 * The hash of the message was calculated successfully, but it
907 * differs from the expected hash.
908 * \retval #PSA_ERROR_NOT_SUPPORTED
909 * \p alg is not supported or is not a hash algorithm.
910 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
911 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
912 * \retval #PSA_ERROR_HARDWARE_FAILURE
913 * \retval #PSA_ERROR_TAMPERING_DETECTED
914 */
915psa_status_t psa_hash_compare(psa_algorithm_t alg,
916 const uint8_t *input,
917 size_t input_length,
918 const uint8_t *hash,
919 const size_t hash_length);
920
Gilles Peskine308b91d2018-02-08 09:47:44 +0100921/** The type of the state data structure for multipart hash operations.
922 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000923 * Before calling any function on a hash operation object, the application must
924 * initialize it by any of the following means:
925 * - Set the structure to all-bits-zero, for example:
926 * \code
927 * psa_hash_operation_t operation;
928 * memset(&operation, 0, sizeof(operation));
929 * \endcode
930 * - Initialize the structure to logical zero values, for example:
931 * \code
932 * psa_hash_operation_t operation = {0};
933 * \endcode
934 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
935 * for example:
936 * \code
937 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
938 * \endcode
939 * - Assign the result of the function psa_hash_operation_init()
940 * to the structure, for example:
941 * \code
942 * psa_hash_operation_t operation;
943 * operation = psa_hash_operation_init();
944 * \endcode
945 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100946 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100947 * make any assumptions about the content of this structure except
948 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100949typedef struct psa_hash_operation_s psa_hash_operation_t;
950
Jaeden Amero6a25b412019-01-04 11:47:44 +0000951/** \def PSA_HASH_OPERATION_INIT
952 *
953 * This macro returns a suitable initializer for a hash operation object
954 * of type #psa_hash_operation_t.
955 */
956#ifdef __DOXYGEN_ONLY__
957/* This is an example definition for documentation purposes.
958 * Implementations should define a suitable value in `crypto_struct.h`.
959 */
960#define PSA_HASH_OPERATION_INIT {0}
961#endif
962
963/** Return an initial value for a hash operation object.
964 */
965static psa_hash_operation_t psa_hash_operation_init(void);
966
Gilles Peskinef45adda2019-01-14 18:29:18 +0100967/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100968 *
969 * The sequence of operations to calculate a hash (message digest)
970 * is as follows:
971 * -# Allocate an operation object which will be passed to all the functions
972 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000973 * -# Initialize the operation object with one of the methods described in the
974 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200975 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100976 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100977 * of the message each time. The hash that is calculated is the hash
978 * of the concatenation of these messages in order.
979 * -# To calculate the hash, call psa_hash_finish().
980 * To compare the hash with an expected value, call psa_hash_verify().
981 *
982 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000983 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100984 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200985 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100986 * eventually terminate the operation. The following events terminate an
987 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100988 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100989 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100990 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000991 * \param[in,out] operation The operation object to set up. It must have
992 * been initialized as per the documentation for
993 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200994 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
995 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100996 *
Gilles Peskine28538492018-07-11 17:34:00 +0200997 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100998 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200999 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001000 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001001 * \retval #PSA_ERROR_BAD_STATE
1002 * The operation state is not valid (already set up and not
1003 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001004 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1005 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1006 * \retval #PSA_ERROR_HARDWARE_FAILURE
1007 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001008 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001009psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001010 psa_algorithm_t alg);
1011
Gilles Peskine308b91d2018-02-08 09:47:44 +01001012/** Add a message fragment to a multipart hash operation.
1013 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001014 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001015 *
1016 * If this function returns an error status, the operation becomes inactive.
1017 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001018 * \param[in,out] operation Active hash operation.
1019 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001020 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001021 *
Gilles Peskine28538492018-07-11 17:34:00 +02001022 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001023 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001024 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001025 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001026 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1027 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1028 * \retval #PSA_ERROR_HARDWARE_FAILURE
1029 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001030 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001031psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1032 const uint8_t *input,
1033 size_t input_length);
1034
Gilles Peskine308b91d2018-02-08 09:47:44 +01001035/** Finish the calculation of the hash of a message.
1036 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001037 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001038 * This function calculates the hash of the message formed by concatenating
1039 * the inputs passed to preceding calls to psa_hash_update().
1040 *
1041 * When this function returns, the operation becomes inactive.
1042 *
1043 * \warning Applications should not call this function if they expect
1044 * a specific value for the hash. Call psa_hash_verify() instead.
1045 * Beware that comparing integrity or authenticity data such as
1046 * hash values with a function such as \c memcmp is risky
1047 * because the time taken by the comparison may leak information
1048 * about the hashed data which could allow an attacker to guess
1049 * a valid hash and thereby bypass security controls.
1050 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001051 * \param[in,out] operation Active hash operation.
1052 * \param[out] hash Buffer where the hash is to be written.
1053 * \param hash_size Size of the \p hash buffer in bytes.
1054 * \param[out] hash_length On success, the number of bytes
1055 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001056 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001057 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001058 *
Gilles Peskine28538492018-07-11 17:34:00 +02001059 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001060 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001061 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001062 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001063 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001064 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001065 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001066 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001067 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1068 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1069 * \retval #PSA_ERROR_HARDWARE_FAILURE
1070 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001071 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001072psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1073 uint8_t *hash,
1074 size_t hash_size,
1075 size_t *hash_length);
1076
Gilles Peskine308b91d2018-02-08 09:47:44 +01001077/** Finish the calculation of the hash of a message and compare it with
1078 * an expected value.
1079 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001080 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001081 * This function calculates the hash of the message formed by concatenating
1082 * the inputs passed to preceding calls to psa_hash_update(). It then
1083 * compares the calculated hash with the expected hash passed as a
1084 * parameter to this function.
1085 *
1086 * When this function returns, the operation becomes inactive.
1087 *
Gilles Peskine19067982018-03-20 17:54:53 +01001088 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001089 * comparison between the actual hash and the expected hash is performed
1090 * in constant time.
1091 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001092 * \param[in,out] operation Active hash operation.
1093 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001094 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001095 *
Gilles Peskine28538492018-07-11 17:34:00 +02001096 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001097 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001098 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001099 * The hash of the message was calculated successfully, but it
1100 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001101 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001102 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001103 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1104 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1105 * \retval #PSA_ERROR_HARDWARE_FAILURE
1106 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001107 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001108psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1109 const uint8_t *hash,
1110 size_t hash_length);
1111
Gilles Peskine308b91d2018-02-08 09:47:44 +01001112/** Abort a hash operation.
1113 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001114 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001115 * \p operation structure itself. Once aborted, the operation object
1116 * can be reused for another operation by calling
1117 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001118 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001119 * You may call this function any time after the operation object has
1120 * been initialized by any of the following methods:
1121 * - A call to psa_hash_setup(), whether it succeeds or not.
1122 * - Initializing the \c struct to all-bits-zero.
1123 * - Initializing the \c struct to logical zeros, e.g.
1124 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001125 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001126 * In particular, calling psa_hash_abort() after the operation has been
1127 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1128 * psa_hash_verify() is safe and has no effect.
1129 *
1130 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001131 *
Gilles Peskine28538492018-07-11 17:34:00 +02001132 * \retval #PSA_SUCCESS
1133 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001134 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001135 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1136 * \retval #PSA_ERROR_HARDWARE_FAILURE
1137 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001138 */
1139psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001140
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001141/** Clone a hash operation.
1142 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001143 * This function copies the state of an ongoing hash operation to
1144 * a new operation object. In other words, this function is equivalent
1145 * to calling psa_hash_setup() on \p target_operation with the same
1146 * algorithm that \p source_operation was set up for, then
1147 * psa_hash_update() on \p target_operation with the same input that
1148 * that was passed to \p source_operation. After this function returns, the
1149 * two objects are independent, i.e. subsequent calls involving one of
1150 * the objects do not affect the other object.
1151 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001152 * \param[in] source_operation The active hash operation to clone.
1153 * \param[in,out] target_operation The operation object to set up.
1154 * It must be initialized but not active.
1155 *
1156 * \retval #PSA_SUCCESS
1157 * \retval #PSA_ERROR_BAD_STATE
1158 * \p source_operation is not an active hash operation.
1159 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001160 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001161 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1162 * \retval #PSA_ERROR_HARDWARE_FAILURE
1163 * \retval #PSA_ERROR_TAMPERING_DETECTED
1164 */
1165psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1166 psa_hash_operation_t *target_operation);
1167
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001168/**@}*/
1169
Gilles Peskine8c9def32018-02-08 10:02:12 +01001170/** \defgroup MAC Message authentication codes
1171 * @{
1172 */
1173
Gilles Peskine69647a42019-01-14 20:18:12 +01001174/** Calculate the MAC (message authentication code) of a message.
1175 *
1176 * \note To verify the MAC of a message against an
1177 * expected value, use psa_mac_verify() instead.
1178 * Beware that comparing integrity or authenticity data such as
1179 * MAC values with a function such as \c memcmp is risky
1180 * because the time taken by the comparison may leak information
1181 * about the MAC value which could allow an attacker to guess
1182 * a valid MAC and thereby bypass security controls.
1183 *
1184 * \param handle Handle to the key to use for the operation.
1185 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001186 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001187 * \param[in] input Buffer containing the input message.
1188 * \param input_length Size of the \p input buffer in bytes.
1189 * \param[out] mac Buffer where the MAC value is to be written.
1190 * \param mac_size Size of the \p mac buffer in bytes.
1191 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001192 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001193 *
1194 * \retval #PSA_SUCCESS
1195 * Success.
1196 * \retval #PSA_ERROR_INVALID_HANDLE
1197 * \retval #PSA_ERROR_EMPTY_SLOT
1198 * \retval #PSA_ERROR_NOT_PERMITTED
1199 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001200 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001201 * \retval #PSA_ERROR_NOT_SUPPORTED
1202 * \p alg is not supported or is not a MAC algorithm.
1203 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1204 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1205 * \retval #PSA_ERROR_HARDWARE_FAILURE
1206 * \retval #PSA_ERROR_TAMPERING_DETECTED
1207 * \retval #PSA_ERROR_BAD_STATE
1208 * The library has not been previously initialized by psa_crypto_init().
1209 * It is implementation-dependent whether a failure to initialize
1210 * results in this error code.
1211 */
1212psa_status_t psa_mac_compute(psa_key_handle_t handle,
1213 psa_algorithm_t alg,
1214 const uint8_t *input,
1215 size_t input_length,
1216 uint8_t *mac,
1217 size_t mac_size,
1218 size_t *mac_length);
1219
1220/** Calculate the MAC of a message and compare it with a reference value.
1221 *
1222 * \param handle Handle to the key to use for the operation.
1223 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001224 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001225 * \param[in] input Buffer containing the input message.
1226 * \param input_length Size of the \p input buffer in bytes.
1227 * \param[out] mac Buffer containing the expected MAC value.
1228 * \param mac_length Size of the \p mac buffer in bytes.
1229 *
1230 * \retval #PSA_SUCCESS
1231 * The expected MAC is identical to the actual MAC of the input.
1232 * \retval #PSA_ERROR_INVALID_SIGNATURE
1233 * The MAC of the message was calculated successfully, but it
1234 * differs from the expected value.
1235 * \retval #PSA_ERROR_INVALID_HANDLE
1236 * \retval #PSA_ERROR_EMPTY_SLOT
1237 * \retval #PSA_ERROR_NOT_PERMITTED
1238 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001239 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001240 * \retval #PSA_ERROR_NOT_SUPPORTED
1241 * \p alg is not supported or is not a MAC algorithm.
1242 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1243 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1244 * \retval #PSA_ERROR_HARDWARE_FAILURE
1245 * \retval #PSA_ERROR_TAMPERING_DETECTED
1246 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001247psa_status_t psa_mac_verify(psa_key_handle_t handle,
1248 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001249 const uint8_t *input,
1250 size_t input_length,
1251 const uint8_t *mac,
1252 const size_t mac_length);
1253
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001254/** The type of the state data structure for multipart MAC operations.
1255 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001256 * Before calling any function on a MAC operation object, the application must
1257 * initialize it by any of the following means:
1258 * - Set the structure to all-bits-zero, for example:
1259 * \code
1260 * psa_mac_operation_t operation;
1261 * memset(&operation, 0, sizeof(operation));
1262 * \endcode
1263 * - Initialize the structure to logical zero values, for example:
1264 * \code
1265 * psa_mac_operation_t operation = {0};
1266 * \endcode
1267 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1268 * for example:
1269 * \code
1270 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1271 * \endcode
1272 * - Assign the result of the function psa_mac_operation_init()
1273 * to the structure, for example:
1274 * \code
1275 * psa_mac_operation_t operation;
1276 * operation = psa_mac_operation_init();
1277 * \endcode
1278 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001279 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001280 * make any assumptions about the content of this structure except
1281 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001282typedef struct psa_mac_operation_s psa_mac_operation_t;
1283
Jaeden Amero769ce272019-01-04 11:48:03 +00001284/** \def PSA_MAC_OPERATION_INIT
1285 *
1286 * This macro returns a suitable initializer for a MAC operation object of type
1287 * #psa_mac_operation_t.
1288 */
1289#ifdef __DOXYGEN_ONLY__
1290/* This is an example definition for documentation purposes.
1291 * Implementations should define a suitable value in `crypto_struct.h`.
1292 */
1293#define PSA_MAC_OPERATION_INIT {0}
1294#endif
1295
1296/** Return an initial value for a MAC operation object.
1297 */
1298static psa_mac_operation_t psa_mac_operation_init(void);
1299
Gilles Peskinef45adda2019-01-14 18:29:18 +01001300/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001301 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001302 * This function sets up the calculation of the MAC
1303 * (message authentication code) of a byte string.
1304 * To verify the MAC of a message against an
1305 * expected value, use psa_mac_verify_setup() instead.
1306 *
1307 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001308 * -# Allocate an operation object which will be passed to all the functions
1309 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001310 * -# Initialize the operation object with one of the methods described in the
1311 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001312 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001313 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1314 * of the message each time. The MAC that is calculated is the MAC
1315 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001316 * -# At the end of the message, call psa_mac_sign_finish() to finish
1317 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001318 *
1319 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001320 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001321 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001322 * After a successful call to psa_mac_sign_setup(), the application must
1323 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001324 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001325 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001326 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001327 * \param[in,out] operation The operation object to set up. It must have
1328 * been initialized as per the documentation for
1329 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001330 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001331 * It must remain valid until the operation
1332 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001333 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001334 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001335 *
Gilles Peskine28538492018-07-11 17:34:00 +02001336 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001337 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001338 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001339 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001340 * \retval #PSA_ERROR_NOT_PERMITTED
1341 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001342 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001343 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001344 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001345 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1346 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1347 * \retval #PSA_ERROR_HARDWARE_FAILURE
1348 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001349 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001350 * The operation state is not valid (already set up and not
1351 * subsequently completed).
1352 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001353 * The library has not been previously initialized by psa_crypto_init().
1354 * It is implementation-dependent whether a failure to initialize
1355 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001356 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001357psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001358 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001359 psa_algorithm_t alg);
1360
Gilles Peskinef45adda2019-01-14 18:29:18 +01001361/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001362 *
1363 * This function sets up the verification of the MAC
1364 * (message authentication code) of a byte string against an expected value.
1365 *
1366 * The sequence of operations to verify a MAC is as follows:
1367 * -# Allocate an operation object which will be passed to all the functions
1368 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001369 * -# Initialize the operation object with one of the methods described in the
1370 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001371 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001372 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1373 * of the message each time. The MAC that is calculated is the MAC
1374 * of the concatenation of these messages in order.
1375 * -# At the end of the message, call psa_mac_verify_finish() to finish
1376 * calculating the actual MAC of the message and verify it against
1377 * the expected value.
1378 *
1379 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001380 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001381 *
1382 * After a successful call to psa_mac_verify_setup(), the application must
1383 * eventually terminate the operation through one of the following methods:
1384 * - A failed call to psa_mac_update().
1385 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1386 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001387 * \param[in,out] operation The operation object to set up. It must have
1388 * been initialized as per the documentation for
1389 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001390 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001391 * It must remain valid until the operation
1392 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001393 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1394 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001395 *
Gilles Peskine28538492018-07-11 17:34:00 +02001396 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001397 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001398 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001399 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001400 * \retval #PSA_ERROR_NOT_PERMITTED
1401 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001402 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001403 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001404 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001405 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1406 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1407 * \retval #PSA_ERROR_HARDWARE_FAILURE
1408 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001409 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001410 * The operation state is not valid (already set up and not
1411 * subsequently completed).
1412 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001413 * The library has not been previously initialized by psa_crypto_init().
1414 * It is implementation-dependent whether a failure to initialize
1415 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001416 */
1417psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001418 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001419 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001420
Gilles Peskinedcd14942018-07-12 00:30:52 +02001421/** Add a message fragment to a multipart MAC operation.
1422 *
1423 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1424 * before calling this function.
1425 *
1426 * If this function returns an error status, the operation becomes inactive.
1427 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001428 * \param[in,out] operation Active MAC operation.
1429 * \param[in] input Buffer containing the message fragment to add to
1430 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001431 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001432 *
1433 * \retval #PSA_SUCCESS
1434 * Success.
1435 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001436 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001437 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1438 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1439 * \retval #PSA_ERROR_HARDWARE_FAILURE
1440 * \retval #PSA_ERROR_TAMPERING_DETECTED
1441 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001442psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1443 const uint8_t *input,
1444 size_t input_length);
1445
Gilles Peskinedcd14942018-07-12 00:30:52 +02001446/** Finish the calculation of the MAC of a message.
1447 *
1448 * The application must call psa_mac_sign_setup() before calling this function.
1449 * This function calculates the MAC of the message formed by concatenating
1450 * the inputs passed to preceding calls to psa_mac_update().
1451 *
1452 * When this function returns, the operation becomes inactive.
1453 *
1454 * \warning Applications should not call this function if they expect
1455 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1456 * Beware that comparing integrity or authenticity data such as
1457 * MAC values with a function such as \c memcmp is risky
1458 * because the time taken by the comparison may leak information
1459 * about the MAC value which could allow an attacker to guess
1460 * a valid MAC and thereby bypass security controls.
1461 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001462 * \param[in,out] operation Active MAC operation.
1463 * \param[out] mac Buffer where the MAC value is to be written.
1464 * \param mac_size Size of the \p mac buffer in bytes.
1465 * \param[out] mac_length On success, the number of bytes
1466 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001467 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001468 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001469 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001470 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001471 *
1472 * \retval #PSA_SUCCESS
1473 * Success.
1474 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001475 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001476 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001477 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001478 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1479 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1480 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1481 * \retval #PSA_ERROR_HARDWARE_FAILURE
1482 * \retval #PSA_ERROR_TAMPERING_DETECTED
1483 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001484psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1485 uint8_t *mac,
1486 size_t mac_size,
1487 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001488
Gilles Peskinedcd14942018-07-12 00:30:52 +02001489/** Finish the calculation of the MAC of a message and compare it with
1490 * an expected value.
1491 *
1492 * The application must call psa_mac_verify_setup() before calling this function.
1493 * This function calculates the MAC of the message formed by concatenating
1494 * the inputs passed to preceding calls to psa_mac_update(). It then
1495 * compares the calculated MAC with the expected MAC passed as a
1496 * parameter to this function.
1497 *
1498 * When this function returns, the operation becomes inactive.
1499 *
1500 * \note Implementations shall make the best effort to ensure that the
1501 * comparison between the actual MAC and the expected MAC is performed
1502 * in constant time.
1503 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001504 * \param[in,out] operation Active MAC operation.
1505 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001506 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001507 *
1508 * \retval #PSA_SUCCESS
1509 * The expected MAC is identical to the actual MAC of the message.
1510 * \retval #PSA_ERROR_INVALID_SIGNATURE
1511 * The MAC of the message was calculated successfully, but it
1512 * differs from the expected MAC.
1513 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001514 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001515 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1516 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1517 * \retval #PSA_ERROR_HARDWARE_FAILURE
1518 * \retval #PSA_ERROR_TAMPERING_DETECTED
1519 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001520psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1521 const uint8_t *mac,
1522 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001523
Gilles Peskinedcd14942018-07-12 00:30:52 +02001524/** Abort a MAC operation.
1525 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001526 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001527 * \p operation structure itself. Once aborted, the operation object
1528 * can be reused for another operation by calling
1529 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001530 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001531 * You may call this function any time after the operation object has
1532 * been initialized by any of the following methods:
1533 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1534 * it succeeds or not.
1535 * - Initializing the \c struct to all-bits-zero.
1536 * - Initializing the \c struct to logical zeros, e.g.
1537 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001538 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001539 * In particular, calling psa_mac_abort() after the operation has been
1540 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1541 * psa_mac_verify_finish() is safe and has no effect.
1542 *
1543 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001544 *
1545 * \retval #PSA_SUCCESS
1546 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001547 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001548 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1549 * \retval #PSA_ERROR_HARDWARE_FAILURE
1550 * \retval #PSA_ERROR_TAMPERING_DETECTED
1551 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001552psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1553
1554/**@}*/
1555
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001556/** \defgroup cipher Symmetric ciphers
1557 * @{
1558 */
1559
Gilles Peskine69647a42019-01-14 20:18:12 +01001560/** Encrypt a message using a symmetric cipher.
1561 *
1562 * This function encrypts a message with a random IV (initialization
1563 * vector).
1564 *
1565 * \param handle Handle to the key to use for the operation.
1566 * It must remain valid until the operation
1567 * terminates.
1568 * \param alg The cipher algorithm to compute
1569 * (\c PSA_ALG_XXX value such that
1570 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1571 * \param[in] input Buffer containing the message to encrypt.
1572 * \param input_length Size of the \p input buffer in bytes.
1573 * \param[out] output Buffer where the output is to be written.
1574 * The output contains the IV followed by
1575 * the ciphertext proper.
1576 * \param output_size Size of the \p output buffer in bytes.
1577 * \param[out] output_length On success, the number of bytes
1578 * that make up the output.
1579 *
1580 * \retval #PSA_SUCCESS
1581 * Success.
1582 * \retval #PSA_ERROR_INVALID_HANDLE
1583 * \retval #PSA_ERROR_EMPTY_SLOT
1584 * \retval #PSA_ERROR_NOT_PERMITTED
1585 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001586 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001587 * \retval #PSA_ERROR_NOT_SUPPORTED
1588 * \p alg is not supported or is not a cipher algorithm.
1589 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1590 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1591 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1592 * \retval #PSA_ERROR_HARDWARE_FAILURE
1593 * \retval #PSA_ERROR_TAMPERING_DETECTED
1594 */
1595psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1596 psa_algorithm_t alg,
1597 const uint8_t *input,
1598 size_t input_length,
1599 uint8_t *output,
1600 size_t output_size,
1601 size_t *output_length);
1602
1603/** Decrypt a message using a symmetric cipher.
1604 *
1605 * This function decrypts a message encrypted with a symmetric cipher.
1606 *
1607 * \param handle Handle to the key to use for the operation.
1608 * It must remain valid until the operation
1609 * terminates.
1610 * \param alg The cipher algorithm to compute
1611 * (\c PSA_ALG_XXX value such that
1612 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1613 * \param[in] input Buffer containing the message to decrypt.
1614 * This consists of the IV followed by the
1615 * ciphertext proper.
1616 * \param input_length Size of the \p input buffer in bytes.
1617 * \param[out] output Buffer where the plaintext is to be written.
1618 * \param output_size Size of the \p output buffer in bytes.
1619 * \param[out] output_length On success, the number of bytes
1620 * that make up the output.
1621 *
1622 * \retval #PSA_SUCCESS
1623 * Success.
1624 * \retval #PSA_ERROR_INVALID_HANDLE
1625 * \retval #PSA_ERROR_EMPTY_SLOT
1626 * \retval #PSA_ERROR_NOT_PERMITTED
1627 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001628 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001629 * \retval #PSA_ERROR_NOT_SUPPORTED
1630 * \p alg is not supported or is not a cipher algorithm.
1631 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1632 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1633 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1634 * \retval #PSA_ERROR_HARDWARE_FAILURE
1635 * \retval #PSA_ERROR_TAMPERING_DETECTED
1636 */
1637psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1638 psa_algorithm_t alg,
1639 const uint8_t *input,
1640 size_t input_length,
1641 uint8_t *output,
1642 size_t output_size,
1643 size_t *output_length);
1644
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001645/** The type of the state data structure for multipart cipher operations.
1646 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001647 * Before calling any function on a cipher operation object, the application
1648 * must initialize it by any of the following means:
1649 * - Set the structure to all-bits-zero, for example:
1650 * \code
1651 * psa_cipher_operation_t operation;
1652 * memset(&operation, 0, sizeof(operation));
1653 * \endcode
1654 * - Initialize the structure to logical zero values, for example:
1655 * \code
1656 * psa_cipher_operation_t operation = {0};
1657 * \endcode
1658 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1659 * for example:
1660 * \code
1661 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1662 * \endcode
1663 * - Assign the result of the function psa_cipher_operation_init()
1664 * to the structure, for example:
1665 * \code
1666 * psa_cipher_operation_t operation;
1667 * operation = psa_cipher_operation_init();
1668 * \endcode
1669 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001670 * This is an implementation-defined \c struct. Applications should not
1671 * make any assumptions about the content of this structure except
1672 * as directed by the documentation of a specific implementation. */
1673typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1674
Jaeden Amero5bae2272019-01-04 11:48:27 +00001675/** \def PSA_CIPHER_OPERATION_INIT
1676 *
1677 * This macro returns a suitable initializer for a cipher operation object of
1678 * type #psa_cipher_operation_t.
1679 */
1680#ifdef __DOXYGEN_ONLY__
1681/* This is an example definition for documentation purposes.
1682 * Implementations should define a suitable value in `crypto_struct.h`.
1683 */
1684#define PSA_CIPHER_OPERATION_INIT {0}
1685#endif
1686
1687/** Return an initial value for a cipher operation object.
1688 */
1689static psa_cipher_operation_t psa_cipher_operation_init(void);
1690
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001691/** Set the key for a multipart symmetric encryption operation.
1692 *
1693 * The sequence of operations to encrypt a message with a symmetric cipher
1694 * is as follows:
1695 * -# Allocate an operation object which will be passed to all the functions
1696 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001697 * -# Initialize the operation object with one of the methods described in the
1698 * documentation for #psa_cipher_operation_t, e.g.
1699 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001700 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001701 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001702 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001703 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001704 * requires a specific IV value.
1705 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1706 * of the message each time.
1707 * -# Call psa_cipher_finish().
1708 *
1709 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001710 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001711 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001712 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001713 * eventually terminate the operation. The following events terminate an
1714 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001715 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001716 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001717 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001718 * \param[in,out] operation The operation object to set up. It must have
1719 * been initialized as per the documentation for
1720 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001721 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001722 * It must remain valid until the operation
1723 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001724 * \param alg The cipher algorithm to compute
1725 * (\c PSA_ALG_XXX value such that
1726 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001727 *
Gilles Peskine28538492018-07-11 17:34:00 +02001728 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001729 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001730 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001731 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001732 * \retval #PSA_ERROR_NOT_PERMITTED
1733 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001734 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001735 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001736 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001737 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1738 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1739 * \retval #PSA_ERROR_HARDWARE_FAILURE
1740 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001741 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001742 * The operation state is not valid (already set up and not
1743 * subsequently completed).
1744 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001745 * The library has not been previously initialized by psa_crypto_init().
1746 * It is implementation-dependent whether a failure to initialize
1747 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001748 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001749psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001750 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001751 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001752
1753/** Set the key for a multipart symmetric decryption operation.
1754 *
1755 * The sequence of operations to decrypt a message with a symmetric cipher
1756 * is as follows:
1757 * -# Allocate an operation object which will be passed to all the functions
1758 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001759 * -# Initialize the operation object with one of the methods described in the
1760 * documentation for #psa_cipher_operation_t, e.g.
1761 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001762 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001763 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001764 * decryption. If the IV is prepended to the ciphertext, you can call
1765 * psa_cipher_update() on a buffer containing the IV followed by the
1766 * beginning of the message.
1767 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1768 * of the message each time.
1769 * -# Call psa_cipher_finish().
1770 *
1771 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001772 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001773 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001774 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001775 * eventually terminate the operation. The following events terminate an
1776 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001777 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001778 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001779 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001780 * \param[in,out] operation The operation object to set up. It must have
1781 * been initialized as per the documentation for
1782 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001783 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001784 * It must remain valid until the operation
1785 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001786 * \param alg The cipher algorithm to compute
1787 * (\c PSA_ALG_XXX value such that
1788 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001789 *
Gilles Peskine28538492018-07-11 17:34:00 +02001790 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001791 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001792 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001793 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001794 * \retval #PSA_ERROR_NOT_PERMITTED
1795 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001796 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001797 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001798 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001799 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1800 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1801 * \retval #PSA_ERROR_HARDWARE_FAILURE
1802 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001803 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001804 * The operation state is not valid (already set up and not
1805 * subsequently completed).
1806 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001807 * The library has not been previously initialized by psa_crypto_init().
1808 * It is implementation-dependent whether a failure to initialize
1809 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001810 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001811psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001812 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001813 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001814
Gilles Peskinedcd14942018-07-12 00:30:52 +02001815/** Generate an IV for a symmetric encryption operation.
1816 *
1817 * This function generates a random IV (initialization vector), nonce
1818 * or initial counter value for the encryption operation as appropriate
1819 * for the chosen algorithm, key type and key size.
1820 *
1821 * The application must call psa_cipher_encrypt_setup() before
1822 * calling this function.
1823 *
1824 * If this function returns an error status, the operation becomes inactive.
1825 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001826 * \param[in,out] operation Active cipher operation.
1827 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001828 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001829 * \param[out] iv_length On success, the number of bytes of the
1830 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001831 *
1832 * \retval #PSA_SUCCESS
1833 * Success.
1834 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001835 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001836 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001837 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001838 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1839 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1840 * \retval #PSA_ERROR_HARDWARE_FAILURE
1841 * \retval #PSA_ERROR_TAMPERING_DETECTED
1842 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001843psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1844 unsigned char *iv,
1845 size_t iv_size,
1846 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001847
Gilles Peskinedcd14942018-07-12 00:30:52 +02001848/** Set the IV for a symmetric encryption or decryption operation.
1849 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001850 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001851 * or initial counter value for the encryption or decryption operation.
1852 *
1853 * The application must call psa_cipher_encrypt_setup() before
1854 * calling this function.
1855 *
1856 * If this function returns an error status, the operation becomes inactive.
1857 *
1858 * \note When encrypting, applications should use psa_cipher_generate_iv()
1859 * instead of this function, unless implementing a protocol that requires
1860 * a non-random IV.
1861 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001862 * \param[in,out] operation Active cipher operation.
1863 * \param[in] iv Buffer containing the IV to use.
1864 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001865 *
1866 * \retval #PSA_SUCCESS
1867 * Success.
1868 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001869 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001870 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001871 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001872 * or the chosen algorithm does not use an IV.
1873 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1874 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1875 * \retval #PSA_ERROR_HARDWARE_FAILURE
1876 * \retval #PSA_ERROR_TAMPERING_DETECTED
1877 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001878psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1879 const unsigned char *iv,
1880 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001881
Gilles Peskinedcd14942018-07-12 00:30:52 +02001882/** Encrypt or decrypt a message fragment in an active cipher operation.
1883 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001884 * Before calling this function, you must:
1885 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1886 * The choice of setup function determines whether this function
1887 * encrypts or decrypts its input.
1888 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1889 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001890 *
1891 * If this function returns an error status, the operation becomes inactive.
1892 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001893 * \param[in,out] operation Active cipher operation.
1894 * \param[in] input Buffer containing the message fragment to
1895 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001896 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001897 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001898 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001899 * \param[out] output_length On success, the number of bytes
1900 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001901 *
1902 * \retval #PSA_SUCCESS
1903 * Success.
1904 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001905 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001906 * not set, or already completed).
1907 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1908 * The size of the \p output buffer is too small.
1909 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1910 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1911 * \retval #PSA_ERROR_HARDWARE_FAILURE
1912 * \retval #PSA_ERROR_TAMPERING_DETECTED
1913 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001914psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1915 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001916 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001917 unsigned char *output,
1918 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001919 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001920
Gilles Peskinedcd14942018-07-12 00:30:52 +02001921/** Finish encrypting or decrypting a message in a cipher operation.
1922 *
1923 * The application must call psa_cipher_encrypt_setup() or
1924 * psa_cipher_decrypt_setup() before calling this function. The choice
1925 * of setup function determines whether this function encrypts or
1926 * decrypts its input.
1927 *
1928 * This function finishes the encryption or decryption of the message
1929 * formed by concatenating the inputs passed to preceding calls to
1930 * psa_cipher_update().
1931 *
1932 * When this function returns, the operation becomes inactive.
1933 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001934 * \param[in,out] operation Active cipher operation.
1935 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001936 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001937 * \param[out] output_length On success, the number of bytes
1938 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001939 *
1940 * \retval #PSA_SUCCESS
1941 * Success.
1942 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001943 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001944 * not set, or already completed).
1945 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1946 * The size of the \p output buffer is too small.
1947 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1948 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1949 * \retval #PSA_ERROR_HARDWARE_FAILURE
1950 * \retval #PSA_ERROR_TAMPERING_DETECTED
1951 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001952psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001953 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001954 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001955 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001956
Gilles Peskinedcd14942018-07-12 00:30:52 +02001957/** Abort a cipher operation.
1958 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001959 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001960 * \p operation structure itself. Once aborted, the operation object
1961 * can be reused for another operation by calling
1962 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001963 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001964 * You may call this function any time after the operation object has
1965 * been initialized by any of the following methods:
1966 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1967 * whether it succeeds or not.
1968 * - Initializing the \c struct to all-bits-zero.
1969 * - Initializing the \c struct to logical zeros, e.g.
1970 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001971 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001972 * In particular, calling psa_cipher_abort() after the operation has been
1973 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1974 * is safe and has no effect.
1975 *
1976 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001977 *
1978 * \retval #PSA_SUCCESS
1979 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001980 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001981 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1982 * \retval #PSA_ERROR_HARDWARE_FAILURE
1983 * \retval #PSA_ERROR_TAMPERING_DETECTED
1984 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001985psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1986
1987/**@}*/
1988
Gilles Peskine3b555712018-03-03 21:27:57 +01001989/** \defgroup aead Authenticated encryption with associated data (AEAD)
1990 * @{
1991 */
1992
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001993/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001994 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001995 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001996 * \param alg The AEAD algorithm to compute
1997 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001998 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001999 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002000 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002001 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002002 * but not encrypted.
2003 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002004 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002005 * encrypted.
2006 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002007 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002008 * encrypted data. The additional data is not
2009 * part of this output. For algorithms where the
2010 * encrypted data and the authentication tag
2011 * are defined as separate outputs, the
2012 * authentication tag is appended to the
2013 * encrypted data.
2014 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2015 * This must be at least
2016 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2017 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002018 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002019 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002020 *
Gilles Peskine28538492018-07-11 17:34:00 +02002021 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002022 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002023 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002024 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002025 * \retval #PSA_ERROR_NOT_PERMITTED
2026 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002027 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002028 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002029 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002030 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2031 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2032 * \retval #PSA_ERROR_HARDWARE_FAILURE
2033 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002034 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002035 * The library has not been previously initialized by psa_crypto_init().
2036 * It is implementation-dependent whether a failure to initialize
2037 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002038 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002039psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002040 psa_algorithm_t alg,
2041 const uint8_t *nonce,
2042 size_t nonce_length,
2043 const uint8_t *additional_data,
2044 size_t additional_data_length,
2045 const uint8_t *plaintext,
2046 size_t plaintext_length,
2047 uint8_t *ciphertext,
2048 size_t ciphertext_size,
2049 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002050
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002051/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002052 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002053 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002054 * \param alg The AEAD algorithm to compute
2055 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002056 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002057 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002058 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002059 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002060 * but not encrypted.
2061 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002062 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002063 * encrypted. For algorithms where the
2064 * encrypted data and the authentication tag
2065 * are defined as separate inputs, the buffer
2066 * must contain the encrypted data followed
2067 * by the authentication tag.
2068 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002069 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002070 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2071 * This must be at least
2072 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2073 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002074 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002075 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002076 *
Gilles Peskine28538492018-07-11 17:34:00 +02002077 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002078 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002079 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002080 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002081 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002082 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002083 * \retval #PSA_ERROR_NOT_PERMITTED
2084 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002085 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002086 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002087 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002088 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2089 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2090 * \retval #PSA_ERROR_HARDWARE_FAILURE
2091 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002092 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002093 * The library has not been previously initialized by psa_crypto_init().
2094 * It is implementation-dependent whether a failure to initialize
2095 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002096 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002097psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002098 psa_algorithm_t alg,
2099 const uint8_t *nonce,
2100 size_t nonce_length,
2101 const uint8_t *additional_data,
2102 size_t additional_data_length,
2103 const uint8_t *ciphertext,
2104 size_t ciphertext_length,
2105 uint8_t *plaintext,
2106 size_t plaintext_size,
2107 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002108
Gilles Peskine30a9e412019-01-14 18:36:12 +01002109/** The type of the state data structure for multipart AEAD operations.
2110 *
2111 * Before calling any function on an AEAD operation object, the application
2112 * must initialize it by any of the following means:
2113 * - Set the structure to all-bits-zero, for example:
2114 * \code
2115 * psa_aead_operation_t operation;
2116 * memset(&operation, 0, sizeof(operation));
2117 * \endcode
2118 * - Initialize the structure to logical zero values, for example:
2119 * \code
2120 * psa_aead_operation_t operation = {0};
2121 * \endcode
2122 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2123 * for example:
2124 * \code
2125 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2126 * \endcode
2127 * - Assign the result of the function psa_aead_operation_init()
2128 * to the structure, for example:
2129 * \code
2130 * psa_aead_operation_t operation;
2131 * operation = psa_aead_operation_init();
2132 * \endcode
2133 *
2134 * This is an implementation-defined \c struct. Applications should not
2135 * make any assumptions about the content of this structure except
2136 * as directed by the documentation of a specific implementation. */
2137typedef struct psa_aead_operation_s psa_aead_operation_t;
2138
2139/** \def PSA_AEAD_OPERATION_INIT
2140 *
2141 * This macro returns a suitable initializer for an AEAD operation object of
2142 * type #psa_aead_operation_t.
2143 */
2144#ifdef __DOXYGEN_ONLY__
2145/* This is an example definition for documentation purposes.
2146 * Implementations should define a suitable value in `crypto_struct.h`.
2147 */
2148#define PSA_AEAD_OPERATION_INIT {0}
2149#endif
2150
2151/** Return an initial value for an AEAD operation object.
2152 */
2153static psa_aead_operation_t psa_aead_operation_init(void);
2154
2155/** Set the key for a multipart authenticated encryption operation.
2156 *
2157 * The sequence of operations to encrypt a message with authentication
2158 * is as follows:
2159 * -# Allocate an operation object which will be passed to all the functions
2160 * listed here.
2161 * -# Initialize the operation object with one of the methods described in the
2162 * documentation for #psa_aead_operation_t, e.g.
2163 * PSA_AEAD_OPERATION_INIT.
2164 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002165 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2166 * inputs to the subsequent calls to psa_aead_update_ad() and
2167 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2168 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002169 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2170 * generate or set the nonce. You should use
2171 * psa_aead_generate_nonce() unless the protocol you are implementing
2172 * requires a specific nonce value.
2173 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2174 * of the non-encrypted additional authenticated data each time.
2175 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002176 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002177 * -# Call psa_aead_finish().
2178 *
2179 * The application may call psa_aead_abort() at any time after the operation
2180 * has been initialized.
2181 *
2182 * After a successful call to psa_aead_encrypt_setup(), the application must
2183 * eventually terminate the operation. The following events terminate an
2184 * operation:
2185 * - A failed call to any of the \c psa_aead_xxx functions.
2186 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2187 *
2188 * \param[in,out] operation The operation object to set up. It must have
2189 * been initialized as per the documentation for
2190 * #psa_aead_operation_t and not yet in use.
2191 * \param handle Handle to the key to use for the operation.
2192 * It must remain valid until the operation
2193 * terminates.
2194 * \param alg The AEAD algorithm to compute
2195 * (\c PSA_ALG_XXX value such that
2196 * #PSA_ALG_IS_AEAD(\p alg) is true).
2197 *
2198 * \retval #PSA_SUCCESS
2199 * Success.
2200 * \retval #PSA_ERROR_INVALID_HANDLE
2201 * \retval #PSA_ERROR_EMPTY_SLOT
2202 * \retval #PSA_ERROR_NOT_PERMITTED
2203 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002204 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002205 * \retval #PSA_ERROR_NOT_SUPPORTED
2206 * \p alg is not supported or is not an AEAD algorithm.
2207 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2208 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2209 * \retval #PSA_ERROR_HARDWARE_FAILURE
2210 * \retval #PSA_ERROR_TAMPERING_DETECTED
2211 * \retval #PSA_ERROR_BAD_STATE
2212 * The library has not been previously initialized by psa_crypto_init().
2213 * It is implementation-dependent whether a failure to initialize
2214 * results in this error code.
2215 */
2216psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2217 psa_key_handle_t handle,
2218 psa_algorithm_t alg);
2219
2220/** Set the key for a multipart authenticated decryption operation.
2221 *
2222 * The sequence of operations to decrypt a message with authentication
2223 * is as follows:
2224 * -# Allocate an operation object which will be passed to all the functions
2225 * listed here.
2226 * -# Initialize the operation object with one of the methods described in the
2227 * documentation for #psa_aead_operation_t, e.g.
2228 * PSA_AEAD_OPERATION_INIT.
2229 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002230 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2231 * inputs to the subsequent calls to psa_aead_update_ad() and
2232 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2233 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002234 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2235 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2236 * of the non-encrypted additional authenticated data each time.
2237 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002238 * of the ciphertext to decrypt each time.
2239 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002240 *
2241 * The application may call psa_aead_abort() at any time after the operation
2242 * has been initialized.
2243 *
2244 * After a successful call to psa_aead_decrypt_setup(), the application must
2245 * eventually terminate the operation. The following events terminate an
2246 * operation:
2247 * - A failed call to any of the \c psa_aead_xxx functions.
2248 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2249 *
2250 * \param[in,out] operation The operation object to set up. It must have
2251 * been initialized as per the documentation for
2252 * #psa_aead_operation_t and not yet in use.
2253 * \param handle Handle to the key to use for the operation.
2254 * It must remain valid until the operation
2255 * terminates.
2256 * \param alg The AEAD algorithm to compute
2257 * (\c PSA_ALG_XXX value such that
2258 * #PSA_ALG_IS_AEAD(\p alg) is true).
2259 *
2260 * \retval #PSA_SUCCESS
2261 * Success.
2262 * \retval #PSA_ERROR_INVALID_HANDLE
2263 * \retval #PSA_ERROR_EMPTY_SLOT
2264 * \retval #PSA_ERROR_NOT_PERMITTED
2265 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002266 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002267 * \retval #PSA_ERROR_NOT_SUPPORTED
2268 * \p alg is not supported or is not an AEAD algorithm.
2269 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2270 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2271 * \retval #PSA_ERROR_HARDWARE_FAILURE
2272 * \retval #PSA_ERROR_TAMPERING_DETECTED
2273 * \retval #PSA_ERROR_BAD_STATE
2274 * The library has not been previously initialized by psa_crypto_init().
2275 * It is implementation-dependent whether a failure to initialize
2276 * results in this error code.
2277 */
2278psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2279 psa_key_handle_t handle,
2280 psa_algorithm_t alg);
2281
2282/** Generate a random nonce for an authenticated encryption operation.
2283 *
2284 * This function generates a random nonce for the authenticated encryption
2285 * operation with an appropriate size for the chosen algorithm, key type
2286 * and key size.
2287 *
2288 * The application must call psa_aead_encrypt_setup() before
2289 * calling this function.
2290 *
2291 * If this function returns an error status, the operation becomes inactive.
2292 *
2293 * \param[in,out] operation Active AEAD operation.
2294 * \param[out] nonce Buffer where the generated nonce is to be
2295 * written.
2296 * \param nonce_size Size of the \p nonce buffer in bytes.
2297 * \param[out] nonce_length On success, the number of bytes of the
2298 * generated nonce.
2299 *
2300 * \retval #PSA_SUCCESS
2301 * Success.
2302 * \retval #PSA_ERROR_BAD_STATE
2303 * The operation state is not valid (not set up, or nonce already set).
2304 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2305 * The size of the \p nonce buffer is too small.
2306 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2307 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2308 * \retval #PSA_ERROR_HARDWARE_FAILURE
2309 * \retval #PSA_ERROR_TAMPERING_DETECTED
2310 */
2311psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2312 unsigned char *nonce,
2313 size_t nonce_size,
2314 size_t *nonce_length);
2315
2316/** Set the nonce for an authenticated encryption or decryption operation.
2317 *
2318 * This function sets the nonce for the authenticated
2319 * encryption or decryption operation.
2320 *
2321 * The application must call psa_aead_encrypt_setup() before
2322 * calling this function.
2323 *
2324 * If this function returns an error status, the operation becomes inactive.
2325 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002326 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002327 * instead of this function, unless implementing a protocol that requires
2328 * a non-random IV.
2329 *
2330 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002331 * \param[in] nonce Buffer containing the nonce to use.
2332 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002333 *
2334 * \retval #PSA_SUCCESS
2335 * Success.
2336 * \retval #PSA_ERROR_BAD_STATE
2337 * The operation state is not valid (not set up, or nonce already set).
2338 * \retval #PSA_ERROR_INVALID_ARGUMENT
2339 * The size of \p nonce is not acceptable for the chosen algorithm.
2340 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2341 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2342 * \retval #PSA_ERROR_HARDWARE_FAILURE
2343 * \retval #PSA_ERROR_TAMPERING_DETECTED
2344 */
2345psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2346 const unsigned char *nonce,
2347 size_t nonce_length);
2348
Gilles Peskinebc59c852019-01-17 15:26:08 +01002349/** Declare the lengths of the message and additional data for AEAD.
2350 *
2351 * The application must call this function before calling
2352 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2353 * the operation requires it. If the algorithm does not require it,
2354 * calling this function is optional, but if this function is called
2355 * then the implementation must enforce the lengths.
2356 *
2357 * You may call this function before or after setting the nonce with
2358 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2359 *
2360 * - For #PSA_ALG_CCM, calling this function is required.
2361 * - For the other AEAD algorithms defined in this specification, calling
2362 * this function is not required.
2363 * - For vendor-defined algorithm, refer to the vendor documentation.
2364 *
2365 * \param[in,out] operation Active AEAD operation.
2366 * \param ad_length Size of the non-encrypted additional
2367 * authenticated data in bytes.
2368 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2369 *
2370 * \retval #PSA_SUCCESS
2371 * Success.
2372 * \retval #PSA_ERROR_BAD_STATE
2373 * The operation state is not valid (not set up, already completed,
2374 * or psa_aead_update_ad() or psa_aead_update() already called).
2375 * \retval #PSA_ERROR_INVALID_ARGUMENT
2376 * At least one of the lengths is not acceptable for the chosen
2377 * algorithm.
2378 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2379 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2380 * \retval #PSA_ERROR_HARDWARE_FAILURE
2381 * \retval #PSA_ERROR_TAMPERING_DETECTED
2382 */
2383psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2384 size_t ad_length,
2385 size_t plaintext_length);
2386
Gilles Peskine30a9e412019-01-14 18:36:12 +01002387/** Pass additional data to an active AEAD operation.
2388 *
2389 * Additional data is authenticated, but not encrypted.
2390 *
2391 * You may call this function multiple times to pass successive fragments
2392 * of the additional data. You may not call this function after passing
2393 * data to encrypt or decrypt with psa_aead_update().
2394 *
2395 * Before calling this function, you must:
2396 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2397 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2398 *
2399 * If this function returns an error status, the operation becomes inactive.
2400 *
2401 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2402 * there is no guarantee that the input is valid. Therefore, until
2403 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2404 * treat the input as untrusted and prepare to undo any action that
2405 * depends on the input if psa_aead_verify() returns an error status.
2406 *
2407 * \param[in,out] operation Active AEAD operation.
2408 * \param[in] input Buffer containing the fragment of
2409 * additional data.
2410 * \param input_length Size of the \p input buffer in bytes.
2411 *
2412 * \retval #PSA_SUCCESS
2413 * Success.
2414 * \retval #PSA_ERROR_BAD_STATE
2415 * The operation state is not valid (not set up, nonce not set,
2416 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002417 * \retval #PSA_ERROR_INVALID_ARGUMENT
2418 * The total input length overflows the additional data length that
2419 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002420 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2421 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2422 * \retval #PSA_ERROR_HARDWARE_FAILURE
2423 * \retval #PSA_ERROR_TAMPERING_DETECTED
2424 */
2425psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2426 const uint8_t *input,
2427 size_t input_length);
2428
2429/** Encrypt or decrypt a message fragment in an active AEAD operation.
2430 *
2431 * Before calling this function, you must:
2432 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2433 * The choice of setup function determines whether this function
2434 * encrypts or decrypts its input.
2435 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2436 * 3. Call psa_aead_update_ad() to pass all the additional data.
2437 *
2438 * If this function returns an error status, the operation becomes inactive.
2439 *
2440 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2441 * there is no guarantee that the input is valid. Therefore, until
2442 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2443 * - Do not use the output in any way other than storing it in a
2444 * confidential location. If you take any action that depends
2445 * on the tentative decrypted data, this action will need to be
2446 * undone if the input turns out not to be valid. Furthermore,
2447 * if an adversary can observe that this action took place
2448 * (for example through timing), they may be able to use this
2449 * fact as an oracle to decrypt any message encrypted with the
2450 * same key.
2451 * - In particular, do not copy the output anywhere but to a
2452 * memory or storage space that you have exclusive access to.
2453 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002454 * This function does not require the input to be aligned to any
2455 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002456 * a whole block at a time, it must consume all the input provided, but
2457 * it may delay the end of the corresponding output until a subsequent
2458 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2459 * provides sufficient input. The amount of data that can be delayed
2460 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002461 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002462 * \param[in,out] operation Active AEAD operation.
2463 * \param[in] input Buffer containing the message fragment to
2464 * encrypt or decrypt.
2465 * \param input_length Size of the \p input buffer in bytes.
2466 * \param[out] output Buffer where the output is to be written.
2467 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002468 * This must be at least
2469 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2470 * \p input_length) where \c alg is the
2471 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002472 * \param[out] output_length On success, the number of bytes
2473 * that make up the returned output.
2474 *
2475 * \retval #PSA_SUCCESS
2476 * Success.
2477 * \retval #PSA_ERROR_BAD_STATE
2478 * The operation state is not valid (not set up, nonce not set
2479 * or already completed).
2480 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2481 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002482 * You can determine a sufficient buffer size by calling
2483 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2484 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002485 * \retval #PSA_ERROR_INVALID_ARGUMENT
2486 * The total length of input to psa_aead_update_ad() so far is
2487 * less than the additional data length that was previously
2488 * specified with psa_aead_set_lengths().
2489 * \retval #PSA_ERROR_INVALID_ARGUMENT
2490 * The total input length overflows the plaintext length that
2491 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002492 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2493 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2494 * \retval #PSA_ERROR_HARDWARE_FAILURE
2495 * \retval #PSA_ERROR_TAMPERING_DETECTED
2496 */
2497psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2498 const uint8_t *input,
2499 size_t input_length,
2500 unsigned char *output,
2501 size_t output_size,
2502 size_t *output_length);
2503
2504/** Finish encrypting a message in an AEAD operation.
2505 *
2506 * The operation must have been set up with psa_aead_encrypt_setup().
2507 *
2508 * This function finishes the authentication of the additional data
2509 * formed by concatenating the inputs passed to preceding calls to
2510 * psa_aead_update_ad() with the plaintext formed by concatenating the
2511 * inputs passed to preceding calls to psa_aead_update().
2512 *
2513 * This function has two output buffers:
2514 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002515 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002516 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002517 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002518 * that the operation performs.
2519 *
2520 * When this function returns, the operation becomes inactive.
2521 *
2522 * \param[in,out] operation Active AEAD operation.
2523 * \param[out] ciphertext Buffer where the last part of the ciphertext
2524 * is to be written.
2525 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002526 * This must be at least
2527 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2528 * \c alg is the algorithm that is being
2529 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002530 * \param[out] ciphertext_length On success, the number of bytes of
2531 * returned ciphertext.
2532 * \param[out] tag Buffer where the authentication tag is
2533 * to be written.
2534 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002535 * This must be at least
2536 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2537 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002538 * \param[out] tag_length On success, the number of bytes
2539 * that make up the returned tag.
2540 *
2541 * \retval #PSA_SUCCESS
2542 * Success.
2543 * \retval #PSA_ERROR_BAD_STATE
2544 * The operation state is not valid (not set up, nonce not set,
2545 * decryption, or already completed).
2546 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002547 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002548 * You can determine a sufficient buffer size for \p ciphertext by
2549 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2550 * where \c alg is the algorithm that is being calculated.
2551 * You can determine a sufficient buffer size for \p tag by
2552 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002553 * \retval #PSA_ERROR_INVALID_ARGUMENT
2554 * The total length of input to psa_aead_update_ad() so far is
2555 * less than the additional data length that was previously
2556 * specified with psa_aead_set_lengths().
2557 * \retval #PSA_ERROR_INVALID_ARGUMENT
2558 * The total length of input to psa_aead_update() so far is
2559 * less than the plaintext length that was previously
2560 * 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_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002567 uint8_t *ciphertext,
2568 size_t ciphertext_size,
2569 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002570 uint8_t *tag,
2571 size_t tag_size,
2572 size_t *tag_length);
2573
2574/** Finish authenticating and decrypting a message in an AEAD operation.
2575 *
2576 * The operation must have been set up with psa_aead_decrypt_setup().
2577 *
2578 * This function finishes the authentication of the additional data
2579 * formed by concatenating the inputs passed to preceding calls to
2580 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2581 * inputs passed to preceding calls to psa_aead_update().
2582 *
2583 * When this function returns, the operation becomes inactive.
2584 *
2585 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002586 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002587 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002588 * from previous calls to psa_aead_update()
2589 * that could not be processed until the end
2590 * of the input.
2591 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002592 * This must be at least
2593 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2594 * \c alg is the algorithm that is being
2595 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002596 * \param[out] plaintext_length On success, the number of bytes of
2597 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002598 * \param[in] tag Buffer containing the authentication tag.
2599 * \param tag_length Size of the \p tag buffer in bytes.
2600 *
2601 * \retval #PSA_SUCCESS
2602 * Success.
2603 * \retval #PSA_ERROR_BAD_STATE
2604 * The operation state is not valid (not set up, nonce not set,
2605 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002606 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2607 * The size of the \p plaintext buffer is too small.
2608 * You can determine a sufficient buffer size for \p plaintext by
2609 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2610 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002611 * \retval #PSA_ERROR_INVALID_ARGUMENT
2612 * The total length of input to psa_aead_update_ad() so far is
2613 * less than the additional data length that was previously
2614 * specified with psa_aead_set_lengths().
2615 * \retval #PSA_ERROR_INVALID_ARGUMENT
2616 * The total length of input to psa_aead_update() so far is
2617 * less than the plaintext length that was previously
2618 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002619 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2620 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2621 * \retval #PSA_ERROR_HARDWARE_FAILURE
2622 * \retval #PSA_ERROR_TAMPERING_DETECTED
2623 */
2624psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002625 uint8_t *plaintext,
2626 size_t plaintext_size,
2627 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002628 const uint8_t *tag,
2629 size_t tag_length);
2630
2631/** Abort an AEAD operation.
2632 *
2633 * Aborting an operation frees all associated resources except for the
2634 * \p operation structure itself. Once aborted, the operation object
2635 * can be reused for another operation by calling
2636 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2637 *
2638 * You may call this function any time after the operation object has
2639 * been initialized by any of the following methods:
2640 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2641 * whether it succeeds or not.
2642 * - Initializing the \c struct to all-bits-zero.
2643 * - Initializing the \c struct to logical zeros, e.g.
2644 * `psa_aead_operation_t operation = {0}`.
2645 *
2646 * In particular, calling psa_aead_abort() after the operation has been
2647 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2648 * is safe and has no effect.
2649 *
2650 * \param[in,out] operation Initialized AEAD operation.
2651 *
2652 * \retval #PSA_SUCCESS
2653 * \retval #PSA_ERROR_BAD_STATE
2654 * \p operation is not an active AEAD operation.
2655 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2656 * \retval #PSA_ERROR_HARDWARE_FAILURE
2657 * \retval #PSA_ERROR_TAMPERING_DETECTED
2658 */
2659psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2660
Gilles Peskine3b555712018-03-03 21:27:57 +01002661/**@}*/
2662
Gilles Peskine20035e32018-02-03 22:44:14 +01002663/** \defgroup asymmetric Asymmetric cryptography
2664 * @{
2665 */
2666
2667/**
2668 * \brief Sign a hash or short message with a private key.
2669 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002670 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002671 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002672 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2673 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2674 * to determine the hash algorithm to use.
2675 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002676 * \param handle Handle to the key to use for the operation.
2677 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002678 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002679 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002680 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002681 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002682 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002683 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002684 * \param[out] signature_length On success, the number of bytes
2685 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002686 *
Gilles Peskine28538492018-07-11 17:34:00 +02002687 * \retval #PSA_SUCCESS
2688 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002689 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002690 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002691 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002692 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002693 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002694 * \retval #PSA_ERROR_NOT_SUPPORTED
2695 * \retval #PSA_ERROR_INVALID_ARGUMENT
2696 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2697 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2698 * \retval #PSA_ERROR_HARDWARE_FAILURE
2699 * \retval #PSA_ERROR_TAMPERING_DETECTED
2700 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002701 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002702 * The library has not been previously initialized by psa_crypto_init().
2703 * It is implementation-dependent whether a failure to initialize
2704 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002705 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002706psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002707 psa_algorithm_t alg,
2708 const uint8_t *hash,
2709 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002710 uint8_t *signature,
2711 size_t signature_size,
2712 size_t *signature_length);
2713
2714/**
2715 * \brief Verify the signature a hash or short message using a public key.
2716 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002717 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002718 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002719 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2720 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2721 * to determine the hash algorithm to use.
2722 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002723 * \param handle Handle to the key to use for the operation.
2724 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002725 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002726 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002727 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002728 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002729 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002730 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002731 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002732 *
Gilles Peskine28538492018-07-11 17:34:00 +02002733 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002734 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002735 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002736 * The calculation was perfomed successfully, but the passed
2737 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002738 * \retval #PSA_ERROR_NOT_SUPPORTED
2739 * \retval #PSA_ERROR_INVALID_ARGUMENT
2740 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2741 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2742 * \retval #PSA_ERROR_HARDWARE_FAILURE
2743 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002744 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002745 * The library has not been previously initialized by psa_crypto_init().
2746 * It is implementation-dependent whether a failure to initialize
2747 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002748 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002749psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002750 psa_algorithm_t alg,
2751 const uint8_t *hash,
2752 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002753 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002754 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002755
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002756/**
2757 * \brief Encrypt a short message with a public key.
2758 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002759 * \param handle Handle to the key to use for the operation.
2760 * It must be a public key or an asymmetric
2761 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002762 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002763 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002764 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002765 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002766 * \param[in] salt A salt or label, if supported by the
2767 * encryption algorithm.
2768 * If the algorithm does not support a
2769 * salt, pass \c NULL.
2770 * If the algorithm supports an optional
2771 * salt and you do not want to pass a salt,
2772 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002773 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002774 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2775 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002776 * \param salt_length Size of the \p salt buffer in bytes.
2777 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002778 * \param[out] output Buffer where the encrypted message is to
2779 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002780 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002781 * \param[out] output_length On success, the number of bytes
2782 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002783 *
Gilles Peskine28538492018-07-11 17:34:00 +02002784 * \retval #PSA_SUCCESS
2785 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002786 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002787 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002788 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002789 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002790 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002791 * \retval #PSA_ERROR_NOT_SUPPORTED
2792 * \retval #PSA_ERROR_INVALID_ARGUMENT
2793 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2794 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2795 * \retval #PSA_ERROR_HARDWARE_FAILURE
2796 * \retval #PSA_ERROR_TAMPERING_DETECTED
2797 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002798 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002799 * The library has not been previously initialized by psa_crypto_init().
2800 * It is implementation-dependent whether a failure to initialize
2801 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002802 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002803psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002804 psa_algorithm_t alg,
2805 const uint8_t *input,
2806 size_t input_length,
2807 const uint8_t *salt,
2808 size_t salt_length,
2809 uint8_t *output,
2810 size_t output_size,
2811 size_t *output_length);
2812
2813/**
2814 * \brief Decrypt a short message with a private key.
2815 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002816 * \param handle Handle to the key to use for the operation.
2817 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002818 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002819 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002820 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002821 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002822 * \param[in] salt A salt or label, if supported by the
2823 * encryption algorithm.
2824 * If the algorithm does not support a
2825 * salt, pass \c NULL.
2826 * If the algorithm supports an optional
2827 * salt and you do not want to pass a salt,
2828 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002829 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002830 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2831 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002832 * \param salt_length Size of the \p salt buffer in bytes.
2833 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002834 * \param[out] output Buffer where the decrypted message is to
2835 * be written.
2836 * \param output_size Size of the \c output buffer in bytes.
2837 * \param[out] output_length On success, the number of bytes
2838 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002839 *
Gilles Peskine28538492018-07-11 17:34:00 +02002840 * \retval #PSA_SUCCESS
2841 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002842 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002843 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002844 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002845 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002846 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002847 * \retval #PSA_ERROR_NOT_SUPPORTED
2848 * \retval #PSA_ERROR_INVALID_ARGUMENT
2849 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2850 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2851 * \retval #PSA_ERROR_HARDWARE_FAILURE
2852 * \retval #PSA_ERROR_TAMPERING_DETECTED
2853 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2854 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002855 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002856 * The library has not been previously initialized by psa_crypto_init().
2857 * It is implementation-dependent whether a failure to initialize
2858 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002859 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002860psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002861 psa_algorithm_t alg,
2862 const uint8_t *input,
2863 size_t input_length,
2864 const uint8_t *salt,
2865 size_t salt_length,
2866 uint8_t *output,
2867 size_t output_size,
2868 size_t *output_length);
2869
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002870/**@}*/
2871
Gilles Peskine35675b62019-05-16 17:26:11 +02002872/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002873 * @{
2874 */
2875
Gilles Peskine35675b62019-05-16 17:26:11 +02002876/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002877 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002878 * Before calling any function on a key derivation operation object, the
2879 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002880 * - Set the structure to all-bits-zero, for example:
2881 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002882 * psa_key_derivation_operation_t operation;
2883 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002884 * \endcode
2885 * - Initialize the structure to logical zero values, for example:
2886 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002887 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002888 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002889 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002890 * for example:
2891 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002892 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002893 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002894 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002895 * to the structure, for example:
2896 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002897 * psa_key_derivation_operation_t operation;
2898 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002899 * \endcode
2900 *
2901 * This is an implementation-defined \c struct. Applications should not
2902 * make any assumptions about the content of this structure except
2903 * as directed by the documentation of a specific implementation.
2904 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02002905typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002906
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002907/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002908 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002909 * This macro returns a suitable initializer for a key derivation operation
2910 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002911 */
2912#ifdef __DOXYGEN_ONLY__
2913/* This is an example definition for documentation purposes.
2914 * Implementations should define a suitable value in `crypto_struct.h`.
2915 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002916#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02002917#endif
2918
Gilles Peskine35675b62019-05-16 17:26:11 +02002919/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002920 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002921static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002922
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002923/** Set up a key derivation operation.
2924 *
2925 * A key derivation algorithm takes some inputs and uses them to generate
2926 * a byte stream in a deterministic way.
2927 * This byte stream can be used to produce keys and other
2928 * cryptographic material.
2929 *
2930 * To derive a key:
2931 * - Start with an initialized object of type #psa_key_derivation_operation_t.
2932 * - Call psa_key_derivation_setup() to select the algorithm.
2933 * - Provide the inputs for the key derivation by calling
2934 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2935 * as appropriate. Which inputs are needed, in what order, and whether
2936 * they may be keys and if so of what type depends on the algorithm.
2937 * - Optionally set the operation's maximum capacity with
2938 * psa_key_derivation_set_capacity(). You may do this before, in the middle
2939 * of or after providing inputs. For some algorithms, this step is mandatory
2940 * because the output depends on the maximum capacity.
2941 * - To derive a key, call psa_key_derivation_output_key().
2942 * To derive a byte string for a different purpose, call
2943 * - psa_key_derivation_output_bytes().
2944 * Successive calls to these functions use successive output bytes
2945 * calculated by the key derivation algorithm.
2946 * - Clean up the key derivation operation object with
2947 * psa_key_derivation_abort().
2948 *
2949 * \param[in,out] operation The key derivation operation object
2950 * to set up. It must
2951 * have been initialized but not set up yet.
2952 * \param alg The key derivation algorithm to compute
2953 * (\c PSA_ALG_XXX value such that
2954 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2955 *
2956 * \retval #PSA_SUCCESS
2957 * Success.
2958 * \retval #PSA_ERROR_INVALID_ARGUMENT
2959 * \c alg is not a key derivation algorithm.
2960 * \retval #PSA_ERROR_NOT_SUPPORTED
2961 * \c alg is not supported or is not a key derivation algorithm.
2962 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2963 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2964 * \retval #PSA_ERROR_HARDWARE_FAILURE
2965 * \retval #PSA_ERROR_TAMPERING_DETECTED
2966 * \retval #PSA_ERROR_BAD_STATE
2967 */
2968psa_status_t psa_key_derivation_setup(
2969 psa_key_derivation_operation_t *operation,
2970 psa_algorithm_t alg);
2971
Gilles Peskine35675b62019-05-16 17:26:11 +02002972/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002973 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002974 * The capacity of a key derivation is the maximum number of bytes that it can
2975 * return. When you get *N* bytes of output from a key derivation operation,
2976 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002977 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002978 * \param[in] operation The operation to query.
2979 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002980 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002981 * \retval #PSA_SUCCESS
2982 * \retval #PSA_ERROR_BAD_STATE
2983 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002984 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002985psa_status_t psa_key_derivation_get_capacity(
2986 const psa_key_derivation_operation_t *operation,
2987 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002988
Gilles Peskine35675b62019-05-16 17:26:11 +02002989/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002990 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002991 * The capacity of a key derivation operation is the maximum number of bytes
2992 * that the key derivation operation can return from this point onwards.
2993 *
2994 * \param[in,out] operation The key derivation operation object to modify.
2995 * \param capacity The new capacity of the operation.
2996 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002997 * current capacity.
2998 *
2999 * \retval #PSA_SUCCESS
3000 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003001 * \p capacity is larger than the operation's current capacity.
3002 * In this case, the operation object remains valid and its capacity
3003 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003004 * \retval #PSA_ERROR_BAD_STATE
3005 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3006 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003007psa_status_t psa_key_derivation_set_capacity(
3008 psa_key_derivation_operation_t *operation,
3009 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003010
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003011/** Use the maximum possible capacity for a key derivation operation.
3012 *
3013 * Use this value as the capacity argument when setting up a key derivation
3014 * to indicate that the operation should have the maximum possible capacity.
3015 * The value of the maximum possible capacity depends on the key derivation
3016 * algorithm.
3017 */
3018#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3019
3020/** Provide an input for key derivation or key agreement.
3021 *
3022 * Which inputs are required and in what order depends on the algorithm.
3023 * Refer to the documentation of each key derivation or key agreement
3024 * algorithm for information.
3025 *
3026 * This function passes direct inputs. Some inputs must be passed as keys
3027 * using psa_key_derivation_input_key() instead of this function. Refer to
3028 * the documentation of individual step types for information.
3029 *
3030 * \param[in,out] operation The key derivation operation object to use.
3031 * It must have been set up with
3032 * psa_key_derivation_setup() and must not
3033 * have produced any output yet.
3034 * \param step Which step the input data is for.
3035 * \param[in] data Input data to use.
3036 * \param data_length Size of the \p data buffer in bytes.
3037 *
3038 * \retval #PSA_SUCCESS
3039 * Success.
3040 * \retval #PSA_ERROR_INVALID_ARGUMENT
3041 * \c step is not compatible with the operation's algorithm.
3042 * \retval #PSA_ERROR_INVALID_ARGUMENT
3043 * \c step does not allow direct inputs.
3044 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3045 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3046 * \retval #PSA_ERROR_HARDWARE_FAILURE
3047 * \retval #PSA_ERROR_TAMPERING_DETECTED
3048 * \retval #PSA_ERROR_BAD_STATE
3049 * The value of \p step is not valid given the state of \p operation.
3050 * \retval #PSA_ERROR_BAD_STATE
3051 * The library has not been previously initialized by psa_crypto_init().
3052 * It is implementation-dependent whether a failure to initialize
3053 * results in this error code.
3054 */
3055psa_status_t psa_key_derivation_input_bytes(
3056 psa_key_derivation_operation_t *operation,
3057 psa_key_derivation_step_t step,
3058 const uint8_t *data,
3059 size_t data_length);
3060
3061/** Provide an input for key derivation in the form of a key.
3062 *
3063 * Which inputs are required and in what order depends on the algorithm.
3064 * Refer to the documentation of each key derivation or key agreement
3065 * algorithm for information.
3066 *
3067 * This function passes key inputs. Some inputs must be passed as keys
3068 * of the appropriate type using this function, while others must be
3069 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3070 * the documentation of individual step types for information.
3071 *
3072 * \param[in,out] operation The key derivation operation object to use.
3073 * It must have been set up with
3074 * psa_key_derivation_setup() and must not
3075 * have produced any output yet.
3076 * \param step Which step the input data is for.
3077 * \param handle Handle to the key. It must have an
3078 * appropriate type for \p step and must
3079 * allow the usage #PSA_KEY_USAGE_DERIVE.
3080 *
3081 * \retval #PSA_SUCCESS
3082 * Success.
3083 * \retval #PSA_ERROR_INVALID_HANDLE
3084 * \retval #PSA_ERROR_DOES_NOT_EXIST
3085 * \retval #PSA_ERROR_NOT_PERMITTED
3086 * \retval #PSA_ERROR_INVALID_ARGUMENT
3087 * \c step is not compatible with the operation's algorithm.
3088 * \retval #PSA_ERROR_INVALID_ARGUMENT
3089 * \c step does not allow key inputs.
3090 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3091 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3092 * \retval #PSA_ERROR_HARDWARE_FAILURE
3093 * \retval #PSA_ERROR_TAMPERING_DETECTED
3094 * \retval #PSA_ERROR_BAD_STATE
3095 * The value of \p step is not valid given the state of \p operation.
3096 * \retval #PSA_ERROR_BAD_STATE
3097 * The library has not been previously initialized by psa_crypto_init().
3098 * It is implementation-dependent whether a failure to initialize
3099 * results in this error code.
3100 */
3101psa_status_t psa_key_derivation_input_key(
3102 psa_key_derivation_operation_t *operation,
3103 psa_key_derivation_step_t step,
3104 psa_key_handle_t handle);
3105
3106/** Perform a key agreement and use the shared secret as input to a key
3107 * derivation.
3108 *
3109 * A key agreement algorithm takes two inputs: a private key \p private_key
3110 * a public key \p peer_key.
3111 * The result of this function is passed as input to a key derivation.
3112 * The output of this key derivation can be extracted by reading from the
3113 * resulting operation to produce keys and other cryptographic material.
3114 *
3115 * \param[in,out] operation The key derivation operation object to use.
3116 * It must have been set up with
3117 * psa_key_derivation_setup() with a
3118 * key agreement and derivation algorithm
3119 * \c alg (\c PSA_ALG_XXX value such that
3120 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3121 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3122 * is false).
3123 * The operation must be ready for an
3124 * input of the type given by \p step.
3125 * \param step Which step the input data is for.
3126 * \param private_key Handle to the private key to use.
3127 * \param[in] peer_key Public key of the peer. The peer key must be in the
3128 * same format that psa_import_key() accepts for the
3129 * public key type corresponding to the type of
3130 * private_key. That is, this function performs the
3131 * equivalent of
3132 * #psa_import_key(...,
3133 * `peer_key`, `peer_key_length`) where
3134 * with key attributes indicating the public key
3135 * type corresponding to the type of `private_key`.
3136 * For example, for EC keys, this means that peer_key
3137 * is interpreted as a point on the curve that the
3138 * private key is on. The standard formats for public
3139 * keys are documented in the documentation of
3140 * psa_export_public_key().
3141 * \param peer_key_length Size of \p peer_key in bytes.
3142 *
3143 * \retval #PSA_SUCCESS
3144 * Success.
3145 * \retval #PSA_ERROR_INVALID_HANDLE
3146 * \retval #PSA_ERROR_DOES_NOT_EXIST
3147 * \retval #PSA_ERROR_NOT_PERMITTED
3148 * \retval #PSA_ERROR_INVALID_ARGUMENT
3149 * \c private_key is not compatible with \c alg,
3150 * or \p peer_key is not valid for \c alg or not compatible with
3151 * \c private_key.
3152 * \retval #PSA_ERROR_NOT_SUPPORTED
3153 * \c alg is not supported or is not a key derivation algorithm.
3154 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3155 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3156 * \retval #PSA_ERROR_HARDWARE_FAILURE
3157 * \retval #PSA_ERROR_TAMPERING_DETECTED
3158 */
3159psa_status_t psa_key_derivation_key_agreement(
3160 psa_key_derivation_operation_t *operation,
3161 psa_key_derivation_step_t step,
3162 psa_key_handle_t private_key,
3163 const uint8_t *peer_key,
3164 size_t peer_key_length);
3165
Gilles Peskine35675b62019-05-16 17:26:11 +02003166/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003167 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003168 * This function calculates output bytes from a key derivation algorithm and
3169 * return those bytes.
3170 * If you view the key derivation's output as a stream of bytes, this
3171 * function destructively reads the requested number of bytes from the
3172 * stream.
3173 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003174 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003175 * \param[in,out] operation The key derivation operation object to read from.
3176 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003177 * \param output_length Number of bytes to output.
3178 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003179 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003180 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003181 * The operation's capacity was less than
3182 * \p output_length bytes. Note that in this case,
3183 * no output is written to the output buffer.
3184 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003185 * subsequent calls to this function will not
3186 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003187 * \retval #PSA_ERROR_BAD_STATE
3188 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3189 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3190 * \retval #PSA_ERROR_HARDWARE_FAILURE
3191 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003192 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003193psa_status_t psa_key_derivation_output_bytes(
3194 psa_key_derivation_operation_t *operation,
3195 uint8_t *output,
3196 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003197
Gilles Peskine35675b62019-05-16 17:26:11 +02003198/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003199 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003200 * This function calculates output bytes from a key derivation algorithm
3201 * and uses those bytes to generate a key deterministically.
3202 * If you view the key derivation's output as a stream of bytes, this
3203 * function destructively reads as many bytes as required from the
3204 * stream.
3205 * The operation's capacity decreases by the number of bytes read.
3206 *
3207 * How much output is produced and consumed from the operation, and how
3208 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003209 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003210 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003211 * of a given size, this function is functionally equivalent to
3212 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003213 * and passing the resulting output to #psa_import_key.
3214 * However, this function has a security benefit:
3215 * if the implementation provides an isolation boundary then
3216 * the key material is not exposed outside the isolation boundary.
3217 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003218 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003219 * The following key types defined in this specification follow this scheme:
3220 *
3221 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003222 * - #PSA_KEY_TYPE_ARC4;
3223 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003224 * - #PSA_KEY_TYPE_DERIVE;
3225 * - #PSA_KEY_TYPE_HMAC.
3226 *
3227 * - For ECC keys on a Montgomery elliptic curve
3228 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3229 * Montgomery curve), this function always draws a byte string whose
3230 * length is determined by the curve, and sets the mandatory bits
3231 * accordingly. That is:
3232 *
3233 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3234 * and process it as specified in RFC 7748 &sect;5.
3235 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3236 * and process it as specified in RFC 7748 &sect;5.
3237 *
3238 * - For key types for which the key is represented by a single sequence of
3239 * \p bits bits with constraints as to which bit sequences are acceptable,
3240 * this function draws a byte string of length (\p bits / 8) bytes rounded
3241 * up to the nearest whole number of bytes. If the resulting byte string
3242 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3243 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003244 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003245 * for the output produced by psa_export_key().
3246 * The following key types defined in this specification follow this scheme:
3247 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003248 * - #PSA_KEY_TYPE_DES.
3249 * Force-set the parity bits, but discard forbidden weak keys.
3250 * For 2-key and 3-key triple-DES, the three keys are generated
3251 * successively (for example, for 3-key triple-DES,
3252 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3253 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003254 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003255 * two keys).
3256 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR),
3257 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
3258 * ECC keys on a Weierstrass elliptic curve
3259 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3260 * Weierstrass curve).
3261 * For these key types, interpret the byte string as integer
3262 * in big-endian order. Discard it if it is not in the range
3263 * [0, *N* - 2] where *N* is the boundary of the private key domain
3264 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003265 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003266 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003267 * This method allows compliance to NIST standards, specifically
3268 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003269 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3270 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3271 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3272 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003273 *
3274 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003275 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003276 * implementation-defined.
3277 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003278 * In all cases, the data that is read is discarded from the operation.
3279 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003280 *
Gilles Peskine20628592019-04-19 19:29:50 +02003281 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003282 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003283 * \param[out] handle On success, a handle to the newly created key.
3284 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003285 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003286 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003287 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003288 * If the key is persistent, the key material and the key's metadata
3289 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003290 * \retval #PSA_ERROR_ALREADY_EXISTS
3291 * This is an attempt to create a persistent key, and there is
3292 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003293 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003294 * There was not enough data to create the desired key.
3295 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003296 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003297 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003298 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003299 * The key type or key size is not supported, either by the
3300 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003301 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003302 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3303 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3304 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3305 * \retval #PSA_ERROR_HARDWARE_FAILURE
3306 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003307 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003308 * The library has not been previously initialized by psa_crypto_init().
3309 * It is implementation-dependent whether a failure to initialize
3310 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003311 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003312psa_status_t psa_key_derivation_output_key(
3313 const psa_key_attributes_t *attributes,
3314 psa_key_derivation_operation_t *operation,
3315 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003316
Gilles Peskine35675b62019-05-16 17:26:11 +02003317/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003318 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003319 * Once a key derivation operation has been aborted, its capacity is zero.
3320 * Aborting an operation frees all associated resources except for the
3321 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003322 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003323 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003324 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003325 * psa_key_derivation_operation_init() or a zero value. In particular,
3326 * it is valid to call psa_key_derivation_abort() twice, or to call
3327 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003328 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003329 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003330 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003331 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003332 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003333 * \retval #PSA_SUCCESS
3334 * \retval #PSA_ERROR_BAD_STATE
3335 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3336 * \retval #PSA_ERROR_HARDWARE_FAILURE
3337 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003338 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003339psa_status_t psa_key_derivation_abort(
3340 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003341
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003342/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003343 *
3344 * \warning The raw result of a key agreement algorithm such as finite-field
3345 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3346 * not be used directly as key material. It should instead be passed as
3347 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003348 * a key derivation, use psa_key_derivation_key_agreement() and other
3349 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003350 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003351 * \param alg The key agreement algorithm to compute
3352 * (\c PSA_ALG_XXX value such that
3353 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3354 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003355 * \param private_key Handle to the private key to use.
3356 * \param[in] peer_key Public key of the peer. It must be
3357 * in the same format that psa_import_key()
3358 * accepts. The standard formats for public
3359 * keys are documented in the documentation
3360 * of psa_export_public_key().
3361 * \param peer_key_length Size of \p peer_key in bytes.
3362 * \param[out] output Buffer where the decrypted message is to
3363 * be written.
3364 * \param output_size Size of the \c output buffer in bytes.
3365 * \param[out] output_length On success, the number of bytes
3366 * that make up the returned output.
3367 *
3368 * \retval #PSA_SUCCESS
3369 * Success.
3370 * \retval #PSA_ERROR_INVALID_HANDLE
3371 * \retval #PSA_ERROR_EMPTY_SLOT
3372 * \retval #PSA_ERROR_NOT_PERMITTED
3373 * \retval #PSA_ERROR_INVALID_ARGUMENT
3374 * \p alg is not a key agreement algorithm
3375 * \retval #PSA_ERROR_INVALID_ARGUMENT
3376 * \p private_key is not compatible with \p alg,
3377 * or \p peer_key is not valid for \p alg or not compatible with
3378 * \p private_key.
3379 * \retval #PSA_ERROR_NOT_SUPPORTED
3380 * \p alg is not a supported key agreement algorithm.
3381 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3382 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3383 * \retval #PSA_ERROR_HARDWARE_FAILURE
3384 * \retval #PSA_ERROR_TAMPERING_DETECTED
3385 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003386psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3387 psa_key_handle_t private_key,
3388 const uint8_t *peer_key,
3389 size_t peer_key_length,
3390 uint8_t *output,
3391 size_t output_size,
3392 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003393
Gilles Peskineea0fb492018-07-12 17:17:20 +02003394/**@}*/
3395
Gilles Peskineedd76872018-07-20 17:42:05 +02003396/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003397 * @{
3398 */
3399
3400/**
3401 * \brief Generate random bytes.
3402 *
3403 * \warning This function **can** fail! Callers MUST check the return status
3404 * and MUST NOT use the content of the output buffer if the return
3405 * status is not #PSA_SUCCESS.
3406 *
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003407 * \note To generate a key, use psa_generate_random_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003408 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003409 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003410 * \param output_size Number of bytes to generate and output.
3411 *
Gilles Peskine28538492018-07-11 17:34:00 +02003412 * \retval #PSA_SUCCESS
3413 * \retval #PSA_ERROR_NOT_SUPPORTED
3414 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3415 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3416 * \retval #PSA_ERROR_HARDWARE_FAILURE
3417 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003418 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003419 * The library has not been previously initialized by psa_crypto_init().
3420 * It is implementation-dependent whether a failure to initialize
3421 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003422 */
3423psa_status_t psa_generate_random(uint8_t *output,
3424 size_t output_size);
3425
3426/**
3427 * \brief Generate a key or key pair.
3428 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003429 * The key is generated randomly.
3430 * Its location, policy, type and size are taken from \p attributes.
3431 *
Gilles Peskine20628592019-04-19 19:29:50 +02003432 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003433 * \param[out] handle On success, a handle to the newly created key.
3434 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003435 *
Gilles Peskine28538492018-07-11 17:34:00 +02003436 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003437 * Success.
3438 * If the key is persistent, the key material and the key's metadata
3439 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003440 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003441 * This is an attempt to create a persistent key, and there is
3442 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003443 * \retval #PSA_ERROR_NOT_SUPPORTED
3444 * \retval #PSA_ERROR_INVALID_ARGUMENT
3445 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3446 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3447 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3448 * \retval #PSA_ERROR_HARDWARE_FAILURE
3449 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003450 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003451 * The library has not been previously initialized by psa_crypto_init().
3452 * It is implementation-dependent whether a failure to initialize
3453 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003454 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003455psa_status_t psa_generate_random_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003456 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003457
3458/**@}*/
3459
Gilles Peskinee59236f2018-01-27 23:32:46 +01003460#ifdef __cplusplus
3461}
3462#endif
3463
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003464/* The file "crypto_sizes.h" contains definitions for size calculation
3465 * macros whose definitions are implementation-specific. */
3466#include "crypto_sizes.h"
3467
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003468/* The file "crypto_struct.h" contains definitions for
3469 * implementation-specific structs that are declared above. */
3470#include "crypto_struct.h"
3471
3472/* The file "crypto_extra.h" contains vendor-specific definitions. This
3473 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003474#include "crypto_extra.h"
3475
3476#endif /* PSA_CRYPTO_H */