blob: 08bdb846876efdec2a39afc1f8036fd0202140a4 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
89 * \retval #PSA_ERROR_TAMPERING_DETECTED
90 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
Gilles Peskine87a5e562019-04-17 12:28:25 +020096/** \defgroup attributes Key attributes
97 * @{
98 */
99
100/** The type of a structure containing key attributes.
101 *
102 * This is an opaque structure that can represent the metadata of a key
Gilles Peskine9c640f92019-04-28 11:36:21 +0200103 * object. Metadata that can be stored in attributes includes:
104 * - The location of the key in storage, indicated by its key identifier
105 * and its lifetime.
106 * - The key's policy, comprising usage flags and a specification of
107 * the permitted algorithm(s).
Gilles Peskine24f10f82019-05-16 12:18:32 +0200108 * - Information about the key itself: the key type and its size.
Gilles Peskine9c640f92019-04-28 11:36:21 +0200109 * - Implementations may define additional attributes.
Gilles Peskine87a5e562019-04-17 12:28:25 +0200110 *
111 * The actual key material is not considered an attribute of a key.
112 * Key attributes do not contain information that is generally considered
113 * highly confidential.
Gilles Peskine20628592019-04-19 19:29:50 +0200114 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200115 * An attribute structure can be a simple data structure where each function
116 * `psa_set_key_xxx` sets a field and the corresponding function
117 * `psa_get_key_xxx` retrieves the value of the corresponding field.
118 * However, implementations may report values that are equivalent to the
119 * original one, but have a different encoding. For example, an
120 * implementation may use a more compact representation for types where
121 * many bit-patterns are invalid or not supported, and store all values
122 * that it does not support as a special marker value. In such an
123 * implementation, after setting an invalid value, the corresponding
124 * get function returns an invalid value which may not be the one that
125 * was originally stored.
126 *
127 * An attribute structure may contain references to auxiliary resources,
128 * for example pointers to allocated memory or indirect references to
129 * pre-calculated values. In order to free such resources, the application
130 * must call psa_reset_key_attributes(). As an exception, calling
131 * psa_reset_key_attributes() on an attribute structure is optional if
132 * the structure has only been modified by the following functions
133 * since it was initialized or last reset with psa_reset_key_attributes():
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200134 * - psa_set_key_id()
135 * - psa_set_key_lifetime()
Gilles Peskine9c640f92019-04-28 11:36:21 +0200136 * - psa_set_key_type()
137 * - psa_set_key_bits()
138 * - psa_set_key_usage_flags()
139 * - psa_set_key_algorithm()
140 *
Gilles Peskine20628592019-04-19 19:29:50 +0200141 * Before calling any function on a key attribute structure, the application
142 * must initialize it by any of the following means:
143 * - Set the structure to all-bits-zero, for example:
144 * \code
145 * psa_key_attributes_t attributes;
146 * memset(&attributes, 0, sizeof(attributes));
147 * \endcode
148 * - Initialize the structure to logical zero values, for example:
149 * \code
150 * psa_key_attributes_t attributes = {0};
151 * \endcode
152 * - Initialize the structure to the initializer #PSA_KEY_ATTRIBUTES_INIT,
153 * for example:
154 * \code
155 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
156 * \endcode
157 * - Assign the result of the function psa_key_attributes_init()
158 * to the structure, for example:
159 * \code
160 * psa_key_attributes_t attributes;
161 * attributes = psa_key_attributes_init();
162 * \endcode
163 *
164 * A freshly initialized attribute structure contains the following
165 * values:
166 *
167 * - lifetime: #PSA_KEY_LIFETIME_VOLATILE.
168 * - key identifier: unspecified.
Gilles Peskine24f10f82019-05-16 12:18:32 +0200169 * - type: \c 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200170 * - key size: \c 0.
171 * - usage flags: \c 0.
172 * - algorithm: \c 0.
173 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200174 * A typical sequence to create a key is as follows:
175 * -# Create and initialize an attribute structure.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200176 * -# If the key is persistent, call psa_set_key_id().
177 * Also call psa_set_key_lifetime() to place the key in a non-default
178 * location.
Gilles Peskine9c640f92019-04-28 11:36:21 +0200179 * -# Set the key policy with psa_set_key_usage_flags() and
180 * psa_set_key_algorithm().
Gilles Peskine24f10f82019-05-16 12:18:32 +0200181 * -# Set the key type with psa_set_key_type().
Gilles Peskine9c640f92019-04-28 11:36:21 +0200182 * Skip this step if copying an existing key with psa_copy_key().
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100183 * -# When generating a random key with psa_generate_random_key() or deriving a key
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200184 * with psa_key_derivation_output_key(), set the desired key size with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200185 * psa_set_key_bits().
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100186 * -# Call a key creation function: psa_import_key(), psa_generate_random_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200187 * psa_key_derivation_output_key() or psa_copy_key(). This function reads
Gilles Peskine1ea5e442019-05-02 20:31:10 +0200188 * the attribute structure, creates a key with these attributes, and
189 * outputs a handle to the newly created key.
Gilles Peskine24f10f82019-05-16 12:18:32 +0200190 * -# The attribute structure is now no longer necessary.
191 * You may call psa_reset_key_attributes(), although this is optional
192 * with the workflow presented here because the attributes currently
193 * defined in this specification do not require any additional resources
194 * beyond the structure itself.
Gilles Peskine20628592019-04-19 19:29:50 +0200195 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200196 * A typical sequence to query a key's attributes is as follows:
197 * -# Call psa_get_key_attributes().
198 * -# Call `psa_get_key_xxx` functions to retrieve the attribute(s) that
199 * you are interested in.
200 * -# Call psa_reset_key_attributes() to free any resources that may be
201 * used by the attribute structure.
202 *
203 * Once a key has been created, it is impossible to change its attributes.
Gilles Peskine87a5e562019-04-17 12:28:25 +0200204 */
205typedef struct psa_key_attributes_s psa_key_attributes_t;
206
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200207/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200208 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200209 * If the attribute structure currently declares the key as volatile (which
210 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200211 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200212 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200213 * This function does not access storage, it merely stores the given
214 * value in the structure.
215 * The persistent key will be written to storage when the attribute
216 * structure is passed to a key creation function such as
217 * psa_import_key(), psa_generate_random_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200218 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200219 *
Gilles Peskine20628592019-04-19 19:29:50 +0200220 * This function may be declared as `static` (i.e. without external
221 * linkage). This function may be provided as a function-like macro,
222 * but in this case it must evaluate each of its arguments exactly once.
223 *
224 * \param[out] attributes The attribute structure to write to.
225 * \param id The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200226 */
227static void psa_set_key_id(psa_key_attributes_t *attributes,
228 psa_key_id_t id);
229
230/** Set the location of a persistent key.
231 *
232 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200233 * with psa_set_key_id(). By default, a key that has a persistent identifier
234 * is stored in the default storage area identifier by
235 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
236 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200237 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200238 * This function does not access storage, it merely stores the given
239 * value in the structure.
240 * The persistent key will be written to storage when the attribute
241 * structure is passed to a key creation function such as
242 * psa_import_key(), psa_generate_random_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200243 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200244 *
245 * This function may be declared as `static` (i.e. without external
246 * linkage). This function may be provided as a function-like macro,
247 * but in this case it must evaluate each of its arguments exactly once.
248 *
249 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200250 * \param lifetime The lifetime for the key.
251 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200252 * key will be volatile, and the key identifier
253 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200254 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200255static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
256 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200257
Gilles Peskine20628592019-04-19 19:29:50 +0200258/** Retrieve the key identifier from key attributes.
259 *
260 * This function may be declared as `static` (i.e. without external
261 * linkage). This function may be provided as a function-like macro,
262 * but in this case it must evaluate its argument exactly once.
263 *
264 * \param[in] attributes The key attribute structure to query.
265 *
266 * \return The persistent identifier stored in the attribute structure.
267 * This value is unspecified if the attribute structure declares
268 * the key as volatile.
269 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200270static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
271
Gilles Peskine20628592019-04-19 19:29:50 +0200272/** Retrieve the lifetime from key attributes.
273 *
274 * This function may be declared as `static` (i.e. without external
275 * linkage). This function may be provided as a function-like macro,
276 * but in this case it must evaluate its argument exactly once.
277 *
278 * \param[in] attributes The key attribute structure to query.
279 *
280 * \return The lifetime value stored in the attribute structure.
281 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200282static psa_key_lifetime_t psa_get_key_lifetime(
283 const psa_key_attributes_t *attributes);
284
Gilles Peskine20628592019-04-19 19:29:50 +0200285/** Declare usage flags for a key.
286 *
287 * Usage flags are part of a key's usage policy. They encode what
288 * kind of operations are permitted on the key. For more details,
289 * refer to the documentation of the type #psa_key_usage_t.
290 *
291 * This function overwrites any usage flags
292 * previously set in \p attributes.
293 *
294 * This function may be declared as `static` (i.e. without external
295 * linkage). This function may be provided as a function-like macro,
296 * but in this case it must evaluate each of its arguments exactly once.
297 *
298 * \param[out] attributes The attribute structure to write to.
299 * \param usage_flags The usage flags to write.
300 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200301static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
302 psa_key_usage_t usage_flags);
303
Gilles Peskine20628592019-04-19 19:29:50 +0200304/** Retrieve the usage flags from key attributes.
305 *
306 * This function may be declared as `static` (i.e. without external
307 * linkage). This function may be provided as a function-like macro,
308 * but in this case it must evaluate its argument exactly once.
309 *
310 * \param[in] attributes The key attribute structure to query.
311 *
312 * \return The usage flags stored in the attribute structure.
313 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200314static psa_key_usage_t psa_get_key_usage_flags(
315 const psa_key_attributes_t *attributes);
316
Gilles Peskine20628592019-04-19 19:29:50 +0200317/** Declare the permitted algorithm policy for a key.
318 *
319 * The permitted algorithm policy of a key encodes which algorithm or
320 * algorithms are permitted to be used with this key.
321 *
322 * This function overwrites any algorithm policy
323 * previously set in \p attributes.
324 *
325 * This function may be declared as `static` (i.e. without external
326 * linkage). This function may be provided as a function-like macro,
327 * but in this case it must evaluate each of its arguments exactly once.
328 *
329 * \param[out] attributes The attribute structure to write to.
330 * \param alg The permitted algorithm policy to write.
331 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200332static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
333 psa_algorithm_t alg);
334
Gilles Peskine20628592019-04-19 19:29:50 +0200335/** Retrieve the algorithm policy from key attributes.
336 *
337 * This function may be declared as `static` (i.e. without external
338 * linkage). This function may be provided as a function-like macro,
339 * but in this case it must evaluate its argument exactly once.
340 *
341 * \param[in] attributes The key attribute structure to query.
342 *
343 * \return The algorithm stored in the attribute structure.
344 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200345static psa_algorithm_t psa_get_key_algorithm(
346 const psa_key_attributes_t *attributes);
347
Gilles Peskine20628592019-04-19 19:29:50 +0200348/** Declare the type of a key.
349 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200350 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200351 * previously set in \p attributes.
352 *
353 * This function may be declared as `static` (i.e. without external
354 * linkage). This function may be provided as a function-like macro,
355 * but in this case it must evaluate each of its arguments exactly once.
356 *
357 * \param[out] attributes The attribute structure to write to.
358 * \param type The key type to write.
359 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200360static void psa_set_key_type(psa_key_attributes_t *attributes,
361 psa_key_type_t type);
362
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200363/** Declare the size of a key.
364 *
365 * This function overwrites any key size previously set in \p attributes.
366 *
367 * This function may be declared as `static` (i.e. without external
368 * linkage). This function may be provided as a function-like macro,
369 * but in this case it must evaluate each of its arguments exactly once.
370 *
371 * \param[out] attributes The attribute structure to write to.
372 * \param bits The key size in bits.
373 */
374static void psa_set_key_bits(psa_key_attributes_t *attributes,
375 size_t bits);
376
Gilles Peskine20628592019-04-19 19:29:50 +0200377/** Retrieve the key type from key attributes.
378 *
379 * This function may be declared as `static` (i.e. without external
380 * linkage). This function may be provided as a function-like macro,
381 * but in this case it must evaluate its argument exactly once.
382 *
383 * \param[in] attributes The key attribute structure to query.
384 *
385 * \return The key type stored in the attribute structure.
386 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200387static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
388
Gilles Peskine20628592019-04-19 19:29:50 +0200389/** Retrieve the key size from key attributes.
390 *
391 * This function may be declared as `static` (i.e. without external
392 * linkage). This function may be provided as a function-like macro,
393 * but in this case it must evaluate its argument exactly once.
394 *
395 * \param[in] attributes The key attribute structure to query.
396 *
397 * \return The key size stored in the attribute structure, in bits.
398 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200399static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
400
Gilles Peskine20628592019-04-19 19:29:50 +0200401/** Retrieve the attributes of a key.
402 *
403 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200404 * psa_reset_key_attributes(). It then copies the attributes of
405 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200406 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200407 * \note This function may allocate memory or other resources.
408 * Once you have called this function on an attribute structure,
409 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200410 *
Gilles Peskine20628592019-04-19 19:29:50 +0200411 * \param[in] handle Handle to the key to query.
412 * \param[in,out] attributes On success, the attributes of the key.
413 * On failure, equivalent to a
414 * freshly-initialized structure.
415 *
416 * \retval #PSA_SUCCESS
417 * \retval #PSA_ERROR_INVALID_HANDLE
418 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
419 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
420 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200421psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
422 psa_key_attributes_t *attributes);
423
Gilles Peskine20628592019-04-19 19:29:50 +0200424/** Reset a key attribute structure to a freshly initialized state.
425 *
426 * You must initialize the attribute structure as described in the
427 * documentation of the type #psa_key_attributes_t before calling this
428 * function. Once the structure has been initialized, you may call this
429 * function at any time.
430 *
431 * This function frees any auxiliary resources that the structure
432 * may contain.
433 *
434 * \param[in,out] attributes The attribute structure to reset.
435 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200436void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200437
Gilles Peskine87a5e562019-04-17 12:28:25 +0200438/**@}*/
439
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100440/** \defgroup key_management Key management
441 * @{
442 */
443
Gilles Peskinef535eb22018-11-30 14:08:36 +0100444/** Open a handle to an existing persistent key.
445 *
446 * Open a handle to a key which was previously created with psa_create_key().
447 *
Gilles Peskine4a231b82019-05-06 18:56:14 +0200448 * Implementations may provide additional keys that can be opened with
449 * psa_open_key(). Such keys have a key identifier in the vendor range,
450 * as documented in the description of #psa_key_id_t.
451 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100452 * \param id The persistent identifier of the key.
453 * \param[out] handle On success, a handle to a key slot which contains
454 * the data and metadata loaded from the specified
455 * persistent location.
456 *
457 * \retval #PSA_SUCCESS
458 * Success. The application can now use the value of `*handle`
459 * to access the newly allocated key slot.
460 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200461 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100462 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine225010f2019-05-06 18:44:55 +0200463 * \p id is invalid.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100464 * \retval #PSA_ERROR_NOT_PERMITTED
465 * The specified key exists, but the application does not have the
466 * permission to access it. Note that this specification does not
467 * define any way to create such a key, but it may be possible
468 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200469 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
470 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100471 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200472psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100473 psa_key_handle_t *handle);
474
Gilles Peskinef535eb22018-11-30 14:08:36 +0100475/** Close a key handle.
476 *
477 * If the handle designates a volatile key, destroy the key material and
478 * free all associated resources, just like psa_destroy_key().
479 *
480 * If the handle designates a persistent key, free all resources associated
481 * with the key in volatile memory. The key slot in persistent storage is
482 * not affected and can be opened again later with psa_open_key().
483 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100484 * If the key is currently in use in a multipart operation,
485 * the multipart operation is aborted.
486 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100487 * \param handle The key handle to close.
488 *
489 * \retval #PSA_SUCCESS
490 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100491 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100492 */
493psa_status_t psa_close_key(psa_key_handle_t handle);
494
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100495/**@}*/
496
497/** \defgroup import_export Key import and export
498 * @{
499 */
500
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100501/**
502 * \brief Import a key in binary format.
503 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100504 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100505 * documentation of psa_export_public_key() for the format of public keys
506 * and to the documentation of psa_export_key() for the format for
507 * other key types.
508 *
509 * This specification supports a single format for each key type.
510 * Implementations may support other formats as long as the standard
511 * format is supported. Implementations that support other formats
512 * should ensure that the formats are clearly unambiguous so as to
513 * minimize the risk that an invalid input is accidentally interpreted
514 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100515 *
Gilles Peskine20628592019-04-19 19:29:50 +0200516 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200517 * The key size is always determined from the
518 * \p data buffer.
519 * If the key size in \p attributes is nonzero,
520 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200521 * \param[out] handle On success, a handle to the newly created key.
522 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100523 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200524 * buffer is interpreted according to the type declared
525 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200526 * All implementations must support at least the format
527 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100528 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200529 * the chosen type. Implementations may allow other
530 * formats, but should be conservative: implementations
531 * should err on the side of rejecting content if it
532 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200533 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100534 *
Gilles Peskine28538492018-07-11 17:34:00 +0200535 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100536 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100537 * If the key is persistent, the key material and the key's metadata
538 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200539 * \retval #PSA_ERROR_ALREADY_EXISTS
540 * This is an attempt to create a persistent key, and there is
541 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200542 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200543 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200544 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200545 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200546 * The key attributes, as a whole, are invalid.
547 * \retval #PSA_ERROR_INVALID_ARGUMENT
548 * The key data is not correctly formatted.
549 * \retval #PSA_ERROR_INVALID_ARGUMENT
550 * The size in \p attributes is nonzero and does not match the size
551 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200552 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
553 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
554 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100555 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200556 * \retval #PSA_ERROR_HARDWARE_FAILURE
557 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300558 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300559 * The library has not been previously initialized by psa_crypto_init().
560 * It is implementation-dependent whether a failure to initialize
561 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200563psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100564 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200565 size_t data_length,
566 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100567
568/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100569 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200570 *
571 * This function destroys the content of the key slot from both volatile
572 * memory and, if applicable, non-volatile storage. Implementations shall
573 * make a best effort to ensure that any previous content of the slot is
574 * unrecoverable.
575 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100576 * This function also erases any metadata such as policies and frees all
577 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200578 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100579 * If the key is currently in use in a multipart operation,
580 * the multipart operation is aborted.
581 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100582 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100583 *
Gilles Peskine28538492018-07-11 17:34:00 +0200584 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200585 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200586 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200587 * The slot holds content and cannot be erased because it is
588 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100589 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200590 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200591 * There was an failure in communication with the cryptoprocessor.
592 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200593 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200594 * The storage is corrupted. Implementations shall make a best effort
595 * to erase key material even in this stage, however applications
596 * should be aware that it may be impossible to guarantee that the
597 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200598 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200599 * An unexpected condition which is not a storage corruption or
600 * a communication failure occurred. The cryptoprocessor may have
601 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300602 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300603 * The library has not been previously initialized by psa_crypto_init().
604 * It is implementation-dependent whether a failure to initialize
605 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100606 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100607psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100608
609/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100610 * \brief Export a key in binary format.
611 *
612 * The output of this function can be passed to psa_import_key() to
613 * create an equivalent object.
614 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100615 * If the implementation of psa_import_key() supports other formats
616 * beyond the format specified here, the output from psa_export_key()
617 * must use the representation specified here, not the original
618 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100620 * For standard key types, the output format is as follows:
621 *
622 * - For symmetric keys (including MAC keys), the format is the
623 * raw bytes of the key.
624 * - For DES, the key data consists of 8 bytes. The parity bits must be
625 * correct.
626 * - For Triple-DES, the format is the concatenation of the
627 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100628 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200629 * is the non-encrypted DER encoding of the representation defined by
630 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
631 * ```
632 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200633 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200634 * modulus INTEGER, -- n
635 * publicExponent INTEGER, -- e
636 * privateExponent INTEGER, -- d
637 * prime1 INTEGER, -- p
638 * prime2 INTEGER, -- q
639 * exponent1 INTEGER, -- d mod (p-1)
640 * exponent2 INTEGER, -- d mod (q-1)
641 * coefficient INTEGER, -- (inverse of q) mod p
642 * }
643 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000644 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
645 * representation of the private key `x` as a big-endian byte string. The
646 * length of the byte string is the private key size in bytes (leading zeroes
647 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200648 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100649 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100650 * a representation of the private value as a `ceiling(m/8)`-byte string
651 * where `m` is the bit size associated with the curve, i.e. the bit size
652 * of the order of the curve's coordinate field. This byte string is
653 * in little-endian order for Montgomery curves (curve types
654 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
655 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
656 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100657 * This is the content of the `privateKey` field of the `ECPrivateKey`
658 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200659 * - For Diffie-Hellman key exchange key pairs (key types for which
660 * #PSA_KEY_TYPE_IS_DH_KEYPAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000661 * format is the representation of the private key `x` as a big-endian byte
662 * string. The length of the byte string is the private key size in bytes
663 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200664 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
665 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100666 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200667 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
668 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100669 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200670 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200671 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200672 * \param[out] data_length On success, the number of bytes
673 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100674 *
Gilles Peskine28538492018-07-11 17:34:00 +0200675 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100676 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200677 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200678 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200679 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100680 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200681 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
682 * The size of the \p data buffer is too small. You can determine a
683 * sufficient buffer size by calling
684 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
685 * where \c type is the key type
686 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200687 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
688 * \retval #PSA_ERROR_HARDWARE_FAILURE
689 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300690 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300691 * The library has not been previously initialized by psa_crypto_init().
692 * It is implementation-dependent whether a failure to initialize
693 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100694 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100695psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100696 uint8_t *data,
697 size_t data_size,
698 size_t *data_length);
699
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100700/**
701 * \brief Export a public key or the public part of a key pair in binary format.
702 *
703 * The output of this function can be passed to psa_import_key() to
704 * create an object that is equivalent to the public key.
705 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000706 * This specification supports a single format for each key type.
707 * Implementations may support other formats as long as the standard
708 * format is supported. Implementations that support other formats
709 * should ensure that the formats are clearly unambiguous so as to
710 * minimize the risk that an invalid input is accidentally interpreted
711 * according to a different format.
712 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000713 * For standard key types, the output format is as follows:
714 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
715 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
716 * ```
717 * RSAPublicKey ::= SEQUENCE {
718 * modulus INTEGER, -- n
719 * publicExponent INTEGER } -- e
720 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000721 * - For elliptic curve public keys (key types for which
722 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
723 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
724 * Let `m` be the bit size associated with the curve, i.e. the bit size of
725 * `q` for a curve over `F_q`. The representation consists of:
726 * - The byte 0x04;
727 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
728 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000729 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
730 * representation of the public key `y = g^x mod p` as a big-endian byte
731 * string. The length of the byte string is the length of the base prime `p`
732 * in bytes.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200733 * - For Diffie-Hellman key exchange public keys (key types for which
734 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000735 * the format is the representation of the public key `y = g^x mod p` as a
736 * big-endian byte string. The length of the byte string is the length of the
737 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100738 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200739 * Exporting a public key object or the public part of a key pair is
740 * always permitted, regardless of the key's usage flags.
741 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100742 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200743 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200744 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200745 * \param[out] data_length On success, the number of bytes
746 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100747 *
Gilles Peskine28538492018-07-11 17:34:00 +0200748 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100749 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200750 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200751 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200752 * The key is neither a public key nor a key pair.
753 * \retval #PSA_ERROR_NOT_SUPPORTED
754 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
755 * The size of the \p data buffer is too small. You can determine a
756 * sufficient buffer size by calling
757 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
758 * where \c type is the key type
759 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200760 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
761 * \retval #PSA_ERROR_HARDWARE_FAILURE
762 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300763 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300764 * The library has not been previously initialized by psa_crypto_init().
765 * It is implementation-dependent whether a failure to initialize
766 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100767 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100768psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100769 uint8_t *data,
770 size_t data_size,
771 size_t *data_length);
772
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100773/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100774 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100775 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000776 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100777 * This function is primarily useful to copy a key from one location
778 * to another, since it populates a key using the material from
779 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200780 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100781 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100782 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100783 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100784 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200785 * The policy on the source key must have the usage flag
786 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200787 * This flag is sufficient to permit the copy if the key has the lifetime
788 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
789 * Some secure elements do not provide a way to copy a key without
790 * making it extractable from the secure element. If a key is located
791 * in such a secure element, then the key must have both usage flags
792 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
793 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200794 *
Gilles Peskine20628592019-04-19 19:29:50 +0200795 * The resulting key may only be used in a way that conforms to
796 * both the policy of the original key and the policy specified in
797 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100798 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200799 * usage flags on the source policy and the usage flags in \p attributes.
800 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100801 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200802 * - If either of the policies allows an algorithm and the other policy
803 * allows a wildcard-based algorithm policy that includes this algorithm,
804 * the resulting key allows the same algorithm.
805 * - If the policies do not allow any algorithm in common, this function
806 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200807 *
Gilles Peskine20628592019-04-19 19:29:50 +0200808 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100809 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200810 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100811 * \param source_handle The key to copy. It must be a handle to an
812 * occupied slot.
Gilles Peskine20628592019-04-19 19:29:50 +0200813 * \param[in] attributes The attributes for the new key.
814 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200815 * - The key type and size may be 0. If either is
816 * nonzero, it must match the corresponding
817 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200818 * - The key location (the lifetime and, for
819 * persistent keys, the key identifier) is
820 * used directly.
821 * - The policy constraints (usage flags and
822 * algorithm policy) are combined from
823 * the source key and \p attributes so that
824 * both sets of restrictions apply, as
825 * described in the documentation of this function.
826 * \param[out] target_handle On success, a handle to the newly created key.
827 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200828 *
829 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100830 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200831 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200832 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200833 * This is an attempt to create a persistent key, and there is
834 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200835 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200836 * The lifetime or identifier in \p attributes are invalid.
837 * \retval #PSA_ERROR_INVALID_ARGUMENT
838 * The policy constraints on the source and specified in
839 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200840 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200841 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200842 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100843 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200844 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
845 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100846 * The source key is not exportable and its lifetime does not
847 * allow copying it to the target's lifetime.
848 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
849 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200850 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
851 * \retval #PSA_ERROR_HARDWARE_FAILURE
852 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100853 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100854psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200855 const psa_key_attributes_t *attributes,
856 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100857
858/**@}*/
859
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100860/** \defgroup hash Message digests
861 * @{
862 */
863
Gilles Peskine69647a42019-01-14 20:18:12 +0100864/** Calculate the hash (digest) of a message.
865 *
866 * \note To verify the hash of a message against an
867 * expected value, use psa_hash_compare() instead.
868 *
869 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
870 * such that #PSA_ALG_IS_HASH(\p alg) is true).
871 * \param[in] input Buffer containing the message to hash.
872 * \param input_length Size of the \p input buffer in bytes.
873 * \param[out] hash Buffer where the hash is to be written.
874 * \param hash_size Size of the \p hash buffer in bytes.
875 * \param[out] hash_length On success, the number of bytes
876 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100877 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100878 *
879 * \retval #PSA_SUCCESS
880 * Success.
881 * \retval #PSA_ERROR_NOT_SUPPORTED
882 * \p alg is not supported or is not a hash algorithm.
883 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
884 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
885 * \retval #PSA_ERROR_HARDWARE_FAILURE
886 * \retval #PSA_ERROR_TAMPERING_DETECTED
887 */
888psa_status_t psa_hash_compute(psa_algorithm_t alg,
889 const uint8_t *input,
890 size_t input_length,
891 uint8_t *hash,
892 size_t hash_size,
893 size_t *hash_length);
894
895/** Calculate the hash (digest) of a message and compare it with a
896 * reference value.
897 *
898 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
899 * such that #PSA_ALG_IS_HASH(\p alg) is true).
900 * \param[in] input Buffer containing the message to hash.
901 * \param input_length Size of the \p input buffer in bytes.
902 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100903 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100904 *
905 * \retval #PSA_SUCCESS
906 * The expected hash is identical to the actual hash of the input.
907 * \retval #PSA_ERROR_INVALID_SIGNATURE
908 * The hash of the message was calculated successfully, but it
909 * differs from the expected hash.
910 * \retval #PSA_ERROR_NOT_SUPPORTED
911 * \p alg is not supported or is not a hash algorithm.
912 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
913 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
914 * \retval #PSA_ERROR_HARDWARE_FAILURE
915 * \retval #PSA_ERROR_TAMPERING_DETECTED
916 */
917psa_status_t psa_hash_compare(psa_algorithm_t alg,
918 const uint8_t *input,
919 size_t input_length,
920 const uint8_t *hash,
921 const size_t hash_length);
922
Gilles Peskine308b91d2018-02-08 09:47:44 +0100923/** The type of the state data structure for multipart hash operations.
924 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000925 * Before calling any function on a hash operation object, the application must
926 * initialize it by any of the following means:
927 * - Set the structure to all-bits-zero, for example:
928 * \code
929 * psa_hash_operation_t operation;
930 * memset(&operation, 0, sizeof(operation));
931 * \endcode
932 * - Initialize the structure to logical zero values, for example:
933 * \code
934 * psa_hash_operation_t operation = {0};
935 * \endcode
936 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
937 * for example:
938 * \code
939 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
940 * \endcode
941 * - Assign the result of the function psa_hash_operation_init()
942 * to the structure, for example:
943 * \code
944 * psa_hash_operation_t operation;
945 * operation = psa_hash_operation_init();
946 * \endcode
947 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100948 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100949 * make any assumptions about the content of this structure except
950 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100951typedef struct psa_hash_operation_s psa_hash_operation_t;
952
Jaeden Amero6a25b412019-01-04 11:47:44 +0000953/** \def PSA_HASH_OPERATION_INIT
954 *
955 * This macro returns a suitable initializer for a hash operation object
956 * of type #psa_hash_operation_t.
957 */
958#ifdef __DOXYGEN_ONLY__
959/* This is an example definition for documentation purposes.
960 * Implementations should define a suitable value in `crypto_struct.h`.
961 */
962#define PSA_HASH_OPERATION_INIT {0}
963#endif
964
965/** Return an initial value for a hash operation object.
966 */
967static psa_hash_operation_t psa_hash_operation_init(void);
968
Gilles Peskinef45adda2019-01-14 18:29:18 +0100969/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100970 *
971 * The sequence of operations to calculate a hash (message digest)
972 * is as follows:
973 * -# Allocate an operation object which will be passed to all the functions
974 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000975 * -# Initialize the operation object with one of the methods described in the
976 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200977 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100978 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100979 * of the message each time. The hash that is calculated is the hash
980 * of the concatenation of these messages in order.
981 * -# To calculate the hash, call psa_hash_finish().
982 * To compare the hash with an expected value, call psa_hash_verify().
983 *
984 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000985 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100986 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200987 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100988 * eventually terminate the operation. The following events terminate an
989 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100990 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100991 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100992 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000993 * \param[in,out] operation The operation object to set up. It must have
994 * been initialized as per the documentation for
995 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200996 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
997 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100998 *
Gilles Peskine28538492018-07-11 17:34:00 +0200999 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001000 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001001 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001002 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001003 * \retval #PSA_ERROR_BAD_STATE
1004 * The operation state is not valid (already set up and not
1005 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001006 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1007 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1008 * \retval #PSA_ERROR_HARDWARE_FAILURE
1009 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001010 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001011psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001012 psa_algorithm_t alg);
1013
Gilles Peskine308b91d2018-02-08 09:47:44 +01001014/** Add a message fragment to a multipart hash operation.
1015 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001016 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001017 *
1018 * If this function returns an error status, the operation becomes inactive.
1019 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001020 * \param[in,out] operation Active hash operation.
1021 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001022 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001023 *
Gilles Peskine28538492018-07-11 17:34:00 +02001024 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001025 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001026 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001027 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001028 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1029 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1030 * \retval #PSA_ERROR_HARDWARE_FAILURE
1031 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001032 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001033psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1034 const uint8_t *input,
1035 size_t input_length);
1036
Gilles Peskine308b91d2018-02-08 09:47:44 +01001037/** Finish the calculation of the hash of a message.
1038 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001039 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001040 * This function calculates the hash of the message formed by concatenating
1041 * the inputs passed to preceding calls to psa_hash_update().
1042 *
1043 * When this function returns, the operation becomes inactive.
1044 *
1045 * \warning Applications should not call this function if they expect
1046 * a specific value for the hash. Call psa_hash_verify() instead.
1047 * Beware that comparing integrity or authenticity data such as
1048 * hash values with a function such as \c memcmp is risky
1049 * because the time taken by the comparison may leak information
1050 * about the hashed data which could allow an attacker to guess
1051 * a valid hash and thereby bypass security controls.
1052 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001053 * \param[in,out] operation Active hash operation.
1054 * \param[out] hash Buffer where the hash is to be written.
1055 * \param hash_size Size of the \p hash buffer in bytes.
1056 * \param[out] hash_length On success, the number of bytes
1057 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001058 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001059 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001060 *
Gilles Peskine28538492018-07-11 17:34:00 +02001061 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001062 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001063 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001064 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001065 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001066 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001067 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001068 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001069 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1070 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1071 * \retval #PSA_ERROR_HARDWARE_FAILURE
1072 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001073 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001074psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1075 uint8_t *hash,
1076 size_t hash_size,
1077 size_t *hash_length);
1078
Gilles Peskine308b91d2018-02-08 09:47:44 +01001079/** Finish the calculation of the hash of a message and compare it with
1080 * an expected value.
1081 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001082 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001083 * This function calculates the hash of the message formed by concatenating
1084 * the inputs passed to preceding calls to psa_hash_update(). It then
1085 * compares the calculated hash with the expected hash passed as a
1086 * parameter to this function.
1087 *
1088 * When this function returns, the operation becomes inactive.
1089 *
Gilles Peskine19067982018-03-20 17:54:53 +01001090 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001091 * comparison between the actual hash and the expected hash is performed
1092 * in constant time.
1093 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001094 * \param[in,out] operation Active hash operation.
1095 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001096 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001097 *
Gilles Peskine28538492018-07-11 17:34:00 +02001098 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001099 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001100 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001101 * The hash of the message was calculated successfully, but it
1102 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001103 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001104 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001105 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1106 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1107 * \retval #PSA_ERROR_HARDWARE_FAILURE
1108 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001109 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001110psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1111 const uint8_t *hash,
1112 size_t hash_length);
1113
Gilles Peskine308b91d2018-02-08 09:47:44 +01001114/** Abort a hash operation.
1115 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001116 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001117 * \p operation structure itself. Once aborted, the operation object
1118 * can be reused for another operation by calling
1119 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001120 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001121 * You may call this function any time after the operation object has
1122 * been initialized by any of the following methods:
1123 * - A call to psa_hash_setup(), whether it succeeds or not.
1124 * - Initializing the \c struct to all-bits-zero.
1125 * - Initializing the \c struct to logical zeros, e.g.
1126 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001127 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001128 * In particular, calling psa_hash_abort() after the operation has been
1129 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1130 * psa_hash_verify() is safe and has no effect.
1131 *
1132 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001133 *
Gilles Peskine28538492018-07-11 17:34:00 +02001134 * \retval #PSA_SUCCESS
1135 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001136 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001137 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1138 * \retval #PSA_ERROR_HARDWARE_FAILURE
1139 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001140 */
1141psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001142
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001143/** Clone a hash operation.
1144 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001145 * This function copies the state of an ongoing hash operation to
1146 * a new operation object. In other words, this function is equivalent
1147 * to calling psa_hash_setup() on \p target_operation with the same
1148 * algorithm that \p source_operation was set up for, then
1149 * psa_hash_update() on \p target_operation with the same input that
1150 * that was passed to \p source_operation. After this function returns, the
1151 * two objects are independent, i.e. subsequent calls involving one of
1152 * the objects do not affect the other object.
1153 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001154 * \param[in] source_operation The active hash operation to clone.
1155 * \param[in,out] target_operation The operation object to set up.
1156 * It must be initialized but not active.
1157 *
1158 * \retval #PSA_SUCCESS
1159 * \retval #PSA_ERROR_BAD_STATE
1160 * \p source_operation is not an active hash operation.
1161 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001162 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001163 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1164 * \retval #PSA_ERROR_HARDWARE_FAILURE
1165 * \retval #PSA_ERROR_TAMPERING_DETECTED
1166 */
1167psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1168 psa_hash_operation_t *target_operation);
1169
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001170/**@}*/
1171
Gilles Peskine8c9def32018-02-08 10:02:12 +01001172/** \defgroup MAC Message authentication codes
1173 * @{
1174 */
1175
Gilles Peskine69647a42019-01-14 20:18:12 +01001176/** Calculate the MAC (message authentication code) of a message.
1177 *
1178 * \note To verify the MAC of a message against an
1179 * expected value, use psa_mac_verify() instead.
1180 * Beware that comparing integrity or authenticity data such as
1181 * MAC values with a function such as \c memcmp is risky
1182 * because the time taken by the comparison may leak information
1183 * about the MAC value which could allow an attacker to guess
1184 * a valid MAC and thereby bypass security controls.
1185 *
1186 * \param handle Handle to the key to use for the operation.
1187 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001188 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001189 * \param[in] input Buffer containing the input message.
1190 * \param input_length Size of the \p input buffer in bytes.
1191 * \param[out] mac Buffer where the MAC value is to be written.
1192 * \param mac_size Size of the \p mac buffer in bytes.
1193 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001194 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001195 *
1196 * \retval #PSA_SUCCESS
1197 * Success.
1198 * \retval #PSA_ERROR_INVALID_HANDLE
1199 * \retval #PSA_ERROR_EMPTY_SLOT
1200 * \retval #PSA_ERROR_NOT_PERMITTED
1201 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001202 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001203 * \retval #PSA_ERROR_NOT_SUPPORTED
1204 * \p alg is not supported or is not a MAC algorithm.
1205 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1206 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1207 * \retval #PSA_ERROR_HARDWARE_FAILURE
1208 * \retval #PSA_ERROR_TAMPERING_DETECTED
1209 * \retval #PSA_ERROR_BAD_STATE
1210 * The library has not been previously initialized by psa_crypto_init().
1211 * It is implementation-dependent whether a failure to initialize
1212 * results in this error code.
1213 */
1214psa_status_t psa_mac_compute(psa_key_handle_t handle,
1215 psa_algorithm_t alg,
1216 const uint8_t *input,
1217 size_t input_length,
1218 uint8_t *mac,
1219 size_t mac_size,
1220 size_t *mac_length);
1221
1222/** Calculate the MAC of a message and compare it with a reference value.
1223 *
1224 * \param handle Handle to the key to use for the operation.
1225 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001226 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001227 * \param[in] input Buffer containing the input message.
1228 * \param input_length Size of the \p input buffer in bytes.
1229 * \param[out] mac Buffer containing the expected MAC value.
1230 * \param mac_length Size of the \p mac buffer in bytes.
1231 *
1232 * \retval #PSA_SUCCESS
1233 * The expected MAC is identical to the actual MAC of the input.
1234 * \retval #PSA_ERROR_INVALID_SIGNATURE
1235 * The MAC of the message was calculated successfully, but it
1236 * differs from the expected value.
1237 * \retval #PSA_ERROR_INVALID_HANDLE
1238 * \retval #PSA_ERROR_EMPTY_SLOT
1239 * \retval #PSA_ERROR_NOT_PERMITTED
1240 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001241 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001242 * \retval #PSA_ERROR_NOT_SUPPORTED
1243 * \p alg is not supported or is not a MAC algorithm.
1244 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1245 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1246 * \retval #PSA_ERROR_HARDWARE_FAILURE
1247 * \retval #PSA_ERROR_TAMPERING_DETECTED
1248 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001249psa_status_t psa_mac_verify(psa_key_handle_t handle,
1250 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001251 const uint8_t *input,
1252 size_t input_length,
1253 const uint8_t *mac,
1254 const size_t mac_length);
1255
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001256/** The type of the state data structure for multipart MAC operations.
1257 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001258 * Before calling any function on a MAC operation object, the application must
1259 * initialize it by any of the following means:
1260 * - Set the structure to all-bits-zero, for example:
1261 * \code
1262 * psa_mac_operation_t operation;
1263 * memset(&operation, 0, sizeof(operation));
1264 * \endcode
1265 * - Initialize the structure to logical zero values, for example:
1266 * \code
1267 * psa_mac_operation_t operation = {0};
1268 * \endcode
1269 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1270 * for example:
1271 * \code
1272 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1273 * \endcode
1274 * - Assign the result of the function psa_mac_operation_init()
1275 * to the structure, for example:
1276 * \code
1277 * psa_mac_operation_t operation;
1278 * operation = psa_mac_operation_init();
1279 * \endcode
1280 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001281 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001282 * make any assumptions about the content of this structure except
1283 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001284typedef struct psa_mac_operation_s psa_mac_operation_t;
1285
Jaeden Amero769ce272019-01-04 11:48:03 +00001286/** \def PSA_MAC_OPERATION_INIT
1287 *
1288 * This macro returns a suitable initializer for a MAC operation object of type
1289 * #psa_mac_operation_t.
1290 */
1291#ifdef __DOXYGEN_ONLY__
1292/* This is an example definition for documentation purposes.
1293 * Implementations should define a suitable value in `crypto_struct.h`.
1294 */
1295#define PSA_MAC_OPERATION_INIT {0}
1296#endif
1297
1298/** Return an initial value for a MAC operation object.
1299 */
1300static psa_mac_operation_t psa_mac_operation_init(void);
1301
Gilles Peskinef45adda2019-01-14 18:29:18 +01001302/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001303 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001304 * This function sets up the calculation of the MAC
1305 * (message authentication code) of a byte string.
1306 * To verify the MAC of a message against an
1307 * expected value, use psa_mac_verify_setup() instead.
1308 *
1309 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001310 * -# Allocate an operation object which will be passed to all the functions
1311 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001312 * -# Initialize the operation object with one of the methods described in the
1313 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001314 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001315 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1316 * of the message each time. The MAC that is calculated is the MAC
1317 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001318 * -# At the end of the message, call psa_mac_sign_finish() to finish
1319 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001320 *
1321 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001322 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001323 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001324 * After a successful call to psa_mac_sign_setup(), the application must
1325 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001326 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001327 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001328 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001329 * \param[in,out] operation The operation object to set up. It must have
1330 * been initialized as per the documentation for
1331 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001332 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001333 * It must remain valid until the operation
1334 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001335 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001336 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001337 *
Gilles Peskine28538492018-07-11 17:34:00 +02001338 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001339 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001340 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001341 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001342 * \retval #PSA_ERROR_NOT_PERMITTED
1343 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001344 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001345 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001346 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001347 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1348 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1349 * \retval #PSA_ERROR_HARDWARE_FAILURE
1350 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001351 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001352 * The operation state is not valid (already set up and not
1353 * subsequently completed).
1354 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001355 * The library has not been previously initialized by psa_crypto_init().
1356 * It is implementation-dependent whether a failure to initialize
1357 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001358 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001359psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001360 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001361 psa_algorithm_t alg);
1362
Gilles Peskinef45adda2019-01-14 18:29:18 +01001363/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001364 *
1365 * This function sets up the verification of the MAC
1366 * (message authentication code) of a byte string against an expected value.
1367 *
1368 * The sequence of operations to verify a MAC is as follows:
1369 * -# Allocate an operation object which will be passed to all the functions
1370 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001371 * -# Initialize the operation object with one of the methods described in the
1372 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001373 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001374 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1375 * of the message each time. The MAC that is calculated is the MAC
1376 * of the concatenation of these messages in order.
1377 * -# At the end of the message, call psa_mac_verify_finish() to finish
1378 * calculating the actual MAC of the message and verify it against
1379 * the expected value.
1380 *
1381 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001382 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001383 *
1384 * After a successful call to psa_mac_verify_setup(), the application must
1385 * eventually terminate the operation through one of the following methods:
1386 * - A failed call to psa_mac_update().
1387 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1388 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001389 * \param[in,out] operation The operation object to set up. It must have
1390 * been initialized as per the documentation for
1391 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001392 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001393 * It must remain valid until the operation
1394 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001395 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1396 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001397 *
Gilles Peskine28538492018-07-11 17:34:00 +02001398 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001399 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001400 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001401 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001402 * \retval #PSA_ERROR_NOT_PERMITTED
1403 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001404 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001405 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001406 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001407 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1408 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1409 * \retval #PSA_ERROR_HARDWARE_FAILURE
1410 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001411 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001412 * The operation state is not valid (already set up and not
1413 * subsequently completed).
1414 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001415 * The library has not been previously initialized by psa_crypto_init().
1416 * It is implementation-dependent whether a failure to initialize
1417 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001418 */
1419psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001420 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001421 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001422
Gilles Peskinedcd14942018-07-12 00:30:52 +02001423/** Add a message fragment to a multipart MAC operation.
1424 *
1425 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1426 * before calling this function.
1427 *
1428 * If this function returns an error status, the operation becomes inactive.
1429 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001430 * \param[in,out] operation Active MAC operation.
1431 * \param[in] input Buffer containing the message fragment to add to
1432 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001433 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001434 *
1435 * \retval #PSA_SUCCESS
1436 * Success.
1437 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001438 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001439 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1440 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1441 * \retval #PSA_ERROR_HARDWARE_FAILURE
1442 * \retval #PSA_ERROR_TAMPERING_DETECTED
1443 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001444psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1445 const uint8_t *input,
1446 size_t input_length);
1447
Gilles Peskinedcd14942018-07-12 00:30:52 +02001448/** Finish the calculation of the MAC of a message.
1449 *
1450 * The application must call psa_mac_sign_setup() before calling this function.
1451 * This function calculates the MAC of the message formed by concatenating
1452 * the inputs passed to preceding calls to psa_mac_update().
1453 *
1454 * When this function returns, the operation becomes inactive.
1455 *
1456 * \warning Applications should not call this function if they expect
1457 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1458 * Beware that comparing integrity or authenticity data such as
1459 * MAC values with a function such as \c memcmp is risky
1460 * because the time taken by the comparison may leak information
1461 * about the MAC value which could allow an attacker to guess
1462 * a valid MAC and thereby bypass security controls.
1463 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001464 * \param[in,out] operation Active MAC operation.
1465 * \param[out] mac Buffer where the MAC value is to be written.
1466 * \param mac_size Size of the \p mac buffer in bytes.
1467 * \param[out] mac_length On success, the number of bytes
1468 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001469 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001470 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001471 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001472 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001473 *
1474 * \retval #PSA_SUCCESS
1475 * Success.
1476 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001477 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001478 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001479 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001480 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1481 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1482 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1483 * \retval #PSA_ERROR_HARDWARE_FAILURE
1484 * \retval #PSA_ERROR_TAMPERING_DETECTED
1485 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001486psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1487 uint8_t *mac,
1488 size_t mac_size,
1489 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001490
Gilles Peskinedcd14942018-07-12 00:30:52 +02001491/** Finish the calculation of the MAC of a message and compare it with
1492 * an expected value.
1493 *
1494 * The application must call psa_mac_verify_setup() before calling this function.
1495 * This function calculates the MAC of the message formed by concatenating
1496 * the inputs passed to preceding calls to psa_mac_update(). It then
1497 * compares the calculated MAC with the expected MAC passed as a
1498 * parameter to this function.
1499 *
1500 * When this function returns, the operation becomes inactive.
1501 *
1502 * \note Implementations shall make the best effort to ensure that the
1503 * comparison between the actual MAC and the expected MAC is performed
1504 * in constant time.
1505 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001506 * \param[in,out] operation Active MAC operation.
1507 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001508 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001509 *
1510 * \retval #PSA_SUCCESS
1511 * The expected MAC is identical to the actual MAC of the message.
1512 * \retval #PSA_ERROR_INVALID_SIGNATURE
1513 * The MAC of the message was calculated successfully, but it
1514 * differs from the expected MAC.
1515 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001516 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001517 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1518 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1519 * \retval #PSA_ERROR_HARDWARE_FAILURE
1520 * \retval #PSA_ERROR_TAMPERING_DETECTED
1521 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001522psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1523 const uint8_t *mac,
1524 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001525
Gilles Peskinedcd14942018-07-12 00:30:52 +02001526/** Abort a MAC operation.
1527 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001528 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001529 * \p operation structure itself. Once aborted, the operation object
1530 * can be reused for another operation by calling
1531 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001532 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001533 * You may call this function any time after the operation object has
1534 * been initialized by any of the following methods:
1535 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1536 * it succeeds or not.
1537 * - Initializing the \c struct to all-bits-zero.
1538 * - Initializing the \c struct to logical zeros, e.g.
1539 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001540 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001541 * In particular, calling psa_mac_abort() after the operation has been
1542 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1543 * psa_mac_verify_finish() is safe and has no effect.
1544 *
1545 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001546 *
1547 * \retval #PSA_SUCCESS
1548 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001549 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001550 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1551 * \retval #PSA_ERROR_HARDWARE_FAILURE
1552 * \retval #PSA_ERROR_TAMPERING_DETECTED
1553 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001554psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1555
1556/**@}*/
1557
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001558/** \defgroup cipher Symmetric ciphers
1559 * @{
1560 */
1561
Gilles Peskine69647a42019-01-14 20:18:12 +01001562/** Encrypt a message using a symmetric cipher.
1563 *
1564 * This function encrypts a message with a random IV (initialization
1565 * vector).
1566 *
1567 * \param handle Handle to the key to use for the operation.
1568 * It must remain valid until the operation
1569 * terminates.
1570 * \param alg The cipher algorithm to compute
1571 * (\c PSA_ALG_XXX value such that
1572 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1573 * \param[in] input Buffer containing the message to encrypt.
1574 * \param input_length Size of the \p input buffer in bytes.
1575 * \param[out] output Buffer where the output is to be written.
1576 * The output contains the IV followed by
1577 * the ciphertext proper.
1578 * \param output_size Size of the \p output buffer in bytes.
1579 * \param[out] output_length On success, the number of bytes
1580 * that make up the output.
1581 *
1582 * \retval #PSA_SUCCESS
1583 * Success.
1584 * \retval #PSA_ERROR_INVALID_HANDLE
1585 * \retval #PSA_ERROR_EMPTY_SLOT
1586 * \retval #PSA_ERROR_NOT_PERMITTED
1587 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001588 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001589 * \retval #PSA_ERROR_NOT_SUPPORTED
1590 * \p alg is not supported or is not a cipher algorithm.
1591 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1592 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1593 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1594 * \retval #PSA_ERROR_HARDWARE_FAILURE
1595 * \retval #PSA_ERROR_TAMPERING_DETECTED
1596 */
1597psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1598 psa_algorithm_t alg,
1599 const uint8_t *input,
1600 size_t input_length,
1601 uint8_t *output,
1602 size_t output_size,
1603 size_t *output_length);
1604
1605/** Decrypt a message using a symmetric cipher.
1606 *
1607 * This function decrypts a message encrypted with a symmetric cipher.
1608 *
1609 * \param handle Handle to the key to use for the operation.
1610 * It must remain valid until the operation
1611 * terminates.
1612 * \param alg The cipher algorithm to compute
1613 * (\c PSA_ALG_XXX value such that
1614 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1615 * \param[in] input Buffer containing the message to decrypt.
1616 * This consists of the IV followed by the
1617 * ciphertext proper.
1618 * \param input_length Size of the \p input buffer in bytes.
1619 * \param[out] output Buffer where the plaintext is to be written.
1620 * \param output_size Size of the \p output buffer in bytes.
1621 * \param[out] output_length On success, the number of bytes
1622 * that make up the output.
1623 *
1624 * \retval #PSA_SUCCESS
1625 * Success.
1626 * \retval #PSA_ERROR_INVALID_HANDLE
1627 * \retval #PSA_ERROR_EMPTY_SLOT
1628 * \retval #PSA_ERROR_NOT_PERMITTED
1629 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001630 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001631 * \retval #PSA_ERROR_NOT_SUPPORTED
1632 * \p alg is not supported or is not a cipher algorithm.
1633 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1634 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1635 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1636 * \retval #PSA_ERROR_HARDWARE_FAILURE
1637 * \retval #PSA_ERROR_TAMPERING_DETECTED
1638 */
1639psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1640 psa_algorithm_t alg,
1641 const uint8_t *input,
1642 size_t input_length,
1643 uint8_t *output,
1644 size_t output_size,
1645 size_t *output_length);
1646
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001647/** The type of the state data structure for multipart cipher operations.
1648 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001649 * Before calling any function on a cipher operation object, the application
1650 * must initialize it by any of the following means:
1651 * - Set the structure to all-bits-zero, for example:
1652 * \code
1653 * psa_cipher_operation_t operation;
1654 * memset(&operation, 0, sizeof(operation));
1655 * \endcode
1656 * - Initialize the structure to logical zero values, for example:
1657 * \code
1658 * psa_cipher_operation_t operation = {0};
1659 * \endcode
1660 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1661 * for example:
1662 * \code
1663 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1664 * \endcode
1665 * - Assign the result of the function psa_cipher_operation_init()
1666 * to the structure, for example:
1667 * \code
1668 * psa_cipher_operation_t operation;
1669 * operation = psa_cipher_operation_init();
1670 * \endcode
1671 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001672 * This is an implementation-defined \c struct. Applications should not
1673 * make any assumptions about the content of this structure except
1674 * as directed by the documentation of a specific implementation. */
1675typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1676
Jaeden Amero5bae2272019-01-04 11:48:27 +00001677/** \def PSA_CIPHER_OPERATION_INIT
1678 *
1679 * This macro returns a suitable initializer for a cipher operation object of
1680 * type #psa_cipher_operation_t.
1681 */
1682#ifdef __DOXYGEN_ONLY__
1683/* This is an example definition for documentation purposes.
1684 * Implementations should define a suitable value in `crypto_struct.h`.
1685 */
1686#define PSA_CIPHER_OPERATION_INIT {0}
1687#endif
1688
1689/** Return an initial value for a cipher operation object.
1690 */
1691static psa_cipher_operation_t psa_cipher_operation_init(void);
1692
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001693/** Set the key for a multipart symmetric encryption operation.
1694 *
1695 * The sequence of operations to encrypt a message with a symmetric cipher
1696 * is as follows:
1697 * -# Allocate an operation object which will be passed to all the functions
1698 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001699 * -# Initialize the operation object with one of the methods described in the
1700 * documentation for #psa_cipher_operation_t, e.g.
1701 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001702 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001703 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001704 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001705 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001706 * requires a specific IV value.
1707 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1708 * of the message each time.
1709 * -# Call psa_cipher_finish().
1710 *
1711 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001712 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001713 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001714 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001715 * eventually terminate the operation. The following events terminate an
1716 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001717 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001718 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001719 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001720 * \param[in,out] operation The operation object to set up. It must have
1721 * been initialized as per the documentation for
1722 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001723 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001724 * It must remain valid until the operation
1725 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001726 * \param alg The cipher algorithm to compute
1727 * (\c PSA_ALG_XXX value such that
1728 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001729 *
Gilles Peskine28538492018-07-11 17:34:00 +02001730 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001731 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001732 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001733 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001734 * \retval #PSA_ERROR_NOT_PERMITTED
1735 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001736 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001737 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001738 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001739 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1740 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1741 * \retval #PSA_ERROR_HARDWARE_FAILURE
1742 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001743 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001744 * The operation state is not valid (already set up and not
1745 * subsequently completed).
1746 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001747 * The library has not been previously initialized by psa_crypto_init().
1748 * It is implementation-dependent whether a failure to initialize
1749 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001750 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001751psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001752 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001753 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001754
1755/** Set the key for a multipart symmetric decryption operation.
1756 *
1757 * The sequence of operations to decrypt a message with a symmetric cipher
1758 * is as follows:
1759 * -# Allocate an operation object which will be passed to all the functions
1760 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001761 * -# Initialize the operation object with one of the methods described in the
1762 * documentation for #psa_cipher_operation_t, e.g.
1763 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001764 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001765 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001766 * decryption. If the IV is prepended to the ciphertext, you can call
1767 * psa_cipher_update() on a buffer containing the IV followed by the
1768 * beginning of the message.
1769 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1770 * of the message each time.
1771 * -# Call psa_cipher_finish().
1772 *
1773 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001774 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001775 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001776 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001777 * eventually terminate the operation. The following events terminate an
1778 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001779 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001780 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001781 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001782 * \param[in,out] operation The operation object to set up. It must have
1783 * been initialized as per the documentation for
1784 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001785 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001786 * It must remain valid until the operation
1787 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001788 * \param alg The cipher algorithm to compute
1789 * (\c PSA_ALG_XXX value such that
1790 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001791 *
Gilles Peskine28538492018-07-11 17:34:00 +02001792 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001793 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001794 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001795 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001796 * \retval #PSA_ERROR_NOT_PERMITTED
1797 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001798 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001799 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001800 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001801 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1802 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1803 * \retval #PSA_ERROR_HARDWARE_FAILURE
1804 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001805 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001806 * The operation state is not valid (already set up and not
1807 * subsequently completed).
1808 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001809 * The library has not been previously initialized by psa_crypto_init().
1810 * It is implementation-dependent whether a failure to initialize
1811 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001812 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001813psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001814 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001815 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001816
Gilles Peskinedcd14942018-07-12 00:30:52 +02001817/** Generate an IV for a symmetric encryption operation.
1818 *
1819 * This function generates a random IV (initialization vector), nonce
1820 * or initial counter value for the encryption operation as appropriate
1821 * for the chosen algorithm, key type and key size.
1822 *
1823 * The application must call psa_cipher_encrypt_setup() before
1824 * calling this function.
1825 *
1826 * If this function returns an error status, the operation becomes inactive.
1827 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001828 * \param[in,out] operation Active cipher operation.
1829 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001830 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001831 * \param[out] iv_length On success, the number of bytes of the
1832 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001833 *
1834 * \retval #PSA_SUCCESS
1835 * Success.
1836 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001837 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001838 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001839 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001840 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1841 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1842 * \retval #PSA_ERROR_HARDWARE_FAILURE
1843 * \retval #PSA_ERROR_TAMPERING_DETECTED
1844 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001845psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1846 unsigned char *iv,
1847 size_t iv_size,
1848 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001849
Gilles Peskinedcd14942018-07-12 00:30:52 +02001850/** Set the IV for a symmetric encryption or decryption operation.
1851 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001852 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001853 * or initial counter value for the encryption or decryption operation.
1854 *
1855 * The application must call psa_cipher_encrypt_setup() before
1856 * calling this function.
1857 *
1858 * If this function returns an error status, the operation becomes inactive.
1859 *
1860 * \note When encrypting, applications should use psa_cipher_generate_iv()
1861 * instead of this function, unless implementing a protocol that requires
1862 * a non-random IV.
1863 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001864 * \param[in,out] operation Active cipher operation.
1865 * \param[in] iv Buffer containing the IV to use.
1866 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001867 *
1868 * \retval #PSA_SUCCESS
1869 * Success.
1870 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001871 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001872 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001873 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001874 * or the chosen algorithm does not use an IV.
1875 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1876 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1877 * \retval #PSA_ERROR_HARDWARE_FAILURE
1878 * \retval #PSA_ERROR_TAMPERING_DETECTED
1879 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001880psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1881 const unsigned char *iv,
1882 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001883
Gilles Peskinedcd14942018-07-12 00:30:52 +02001884/** Encrypt or decrypt a message fragment in an active cipher operation.
1885 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001886 * Before calling this function, you must:
1887 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1888 * The choice of setup function determines whether this function
1889 * encrypts or decrypts its input.
1890 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1891 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001892 *
1893 * If this function returns an error status, the operation becomes inactive.
1894 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001895 * \param[in,out] operation Active cipher operation.
1896 * \param[in] input Buffer containing the message fragment to
1897 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001898 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001899 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001900 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001901 * \param[out] output_length On success, the number of bytes
1902 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001903 *
1904 * \retval #PSA_SUCCESS
1905 * Success.
1906 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001907 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001908 * not set, or already completed).
1909 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1910 * The size of the \p output buffer is too small.
1911 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1912 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1913 * \retval #PSA_ERROR_HARDWARE_FAILURE
1914 * \retval #PSA_ERROR_TAMPERING_DETECTED
1915 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001916psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1917 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001918 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001919 unsigned char *output,
1920 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001921 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001922
Gilles Peskinedcd14942018-07-12 00:30:52 +02001923/** Finish encrypting or decrypting a message in a cipher operation.
1924 *
1925 * The application must call psa_cipher_encrypt_setup() or
1926 * psa_cipher_decrypt_setup() before calling this function. The choice
1927 * of setup function determines whether this function encrypts or
1928 * decrypts its input.
1929 *
1930 * This function finishes the encryption or decryption of the message
1931 * formed by concatenating the inputs passed to preceding calls to
1932 * psa_cipher_update().
1933 *
1934 * When this function returns, the operation becomes inactive.
1935 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001936 * \param[in,out] operation Active cipher operation.
1937 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001938 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001939 * \param[out] output_length On success, the number of bytes
1940 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001941 *
1942 * \retval #PSA_SUCCESS
1943 * Success.
1944 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001945 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001946 * not set, or already completed).
1947 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1948 * The size of the \p output buffer is too small.
1949 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1950 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1951 * \retval #PSA_ERROR_HARDWARE_FAILURE
1952 * \retval #PSA_ERROR_TAMPERING_DETECTED
1953 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001954psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001955 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001956 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001957 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001958
Gilles Peskinedcd14942018-07-12 00:30:52 +02001959/** Abort a cipher operation.
1960 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001961 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001962 * \p operation structure itself. Once aborted, the operation object
1963 * can be reused for another operation by calling
1964 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001965 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001966 * You may call this function any time after the operation object has
1967 * been initialized by any of the following methods:
1968 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1969 * whether it succeeds or not.
1970 * - Initializing the \c struct to all-bits-zero.
1971 * - Initializing the \c struct to logical zeros, e.g.
1972 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001973 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001974 * In particular, calling psa_cipher_abort() after the operation has been
1975 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1976 * is safe and has no effect.
1977 *
1978 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001979 *
1980 * \retval #PSA_SUCCESS
1981 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001982 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001983 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1984 * \retval #PSA_ERROR_HARDWARE_FAILURE
1985 * \retval #PSA_ERROR_TAMPERING_DETECTED
1986 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001987psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1988
1989/**@}*/
1990
Gilles Peskine3b555712018-03-03 21:27:57 +01001991/** \defgroup aead Authenticated encryption with associated data (AEAD)
1992 * @{
1993 */
1994
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001995/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001996 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001997 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001998 * \param alg The AEAD algorithm to compute
1999 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002000 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002001 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002002 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002003 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002004 * but not encrypted.
2005 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002006 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002007 * encrypted.
2008 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002009 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002010 * encrypted data. The additional data is not
2011 * part of this output. For algorithms where the
2012 * encrypted data and the authentication tag
2013 * are defined as separate outputs, the
2014 * authentication tag is appended to the
2015 * encrypted data.
2016 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2017 * This must be at least
2018 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2019 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002020 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002021 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002022 *
Gilles Peskine28538492018-07-11 17:34:00 +02002023 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002024 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002025 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002026 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002027 * \retval #PSA_ERROR_NOT_PERMITTED
2028 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002029 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002030 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002031 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002032 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2033 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2034 * \retval #PSA_ERROR_HARDWARE_FAILURE
2035 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002036 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002037 * The library has not been previously initialized by psa_crypto_init().
2038 * It is implementation-dependent whether a failure to initialize
2039 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002040 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002041psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002042 psa_algorithm_t alg,
2043 const uint8_t *nonce,
2044 size_t nonce_length,
2045 const uint8_t *additional_data,
2046 size_t additional_data_length,
2047 const uint8_t *plaintext,
2048 size_t plaintext_length,
2049 uint8_t *ciphertext,
2050 size_t ciphertext_size,
2051 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002052
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002053/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002054 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002055 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002056 * \param alg The AEAD algorithm to compute
2057 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002058 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002059 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002060 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002061 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002062 * but not encrypted.
2063 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002064 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002065 * encrypted. For algorithms where the
2066 * encrypted data and the authentication tag
2067 * are defined as separate inputs, the buffer
2068 * must contain the encrypted data followed
2069 * by the authentication tag.
2070 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002071 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002072 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2073 * This must be at least
2074 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2075 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002076 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002077 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002078 *
Gilles Peskine28538492018-07-11 17:34:00 +02002079 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002080 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002081 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002082 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002083 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002084 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002085 * \retval #PSA_ERROR_NOT_PERMITTED
2086 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002087 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002088 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002089 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002090 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2091 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2092 * \retval #PSA_ERROR_HARDWARE_FAILURE
2093 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002094 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002095 * The library has not been previously initialized by psa_crypto_init().
2096 * It is implementation-dependent whether a failure to initialize
2097 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002098 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002099psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002100 psa_algorithm_t alg,
2101 const uint8_t *nonce,
2102 size_t nonce_length,
2103 const uint8_t *additional_data,
2104 size_t additional_data_length,
2105 const uint8_t *ciphertext,
2106 size_t ciphertext_length,
2107 uint8_t *plaintext,
2108 size_t plaintext_size,
2109 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002110
Gilles Peskine30a9e412019-01-14 18:36:12 +01002111/** The type of the state data structure for multipart AEAD operations.
2112 *
2113 * Before calling any function on an AEAD operation object, the application
2114 * must initialize it by any of the following means:
2115 * - Set the structure to all-bits-zero, for example:
2116 * \code
2117 * psa_aead_operation_t operation;
2118 * memset(&operation, 0, sizeof(operation));
2119 * \endcode
2120 * - Initialize the structure to logical zero values, for example:
2121 * \code
2122 * psa_aead_operation_t operation = {0};
2123 * \endcode
2124 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2125 * for example:
2126 * \code
2127 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2128 * \endcode
2129 * - Assign the result of the function psa_aead_operation_init()
2130 * to the structure, for example:
2131 * \code
2132 * psa_aead_operation_t operation;
2133 * operation = psa_aead_operation_init();
2134 * \endcode
2135 *
2136 * This is an implementation-defined \c struct. Applications should not
2137 * make any assumptions about the content of this structure except
2138 * as directed by the documentation of a specific implementation. */
2139typedef struct psa_aead_operation_s psa_aead_operation_t;
2140
2141/** \def PSA_AEAD_OPERATION_INIT
2142 *
2143 * This macro returns a suitable initializer for an AEAD operation object of
2144 * type #psa_aead_operation_t.
2145 */
2146#ifdef __DOXYGEN_ONLY__
2147/* This is an example definition for documentation purposes.
2148 * Implementations should define a suitable value in `crypto_struct.h`.
2149 */
2150#define PSA_AEAD_OPERATION_INIT {0}
2151#endif
2152
2153/** Return an initial value for an AEAD operation object.
2154 */
2155static psa_aead_operation_t psa_aead_operation_init(void);
2156
2157/** Set the key for a multipart authenticated encryption operation.
2158 *
2159 * The sequence of operations to encrypt a message with authentication
2160 * is as follows:
2161 * -# Allocate an operation object which will be passed to all the functions
2162 * listed here.
2163 * -# Initialize the operation object with one of the methods described in the
2164 * documentation for #psa_aead_operation_t, e.g.
2165 * PSA_AEAD_OPERATION_INIT.
2166 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002167 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2168 * inputs to the subsequent calls to psa_aead_update_ad() and
2169 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2170 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002171 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2172 * generate or set the nonce. You should use
2173 * psa_aead_generate_nonce() unless the protocol you are implementing
2174 * requires a specific nonce value.
2175 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2176 * of the non-encrypted additional authenticated data each time.
2177 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002178 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002179 * -# Call psa_aead_finish().
2180 *
2181 * The application may call psa_aead_abort() at any time after the operation
2182 * has been initialized.
2183 *
2184 * After a successful call to psa_aead_encrypt_setup(), the application must
2185 * eventually terminate the operation. The following events terminate an
2186 * operation:
2187 * - A failed call to any of the \c psa_aead_xxx functions.
2188 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2189 *
2190 * \param[in,out] operation The operation object to set up. It must have
2191 * been initialized as per the documentation for
2192 * #psa_aead_operation_t and not yet in use.
2193 * \param handle Handle to the key to use for the operation.
2194 * It must remain valid until the operation
2195 * terminates.
2196 * \param alg The AEAD algorithm to compute
2197 * (\c PSA_ALG_XXX value such that
2198 * #PSA_ALG_IS_AEAD(\p alg) is true).
2199 *
2200 * \retval #PSA_SUCCESS
2201 * Success.
2202 * \retval #PSA_ERROR_INVALID_HANDLE
2203 * \retval #PSA_ERROR_EMPTY_SLOT
2204 * \retval #PSA_ERROR_NOT_PERMITTED
2205 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002206 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002207 * \retval #PSA_ERROR_NOT_SUPPORTED
2208 * \p alg is not supported or is not an AEAD algorithm.
2209 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2210 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2211 * \retval #PSA_ERROR_HARDWARE_FAILURE
2212 * \retval #PSA_ERROR_TAMPERING_DETECTED
2213 * \retval #PSA_ERROR_BAD_STATE
2214 * The library has not been previously initialized by psa_crypto_init().
2215 * It is implementation-dependent whether a failure to initialize
2216 * results in this error code.
2217 */
2218psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2219 psa_key_handle_t handle,
2220 psa_algorithm_t alg);
2221
2222/** Set the key for a multipart authenticated decryption operation.
2223 *
2224 * The sequence of operations to decrypt a message with authentication
2225 * is as follows:
2226 * -# Allocate an operation object which will be passed to all the functions
2227 * listed here.
2228 * -# Initialize the operation object with one of the methods described in the
2229 * documentation for #psa_aead_operation_t, e.g.
2230 * PSA_AEAD_OPERATION_INIT.
2231 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002232 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2233 * inputs to the subsequent calls to psa_aead_update_ad() and
2234 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2235 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002236 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2237 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2238 * of the non-encrypted additional authenticated data each time.
2239 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002240 * of the ciphertext to decrypt each time.
2241 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002242 *
2243 * The application may call psa_aead_abort() at any time after the operation
2244 * has been initialized.
2245 *
2246 * After a successful call to psa_aead_decrypt_setup(), the application must
2247 * eventually terminate the operation. The following events terminate an
2248 * operation:
2249 * - A failed call to any of the \c psa_aead_xxx functions.
2250 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2251 *
2252 * \param[in,out] operation The operation object to set up. It must have
2253 * been initialized as per the documentation for
2254 * #psa_aead_operation_t and not yet in use.
2255 * \param handle Handle to the key to use for the operation.
2256 * It must remain valid until the operation
2257 * terminates.
2258 * \param alg The AEAD algorithm to compute
2259 * (\c PSA_ALG_XXX value such that
2260 * #PSA_ALG_IS_AEAD(\p alg) is true).
2261 *
2262 * \retval #PSA_SUCCESS
2263 * Success.
2264 * \retval #PSA_ERROR_INVALID_HANDLE
2265 * \retval #PSA_ERROR_EMPTY_SLOT
2266 * \retval #PSA_ERROR_NOT_PERMITTED
2267 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002268 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002269 * \retval #PSA_ERROR_NOT_SUPPORTED
2270 * \p alg is not supported or is not an AEAD algorithm.
2271 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2272 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2273 * \retval #PSA_ERROR_HARDWARE_FAILURE
2274 * \retval #PSA_ERROR_TAMPERING_DETECTED
2275 * \retval #PSA_ERROR_BAD_STATE
2276 * The library has not been previously initialized by psa_crypto_init().
2277 * It is implementation-dependent whether a failure to initialize
2278 * results in this error code.
2279 */
2280psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2281 psa_key_handle_t handle,
2282 psa_algorithm_t alg);
2283
2284/** Generate a random nonce for an authenticated encryption operation.
2285 *
2286 * This function generates a random nonce for the authenticated encryption
2287 * operation with an appropriate size for the chosen algorithm, key type
2288 * and key size.
2289 *
2290 * The application must call psa_aead_encrypt_setup() before
2291 * calling this function.
2292 *
2293 * If this function returns an error status, the operation becomes inactive.
2294 *
2295 * \param[in,out] operation Active AEAD operation.
2296 * \param[out] nonce Buffer where the generated nonce is to be
2297 * written.
2298 * \param nonce_size Size of the \p nonce buffer in bytes.
2299 * \param[out] nonce_length On success, the number of bytes of the
2300 * generated nonce.
2301 *
2302 * \retval #PSA_SUCCESS
2303 * Success.
2304 * \retval #PSA_ERROR_BAD_STATE
2305 * The operation state is not valid (not set up, or nonce already set).
2306 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2307 * The size of the \p nonce buffer is too small.
2308 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2309 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2310 * \retval #PSA_ERROR_HARDWARE_FAILURE
2311 * \retval #PSA_ERROR_TAMPERING_DETECTED
2312 */
2313psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2314 unsigned char *nonce,
2315 size_t nonce_size,
2316 size_t *nonce_length);
2317
2318/** Set the nonce for an authenticated encryption or decryption operation.
2319 *
2320 * This function sets the nonce for the authenticated
2321 * encryption or decryption operation.
2322 *
2323 * The application must call psa_aead_encrypt_setup() before
2324 * calling this function.
2325 *
2326 * If this function returns an error status, the operation becomes inactive.
2327 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002328 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002329 * instead of this function, unless implementing a protocol that requires
2330 * a non-random IV.
2331 *
2332 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002333 * \param[in] nonce Buffer containing the nonce to use.
2334 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002335 *
2336 * \retval #PSA_SUCCESS
2337 * Success.
2338 * \retval #PSA_ERROR_BAD_STATE
2339 * The operation state is not valid (not set up, or nonce already set).
2340 * \retval #PSA_ERROR_INVALID_ARGUMENT
2341 * The size of \p nonce is not acceptable for the chosen algorithm.
2342 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2343 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2344 * \retval #PSA_ERROR_HARDWARE_FAILURE
2345 * \retval #PSA_ERROR_TAMPERING_DETECTED
2346 */
2347psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2348 const unsigned char *nonce,
2349 size_t nonce_length);
2350
Gilles Peskinebc59c852019-01-17 15:26:08 +01002351/** Declare the lengths of the message and additional data for AEAD.
2352 *
2353 * The application must call this function before calling
2354 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2355 * the operation requires it. If the algorithm does not require it,
2356 * calling this function is optional, but if this function is called
2357 * then the implementation must enforce the lengths.
2358 *
2359 * You may call this function before or after setting the nonce with
2360 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2361 *
2362 * - For #PSA_ALG_CCM, calling this function is required.
2363 * - For the other AEAD algorithms defined in this specification, calling
2364 * this function is not required.
2365 * - For vendor-defined algorithm, refer to the vendor documentation.
2366 *
2367 * \param[in,out] operation Active AEAD operation.
2368 * \param ad_length Size of the non-encrypted additional
2369 * authenticated data in bytes.
2370 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2371 *
2372 * \retval #PSA_SUCCESS
2373 * Success.
2374 * \retval #PSA_ERROR_BAD_STATE
2375 * The operation state is not valid (not set up, already completed,
2376 * or psa_aead_update_ad() or psa_aead_update() already called).
2377 * \retval #PSA_ERROR_INVALID_ARGUMENT
2378 * At least one of the lengths is not acceptable for the chosen
2379 * algorithm.
2380 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2381 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2382 * \retval #PSA_ERROR_HARDWARE_FAILURE
2383 * \retval #PSA_ERROR_TAMPERING_DETECTED
2384 */
2385psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2386 size_t ad_length,
2387 size_t plaintext_length);
2388
Gilles Peskine30a9e412019-01-14 18:36:12 +01002389/** Pass additional data to an active AEAD operation.
2390 *
2391 * Additional data is authenticated, but not encrypted.
2392 *
2393 * You may call this function multiple times to pass successive fragments
2394 * of the additional data. You may not call this function after passing
2395 * data to encrypt or decrypt with psa_aead_update().
2396 *
2397 * Before calling this function, you must:
2398 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2399 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2400 *
2401 * If this function returns an error status, the operation becomes inactive.
2402 *
2403 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2404 * there is no guarantee that the input is valid. Therefore, until
2405 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2406 * treat the input as untrusted and prepare to undo any action that
2407 * depends on the input if psa_aead_verify() returns an error status.
2408 *
2409 * \param[in,out] operation Active AEAD operation.
2410 * \param[in] input Buffer containing the fragment of
2411 * additional data.
2412 * \param input_length Size of the \p input buffer in bytes.
2413 *
2414 * \retval #PSA_SUCCESS
2415 * Success.
2416 * \retval #PSA_ERROR_BAD_STATE
2417 * The operation state is not valid (not set up, nonce not set,
2418 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002419 * \retval #PSA_ERROR_INVALID_ARGUMENT
2420 * The total input length overflows the additional data length that
2421 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002422 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2423 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2424 * \retval #PSA_ERROR_HARDWARE_FAILURE
2425 * \retval #PSA_ERROR_TAMPERING_DETECTED
2426 */
2427psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2428 const uint8_t *input,
2429 size_t input_length);
2430
2431/** Encrypt or decrypt a message fragment in an active AEAD operation.
2432 *
2433 * Before calling this function, you must:
2434 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2435 * The choice of setup function determines whether this function
2436 * encrypts or decrypts its input.
2437 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2438 * 3. Call psa_aead_update_ad() to pass all the additional data.
2439 *
2440 * If this function returns an error status, the operation becomes inactive.
2441 *
2442 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2443 * there is no guarantee that the input is valid. Therefore, until
2444 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2445 * - Do not use the output in any way other than storing it in a
2446 * confidential location. If you take any action that depends
2447 * on the tentative decrypted data, this action will need to be
2448 * undone if the input turns out not to be valid. Furthermore,
2449 * if an adversary can observe that this action took place
2450 * (for example through timing), they may be able to use this
2451 * fact as an oracle to decrypt any message encrypted with the
2452 * same key.
2453 * - In particular, do not copy the output anywhere but to a
2454 * memory or storage space that you have exclusive access to.
2455 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002456 * This function does not require the input to be aligned to any
2457 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002458 * a whole block at a time, it must consume all the input provided, but
2459 * it may delay the end of the corresponding output until a subsequent
2460 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2461 * provides sufficient input. The amount of data that can be delayed
2462 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002463 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002464 * \param[in,out] operation Active AEAD operation.
2465 * \param[in] input Buffer containing the message fragment to
2466 * encrypt or decrypt.
2467 * \param input_length Size of the \p input buffer in bytes.
2468 * \param[out] output Buffer where the output is to be written.
2469 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002470 * This must be at least
2471 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2472 * \p input_length) where \c alg is the
2473 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002474 * \param[out] output_length On success, the number of bytes
2475 * that make up the returned output.
2476 *
2477 * \retval #PSA_SUCCESS
2478 * Success.
2479 * \retval #PSA_ERROR_BAD_STATE
2480 * The operation state is not valid (not set up, nonce not set
2481 * or already completed).
2482 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2483 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002484 * You can determine a sufficient buffer size by calling
2485 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2486 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002487 * \retval #PSA_ERROR_INVALID_ARGUMENT
2488 * The total length of input to psa_aead_update_ad() so far is
2489 * less than the additional data length that was previously
2490 * specified with psa_aead_set_lengths().
2491 * \retval #PSA_ERROR_INVALID_ARGUMENT
2492 * The total input length overflows the plaintext length that
2493 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002494 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2495 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2496 * \retval #PSA_ERROR_HARDWARE_FAILURE
2497 * \retval #PSA_ERROR_TAMPERING_DETECTED
2498 */
2499psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2500 const uint8_t *input,
2501 size_t input_length,
2502 unsigned char *output,
2503 size_t output_size,
2504 size_t *output_length);
2505
2506/** Finish encrypting a message in an AEAD operation.
2507 *
2508 * The operation must have been set up with psa_aead_encrypt_setup().
2509 *
2510 * This function finishes the authentication of the additional data
2511 * formed by concatenating the inputs passed to preceding calls to
2512 * psa_aead_update_ad() with the plaintext formed by concatenating the
2513 * inputs passed to preceding calls to psa_aead_update().
2514 *
2515 * This function has two output buffers:
2516 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002517 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002518 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002519 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002520 * that the operation performs.
2521 *
2522 * When this function returns, the operation becomes inactive.
2523 *
2524 * \param[in,out] operation Active AEAD operation.
2525 * \param[out] ciphertext Buffer where the last part of the ciphertext
2526 * is to be written.
2527 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002528 * This must be at least
2529 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2530 * \c alg is the algorithm that is being
2531 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002532 * \param[out] ciphertext_length On success, the number of bytes of
2533 * returned ciphertext.
2534 * \param[out] tag Buffer where the authentication tag is
2535 * to be written.
2536 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002537 * This must be at least
2538 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2539 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002540 * \param[out] tag_length On success, the number of bytes
2541 * that make up the returned tag.
2542 *
2543 * \retval #PSA_SUCCESS
2544 * Success.
2545 * \retval #PSA_ERROR_BAD_STATE
2546 * The operation state is not valid (not set up, nonce not set,
2547 * decryption, or already completed).
2548 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002549 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002550 * You can determine a sufficient buffer size for \p ciphertext by
2551 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2552 * where \c alg is the algorithm that is being calculated.
2553 * You can determine a sufficient buffer size for \p tag by
2554 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002555 * \retval #PSA_ERROR_INVALID_ARGUMENT
2556 * The total length of input to psa_aead_update_ad() so far is
2557 * less than the additional data length that was previously
2558 * specified with psa_aead_set_lengths().
2559 * \retval #PSA_ERROR_INVALID_ARGUMENT
2560 * The total length of input to psa_aead_update() so far is
2561 * less than the plaintext length that was previously
2562 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002563 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2564 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2565 * \retval #PSA_ERROR_HARDWARE_FAILURE
2566 * \retval #PSA_ERROR_TAMPERING_DETECTED
2567 */
2568psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002569 uint8_t *ciphertext,
2570 size_t ciphertext_size,
2571 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002572 uint8_t *tag,
2573 size_t tag_size,
2574 size_t *tag_length);
2575
2576/** Finish authenticating and decrypting a message in an AEAD operation.
2577 *
2578 * The operation must have been set up with psa_aead_decrypt_setup().
2579 *
2580 * This function finishes the authentication of the additional data
2581 * formed by concatenating the inputs passed to preceding calls to
2582 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2583 * inputs passed to preceding calls to psa_aead_update().
2584 *
2585 * When this function returns, the operation becomes inactive.
2586 *
2587 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002588 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002589 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002590 * from previous calls to psa_aead_update()
2591 * that could not be processed until the end
2592 * of the input.
2593 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002594 * This must be at least
2595 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2596 * \c alg is the algorithm that is being
2597 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002598 * \param[out] plaintext_length On success, the number of bytes of
2599 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002600 * \param[in] tag Buffer containing the authentication tag.
2601 * \param tag_length Size of the \p tag buffer in bytes.
2602 *
2603 * \retval #PSA_SUCCESS
2604 * Success.
2605 * \retval #PSA_ERROR_BAD_STATE
2606 * The operation state is not valid (not set up, nonce not set,
2607 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002608 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2609 * The size of the \p plaintext buffer is too small.
2610 * You can determine a sufficient buffer size for \p plaintext by
2611 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2612 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002613 * \retval #PSA_ERROR_INVALID_ARGUMENT
2614 * The total length of input to psa_aead_update_ad() so far is
2615 * less than the additional data length that was previously
2616 * specified with psa_aead_set_lengths().
2617 * \retval #PSA_ERROR_INVALID_ARGUMENT
2618 * The total length of input to psa_aead_update() so far is
2619 * less than the plaintext length that was previously
2620 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002621 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2622 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2623 * \retval #PSA_ERROR_HARDWARE_FAILURE
2624 * \retval #PSA_ERROR_TAMPERING_DETECTED
2625 */
2626psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002627 uint8_t *plaintext,
2628 size_t plaintext_size,
2629 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002630 const uint8_t *tag,
2631 size_t tag_length);
2632
2633/** Abort an AEAD operation.
2634 *
2635 * Aborting an operation frees all associated resources except for the
2636 * \p operation structure itself. Once aborted, the operation object
2637 * can be reused for another operation by calling
2638 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2639 *
2640 * You may call this function any time after the operation object has
2641 * been initialized by any of the following methods:
2642 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2643 * whether it succeeds or not.
2644 * - Initializing the \c struct to all-bits-zero.
2645 * - Initializing the \c struct to logical zeros, e.g.
2646 * `psa_aead_operation_t operation = {0}`.
2647 *
2648 * In particular, calling psa_aead_abort() after the operation has been
2649 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2650 * is safe and has no effect.
2651 *
2652 * \param[in,out] operation Initialized AEAD operation.
2653 *
2654 * \retval #PSA_SUCCESS
2655 * \retval #PSA_ERROR_BAD_STATE
2656 * \p operation is not an active AEAD operation.
2657 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2658 * \retval #PSA_ERROR_HARDWARE_FAILURE
2659 * \retval #PSA_ERROR_TAMPERING_DETECTED
2660 */
2661psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2662
Gilles Peskine3b555712018-03-03 21:27:57 +01002663/**@}*/
2664
Gilles Peskine20035e32018-02-03 22:44:14 +01002665/** \defgroup asymmetric Asymmetric cryptography
2666 * @{
2667 */
2668
2669/**
2670 * \brief Sign a hash or short message with a private key.
2671 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002672 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002673 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002674 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2675 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2676 * to determine the hash algorithm to use.
2677 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002678 * \param handle Handle to the key to use for the operation.
2679 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002680 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002681 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002682 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002683 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002684 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002685 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002686 * \param[out] signature_length On success, the number of bytes
2687 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002688 *
Gilles Peskine28538492018-07-11 17:34:00 +02002689 * \retval #PSA_SUCCESS
2690 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002691 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002692 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002693 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002694 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002695 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002696 * \retval #PSA_ERROR_NOT_SUPPORTED
2697 * \retval #PSA_ERROR_INVALID_ARGUMENT
2698 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2699 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2700 * \retval #PSA_ERROR_HARDWARE_FAILURE
2701 * \retval #PSA_ERROR_TAMPERING_DETECTED
2702 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002703 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002704 * The library has not been previously initialized by psa_crypto_init().
2705 * It is implementation-dependent whether a failure to initialize
2706 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002707 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002708psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002709 psa_algorithm_t alg,
2710 const uint8_t *hash,
2711 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002712 uint8_t *signature,
2713 size_t signature_size,
2714 size_t *signature_length);
2715
2716/**
2717 * \brief Verify the signature a hash or short message using a public key.
2718 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002719 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002720 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002721 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2722 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2723 * to determine the hash algorithm to use.
2724 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002725 * \param handle Handle to the key to use for the operation.
2726 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002727 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002728 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002729 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002730 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002731 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002732 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002733 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002734 *
Gilles Peskine28538492018-07-11 17:34:00 +02002735 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002736 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002737 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002738 * The calculation was perfomed successfully, but the passed
2739 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002740 * \retval #PSA_ERROR_NOT_SUPPORTED
2741 * \retval #PSA_ERROR_INVALID_ARGUMENT
2742 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2743 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2744 * \retval #PSA_ERROR_HARDWARE_FAILURE
2745 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002746 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002747 * The library has not been previously initialized by psa_crypto_init().
2748 * It is implementation-dependent whether a failure to initialize
2749 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002750 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002751psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002752 psa_algorithm_t alg,
2753 const uint8_t *hash,
2754 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002755 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002756 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002757
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002758/**
2759 * \brief Encrypt a short message with a public key.
2760 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002761 * \param handle Handle to the key to use for the operation.
2762 * It must be a public key or an asymmetric
2763 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002764 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002765 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002766 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002767 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002768 * \param[in] salt A salt or label, if supported by the
2769 * encryption algorithm.
2770 * If the algorithm does not support a
2771 * salt, pass \c NULL.
2772 * If the algorithm supports an optional
2773 * salt and you do not want to pass a salt,
2774 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002775 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002776 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2777 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002778 * \param salt_length Size of the \p salt buffer in bytes.
2779 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002780 * \param[out] output Buffer where the encrypted message is to
2781 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002782 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002783 * \param[out] output_length On success, the number of bytes
2784 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002785 *
Gilles Peskine28538492018-07-11 17:34:00 +02002786 * \retval #PSA_SUCCESS
2787 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002788 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002789 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002790 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002791 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002792 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002793 * \retval #PSA_ERROR_NOT_SUPPORTED
2794 * \retval #PSA_ERROR_INVALID_ARGUMENT
2795 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2796 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2797 * \retval #PSA_ERROR_HARDWARE_FAILURE
2798 * \retval #PSA_ERROR_TAMPERING_DETECTED
2799 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002800 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002801 * The library has not been previously initialized by psa_crypto_init().
2802 * It is implementation-dependent whether a failure to initialize
2803 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002804 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002805psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002806 psa_algorithm_t alg,
2807 const uint8_t *input,
2808 size_t input_length,
2809 const uint8_t *salt,
2810 size_t salt_length,
2811 uint8_t *output,
2812 size_t output_size,
2813 size_t *output_length);
2814
2815/**
2816 * \brief Decrypt a short message with a private key.
2817 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002818 * \param handle Handle to the key to use for the operation.
2819 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002820 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002821 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002822 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002823 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002824 * \param[in] salt A salt or label, if supported by the
2825 * encryption algorithm.
2826 * If the algorithm does not support a
2827 * salt, pass \c NULL.
2828 * If the algorithm supports an optional
2829 * salt and you do not want to pass a salt,
2830 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002831 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002832 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2833 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002834 * \param salt_length Size of the \p salt buffer in bytes.
2835 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002836 * \param[out] output Buffer where the decrypted message is to
2837 * be written.
2838 * \param output_size Size of the \c output buffer in bytes.
2839 * \param[out] output_length On success, the number of bytes
2840 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002841 *
Gilles Peskine28538492018-07-11 17:34:00 +02002842 * \retval #PSA_SUCCESS
2843 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002844 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002845 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002846 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002847 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002848 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002849 * \retval #PSA_ERROR_NOT_SUPPORTED
2850 * \retval #PSA_ERROR_INVALID_ARGUMENT
2851 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2852 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2853 * \retval #PSA_ERROR_HARDWARE_FAILURE
2854 * \retval #PSA_ERROR_TAMPERING_DETECTED
2855 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2856 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002857 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002858 * The library has not been previously initialized by psa_crypto_init().
2859 * It is implementation-dependent whether a failure to initialize
2860 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002861 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002862psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002863 psa_algorithm_t alg,
2864 const uint8_t *input,
2865 size_t input_length,
2866 const uint8_t *salt,
2867 size_t salt_length,
2868 uint8_t *output,
2869 size_t output_size,
2870 size_t *output_length);
2871
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002872/**@}*/
2873
Gilles Peskine35675b62019-05-16 17:26:11 +02002874/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002875 * @{
2876 */
2877
Gilles Peskine35675b62019-05-16 17:26:11 +02002878/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002879 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002880 * Before calling any function on a key derivation operation object, the
2881 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002882 * - Set the structure to all-bits-zero, for example:
2883 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002884 * psa_key_derivation_operation_t operation;
2885 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002886 * \endcode
2887 * - Initialize the structure to logical zero values, for example:
2888 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002889 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002890 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002891 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002892 * for example:
2893 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002894 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002895 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002896 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002897 * to the structure, for example:
2898 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002899 * psa_key_derivation_operation_t operation;
2900 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002901 * \endcode
2902 *
2903 * This is an implementation-defined \c struct. Applications should not
2904 * make any assumptions about the content of this structure except
2905 * as directed by the documentation of a specific implementation.
2906 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02002907typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002908
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002909/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002910 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002911 * This macro returns a suitable initializer for a key derivation operation
2912 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002913 */
2914#ifdef __DOXYGEN_ONLY__
2915/* This is an example definition for documentation purposes.
2916 * Implementations should define a suitable value in `crypto_struct.h`.
2917 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002918#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02002919#endif
2920
Gilles Peskine35675b62019-05-16 17:26:11 +02002921/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002922 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002923static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002924
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002925/** Set up a key derivation operation.
2926 *
2927 * A key derivation algorithm takes some inputs and uses them to generate
2928 * a byte stream in a deterministic way.
2929 * This byte stream can be used to produce keys and other
2930 * cryptographic material.
2931 *
2932 * To derive a key:
2933 * - Start with an initialized object of type #psa_key_derivation_operation_t.
2934 * - Call psa_key_derivation_setup() to select the algorithm.
2935 * - Provide the inputs for the key derivation by calling
2936 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2937 * as appropriate. Which inputs are needed, in what order, and whether
2938 * they may be keys and if so of what type depends on the algorithm.
2939 * - Optionally set the operation's maximum capacity with
2940 * psa_key_derivation_set_capacity(). You may do this before, in the middle
2941 * of or after providing inputs. For some algorithms, this step is mandatory
2942 * because the output depends on the maximum capacity.
2943 * - To derive a key, call psa_key_derivation_output_key().
2944 * To derive a byte string for a different purpose, call
2945 * - psa_key_derivation_output_bytes().
2946 * Successive calls to these functions use successive output bytes
2947 * calculated by the key derivation algorithm.
2948 * - Clean up the key derivation operation object with
2949 * psa_key_derivation_abort().
2950 *
2951 * \param[in,out] operation The key derivation operation object
2952 * to set up. It must
2953 * have been initialized but not set up yet.
2954 * \param alg The key derivation algorithm to compute
2955 * (\c PSA_ALG_XXX value such that
2956 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2957 *
2958 * \retval #PSA_SUCCESS
2959 * Success.
2960 * \retval #PSA_ERROR_INVALID_ARGUMENT
2961 * \c alg is not a key derivation algorithm.
2962 * \retval #PSA_ERROR_NOT_SUPPORTED
2963 * \c alg is not supported or is not a key derivation algorithm.
2964 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2965 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2966 * \retval #PSA_ERROR_HARDWARE_FAILURE
2967 * \retval #PSA_ERROR_TAMPERING_DETECTED
2968 * \retval #PSA_ERROR_BAD_STATE
2969 */
2970psa_status_t psa_key_derivation_setup(
2971 psa_key_derivation_operation_t *operation,
2972 psa_algorithm_t alg);
2973
Gilles Peskine35675b62019-05-16 17:26:11 +02002974/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002975 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002976 * The capacity of a key derivation is the maximum number of bytes that it can
2977 * return. When you get *N* bytes of output from a key derivation operation,
2978 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002979 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002980 * \param[in] operation The operation to query.
2981 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002982 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002983 * \retval #PSA_SUCCESS
2984 * \retval #PSA_ERROR_BAD_STATE
2985 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002986 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002987psa_status_t psa_key_derivation_get_capacity(
2988 const psa_key_derivation_operation_t *operation,
2989 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002990
Gilles Peskine35675b62019-05-16 17:26:11 +02002991/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002992 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002993 * The capacity of a key derivation operation is the maximum number of bytes
2994 * that the key derivation operation can return from this point onwards.
2995 *
2996 * \param[in,out] operation The key derivation operation object to modify.
2997 * \param capacity The new capacity of the operation.
2998 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002999 * current capacity.
3000 *
3001 * \retval #PSA_SUCCESS
3002 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003003 * \p capacity is larger than the operation's current capacity.
3004 * In this case, the operation object remains valid and its capacity
3005 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003006 * \retval #PSA_ERROR_BAD_STATE
3007 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3008 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003009psa_status_t psa_key_derivation_set_capacity(
3010 psa_key_derivation_operation_t *operation,
3011 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003012
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003013/** Use the maximum possible capacity for a key derivation operation.
3014 *
3015 * Use this value as the capacity argument when setting up a key derivation
3016 * to indicate that the operation should have the maximum possible capacity.
3017 * The value of the maximum possible capacity depends on the key derivation
3018 * algorithm.
3019 */
3020#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3021
3022/** Provide an input for key derivation or key agreement.
3023 *
3024 * Which inputs are required and in what order depends on the algorithm.
3025 * Refer to the documentation of each key derivation or key agreement
3026 * algorithm for information.
3027 *
3028 * This function passes direct inputs. Some inputs must be passed as keys
3029 * using psa_key_derivation_input_key() instead of this function. Refer to
3030 * the documentation of individual step types for information.
3031 *
3032 * \param[in,out] operation The key derivation operation object to use.
3033 * It must have been set up with
3034 * psa_key_derivation_setup() and must not
3035 * have produced any output yet.
3036 * \param step Which step the input data is for.
3037 * \param[in] data Input data to use.
3038 * \param data_length Size of the \p data buffer in bytes.
3039 *
3040 * \retval #PSA_SUCCESS
3041 * Success.
3042 * \retval #PSA_ERROR_INVALID_ARGUMENT
3043 * \c step is not compatible with the operation's algorithm.
3044 * \retval #PSA_ERROR_INVALID_ARGUMENT
3045 * \c step does not allow direct inputs.
3046 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3047 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3048 * \retval #PSA_ERROR_HARDWARE_FAILURE
3049 * \retval #PSA_ERROR_TAMPERING_DETECTED
3050 * \retval #PSA_ERROR_BAD_STATE
3051 * The value of \p step is not valid given the state of \p operation.
3052 * \retval #PSA_ERROR_BAD_STATE
3053 * The library has not been previously initialized by psa_crypto_init().
3054 * It is implementation-dependent whether a failure to initialize
3055 * results in this error code.
3056 */
3057psa_status_t psa_key_derivation_input_bytes(
3058 psa_key_derivation_operation_t *operation,
3059 psa_key_derivation_step_t step,
3060 const uint8_t *data,
3061 size_t data_length);
3062
3063/** Provide an input for key derivation in the form of a key.
3064 *
3065 * Which inputs are required and in what order depends on the algorithm.
3066 * Refer to the documentation of each key derivation or key agreement
3067 * algorithm for information.
3068 *
3069 * This function passes key inputs. Some inputs must be passed as keys
3070 * of the appropriate type using this function, while others must be
3071 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3072 * the documentation of individual step types for information.
3073 *
3074 * \param[in,out] operation The key derivation operation object to use.
3075 * It must have been set up with
3076 * psa_key_derivation_setup() and must not
3077 * have produced any output yet.
3078 * \param step Which step the input data is for.
3079 * \param handle Handle to the key. It must have an
3080 * appropriate type for \p step and must
3081 * allow the usage #PSA_KEY_USAGE_DERIVE.
3082 *
3083 * \retval #PSA_SUCCESS
3084 * Success.
3085 * \retval #PSA_ERROR_INVALID_HANDLE
3086 * \retval #PSA_ERROR_DOES_NOT_EXIST
3087 * \retval #PSA_ERROR_NOT_PERMITTED
3088 * \retval #PSA_ERROR_INVALID_ARGUMENT
3089 * \c step is not compatible with the operation's algorithm.
3090 * \retval #PSA_ERROR_INVALID_ARGUMENT
3091 * \c step does not allow key inputs.
3092 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3093 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3094 * \retval #PSA_ERROR_HARDWARE_FAILURE
3095 * \retval #PSA_ERROR_TAMPERING_DETECTED
3096 * \retval #PSA_ERROR_BAD_STATE
3097 * The value of \p step is not valid given the state of \p operation.
3098 * \retval #PSA_ERROR_BAD_STATE
3099 * The library has not been previously initialized by psa_crypto_init().
3100 * It is implementation-dependent whether a failure to initialize
3101 * results in this error code.
3102 */
3103psa_status_t psa_key_derivation_input_key(
3104 psa_key_derivation_operation_t *operation,
3105 psa_key_derivation_step_t step,
3106 psa_key_handle_t handle);
3107
3108/** Perform a key agreement and use the shared secret as input to a key
3109 * derivation.
3110 *
3111 * A key agreement algorithm takes two inputs: a private key \p private_key
3112 * a public key \p peer_key.
3113 * The result of this function is passed as input to a key derivation.
3114 * The output of this key derivation can be extracted by reading from the
3115 * resulting operation to produce keys and other cryptographic material.
3116 *
3117 * \param[in,out] operation The key derivation operation object to use.
3118 * It must have been set up with
3119 * psa_key_derivation_setup() with a
3120 * key agreement and derivation algorithm
3121 * \c alg (\c PSA_ALG_XXX value such that
3122 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3123 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3124 * is false).
3125 * The operation must be ready for an
3126 * input of the type given by \p step.
3127 * \param step Which step the input data is for.
3128 * \param private_key Handle to the private key to use.
3129 * \param[in] peer_key Public key of the peer. The peer key must be in the
3130 * same format that psa_import_key() accepts for the
3131 * public key type corresponding to the type of
3132 * private_key. That is, this function performs the
3133 * equivalent of
3134 * #psa_import_key(...,
3135 * `peer_key`, `peer_key_length`) where
3136 * with key attributes indicating the public key
3137 * type corresponding to the type of `private_key`.
3138 * For example, for EC keys, this means that peer_key
3139 * is interpreted as a point on the curve that the
3140 * private key is on. The standard formats for public
3141 * keys are documented in the documentation of
3142 * psa_export_public_key().
3143 * \param peer_key_length Size of \p peer_key in bytes.
3144 *
3145 * \retval #PSA_SUCCESS
3146 * Success.
3147 * \retval #PSA_ERROR_INVALID_HANDLE
3148 * \retval #PSA_ERROR_DOES_NOT_EXIST
3149 * \retval #PSA_ERROR_NOT_PERMITTED
3150 * \retval #PSA_ERROR_INVALID_ARGUMENT
3151 * \c private_key is not compatible with \c alg,
3152 * or \p peer_key is not valid for \c alg or not compatible with
3153 * \c private_key.
3154 * \retval #PSA_ERROR_NOT_SUPPORTED
3155 * \c alg is not supported or is not a key derivation algorithm.
3156 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3157 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3158 * \retval #PSA_ERROR_HARDWARE_FAILURE
3159 * \retval #PSA_ERROR_TAMPERING_DETECTED
3160 */
3161psa_status_t psa_key_derivation_key_agreement(
3162 psa_key_derivation_operation_t *operation,
3163 psa_key_derivation_step_t step,
3164 psa_key_handle_t private_key,
3165 const uint8_t *peer_key,
3166 size_t peer_key_length);
3167
Gilles Peskine35675b62019-05-16 17:26:11 +02003168/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003169 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003170 * This function calculates output bytes from a key derivation algorithm and
3171 * return those bytes.
3172 * If you view the key derivation's output as a stream of bytes, this
3173 * function destructively reads the requested number of bytes from the
3174 * stream.
3175 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003176 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003177 * \param[in,out] operation The key derivation operation object to read from.
3178 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003179 * \param output_length Number of bytes to output.
3180 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003181 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003182 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003183 * The operation's capacity was less than
3184 * \p output_length bytes. Note that in this case,
3185 * no output is written to the output buffer.
3186 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003187 * subsequent calls to this function will not
3188 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003189 * \retval #PSA_ERROR_BAD_STATE
3190 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3191 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3192 * \retval #PSA_ERROR_HARDWARE_FAILURE
3193 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003194 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003195psa_status_t psa_key_derivation_output_bytes(
3196 psa_key_derivation_operation_t *operation,
3197 uint8_t *output,
3198 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003199
Gilles Peskine35675b62019-05-16 17:26:11 +02003200/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003201 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003202 * This function calculates output bytes from a key derivation algorithm
3203 * and uses those bytes to generate a key deterministically.
3204 * If you view the key derivation's output as a stream of bytes, this
3205 * function destructively reads as many bytes as required from the
3206 * stream.
3207 * The operation's capacity decreases by the number of bytes read.
3208 *
3209 * How much output is produced and consumed from the operation, and how
3210 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003211 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003212 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003213 * of a given size, this function is functionally equivalent to
3214 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003215 * and passing the resulting output to #psa_import_key.
3216 * However, this function has a security benefit:
3217 * if the implementation provides an isolation boundary then
3218 * the key material is not exposed outside the isolation boundary.
3219 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003220 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003221 * The following key types defined in this specification follow this scheme:
3222 *
3223 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003224 * - #PSA_KEY_TYPE_ARC4;
3225 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003226 * - #PSA_KEY_TYPE_DERIVE;
3227 * - #PSA_KEY_TYPE_HMAC.
3228 *
3229 * - For ECC keys on a Montgomery elliptic curve
3230 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3231 * Montgomery curve), this function always draws a byte string whose
3232 * length is determined by the curve, and sets the mandatory bits
3233 * accordingly. That is:
3234 *
3235 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3236 * and process it as specified in RFC 7748 &sect;5.
3237 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3238 * and process it as specified in RFC 7748 &sect;5.
3239 *
3240 * - For key types for which the key is represented by a single sequence of
3241 * \p bits bits with constraints as to which bit sequences are acceptable,
3242 * this function draws a byte string of length (\p bits / 8) bytes rounded
3243 * up to the nearest whole number of bytes. If the resulting byte string
3244 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3245 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003246 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003247 * for the output produced by psa_export_key().
3248 * The following key types defined in this specification follow this scheme:
3249 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003250 * - #PSA_KEY_TYPE_DES.
3251 * Force-set the parity bits, but discard forbidden weak keys.
3252 * For 2-key and 3-key triple-DES, the three keys are generated
3253 * successively (for example, for 3-key triple-DES,
3254 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3255 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003256 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003257 * two keys).
Gilles Peskinedcaefae2019-05-16 12:55:35 +02003258 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR(\c group)
3259 * where \c group designates any Diffie-Hellman group),
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003260 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
3261 * ECC keys on a Weierstrass elliptic curve
3262 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3263 * Weierstrass curve).
3264 * For these key types, interpret the byte string as integer
3265 * in big-endian order. Discard it if it is not in the range
3266 * [0, *N* - 2] where *N* is the boundary of the private key domain
3267 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003268 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003269 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003270 * This method allows compliance to NIST standards, specifically
3271 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003272 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3273 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3274 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3275 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003276 *
3277 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003278 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003279 * implementation-defined.
3280 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003281 * In all cases, the data that is read is discarded from the operation.
3282 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003283 *
Gilles Peskine20628592019-04-19 19:29:50 +02003284 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003285 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003286 * \param[out] handle On success, a handle to the newly created key.
3287 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003288 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003289 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003290 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003291 * If the key is persistent, the key material and the key's metadata
3292 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003293 * \retval #PSA_ERROR_ALREADY_EXISTS
3294 * This is an attempt to create a persistent key, and there is
3295 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003296 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003297 * There was not enough data to create the desired key.
3298 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003299 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003300 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003301 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003302 * The key type or key size is not supported, either by the
3303 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003304 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003305 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3306 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3307 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3308 * \retval #PSA_ERROR_HARDWARE_FAILURE
3309 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003310 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003311 * The library has not been previously initialized by psa_crypto_init().
3312 * It is implementation-dependent whether a failure to initialize
3313 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003314 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003315psa_status_t psa_key_derivation_output_key(
3316 const psa_key_attributes_t *attributes,
3317 psa_key_derivation_operation_t *operation,
3318 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003319
Gilles Peskine35675b62019-05-16 17:26:11 +02003320/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003321 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003322 * Once a key derivation operation has been aborted, its capacity is zero.
3323 * Aborting an operation frees all associated resources except for the
3324 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003325 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003326 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003327 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003328 * psa_key_derivation_operation_init() or a zero value. In particular,
3329 * it is valid to call psa_key_derivation_abort() twice, or to call
3330 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003331 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003332 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003333 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003334 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003335 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003336 * \retval #PSA_SUCCESS
3337 * \retval #PSA_ERROR_BAD_STATE
3338 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3339 * \retval #PSA_ERROR_HARDWARE_FAILURE
3340 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003341 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003342psa_status_t psa_key_derivation_abort(
3343 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003344
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003345/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003346 *
3347 * \warning The raw result of a key agreement algorithm such as finite-field
3348 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3349 * not be used directly as key material. It should instead be passed as
3350 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003351 * a key derivation, use psa_key_derivation_key_agreement() and other
3352 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003353 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003354 * \param alg The key agreement algorithm to compute
3355 * (\c PSA_ALG_XXX value such that
3356 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3357 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003358 * \param private_key Handle to the private key to use.
3359 * \param[in] peer_key Public key of the peer. It must be
3360 * in the same format that psa_import_key()
3361 * accepts. The standard formats for public
3362 * keys are documented in the documentation
3363 * of psa_export_public_key().
3364 * \param peer_key_length Size of \p peer_key in bytes.
3365 * \param[out] output Buffer where the decrypted message is to
3366 * be written.
3367 * \param output_size Size of the \c output buffer in bytes.
3368 * \param[out] output_length On success, the number of bytes
3369 * that make up the returned output.
3370 *
3371 * \retval #PSA_SUCCESS
3372 * Success.
3373 * \retval #PSA_ERROR_INVALID_HANDLE
3374 * \retval #PSA_ERROR_EMPTY_SLOT
3375 * \retval #PSA_ERROR_NOT_PERMITTED
3376 * \retval #PSA_ERROR_INVALID_ARGUMENT
3377 * \p alg is not a key agreement algorithm
3378 * \retval #PSA_ERROR_INVALID_ARGUMENT
3379 * \p private_key is not compatible with \p alg,
3380 * or \p peer_key is not valid for \p alg or not compatible with
3381 * \p private_key.
3382 * \retval #PSA_ERROR_NOT_SUPPORTED
3383 * \p alg is not a supported key agreement algorithm.
3384 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3385 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3386 * \retval #PSA_ERROR_HARDWARE_FAILURE
3387 * \retval #PSA_ERROR_TAMPERING_DETECTED
3388 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003389psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3390 psa_key_handle_t private_key,
3391 const uint8_t *peer_key,
3392 size_t peer_key_length,
3393 uint8_t *output,
3394 size_t output_size,
3395 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003396
Gilles Peskineea0fb492018-07-12 17:17:20 +02003397/**@}*/
3398
Gilles Peskineedd76872018-07-20 17:42:05 +02003399/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003400 * @{
3401 */
3402
3403/**
3404 * \brief Generate random bytes.
3405 *
3406 * \warning This function **can** fail! Callers MUST check the return status
3407 * and MUST NOT use the content of the output buffer if the return
3408 * status is not #PSA_SUCCESS.
3409 *
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003410 * \note To generate a key, use psa_generate_random_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003411 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003412 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003413 * \param output_size Number of bytes to generate and output.
3414 *
Gilles Peskine28538492018-07-11 17:34:00 +02003415 * \retval #PSA_SUCCESS
3416 * \retval #PSA_ERROR_NOT_SUPPORTED
3417 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3418 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3419 * \retval #PSA_ERROR_HARDWARE_FAILURE
3420 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003421 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003422 * The library has not been previously initialized by psa_crypto_init().
3423 * It is implementation-dependent whether a failure to initialize
3424 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003425 */
3426psa_status_t psa_generate_random(uint8_t *output,
3427 size_t output_size);
3428
3429/**
3430 * \brief Generate a key or key pair.
3431 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003432 * The key is generated randomly.
3433 * Its location, policy, type and size are taken from \p attributes.
3434 *
Gilles Peskine20628592019-04-19 19:29:50 +02003435 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003436 * \param[out] handle On success, a handle to the newly created key.
3437 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003438 *
Gilles Peskine28538492018-07-11 17:34:00 +02003439 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003440 * Success.
3441 * If the key is persistent, the key material and the key's metadata
3442 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003443 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003444 * This is an attempt to create a persistent key, and there is
3445 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003446 * \retval #PSA_ERROR_NOT_SUPPORTED
3447 * \retval #PSA_ERROR_INVALID_ARGUMENT
3448 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3449 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3450 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3451 * \retval #PSA_ERROR_HARDWARE_FAILURE
3452 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003453 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003454 * The library has not been previously initialized by psa_crypto_init().
3455 * It is implementation-dependent whether a failure to initialize
3456 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003457 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003458psa_status_t psa_generate_random_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003459 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003460
3461/**@}*/
3462
Gilles Peskinee59236f2018-01-27 23:32:46 +01003463#ifdef __cplusplus
3464}
3465#endif
3466
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003467/* The file "crypto_sizes.h" contains definitions for size calculation
3468 * macros whose definitions are implementation-specific. */
3469#include "crypto_sizes.h"
3470
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003471/* The file "crypto_struct.h" contains definitions for
3472 * implementation-specific structs that are declared above. */
3473#include "crypto_struct.h"
3474
3475/* The file "crypto_extra.h" contains vendor-specific definitions. This
3476 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003477#include "crypto_extra.h"
3478
3479#endif /* PSA_CRYPTO_H */