blob: f4198c8d0d97ebc0d10428f94e00eecb34545318 [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
Gilles Peskine4b3eb692019-05-16 21:35:18 +020089 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +020090 * \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().
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200183 * -# When generating a random key with psa_generate_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().
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200186 * -# Call a key creation function: psa_import_key(), psa_generate_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
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200217 * psa_import_key(), psa_generate_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
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200242 * psa_import_key(), psa_generate_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
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100335
Gilles Peskine20628592019-04-19 19:29:50 +0200336/** Retrieve the algorithm policy from key attributes.
337 *
338 * This function may be declared as `static` (i.e. without external
339 * linkage). This function may be provided as a function-like macro,
340 * but in this case it must evaluate its argument exactly once.
341 *
342 * \param[in] attributes The key attribute structure to query.
343 *
344 * \return The algorithm stored in the attribute structure.
345 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200346static psa_algorithm_t psa_get_key_algorithm(
347 const psa_key_attributes_t *attributes);
348
Gilles Peskine20628592019-04-19 19:29:50 +0200349/** Declare the type of a key.
350 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200351 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200352 * previously set in \p attributes.
353 *
354 * This function may be declared as `static` (i.e. without external
355 * linkage). This function may be provided as a function-like macro,
356 * but in this case it must evaluate each of its arguments exactly once.
357 *
358 * \param[out] attributes The attribute structure to write to.
359 * \param type The key type to write.
360 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200361static void psa_set_key_type(psa_key_attributes_t *attributes,
362 psa_key_type_t type);
363
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100364
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200365/** Declare the size of a key.
366 *
367 * This function overwrites any key size previously set in \p attributes.
368 *
369 * This function may be declared as `static` (i.e. without external
370 * linkage). This function may be provided as a function-like macro,
371 * but in this case it must evaluate each of its arguments exactly once.
372 *
373 * \param[out] attributes The attribute structure to write to.
374 * \param bits The key size in bits.
375 */
376static void psa_set_key_bits(psa_key_attributes_t *attributes,
377 size_t bits);
378
Gilles Peskine20628592019-04-19 19:29:50 +0200379/** Retrieve the key type from key attributes.
380 *
381 * This function may be declared as `static` (i.e. without external
382 * linkage). This function may be provided as a function-like macro,
383 * but in this case it must evaluate its argument exactly once.
384 *
385 * \param[in] attributes The key attribute structure to query.
386 *
387 * \return The key type stored in the attribute structure.
388 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200389static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
390
Gilles Peskine20628592019-04-19 19:29:50 +0200391/** Retrieve the key size from key attributes.
392 *
393 * This function may be declared as `static` (i.e. without external
394 * linkage). This function may be provided as a function-like macro,
395 * but in this case it must evaluate its argument exactly once.
396 *
397 * \param[in] attributes The key attribute structure to query.
398 *
399 * \return The key size stored in the attribute structure, in bits.
400 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200401static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
402
Gilles Peskine20628592019-04-19 19:29:50 +0200403/** Retrieve the attributes of a key.
404 *
405 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200406 * psa_reset_key_attributes(). It then copies the attributes of
407 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200408 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200409 * \note This function may allocate memory or other resources.
410 * Once you have called this function on an attribute structure,
411 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200412 *
Gilles Peskine20628592019-04-19 19:29:50 +0200413 * \param[in] handle Handle to the key to query.
414 * \param[in,out] attributes On success, the attributes of the key.
415 * On failure, equivalent to a
416 * freshly-initialized structure.
417 *
418 * \retval #PSA_SUCCESS
419 * \retval #PSA_ERROR_INVALID_HANDLE
420 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
421 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
422 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200423psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
424 psa_key_attributes_t *attributes);
425
Gilles Peskine20628592019-04-19 19:29:50 +0200426/** Reset a key attribute structure to a freshly initialized state.
427 *
428 * You must initialize the attribute structure as described in the
429 * documentation of the type #psa_key_attributes_t before calling this
430 * function. Once the structure has been initialized, you may call this
431 * function at any time.
432 *
433 * This function frees any auxiliary resources that the structure
434 * may contain.
435 *
436 * \param[in,out] attributes The attribute structure to reset.
437 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200438void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200439
Gilles Peskine87a5e562019-04-17 12:28:25 +0200440/**@}*/
441
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100442/** \defgroup key_management Key management
443 * @{
444 */
445
Gilles Peskinef535eb22018-11-30 14:08:36 +0100446/** Open a handle to an existing persistent key.
447 *
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100448 * Open a handle to a key which was previously created with
449 * psa_make_key_persistent() when setting its attributes.
Adrian L. Shaw52d83da2019-05-15 11:39:06 +0100450 * The handle should eventually be closed with psa_close_key()
451 * to release associated resources.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100452 *
Gilles Peskine4a231b82019-05-06 18:56:14 +0200453 * Implementations may provide additional keys that can be opened with
454 * psa_open_key(). Such keys have a key identifier in the vendor range,
455 * as documented in the description of #psa_key_id_t.
456 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100457 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100458 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100459 *
460 * \retval #PSA_SUCCESS
461 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100462 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100463 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200464 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100465 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine225010f2019-05-06 18:44:55 +0200466 * \p id is invalid.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100467 * \retval #PSA_ERROR_NOT_PERMITTED
468 * The specified key exists, but the application does not have the
469 * permission to access it. Note that this specification does not
470 * define any way to create such a key, but it may be possible
471 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200472 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
473 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100474 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200475psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100476 psa_key_handle_t *handle);
477
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100478
Gilles Peskinef535eb22018-11-30 14:08:36 +0100479/** Close a key handle.
480 *
481 * If the handle designates a volatile key, destroy the key material and
482 * free all associated resources, just like psa_destroy_key().
483 *
484 * If the handle designates a persistent key, free all resources associated
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100485 * with the key in volatile memory. The key in persistent storage is
Gilles Peskinef535eb22018-11-30 14:08:36 +0100486 * not affected and can be opened again later with psa_open_key().
487 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100488 * If the key is currently in use in a multipart operation,
489 * the multipart operation is aborted.
490 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100491 * \param handle The key handle to close.
492 *
493 * \retval #PSA_SUCCESS
494 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100495 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100496 */
497psa_status_t psa_close_key(psa_key_handle_t handle);
498
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100499/**@}*/
500
501/** \defgroup import_export Key import and export
502 * @{
503 */
504
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100505/**
506 * \brief Import a key in binary format.
507 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100508 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100509 * documentation of psa_export_public_key() for the format of public keys
510 * and to the documentation of psa_export_key() for the format for
511 * other key types.
512 *
513 * This specification supports a single format for each key type.
514 * Implementations may support other formats as long as the standard
515 * format is supported. Implementations that support other formats
516 * should ensure that the formats are clearly unambiguous so as to
517 * minimize the risk that an invalid input is accidentally interpreted
518 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100519 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100520
Gilles Peskine20628592019-04-19 19:29:50 +0200521 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200522 * The key size is always determined from the
523 * \p data buffer.
524 * If the key size in \p attributes is nonzero,
525 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200526 * \param[out] handle On success, a handle to the newly created key.
527 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100528 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200529 * buffer is interpreted according to the type declared
530 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200531 * All implementations must support at least the format
532 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100533 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200534 * the chosen type. Implementations may allow other
535 * formats, but should be conservative: implementations
536 * should err on the side of rejecting content if it
537 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200538 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100539 *
Gilles Peskine28538492018-07-11 17:34:00 +0200540 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100541 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100542 * If the key is persistent, the key material and the key's metadata
543 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200544 * \retval #PSA_ERROR_ALREADY_EXISTS
545 * This is an attempt to create a persistent key, and there is
546 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200547 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200548 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200549 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200550 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200551 * The key attributes, as a whole, are invalid.
552 * \retval #PSA_ERROR_INVALID_ARGUMENT
553 * The key data is not correctly formatted.
554 * \retval #PSA_ERROR_INVALID_ARGUMENT
555 * The size in \p attributes is nonzero and does not match the size
556 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200557 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
558 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
559 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100560 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200561 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200562 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300563 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300564 * The library has not been previously initialized by psa_crypto_init().
565 * It is implementation-dependent whether a failure to initialize
566 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100567 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200568psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100569 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200570 size_t data_length,
571 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100572
573/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100574 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200575 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100576 * This function destroys a key from both volatile
Gilles Peskine154bd952018-04-19 08:38:16 +0200577 * memory and, if applicable, non-volatile storage. Implementations shall
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100578 * make a best effort to ensure that that the key material cannot be recovered.
Gilles Peskine154bd952018-04-19 08:38:16 +0200579 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100580 * This function also erases any metadata such as policies and frees all
581 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200582 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100583 * \param handle Handle to the key to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100584 *
Gilles Peskine28538492018-07-11 17:34:00 +0200585 * \retval #PSA_SUCCESS
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100586 * The key material has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200587 * \retval #PSA_ERROR_NOT_PERMITTED
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100588 * The key cannot be erased because it is
Gilles Peskine65eb8582018-04-19 08:28:58 +0200589 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100590 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200591 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200592 * There was an failure in communication with the cryptoprocessor.
593 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200594 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200595 * The storage is corrupted. Implementations shall make a best effort
596 * to erase key material even in this stage, however applications
597 * should be aware that it may be impossible to guarantee that the
598 * key material is not recoverable in such cases.
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200599 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200600 * An unexpected condition which is not a storage corruption or
601 * a communication failure occurred. The cryptoprocessor may have
602 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300603 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300604 * The library has not been previously initialized by psa_crypto_init().
605 * It is implementation-dependent whether a failure to initialize
606 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100607 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100608psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100609
610/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100611 * \brief Export a key in binary format.
612 *
613 * The output of this function can be passed to psa_import_key() to
614 * create an equivalent object.
615 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100616 * If the implementation of psa_import_key() supports other formats
617 * beyond the format specified here, the output from psa_export_key()
618 * must use the representation specified here, not the original
619 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100620 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100621 * For standard key types, the output format is as follows:
622 *
623 * - For symmetric keys (including MAC keys), the format is the
624 * raw bytes of the key.
625 * - For DES, the key data consists of 8 bytes. The parity bits must be
626 * correct.
627 * - For Triple-DES, the format is the concatenation of the
628 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200629 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200630 * is the non-encrypted DER encoding of the representation defined by
631 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
632 * ```
633 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200634 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200635 * modulus INTEGER, -- n
636 * publicExponent INTEGER, -- e
637 * privateExponent INTEGER, -- d
638 * prime1 INTEGER, -- p
639 * prime2 INTEGER, -- q
640 * exponent1 INTEGER, -- d mod (p-1)
641 * exponent2 INTEGER, -- d mod (q-1)
642 * coefficient INTEGER, -- (inverse of q) mod p
643 * }
644 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200645 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200646 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100647 * a representation of the private value as a `ceiling(m/8)`-byte string
648 * where `m` is the bit size associated with the curve, i.e. the bit size
649 * of the order of the curve's coordinate field. This byte string is
650 * in little-endian order for Montgomery curves (curve types
651 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
652 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
653 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100654 * This is the content of the `privateKey` field of the `ECPrivateKey`
655 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200656 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200657 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000658 * format is the representation of the private key `x` as a big-endian byte
659 * string. The length of the byte string is the private key size in bytes
660 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200661 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
662 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100663 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200664 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
665 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100666 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200667 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200668 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200669 * \param[out] data_length On success, the number of bytes
670 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100671 *
Gilles Peskine28538492018-07-11 17:34:00 +0200672 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100673 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200674 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200675 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200676 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100677 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200678 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
679 * The size of the \p data buffer is too small. You can determine a
680 * sufficient buffer size by calling
681 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
682 * where \c type is the key type
683 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200684 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
685 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200686 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300687 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300688 * The library has not been previously initialized by psa_crypto_init().
689 * It is implementation-dependent whether a failure to initialize
690 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100691 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100692psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100693 uint8_t *data,
694 size_t data_size,
695 size_t *data_length);
696
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100697/**
698 * \brief Export a public key or the public part of a key pair in binary format.
699 *
700 * The output of this function can be passed to psa_import_key() to
701 * create an object that is equivalent to the public key.
702 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000703 * This specification supports a single format for each key type.
704 * Implementations may support other formats as long as the standard
705 * format is supported. Implementations that support other formats
706 * should ensure that the formats are clearly unambiguous so as to
707 * minimize the risk that an invalid input is accidentally interpreted
708 * according to a different format.
709 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000710 * For standard key types, the output format is as follows:
711 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
712 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
713 * ```
714 * RSAPublicKey ::= SEQUENCE {
715 * modulus INTEGER, -- n
716 * publicExponent INTEGER } -- e
717 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000718 * - For elliptic curve public keys (key types for which
719 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
720 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
721 * Let `m` be the bit size associated with the curve, i.e. the bit size of
722 * `q` for a curve over `F_q`. The representation consists of:
723 * - The byte 0x04;
724 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
725 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200726 * - For Diffie-Hellman key exchange public keys (key types for which
727 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000728 * the format is the representation of the public key `y = g^x mod p` as a
729 * big-endian byte string. The length of the byte string is the length of the
730 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100731 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200732 * Exporting a public key object or the public part of a key pair is
733 * always permitted, regardless of the key's usage flags.
734 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100735 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200736 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200737 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200738 * \param[out] data_length On success, the number of bytes
739 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100740 *
Gilles Peskine28538492018-07-11 17:34:00 +0200741 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100742 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200743 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200744 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200745 * The key is neither a public key nor a key pair.
746 * \retval #PSA_ERROR_NOT_SUPPORTED
747 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
748 * The size of the \p data buffer is too small. You can determine a
749 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200750 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200751 * where \c type is the key type
752 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200753 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
754 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200755 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300756 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300757 * The library has not been previously initialized by psa_crypto_init().
758 * It is implementation-dependent whether a failure to initialize
759 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100760 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100761psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100762 uint8_t *data,
763 size_t data_size,
764 size_t *data_length);
765
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100766/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100767 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100768 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000769 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100770 * This function is primarily useful to copy a key from one location
771 * to another, since it populates a key using the material from
772 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200773 *
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100774 * This function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100775 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100776 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200777 * The policy on the source key must have the usage flag
778 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200779 * This flag is sufficient to permit the copy if the key has the lifetime
780 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
781 * Some secure elements do not provide a way to copy a key without
782 * making it extractable from the secure element. If a key is located
783 * in such a secure element, then the key must have both usage flags
784 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
785 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200786 *
Gilles Peskine20628592019-04-19 19:29:50 +0200787 * The resulting key may only be used in a way that conforms to
788 * both the policy of the original key and the policy specified in
789 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100790 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200791 * usage flags on the source policy and the usage flags in \p attributes.
792 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100793 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200794 * - If either of the policies allows an algorithm and the other policy
795 * allows a wildcard-based algorithm policy that includes this algorithm,
796 * the resulting key allows the same algorithm.
797 * - If the policies do not allow any algorithm in common, this function
798 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200799 *
Gilles Peskine20628592019-04-19 19:29:50 +0200800 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100801 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200802 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100803 * \param source_handle The key to copy. It must be a valid key handle.
Gilles Peskine20628592019-04-19 19:29:50 +0200804 * \param[in] attributes The attributes for the new key.
805 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200806 * - The key type and size may be 0. If either is
807 * nonzero, it must match the corresponding
808 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200809 * - The key location (the lifetime and, for
810 * persistent keys, the key identifier) is
811 * used directly.
812 * - The policy constraints (usage flags and
813 * algorithm policy) are combined from
814 * the source key and \p attributes so that
815 * both sets of restrictions apply, as
816 * described in the documentation of this function.
817 * \param[out] target_handle On success, a handle to the newly created key.
818 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200819 *
820 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100821 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200822 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200823 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200824 * This is an attempt to create a persistent key, and there is
825 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200826 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200827 * The lifetime or identifier in \p attributes are invalid.
828 * \retval #PSA_ERROR_INVALID_ARGUMENT
829 * The policy constraints on the source and specified in
830 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200831 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200832 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200833 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100834 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200835 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
836 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100837 * The source key is not exportable and its lifetime does not
838 * allow copying it to the target's lifetime.
839 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
840 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200841 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
842 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200843 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100844 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100845psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200846 const psa_key_attributes_t *attributes,
847 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100848
849/**@}*/
850
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100851/** \defgroup hash Message digests
852 * @{
853 */
854
Gilles Peskine69647a42019-01-14 20:18:12 +0100855/** Calculate the hash (digest) of a message.
856 *
857 * \note To verify the hash of a message against an
858 * expected value, use psa_hash_compare() instead.
859 *
860 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
861 * such that #PSA_ALG_IS_HASH(\p alg) is true).
862 * \param[in] input Buffer containing the message to hash.
863 * \param input_length Size of the \p input buffer in bytes.
864 * \param[out] hash Buffer where the hash is to be written.
865 * \param hash_size Size of the \p hash buffer in bytes.
866 * \param[out] hash_length On success, the number of bytes
867 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100868 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100869 *
870 * \retval #PSA_SUCCESS
871 * Success.
872 * \retval #PSA_ERROR_NOT_SUPPORTED
873 * \p alg is not supported or is not a hash algorithm.
874 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
875 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
876 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200877 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100878 */
879psa_status_t psa_hash_compute(psa_algorithm_t alg,
880 const uint8_t *input,
881 size_t input_length,
882 uint8_t *hash,
883 size_t hash_size,
884 size_t *hash_length);
885
886/** Calculate the hash (digest) of a message and compare it with a
887 * reference value.
888 *
889 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
890 * such that #PSA_ALG_IS_HASH(\p alg) is true).
891 * \param[in] input Buffer containing the message to hash.
892 * \param input_length Size of the \p input buffer in bytes.
893 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100894 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100895 *
896 * \retval #PSA_SUCCESS
897 * The expected hash is identical to the actual hash of the input.
898 * \retval #PSA_ERROR_INVALID_SIGNATURE
899 * The hash of the message was calculated successfully, but it
900 * differs from the expected hash.
901 * \retval #PSA_ERROR_NOT_SUPPORTED
902 * \p alg is not supported or is not a hash algorithm.
903 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
904 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
905 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200906 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100907 */
908psa_status_t psa_hash_compare(psa_algorithm_t alg,
909 const uint8_t *input,
910 size_t input_length,
911 const uint8_t *hash,
912 const size_t hash_length);
913
Gilles Peskine308b91d2018-02-08 09:47:44 +0100914/** The type of the state data structure for multipart hash operations.
915 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000916 * Before calling any function on a hash operation object, the application must
917 * initialize it by any of the following means:
918 * - Set the structure to all-bits-zero, for example:
919 * \code
920 * psa_hash_operation_t operation;
921 * memset(&operation, 0, sizeof(operation));
922 * \endcode
923 * - Initialize the structure to logical zero values, for example:
924 * \code
925 * psa_hash_operation_t operation = {0};
926 * \endcode
927 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
928 * for example:
929 * \code
930 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
931 * \endcode
932 * - Assign the result of the function psa_hash_operation_init()
933 * to the structure, for example:
934 * \code
935 * psa_hash_operation_t operation;
936 * operation = psa_hash_operation_init();
937 * \endcode
938 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100939 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100940 * make any assumptions about the content of this structure except
941 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100942typedef struct psa_hash_operation_s psa_hash_operation_t;
943
Jaeden Amero6a25b412019-01-04 11:47:44 +0000944/** \def PSA_HASH_OPERATION_INIT
945 *
946 * This macro returns a suitable initializer for a hash operation object
947 * of type #psa_hash_operation_t.
948 */
949#ifdef __DOXYGEN_ONLY__
950/* This is an example definition for documentation purposes.
951 * Implementations should define a suitable value in `crypto_struct.h`.
952 */
953#define PSA_HASH_OPERATION_INIT {0}
954#endif
955
956/** Return an initial value for a hash operation object.
957 */
958static psa_hash_operation_t psa_hash_operation_init(void);
959
Gilles Peskinef45adda2019-01-14 18:29:18 +0100960/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100961 *
962 * The sequence of operations to calculate a hash (message digest)
963 * is as follows:
964 * -# Allocate an operation object which will be passed to all the functions
965 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000966 * -# Initialize the operation object with one of the methods described in the
967 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200968 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100969 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100970 * of the message each time. The hash that is calculated is the hash
971 * of the concatenation of these messages in order.
972 * -# To calculate the hash, call psa_hash_finish().
973 * To compare the hash with an expected value, call psa_hash_verify().
974 *
975 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000976 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100977 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200978 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100979 * eventually terminate the operation. The following events terminate an
980 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100981 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100982 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100983 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000984 * \param[in,out] operation The operation object to set up. It must have
985 * been initialized as per the documentation for
986 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200987 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
988 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100989 *
Gilles Peskine28538492018-07-11 17:34:00 +0200990 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100991 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200992 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200993 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100994 * \retval #PSA_ERROR_BAD_STATE
995 * The operation state is not valid (already set up and not
996 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200997 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
998 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
999 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001000 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001001 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001002psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001003 psa_algorithm_t alg);
1004
Gilles Peskine308b91d2018-02-08 09:47:44 +01001005/** Add a message fragment to a multipart hash operation.
1006 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001007 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001008 *
1009 * If this function returns an error status, the operation becomes inactive.
1010 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001011 * \param[in,out] operation Active hash operation.
1012 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001013 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001014 *
Gilles Peskine28538492018-07-11 17:34:00 +02001015 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001016 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001017 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001018 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001019 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1020 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1021 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001022 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001023 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001024psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1025 const uint8_t *input,
1026 size_t input_length);
1027
Gilles Peskine308b91d2018-02-08 09:47:44 +01001028/** Finish the calculation of the hash of a message.
1029 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001030 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001031 * This function calculates the hash of the message formed by concatenating
1032 * the inputs passed to preceding calls to psa_hash_update().
1033 *
1034 * When this function returns, the operation becomes inactive.
1035 *
1036 * \warning Applications should not call this function if they expect
1037 * a specific value for the hash. Call psa_hash_verify() instead.
1038 * Beware that comparing integrity or authenticity data such as
1039 * hash values with a function such as \c memcmp is risky
1040 * because the time taken by the comparison may leak information
1041 * about the hashed data which could allow an attacker to guess
1042 * a valid hash and thereby bypass security controls.
1043 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001044 * \param[in,out] operation Active hash operation.
1045 * \param[out] hash Buffer where the hash is to be written.
1046 * \param hash_size Size of the \p hash buffer in bytes.
1047 * \param[out] hash_length On success, the number of bytes
1048 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001049 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001050 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001051 *
Gilles Peskine28538492018-07-11 17:34:00 +02001052 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001053 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001054 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001055 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001056 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001057 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001058 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001059 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001060 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1061 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1062 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001063 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001064 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001065psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1066 uint8_t *hash,
1067 size_t hash_size,
1068 size_t *hash_length);
1069
Gilles Peskine308b91d2018-02-08 09:47:44 +01001070/** Finish the calculation of the hash of a message and compare it with
1071 * an expected value.
1072 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001073 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074 * This function calculates the hash of the message formed by concatenating
1075 * the inputs passed to preceding calls to psa_hash_update(). It then
1076 * compares the calculated hash with the expected hash passed as a
1077 * parameter to this function.
1078 *
1079 * When this function returns, the operation becomes inactive.
1080 *
Gilles Peskine19067982018-03-20 17:54:53 +01001081 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001082 * comparison between the actual hash and the expected hash is performed
1083 * in constant time.
1084 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001085 * \param[in,out] operation Active hash operation.
1086 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001087 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001088 *
Gilles Peskine28538492018-07-11 17:34:00 +02001089 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001090 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001091 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001092 * The hash of the message was calculated successfully, but it
1093 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001094 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001095 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001096 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1097 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1098 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001099 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001100 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001101psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1102 const uint8_t *hash,
1103 size_t hash_length);
1104
Gilles Peskine308b91d2018-02-08 09:47:44 +01001105/** Abort a hash operation.
1106 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001107 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001108 * \p operation structure itself. Once aborted, the operation object
1109 * can be reused for another operation by calling
1110 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001111 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001112 * You may call this function any time after the operation object has
1113 * been initialized by any of the following methods:
1114 * - A call to psa_hash_setup(), whether it succeeds or not.
1115 * - Initializing the \c struct to all-bits-zero.
1116 * - Initializing the \c struct to logical zeros, e.g.
1117 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001118 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001119 * In particular, calling psa_hash_abort() after the operation has been
1120 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1121 * psa_hash_verify() is safe and has no effect.
1122 *
1123 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001124 *
Gilles Peskine28538492018-07-11 17:34:00 +02001125 * \retval #PSA_SUCCESS
1126 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001127 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001128 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1129 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001130 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001131 */
1132psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001133
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001134/** Clone a hash operation.
1135 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001136 * This function copies the state of an ongoing hash operation to
1137 * a new operation object. In other words, this function is equivalent
1138 * to calling psa_hash_setup() on \p target_operation with the same
1139 * algorithm that \p source_operation was set up for, then
1140 * psa_hash_update() on \p target_operation with the same input that
1141 * that was passed to \p source_operation. After this function returns, the
1142 * two objects are independent, i.e. subsequent calls involving one of
1143 * the objects do not affect the other object.
1144 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001145 * \param[in] source_operation The active hash operation to clone.
1146 * \param[in,out] target_operation The operation object to set up.
1147 * It must be initialized but not active.
1148 *
1149 * \retval #PSA_SUCCESS
1150 * \retval #PSA_ERROR_BAD_STATE
1151 * \p source_operation is not an active hash operation.
1152 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001153 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001154 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1155 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001156 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001157 */
1158psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1159 psa_hash_operation_t *target_operation);
1160
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001161/**@}*/
1162
Gilles Peskine8c9def32018-02-08 10:02:12 +01001163/** \defgroup MAC Message authentication codes
1164 * @{
1165 */
1166
Gilles Peskine69647a42019-01-14 20:18:12 +01001167/** Calculate the MAC (message authentication code) of a message.
1168 *
1169 * \note To verify the MAC of a message against an
1170 * expected value, use psa_mac_verify() instead.
1171 * Beware that comparing integrity or authenticity data such as
1172 * MAC values with a function such as \c memcmp is risky
1173 * because the time taken by the comparison may leak information
1174 * about the MAC value which could allow an attacker to guess
1175 * a valid MAC and thereby bypass security controls.
1176 *
1177 * \param handle Handle to the key to use for the operation.
1178 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001179 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001180 * \param[in] input Buffer containing the input message.
1181 * \param input_length Size of the \p input buffer in bytes.
1182 * \param[out] mac Buffer where the MAC value is to be written.
1183 * \param mac_size Size of the \p mac buffer in bytes.
1184 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001185 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001186 *
1187 * \retval #PSA_SUCCESS
1188 * Success.
1189 * \retval #PSA_ERROR_INVALID_HANDLE
1190 * \retval #PSA_ERROR_EMPTY_SLOT
1191 * \retval #PSA_ERROR_NOT_PERMITTED
1192 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001193 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001194 * \retval #PSA_ERROR_NOT_SUPPORTED
1195 * \p alg is not supported or is not a MAC algorithm.
1196 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1197 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1198 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001199 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001200 * \retval #PSA_ERROR_BAD_STATE
1201 * The library has not been previously initialized by psa_crypto_init().
1202 * It is implementation-dependent whether a failure to initialize
1203 * results in this error code.
1204 */
1205psa_status_t psa_mac_compute(psa_key_handle_t handle,
1206 psa_algorithm_t alg,
1207 const uint8_t *input,
1208 size_t input_length,
1209 uint8_t *mac,
1210 size_t mac_size,
1211 size_t *mac_length);
1212
1213/** Calculate the MAC of a message and compare it with a reference value.
1214 *
1215 * \param handle Handle to the key to use for the operation.
1216 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001217 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001218 * \param[in] input Buffer containing the input message.
1219 * \param input_length Size of the \p input buffer in bytes.
1220 * \param[out] mac Buffer containing the expected MAC value.
1221 * \param mac_length Size of the \p mac buffer in bytes.
1222 *
1223 * \retval #PSA_SUCCESS
1224 * The expected MAC is identical to the actual MAC of the input.
1225 * \retval #PSA_ERROR_INVALID_SIGNATURE
1226 * The MAC of the message was calculated successfully, but it
1227 * differs from the expected value.
1228 * \retval #PSA_ERROR_INVALID_HANDLE
1229 * \retval #PSA_ERROR_EMPTY_SLOT
1230 * \retval #PSA_ERROR_NOT_PERMITTED
1231 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001232 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001233 * \retval #PSA_ERROR_NOT_SUPPORTED
1234 * \p alg is not supported or is not a MAC algorithm.
1235 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1236 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1237 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001238 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001239 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001240psa_status_t psa_mac_verify(psa_key_handle_t handle,
1241 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001242 const uint8_t *input,
1243 size_t input_length,
1244 const uint8_t *mac,
1245 const size_t mac_length);
1246
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001247/** The type of the state data structure for multipart MAC operations.
1248 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001249 * Before calling any function on a MAC operation object, the application must
1250 * initialize it by any of the following means:
1251 * - Set the structure to all-bits-zero, for example:
1252 * \code
1253 * psa_mac_operation_t operation;
1254 * memset(&operation, 0, sizeof(operation));
1255 * \endcode
1256 * - Initialize the structure to logical zero values, for example:
1257 * \code
1258 * psa_mac_operation_t operation = {0};
1259 * \endcode
1260 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1261 * for example:
1262 * \code
1263 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1264 * \endcode
1265 * - Assign the result of the function psa_mac_operation_init()
1266 * to the structure, for example:
1267 * \code
1268 * psa_mac_operation_t operation;
1269 * operation = psa_mac_operation_init();
1270 * \endcode
1271 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001272 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001273 * make any assumptions about the content of this structure except
1274 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001275typedef struct psa_mac_operation_s psa_mac_operation_t;
1276
Jaeden Amero769ce272019-01-04 11:48:03 +00001277/** \def PSA_MAC_OPERATION_INIT
1278 *
1279 * This macro returns a suitable initializer for a MAC operation object of type
1280 * #psa_mac_operation_t.
1281 */
1282#ifdef __DOXYGEN_ONLY__
1283/* This is an example definition for documentation purposes.
1284 * Implementations should define a suitable value in `crypto_struct.h`.
1285 */
1286#define PSA_MAC_OPERATION_INIT {0}
1287#endif
1288
1289/** Return an initial value for a MAC operation object.
1290 */
1291static psa_mac_operation_t psa_mac_operation_init(void);
1292
Gilles Peskinef45adda2019-01-14 18:29:18 +01001293/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001294 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001295 * This function sets up the calculation of the MAC
1296 * (message authentication code) of a byte string.
1297 * To verify the MAC of a message against an
1298 * expected value, use psa_mac_verify_setup() instead.
1299 *
1300 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001301 * -# Allocate an operation object which will be passed to all the functions
1302 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001303 * -# Initialize the operation object with one of the methods described in the
1304 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001305 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001306 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1307 * of the message each time. The MAC that is calculated is the MAC
1308 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001309 * -# At the end of the message, call psa_mac_sign_finish() to finish
1310 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001311 *
1312 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001313 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001314 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001315 * After a successful call to psa_mac_sign_setup(), the application must
1316 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001317 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001318 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001319 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001320 * \param[in,out] operation The operation object to set up. It must have
1321 * been initialized as per the documentation for
1322 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001323 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001324 * It must remain valid until the operation
1325 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001326 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001327 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001328 *
Gilles Peskine28538492018-07-11 17:34:00 +02001329 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001330 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001331 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001332 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001333 * \retval #PSA_ERROR_NOT_PERMITTED
1334 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001335 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001336 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001337 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001338 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1339 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1340 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001341 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001342 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001343 * The operation state is not valid (already set up and not
1344 * subsequently completed).
1345 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001346 * The library has not been previously initialized by psa_crypto_init().
1347 * It is implementation-dependent whether a failure to initialize
1348 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001349 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001350psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001351 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001352 psa_algorithm_t alg);
1353
Gilles Peskinef45adda2019-01-14 18:29:18 +01001354/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001355 *
1356 * This function sets up the verification of the MAC
1357 * (message authentication code) of a byte string against an expected value.
1358 *
1359 * The sequence of operations to verify a MAC is as follows:
1360 * -# Allocate an operation object which will be passed to all the functions
1361 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001362 * -# Initialize the operation object with one of the methods described in the
1363 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001364 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001365 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1366 * of the message each time. The MAC that is calculated is the MAC
1367 * of the concatenation of these messages in order.
1368 * -# At the end of the message, call psa_mac_verify_finish() to finish
1369 * calculating the actual MAC of the message and verify it against
1370 * the expected value.
1371 *
1372 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001373 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001374 *
1375 * After a successful call to psa_mac_verify_setup(), the application must
1376 * eventually terminate the operation through one of the following methods:
1377 * - A failed call to psa_mac_update().
1378 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1379 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001380 * \param[in,out] operation The operation object to set up. It must have
1381 * been initialized as per the documentation for
1382 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001383 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001384 * It must remain valid until the operation
1385 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001386 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1387 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001388 *
Gilles Peskine28538492018-07-11 17:34:00 +02001389 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001390 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001391 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001392 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001393 * \retval #PSA_ERROR_NOT_PERMITTED
1394 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001395 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001396 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001397 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001398 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1399 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1400 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001401 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001402 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001403 * The operation state is not valid (already set up and not
1404 * subsequently completed).
1405 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001406 * The library has not been previously initialized by psa_crypto_init().
1407 * It is implementation-dependent whether a failure to initialize
1408 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001409 */
1410psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001411 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001412 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001413
Gilles Peskinedcd14942018-07-12 00:30:52 +02001414/** Add a message fragment to a multipart MAC operation.
1415 *
1416 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1417 * before calling this function.
1418 *
1419 * If this function returns an error status, the operation becomes inactive.
1420 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001421 * \param[in,out] operation Active MAC operation.
1422 * \param[in] input Buffer containing the message fragment to add to
1423 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001424 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001425 *
1426 * \retval #PSA_SUCCESS
1427 * Success.
1428 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001429 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001430 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1431 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1432 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001433 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001434 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001435psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1436 const uint8_t *input,
1437 size_t input_length);
1438
Gilles Peskinedcd14942018-07-12 00:30:52 +02001439/** Finish the calculation of the MAC of a message.
1440 *
1441 * The application must call psa_mac_sign_setup() before calling this function.
1442 * This function calculates the MAC of the message formed by concatenating
1443 * the inputs passed to preceding calls to psa_mac_update().
1444 *
1445 * When this function returns, the operation becomes inactive.
1446 *
1447 * \warning Applications should not call this function if they expect
1448 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1449 * Beware that comparing integrity or authenticity data such as
1450 * MAC values with a function such as \c memcmp is risky
1451 * because the time taken by the comparison may leak information
1452 * about the MAC value which could allow an attacker to guess
1453 * a valid MAC and thereby bypass security controls.
1454 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001455 * \param[in,out] operation Active MAC operation.
1456 * \param[out] mac Buffer where the MAC value is to be written.
1457 * \param mac_size Size of the \p mac buffer in bytes.
1458 * \param[out] mac_length On success, the number of bytes
1459 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001460 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001461 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001462 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001463 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001464 *
1465 * \retval #PSA_SUCCESS
1466 * Success.
1467 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001468 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001469 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001470 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001471 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1472 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1473 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1474 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001475 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001476 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001477psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1478 uint8_t *mac,
1479 size_t mac_size,
1480 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001481
Gilles Peskinedcd14942018-07-12 00:30:52 +02001482/** Finish the calculation of the MAC of a message and compare it with
1483 * an expected value.
1484 *
1485 * The application must call psa_mac_verify_setup() before calling this function.
1486 * This function calculates the MAC of the message formed by concatenating
1487 * the inputs passed to preceding calls to psa_mac_update(). It then
1488 * compares the calculated MAC with the expected MAC passed as a
1489 * parameter to this function.
1490 *
1491 * When this function returns, the operation becomes inactive.
1492 *
1493 * \note Implementations shall make the best effort to ensure that the
1494 * comparison between the actual MAC and the expected MAC is performed
1495 * in constant time.
1496 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001497 * \param[in,out] operation Active MAC operation.
1498 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001499 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001500 *
1501 * \retval #PSA_SUCCESS
1502 * The expected MAC is identical to the actual MAC of the message.
1503 * \retval #PSA_ERROR_INVALID_SIGNATURE
1504 * The MAC of the message was calculated successfully, but it
1505 * differs from the expected MAC.
1506 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001507 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001508 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1509 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1510 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001511 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001512 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001513psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1514 const uint8_t *mac,
1515 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001516
Gilles Peskinedcd14942018-07-12 00:30:52 +02001517/** Abort a MAC operation.
1518 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001519 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001520 * \p operation structure itself. Once aborted, the operation object
1521 * can be reused for another operation by calling
1522 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001523 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001524 * You may call this function any time after the operation object has
1525 * been initialized by any of the following methods:
1526 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1527 * it succeeds or not.
1528 * - Initializing the \c struct to all-bits-zero.
1529 * - Initializing the \c struct to logical zeros, e.g.
1530 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001531 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001532 * In particular, calling psa_mac_abort() after the operation has been
1533 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1534 * psa_mac_verify_finish() is safe and has no effect.
1535 *
1536 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001537 *
1538 * \retval #PSA_SUCCESS
1539 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001540 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001541 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1542 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001543 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001544 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001545psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1546
1547/**@}*/
1548
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001549/** \defgroup cipher Symmetric ciphers
1550 * @{
1551 */
1552
Gilles Peskine69647a42019-01-14 20:18:12 +01001553/** Encrypt a message using a symmetric cipher.
1554 *
1555 * This function encrypts a message with a random IV (initialization
1556 * vector).
1557 *
1558 * \param handle Handle to the key to use for the operation.
1559 * It must remain valid until the operation
1560 * terminates.
1561 * \param alg The cipher algorithm to compute
1562 * (\c PSA_ALG_XXX value such that
1563 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1564 * \param[in] input Buffer containing the message to encrypt.
1565 * \param input_length Size of the \p input buffer in bytes.
1566 * \param[out] output Buffer where the output is to be written.
1567 * The output contains the IV followed by
1568 * the ciphertext proper.
1569 * \param output_size Size of the \p output buffer in bytes.
1570 * \param[out] output_length On success, the number of bytes
1571 * that make up the output.
1572 *
1573 * \retval #PSA_SUCCESS
1574 * Success.
1575 * \retval #PSA_ERROR_INVALID_HANDLE
1576 * \retval #PSA_ERROR_EMPTY_SLOT
1577 * \retval #PSA_ERROR_NOT_PERMITTED
1578 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001579 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001580 * \retval #PSA_ERROR_NOT_SUPPORTED
1581 * \p alg is not supported or is not a cipher algorithm.
1582 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1583 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1584 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1585 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001586 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001587 */
1588psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1589 psa_algorithm_t alg,
1590 const uint8_t *input,
1591 size_t input_length,
1592 uint8_t *output,
1593 size_t output_size,
1594 size_t *output_length);
1595
1596/** Decrypt a message using a symmetric cipher.
1597 *
1598 * This function decrypts a message encrypted with a symmetric cipher.
1599 *
1600 * \param handle Handle to the key to use for the operation.
1601 * It must remain valid until the operation
1602 * terminates.
1603 * \param alg The cipher algorithm to compute
1604 * (\c PSA_ALG_XXX value such that
1605 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1606 * \param[in] input Buffer containing the message to decrypt.
1607 * This consists of the IV followed by the
1608 * ciphertext proper.
1609 * \param input_length Size of the \p input buffer in bytes.
1610 * \param[out] output Buffer where the plaintext is to be written.
1611 * \param output_size Size of the \p output buffer in bytes.
1612 * \param[out] output_length On success, the number of bytes
1613 * that make up the output.
1614 *
1615 * \retval #PSA_SUCCESS
1616 * Success.
1617 * \retval #PSA_ERROR_INVALID_HANDLE
1618 * \retval #PSA_ERROR_EMPTY_SLOT
1619 * \retval #PSA_ERROR_NOT_PERMITTED
1620 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001621 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001622 * \retval #PSA_ERROR_NOT_SUPPORTED
1623 * \p alg is not supported or is not a cipher algorithm.
1624 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1625 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1626 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1627 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001628 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001629 */
1630psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1631 psa_algorithm_t alg,
1632 const uint8_t *input,
1633 size_t input_length,
1634 uint8_t *output,
1635 size_t output_size,
1636 size_t *output_length);
1637
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001638/** The type of the state data structure for multipart cipher operations.
1639 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001640 * Before calling any function on a cipher operation object, the application
1641 * must initialize it by any of the following means:
1642 * - Set the structure to all-bits-zero, for example:
1643 * \code
1644 * psa_cipher_operation_t operation;
1645 * memset(&operation, 0, sizeof(operation));
1646 * \endcode
1647 * - Initialize the structure to logical zero values, for example:
1648 * \code
1649 * psa_cipher_operation_t operation = {0};
1650 * \endcode
1651 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1652 * for example:
1653 * \code
1654 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1655 * \endcode
1656 * - Assign the result of the function psa_cipher_operation_init()
1657 * to the structure, for example:
1658 * \code
1659 * psa_cipher_operation_t operation;
1660 * operation = psa_cipher_operation_init();
1661 * \endcode
1662 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001663 * This is an implementation-defined \c struct. Applications should not
1664 * make any assumptions about the content of this structure except
1665 * as directed by the documentation of a specific implementation. */
1666typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1667
Jaeden Amero5bae2272019-01-04 11:48:27 +00001668/** \def PSA_CIPHER_OPERATION_INIT
1669 *
1670 * This macro returns a suitable initializer for a cipher operation object of
1671 * type #psa_cipher_operation_t.
1672 */
1673#ifdef __DOXYGEN_ONLY__
1674/* This is an example definition for documentation purposes.
1675 * Implementations should define a suitable value in `crypto_struct.h`.
1676 */
1677#define PSA_CIPHER_OPERATION_INIT {0}
1678#endif
1679
1680/** Return an initial value for a cipher operation object.
1681 */
1682static psa_cipher_operation_t psa_cipher_operation_init(void);
1683
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001684/** Set the key for a multipart symmetric encryption operation.
1685 *
1686 * The sequence of operations to encrypt a message with a symmetric cipher
1687 * is as follows:
1688 * -# Allocate an operation object which will be passed to all the functions
1689 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001690 * -# Initialize the operation object with one of the methods described in the
1691 * documentation for #psa_cipher_operation_t, e.g.
1692 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001693 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001694 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001695 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001696 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001697 * requires a specific IV value.
1698 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1699 * of the message each time.
1700 * -# Call psa_cipher_finish().
1701 *
1702 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001703 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001704 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001705 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001706 * eventually terminate the operation. The following events terminate an
1707 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001708 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001709 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001710 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001711 * \param[in,out] operation The operation object to set up. It must have
1712 * been initialized as per the documentation for
1713 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001714 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001715 * It must remain valid until the operation
1716 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001717 * \param alg The cipher algorithm to compute
1718 * (\c PSA_ALG_XXX value such that
1719 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001720 *
Gilles Peskine28538492018-07-11 17:34:00 +02001721 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001722 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001723 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001724 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001725 * \retval #PSA_ERROR_NOT_PERMITTED
1726 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001727 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001728 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001729 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001730 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1731 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1732 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001733 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001734 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001735 * The operation state is not valid (already set up and not
1736 * subsequently completed).
1737 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001738 * The library has not been previously initialized by psa_crypto_init().
1739 * It is implementation-dependent whether a failure to initialize
1740 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001741 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001742psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001743 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001744 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001745
1746/** Set the key for a multipart symmetric decryption operation.
1747 *
1748 * The sequence of operations to decrypt a message with a symmetric cipher
1749 * is as follows:
1750 * -# Allocate an operation object which will be passed to all the functions
1751 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001752 * -# Initialize the operation object with one of the methods described in the
1753 * documentation for #psa_cipher_operation_t, e.g.
1754 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001755 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001756 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001757 * decryption. If the IV is prepended to the ciphertext, you can call
1758 * psa_cipher_update() on a buffer containing the IV followed by the
1759 * beginning of the message.
1760 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1761 * of the message each time.
1762 * -# Call psa_cipher_finish().
1763 *
1764 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001765 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001766 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001767 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001768 * eventually terminate the operation. The following events terminate an
1769 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001770 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001771 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001772 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001773 * \param[in,out] operation The operation object to set up. It must have
1774 * been initialized as per the documentation for
1775 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001776 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001777 * It must remain valid until the operation
1778 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001779 * \param alg The cipher algorithm to compute
1780 * (\c PSA_ALG_XXX value such that
1781 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001782 *
Gilles Peskine28538492018-07-11 17:34:00 +02001783 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001784 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001785 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001786 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001787 * \retval #PSA_ERROR_NOT_PERMITTED
1788 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001789 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001790 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001791 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001792 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1793 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1794 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001795 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001796 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001797 * The operation state is not valid (already set up and not
1798 * subsequently completed).
1799 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001800 * The library has not been previously initialized by psa_crypto_init().
1801 * It is implementation-dependent whether a failure to initialize
1802 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001803 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001804psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001805 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001806 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001807
Gilles Peskinedcd14942018-07-12 00:30:52 +02001808/** Generate an IV for a symmetric encryption operation.
1809 *
1810 * This function generates a random IV (initialization vector), nonce
1811 * or initial counter value for the encryption operation as appropriate
1812 * for the chosen algorithm, key type and key size.
1813 *
1814 * The application must call psa_cipher_encrypt_setup() before
1815 * calling this function.
1816 *
1817 * If this function returns an error status, the operation becomes inactive.
1818 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001819 * \param[in,out] operation Active cipher operation.
1820 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001821 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001822 * \param[out] iv_length On success, the number of bytes of the
1823 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001824 *
1825 * \retval #PSA_SUCCESS
1826 * Success.
1827 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001828 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001829 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001830 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001831 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1832 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1833 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001834 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001835 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001836psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1837 unsigned char *iv,
1838 size_t iv_size,
1839 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001840
Gilles Peskinedcd14942018-07-12 00:30:52 +02001841/** Set the IV for a symmetric encryption or decryption operation.
1842 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001843 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001844 * or initial counter value for the encryption or decryption operation.
1845 *
1846 * The application must call psa_cipher_encrypt_setup() before
1847 * calling this function.
1848 *
1849 * If this function returns an error status, the operation becomes inactive.
1850 *
1851 * \note When encrypting, applications should use psa_cipher_generate_iv()
1852 * instead of this function, unless implementing a protocol that requires
1853 * a non-random IV.
1854 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001855 * \param[in,out] operation Active cipher operation.
1856 * \param[in] iv Buffer containing the IV to use.
1857 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001858 *
1859 * \retval #PSA_SUCCESS
1860 * Success.
1861 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001862 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001863 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001864 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001865 * or the chosen algorithm does not use an IV.
1866 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1867 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1868 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001869 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001870 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001871psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1872 const unsigned char *iv,
1873 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001874
Gilles Peskinedcd14942018-07-12 00:30:52 +02001875/** Encrypt or decrypt a message fragment in an active cipher operation.
1876 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001877 * Before calling this function, you must:
1878 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1879 * The choice of setup function determines whether this function
1880 * encrypts or decrypts its input.
1881 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1882 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001883 *
1884 * If this function returns an error status, the operation becomes inactive.
1885 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001886 * \param[in,out] operation Active cipher operation.
1887 * \param[in] input Buffer containing the message fragment to
1888 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001889 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001890 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001891 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001892 * \param[out] output_length On success, the number of bytes
1893 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001894 *
1895 * \retval #PSA_SUCCESS
1896 * Success.
1897 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001898 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001899 * not set, or already completed).
1900 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1901 * The size of the \p output buffer is too small.
1902 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1903 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1904 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001905 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001906 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001907psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1908 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001909 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001910 unsigned char *output,
1911 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001912 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001913
Gilles Peskinedcd14942018-07-12 00:30:52 +02001914/** Finish encrypting or decrypting a message in a cipher operation.
1915 *
1916 * The application must call psa_cipher_encrypt_setup() or
1917 * psa_cipher_decrypt_setup() before calling this function. The choice
1918 * of setup function determines whether this function encrypts or
1919 * decrypts its input.
1920 *
1921 * This function finishes the encryption or decryption of the message
1922 * formed by concatenating the inputs passed to preceding calls to
1923 * psa_cipher_update().
1924 *
1925 * When this function returns, the operation becomes inactive.
1926 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001927 * \param[in,out] operation Active cipher operation.
1928 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001929 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001930 * \param[out] output_length On success, the number of bytes
1931 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001932 *
1933 * \retval #PSA_SUCCESS
1934 * Success.
1935 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001936 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001937 * not set, or already completed).
1938 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1939 * The size of the \p output buffer is too small.
1940 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1941 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1942 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001943 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001944 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001945psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001946 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001947 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001948 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001949
Gilles Peskinedcd14942018-07-12 00:30:52 +02001950/** Abort a cipher operation.
1951 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001952 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001953 * \p operation structure itself. Once aborted, the operation object
1954 * can be reused for another operation by calling
1955 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001956 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001957 * You may call this function any time after the operation object has
1958 * been initialized by any of the following methods:
1959 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1960 * whether it succeeds or not.
1961 * - Initializing the \c struct to all-bits-zero.
1962 * - Initializing the \c struct to logical zeros, e.g.
1963 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001964 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001965 * In particular, calling psa_cipher_abort() after the operation has been
1966 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1967 * is safe and has no effect.
1968 *
1969 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001970 *
1971 * \retval #PSA_SUCCESS
1972 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001973 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001974 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1975 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001976 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001977 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001978psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1979
1980/**@}*/
1981
Gilles Peskine3b555712018-03-03 21:27:57 +01001982/** \defgroup aead Authenticated encryption with associated data (AEAD)
1983 * @{
1984 */
1985
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001986/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001987 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001988 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001989 * \param alg The AEAD algorithm to compute
1990 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001991 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001992 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001993 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001994 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001995 * but not encrypted.
1996 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001997 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001998 * encrypted.
1999 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002000 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002001 * encrypted data. The additional data is not
2002 * part of this output. For algorithms where the
2003 * encrypted data and the authentication tag
2004 * are defined as separate outputs, the
2005 * authentication tag is appended to the
2006 * encrypted data.
2007 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2008 * This must be at least
2009 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2010 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002011 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002012 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002013 *
Gilles Peskine28538492018-07-11 17:34:00 +02002014 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002015 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002016 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002017 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002018 * \retval #PSA_ERROR_NOT_PERMITTED
2019 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002020 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002021 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002022 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002023 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2024 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2025 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002026 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002027 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002028 * The library has not been previously initialized by psa_crypto_init().
2029 * It is implementation-dependent whether a failure to initialize
2030 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002031 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002032psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002033 psa_algorithm_t alg,
2034 const uint8_t *nonce,
2035 size_t nonce_length,
2036 const uint8_t *additional_data,
2037 size_t additional_data_length,
2038 const uint8_t *plaintext,
2039 size_t plaintext_length,
2040 uint8_t *ciphertext,
2041 size_t ciphertext_size,
2042 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002043
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002044/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002045 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002046 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002047 * \param alg The AEAD algorithm to compute
2048 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002049 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002050 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002051 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002052 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002053 * but not encrypted.
2054 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002055 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002056 * encrypted. For algorithms where the
2057 * encrypted data and the authentication tag
2058 * are defined as separate inputs, the buffer
2059 * must contain the encrypted data followed
2060 * by the authentication tag.
2061 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002062 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002063 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2064 * This must be at least
2065 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2066 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002067 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002068 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002069 *
Gilles Peskine28538492018-07-11 17:34:00 +02002070 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002071 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002072 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002073 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002074 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002075 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002076 * \retval #PSA_ERROR_NOT_PERMITTED
2077 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002078 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002079 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002080 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002081 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2082 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2083 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002084 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002085 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002086 * The library has not been previously initialized by psa_crypto_init().
2087 * It is implementation-dependent whether a failure to initialize
2088 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002089 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002090psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002091 psa_algorithm_t alg,
2092 const uint8_t *nonce,
2093 size_t nonce_length,
2094 const uint8_t *additional_data,
2095 size_t additional_data_length,
2096 const uint8_t *ciphertext,
2097 size_t ciphertext_length,
2098 uint8_t *plaintext,
2099 size_t plaintext_size,
2100 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002101
Gilles Peskine30a9e412019-01-14 18:36:12 +01002102/** The type of the state data structure for multipart AEAD operations.
2103 *
2104 * Before calling any function on an AEAD operation object, the application
2105 * must initialize it by any of the following means:
2106 * - Set the structure to all-bits-zero, for example:
2107 * \code
2108 * psa_aead_operation_t operation;
2109 * memset(&operation, 0, sizeof(operation));
2110 * \endcode
2111 * - Initialize the structure to logical zero values, for example:
2112 * \code
2113 * psa_aead_operation_t operation = {0};
2114 * \endcode
2115 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2116 * for example:
2117 * \code
2118 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2119 * \endcode
2120 * - Assign the result of the function psa_aead_operation_init()
2121 * to the structure, for example:
2122 * \code
2123 * psa_aead_operation_t operation;
2124 * operation = psa_aead_operation_init();
2125 * \endcode
2126 *
2127 * This is an implementation-defined \c struct. Applications should not
2128 * make any assumptions about the content of this structure except
2129 * as directed by the documentation of a specific implementation. */
2130typedef struct psa_aead_operation_s psa_aead_operation_t;
2131
2132/** \def PSA_AEAD_OPERATION_INIT
2133 *
2134 * This macro returns a suitable initializer for an AEAD operation object of
2135 * type #psa_aead_operation_t.
2136 */
2137#ifdef __DOXYGEN_ONLY__
2138/* This is an example definition for documentation purposes.
2139 * Implementations should define a suitable value in `crypto_struct.h`.
2140 */
2141#define PSA_AEAD_OPERATION_INIT {0}
2142#endif
2143
2144/** Return an initial value for an AEAD operation object.
2145 */
2146static psa_aead_operation_t psa_aead_operation_init(void);
2147
2148/** Set the key for a multipart authenticated encryption operation.
2149 *
2150 * The sequence of operations to encrypt a message with authentication
2151 * is as follows:
2152 * -# Allocate an operation object which will be passed to all the functions
2153 * listed here.
2154 * -# Initialize the operation object with one of the methods described in the
2155 * documentation for #psa_aead_operation_t, e.g.
2156 * PSA_AEAD_OPERATION_INIT.
2157 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002158 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2159 * inputs to the subsequent calls to psa_aead_update_ad() and
2160 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2161 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002162 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2163 * generate or set the nonce. You should use
2164 * psa_aead_generate_nonce() unless the protocol you are implementing
2165 * requires a specific nonce value.
2166 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2167 * of the non-encrypted additional authenticated data each time.
2168 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002169 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002170 * -# Call psa_aead_finish().
2171 *
2172 * The application may call psa_aead_abort() at any time after the operation
2173 * has been initialized.
2174 *
2175 * After a successful call to psa_aead_encrypt_setup(), the application must
2176 * eventually terminate the operation. The following events terminate an
2177 * operation:
2178 * - A failed call to any of the \c psa_aead_xxx functions.
2179 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2180 *
2181 * \param[in,out] operation The operation object to set up. It must have
2182 * been initialized as per the documentation for
2183 * #psa_aead_operation_t and not yet in use.
2184 * \param handle Handle to the key to use for the operation.
2185 * It must remain valid until the operation
2186 * terminates.
2187 * \param alg The AEAD algorithm to compute
2188 * (\c PSA_ALG_XXX value such that
2189 * #PSA_ALG_IS_AEAD(\p alg) is true).
2190 *
2191 * \retval #PSA_SUCCESS
2192 * Success.
2193 * \retval #PSA_ERROR_INVALID_HANDLE
2194 * \retval #PSA_ERROR_EMPTY_SLOT
2195 * \retval #PSA_ERROR_NOT_PERMITTED
2196 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002197 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002198 * \retval #PSA_ERROR_NOT_SUPPORTED
2199 * \p alg is not supported or is not an AEAD algorithm.
2200 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2201 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2202 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002203 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002204 * \retval #PSA_ERROR_BAD_STATE
2205 * The library has not been previously initialized by psa_crypto_init().
2206 * It is implementation-dependent whether a failure to initialize
2207 * results in this error code.
2208 */
2209psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2210 psa_key_handle_t handle,
2211 psa_algorithm_t alg);
2212
2213/** Set the key for a multipart authenticated decryption operation.
2214 *
2215 * The sequence of operations to decrypt a message with authentication
2216 * is as follows:
2217 * -# Allocate an operation object which will be passed to all the functions
2218 * listed here.
2219 * -# Initialize the operation object with one of the methods described in the
2220 * documentation for #psa_aead_operation_t, e.g.
2221 * PSA_AEAD_OPERATION_INIT.
2222 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002223 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2224 * inputs to the subsequent calls to psa_aead_update_ad() and
2225 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2226 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002227 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2228 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2229 * of the non-encrypted additional authenticated data each time.
2230 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002231 * of the ciphertext to decrypt each time.
2232 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002233 *
2234 * The application may call psa_aead_abort() at any time after the operation
2235 * has been initialized.
2236 *
2237 * After a successful call to psa_aead_decrypt_setup(), the application must
2238 * eventually terminate the operation. The following events terminate an
2239 * operation:
2240 * - A failed call to any of the \c psa_aead_xxx functions.
2241 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2242 *
2243 * \param[in,out] operation The operation object to set up. It must have
2244 * been initialized as per the documentation for
2245 * #psa_aead_operation_t and not yet in use.
2246 * \param handle Handle to the key to use for the operation.
2247 * It must remain valid until the operation
2248 * terminates.
2249 * \param alg The AEAD algorithm to compute
2250 * (\c PSA_ALG_XXX value such that
2251 * #PSA_ALG_IS_AEAD(\p alg) is true).
2252 *
2253 * \retval #PSA_SUCCESS
2254 * Success.
2255 * \retval #PSA_ERROR_INVALID_HANDLE
2256 * \retval #PSA_ERROR_EMPTY_SLOT
2257 * \retval #PSA_ERROR_NOT_PERMITTED
2258 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002259 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002260 * \retval #PSA_ERROR_NOT_SUPPORTED
2261 * \p alg is not supported or is not an AEAD algorithm.
2262 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2263 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2264 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002265 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002266 * \retval #PSA_ERROR_BAD_STATE
2267 * The library has not been previously initialized by psa_crypto_init().
2268 * It is implementation-dependent whether a failure to initialize
2269 * results in this error code.
2270 */
2271psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2272 psa_key_handle_t handle,
2273 psa_algorithm_t alg);
2274
2275/** Generate a random nonce for an authenticated encryption operation.
2276 *
2277 * This function generates a random nonce for the authenticated encryption
2278 * operation with an appropriate size for the chosen algorithm, key type
2279 * and key size.
2280 *
2281 * The application must call psa_aead_encrypt_setup() before
2282 * calling this function.
2283 *
2284 * If this function returns an error status, the operation becomes inactive.
2285 *
2286 * \param[in,out] operation Active AEAD operation.
2287 * \param[out] nonce Buffer where the generated nonce is to be
2288 * written.
2289 * \param nonce_size Size of the \p nonce buffer in bytes.
2290 * \param[out] nonce_length On success, the number of bytes of the
2291 * generated nonce.
2292 *
2293 * \retval #PSA_SUCCESS
2294 * Success.
2295 * \retval #PSA_ERROR_BAD_STATE
2296 * The operation state is not valid (not set up, or nonce already set).
2297 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2298 * The size of the \p nonce buffer is too small.
2299 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2300 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2301 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002302 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002303 */
2304psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2305 unsigned char *nonce,
2306 size_t nonce_size,
2307 size_t *nonce_length);
2308
2309/** Set the nonce for an authenticated encryption or decryption operation.
2310 *
2311 * This function sets the nonce for the authenticated
2312 * encryption or decryption operation.
2313 *
2314 * The application must call psa_aead_encrypt_setup() before
2315 * calling this function.
2316 *
2317 * If this function returns an error status, the operation becomes inactive.
2318 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002319 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002320 * instead of this function, unless implementing a protocol that requires
2321 * a non-random IV.
2322 *
2323 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002324 * \param[in] nonce Buffer containing the nonce to use.
2325 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002326 *
2327 * \retval #PSA_SUCCESS
2328 * Success.
2329 * \retval #PSA_ERROR_BAD_STATE
2330 * The operation state is not valid (not set up, or nonce already set).
2331 * \retval #PSA_ERROR_INVALID_ARGUMENT
2332 * The size of \p nonce is not acceptable for the chosen algorithm.
2333 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2334 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2335 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002336 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002337 */
2338psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2339 const unsigned char *nonce,
2340 size_t nonce_length);
2341
Gilles Peskinebc59c852019-01-17 15:26:08 +01002342/** Declare the lengths of the message and additional data for AEAD.
2343 *
2344 * The application must call this function before calling
2345 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2346 * the operation requires it. If the algorithm does not require it,
2347 * calling this function is optional, but if this function is called
2348 * then the implementation must enforce the lengths.
2349 *
2350 * You may call this function before or after setting the nonce with
2351 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2352 *
2353 * - For #PSA_ALG_CCM, calling this function is required.
2354 * - For the other AEAD algorithms defined in this specification, calling
2355 * this function is not required.
2356 * - For vendor-defined algorithm, refer to the vendor documentation.
2357 *
2358 * \param[in,out] operation Active AEAD operation.
2359 * \param ad_length Size of the non-encrypted additional
2360 * authenticated data in bytes.
2361 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2362 *
2363 * \retval #PSA_SUCCESS
2364 * Success.
2365 * \retval #PSA_ERROR_BAD_STATE
2366 * The operation state is not valid (not set up, already completed,
2367 * or psa_aead_update_ad() or psa_aead_update() already called).
2368 * \retval #PSA_ERROR_INVALID_ARGUMENT
2369 * At least one of the lengths is not acceptable for the chosen
2370 * algorithm.
2371 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2372 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2373 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002374 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinebc59c852019-01-17 15:26:08 +01002375 */
2376psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2377 size_t ad_length,
2378 size_t plaintext_length);
2379
Gilles Peskine30a9e412019-01-14 18:36:12 +01002380/** Pass additional data to an active AEAD operation.
2381 *
2382 * Additional data is authenticated, but not encrypted.
2383 *
2384 * You may call this function multiple times to pass successive fragments
2385 * of the additional data. You may not call this function after passing
2386 * data to encrypt or decrypt with psa_aead_update().
2387 *
2388 * Before calling this function, you must:
2389 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2390 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2391 *
2392 * If this function returns an error status, the operation becomes inactive.
2393 *
2394 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2395 * there is no guarantee that the input is valid. Therefore, until
2396 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2397 * treat the input as untrusted and prepare to undo any action that
2398 * depends on the input if psa_aead_verify() returns an error status.
2399 *
2400 * \param[in,out] operation Active AEAD operation.
2401 * \param[in] input Buffer containing the fragment of
2402 * additional data.
2403 * \param input_length Size of the \p input buffer in bytes.
2404 *
2405 * \retval #PSA_SUCCESS
2406 * Success.
2407 * \retval #PSA_ERROR_BAD_STATE
2408 * The operation state is not valid (not set up, nonce not set,
2409 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002410 * \retval #PSA_ERROR_INVALID_ARGUMENT
2411 * The total input length overflows the additional data length that
2412 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002413 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2414 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2415 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002416 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002417 */
2418psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2419 const uint8_t *input,
2420 size_t input_length);
2421
2422/** Encrypt or decrypt a message fragment in an active AEAD operation.
2423 *
2424 * Before calling this function, you must:
2425 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2426 * The choice of setup function determines whether this function
2427 * encrypts or decrypts its input.
2428 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2429 * 3. Call psa_aead_update_ad() to pass all the additional data.
2430 *
2431 * If this function returns an error status, the operation becomes inactive.
2432 *
2433 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2434 * there is no guarantee that the input is valid. Therefore, until
2435 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2436 * - Do not use the output in any way other than storing it in a
2437 * confidential location. If you take any action that depends
2438 * on the tentative decrypted data, this action will need to be
2439 * undone if the input turns out not to be valid. Furthermore,
2440 * if an adversary can observe that this action took place
2441 * (for example through timing), they may be able to use this
2442 * fact as an oracle to decrypt any message encrypted with the
2443 * same key.
2444 * - In particular, do not copy the output anywhere but to a
2445 * memory or storage space that you have exclusive access to.
2446 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002447 * This function does not require the input to be aligned to any
2448 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002449 * a whole block at a time, it must consume all the input provided, but
2450 * it may delay the end of the corresponding output until a subsequent
2451 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2452 * provides sufficient input. The amount of data that can be delayed
2453 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002454 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002455 * \param[in,out] operation Active AEAD operation.
2456 * \param[in] input Buffer containing the message fragment to
2457 * encrypt or decrypt.
2458 * \param input_length Size of the \p input buffer in bytes.
2459 * \param[out] output Buffer where the output is to be written.
2460 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002461 * This must be at least
2462 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2463 * \p input_length) where \c alg is the
2464 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002465 * \param[out] output_length On success, the number of bytes
2466 * that make up the returned output.
2467 *
2468 * \retval #PSA_SUCCESS
2469 * Success.
2470 * \retval #PSA_ERROR_BAD_STATE
2471 * The operation state is not valid (not set up, nonce not set
2472 * or already completed).
2473 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2474 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002475 * You can determine a sufficient buffer size by calling
2476 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2477 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002478 * \retval #PSA_ERROR_INVALID_ARGUMENT
2479 * The total length of input to psa_aead_update_ad() so far is
2480 * less than the additional data length that was previously
2481 * specified with psa_aead_set_lengths().
2482 * \retval #PSA_ERROR_INVALID_ARGUMENT
2483 * The total input length overflows the plaintext length that
2484 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002485 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2486 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2487 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002488 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002489 */
2490psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2491 const uint8_t *input,
2492 size_t input_length,
2493 unsigned char *output,
2494 size_t output_size,
2495 size_t *output_length);
2496
2497/** Finish encrypting a message in an AEAD operation.
2498 *
2499 * The operation must have been set up with psa_aead_encrypt_setup().
2500 *
2501 * This function finishes the authentication of the additional data
2502 * formed by concatenating the inputs passed to preceding calls to
2503 * psa_aead_update_ad() with the plaintext formed by concatenating the
2504 * inputs passed to preceding calls to psa_aead_update().
2505 *
2506 * This function has two output buffers:
2507 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002508 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002509 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002510 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002511 * that the operation performs.
2512 *
2513 * When this function returns, the operation becomes inactive.
2514 *
2515 * \param[in,out] operation Active AEAD operation.
2516 * \param[out] ciphertext Buffer where the last part of the ciphertext
2517 * is to be written.
2518 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002519 * This must be at least
2520 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2521 * \c alg is the algorithm that is being
2522 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002523 * \param[out] ciphertext_length On success, the number of bytes of
2524 * returned ciphertext.
2525 * \param[out] tag Buffer where the authentication tag is
2526 * to be written.
2527 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002528 * This must be at least
2529 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2530 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002531 * \param[out] tag_length On success, the number of bytes
2532 * that make up the returned tag.
2533 *
2534 * \retval #PSA_SUCCESS
2535 * Success.
2536 * \retval #PSA_ERROR_BAD_STATE
2537 * The operation state is not valid (not set up, nonce not set,
2538 * decryption, or already completed).
2539 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002540 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002541 * You can determine a sufficient buffer size for \p ciphertext by
2542 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2543 * where \c alg is the algorithm that is being calculated.
2544 * You can determine a sufficient buffer size for \p tag by
2545 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002546 * \retval #PSA_ERROR_INVALID_ARGUMENT
2547 * The total length of input to psa_aead_update_ad() so far is
2548 * less than the additional data length that was previously
2549 * specified with psa_aead_set_lengths().
2550 * \retval #PSA_ERROR_INVALID_ARGUMENT
2551 * The total length of input to psa_aead_update() so far is
2552 * less than the plaintext length that was previously
2553 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002554 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2555 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2556 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002557 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002558 */
2559psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002560 uint8_t *ciphertext,
2561 size_t ciphertext_size,
2562 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002563 uint8_t *tag,
2564 size_t tag_size,
2565 size_t *tag_length);
2566
2567/** Finish authenticating and decrypting a message in an AEAD operation.
2568 *
2569 * The operation must have been set up with psa_aead_decrypt_setup().
2570 *
2571 * This function finishes the authentication of the additional data
2572 * formed by concatenating the inputs passed to preceding calls to
2573 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2574 * inputs passed to preceding calls to psa_aead_update().
2575 *
2576 * When this function returns, the operation becomes inactive.
2577 *
2578 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002579 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002580 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002581 * from previous calls to psa_aead_update()
2582 * that could not be processed until the end
2583 * of the input.
2584 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002585 * This must be at least
2586 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2587 * \c alg is the algorithm that is being
2588 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002589 * \param[out] plaintext_length On success, the number of bytes of
2590 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002591 * \param[in] tag Buffer containing the authentication tag.
2592 * \param tag_length Size of the \p tag buffer in bytes.
2593 *
2594 * \retval #PSA_SUCCESS
2595 * Success.
2596 * \retval #PSA_ERROR_BAD_STATE
2597 * The operation state is not valid (not set up, nonce not set,
2598 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002599 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2600 * The size of the \p plaintext buffer is too small.
2601 * You can determine a sufficient buffer size for \p plaintext by
2602 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2603 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002604 * \retval #PSA_ERROR_INVALID_ARGUMENT
2605 * The total length of input to psa_aead_update_ad() so far is
2606 * less than the additional data length that was previously
2607 * specified with psa_aead_set_lengths().
2608 * \retval #PSA_ERROR_INVALID_ARGUMENT
2609 * The total length of input to psa_aead_update() so far is
2610 * less than the plaintext length that was previously
2611 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002612 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2613 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2614 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002615 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002616 */
2617psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002618 uint8_t *plaintext,
2619 size_t plaintext_size,
2620 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002621 const uint8_t *tag,
2622 size_t tag_length);
2623
2624/** Abort an AEAD operation.
2625 *
2626 * Aborting an operation frees all associated resources except for the
2627 * \p operation structure itself. Once aborted, the operation object
2628 * can be reused for another operation by calling
2629 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2630 *
2631 * You may call this function any time after the operation object has
2632 * been initialized by any of the following methods:
2633 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2634 * whether it succeeds or not.
2635 * - Initializing the \c struct to all-bits-zero.
2636 * - Initializing the \c struct to logical zeros, e.g.
2637 * `psa_aead_operation_t operation = {0}`.
2638 *
2639 * In particular, calling psa_aead_abort() after the operation has been
2640 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2641 * is safe and has no effect.
2642 *
2643 * \param[in,out] operation Initialized AEAD operation.
2644 *
2645 * \retval #PSA_SUCCESS
2646 * \retval #PSA_ERROR_BAD_STATE
2647 * \p operation is not an active AEAD operation.
2648 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2649 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002650 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002651 */
2652psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2653
Gilles Peskine3b555712018-03-03 21:27:57 +01002654/**@}*/
2655
Gilles Peskine20035e32018-02-03 22:44:14 +01002656/** \defgroup asymmetric Asymmetric cryptography
2657 * @{
2658 */
2659
2660/**
2661 * \brief Sign a hash or short message with a private key.
2662 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002663 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002664 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002665 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2666 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2667 * to determine the hash algorithm to use.
2668 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002669 * \param handle Handle to the key to use for the operation.
2670 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002671 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002672 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002673 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002674 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002675 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002676 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002677 * \param[out] signature_length On success, the number of bytes
2678 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002679 *
Gilles Peskine28538492018-07-11 17:34:00 +02002680 * \retval #PSA_SUCCESS
2681 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002682 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002683 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002684 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002685 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002686 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002687 * \retval #PSA_ERROR_NOT_SUPPORTED
2688 * \retval #PSA_ERROR_INVALID_ARGUMENT
2689 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2690 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2691 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002692 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002693 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002694 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002695 * The library has not been previously initialized by psa_crypto_init().
2696 * It is implementation-dependent whether a failure to initialize
2697 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002698 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002699psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002700 psa_algorithm_t alg,
2701 const uint8_t *hash,
2702 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002703 uint8_t *signature,
2704 size_t signature_size,
2705 size_t *signature_length);
2706
2707/**
2708 * \brief Verify the signature a hash or short message using a public key.
2709 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002710 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002711 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002712 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2713 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2714 * to determine the hash algorithm to use.
2715 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002716 * \param handle Handle to the key to use for the operation.
2717 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002718 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002719 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002720 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002721 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002722 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002723 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002724 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002725 *
Gilles Peskine28538492018-07-11 17:34:00 +02002726 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002727 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002728 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002729 * The calculation was perfomed successfully, but the passed
2730 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002731 * \retval #PSA_ERROR_NOT_SUPPORTED
2732 * \retval #PSA_ERROR_INVALID_ARGUMENT
2733 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2734 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2735 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002736 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002737 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002738 * The library has not been previously initialized by psa_crypto_init().
2739 * It is implementation-dependent whether a failure to initialize
2740 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002741 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002742psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002743 psa_algorithm_t alg,
2744 const uint8_t *hash,
2745 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002746 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002747 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002748
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002749/**
2750 * \brief Encrypt a short message with a public key.
2751 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002752 * \param handle Handle to the key to use for the operation.
2753 * It must be a public key or an asymmetric
2754 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002755 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002756 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002757 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002758 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002759 * \param[in] salt A salt or label, if supported by the
2760 * encryption algorithm.
2761 * If the algorithm does not support a
2762 * salt, pass \c NULL.
2763 * If the algorithm supports an optional
2764 * salt and you do not want to pass a salt,
2765 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002766 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002767 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2768 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002769 * \param salt_length Size of the \p salt buffer in bytes.
2770 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002771 * \param[out] output Buffer where the encrypted message is to
2772 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002773 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002774 * \param[out] output_length On success, the number of bytes
2775 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002776 *
Gilles Peskine28538492018-07-11 17:34:00 +02002777 * \retval #PSA_SUCCESS
2778 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002779 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002780 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002781 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002782 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002783 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002784 * \retval #PSA_ERROR_NOT_SUPPORTED
2785 * \retval #PSA_ERROR_INVALID_ARGUMENT
2786 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2787 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2788 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002789 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002790 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002791 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002792 * The library has not been previously initialized by psa_crypto_init().
2793 * It is implementation-dependent whether a failure to initialize
2794 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002795 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002796psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002797 psa_algorithm_t alg,
2798 const uint8_t *input,
2799 size_t input_length,
2800 const uint8_t *salt,
2801 size_t salt_length,
2802 uint8_t *output,
2803 size_t output_size,
2804 size_t *output_length);
2805
2806/**
2807 * \brief Decrypt a short message with a private key.
2808 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002809 * \param handle Handle to the key to use for the operation.
2810 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002811 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002812 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002813 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002814 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002815 * \param[in] salt A salt or label, if supported by the
2816 * encryption algorithm.
2817 * If the algorithm does not support a
2818 * salt, pass \c NULL.
2819 * If the algorithm supports an optional
2820 * salt and you do not want to pass a salt,
2821 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002822 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002823 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2824 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002825 * \param salt_length Size of the \p salt buffer in bytes.
2826 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002827 * \param[out] output Buffer where the decrypted message is to
2828 * be written.
2829 * \param output_size Size of the \c output buffer in bytes.
2830 * \param[out] output_length On success, the number of bytes
2831 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002832 *
Gilles Peskine28538492018-07-11 17:34:00 +02002833 * \retval #PSA_SUCCESS
2834 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002835 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002836 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002837 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002838 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002839 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002840 * \retval #PSA_ERROR_NOT_SUPPORTED
2841 * \retval #PSA_ERROR_INVALID_ARGUMENT
2842 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2843 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2844 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002845 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002846 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2847 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002848 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002849 * The library has not been previously initialized by psa_crypto_init().
2850 * It is implementation-dependent whether a failure to initialize
2851 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002852 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002853psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002854 psa_algorithm_t alg,
2855 const uint8_t *input,
2856 size_t input_length,
2857 const uint8_t *salt,
2858 size_t salt_length,
2859 uint8_t *output,
2860 size_t output_size,
2861 size_t *output_length);
2862
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002863/**@}*/
2864
Gilles Peskine35675b62019-05-16 17:26:11 +02002865/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002866 * @{
2867 */
2868
Gilles Peskine35675b62019-05-16 17:26:11 +02002869/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002870 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002871 * Before calling any function on a key derivation operation object, the
2872 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002873 * - Set the structure to all-bits-zero, for example:
2874 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002875 * psa_key_derivation_operation_t operation;
2876 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002877 * \endcode
2878 * - Initialize the structure to logical zero values, for example:
2879 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002880 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002881 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002882 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002883 * for example:
2884 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002885 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002886 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002887 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002888 * to the structure, for example:
2889 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002890 * psa_key_derivation_operation_t operation;
2891 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002892 * \endcode
2893 *
2894 * This is an implementation-defined \c struct. Applications should not
2895 * make any assumptions about the content of this structure except
2896 * as directed by the documentation of a specific implementation.
2897 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02002898typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002899
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002900/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002901 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002902 * This macro returns a suitable initializer for a key derivation operation
2903 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002904 */
2905#ifdef __DOXYGEN_ONLY__
2906/* This is an example definition for documentation purposes.
2907 * Implementations should define a suitable value in `crypto_struct.h`.
2908 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002909#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02002910#endif
2911
Gilles Peskine35675b62019-05-16 17:26:11 +02002912/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002913 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002914static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002915
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002916/** Set up a key derivation operation.
2917 *
2918 * A key derivation algorithm takes some inputs and uses them to generate
2919 * a byte stream in a deterministic way.
2920 * This byte stream can be used to produce keys and other
2921 * cryptographic material.
2922 *
2923 * To derive a key:
2924 * - Start with an initialized object of type #psa_key_derivation_operation_t.
2925 * - Call psa_key_derivation_setup() to select the algorithm.
2926 * - Provide the inputs for the key derivation by calling
2927 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2928 * as appropriate. Which inputs are needed, in what order, and whether
2929 * they may be keys and if so of what type depends on the algorithm.
2930 * - Optionally set the operation's maximum capacity with
2931 * psa_key_derivation_set_capacity(). You may do this before, in the middle
2932 * of or after providing inputs. For some algorithms, this step is mandatory
2933 * because the output depends on the maximum capacity.
2934 * - To derive a key, call psa_key_derivation_output_key().
2935 * To derive a byte string for a different purpose, call
2936 * - psa_key_derivation_output_bytes().
2937 * Successive calls to these functions use successive output bytes
2938 * calculated by the key derivation algorithm.
2939 * - Clean up the key derivation operation object with
2940 * psa_key_derivation_abort().
2941 *
2942 * \param[in,out] operation The key derivation operation object
2943 * to set up. It must
2944 * have been initialized but not set up yet.
2945 * \param alg The key derivation algorithm to compute
2946 * (\c PSA_ALG_XXX value such that
2947 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2948 *
2949 * \retval #PSA_SUCCESS
2950 * Success.
2951 * \retval #PSA_ERROR_INVALID_ARGUMENT
2952 * \c alg is not a key derivation algorithm.
2953 * \retval #PSA_ERROR_NOT_SUPPORTED
2954 * \c alg is not supported or is not a key derivation algorithm.
2955 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2956 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2957 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002958 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002959 * \retval #PSA_ERROR_BAD_STATE
2960 */
2961psa_status_t psa_key_derivation_setup(
2962 psa_key_derivation_operation_t *operation,
2963 psa_algorithm_t alg);
2964
Gilles Peskine35675b62019-05-16 17:26:11 +02002965/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002966 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002967 * The capacity of a key derivation is the maximum number of bytes that it can
2968 * return. When you get *N* bytes of output from a key derivation operation,
2969 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002970 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002971 * \param[in] operation The operation to query.
2972 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002973 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002974 * \retval #PSA_SUCCESS
2975 * \retval #PSA_ERROR_BAD_STATE
2976 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002977 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002978psa_status_t psa_key_derivation_get_capacity(
2979 const psa_key_derivation_operation_t *operation,
2980 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002981
Gilles Peskine35675b62019-05-16 17:26:11 +02002982/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002983 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002984 * The capacity of a key derivation operation is the maximum number of bytes
2985 * that the key derivation operation can return from this point onwards.
2986 *
2987 * \param[in,out] operation The key derivation operation object to modify.
2988 * \param capacity The new capacity of the operation.
2989 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002990 * current capacity.
2991 *
2992 * \retval #PSA_SUCCESS
2993 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02002994 * \p capacity is larger than the operation's current capacity.
2995 * In this case, the operation object remains valid and its capacity
2996 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002997 * \retval #PSA_ERROR_BAD_STATE
2998 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2999 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003000psa_status_t psa_key_derivation_set_capacity(
3001 psa_key_derivation_operation_t *operation,
3002 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003003
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003004/** Use the maximum possible capacity for a key derivation operation.
3005 *
3006 * Use this value as the capacity argument when setting up a key derivation
3007 * to indicate that the operation should have the maximum possible capacity.
3008 * The value of the maximum possible capacity depends on the key derivation
3009 * algorithm.
3010 */
3011#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3012
3013/** Provide an input for key derivation or key agreement.
3014 *
3015 * Which inputs are required and in what order depends on the algorithm.
3016 * Refer to the documentation of each key derivation or key agreement
3017 * algorithm for information.
3018 *
3019 * This function passes direct inputs. Some inputs must be passed as keys
3020 * using psa_key_derivation_input_key() instead of this function. Refer to
3021 * the documentation of individual step types for information.
3022 *
3023 * \param[in,out] operation The key derivation operation object to use.
3024 * It must have been set up with
3025 * psa_key_derivation_setup() and must not
3026 * have produced any output yet.
3027 * \param step Which step the input data is for.
3028 * \param[in] data Input data to use.
3029 * \param data_length Size of the \p data buffer in bytes.
3030 *
3031 * \retval #PSA_SUCCESS
3032 * Success.
3033 * \retval #PSA_ERROR_INVALID_ARGUMENT
3034 * \c step is not compatible with the operation's algorithm.
3035 * \retval #PSA_ERROR_INVALID_ARGUMENT
3036 * \c step does not allow direct inputs.
3037 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3038 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3039 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003040 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003041 * \retval #PSA_ERROR_BAD_STATE
3042 * The value of \p step is not valid given the state of \p operation.
3043 * \retval #PSA_ERROR_BAD_STATE
3044 * The library has not been previously initialized by psa_crypto_init().
3045 * It is implementation-dependent whether a failure to initialize
3046 * results in this error code.
3047 */
3048psa_status_t psa_key_derivation_input_bytes(
3049 psa_key_derivation_operation_t *operation,
3050 psa_key_derivation_step_t step,
3051 const uint8_t *data,
3052 size_t data_length);
3053
3054/** Provide an input for key derivation in the form of a key.
3055 *
3056 * Which inputs are required and in what order depends on the algorithm.
3057 * Refer to the documentation of each key derivation or key agreement
3058 * algorithm for information.
3059 *
3060 * This function passes key inputs. Some inputs must be passed as keys
3061 * of the appropriate type using this function, while others must be
3062 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3063 * the documentation of individual step types for information.
3064 *
3065 * \param[in,out] operation The key derivation operation object to use.
3066 * It must have been set up with
3067 * psa_key_derivation_setup() and must not
3068 * have produced any output yet.
3069 * \param step Which step the input data is for.
3070 * \param handle Handle to the key. It must have an
3071 * appropriate type for \p step and must
3072 * allow the usage #PSA_KEY_USAGE_DERIVE.
3073 *
3074 * \retval #PSA_SUCCESS
3075 * Success.
3076 * \retval #PSA_ERROR_INVALID_HANDLE
3077 * \retval #PSA_ERROR_DOES_NOT_EXIST
3078 * \retval #PSA_ERROR_NOT_PERMITTED
3079 * \retval #PSA_ERROR_INVALID_ARGUMENT
3080 * \c step is not compatible with the operation's algorithm.
3081 * \retval #PSA_ERROR_INVALID_ARGUMENT
3082 * \c step does not allow key inputs.
3083 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3084 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3085 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003086 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003087 * \retval #PSA_ERROR_BAD_STATE
3088 * The value of \p step is not valid given the state of \p operation.
3089 * \retval #PSA_ERROR_BAD_STATE
3090 * The library has not been previously initialized by psa_crypto_init().
3091 * It is implementation-dependent whether a failure to initialize
3092 * results in this error code.
3093 */
3094psa_status_t psa_key_derivation_input_key(
3095 psa_key_derivation_operation_t *operation,
3096 psa_key_derivation_step_t step,
3097 psa_key_handle_t handle);
3098
3099/** Perform a key agreement and use the shared secret as input to a key
3100 * derivation.
3101 *
3102 * A key agreement algorithm takes two inputs: a private key \p private_key
3103 * a public key \p peer_key.
3104 * The result of this function is passed as input to a key derivation.
3105 * The output of this key derivation can be extracted by reading from the
3106 * resulting operation to produce keys and other cryptographic material.
3107 *
3108 * \param[in,out] operation The key derivation operation object to use.
3109 * It must have been set up with
3110 * psa_key_derivation_setup() with a
3111 * key agreement and derivation algorithm
3112 * \c alg (\c PSA_ALG_XXX value such that
3113 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3114 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3115 * is false).
3116 * The operation must be ready for an
3117 * input of the type given by \p step.
3118 * \param step Which step the input data is for.
3119 * \param private_key Handle to the private key to use.
3120 * \param[in] peer_key Public key of the peer. The peer key must be in the
3121 * same format that psa_import_key() accepts for the
3122 * public key type corresponding to the type of
3123 * private_key. That is, this function performs the
3124 * equivalent of
3125 * #psa_import_key(...,
3126 * `peer_key`, `peer_key_length`) where
3127 * with key attributes indicating the public key
3128 * type corresponding to the type of `private_key`.
3129 * For example, for EC keys, this means that peer_key
3130 * is interpreted as a point on the curve that the
3131 * private key is on. The standard formats for public
3132 * keys are documented in the documentation of
3133 * psa_export_public_key().
3134 * \param peer_key_length Size of \p peer_key in bytes.
3135 *
3136 * \retval #PSA_SUCCESS
3137 * Success.
3138 * \retval #PSA_ERROR_INVALID_HANDLE
3139 * \retval #PSA_ERROR_DOES_NOT_EXIST
3140 * \retval #PSA_ERROR_NOT_PERMITTED
3141 * \retval #PSA_ERROR_INVALID_ARGUMENT
3142 * \c private_key is not compatible with \c alg,
3143 * or \p peer_key is not valid for \c alg or not compatible with
3144 * \c private_key.
3145 * \retval #PSA_ERROR_NOT_SUPPORTED
3146 * \c alg is not supported or is not a key derivation algorithm.
3147 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3148 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3149 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003150 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003151 */
3152psa_status_t psa_key_derivation_key_agreement(
3153 psa_key_derivation_operation_t *operation,
3154 psa_key_derivation_step_t step,
3155 psa_key_handle_t private_key,
3156 const uint8_t *peer_key,
3157 size_t peer_key_length);
3158
Gilles Peskine35675b62019-05-16 17:26:11 +02003159/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003160 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003161 * This function calculates output bytes from a key derivation algorithm and
3162 * return those bytes.
3163 * If you view the key derivation's output as a stream of bytes, this
3164 * function destructively reads the requested number of bytes from the
3165 * stream.
3166 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003167 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003168 * \param[in,out] operation The key derivation operation object to read from.
3169 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003170 * \param output_length Number of bytes to output.
3171 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003172 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003173 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003174 * The operation's capacity was less than
3175 * \p output_length bytes. Note that in this case,
3176 * no output is written to the output buffer.
3177 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003178 * subsequent calls to this function will not
3179 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003180 * \retval #PSA_ERROR_BAD_STATE
3181 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3182 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3183 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003184 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003185 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003186psa_status_t psa_key_derivation_output_bytes(
3187 psa_key_derivation_operation_t *operation,
3188 uint8_t *output,
3189 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003190
Gilles Peskine35675b62019-05-16 17:26:11 +02003191/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003192 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003193 * This function calculates output bytes from a key derivation algorithm
3194 * and uses those bytes to generate a key deterministically.
3195 * If you view the key derivation's output as a stream of bytes, this
3196 * function destructively reads as many bytes as required from the
3197 * stream.
3198 * The operation's capacity decreases by the number of bytes read.
3199 *
3200 * How much output is produced and consumed from the operation, and how
3201 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003202 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003203 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003204 * of a given size, this function is functionally equivalent to
3205 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003206 * and passing the resulting output to #psa_import_key.
3207 * However, this function has a security benefit:
3208 * if the implementation provides an isolation boundary then
3209 * the key material is not exposed outside the isolation boundary.
3210 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003211 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003212 * The following key types defined in this specification follow this scheme:
3213 *
3214 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003215 * - #PSA_KEY_TYPE_ARC4;
3216 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003217 * - #PSA_KEY_TYPE_DERIVE;
3218 * - #PSA_KEY_TYPE_HMAC.
3219 *
3220 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003221 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003222 * Montgomery curve), this function always draws a byte string whose
3223 * length is determined by the curve, and sets the mandatory bits
3224 * accordingly. That is:
3225 *
3226 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3227 * and process it as specified in RFC 7748 &sect;5.
3228 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3229 * and process it as specified in RFC 7748 &sect;5.
3230 *
3231 * - For key types for which the key is represented by a single sequence of
3232 * \p bits bits with constraints as to which bit sequences are acceptable,
3233 * this function draws a byte string of length (\p bits / 8) bytes rounded
3234 * up to the nearest whole number of bytes. If the resulting byte string
3235 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3236 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003237 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003238 * for the output produced by psa_export_key().
3239 * The following key types defined in this specification follow this scheme:
3240 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003241 * - #PSA_KEY_TYPE_DES.
3242 * Force-set the parity bits, but discard forbidden weak keys.
3243 * For 2-key and 3-key triple-DES, the three keys are generated
3244 * successively (for example, for 3-key triple-DES,
3245 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3246 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003247 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003248 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003249 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003250 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003251 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003252 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003253 * Weierstrass curve).
3254 * For these key types, interpret the byte string as integer
3255 * in big-endian order. Discard it if it is not in the range
3256 * [0, *N* - 2] where *N* is the boundary of the private key domain
3257 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003258 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003259 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003260 * This method allows compliance to NIST standards, specifically
3261 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003262 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3263 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3264 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3265 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003266 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003267 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003268 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003269 * implementation-defined.
3270 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003271 * In all cases, the data that is read is discarded from the operation.
3272 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003273 *
Gilles Peskine20628592019-04-19 19:29:50 +02003274 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003275 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003276 * \param[out] handle On success, a handle to the newly created key.
3277 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003278 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003279 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003280 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003281 * If the key is persistent, the key material and the key's metadata
3282 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003283 * \retval #PSA_ERROR_ALREADY_EXISTS
3284 * This is an attempt to create a persistent key, and there is
3285 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003286 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003287 * There was not enough data to create the desired key.
3288 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003289 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003290 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003291 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003292 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003293 * implementation in general or in this particular location.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003294 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003295 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3296 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3297 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3298 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003299 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003300 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003301 * The library has not been previously initialized by psa_crypto_init().
3302 * It is implementation-dependent whether a failure to initialize
3303 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003304 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003305psa_status_t psa_key_derivation_output_key(
3306 const psa_key_attributes_t *attributes,
3307 psa_key_derivation_operation_t *operation,
3308 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003309
Gilles Peskine35675b62019-05-16 17:26:11 +02003310/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003311 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003312 * Once a key derivation operation has been aborted, its capacity is zero.
3313 * Aborting an operation frees all associated resources except for the
3314 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003315 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003316 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003317 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003318 * psa_key_derivation_operation_init() or a zero value. In particular,
3319 * it is valid to call psa_key_derivation_abort() twice, or to call
3320 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003321 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003322 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003323 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003324 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003325 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003326 * \retval #PSA_SUCCESS
3327 * \retval #PSA_ERROR_BAD_STATE
3328 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3329 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003330 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003331 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003332psa_status_t psa_key_derivation_abort(
3333 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003334
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003335/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003336 *
3337 * \warning The raw result of a key agreement algorithm such as finite-field
3338 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3339 * not be used directly as key material. It should instead be passed as
3340 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003341 * a key derivation, use psa_key_derivation_key_agreement() and other
3342 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003343 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003344 * \param alg The key agreement algorithm to compute
3345 * (\c PSA_ALG_XXX value such that
3346 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3347 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003348 * \param private_key Handle to the private key to use.
3349 * \param[in] peer_key Public key of the peer. It must be
3350 * in the same format that psa_import_key()
3351 * accepts. The standard formats for public
3352 * keys are documented in the documentation
3353 * of psa_export_public_key().
3354 * \param peer_key_length Size of \p peer_key in bytes.
3355 * \param[out] output Buffer where the decrypted message is to
3356 * be written.
3357 * \param output_size Size of the \c output buffer in bytes.
3358 * \param[out] output_length On success, the number of bytes
3359 * that make up the returned output.
3360 *
3361 * \retval #PSA_SUCCESS
3362 * Success.
3363 * \retval #PSA_ERROR_INVALID_HANDLE
3364 * \retval #PSA_ERROR_EMPTY_SLOT
3365 * \retval #PSA_ERROR_NOT_PERMITTED
3366 * \retval #PSA_ERROR_INVALID_ARGUMENT
3367 * \p alg is not a key agreement algorithm
3368 * \retval #PSA_ERROR_INVALID_ARGUMENT
3369 * \p private_key is not compatible with \p alg,
3370 * or \p peer_key is not valid for \p alg or not compatible with
3371 * \p private_key.
3372 * \retval #PSA_ERROR_NOT_SUPPORTED
3373 * \p alg is not a supported key agreement algorithm.
3374 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3375 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3376 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003377 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine769c7a62019-01-18 16:42:29 +01003378 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003379psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3380 psa_key_handle_t private_key,
3381 const uint8_t *peer_key,
3382 size_t peer_key_length,
3383 uint8_t *output,
3384 size_t output_size,
3385 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003386
Gilles Peskineea0fb492018-07-12 17:17:20 +02003387/**@}*/
3388
Gilles Peskineedd76872018-07-20 17:42:05 +02003389/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003390 * @{
3391 */
3392
3393/**
3394 * \brief Generate random bytes.
3395 *
3396 * \warning This function **can** fail! Callers MUST check the return status
3397 * and MUST NOT use the content of the output buffer if the return
3398 * status is not #PSA_SUCCESS.
3399 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003400 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003401 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003402 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003403 * \param output_size Number of bytes to generate and output.
3404 *
Gilles Peskine28538492018-07-11 17:34:00 +02003405 * \retval #PSA_SUCCESS
3406 * \retval #PSA_ERROR_NOT_SUPPORTED
3407 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3408 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3409 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003410 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003411 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003412 * The library has not been previously initialized by psa_crypto_init().
3413 * It is implementation-dependent whether a failure to initialize
3414 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003415 */
3416psa_status_t psa_generate_random(uint8_t *output,
3417 size_t output_size);
3418
3419/**
3420 * \brief Generate a key or key pair.
3421 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003422 * The key is generated randomly.
3423 * Its location, policy, type and size are taken from \p attributes.
3424 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003425 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003426 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003427 * the public exponent is 65537.
3428 * The modulus is a product of two probabilistic primes
3429 * between 2^{n-1} and 2^n where n is the bit size specified in the
3430 * attributes.
3431 *
Gilles Peskine20628592019-04-19 19:29:50 +02003432 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003433 * \param[out] handle On success, a handle to the newly created key.
3434 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003435 *
Gilles Peskine28538492018-07-11 17:34:00 +02003436 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003437 * Success.
3438 * If the key is persistent, the key material and the key's metadata
3439 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003440 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003441 * This is an attempt to create a persistent key, and there is
3442 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003443 * \retval #PSA_ERROR_NOT_SUPPORTED
3444 * \retval #PSA_ERROR_INVALID_ARGUMENT
3445 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3446 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3447 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3448 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003449 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003450 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003451 * The library has not been previously initialized by psa_crypto_init().
3452 * It is implementation-dependent whether a failure to initialize
3453 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003454 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003455psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003456 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003457
3458/**@}*/
3459
Gilles Peskinee59236f2018-01-27 23:32:46 +01003460#ifdef __cplusplus
3461}
3462#endif
3463
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003464/* The file "crypto_sizes.h" contains definitions for size calculation
3465 * macros whose definitions are implementation-specific. */
3466#include "crypto_sizes.h"
3467
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003468/* The file "crypto_struct.h" contains definitions for
3469 * implementation-specific structs that are declared above. */
3470#include "crypto_struct.h"
3471
3472/* The file "crypto_extra.h" contains vendor-specific definitions. This
3473 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003474#include "crypto_extra.h"
3475
3476#endif /* PSA_CRYPTO_H */