blob: 4a3388994c9f2b0e012a15795662e6db7dc01c7c [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 Peskinec93b80c2019-05-16 19:39:54 +0200628 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), 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 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200644 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200645 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100646 * a representation of the private value as a `ceiling(m/8)`-byte string
647 * where `m` is the bit size associated with the curve, i.e. the bit size
648 * of the order of the curve's coordinate field. This byte string is
649 * in little-endian order for Montgomery curves (curve types
650 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
651 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
652 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100653 * This is the content of the `privateKey` field of the `ECPrivateKey`
654 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200655 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200656 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000657 * format is the representation of the private key `x` as a big-endian byte
658 * string. The length of the byte string is the private key size in bytes
659 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200660 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
661 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100662 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200663 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
664 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100665 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200666 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200667 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200668 * \param[out] data_length On success, the number of bytes
669 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100670 *
Gilles Peskine28538492018-07-11 17:34:00 +0200671 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100672 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200673 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200674 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200675 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100676 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200677 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
678 * The size of the \p data buffer is too small. You can determine a
679 * sufficient buffer size by calling
680 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
681 * where \c type is the key type
682 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200683 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
684 * \retval #PSA_ERROR_HARDWARE_FAILURE
685 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300686 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300687 * The library has not been previously initialized by psa_crypto_init().
688 * It is implementation-dependent whether a failure to initialize
689 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100690 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100691psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100692 uint8_t *data,
693 size_t data_size,
694 size_t *data_length);
695
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100696/**
697 * \brief Export a public key or the public part of a key pair in binary format.
698 *
699 * The output of this function can be passed to psa_import_key() to
700 * create an object that is equivalent to the public key.
701 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000702 * This specification supports a single format for each key type.
703 * Implementations may support other formats as long as the standard
704 * format is supported. Implementations that support other formats
705 * should ensure that the formats are clearly unambiguous so as to
706 * minimize the risk that an invalid input is accidentally interpreted
707 * according to a different format.
708 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000709 * For standard key types, the output format is as follows:
710 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
711 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
712 * ```
713 * RSAPublicKey ::= SEQUENCE {
714 * modulus INTEGER, -- n
715 * publicExponent INTEGER } -- e
716 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000717 * - For elliptic curve public keys (key types for which
718 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
719 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
720 * Let `m` be the bit size associated with the curve, i.e. the bit size of
721 * `q` for a curve over `F_q`. The representation consists of:
722 * - The byte 0x04;
723 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
724 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200725 * - For Diffie-Hellman key exchange public keys (key types for which
726 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000727 * the format is the representation of the public key `y = g^x mod p` as a
728 * big-endian byte string. The length of the byte string is the length of the
729 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100730 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200731 * Exporting a public key object or the public part of a key pair is
732 * always permitted, regardless of the key's usage flags.
733 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100734 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200735 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200736 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200737 * \param[out] data_length On success, the number of bytes
738 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100739 *
Gilles Peskine28538492018-07-11 17:34:00 +0200740 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100741 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200742 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200743 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200744 * The key is neither a public key nor a key pair.
745 * \retval #PSA_ERROR_NOT_SUPPORTED
746 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
747 * The size of the \p data buffer is too small. You can determine a
748 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200749 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200750 * where \c type is the key type
751 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200752 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
753 * \retval #PSA_ERROR_HARDWARE_FAILURE
754 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300755 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300756 * The library has not been previously initialized by psa_crypto_init().
757 * It is implementation-dependent whether a failure to initialize
758 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100759 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100760psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100761 uint8_t *data,
762 size_t data_size,
763 size_t *data_length);
764
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100765/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100766 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100767 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000768 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100769 * This function is primarily useful to copy a key from one location
770 * to another, since it populates a key using the material from
771 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200772 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100773 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100774 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100775 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100776 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200777 * The policy on the source key must have the usage flag
778 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200779 * This flag is sufficient to permit the copy if the key has the lifetime
780 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
781 * Some secure elements do not provide a way to copy a key without
782 * making it extractable from the secure element. If a key is located
783 * in such a secure element, then the key must have both usage flags
784 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
785 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200786 *
Gilles Peskine20628592019-04-19 19:29:50 +0200787 * The resulting key may only be used in a way that conforms to
788 * both the policy of the original key and the policy specified in
789 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100790 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200791 * usage flags on the source policy and the usage flags in \p attributes.
792 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100793 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200794 * - If either of the policies allows an algorithm and the other policy
795 * allows a wildcard-based algorithm policy that includes this algorithm,
796 * the resulting key allows the same algorithm.
797 * - If the policies do not allow any algorithm in common, this function
798 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200799 *
Gilles Peskine20628592019-04-19 19:29:50 +0200800 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100801 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200802 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100803 * \param source_handle The key to copy. It must be a handle to an
804 * occupied slot.
Gilles Peskine20628592019-04-19 19:29:50 +0200805 * \param[in] attributes The attributes for the new key.
806 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200807 * - The key type and size may be 0. If either is
808 * nonzero, it must match the corresponding
809 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200810 * - The key location (the lifetime and, for
811 * persistent keys, the key identifier) is
812 * used directly.
813 * - The policy constraints (usage flags and
814 * algorithm policy) are combined from
815 * the source key and \p attributes so that
816 * both sets of restrictions apply, as
817 * described in the documentation of this function.
818 * \param[out] target_handle On success, a handle to the newly created key.
819 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200820 *
821 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100822 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200823 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200824 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200825 * This is an attempt to create a persistent key, and there is
826 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200827 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200828 * The lifetime or identifier in \p attributes are invalid.
829 * \retval #PSA_ERROR_INVALID_ARGUMENT
830 * The policy constraints on the source and specified in
831 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200832 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200833 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200834 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100835 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200836 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
837 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100838 * The source key is not exportable and its lifetime does not
839 * allow copying it to the target's lifetime.
840 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
841 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200842 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
843 * \retval #PSA_ERROR_HARDWARE_FAILURE
844 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100845 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100846psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200847 const psa_key_attributes_t *attributes,
848 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100849
850/**@}*/
851
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100852/** \defgroup hash Message digests
853 * @{
854 */
855
Gilles Peskine69647a42019-01-14 20:18:12 +0100856/** Calculate the hash (digest) of a message.
857 *
858 * \note To verify the hash of a message against an
859 * expected value, use psa_hash_compare() instead.
860 *
861 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
862 * such that #PSA_ALG_IS_HASH(\p alg) is true).
863 * \param[in] input Buffer containing the message to hash.
864 * \param input_length Size of the \p input buffer in bytes.
865 * \param[out] hash Buffer where the hash is to be written.
866 * \param hash_size Size of the \p hash buffer in bytes.
867 * \param[out] hash_length On success, the number of bytes
868 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100869 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100870 *
871 * \retval #PSA_SUCCESS
872 * Success.
873 * \retval #PSA_ERROR_NOT_SUPPORTED
874 * \p alg is not supported or is not a hash algorithm.
875 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
876 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
877 * \retval #PSA_ERROR_HARDWARE_FAILURE
878 * \retval #PSA_ERROR_TAMPERING_DETECTED
879 */
880psa_status_t psa_hash_compute(psa_algorithm_t alg,
881 const uint8_t *input,
882 size_t input_length,
883 uint8_t *hash,
884 size_t hash_size,
885 size_t *hash_length);
886
887/** Calculate the hash (digest) of a message and compare it with a
888 * reference value.
889 *
890 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
891 * such that #PSA_ALG_IS_HASH(\p alg) is true).
892 * \param[in] input Buffer containing the message to hash.
893 * \param input_length Size of the \p input buffer in bytes.
894 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100895 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100896 *
897 * \retval #PSA_SUCCESS
898 * The expected hash is identical to the actual hash of the input.
899 * \retval #PSA_ERROR_INVALID_SIGNATURE
900 * The hash of the message was calculated successfully, but it
901 * differs from the expected hash.
902 * \retval #PSA_ERROR_NOT_SUPPORTED
903 * \p alg is not supported or is not a hash algorithm.
904 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
905 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
906 * \retval #PSA_ERROR_HARDWARE_FAILURE
907 * \retval #PSA_ERROR_TAMPERING_DETECTED
908 */
909psa_status_t psa_hash_compare(psa_algorithm_t alg,
910 const uint8_t *input,
911 size_t input_length,
912 const uint8_t *hash,
913 const size_t hash_length);
914
Gilles Peskine308b91d2018-02-08 09:47:44 +0100915/** The type of the state data structure for multipart hash operations.
916 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000917 * Before calling any function on a hash operation object, the application must
918 * initialize it by any of the following means:
919 * - Set the structure to all-bits-zero, for example:
920 * \code
921 * psa_hash_operation_t operation;
922 * memset(&operation, 0, sizeof(operation));
923 * \endcode
924 * - Initialize the structure to logical zero values, for example:
925 * \code
926 * psa_hash_operation_t operation = {0};
927 * \endcode
928 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
929 * for example:
930 * \code
931 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
932 * \endcode
933 * - Assign the result of the function psa_hash_operation_init()
934 * to the structure, for example:
935 * \code
936 * psa_hash_operation_t operation;
937 * operation = psa_hash_operation_init();
938 * \endcode
939 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100940 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100941 * make any assumptions about the content of this structure except
942 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100943typedef struct psa_hash_operation_s psa_hash_operation_t;
944
Jaeden Amero6a25b412019-01-04 11:47:44 +0000945/** \def PSA_HASH_OPERATION_INIT
946 *
947 * This macro returns a suitable initializer for a hash operation object
948 * of type #psa_hash_operation_t.
949 */
950#ifdef __DOXYGEN_ONLY__
951/* This is an example definition for documentation purposes.
952 * Implementations should define a suitable value in `crypto_struct.h`.
953 */
954#define PSA_HASH_OPERATION_INIT {0}
955#endif
956
957/** Return an initial value for a hash operation object.
958 */
959static psa_hash_operation_t psa_hash_operation_init(void);
960
Gilles Peskinef45adda2019-01-14 18:29:18 +0100961/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100962 *
963 * The sequence of operations to calculate a hash (message digest)
964 * is as follows:
965 * -# Allocate an operation object which will be passed to all the functions
966 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000967 * -# Initialize the operation object with one of the methods described in the
968 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200969 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100970 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100971 * of the message each time. The hash that is calculated is the hash
972 * of the concatenation of these messages in order.
973 * -# To calculate the hash, call psa_hash_finish().
974 * To compare the hash with an expected value, call psa_hash_verify().
975 *
976 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000977 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100978 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200979 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100980 * eventually terminate the operation. The following events terminate an
981 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100982 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100983 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100984 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000985 * \param[in,out] operation The operation object to set up. It must have
986 * been initialized as per the documentation for
987 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200988 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
989 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100990 *
Gilles Peskine28538492018-07-11 17:34:00 +0200991 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100992 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200993 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200994 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100995 * \retval #PSA_ERROR_BAD_STATE
996 * The operation state is not valid (already set up and not
997 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200998 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
999 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1000 * \retval #PSA_ERROR_HARDWARE_FAILURE
1001 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001002 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001003psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001004 psa_algorithm_t alg);
1005
Gilles Peskine308b91d2018-02-08 09:47:44 +01001006/** Add a message fragment to a multipart hash operation.
1007 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001008 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001009 *
1010 * If this function returns an error status, the operation becomes inactive.
1011 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001012 * \param[in,out] operation Active hash operation.
1013 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001014 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001015 *
Gilles Peskine28538492018-07-11 17:34:00 +02001016 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001017 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001018 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001019 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001020 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1021 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1022 * \retval #PSA_ERROR_HARDWARE_FAILURE
1023 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001024 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001025psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1026 const uint8_t *input,
1027 size_t input_length);
1028
Gilles Peskine308b91d2018-02-08 09:47:44 +01001029/** Finish the calculation of the hash of a message.
1030 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001031 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001032 * This function calculates the hash of the message formed by concatenating
1033 * the inputs passed to preceding calls to psa_hash_update().
1034 *
1035 * When this function returns, the operation becomes inactive.
1036 *
1037 * \warning Applications should not call this function if they expect
1038 * a specific value for the hash. Call psa_hash_verify() instead.
1039 * Beware that comparing integrity or authenticity data such as
1040 * hash values with a function such as \c memcmp is risky
1041 * because the time taken by the comparison may leak information
1042 * about the hashed data which could allow an attacker to guess
1043 * a valid hash and thereby bypass security controls.
1044 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001045 * \param[in,out] operation Active hash operation.
1046 * \param[out] hash Buffer where the hash is to be written.
1047 * \param hash_size Size of the \p hash buffer in bytes.
1048 * \param[out] hash_length On success, the number of bytes
1049 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001050 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001051 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001052 *
Gilles Peskine28538492018-07-11 17:34:00 +02001053 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001054 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001055 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001056 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001057 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001058 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001059 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001060 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001061 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1062 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1063 * \retval #PSA_ERROR_HARDWARE_FAILURE
1064 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001065 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001066psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1067 uint8_t *hash,
1068 size_t hash_size,
1069 size_t *hash_length);
1070
Gilles Peskine308b91d2018-02-08 09:47:44 +01001071/** Finish the calculation of the hash of a message and compare it with
1072 * an expected value.
1073 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001074 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001075 * This function calculates the hash of the message formed by concatenating
1076 * the inputs passed to preceding calls to psa_hash_update(). It then
1077 * compares the calculated hash with the expected hash passed as a
1078 * parameter to this function.
1079 *
1080 * When this function returns, the operation becomes inactive.
1081 *
Gilles Peskine19067982018-03-20 17:54:53 +01001082 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001083 * comparison between the actual hash and the expected hash is performed
1084 * in constant time.
1085 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001086 * \param[in,out] operation Active hash operation.
1087 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001088 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001089 *
Gilles Peskine28538492018-07-11 17:34:00 +02001090 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001091 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001092 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001093 * The hash of the message was calculated successfully, but it
1094 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001095 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001096 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001097 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1098 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1099 * \retval #PSA_ERROR_HARDWARE_FAILURE
1100 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001101 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001102psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1103 const uint8_t *hash,
1104 size_t hash_length);
1105
Gilles Peskine308b91d2018-02-08 09:47:44 +01001106/** Abort a hash operation.
1107 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001108 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001109 * \p operation structure itself. Once aborted, the operation object
1110 * can be reused for another operation by calling
1111 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001112 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001113 * You may call this function any time after the operation object has
1114 * been initialized by any of the following methods:
1115 * - A call to psa_hash_setup(), whether it succeeds or not.
1116 * - Initializing the \c struct to all-bits-zero.
1117 * - Initializing the \c struct to logical zeros, e.g.
1118 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001119 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001120 * In particular, calling psa_hash_abort() after the operation has been
1121 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1122 * psa_hash_verify() is safe and has no effect.
1123 *
1124 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001125 *
Gilles Peskine28538492018-07-11 17:34:00 +02001126 * \retval #PSA_SUCCESS
1127 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001128 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001129 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1130 * \retval #PSA_ERROR_HARDWARE_FAILURE
1131 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001132 */
1133psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001134
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001135/** Clone a hash operation.
1136 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001137 * This function copies the state of an ongoing hash operation to
1138 * a new operation object. In other words, this function is equivalent
1139 * to calling psa_hash_setup() on \p target_operation with the same
1140 * algorithm that \p source_operation was set up for, then
1141 * psa_hash_update() on \p target_operation with the same input that
1142 * that was passed to \p source_operation. After this function returns, the
1143 * two objects are independent, i.e. subsequent calls involving one of
1144 * the objects do not affect the other object.
1145 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001146 * \param[in] source_operation The active hash operation to clone.
1147 * \param[in,out] target_operation The operation object to set up.
1148 * It must be initialized but not active.
1149 *
1150 * \retval #PSA_SUCCESS
1151 * \retval #PSA_ERROR_BAD_STATE
1152 * \p source_operation is not an active hash operation.
1153 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001154 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001155 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1156 * \retval #PSA_ERROR_HARDWARE_FAILURE
1157 * \retval #PSA_ERROR_TAMPERING_DETECTED
1158 */
1159psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1160 psa_hash_operation_t *target_operation);
1161
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001162/**@}*/
1163
Gilles Peskine8c9def32018-02-08 10:02:12 +01001164/** \defgroup MAC Message authentication codes
1165 * @{
1166 */
1167
Gilles Peskine69647a42019-01-14 20:18:12 +01001168/** Calculate the MAC (message authentication code) of a message.
1169 *
1170 * \note To verify the MAC of a message against an
1171 * expected value, use psa_mac_verify() instead.
1172 * Beware that comparing integrity or authenticity data such as
1173 * MAC values with a function such as \c memcmp is risky
1174 * because the time taken by the comparison may leak information
1175 * about the MAC value which could allow an attacker to guess
1176 * a valid MAC and thereby bypass security controls.
1177 *
1178 * \param handle Handle to the key to use for the operation.
1179 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001180 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001181 * \param[in] input Buffer containing the input message.
1182 * \param input_length Size of the \p input buffer in bytes.
1183 * \param[out] mac Buffer where the MAC value is to be written.
1184 * \param mac_size Size of the \p mac buffer in bytes.
1185 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001186 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001187 *
1188 * \retval #PSA_SUCCESS
1189 * Success.
1190 * \retval #PSA_ERROR_INVALID_HANDLE
1191 * \retval #PSA_ERROR_EMPTY_SLOT
1192 * \retval #PSA_ERROR_NOT_PERMITTED
1193 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001194 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001195 * \retval #PSA_ERROR_NOT_SUPPORTED
1196 * \p alg is not supported or is not a MAC algorithm.
1197 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1198 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1199 * \retval #PSA_ERROR_HARDWARE_FAILURE
1200 * \retval #PSA_ERROR_TAMPERING_DETECTED
1201 * \retval #PSA_ERROR_BAD_STATE
1202 * The library has not been previously initialized by psa_crypto_init().
1203 * It is implementation-dependent whether a failure to initialize
1204 * results in this error code.
1205 */
1206psa_status_t psa_mac_compute(psa_key_handle_t handle,
1207 psa_algorithm_t alg,
1208 const uint8_t *input,
1209 size_t input_length,
1210 uint8_t *mac,
1211 size_t mac_size,
1212 size_t *mac_length);
1213
1214/** Calculate the MAC of a message and compare it with a reference value.
1215 *
1216 * \param handle Handle to the key to use for the operation.
1217 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001218 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001219 * \param[in] input Buffer containing the input message.
1220 * \param input_length Size of the \p input buffer in bytes.
1221 * \param[out] mac Buffer containing the expected MAC value.
1222 * \param mac_length Size of the \p mac buffer in bytes.
1223 *
1224 * \retval #PSA_SUCCESS
1225 * The expected MAC is identical to the actual MAC of the input.
1226 * \retval #PSA_ERROR_INVALID_SIGNATURE
1227 * The MAC of the message was calculated successfully, but it
1228 * differs from the expected value.
1229 * \retval #PSA_ERROR_INVALID_HANDLE
1230 * \retval #PSA_ERROR_EMPTY_SLOT
1231 * \retval #PSA_ERROR_NOT_PERMITTED
1232 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001233 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001234 * \retval #PSA_ERROR_NOT_SUPPORTED
1235 * \p alg is not supported or is not a MAC algorithm.
1236 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1237 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1238 * \retval #PSA_ERROR_HARDWARE_FAILURE
1239 * \retval #PSA_ERROR_TAMPERING_DETECTED
1240 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001241psa_status_t psa_mac_verify(psa_key_handle_t handle,
1242 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001243 const uint8_t *input,
1244 size_t input_length,
1245 const uint8_t *mac,
1246 const size_t mac_length);
1247
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001248/** The type of the state data structure for multipart MAC operations.
1249 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001250 * Before calling any function on a MAC operation object, the application must
1251 * initialize it by any of the following means:
1252 * - Set the structure to all-bits-zero, for example:
1253 * \code
1254 * psa_mac_operation_t operation;
1255 * memset(&operation, 0, sizeof(operation));
1256 * \endcode
1257 * - Initialize the structure to logical zero values, for example:
1258 * \code
1259 * psa_mac_operation_t operation = {0};
1260 * \endcode
1261 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1262 * for example:
1263 * \code
1264 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1265 * \endcode
1266 * - Assign the result of the function psa_mac_operation_init()
1267 * to the structure, for example:
1268 * \code
1269 * psa_mac_operation_t operation;
1270 * operation = psa_mac_operation_init();
1271 * \endcode
1272 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001273 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001274 * make any assumptions about the content of this structure except
1275 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001276typedef struct psa_mac_operation_s psa_mac_operation_t;
1277
Jaeden Amero769ce272019-01-04 11:48:03 +00001278/** \def PSA_MAC_OPERATION_INIT
1279 *
1280 * This macro returns a suitable initializer for a MAC operation object of type
1281 * #psa_mac_operation_t.
1282 */
1283#ifdef __DOXYGEN_ONLY__
1284/* This is an example definition for documentation purposes.
1285 * Implementations should define a suitable value in `crypto_struct.h`.
1286 */
1287#define PSA_MAC_OPERATION_INIT {0}
1288#endif
1289
1290/** Return an initial value for a MAC operation object.
1291 */
1292static psa_mac_operation_t psa_mac_operation_init(void);
1293
Gilles Peskinef45adda2019-01-14 18:29:18 +01001294/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001295 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001296 * This function sets up the calculation of the MAC
1297 * (message authentication code) of a byte string.
1298 * To verify the MAC of a message against an
1299 * expected value, use psa_mac_verify_setup() instead.
1300 *
1301 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001302 * -# Allocate an operation object which will be passed to all the functions
1303 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001304 * -# Initialize the operation object with one of the methods described in the
1305 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001306 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001307 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1308 * of the message each time. The MAC that is calculated is the MAC
1309 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001310 * -# At the end of the message, call psa_mac_sign_finish() to finish
1311 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001312 *
1313 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001314 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001315 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001316 * After a successful call to psa_mac_sign_setup(), the application must
1317 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001318 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001319 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001320 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001321 * \param[in,out] operation The operation object to set up. It must have
1322 * been initialized as per the documentation for
1323 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001324 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001325 * It must remain valid until the operation
1326 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001327 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001328 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001329 *
Gilles Peskine28538492018-07-11 17:34:00 +02001330 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001331 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001332 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001333 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001334 * \retval #PSA_ERROR_NOT_PERMITTED
1335 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001336 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001337 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001338 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001339 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1340 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1341 * \retval #PSA_ERROR_HARDWARE_FAILURE
1342 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001343 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001344 * The operation state is not valid (already set up and not
1345 * subsequently completed).
1346 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001347 * The library has not been previously initialized by psa_crypto_init().
1348 * It is implementation-dependent whether a failure to initialize
1349 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001350 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001351psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001352 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001353 psa_algorithm_t alg);
1354
Gilles Peskinef45adda2019-01-14 18:29:18 +01001355/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001356 *
1357 * This function sets up the verification of the MAC
1358 * (message authentication code) of a byte string against an expected value.
1359 *
1360 * The sequence of operations to verify a MAC is as follows:
1361 * -# Allocate an operation object which will be passed to all the functions
1362 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001363 * -# Initialize the operation object with one of the methods described in the
1364 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001365 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001366 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1367 * of the message each time. The MAC that is calculated is the MAC
1368 * of the concatenation of these messages in order.
1369 * -# At the end of the message, call psa_mac_verify_finish() to finish
1370 * calculating the actual MAC of the message and verify it against
1371 * the expected value.
1372 *
1373 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001374 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001375 *
1376 * After a successful call to psa_mac_verify_setup(), the application must
1377 * eventually terminate the operation through one of the following methods:
1378 * - A failed call to psa_mac_update().
1379 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1380 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001381 * \param[in,out] operation The operation object to set up. It must have
1382 * been initialized as per the documentation for
1383 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001384 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001385 * It must remain valid until the operation
1386 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001387 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1388 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001389 *
Gilles Peskine28538492018-07-11 17:34:00 +02001390 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001391 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001392 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001393 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001394 * \retval #PSA_ERROR_NOT_PERMITTED
1395 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001396 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001397 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001398 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001399 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1400 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1401 * \retval #PSA_ERROR_HARDWARE_FAILURE
1402 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001403 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001404 * The operation state is not valid (already set up and not
1405 * subsequently completed).
1406 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001407 * The library has not been previously initialized by psa_crypto_init().
1408 * It is implementation-dependent whether a failure to initialize
1409 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001410 */
1411psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001412 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001413 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001414
Gilles Peskinedcd14942018-07-12 00:30:52 +02001415/** Add a message fragment to a multipart MAC operation.
1416 *
1417 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1418 * before calling this function.
1419 *
1420 * If this function returns an error status, the operation becomes inactive.
1421 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001422 * \param[in,out] operation Active MAC operation.
1423 * \param[in] input Buffer containing the message fragment to add to
1424 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001425 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001426 *
1427 * \retval #PSA_SUCCESS
1428 * Success.
1429 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001430 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001431 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1432 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1433 * \retval #PSA_ERROR_HARDWARE_FAILURE
1434 * \retval #PSA_ERROR_TAMPERING_DETECTED
1435 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001436psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1437 const uint8_t *input,
1438 size_t input_length);
1439
Gilles Peskinedcd14942018-07-12 00:30:52 +02001440/** Finish the calculation of the MAC of a message.
1441 *
1442 * The application must call psa_mac_sign_setup() before calling this function.
1443 * This function calculates the MAC of the message formed by concatenating
1444 * the inputs passed to preceding calls to psa_mac_update().
1445 *
1446 * When this function returns, the operation becomes inactive.
1447 *
1448 * \warning Applications should not call this function if they expect
1449 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1450 * Beware that comparing integrity or authenticity data such as
1451 * MAC values with a function such as \c memcmp is risky
1452 * because the time taken by the comparison may leak information
1453 * about the MAC value which could allow an attacker to guess
1454 * a valid MAC and thereby bypass security controls.
1455 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001456 * \param[in,out] operation Active MAC operation.
1457 * \param[out] mac Buffer where the MAC value is to be written.
1458 * \param mac_size Size of the \p mac buffer in bytes.
1459 * \param[out] mac_length On success, the number of bytes
1460 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001461 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001462 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001463 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001464 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001465 *
1466 * \retval #PSA_SUCCESS
1467 * Success.
1468 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001469 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001470 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001471 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001472 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1473 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1474 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1475 * \retval #PSA_ERROR_HARDWARE_FAILURE
1476 * \retval #PSA_ERROR_TAMPERING_DETECTED
1477 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001478psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1479 uint8_t *mac,
1480 size_t mac_size,
1481 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001482
Gilles Peskinedcd14942018-07-12 00:30:52 +02001483/** Finish the calculation of the MAC of a message and compare it with
1484 * an expected value.
1485 *
1486 * The application must call psa_mac_verify_setup() before calling this function.
1487 * This function calculates the MAC of the message formed by concatenating
1488 * the inputs passed to preceding calls to psa_mac_update(). It then
1489 * compares the calculated MAC with the expected MAC passed as a
1490 * parameter to this function.
1491 *
1492 * When this function returns, the operation becomes inactive.
1493 *
1494 * \note Implementations shall make the best effort to ensure that the
1495 * comparison between the actual MAC and the expected MAC is performed
1496 * in constant time.
1497 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001498 * \param[in,out] operation Active MAC operation.
1499 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001500 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001501 *
1502 * \retval #PSA_SUCCESS
1503 * The expected MAC is identical to the actual MAC of the message.
1504 * \retval #PSA_ERROR_INVALID_SIGNATURE
1505 * The MAC of the message was calculated successfully, but it
1506 * differs from the expected MAC.
1507 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001508 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001509 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1510 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1511 * \retval #PSA_ERROR_HARDWARE_FAILURE
1512 * \retval #PSA_ERROR_TAMPERING_DETECTED
1513 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001514psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1515 const uint8_t *mac,
1516 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001517
Gilles Peskinedcd14942018-07-12 00:30:52 +02001518/** Abort a MAC operation.
1519 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001520 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001521 * \p operation structure itself. Once aborted, the operation object
1522 * can be reused for another operation by calling
1523 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001524 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001525 * You may call this function any time after the operation object has
1526 * been initialized by any of the following methods:
1527 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1528 * it succeeds or not.
1529 * - Initializing the \c struct to all-bits-zero.
1530 * - Initializing the \c struct to logical zeros, e.g.
1531 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001532 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001533 * In particular, calling psa_mac_abort() after the operation has been
1534 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1535 * psa_mac_verify_finish() is safe and has no effect.
1536 *
1537 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001538 *
1539 * \retval #PSA_SUCCESS
1540 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001541 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001542 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1543 * \retval #PSA_ERROR_HARDWARE_FAILURE
1544 * \retval #PSA_ERROR_TAMPERING_DETECTED
1545 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001546psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1547
1548/**@}*/
1549
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001550/** \defgroup cipher Symmetric ciphers
1551 * @{
1552 */
1553
Gilles Peskine69647a42019-01-14 20:18:12 +01001554/** Encrypt a message using a symmetric cipher.
1555 *
1556 * This function encrypts a message with a random IV (initialization
1557 * vector).
1558 *
1559 * \param handle Handle to the key to use for the operation.
1560 * It must remain valid until the operation
1561 * terminates.
1562 * \param alg The cipher algorithm to compute
1563 * (\c PSA_ALG_XXX value such that
1564 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1565 * \param[in] input Buffer containing the message to encrypt.
1566 * \param input_length Size of the \p input buffer in bytes.
1567 * \param[out] output Buffer where the output is to be written.
1568 * The output contains the IV followed by
1569 * the ciphertext proper.
1570 * \param output_size Size of the \p output buffer in bytes.
1571 * \param[out] output_length On success, the number of bytes
1572 * that make up the output.
1573 *
1574 * \retval #PSA_SUCCESS
1575 * Success.
1576 * \retval #PSA_ERROR_INVALID_HANDLE
1577 * \retval #PSA_ERROR_EMPTY_SLOT
1578 * \retval #PSA_ERROR_NOT_PERMITTED
1579 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001580 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001581 * \retval #PSA_ERROR_NOT_SUPPORTED
1582 * \p alg is not supported or is not a cipher algorithm.
1583 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1584 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1585 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1586 * \retval #PSA_ERROR_HARDWARE_FAILURE
1587 * \retval #PSA_ERROR_TAMPERING_DETECTED
1588 */
1589psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1590 psa_algorithm_t alg,
1591 const uint8_t *input,
1592 size_t input_length,
1593 uint8_t *output,
1594 size_t output_size,
1595 size_t *output_length);
1596
1597/** Decrypt a message using a symmetric cipher.
1598 *
1599 * This function decrypts a message encrypted with a symmetric cipher.
1600 *
1601 * \param handle Handle to the key to use for the operation.
1602 * It must remain valid until the operation
1603 * terminates.
1604 * \param alg The cipher algorithm to compute
1605 * (\c PSA_ALG_XXX value such that
1606 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1607 * \param[in] input Buffer containing the message to decrypt.
1608 * This consists of the IV followed by the
1609 * ciphertext proper.
1610 * \param input_length Size of the \p input buffer in bytes.
1611 * \param[out] output Buffer where the plaintext is to be written.
1612 * \param output_size Size of the \p output buffer in bytes.
1613 * \param[out] output_length On success, the number of bytes
1614 * that make up the output.
1615 *
1616 * \retval #PSA_SUCCESS
1617 * Success.
1618 * \retval #PSA_ERROR_INVALID_HANDLE
1619 * \retval #PSA_ERROR_EMPTY_SLOT
1620 * \retval #PSA_ERROR_NOT_PERMITTED
1621 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001622 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001623 * \retval #PSA_ERROR_NOT_SUPPORTED
1624 * \p alg is not supported or is not a cipher algorithm.
1625 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1626 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1627 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1628 * \retval #PSA_ERROR_HARDWARE_FAILURE
1629 * \retval #PSA_ERROR_TAMPERING_DETECTED
1630 */
1631psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1632 psa_algorithm_t alg,
1633 const uint8_t *input,
1634 size_t input_length,
1635 uint8_t *output,
1636 size_t output_size,
1637 size_t *output_length);
1638
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001639/** The type of the state data structure for multipart cipher operations.
1640 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001641 * Before calling any function on a cipher operation object, the application
1642 * must initialize it by any of the following means:
1643 * - Set the structure to all-bits-zero, for example:
1644 * \code
1645 * psa_cipher_operation_t operation;
1646 * memset(&operation, 0, sizeof(operation));
1647 * \endcode
1648 * - Initialize the structure to logical zero values, for example:
1649 * \code
1650 * psa_cipher_operation_t operation = {0};
1651 * \endcode
1652 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1653 * for example:
1654 * \code
1655 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1656 * \endcode
1657 * - Assign the result of the function psa_cipher_operation_init()
1658 * to the structure, for example:
1659 * \code
1660 * psa_cipher_operation_t operation;
1661 * operation = psa_cipher_operation_init();
1662 * \endcode
1663 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001664 * This is an implementation-defined \c struct. Applications should not
1665 * make any assumptions about the content of this structure except
1666 * as directed by the documentation of a specific implementation. */
1667typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1668
Jaeden Amero5bae2272019-01-04 11:48:27 +00001669/** \def PSA_CIPHER_OPERATION_INIT
1670 *
1671 * This macro returns a suitable initializer for a cipher operation object of
1672 * type #psa_cipher_operation_t.
1673 */
1674#ifdef __DOXYGEN_ONLY__
1675/* This is an example definition for documentation purposes.
1676 * Implementations should define a suitable value in `crypto_struct.h`.
1677 */
1678#define PSA_CIPHER_OPERATION_INIT {0}
1679#endif
1680
1681/** Return an initial value for a cipher operation object.
1682 */
1683static psa_cipher_operation_t psa_cipher_operation_init(void);
1684
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001685/** Set the key for a multipart symmetric encryption operation.
1686 *
1687 * The sequence of operations to encrypt a message with a symmetric cipher
1688 * is as follows:
1689 * -# Allocate an operation object which will be passed to all the functions
1690 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001691 * -# Initialize the operation object with one of the methods described in the
1692 * documentation for #psa_cipher_operation_t, e.g.
1693 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001694 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001695 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001696 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001697 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001698 * requires a specific IV value.
1699 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1700 * of the message each time.
1701 * -# Call psa_cipher_finish().
1702 *
1703 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001704 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001705 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001706 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001707 * eventually terminate the operation. The following events terminate an
1708 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001709 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001710 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001711 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001712 * \param[in,out] operation The operation object to set up. It must have
1713 * been initialized as per the documentation for
1714 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001715 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001716 * It must remain valid until the operation
1717 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001718 * \param alg The cipher algorithm to compute
1719 * (\c PSA_ALG_XXX value such that
1720 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001721 *
Gilles Peskine28538492018-07-11 17:34:00 +02001722 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001723 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001724 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001725 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001726 * \retval #PSA_ERROR_NOT_PERMITTED
1727 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001728 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001729 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001730 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001731 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1732 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1733 * \retval #PSA_ERROR_HARDWARE_FAILURE
1734 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001735 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001736 * The operation state is not valid (already set up and not
1737 * subsequently completed).
1738 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001739 * The library has not been previously initialized by psa_crypto_init().
1740 * It is implementation-dependent whether a failure to initialize
1741 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001742 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001743psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001744 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001745 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001746
1747/** Set the key for a multipart symmetric decryption operation.
1748 *
1749 * The sequence of operations to decrypt a message with a symmetric cipher
1750 * is as follows:
1751 * -# Allocate an operation object which will be passed to all the functions
1752 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001753 * -# Initialize the operation object with one of the methods described in the
1754 * documentation for #psa_cipher_operation_t, e.g.
1755 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001756 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001757 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001758 * decryption. If the IV is prepended to the ciphertext, you can call
1759 * psa_cipher_update() on a buffer containing the IV followed by the
1760 * beginning of the message.
1761 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1762 * of the message each time.
1763 * -# Call psa_cipher_finish().
1764 *
1765 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001766 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001767 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001768 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001769 * eventually terminate the operation. The following events terminate an
1770 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001771 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001772 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001773 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001774 * \param[in,out] operation The operation object to set up. It must have
1775 * been initialized as per the documentation for
1776 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001777 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001778 * It must remain valid until the operation
1779 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001780 * \param alg The cipher algorithm to compute
1781 * (\c PSA_ALG_XXX value such that
1782 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001783 *
Gilles Peskine28538492018-07-11 17:34:00 +02001784 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001785 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001786 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001787 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001788 * \retval #PSA_ERROR_NOT_PERMITTED
1789 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001790 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001791 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001792 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001793 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1794 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1795 * \retval #PSA_ERROR_HARDWARE_FAILURE
1796 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001797 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001798 * The operation state is not valid (already set up and not
1799 * subsequently completed).
1800 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001801 * The library has not been previously initialized by psa_crypto_init().
1802 * It is implementation-dependent whether a failure to initialize
1803 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001804 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001805psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001806 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001807 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001808
Gilles Peskinedcd14942018-07-12 00:30:52 +02001809/** Generate an IV for a symmetric encryption operation.
1810 *
1811 * This function generates a random IV (initialization vector), nonce
1812 * or initial counter value for the encryption operation as appropriate
1813 * for the chosen algorithm, key type and key size.
1814 *
1815 * The application must call psa_cipher_encrypt_setup() before
1816 * calling this function.
1817 *
1818 * If this function returns an error status, the operation becomes inactive.
1819 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001820 * \param[in,out] operation Active cipher operation.
1821 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001822 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001823 * \param[out] iv_length On success, the number of bytes of the
1824 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001825 *
1826 * \retval #PSA_SUCCESS
1827 * Success.
1828 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001829 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001830 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001831 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001832 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1833 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1834 * \retval #PSA_ERROR_HARDWARE_FAILURE
1835 * \retval #PSA_ERROR_TAMPERING_DETECTED
1836 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001837psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1838 unsigned char *iv,
1839 size_t iv_size,
1840 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001841
Gilles Peskinedcd14942018-07-12 00:30:52 +02001842/** Set the IV for a symmetric encryption or decryption operation.
1843 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001844 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001845 * or initial counter value for the encryption or decryption operation.
1846 *
1847 * The application must call psa_cipher_encrypt_setup() before
1848 * calling this function.
1849 *
1850 * If this function returns an error status, the operation becomes inactive.
1851 *
1852 * \note When encrypting, applications should use psa_cipher_generate_iv()
1853 * instead of this function, unless implementing a protocol that requires
1854 * a non-random IV.
1855 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001856 * \param[in,out] operation Active cipher operation.
1857 * \param[in] iv Buffer containing the IV to use.
1858 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001859 *
1860 * \retval #PSA_SUCCESS
1861 * Success.
1862 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001863 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001864 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001865 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001866 * or the chosen algorithm does not use an IV.
1867 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1868 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1869 * \retval #PSA_ERROR_HARDWARE_FAILURE
1870 * \retval #PSA_ERROR_TAMPERING_DETECTED
1871 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001872psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1873 const unsigned char *iv,
1874 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001875
Gilles Peskinedcd14942018-07-12 00:30:52 +02001876/** Encrypt or decrypt a message fragment in an active cipher operation.
1877 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001878 * Before calling this function, you must:
1879 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1880 * The choice of setup function determines whether this function
1881 * encrypts or decrypts its input.
1882 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1883 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001884 *
1885 * If this function returns an error status, the operation becomes inactive.
1886 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001887 * \param[in,out] operation Active cipher operation.
1888 * \param[in] input Buffer containing the message fragment to
1889 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001890 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001891 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001892 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001893 * \param[out] output_length On success, the number of bytes
1894 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001895 *
1896 * \retval #PSA_SUCCESS
1897 * Success.
1898 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001899 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001900 * not set, or already completed).
1901 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1902 * The size of the \p output buffer is too small.
1903 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1904 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1905 * \retval #PSA_ERROR_HARDWARE_FAILURE
1906 * \retval #PSA_ERROR_TAMPERING_DETECTED
1907 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001908psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1909 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001910 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001911 unsigned char *output,
1912 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001913 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001914
Gilles Peskinedcd14942018-07-12 00:30:52 +02001915/** Finish encrypting or decrypting a message in a cipher operation.
1916 *
1917 * The application must call psa_cipher_encrypt_setup() or
1918 * psa_cipher_decrypt_setup() before calling this function. The choice
1919 * of setup function determines whether this function encrypts or
1920 * decrypts its input.
1921 *
1922 * This function finishes the encryption or decryption of the message
1923 * formed by concatenating the inputs passed to preceding calls to
1924 * psa_cipher_update().
1925 *
1926 * When this function returns, the operation becomes inactive.
1927 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001928 * \param[in,out] operation Active cipher operation.
1929 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001930 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001931 * \param[out] output_length On success, the number of bytes
1932 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001933 *
1934 * \retval #PSA_SUCCESS
1935 * Success.
1936 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001937 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001938 * not set, or already completed).
1939 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1940 * The size of the \p output buffer is too small.
1941 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1942 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1943 * \retval #PSA_ERROR_HARDWARE_FAILURE
1944 * \retval #PSA_ERROR_TAMPERING_DETECTED
1945 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001946psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001947 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001948 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001949 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001950
Gilles Peskinedcd14942018-07-12 00:30:52 +02001951/** Abort a cipher operation.
1952 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001953 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001954 * \p operation structure itself. Once aborted, the operation object
1955 * can be reused for another operation by calling
1956 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001957 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001958 * You may call this function any time after the operation object has
1959 * been initialized by any of the following methods:
1960 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1961 * whether it succeeds or not.
1962 * - Initializing the \c struct to all-bits-zero.
1963 * - Initializing the \c struct to logical zeros, e.g.
1964 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001965 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001966 * In particular, calling psa_cipher_abort() after the operation has been
1967 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1968 * is safe and has no effect.
1969 *
1970 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001971 *
1972 * \retval #PSA_SUCCESS
1973 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001974 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001975 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1976 * \retval #PSA_ERROR_HARDWARE_FAILURE
1977 * \retval #PSA_ERROR_TAMPERING_DETECTED
1978 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001979psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1980
1981/**@}*/
1982
Gilles Peskine3b555712018-03-03 21:27:57 +01001983/** \defgroup aead Authenticated encryption with associated data (AEAD)
1984 * @{
1985 */
1986
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001987/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001988 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001989 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001990 * \param alg The AEAD algorithm to compute
1991 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001992 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001993 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001994 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001995 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001996 * but not encrypted.
1997 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001998 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001999 * encrypted.
2000 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002001 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002002 * encrypted data. The additional data is not
2003 * part of this output. For algorithms where the
2004 * encrypted data and the authentication tag
2005 * are defined as separate outputs, the
2006 * authentication tag is appended to the
2007 * encrypted data.
2008 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2009 * This must be at least
2010 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2011 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002012 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002013 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002014 *
Gilles Peskine28538492018-07-11 17:34:00 +02002015 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002016 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002017 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002018 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002019 * \retval #PSA_ERROR_NOT_PERMITTED
2020 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002021 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002022 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002023 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002024 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2025 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2026 * \retval #PSA_ERROR_HARDWARE_FAILURE
2027 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002028 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002029 * The library has not been previously initialized by psa_crypto_init().
2030 * It is implementation-dependent whether a failure to initialize
2031 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002032 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002033psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002034 psa_algorithm_t alg,
2035 const uint8_t *nonce,
2036 size_t nonce_length,
2037 const uint8_t *additional_data,
2038 size_t additional_data_length,
2039 const uint8_t *plaintext,
2040 size_t plaintext_length,
2041 uint8_t *ciphertext,
2042 size_t ciphertext_size,
2043 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002044
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002045/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002046 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002047 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002048 * \param alg The AEAD algorithm to compute
2049 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002050 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002051 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002052 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002053 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002054 * but not encrypted.
2055 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002056 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002057 * encrypted. For algorithms where the
2058 * encrypted data and the authentication tag
2059 * are defined as separate inputs, the buffer
2060 * must contain the encrypted data followed
2061 * by the authentication tag.
2062 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002063 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002064 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2065 * This must be at least
2066 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2067 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002068 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002069 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002070 *
Gilles Peskine28538492018-07-11 17:34:00 +02002071 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002072 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002073 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002074 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002075 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002076 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002077 * \retval #PSA_ERROR_NOT_PERMITTED
2078 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002079 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002080 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002081 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002082 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2083 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2084 * \retval #PSA_ERROR_HARDWARE_FAILURE
2085 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002086 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002087 * The library has not been previously initialized by psa_crypto_init().
2088 * It is implementation-dependent whether a failure to initialize
2089 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002090 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002091psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002092 psa_algorithm_t alg,
2093 const uint8_t *nonce,
2094 size_t nonce_length,
2095 const uint8_t *additional_data,
2096 size_t additional_data_length,
2097 const uint8_t *ciphertext,
2098 size_t ciphertext_length,
2099 uint8_t *plaintext,
2100 size_t plaintext_size,
2101 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002102
Gilles Peskine30a9e412019-01-14 18:36:12 +01002103/** The type of the state data structure for multipart AEAD operations.
2104 *
2105 * Before calling any function on an AEAD operation object, the application
2106 * must initialize it by any of the following means:
2107 * - Set the structure to all-bits-zero, for example:
2108 * \code
2109 * psa_aead_operation_t operation;
2110 * memset(&operation, 0, sizeof(operation));
2111 * \endcode
2112 * - Initialize the structure to logical zero values, for example:
2113 * \code
2114 * psa_aead_operation_t operation = {0};
2115 * \endcode
2116 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2117 * for example:
2118 * \code
2119 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2120 * \endcode
2121 * - Assign the result of the function psa_aead_operation_init()
2122 * to the structure, for example:
2123 * \code
2124 * psa_aead_operation_t operation;
2125 * operation = psa_aead_operation_init();
2126 * \endcode
2127 *
2128 * This is an implementation-defined \c struct. Applications should not
2129 * make any assumptions about the content of this structure except
2130 * as directed by the documentation of a specific implementation. */
2131typedef struct psa_aead_operation_s psa_aead_operation_t;
2132
2133/** \def PSA_AEAD_OPERATION_INIT
2134 *
2135 * This macro returns a suitable initializer for an AEAD operation object of
2136 * type #psa_aead_operation_t.
2137 */
2138#ifdef __DOXYGEN_ONLY__
2139/* This is an example definition for documentation purposes.
2140 * Implementations should define a suitable value in `crypto_struct.h`.
2141 */
2142#define PSA_AEAD_OPERATION_INIT {0}
2143#endif
2144
2145/** Return an initial value for an AEAD operation object.
2146 */
2147static psa_aead_operation_t psa_aead_operation_init(void);
2148
2149/** Set the key for a multipart authenticated encryption operation.
2150 *
2151 * The sequence of operations to encrypt a message with authentication
2152 * is as follows:
2153 * -# Allocate an operation object which will be passed to all the functions
2154 * listed here.
2155 * -# Initialize the operation object with one of the methods described in the
2156 * documentation for #psa_aead_operation_t, e.g.
2157 * PSA_AEAD_OPERATION_INIT.
2158 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002159 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2160 * inputs to the subsequent calls to psa_aead_update_ad() and
2161 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2162 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002163 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2164 * generate or set the nonce. You should use
2165 * psa_aead_generate_nonce() unless the protocol you are implementing
2166 * requires a specific nonce value.
2167 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2168 * of the non-encrypted additional authenticated data each time.
2169 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002170 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002171 * -# Call psa_aead_finish().
2172 *
2173 * The application may call psa_aead_abort() at any time after the operation
2174 * has been initialized.
2175 *
2176 * After a successful call to psa_aead_encrypt_setup(), the application must
2177 * eventually terminate the operation. The following events terminate an
2178 * operation:
2179 * - A failed call to any of the \c psa_aead_xxx functions.
2180 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2181 *
2182 * \param[in,out] operation The operation object to set up. It must have
2183 * been initialized as per the documentation for
2184 * #psa_aead_operation_t and not yet in use.
2185 * \param handle Handle to the key to use for the operation.
2186 * It must remain valid until the operation
2187 * terminates.
2188 * \param alg The AEAD algorithm to compute
2189 * (\c PSA_ALG_XXX value such that
2190 * #PSA_ALG_IS_AEAD(\p alg) is true).
2191 *
2192 * \retval #PSA_SUCCESS
2193 * Success.
2194 * \retval #PSA_ERROR_INVALID_HANDLE
2195 * \retval #PSA_ERROR_EMPTY_SLOT
2196 * \retval #PSA_ERROR_NOT_PERMITTED
2197 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002198 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002199 * \retval #PSA_ERROR_NOT_SUPPORTED
2200 * \p alg is not supported or is not an AEAD algorithm.
2201 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2202 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2203 * \retval #PSA_ERROR_HARDWARE_FAILURE
2204 * \retval #PSA_ERROR_TAMPERING_DETECTED
2205 * \retval #PSA_ERROR_BAD_STATE
2206 * The library has not been previously initialized by psa_crypto_init().
2207 * It is implementation-dependent whether a failure to initialize
2208 * results in this error code.
2209 */
2210psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2211 psa_key_handle_t handle,
2212 psa_algorithm_t alg);
2213
2214/** Set the key for a multipart authenticated decryption operation.
2215 *
2216 * The sequence of operations to decrypt a message with authentication
2217 * is as follows:
2218 * -# Allocate an operation object which will be passed to all the functions
2219 * listed here.
2220 * -# Initialize the operation object with one of the methods described in the
2221 * documentation for #psa_aead_operation_t, e.g.
2222 * PSA_AEAD_OPERATION_INIT.
2223 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002224 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2225 * inputs to the subsequent calls to psa_aead_update_ad() and
2226 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2227 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002228 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2229 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2230 * of the non-encrypted additional authenticated data each time.
2231 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002232 * of the ciphertext to decrypt each time.
2233 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002234 *
2235 * The application may call psa_aead_abort() at any time after the operation
2236 * has been initialized.
2237 *
2238 * After a successful call to psa_aead_decrypt_setup(), the application must
2239 * eventually terminate the operation. The following events terminate an
2240 * operation:
2241 * - A failed call to any of the \c psa_aead_xxx functions.
2242 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2243 *
2244 * \param[in,out] operation The operation object to set up. It must have
2245 * been initialized as per the documentation for
2246 * #psa_aead_operation_t and not yet in use.
2247 * \param handle Handle to the key to use for the operation.
2248 * It must remain valid until the operation
2249 * terminates.
2250 * \param alg The AEAD algorithm to compute
2251 * (\c PSA_ALG_XXX value such that
2252 * #PSA_ALG_IS_AEAD(\p alg) is true).
2253 *
2254 * \retval #PSA_SUCCESS
2255 * Success.
2256 * \retval #PSA_ERROR_INVALID_HANDLE
2257 * \retval #PSA_ERROR_EMPTY_SLOT
2258 * \retval #PSA_ERROR_NOT_PERMITTED
2259 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002260 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002261 * \retval #PSA_ERROR_NOT_SUPPORTED
2262 * \p alg is not supported or is not an AEAD algorithm.
2263 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2264 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2265 * \retval #PSA_ERROR_HARDWARE_FAILURE
2266 * \retval #PSA_ERROR_TAMPERING_DETECTED
2267 * \retval #PSA_ERROR_BAD_STATE
2268 * The library has not been previously initialized by psa_crypto_init().
2269 * It is implementation-dependent whether a failure to initialize
2270 * results in this error code.
2271 */
2272psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2273 psa_key_handle_t handle,
2274 psa_algorithm_t alg);
2275
2276/** Generate a random nonce for an authenticated encryption operation.
2277 *
2278 * This function generates a random nonce for the authenticated encryption
2279 * operation with an appropriate size for the chosen algorithm, key type
2280 * and key size.
2281 *
2282 * The application must call psa_aead_encrypt_setup() before
2283 * calling this function.
2284 *
2285 * If this function returns an error status, the operation becomes inactive.
2286 *
2287 * \param[in,out] operation Active AEAD operation.
2288 * \param[out] nonce Buffer where the generated nonce is to be
2289 * written.
2290 * \param nonce_size Size of the \p nonce buffer in bytes.
2291 * \param[out] nonce_length On success, the number of bytes of the
2292 * generated nonce.
2293 *
2294 * \retval #PSA_SUCCESS
2295 * Success.
2296 * \retval #PSA_ERROR_BAD_STATE
2297 * The operation state is not valid (not set up, or nonce already set).
2298 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2299 * The size of the \p nonce buffer is too small.
2300 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2301 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2302 * \retval #PSA_ERROR_HARDWARE_FAILURE
2303 * \retval #PSA_ERROR_TAMPERING_DETECTED
2304 */
2305psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2306 unsigned char *nonce,
2307 size_t nonce_size,
2308 size_t *nonce_length);
2309
2310/** Set the nonce for an authenticated encryption or decryption operation.
2311 *
2312 * This function sets the nonce for the authenticated
2313 * encryption or decryption operation.
2314 *
2315 * The application must call psa_aead_encrypt_setup() before
2316 * calling this function.
2317 *
2318 * If this function returns an error status, the operation becomes inactive.
2319 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002320 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002321 * instead of this function, unless implementing a protocol that requires
2322 * a non-random IV.
2323 *
2324 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002325 * \param[in] nonce Buffer containing the nonce to use.
2326 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002327 *
2328 * \retval #PSA_SUCCESS
2329 * Success.
2330 * \retval #PSA_ERROR_BAD_STATE
2331 * The operation state is not valid (not set up, or nonce already set).
2332 * \retval #PSA_ERROR_INVALID_ARGUMENT
2333 * The size of \p nonce is not acceptable for the chosen algorithm.
2334 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2335 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2336 * \retval #PSA_ERROR_HARDWARE_FAILURE
2337 * \retval #PSA_ERROR_TAMPERING_DETECTED
2338 */
2339psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2340 const unsigned char *nonce,
2341 size_t nonce_length);
2342
Gilles Peskinebc59c852019-01-17 15:26:08 +01002343/** Declare the lengths of the message and additional data for AEAD.
2344 *
2345 * The application must call this function before calling
2346 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2347 * the operation requires it. If the algorithm does not require it,
2348 * calling this function is optional, but if this function is called
2349 * then the implementation must enforce the lengths.
2350 *
2351 * You may call this function before or after setting the nonce with
2352 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2353 *
2354 * - For #PSA_ALG_CCM, calling this function is required.
2355 * - For the other AEAD algorithms defined in this specification, calling
2356 * this function is not required.
2357 * - For vendor-defined algorithm, refer to the vendor documentation.
2358 *
2359 * \param[in,out] operation Active AEAD operation.
2360 * \param ad_length Size of the non-encrypted additional
2361 * authenticated data in bytes.
2362 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2363 *
2364 * \retval #PSA_SUCCESS
2365 * Success.
2366 * \retval #PSA_ERROR_BAD_STATE
2367 * The operation state is not valid (not set up, already completed,
2368 * or psa_aead_update_ad() or psa_aead_update() already called).
2369 * \retval #PSA_ERROR_INVALID_ARGUMENT
2370 * At least one of the lengths is not acceptable for the chosen
2371 * algorithm.
2372 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2373 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2374 * \retval #PSA_ERROR_HARDWARE_FAILURE
2375 * \retval #PSA_ERROR_TAMPERING_DETECTED
2376 */
2377psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2378 size_t ad_length,
2379 size_t plaintext_length);
2380
Gilles Peskine30a9e412019-01-14 18:36:12 +01002381/** Pass additional data to an active AEAD operation.
2382 *
2383 * Additional data is authenticated, but not encrypted.
2384 *
2385 * You may call this function multiple times to pass successive fragments
2386 * of the additional data. You may not call this function after passing
2387 * data to encrypt or decrypt with psa_aead_update().
2388 *
2389 * Before calling this function, you must:
2390 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2391 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2392 *
2393 * If this function returns an error status, the operation becomes inactive.
2394 *
2395 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2396 * there is no guarantee that the input is valid. Therefore, until
2397 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2398 * treat the input as untrusted and prepare to undo any action that
2399 * depends on the input if psa_aead_verify() returns an error status.
2400 *
2401 * \param[in,out] operation Active AEAD operation.
2402 * \param[in] input Buffer containing the fragment of
2403 * additional data.
2404 * \param input_length Size of the \p input buffer in bytes.
2405 *
2406 * \retval #PSA_SUCCESS
2407 * Success.
2408 * \retval #PSA_ERROR_BAD_STATE
2409 * The operation state is not valid (not set up, nonce not set,
2410 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002411 * \retval #PSA_ERROR_INVALID_ARGUMENT
2412 * The total input length overflows the additional data length that
2413 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002414 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2415 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2416 * \retval #PSA_ERROR_HARDWARE_FAILURE
2417 * \retval #PSA_ERROR_TAMPERING_DETECTED
2418 */
2419psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2420 const uint8_t *input,
2421 size_t input_length);
2422
2423/** Encrypt or decrypt a message fragment in an active AEAD operation.
2424 *
2425 * Before calling this function, you must:
2426 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2427 * The choice of setup function determines whether this function
2428 * encrypts or decrypts its input.
2429 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2430 * 3. Call psa_aead_update_ad() to pass all the additional data.
2431 *
2432 * If this function returns an error status, the operation becomes inactive.
2433 *
2434 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2435 * there is no guarantee that the input is valid. Therefore, until
2436 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2437 * - Do not use the output in any way other than storing it in a
2438 * confidential location. If you take any action that depends
2439 * on the tentative decrypted data, this action will need to be
2440 * undone if the input turns out not to be valid. Furthermore,
2441 * if an adversary can observe that this action took place
2442 * (for example through timing), they may be able to use this
2443 * fact as an oracle to decrypt any message encrypted with the
2444 * same key.
2445 * - In particular, do not copy the output anywhere but to a
2446 * memory or storage space that you have exclusive access to.
2447 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002448 * This function does not require the input to be aligned to any
2449 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002450 * a whole block at a time, it must consume all the input provided, but
2451 * it may delay the end of the corresponding output until a subsequent
2452 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2453 * provides sufficient input. The amount of data that can be delayed
2454 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002455 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002456 * \param[in,out] operation Active AEAD operation.
2457 * \param[in] input Buffer containing the message fragment to
2458 * encrypt or decrypt.
2459 * \param input_length Size of the \p input buffer in bytes.
2460 * \param[out] output Buffer where the output is to be written.
2461 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002462 * This must be at least
2463 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2464 * \p input_length) where \c alg is the
2465 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002466 * \param[out] output_length On success, the number of bytes
2467 * that make up the returned output.
2468 *
2469 * \retval #PSA_SUCCESS
2470 * Success.
2471 * \retval #PSA_ERROR_BAD_STATE
2472 * The operation state is not valid (not set up, nonce not set
2473 * or already completed).
2474 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2475 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002476 * You can determine a sufficient buffer size by calling
2477 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2478 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002479 * \retval #PSA_ERROR_INVALID_ARGUMENT
2480 * The total length of input to psa_aead_update_ad() so far is
2481 * less than the additional data length that was previously
2482 * specified with psa_aead_set_lengths().
2483 * \retval #PSA_ERROR_INVALID_ARGUMENT
2484 * The total input length overflows the plaintext length that
2485 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002486 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2487 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2488 * \retval #PSA_ERROR_HARDWARE_FAILURE
2489 * \retval #PSA_ERROR_TAMPERING_DETECTED
2490 */
2491psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2492 const uint8_t *input,
2493 size_t input_length,
2494 unsigned char *output,
2495 size_t output_size,
2496 size_t *output_length);
2497
2498/** Finish encrypting a message in an AEAD operation.
2499 *
2500 * The operation must have been set up with psa_aead_encrypt_setup().
2501 *
2502 * This function finishes the authentication of the additional data
2503 * formed by concatenating the inputs passed to preceding calls to
2504 * psa_aead_update_ad() with the plaintext formed by concatenating the
2505 * inputs passed to preceding calls to psa_aead_update().
2506 *
2507 * This function has two output buffers:
2508 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002509 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002510 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002511 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002512 * that the operation performs.
2513 *
2514 * When this function returns, the operation becomes inactive.
2515 *
2516 * \param[in,out] operation Active AEAD operation.
2517 * \param[out] ciphertext Buffer where the last part of the ciphertext
2518 * is to be written.
2519 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002520 * This must be at least
2521 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2522 * \c alg is the algorithm that is being
2523 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002524 * \param[out] ciphertext_length On success, the number of bytes of
2525 * returned ciphertext.
2526 * \param[out] tag Buffer where the authentication tag is
2527 * to be written.
2528 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002529 * This must be at least
2530 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2531 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002532 * \param[out] tag_length On success, the number of bytes
2533 * that make up the returned tag.
2534 *
2535 * \retval #PSA_SUCCESS
2536 * Success.
2537 * \retval #PSA_ERROR_BAD_STATE
2538 * The operation state is not valid (not set up, nonce not set,
2539 * decryption, or already completed).
2540 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002541 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002542 * You can determine a sufficient buffer size for \p ciphertext by
2543 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2544 * where \c alg is the algorithm that is being calculated.
2545 * You can determine a sufficient buffer size for \p tag by
2546 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002547 * \retval #PSA_ERROR_INVALID_ARGUMENT
2548 * The total length of input to psa_aead_update_ad() so far is
2549 * less than the additional data length that was previously
2550 * specified with psa_aead_set_lengths().
2551 * \retval #PSA_ERROR_INVALID_ARGUMENT
2552 * The total length of input to psa_aead_update() so far is
2553 * less than the plaintext length that was previously
2554 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002555 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2556 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2557 * \retval #PSA_ERROR_HARDWARE_FAILURE
2558 * \retval #PSA_ERROR_TAMPERING_DETECTED
2559 */
2560psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002561 uint8_t *ciphertext,
2562 size_t ciphertext_size,
2563 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002564 uint8_t *tag,
2565 size_t tag_size,
2566 size_t *tag_length);
2567
2568/** Finish authenticating and decrypting a message in an AEAD operation.
2569 *
2570 * The operation must have been set up with psa_aead_decrypt_setup().
2571 *
2572 * This function finishes the authentication of the additional data
2573 * formed by concatenating the inputs passed to preceding calls to
2574 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2575 * inputs passed to preceding calls to psa_aead_update().
2576 *
2577 * When this function returns, the operation becomes inactive.
2578 *
2579 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002580 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002581 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002582 * from previous calls to psa_aead_update()
2583 * that could not be processed until the end
2584 * of the input.
2585 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002586 * This must be at least
2587 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2588 * \c alg is the algorithm that is being
2589 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002590 * \param[out] plaintext_length On success, the number of bytes of
2591 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002592 * \param[in] tag Buffer containing the authentication tag.
2593 * \param tag_length Size of the \p tag buffer in bytes.
2594 *
2595 * \retval #PSA_SUCCESS
2596 * Success.
2597 * \retval #PSA_ERROR_BAD_STATE
2598 * The operation state is not valid (not set up, nonce not set,
2599 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002600 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2601 * The size of the \p plaintext buffer is too small.
2602 * You can determine a sufficient buffer size for \p plaintext by
2603 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2604 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002605 * \retval #PSA_ERROR_INVALID_ARGUMENT
2606 * The total length of input to psa_aead_update_ad() so far is
2607 * less than the additional data length that was previously
2608 * specified with psa_aead_set_lengths().
2609 * \retval #PSA_ERROR_INVALID_ARGUMENT
2610 * The total length of input to psa_aead_update() so far is
2611 * less than the plaintext length that was previously
2612 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002613 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2614 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2615 * \retval #PSA_ERROR_HARDWARE_FAILURE
2616 * \retval #PSA_ERROR_TAMPERING_DETECTED
2617 */
2618psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002619 uint8_t *plaintext,
2620 size_t plaintext_size,
2621 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002622 const uint8_t *tag,
2623 size_t tag_length);
2624
2625/** Abort an AEAD operation.
2626 *
2627 * Aborting an operation frees all associated resources except for the
2628 * \p operation structure itself. Once aborted, the operation object
2629 * can be reused for another operation by calling
2630 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2631 *
2632 * You may call this function any time after the operation object has
2633 * been initialized by any of the following methods:
2634 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2635 * whether it succeeds or not.
2636 * - Initializing the \c struct to all-bits-zero.
2637 * - Initializing the \c struct to logical zeros, e.g.
2638 * `psa_aead_operation_t operation = {0}`.
2639 *
2640 * In particular, calling psa_aead_abort() after the operation has been
2641 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2642 * is safe and has no effect.
2643 *
2644 * \param[in,out] operation Initialized AEAD operation.
2645 *
2646 * \retval #PSA_SUCCESS
2647 * \retval #PSA_ERROR_BAD_STATE
2648 * \p operation is not an active AEAD operation.
2649 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2650 * \retval #PSA_ERROR_HARDWARE_FAILURE
2651 * \retval #PSA_ERROR_TAMPERING_DETECTED
2652 */
2653psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2654
Gilles Peskine3b555712018-03-03 21:27:57 +01002655/**@}*/
2656
Gilles Peskine20035e32018-02-03 22:44:14 +01002657/** \defgroup asymmetric Asymmetric cryptography
2658 * @{
2659 */
2660
2661/**
2662 * \brief Sign a hash or short message with a private key.
2663 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002664 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002665 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002666 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2667 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2668 * to determine the hash algorithm to use.
2669 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002670 * \param handle Handle to the key to use for the operation.
2671 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002672 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002673 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002674 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002675 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002676 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002677 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002678 * \param[out] signature_length On success, the number of bytes
2679 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002680 *
Gilles Peskine28538492018-07-11 17:34:00 +02002681 * \retval #PSA_SUCCESS
2682 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002683 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002684 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002685 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002686 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002687 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002688 * \retval #PSA_ERROR_NOT_SUPPORTED
2689 * \retval #PSA_ERROR_INVALID_ARGUMENT
2690 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2691 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2692 * \retval #PSA_ERROR_HARDWARE_FAILURE
2693 * \retval #PSA_ERROR_TAMPERING_DETECTED
2694 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002695 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002696 * The library has not been previously initialized by psa_crypto_init().
2697 * It is implementation-dependent whether a failure to initialize
2698 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002699 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002700psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002701 psa_algorithm_t alg,
2702 const uint8_t *hash,
2703 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002704 uint8_t *signature,
2705 size_t signature_size,
2706 size_t *signature_length);
2707
2708/**
2709 * \brief Verify the signature a hash or short message using a public key.
2710 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002711 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002712 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002713 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2714 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2715 * to determine the hash algorithm to use.
2716 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002717 * \param handle Handle to the key to use for the operation.
2718 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002719 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002720 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002721 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002722 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002723 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002724 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002725 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002726 *
Gilles Peskine28538492018-07-11 17:34:00 +02002727 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002728 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002729 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002730 * The calculation was perfomed successfully, but the passed
2731 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002732 * \retval #PSA_ERROR_NOT_SUPPORTED
2733 * \retval #PSA_ERROR_INVALID_ARGUMENT
2734 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2735 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2736 * \retval #PSA_ERROR_HARDWARE_FAILURE
2737 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002738 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002739 * The library has not been previously initialized by psa_crypto_init().
2740 * It is implementation-dependent whether a failure to initialize
2741 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002742 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002743psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002744 psa_algorithm_t alg,
2745 const uint8_t *hash,
2746 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002747 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002748 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002749
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002750/**
2751 * \brief Encrypt a short message with a public key.
2752 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002753 * \param handle Handle to the key to use for the operation.
2754 * It must be a public key or an asymmetric
2755 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002756 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002757 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002758 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002759 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002760 * \param[in] salt A salt or label, if supported by the
2761 * encryption algorithm.
2762 * If the algorithm does not support a
2763 * salt, pass \c NULL.
2764 * If the algorithm supports an optional
2765 * salt and you do not want to pass a salt,
2766 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002767 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002768 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2769 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002770 * \param salt_length Size of the \p salt buffer in bytes.
2771 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002772 * \param[out] output Buffer where the encrypted message is to
2773 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002774 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002775 * \param[out] output_length On success, the number of bytes
2776 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002777 *
Gilles Peskine28538492018-07-11 17:34:00 +02002778 * \retval #PSA_SUCCESS
2779 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002780 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002781 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002782 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002783 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002784 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002785 * \retval #PSA_ERROR_NOT_SUPPORTED
2786 * \retval #PSA_ERROR_INVALID_ARGUMENT
2787 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2788 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2789 * \retval #PSA_ERROR_HARDWARE_FAILURE
2790 * \retval #PSA_ERROR_TAMPERING_DETECTED
2791 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002792 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002793 * The library has not been previously initialized by psa_crypto_init().
2794 * It is implementation-dependent whether a failure to initialize
2795 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002796 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002797psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002798 psa_algorithm_t alg,
2799 const uint8_t *input,
2800 size_t input_length,
2801 const uint8_t *salt,
2802 size_t salt_length,
2803 uint8_t *output,
2804 size_t output_size,
2805 size_t *output_length);
2806
2807/**
2808 * \brief Decrypt a short message with a private key.
2809 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002810 * \param handle Handle to the key to use for the operation.
2811 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002812 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002813 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002814 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002815 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002816 * \param[in] salt A salt or label, if supported by the
2817 * encryption algorithm.
2818 * If the algorithm does not support a
2819 * salt, pass \c NULL.
2820 * If the algorithm supports an optional
2821 * salt and you do not want to pass a salt,
2822 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002823 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002824 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2825 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002826 * \param salt_length Size of the \p salt buffer in bytes.
2827 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002828 * \param[out] output Buffer where the decrypted message is to
2829 * be written.
2830 * \param output_size Size of the \c output buffer in bytes.
2831 * \param[out] output_length On success, the number of bytes
2832 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002833 *
Gilles Peskine28538492018-07-11 17:34:00 +02002834 * \retval #PSA_SUCCESS
2835 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002836 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002837 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002838 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002839 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002840 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002841 * \retval #PSA_ERROR_NOT_SUPPORTED
2842 * \retval #PSA_ERROR_INVALID_ARGUMENT
2843 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2844 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2845 * \retval #PSA_ERROR_HARDWARE_FAILURE
2846 * \retval #PSA_ERROR_TAMPERING_DETECTED
2847 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2848 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002849 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002850 * The library has not been previously initialized by psa_crypto_init().
2851 * It is implementation-dependent whether a failure to initialize
2852 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002853 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002854psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002855 psa_algorithm_t alg,
2856 const uint8_t *input,
2857 size_t input_length,
2858 const uint8_t *salt,
2859 size_t salt_length,
2860 uint8_t *output,
2861 size_t output_size,
2862 size_t *output_length);
2863
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002864/**@}*/
2865
Gilles Peskine35675b62019-05-16 17:26:11 +02002866/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002867 * @{
2868 */
2869
Gilles Peskine35675b62019-05-16 17:26:11 +02002870/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002871 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002872 * Before calling any function on a key derivation operation object, the
2873 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002874 * - Set the structure to all-bits-zero, for example:
2875 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002876 * psa_key_derivation_operation_t operation;
2877 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002878 * \endcode
2879 * - Initialize the structure to logical zero values, for example:
2880 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002881 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002882 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002883 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002884 * for example:
2885 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002886 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002887 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002888 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002889 * to the structure, for example:
2890 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002891 * psa_key_derivation_operation_t operation;
2892 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002893 * \endcode
2894 *
2895 * This is an implementation-defined \c struct. Applications should not
2896 * make any assumptions about the content of this structure except
2897 * as directed by the documentation of a specific implementation.
2898 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02002899typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002900
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002901/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002902 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002903 * This macro returns a suitable initializer for a key derivation operation
2904 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002905 */
2906#ifdef __DOXYGEN_ONLY__
2907/* This is an example definition for documentation purposes.
2908 * Implementations should define a suitable value in `crypto_struct.h`.
2909 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002910#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02002911#endif
2912
Gilles Peskine35675b62019-05-16 17:26:11 +02002913/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002914 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002915static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002916
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002917/** Set up a key derivation operation.
2918 *
2919 * A key derivation algorithm takes some inputs and uses them to generate
2920 * a byte stream in a deterministic way.
2921 * This byte stream can be used to produce keys and other
2922 * cryptographic material.
2923 *
2924 * To derive a key:
2925 * - Start with an initialized object of type #psa_key_derivation_operation_t.
2926 * - Call psa_key_derivation_setup() to select the algorithm.
2927 * - Provide the inputs for the key derivation by calling
2928 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2929 * as appropriate. Which inputs are needed, in what order, and whether
2930 * they may be keys and if so of what type depends on the algorithm.
2931 * - Optionally set the operation's maximum capacity with
2932 * psa_key_derivation_set_capacity(). You may do this before, in the middle
2933 * of or after providing inputs. For some algorithms, this step is mandatory
2934 * because the output depends on the maximum capacity.
2935 * - To derive a key, call psa_key_derivation_output_key().
2936 * To derive a byte string for a different purpose, call
2937 * - psa_key_derivation_output_bytes().
2938 * Successive calls to these functions use successive output bytes
2939 * calculated by the key derivation algorithm.
2940 * - Clean up the key derivation operation object with
2941 * psa_key_derivation_abort().
2942 *
2943 * \param[in,out] operation The key derivation operation object
2944 * to set up. It must
2945 * have been initialized but not set up yet.
2946 * \param alg The key derivation algorithm to compute
2947 * (\c PSA_ALG_XXX value such that
2948 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2949 *
2950 * \retval #PSA_SUCCESS
2951 * Success.
2952 * \retval #PSA_ERROR_INVALID_ARGUMENT
2953 * \c alg is not a key derivation algorithm.
2954 * \retval #PSA_ERROR_NOT_SUPPORTED
2955 * \c alg is not supported or is not a key derivation algorithm.
2956 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2957 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2958 * \retval #PSA_ERROR_HARDWARE_FAILURE
2959 * \retval #PSA_ERROR_TAMPERING_DETECTED
2960 * \retval #PSA_ERROR_BAD_STATE
2961 */
2962psa_status_t psa_key_derivation_setup(
2963 psa_key_derivation_operation_t *operation,
2964 psa_algorithm_t alg);
2965
Gilles Peskine35675b62019-05-16 17:26:11 +02002966/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002967 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002968 * The capacity of a key derivation is the maximum number of bytes that it can
2969 * return. When you get *N* bytes of output from a key derivation operation,
2970 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002971 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002972 * \param[in] operation The operation to query.
2973 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002974 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002975 * \retval #PSA_SUCCESS
2976 * \retval #PSA_ERROR_BAD_STATE
2977 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002978 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002979psa_status_t psa_key_derivation_get_capacity(
2980 const psa_key_derivation_operation_t *operation,
2981 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002982
Gilles Peskine35675b62019-05-16 17:26:11 +02002983/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002984 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002985 * The capacity of a key derivation operation is the maximum number of bytes
2986 * that the key derivation operation can return from this point onwards.
2987 *
2988 * \param[in,out] operation The key derivation operation object to modify.
2989 * \param capacity The new capacity of the operation.
2990 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002991 * current capacity.
2992 *
2993 * \retval #PSA_SUCCESS
2994 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02002995 * \p capacity is larger than the operation's current capacity.
2996 * In this case, the operation object remains valid and its capacity
2997 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002998 * \retval #PSA_ERROR_BAD_STATE
2999 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3000 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003001psa_status_t psa_key_derivation_set_capacity(
3002 psa_key_derivation_operation_t *operation,
3003 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003004
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003005/** Use the maximum possible capacity for a key derivation operation.
3006 *
3007 * Use this value as the capacity argument when setting up a key derivation
3008 * to indicate that the operation should have the maximum possible capacity.
3009 * The value of the maximum possible capacity depends on the key derivation
3010 * algorithm.
3011 */
3012#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3013
3014/** Provide an input for key derivation or key agreement.
3015 *
3016 * Which inputs are required and in what order depends on the algorithm.
3017 * Refer to the documentation of each key derivation or key agreement
3018 * algorithm for information.
3019 *
3020 * This function passes direct inputs. Some inputs must be passed as keys
3021 * using psa_key_derivation_input_key() instead of this function. Refer to
3022 * the documentation of individual step types for information.
3023 *
3024 * \param[in,out] operation The key derivation operation object to use.
3025 * It must have been set up with
3026 * psa_key_derivation_setup() and must not
3027 * have produced any output yet.
3028 * \param step Which step the input data is for.
3029 * \param[in] data Input data to use.
3030 * \param data_length Size of the \p data buffer in bytes.
3031 *
3032 * \retval #PSA_SUCCESS
3033 * Success.
3034 * \retval #PSA_ERROR_INVALID_ARGUMENT
3035 * \c step is not compatible with the operation's algorithm.
3036 * \retval #PSA_ERROR_INVALID_ARGUMENT
3037 * \c step does not allow direct inputs.
3038 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3039 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3040 * \retval #PSA_ERROR_HARDWARE_FAILURE
3041 * \retval #PSA_ERROR_TAMPERING_DETECTED
3042 * \retval #PSA_ERROR_BAD_STATE
3043 * The value of \p step is not valid given the state of \p operation.
3044 * \retval #PSA_ERROR_BAD_STATE
3045 * The library has not been previously initialized by psa_crypto_init().
3046 * It is implementation-dependent whether a failure to initialize
3047 * results in this error code.
3048 */
3049psa_status_t psa_key_derivation_input_bytes(
3050 psa_key_derivation_operation_t *operation,
3051 psa_key_derivation_step_t step,
3052 const uint8_t *data,
3053 size_t data_length);
3054
3055/** Provide an input for key derivation in the form of a key.
3056 *
3057 * Which inputs are required and in what order depends on the algorithm.
3058 * Refer to the documentation of each key derivation or key agreement
3059 * algorithm for information.
3060 *
3061 * This function passes key inputs. Some inputs must be passed as keys
3062 * of the appropriate type using this function, while others must be
3063 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3064 * the documentation of individual step types for information.
3065 *
3066 * \param[in,out] operation The key derivation operation object to use.
3067 * It must have been set up with
3068 * psa_key_derivation_setup() and must not
3069 * have produced any output yet.
3070 * \param step Which step the input data is for.
3071 * \param handle Handle to the key. It must have an
3072 * appropriate type for \p step and must
3073 * allow the usage #PSA_KEY_USAGE_DERIVE.
3074 *
3075 * \retval #PSA_SUCCESS
3076 * Success.
3077 * \retval #PSA_ERROR_INVALID_HANDLE
3078 * \retval #PSA_ERROR_DOES_NOT_EXIST
3079 * \retval #PSA_ERROR_NOT_PERMITTED
3080 * \retval #PSA_ERROR_INVALID_ARGUMENT
3081 * \c step is not compatible with the operation's algorithm.
3082 * \retval #PSA_ERROR_INVALID_ARGUMENT
3083 * \c step does not allow key inputs.
3084 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3085 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3086 * \retval #PSA_ERROR_HARDWARE_FAILURE
3087 * \retval #PSA_ERROR_TAMPERING_DETECTED
3088 * \retval #PSA_ERROR_BAD_STATE
3089 * The value of \p step is not valid given the state of \p operation.
3090 * \retval #PSA_ERROR_BAD_STATE
3091 * The library has not been previously initialized by psa_crypto_init().
3092 * It is implementation-dependent whether a failure to initialize
3093 * results in this error code.
3094 */
3095psa_status_t psa_key_derivation_input_key(
3096 psa_key_derivation_operation_t *operation,
3097 psa_key_derivation_step_t step,
3098 psa_key_handle_t handle);
3099
3100/** Perform a key agreement and use the shared secret as input to a key
3101 * derivation.
3102 *
3103 * A key agreement algorithm takes two inputs: a private key \p private_key
3104 * a public key \p peer_key.
3105 * The result of this function is passed as input to a key derivation.
3106 * The output of this key derivation can be extracted by reading from the
3107 * resulting operation to produce keys and other cryptographic material.
3108 *
3109 * \param[in,out] operation The key derivation operation object to use.
3110 * It must have been set up with
3111 * psa_key_derivation_setup() with a
3112 * key agreement and derivation algorithm
3113 * \c alg (\c PSA_ALG_XXX value such that
3114 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3115 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3116 * is false).
3117 * The operation must be ready for an
3118 * input of the type given by \p step.
3119 * \param step Which step the input data is for.
3120 * \param private_key Handle to the private key to use.
3121 * \param[in] peer_key Public key of the peer. The peer key must be in the
3122 * same format that psa_import_key() accepts for the
3123 * public key type corresponding to the type of
3124 * private_key. That is, this function performs the
3125 * equivalent of
3126 * #psa_import_key(...,
3127 * `peer_key`, `peer_key_length`) where
3128 * with key attributes indicating the public key
3129 * type corresponding to the type of `private_key`.
3130 * For example, for EC keys, this means that peer_key
3131 * is interpreted as a point on the curve that the
3132 * private key is on. The standard formats for public
3133 * keys are documented in the documentation of
3134 * psa_export_public_key().
3135 * \param peer_key_length Size of \p peer_key in bytes.
3136 *
3137 * \retval #PSA_SUCCESS
3138 * Success.
3139 * \retval #PSA_ERROR_INVALID_HANDLE
3140 * \retval #PSA_ERROR_DOES_NOT_EXIST
3141 * \retval #PSA_ERROR_NOT_PERMITTED
3142 * \retval #PSA_ERROR_INVALID_ARGUMENT
3143 * \c private_key is not compatible with \c alg,
3144 * or \p peer_key is not valid for \c alg or not compatible with
3145 * \c private_key.
3146 * \retval #PSA_ERROR_NOT_SUPPORTED
3147 * \c alg is not supported or is not a key derivation algorithm.
3148 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3149 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3150 * \retval #PSA_ERROR_HARDWARE_FAILURE
3151 * \retval #PSA_ERROR_TAMPERING_DETECTED
3152 */
3153psa_status_t psa_key_derivation_key_agreement(
3154 psa_key_derivation_operation_t *operation,
3155 psa_key_derivation_step_t step,
3156 psa_key_handle_t private_key,
3157 const uint8_t *peer_key,
3158 size_t peer_key_length);
3159
Gilles Peskine35675b62019-05-16 17:26:11 +02003160/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003161 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003162 * This function calculates output bytes from a key derivation algorithm and
3163 * return those bytes.
3164 * If you view the key derivation's output as a stream of bytes, this
3165 * function destructively reads the requested number of bytes from the
3166 * stream.
3167 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003168 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003169 * \param[in,out] operation The key derivation operation object to read from.
3170 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003171 * \param output_length Number of bytes to output.
3172 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003173 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003174 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003175 * The operation's capacity was less than
3176 * \p output_length bytes. Note that in this case,
3177 * no output is written to the output buffer.
3178 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003179 * subsequent calls to this function will not
3180 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003181 * \retval #PSA_ERROR_BAD_STATE
3182 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3183 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3184 * \retval #PSA_ERROR_HARDWARE_FAILURE
3185 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003186 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003187psa_status_t psa_key_derivation_output_bytes(
3188 psa_key_derivation_operation_t *operation,
3189 uint8_t *output,
3190 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003191
Gilles Peskine35675b62019-05-16 17:26:11 +02003192/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003193 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003194 * This function calculates output bytes from a key derivation algorithm
3195 * and uses those bytes to generate a key deterministically.
3196 * If you view the key derivation's output as a stream of bytes, this
3197 * function destructively reads as many bytes as required from the
3198 * stream.
3199 * The operation's capacity decreases by the number of bytes read.
3200 *
3201 * How much output is produced and consumed from the operation, and how
3202 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003203 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003204 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003205 * of a given size, this function is functionally equivalent to
3206 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003207 * and passing the resulting output to #psa_import_key.
3208 * However, this function has a security benefit:
3209 * if the implementation provides an isolation boundary then
3210 * the key material is not exposed outside the isolation boundary.
3211 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003212 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003213 * The following key types defined in this specification follow this scheme:
3214 *
3215 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003216 * - #PSA_KEY_TYPE_ARC4;
3217 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003218 * - #PSA_KEY_TYPE_DERIVE;
3219 * - #PSA_KEY_TYPE_HMAC.
3220 *
3221 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003222 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003223 * Montgomery curve), this function always draws a byte string whose
3224 * length is determined by the curve, and sets the mandatory bits
3225 * accordingly. That is:
3226 *
3227 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3228 * and process it as specified in RFC 7748 &sect;5.
3229 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3230 * and process it as specified in RFC 7748 &sect;5.
3231 *
3232 * - For key types for which the key is represented by a single sequence of
3233 * \p bits bits with constraints as to which bit sequences are acceptable,
3234 * this function draws a byte string of length (\p bits / 8) bytes rounded
3235 * up to the nearest whole number of bytes. If the resulting byte string
3236 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3237 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003238 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003239 * for the output produced by psa_export_key().
3240 * The following key types defined in this specification follow this scheme:
3241 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003242 * - #PSA_KEY_TYPE_DES.
3243 * Force-set the parity bits, but discard forbidden weak keys.
3244 * For 2-key and 3-key triple-DES, the three keys are generated
3245 * successively (for example, for 3-key triple-DES,
3246 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3247 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003248 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003249 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003250 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003251 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003252 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003253 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003254 * Weierstrass curve).
3255 * For these key types, interpret the byte string as integer
3256 * in big-endian order. Discard it if it is not in the range
3257 * [0, *N* - 2] where *N* is the boundary of the private key domain
3258 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003259 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003260 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003261 * This method allows compliance to NIST standards, specifically
3262 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003263 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3264 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3265 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3266 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003267 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003268 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003269 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003270 * implementation-defined.
3271 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003272 * In all cases, the data that is read is discarded from the operation.
3273 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003274 *
Gilles Peskine20628592019-04-19 19:29:50 +02003275 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003276 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003277 * \param[out] handle On success, a handle to the newly created key.
3278 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003279 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003280 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003281 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003282 * If the key is persistent, the key material and the key's metadata
3283 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003284 * \retval #PSA_ERROR_ALREADY_EXISTS
3285 * This is an attempt to create a persistent key, and there is
3286 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003287 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003288 * There was not enough data to create the desired key.
3289 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003290 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003291 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003292 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003293 * The key type or key size is not supported, either by the
3294 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003295 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003296 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3297 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3298 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3299 * \retval #PSA_ERROR_HARDWARE_FAILURE
3300 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003301 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003302 * The library has not been previously initialized by psa_crypto_init().
3303 * It is implementation-dependent whether a failure to initialize
3304 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003305 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003306psa_status_t psa_key_derivation_output_key(
3307 const psa_key_attributes_t *attributes,
3308 psa_key_derivation_operation_t *operation,
3309 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003310
Gilles Peskine35675b62019-05-16 17:26:11 +02003311/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003312 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003313 * Once a key derivation operation has been aborted, its capacity is zero.
3314 * Aborting an operation frees all associated resources except for the
3315 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003316 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003317 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003318 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003319 * psa_key_derivation_operation_init() or a zero value. In particular,
3320 * it is valid to call psa_key_derivation_abort() twice, or to call
3321 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003322 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003323 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003324 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003325 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003326 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003327 * \retval #PSA_SUCCESS
3328 * \retval #PSA_ERROR_BAD_STATE
3329 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3330 * \retval #PSA_ERROR_HARDWARE_FAILURE
3331 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003332 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003333psa_status_t psa_key_derivation_abort(
3334 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003335
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003336/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003337 *
3338 * \warning The raw result of a key agreement algorithm such as finite-field
3339 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3340 * not be used directly as key material. It should instead be passed as
3341 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003342 * a key derivation, use psa_key_derivation_key_agreement() and other
3343 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003344 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003345 * \param alg The key agreement algorithm to compute
3346 * (\c PSA_ALG_XXX value such that
3347 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3348 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003349 * \param private_key Handle to the private key to use.
3350 * \param[in] peer_key Public key of the peer. It must be
3351 * in the same format that psa_import_key()
3352 * accepts. The standard formats for public
3353 * keys are documented in the documentation
3354 * of psa_export_public_key().
3355 * \param peer_key_length Size of \p peer_key in bytes.
3356 * \param[out] output Buffer where the decrypted message is to
3357 * be written.
3358 * \param output_size Size of the \c output buffer in bytes.
3359 * \param[out] output_length On success, the number of bytes
3360 * that make up the returned output.
3361 *
3362 * \retval #PSA_SUCCESS
3363 * Success.
3364 * \retval #PSA_ERROR_INVALID_HANDLE
3365 * \retval #PSA_ERROR_EMPTY_SLOT
3366 * \retval #PSA_ERROR_NOT_PERMITTED
3367 * \retval #PSA_ERROR_INVALID_ARGUMENT
3368 * \p alg is not a key agreement algorithm
3369 * \retval #PSA_ERROR_INVALID_ARGUMENT
3370 * \p private_key is not compatible with \p alg,
3371 * or \p peer_key is not valid for \p alg or not compatible with
3372 * \p private_key.
3373 * \retval #PSA_ERROR_NOT_SUPPORTED
3374 * \p alg is not a supported key agreement algorithm.
3375 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3376 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3377 * \retval #PSA_ERROR_HARDWARE_FAILURE
3378 * \retval #PSA_ERROR_TAMPERING_DETECTED
3379 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003380psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3381 psa_key_handle_t private_key,
3382 const uint8_t *peer_key,
3383 size_t peer_key_length,
3384 uint8_t *output,
3385 size_t output_size,
3386 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003387
Gilles Peskineea0fb492018-07-12 17:17:20 +02003388/**@}*/
3389
Gilles Peskineedd76872018-07-20 17:42:05 +02003390/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003391 * @{
3392 */
3393
3394/**
3395 * \brief Generate random bytes.
3396 *
3397 * \warning This function **can** fail! Callers MUST check the return status
3398 * and MUST NOT use the content of the output buffer if the return
3399 * status is not #PSA_SUCCESS.
3400 *
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003401 * \note To generate a key, use psa_generate_random_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003402 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003403 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003404 * \param output_size Number of bytes to generate and output.
3405 *
Gilles Peskine28538492018-07-11 17:34:00 +02003406 * \retval #PSA_SUCCESS
3407 * \retval #PSA_ERROR_NOT_SUPPORTED
3408 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3409 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3410 * \retval #PSA_ERROR_HARDWARE_FAILURE
3411 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003412 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003413 * The library has not been previously initialized by psa_crypto_init().
3414 * It is implementation-dependent whether a failure to initialize
3415 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003416 */
3417psa_status_t psa_generate_random(uint8_t *output,
3418 size_t output_size);
3419
3420/**
3421 * \brief Generate a key or key pair.
3422 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003423 * The key is generated randomly.
3424 * Its location, policy, type and size are taken from \p attributes.
3425 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003426 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003427 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003428 * the public exponent is 65537.
3429 * The modulus is a product of two probabilistic primes
3430 * between 2^{n-1} and 2^n where n is the bit size specified in the
3431 * attributes.
3432 *
Gilles Peskine20628592019-04-19 19:29:50 +02003433 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003434 * \param[out] handle On success, a handle to the newly created key.
3435 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003436 *
Gilles Peskine28538492018-07-11 17:34:00 +02003437 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003438 * Success.
3439 * If the key is persistent, the key material and the key's metadata
3440 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003441 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003442 * This is an attempt to create a persistent key, and there is
3443 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003444 * \retval #PSA_ERROR_NOT_SUPPORTED
3445 * \retval #PSA_ERROR_INVALID_ARGUMENT
3446 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3447 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3448 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3449 * \retval #PSA_ERROR_HARDWARE_FAILURE
3450 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003451 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003452 * The library has not been previously initialized by psa_crypto_init().
3453 * It is implementation-dependent whether a failure to initialize
3454 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003455 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003456psa_status_t psa_generate_random_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003457 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003458
3459/**@}*/
3460
Gilles Peskinee59236f2018-01-27 23:32:46 +01003461#ifdef __cplusplus
3462}
3463#endif
3464
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003465/* The file "crypto_sizes.h" contains definitions for size calculation
3466 * macros whose definitions are implementation-specific. */
3467#include "crypto_sizes.h"
3468
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003469/* The file "crypto_struct.h" contains definitions for
3470 * implementation-specific structs that are declared above. */
3471#include "crypto_struct.h"
3472
3473/* The file "crypto_extra.h" contains vendor-specific definitions. This
3474 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003475#include "crypto_extra.h"
3476
3477#endif /* PSA_CRYPTO_H */